namei.c 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587
  1. /*
  2. * linux/fs/namei.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * Some corrections by tytso.
  8. */
  9. /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
  10. * lookup logic.
  11. */
  12. /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/export.h>
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/fs.h>
  19. #include <linux/namei.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/fsnotify.h>
  22. #include <linux/personality.h>
  23. #include <linux/security.h>
  24. #include <linux/ima.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/mount.h>
  27. #include <linux/audit.h>
  28. #include <linux/capability.h>
  29. #include <linux/file.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/device_cgroup.h>
  32. #include <linux/fs_struct.h>
  33. #include <linux/posix_acl.h>
  34. #include <linux/hash.h>
  35. #include <asm/uaccess.h>
  36. #include "internal.h"
  37. #include "mount.h"
  38. /* [Feb-1997 T. Schoebel-Theuer]
  39. * Fundamental changes in the pathname lookup mechanisms (namei)
  40. * were necessary because of omirr. The reason is that omirr needs
  41. * to know the _real_ pathname, not the user-supplied one, in case
  42. * of symlinks (and also when transname replacements occur).
  43. *
  44. * The new code replaces the old recursive symlink resolution with
  45. * an iterative one (in case of non-nested symlink chains). It does
  46. * this with calls to <fs>_follow_link().
  47. * As a side effect, dir_namei(), _namei() and follow_link() are now
  48. * replaced with a single function lookup_dentry() that can handle all
  49. * the special cases of the former code.
  50. *
  51. * With the new dcache, the pathname is stored at each inode, at least as
  52. * long as the refcount of the inode is positive. As a side effect, the
  53. * size of the dcache depends on the inode cache and thus is dynamic.
  54. *
  55. * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
  56. * resolution to correspond with current state of the code.
  57. *
  58. * Note that the symlink resolution is not *completely* iterative.
  59. * There is still a significant amount of tail- and mid- recursion in
  60. * the algorithm. Also, note that <fs>_readlink() is not used in
  61. * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
  62. * may return different results than <fs>_follow_link(). Many virtual
  63. * filesystems (including /proc) exhibit this behavior.
  64. */
  65. /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
  66. * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
  67. * and the name already exists in form of a symlink, try to create the new
  68. * name indicated by the symlink. The old code always complained that the
  69. * name already exists, due to not following the symlink even if its target
  70. * is nonexistent. The new semantics affects also mknod() and link() when
  71. * the name is a symlink pointing to a non-existent name.
  72. *
  73. * I don't know which semantics is the right one, since I have no access
  74. * to standards. But I found by trial that HP-UX 9.0 has the full "new"
  75. * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
  76. * "old" one. Personally, I think the new semantics is much more logical.
  77. * Note that "ln old new" where "new" is a symlink pointing to a non-existing
  78. * file does succeed in both HP-UX and SunOs, but not in Solaris
  79. * and in the old Linux semantics.
  80. */
  81. /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
  82. * semantics. See the comments in "open_namei" and "do_link" below.
  83. *
  84. * [10-Sep-98 Alan Modra] Another symlink change.
  85. */
  86. /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
  87. * inside the path - always follow.
  88. * in the last component in creation/removal/renaming - never follow.
  89. * if LOOKUP_FOLLOW passed - follow.
  90. * if the pathname has trailing slashes - follow.
  91. * otherwise - don't follow.
  92. * (applied in that order).
  93. *
  94. * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
  95. * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
  96. * During the 2.4 we need to fix the userland stuff depending on it -
  97. * hopefully we will be able to get rid of that wart in 2.5. So far only
  98. * XEmacs seems to be relying on it...
  99. */
  100. /*
  101. * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
  102. * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
  103. * any extra contention...
  104. */
  105. /* In order to reduce some races, while at the same time doing additional
  106. * checking and hopefully speeding things up, we copy filenames to the
  107. * kernel data space before using them..
  108. *
  109. * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
  110. * PATH_MAX includes the nul terminator --RR.
  111. */
  112. #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
  113. struct filename *
  114. getname_flags(const char __user *filename, int flags, int *empty)
  115. {
  116. struct filename *result;
  117. char *kname;
  118. int len;
  119. result = audit_reusename(filename);
  120. if (result)
  121. return result;
  122. result = __getname();
  123. if (unlikely(!result))
  124. return ERR_PTR(-ENOMEM);
  125. /*
  126. * First, try to embed the struct filename inside the names_cache
  127. * allocation
  128. */
  129. kname = (char *)result->iname;
  130. result->name = kname;
  131. len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
  132. if (unlikely(len < 0)) {
  133. __putname(result);
  134. return ERR_PTR(len);
  135. }
  136. /*
  137. * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
  138. * separate struct filename so we can dedicate the entire
  139. * names_cache allocation for the pathname, and re-do the copy from
  140. * userland.
  141. */
  142. if (unlikely(len == EMBEDDED_NAME_MAX)) {
  143. const size_t size = offsetof(struct filename, iname[1]);
  144. kname = (char *)result;
  145. /*
  146. * size is chosen that way we to guarantee that
  147. * result->iname[0] is within the same object and that
  148. * kname can't be equal to result->iname, no matter what.
  149. */
  150. result = kzalloc(size, GFP_KERNEL);
  151. if (unlikely(!result)) {
  152. __putname(kname);
  153. return ERR_PTR(-ENOMEM);
  154. }
  155. result->name = kname;
  156. len = strncpy_from_user(kname, filename, PATH_MAX);
  157. if (unlikely(len < 0)) {
  158. __putname(kname);
  159. kfree(result);
  160. return ERR_PTR(len);
  161. }
  162. if (unlikely(len == PATH_MAX)) {
  163. __putname(kname);
  164. kfree(result);
  165. return ERR_PTR(-ENAMETOOLONG);
  166. }
  167. }
  168. result->refcnt = 1;
  169. /* The empty path is special. */
  170. if (unlikely(!len)) {
  171. if (empty)
  172. *empty = 1;
  173. if (!(flags & LOOKUP_EMPTY)) {
  174. putname(result);
  175. return ERR_PTR(-ENOENT);
  176. }
  177. }
  178. result->uptr = filename;
  179. result->aname = NULL;
  180. audit_getname(result);
  181. return result;
  182. }
  183. struct filename *
  184. getname(const char __user * filename)
  185. {
  186. return getname_flags(filename, 0, NULL);
  187. }
  188. struct filename *
  189. getname_kernel(const char * filename)
  190. {
  191. struct filename *result;
  192. int len = strlen(filename) + 1;
  193. result = __getname();
  194. if (unlikely(!result))
  195. return ERR_PTR(-ENOMEM);
  196. if (len <= EMBEDDED_NAME_MAX) {
  197. result->name = (char *)result->iname;
  198. } else if (len <= PATH_MAX) {
  199. struct filename *tmp;
  200. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  201. if (unlikely(!tmp)) {
  202. __putname(result);
  203. return ERR_PTR(-ENOMEM);
  204. }
  205. tmp->name = (char *)result;
  206. result = tmp;
  207. } else {
  208. __putname(result);
  209. return ERR_PTR(-ENAMETOOLONG);
  210. }
  211. memcpy((char *)result->name, filename, len);
  212. result->uptr = NULL;
  213. result->aname = NULL;
  214. result->refcnt = 1;
  215. audit_getname(result);
  216. return result;
  217. }
  218. void putname(struct filename *name)
  219. {
  220. BUG_ON(name->refcnt <= 0);
  221. if (--name->refcnt > 0)
  222. return;
  223. if (name->name != name->iname) {
  224. __putname(name->name);
  225. kfree(name);
  226. } else
  227. __putname(name);
  228. }
  229. static int check_acl(struct inode *inode, int mask)
  230. {
  231. #ifdef CONFIG_FS_POSIX_ACL
  232. struct posix_acl *acl;
  233. if (mask & MAY_NOT_BLOCK) {
  234. acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
  235. if (!acl)
  236. return -EAGAIN;
  237. /* no ->get_acl() calls in RCU mode... */
  238. if (acl == ACL_NOT_CACHED)
  239. return -ECHILD;
  240. return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
  241. }
  242. acl = get_acl(inode, ACL_TYPE_ACCESS);
  243. if (IS_ERR(acl))
  244. return PTR_ERR(acl);
  245. if (acl) {
  246. int error = posix_acl_permission(inode, acl, mask);
  247. posix_acl_release(acl);
  248. return error;
  249. }
  250. #endif
  251. return -EAGAIN;
  252. }
  253. /*
  254. * This does the basic permission checking
  255. */
  256. static int acl_permission_check(struct inode *inode, int mask)
  257. {
  258. unsigned int mode = inode->i_mode;
  259. if (likely(uid_eq(current_fsuid(), inode->i_uid)))
  260. mode >>= 6;
  261. else {
  262. if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
  263. int error = check_acl(inode, mask);
  264. if (error != -EAGAIN)
  265. return error;
  266. }
  267. if (in_group_p(inode->i_gid))
  268. mode >>= 3;
  269. }
  270. /*
  271. * If the DACs are ok we don't need any capability check.
  272. */
  273. if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  274. return 0;
  275. return -EACCES;
  276. }
  277. /**
  278. * generic_permission - check for access rights on a Posix-like filesystem
  279. * @inode: inode to check access rights for
  280. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
  281. *
  282. * Used to check for read/write/execute permissions on a file.
  283. * We use "fsuid" for this, letting us set arbitrary permissions
  284. * for filesystem access without changing the "normal" uids which
  285. * are used for other things.
  286. *
  287. * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
  288. * request cannot be satisfied (eg. requires blocking or too much complexity).
  289. * It would then be called again in ref-walk mode.
  290. */
  291. int generic_permission(struct inode *inode, int mask)
  292. {
  293. int ret;
  294. /*
  295. * Do the basic permission checks.
  296. */
  297. ret = acl_permission_check(inode, mask);
  298. if (ret != -EACCES)
  299. return ret;
  300. if (S_ISDIR(inode->i_mode)) {
  301. /* DACs are overridable for directories */
  302. if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
  303. return 0;
  304. if (!(mask & MAY_WRITE))
  305. if (capable_wrt_inode_uidgid(inode,
  306. CAP_DAC_READ_SEARCH))
  307. return 0;
  308. return -EACCES;
  309. }
  310. /*
  311. * Read/write DACs are always overridable.
  312. * Executable DACs are overridable when there is
  313. * at least one exec bit set.
  314. */
  315. if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
  316. if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
  317. return 0;
  318. /*
  319. * Searching includes executable on directories, else just read.
  320. */
  321. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  322. if (mask == MAY_READ)
  323. if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
  324. return 0;
  325. return -EACCES;
  326. }
  327. EXPORT_SYMBOL(generic_permission);
  328. /*
  329. * We _really_ want to just do "generic_permission()" without
  330. * even looking at the inode->i_op values. So we keep a cache
  331. * flag in inode->i_opflags, that says "this has not special
  332. * permission function, use the fast case".
  333. */
  334. static inline int do_inode_permission(struct inode *inode, int mask)
  335. {
  336. if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
  337. if (likely(inode->i_op->permission))
  338. return inode->i_op->permission(inode, mask);
  339. /* This gets set once for the inode lifetime */
  340. spin_lock(&inode->i_lock);
  341. inode->i_opflags |= IOP_FASTPERM;
  342. spin_unlock(&inode->i_lock);
  343. }
  344. return generic_permission(inode, mask);
  345. }
  346. /**
  347. * __inode_permission - Check for access rights to a given inode
  348. * @inode: Inode to check permission on
  349. * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  350. *
  351. * Check for read/write/execute permissions on an inode.
  352. *
  353. * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
  354. *
  355. * This does not check for a read-only file system. You probably want
  356. * inode_permission().
  357. */
  358. int __inode_permission(struct inode *inode, int mask)
  359. {
  360. int retval;
  361. if (unlikely(mask & MAY_WRITE)) {
  362. /*
  363. * Nobody gets write access to an immutable file.
  364. */
  365. if (IS_IMMUTABLE(inode))
  366. return -EACCES;
  367. }
  368. retval = do_inode_permission(inode, mask);
  369. if (retval)
  370. return retval;
  371. retval = devcgroup_inode_permission(inode, mask);
  372. if (retval)
  373. return retval;
  374. return security_inode_permission(inode, mask);
  375. }
  376. EXPORT_SYMBOL(__inode_permission);
  377. /**
  378. * sb_permission - Check superblock-level permissions
  379. * @sb: Superblock of inode to check permission on
  380. * @inode: Inode to check permission on
  381. * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  382. *
  383. * Separate out file-system wide checks from inode-specific permission checks.
  384. */
  385. static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
  386. {
  387. if (unlikely(mask & MAY_WRITE)) {
  388. umode_t mode = inode->i_mode;
  389. /* Nobody gets write access to a read-only fs. */
  390. if ((sb->s_flags & MS_RDONLY) &&
  391. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  392. return -EROFS;
  393. }
  394. return 0;
  395. }
  396. /**
  397. * inode_permission - Check for access rights to a given inode
  398. * @inode: Inode to check permission on
  399. * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  400. *
  401. * Check for read/write/execute permissions on an inode. We use fs[ug]id for
  402. * this, letting us set arbitrary permissions for filesystem access without
  403. * changing the "normal" UIDs which are used for other things.
  404. *
  405. * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
  406. */
  407. int inode_permission(struct inode *inode, int mask)
  408. {
  409. int retval;
  410. retval = sb_permission(inode->i_sb, inode, mask);
  411. if (retval)
  412. return retval;
  413. return __inode_permission(inode, mask);
  414. }
  415. EXPORT_SYMBOL(inode_permission);
  416. /**
  417. * path_get - get a reference to a path
  418. * @path: path to get the reference to
  419. *
  420. * Given a path increment the reference count to the dentry and the vfsmount.
  421. */
  422. void path_get(const struct path *path)
  423. {
  424. mntget(path->mnt);
  425. dget(path->dentry);
  426. }
  427. EXPORT_SYMBOL(path_get);
  428. /**
  429. * path_put - put a reference to a path
  430. * @path: path to put the reference to
  431. *
  432. * Given a path decrement the reference count to the dentry and the vfsmount.
  433. */
  434. void path_put(const struct path *path)
  435. {
  436. dput(path->dentry);
  437. mntput(path->mnt);
  438. }
  439. EXPORT_SYMBOL(path_put);
  440. #define EMBEDDED_LEVELS 2
  441. struct nameidata {
  442. struct path path;
  443. struct qstr last;
  444. struct path root;
  445. struct inode *inode; /* path.dentry.d_inode */
  446. unsigned int flags;
  447. unsigned seq, m_seq;
  448. int last_type;
  449. unsigned depth;
  450. int total_link_count;
  451. struct saved {
  452. struct path link;
  453. void *cookie;
  454. const char *name;
  455. struct inode *inode;
  456. unsigned seq;
  457. } *stack, internal[EMBEDDED_LEVELS];
  458. struct filename *name;
  459. struct nameidata *saved;
  460. unsigned root_seq;
  461. int dfd;
  462. };
  463. static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
  464. {
  465. struct nameidata *old = current->nameidata;
  466. p->stack = p->internal;
  467. p->dfd = dfd;
  468. p->name = name;
  469. p->total_link_count = old ? old->total_link_count : 0;
  470. p->saved = old;
  471. current->nameidata = p;
  472. }
  473. static void restore_nameidata(void)
  474. {
  475. struct nameidata *now = current->nameidata, *old = now->saved;
  476. current->nameidata = old;
  477. if (old)
  478. old->total_link_count = now->total_link_count;
  479. if (now->stack != now->internal) {
  480. kfree(now->stack);
  481. now->stack = now->internal;
  482. }
  483. }
  484. static int __nd_alloc_stack(struct nameidata *nd)
  485. {
  486. struct saved *p;
  487. if (nd->flags & LOOKUP_RCU) {
  488. p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
  489. GFP_ATOMIC);
  490. if (unlikely(!p))
  491. return -ECHILD;
  492. } else {
  493. p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
  494. GFP_KERNEL);
  495. if (unlikely(!p))
  496. return -ENOMEM;
  497. }
  498. memcpy(p, nd->internal, sizeof(nd->internal));
  499. nd->stack = p;
  500. return 0;
  501. }
  502. static inline int nd_alloc_stack(struct nameidata *nd)
  503. {
  504. if (likely(nd->depth != EMBEDDED_LEVELS))
  505. return 0;
  506. if (likely(nd->stack != nd->internal))
  507. return 0;
  508. return __nd_alloc_stack(nd);
  509. }
  510. static void drop_links(struct nameidata *nd)
  511. {
  512. int i = nd->depth;
  513. while (i--) {
  514. struct saved *last = nd->stack + i;
  515. struct inode *inode = last->inode;
  516. if (last->cookie && inode->i_op->put_link) {
  517. inode->i_op->put_link(inode, last->cookie);
  518. last->cookie = NULL;
  519. }
  520. }
  521. }
  522. static void terminate_walk(struct nameidata *nd)
  523. {
  524. drop_links(nd);
  525. if (!(nd->flags & LOOKUP_RCU)) {
  526. int i;
  527. path_put(&nd->path);
  528. for (i = 0; i < nd->depth; i++)
  529. path_put(&nd->stack[i].link);
  530. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  531. path_put(&nd->root);
  532. nd->root.mnt = NULL;
  533. }
  534. } else {
  535. nd->flags &= ~LOOKUP_RCU;
  536. if (!(nd->flags & LOOKUP_ROOT))
  537. nd->root.mnt = NULL;
  538. rcu_read_unlock();
  539. }
  540. nd->depth = 0;
  541. }
  542. /* path_put is needed afterwards regardless of success or failure */
  543. static bool legitimize_path(struct nameidata *nd,
  544. struct path *path, unsigned seq)
  545. {
  546. int res = __legitimize_mnt(path->mnt, nd->m_seq);
  547. if (unlikely(res)) {
  548. if (res > 0)
  549. path->mnt = NULL;
  550. path->dentry = NULL;
  551. return false;
  552. }
  553. if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
  554. path->dentry = NULL;
  555. return false;
  556. }
  557. return !read_seqcount_retry(&path->dentry->d_seq, seq);
  558. }
  559. static bool legitimize_links(struct nameidata *nd)
  560. {
  561. int i;
  562. for (i = 0; i < nd->depth; i++) {
  563. struct saved *last = nd->stack + i;
  564. if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
  565. drop_links(nd);
  566. nd->depth = i + 1;
  567. return false;
  568. }
  569. }
  570. return true;
  571. }
  572. /*
  573. * Path walking has 2 modes, rcu-walk and ref-walk (see
  574. * Documentation/filesystems/path-lookup.txt). In situations when we can't
  575. * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
  576. * normal reference counts on dentries and vfsmounts to transition to rcu-walk
  577. * mode. Refcounts are grabbed at the last known good point before rcu-walk
  578. * got stuck, so ref-walk may continue from there. If this is not successful
  579. * (eg. a seqcount has changed), then failure is returned and it's up to caller
  580. * to restart the path walk from the beginning in ref-walk mode.
  581. */
  582. /**
  583. * unlazy_walk - try to switch to ref-walk mode.
  584. * @nd: nameidata pathwalk data
  585. * @dentry: child of nd->path.dentry or NULL
  586. * @seq: seq number to check dentry against
  587. * Returns: 0 on success, -ECHILD on failure
  588. *
  589. * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
  590. * for ref-walk mode. @dentry must be a path found by a do_lookup call on
  591. * @nd or NULL. Must be called from rcu-walk context.
  592. * Nothing should touch nameidata between unlazy_walk() failure and
  593. * terminate_walk().
  594. */
  595. static int unlazy_walk(struct nameidata *nd, struct dentry *dentry, unsigned seq)
  596. {
  597. struct dentry *parent = nd->path.dentry;
  598. BUG_ON(!(nd->flags & LOOKUP_RCU));
  599. nd->flags &= ~LOOKUP_RCU;
  600. if (unlikely(!legitimize_links(nd)))
  601. goto out2;
  602. if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
  603. goto out2;
  604. if (unlikely(!lockref_get_not_dead(&parent->d_lockref)))
  605. goto out1;
  606. /*
  607. * For a negative lookup, the lookup sequence point is the parents
  608. * sequence point, and it only needs to revalidate the parent dentry.
  609. *
  610. * For a positive lookup, we need to move both the parent and the
  611. * dentry from the RCU domain to be properly refcounted. And the
  612. * sequence number in the dentry validates *both* dentry counters,
  613. * since we checked the sequence number of the parent after we got
  614. * the child sequence number. So we know the parent must still
  615. * be valid if the child sequence number is still valid.
  616. */
  617. if (!dentry) {
  618. if (read_seqcount_retry(&parent->d_seq, nd->seq))
  619. goto out;
  620. BUG_ON(nd->inode != parent->d_inode);
  621. } else {
  622. if (!lockref_get_not_dead(&dentry->d_lockref))
  623. goto out;
  624. if (read_seqcount_retry(&dentry->d_seq, seq))
  625. goto drop_dentry;
  626. }
  627. /*
  628. * Sequence counts matched. Now make sure that the root is
  629. * still valid and get it if required.
  630. */
  631. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  632. if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq))) {
  633. rcu_read_unlock();
  634. dput(dentry);
  635. return -ECHILD;
  636. }
  637. }
  638. rcu_read_unlock();
  639. return 0;
  640. drop_dentry:
  641. rcu_read_unlock();
  642. dput(dentry);
  643. goto drop_root_mnt;
  644. out2:
  645. nd->path.mnt = NULL;
  646. out1:
  647. nd->path.dentry = NULL;
  648. out:
  649. rcu_read_unlock();
  650. drop_root_mnt:
  651. if (!(nd->flags & LOOKUP_ROOT))
  652. nd->root.mnt = NULL;
  653. return -ECHILD;
  654. }
  655. static int unlazy_link(struct nameidata *nd, struct path *link, unsigned seq)
  656. {
  657. if (unlikely(!legitimize_path(nd, link, seq))) {
  658. drop_links(nd);
  659. nd->depth = 0;
  660. nd->flags &= ~LOOKUP_RCU;
  661. nd->path.mnt = NULL;
  662. nd->path.dentry = NULL;
  663. if (!(nd->flags & LOOKUP_ROOT))
  664. nd->root.mnt = NULL;
  665. rcu_read_unlock();
  666. } else if (likely(unlazy_walk(nd, NULL, 0)) == 0) {
  667. return 0;
  668. }
  669. path_put(link);
  670. return -ECHILD;
  671. }
  672. static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
  673. {
  674. return dentry->d_op->d_revalidate(dentry, flags);
  675. }
  676. /**
  677. * complete_walk - successful completion of path walk
  678. * @nd: pointer nameidata
  679. *
  680. * If we had been in RCU mode, drop out of it and legitimize nd->path.
  681. * Revalidate the final result, unless we'd already done that during
  682. * the path walk or the filesystem doesn't ask for it. Return 0 on
  683. * success, -error on failure. In case of failure caller does not
  684. * need to drop nd->path.
  685. */
  686. static int complete_walk(struct nameidata *nd)
  687. {
  688. struct dentry *dentry = nd->path.dentry;
  689. int status;
  690. if (nd->flags & LOOKUP_RCU) {
  691. if (!(nd->flags & LOOKUP_ROOT))
  692. nd->root.mnt = NULL;
  693. if (unlikely(unlazy_walk(nd, NULL, 0)))
  694. return -ECHILD;
  695. }
  696. if (likely(!(nd->flags & LOOKUP_JUMPED)))
  697. return 0;
  698. if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
  699. return 0;
  700. status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
  701. if (status > 0)
  702. return 0;
  703. if (!status)
  704. status = -ESTALE;
  705. return status;
  706. }
  707. static void set_root(struct nameidata *nd)
  708. {
  709. get_fs_root(current->fs, &nd->root);
  710. }
  711. static void set_root_rcu(struct nameidata *nd)
  712. {
  713. struct fs_struct *fs = current->fs;
  714. unsigned seq;
  715. do {
  716. seq = read_seqcount_begin(&fs->seq);
  717. nd->root = fs->root;
  718. nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
  719. } while (read_seqcount_retry(&fs->seq, seq));
  720. }
  721. static void path_put_conditional(struct path *path, struct nameidata *nd)
  722. {
  723. dput(path->dentry);
  724. if (path->mnt != nd->path.mnt)
  725. mntput(path->mnt);
  726. }
  727. static inline void path_to_nameidata(const struct path *path,
  728. struct nameidata *nd)
  729. {
  730. if (!(nd->flags & LOOKUP_RCU)) {
  731. dput(nd->path.dentry);
  732. if (nd->path.mnt != path->mnt)
  733. mntput(nd->path.mnt);
  734. }
  735. nd->path.mnt = path->mnt;
  736. nd->path.dentry = path->dentry;
  737. }
  738. /*
  739. * Helper to directly jump to a known parsed path from ->follow_link,
  740. * caller must have taken a reference to path beforehand.
  741. */
  742. void nd_jump_link(struct path *path)
  743. {
  744. struct nameidata *nd = current->nameidata;
  745. path_put(&nd->path);
  746. nd->path = *path;
  747. nd->inode = nd->path.dentry->d_inode;
  748. nd->flags |= LOOKUP_JUMPED;
  749. }
  750. static inline void put_link(struct nameidata *nd)
  751. {
  752. struct saved *last = nd->stack + --nd->depth;
  753. struct inode *inode = last->inode;
  754. if (last->cookie && inode->i_op->put_link)
  755. inode->i_op->put_link(inode, last->cookie);
  756. if (!(nd->flags & LOOKUP_RCU))
  757. path_put(&last->link);
  758. }
  759. int sysctl_protected_symlinks __read_mostly = 0;
  760. int sysctl_protected_hardlinks __read_mostly = 0;
  761. /**
  762. * may_follow_link - Check symlink following for unsafe situations
  763. * @nd: nameidata pathwalk data
  764. *
  765. * In the case of the sysctl_protected_symlinks sysctl being enabled,
  766. * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
  767. * in a sticky world-writable directory. This is to protect privileged
  768. * processes from failing races against path names that may change out
  769. * from under them by way of other users creating malicious symlinks.
  770. * It will permit symlinks to be followed only when outside a sticky
  771. * world-writable directory, or when the uid of the symlink and follower
  772. * match, or when the directory owner matches the symlink's owner.
  773. *
  774. * Returns 0 if following the symlink is allowed, -ve on error.
  775. */
  776. static inline int may_follow_link(struct nameidata *nd)
  777. {
  778. const struct inode *inode;
  779. const struct inode *parent;
  780. if (!sysctl_protected_symlinks)
  781. return 0;
  782. /* Allowed if owner and follower match. */
  783. inode = nd->stack[0].inode;
  784. if (uid_eq(current_cred()->fsuid, inode->i_uid))
  785. return 0;
  786. /* Allowed if parent directory not sticky and world-writable. */
  787. parent = nd->path.dentry->d_inode;
  788. if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
  789. return 0;
  790. /* Allowed if parent directory and link owner match. */
  791. if (uid_eq(parent->i_uid, inode->i_uid))
  792. return 0;
  793. if (nd->flags & LOOKUP_RCU)
  794. return -ECHILD;
  795. audit_log_link_denied("follow_link", &nd->stack[0].link);
  796. return -EACCES;
  797. }
  798. /**
  799. * safe_hardlink_source - Check for safe hardlink conditions
  800. * @inode: the source inode to hardlink from
  801. *
  802. * Return false if at least one of the following conditions:
  803. * - inode is not a regular file
  804. * - inode is setuid
  805. * - inode is setgid and group-exec
  806. * - access failure for read and write
  807. *
  808. * Otherwise returns true.
  809. */
  810. static bool safe_hardlink_source(struct inode *inode)
  811. {
  812. umode_t mode = inode->i_mode;
  813. /* Special files should not get pinned to the filesystem. */
  814. if (!S_ISREG(mode))
  815. return false;
  816. /* Setuid files should not get pinned to the filesystem. */
  817. if (mode & S_ISUID)
  818. return false;
  819. /* Executable setgid files should not get pinned to the filesystem. */
  820. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
  821. return false;
  822. /* Hardlinking to unreadable or unwritable sources is dangerous. */
  823. if (inode_permission(inode, MAY_READ | MAY_WRITE))
  824. return false;
  825. return true;
  826. }
  827. /**
  828. * may_linkat - Check permissions for creating a hardlink
  829. * @link: the source to hardlink from
  830. *
  831. * Block hardlink when all of:
  832. * - sysctl_protected_hardlinks enabled
  833. * - fsuid does not match inode
  834. * - hardlink source is unsafe (see safe_hardlink_source() above)
  835. * - not CAP_FOWNER
  836. *
  837. * Returns 0 if successful, -ve on error.
  838. */
  839. static int may_linkat(struct path *link)
  840. {
  841. const struct cred *cred;
  842. struct inode *inode;
  843. if (!sysctl_protected_hardlinks)
  844. return 0;
  845. cred = current_cred();
  846. inode = link->dentry->d_inode;
  847. /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
  848. * otherwise, it must be a safe source.
  849. */
  850. if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
  851. capable(CAP_FOWNER))
  852. return 0;
  853. audit_log_link_denied("linkat", link);
  854. return -EPERM;
  855. }
  856. static __always_inline
  857. const char *get_link(struct nameidata *nd)
  858. {
  859. struct saved *last = nd->stack + nd->depth - 1;
  860. struct dentry *dentry = last->link.dentry;
  861. struct inode *inode = last->inode;
  862. int error;
  863. const char *res;
  864. if (!(nd->flags & LOOKUP_RCU)) {
  865. touch_atime(&last->link);
  866. cond_resched();
  867. } else if (atime_needs_update(&last->link, inode)) {
  868. if (unlikely(unlazy_walk(nd, NULL, 0)))
  869. return ERR_PTR(-ECHILD);
  870. touch_atime(&last->link);
  871. }
  872. error = security_inode_follow_link(dentry, inode,
  873. nd->flags & LOOKUP_RCU);
  874. if (unlikely(error))
  875. return ERR_PTR(error);
  876. nd->last_type = LAST_BIND;
  877. res = inode->i_link;
  878. if (!res) {
  879. if (nd->flags & LOOKUP_RCU) {
  880. if (unlikely(unlazy_walk(nd, NULL, 0)))
  881. return ERR_PTR(-ECHILD);
  882. }
  883. res = inode->i_op->follow_link(dentry, &last->cookie);
  884. if (IS_ERR_OR_NULL(res)) {
  885. last->cookie = NULL;
  886. return res;
  887. }
  888. }
  889. if (*res == '/') {
  890. if (nd->flags & LOOKUP_RCU) {
  891. struct dentry *d;
  892. if (!nd->root.mnt)
  893. set_root_rcu(nd);
  894. nd->path = nd->root;
  895. d = nd->path.dentry;
  896. nd->inode = d->d_inode;
  897. nd->seq = nd->root_seq;
  898. if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
  899. return ERR_PTR(-ECHILD);
  900. } else {
  901. if (!nd->root.mnt)
  902. set_root(nd);
  903. path_put(&nd->path);
  904. nd->path = nd->root;
  905. path_get(&nd->root);
  906. nd->inode = nd->path.dentry->d_inode;
  907. }
  908. nd->flags |= LOOKUP_JUMPED;
  909. while (unlikely(*++res == '/'))
  910. ;
  911. }
  912. if (!*res)
  913. res = NULL;
  914. return res;
  915. }
  916. /*
  917. * follow_up - Find the mountpoint of path's vfsmount
  918. *
  919. * Given a path, find the mountpoint of its source file system.
  920. * Replace @path with the path of the mountpoint in the parent mount.
  921. * Up is towards /.
  922. *
  923. * Return 1 if we went up a level and 0 if we were already at the
  924. * root.
  925. */
  926. int follow_up(struct path *path)
  927. {
  928. struct mount *mnt = real_mount(path->mnt);
  929. struct mount *parent;
  930. struct dentry *mountpoint;
  931. read_seqlock_excl(&mount_lock);
  932. parent = mnt->mnt_parent;
  933. if (parent == mnt) {
  934. read_sequnlock_excl(&mount_lock);
  935. return 0;
  936. }
  937. mntget(&parent->mnt);
  938. mountpoint = dget(mnt->mnt_mountpoint);
  939. read_sequnlock_excl(&mount_lock);
  940. dput(path->dentry);
  941. path->dentry = mountpoint;
  942. mntput(path->mnt);
  943. path->mnt = &parent->mnt;
  944. return 1;
  945. }
  946. EXPORT_SYMBOL(follow_up);
  947. /*
  948. * Perform an automount
  949. * - return -EISDIR to tell follow_managed() to stop and return the path we
  950. * were called with.
  951. */
  952. static int follow_automount(struct path *path, struct nameidata *nd,
  953. bool *need_mntput)
  954. {
  955. struct vfsmount *mnt;
  956. int err;
  957. if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
  958. return -EREMOTE;
  959. /* We don't want to mount if someone's just doing a stat -
  960. * unless they're stat'ing a directory and appended a '/' to
  961. * the name.
  962. *
  963. * We do, however, want to mount if someone wants to open or
  964. * create a file of any type under the mountpoint, wants to
  965. * traverse through the mountpoint or wants to open the
  966. * mounted directory. Also, autofs may mark negative dentries
  967. * as being automount points. These will need the attentions
  968. * of the daemon to instantiate them before they can be used.
  969. */
  970. if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
  971. LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
  972. path->dentry->d_inode)
  973. return -EISDIR;
  974. nd->total_link_count++;
  975. if (nd->total_link_count >= 40)
  976. return -ELOOP;
  977. mnt = path->dentry->d_op->d_automount(path);
  978. if (IS_ERR(mnt)) {
  979. /*
  980. * The filesystem is allowed to return -EISDIR here to indicate
  981. * it doesn't want to automount. For instance, autofs would do
  982. * this so that its userspace daemon can mount on this dentry.
  983. *
  984. * However, we can only permit this if it's a terminal point in
  985. * the path being looked up; if it wasn't then the remainder of
  986. * the path is inaccessible and we should say so.
  987. */
  988. if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT))
  989. return -EREMOTE;
  990. return PTR_ERR(mnt);
  991. }
  992. if (!mnt) /* mount collision */
  993. return 0;
  994. if (!*need_mntput) {
  995. /* lock_mount() may release path->mnt on error */
  996. mntget(path->mnt);
  997. *need_mntput = true;
  998. }
  999. err = finish_automount(mnt, path);
  1000. switch (err) {
  1001. case -EBUSY:
  1002. /* Someone else made a mount here whilst we were busy */
  1003. return 0;
  1004. case 0:
  1005. path_put(path);
  1006. path->mnt = mnt;
  1007. path->dentry = dget(mnt->mnt_root);
  1008. return 0;
  1009. default:
  1010. return err;
  1011. }
  1012. }
  1013. /*
  1014. * Handle a dentry that is managed in some way.
  1015. * - Flagged for transit management (autofs)
  1016. * - Flagged as mountpoint
  1017. * - Flagged as automount point
  1018. *
  1019. * This may only be called in refwalk mode.
  1020. *
  1021. * Serialization is taken care of in namespace.c
  1022. */
  1023. static int follow_managed(struct path *path, struct nameidata *nd)
  1024. {
  1025. struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
  1026. unsigned managed;
  1027. bool need_mntput = false;
  1028. int ret = 0;
  1029. /* Given that we're not holding a lock here, we retain the value in a
  1030. * local variable for each dentry as we look at it so that we don't see
  1031. * the components of that value change under us */
  1032. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  1033. managed &= DCACHE_MANAGED_DENTRY,
  1034. unlikely(managed != 0)) {
  1035. /* Allow the filesystem to manage the transit without i_mutex
  1036. * being held. */
  1037. if (managed & DCACHE_MANAGE_TRANSIT) {
  1038. BUG_ON(!path->dentry->d_op);
  1039. BUG_ON(!path->dentry->d_op->d_manage);
  1040. ret = path->dentry->d_op->d_manage(path->dentry, false);
  1041. if (ret < 0)
  1042. break;
  1043. }
  1044. /* Transit to a mounted filesystem. */
  1045. if (managed & DCACHE_MOUNTED) {
  1046. struct vfsmount *mounted = lookup_mnt(path);
  1047. if (mounted) {
  1048. dput(path->dentry);
  1049. if (need_mntput)
  1050. mntput(path->mnt);
  1051. path->mnt = mounted;
  1052. path->dentry = dget(mounted->mnt_root);
  1053. need_mntput = true;
  1054. continue;
  1055. }
  1056. /* Something is mounted on this dentry in another
  1057. * namespace and/or whatever was mounted there in this
  1058. * namespace got unmounted before lookup_mnt() could
  1059. * get it */
  1060. }
  1061. /* Handle an automount point */
  1062. if (managed & DCACHE_NEED_AUTOMOUNT) {
  1063. ret = follow_automount(path, nd, &need_mntput);
  1064. if (ret < 0)
  1065. break;
  1066. continue;
  1067. }
  1068. /* We didn't change the current path point */
  1069. break;
  1070. }
  1071. if (need_mntput && path->mnt == mnt)
  1072. mntput(path->mnt);
  1073. if (ret == -EISDIR)
  1074. ret = 0;
  1075. if (need_mntput)
  1076. nd->flags |= LOOKUP_JUMPED;
  1077. if (unlikely(ret < 0))
  1078. path_put_conditional(path, nd);
  1079. return ret;
  1080. }
  1081. int follow_down_one(struct path *path)
  1082. {
  1083. struct vfsmount *mounted;
  1084. mounted = lookup_mnt(path);
  1085. if (mounted) {
  1086. dput(path->dentry);
  1087. mntput(path->mnt);
  1088. path->mnt = mounted;
  1089. path->dentry = dget(mounted->mnt_root);
  1090. return 1;
  1091. }
  1092. return 0;
  1093. }
  1094. EXPORT_SYMBOL(follow_down_one);
  1095. static inline int managed_dentry_rcu(struct dentry *dentry)
  1096. {
  1097. return (dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
  1098. dentry->d_op->d_manage(dentry, true) : 0;
  1099. }
  1100. /*
  1101. * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
  1102. * we meet a managed dentry that would need blocking.
  1103. */
  1104. static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
  1105. struct inode **inode, unsigned *seqp)
  1106. {
  1107. for (;;) {
  1108. struct mount *mounted;
  1109. /*
  1110. * Don't forget we might have a non-mountpoint managed dentry
  1111. * that wants to block transit.
  1112. */
  1113. switch (managed_dentry_rcu(path->dentry)) {
  1114. case -ECHILD:
  1115. default:
  1116. return false;
  1117. case -EISDIR:
  1118. return true;
  1119. case 0:
  1120. break;
  1121. }
  1122. if (!d_mountpoint(path->dentry))
  1123. return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
  1124. mounted = __lookup_mnt(path->mnt, path->dentry);
  1125. if (!mounted)
  1126. break;
  1127. path->mnt = &mounted->mnt;
  1128. path->dentry = mounted->mnt.mnt_root;
  1129. nd->flags |= LOOKUP_JUMPED;
  1130. *seqp = read_seqcount_begin(&path->dentry->d_seq);
  1131. /*
  1132. * Update the inode too. We don't need to re-check the
  1133. * dentry sequence number here after this d_inode read,
  1134. * because a mount-point is always pinned.
  1135. */
  1136. *inode = path->dentry->d_inode;
  1137. }
  1138. return !read_seqretry(&mount_lock, nd->m_seq) &&
  1139. !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
  1140. }
  1141. static int follow_dotdot_rcu(struct nameidata *nd)
  1142. {
  1143. struct inode *inode = nd->inode;
  1144. if (!nd->root.mnt)
  1145. set_root_rcu(nd);
  1146. while (1) {
  1147. if (path_equal(&nd->path, &nd->root))
  1148. break;
  1149. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  1150. struct dentry *old = nd->path.dentry;
  1151. struct dentry *parent = old->d_parent;
  1152. unsigned seq;
  1153. inode = parent->d_inode;
  1154. seq = read_seqcount_begin(&parent->d_seq);
  1155. if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
  1156. return -ECHILD;
  1157. nd->path.dentry = parent;
  1158. nd->seq = seq;
  1159. break;
  1160. } else {
  1161. struct mount *mnt = real_mount(nd->path.mnt);
  1162. struct mount *mparent = mnt->mnt_parent;
  1163. struct dentry *mountpoint = mnt->mnt_mountpoint;
  1164. struct inode *inode2 = mountpoint->d_inode;
  1165. unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
  1166. if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
  1167. return -ECHILD;
  1168. if (&mparent->mnt == nd->path.mnt)
  1169. break;
  1170. /* we know that mountpoint was pinned */
  1171. nd->path.dentry = mountpoint;
  1172. nd->path.mnt = &mparent->mnt;
  1173. inode = inode2;
  1174. nd->seq = seq;
  1175. }
  1176. }
  1177. while (unlikely(d_mountpoint(nd->path.dentry))) {
  1178. struct mount *mounted;
  1179. mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
  1180. if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
  1181. return -ECHILD;
  1182. if (!mounted)
  1183. break;
  1184. nd->path.mnt = &mounted->mnt;
  1185. nd->path.dentry = mounted->mnt.mnt_root;
  1186. inode = nd->path.dentry->d_inode;
  1187. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  1188. }
  1189. nd->inode = inode;
  1190. return 0;
  1191. }
  1192. /*
  1193. * Follow down to the covering mount currently visible to userspace. At each
  1194. * point, the filesystem owning that dentry may be queried as to whether the
  1195. * caller is permitted to proceed or not.
  1196. */
  1197. int follow_down(struct path *path)
  1198. {
  1199. unsigned managed;
  1200. int ret;
  1201. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  1202. unlikely(managed & DCACHE_MANAGED_DENTRY)) {
  1203. /* Allow the filesystem to manage the transit without i_mutex
  1204. * being held.
  1205. *
  1206. * We indicate to the filesystem if someone is trying to mount
  1207. * something here. This gives autofs the chance to deny anyone
  1208. * other than its daemon the right to mount on its
  1209. * superstructure.
  1210. *
  1211. * The filesystem may sleep at this point.
  1212. */
  1213. if (managed & DCACHE_MANAGE_TRANSIT) {
  1214. BUG_ON(!path->dentry->d_op);
  1215. BUG_ON(!path->dentry->d_op->d_manage);
  1216. ret = path->dentry->d_op->d_manage(
  1217. path->dentry, false);
  1218. if (ret < 0)
  1219. return ret == -EISDIR ? 0 : ret;
  1220. }
  1221. /* Transit to a mounted filesystem. */
  1222. if (managed & DCACHE_MOUNTED) {
  1223. struct vfsmount *mounted = lookup_mnt(path);
  1224. if (!mounted)
  1225. break;
  1226. dput(path->dentry);
  1227. mntput(path->mnt);
  1228. path->mnt = mounted;
  1229. path->dentry = dget(mounted->mnt_root);
  1230. continue;
  1231. }
  1232. /* Don't handle automount points here */
  1233. break;
  1234. }
  1235. return 0;
  1236. }
  1237. EXPORT_SYMBOL(follow_down);
  1238. /*
  1239. * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
  1240. */
  1241. static void follow_mount(struct path *path)
  1242. {
  1243. while (d_mountpoint(path->dentry)) {
  1244. struct vfsmount *mounted = lookup_mnt(path);
  1245. if (!mounted)
  1246. break;
  1247. dput(path->dentry);
  1248. mntput(path->mnt);
  1249. path->mnt = mounted;
  1250. path->dentry = dget(mounted->mnt_root);
  1251. }
  1252. }
  1253. static void follow_dotdot(struct nameidata *nd)
  1254. {
  1255. if (!nd->root.mnt)
  1256. set_root(nd);
  1257. while(1) {
  1258. struct dentry *old = nd->path.dentry;
  1259. if (nd->path.dentry == nd->root.dentry &&
  1260. nd->path.mnt == nd->root.mnt) {
  1261. break;
  1262. }
  1263. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  1264. /* rare case of legitimate dget_parent()... */
  1265. nd->path.dentry = dget_parent(nd->path.dentry);
  1266. dput(old);
  1267. break;
  1268. }
  1269. if (!follow_up(&nd->path))
  1270. break;
  1271. }
  1272. follow_mount(&nd->path);
  1273. nd->inode = nd->path.dentry->d_inode;
  1274. }
  1275. /*
  1276. * This looks up the name in dcache, possibly revalidates the old dentry and
  1277. * allocates a new one if not found or not valid. In the need_lookup argument
  1278. * returns whether i_op->lookup is necessary.
  1279. *
  1280. * dir->d_inode->i_mutex must be held
  1281. */
  1282. static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
  1283. unsigned int flags, bool *need_lookup)
  1284. {
  1285. struct dentry *dentry;
  1286. int error;
  1287. *need_lookup = false;
  1288. dentry = d_lookup(dir, name);
  1289. if (dentry) {
  1290. if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
  1291. error = d_revalidate(dentry, flags);
  1292. if (unlikely(error <= 0)) {
  1293. if (error < 0) {
  1294. dput(dentry);
  1295. return ERR_PTR(error);
  1296. } else {
  1297. d_invalidate(dentry);
  1298. dput(dentry);
  1299. dentry = NULL;
  1300. }
  1301. }
  1302. }
  1303. }
  1304. if (!dentry) {
  1305. dentry = d_alloc(dir, name);
  1306. if (unlikely(!dentry))
  1307. return ERR_PTR(-ENOMEM);
  1308. *need_lookup = true;
  1309. }
  1310. return dentry;
  1311. }
  1312. /*
  1313. * Call i_op->lookup on the dentry. The dentry must be negative and
  1314. * unhashed.
  1315. *
  1316. * dir->d_inode->i_mutex must be held
  1317. */
  1318. static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
  1319. unsigned int flags)
  1320. {
  1321. struct dentry *old;
  1322. /* Don't create child dentry for a dead directory. */
  1323. if (unlikely(IS_DEADDIR(dir))) {
  1324. dput(dentry);
  1325. return ERR_PTR(-ENOENT);
  1326. }
  1327. old = dir->i_op->lookup(dir, dentry, flags);
  1328. if (unlikely(old)) {
  1329. dput(dentry);
  1330. dentry = old;
  1331. }
  1332. return dentry;
  1333. }
  1334. static struct dentry *__lookup_hash(struct qstr *name,
  1335. struct dentry *base, unsigned int flags)
  1336. {
  1337. bool need_lookup;
  1338. struct dentry *dentry;
  1339. dentry = lookup_dcache(name, base, flags, &need_lookup);
  1340. if (!need_lookup)
  1341. return dentry;
  1342. return lookup_real(base->d_inode, dentry, flags);
  1343. }
  1344. /*
  1345. * It's more convoluted than I'd like it to be, but... it's still fairly
  1346. * small and for now I'd prefer to have fast path as straight as possible.
  1347. * It _is_ time-critical.
  1348. */
  1349. static int lookup_fast(struct nameidata *nd,
  1350. struct path *path, struct inode **inode,
  1351. unsigned *seqp)
  1352. {
  1353. struct vfsmount *mnt = nd->path.mnt;
  1354. struct dentry *dentry, *parent = nd->path.dentry;
  1355. int need_reval = 1;
  1356. int status = 1;
  1357. int err;
  1358. /*
  1359. * Rename seqlock is not required here because in the off chance
  1360. * of a false negative due to a concurrent rename, we're going to
  1361. * do the non-racy lookup, below.
  1362. */
  1363. if (nd->flags & LOOKUP_RCU) {
  1364. unsigned seq;
  1365. bool negative;
  1366. dentry = __d_lookup_rcu(parent, &nd->last, &seq);
  1367. if (!dentry)
  1368. goto unlazy;
  1369. /*
  1370. * This sequence count validates that the inode matches
  1371. * the dentry name information from lookup.
  1372. */
  1373. *inode = d_backing_inode(dentry);
  1374. negative = d_is_negative(dentry);
  1375. if (read_seqcount_retry(&dentry->d_seq, seq))
  1376. return -ECHILD;
  1377. if (negative)
  1378. return -ENOENT;
  1379. /*
  1380. * This sequence count validates that the parent had no
  1381. * changes while we did the lookup of the dentry above.
  1382. *
  1383. * The memory barrier in read_seqcount_begin of child is
  1384. * enough, we can use __read_seqcount_retry here.
  1385. */
  1386. if (__read_seqcount_retry(&parent->d_seq, nd->seq))
  1387. return -ECHILD;
  1388. *seqp = seq;
  1389. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1390. status = d_revalidate(dentry, nd->flags);
  1391. if (unlikely(status <= 0)) {
  1392. if (status != -ECHILD)
  1393. need_reval = 0;
  1394. goto unlazy;
  1395. }
  1396. }
  1397. path->mnt = mnt;
  1398. path->dentry = dentry;
  1399. if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
  1400. return 0;
  1401. unlazy:
  1402. if (unlazy_walk(nd, dentry, seq))
  1403. return -ECHILD;
  1404. } else {
  1405. dentry = __d_lookup(parent, &nd->last);
  1406. }
  1407. if (unlikely(!dentry))
  1408. goto need_lookup;
  1409. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
  1410. status = d_revalidate(dentry, nd->flags);
  1411. if (unlikely(status <= 0)) {
  1412. if (status < 0) {
  1413. dput(dentry);
  1414. return status;
  1415. }
  1416. d_invalidate(dentry);
  1417. dput(dentry);
  1418. goto need_lookup;
  1419. }
  1420. if (unlikely(d_is_negative(dentry))) {
  1421. dput(dentry);
  1422. return -ENOENT;
  1423. }
  1424. path->mnt = mnt;
  1425. path->dentry = dentry;
  1426. err = follow_managed(path, nd);
  1427. if (likely(!err))
  1428. *inode = d_backing_inode(path->dentry);
  1429. return err;
  1430. need_lookup:
  1431. return 1;
  1432. }
  1433. /* Fast lookup failed, do it the slow way */
  1434. static int lookup_slow(struct nameidata *nd, struct path *path)
  1435. {
  1436. struct dentry *dentry, *parent;
  1437. parent = nd->path.dentry;
  1438. BUG_ON(nd->inode != parent->d_inode);
  1439. mutex_lock(&parent->d_inode->i_mutex);
  1440. dentry = __lookup_hash(&nd->last, parent, nd->flags);
  1441. mutex_unlock(&parent->d_inode->i_mutex);
  1442. if (IS_ERR(dentry))
  1443. return PTR_ERR(dentry);
  1444. path->mnt = nd->path.mnt;
  1445. path->dentry = dentry;
  1446. return follow_managed(path, nd);
  1447. }
  1448. static inline int may_lookup(struct nameidata *nd)
  1449. {
  1450. if (nd->flags & LOOKUP_RCU) {
  1451. int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
  1452. if (err != -ECHILD)
  1453. return err;
  1454. if (unlazy_walk(nd, NULL, 0))
  1455. return -ECHILD;
  1456. }
  1457. return inode_permission(nd->inode, MAY_EXEC);
  1458. }
  1459. static inline int handle_dots(struct nameidata *nd, int type)
  1460. {
  1461. if (type == LAST_DOTDOT) {
  1462. if (nd->flags & LOOKUP_RCU) {
  1463. return follow_dotdot_rcu(nd);
  1464. } else
  1465. follow_dotdot(nd);
  1466. }
  1467. return 0;
  1468. }
  1469. static int pick_link(struct nameidata *nd, struct path *link,
  1470. struct inode *inode, unsigned seq)
  1471. {
  1472. int error;
  1473. struct saved *last;
  1474. if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) {
  1475. path_to_nameidata(link, nd);
  1476. return -ELOOP;
  1477. }
  1478. if (!(nd->flags & LOOKUP_RCU)) {
  1479. if (link->mnt == nd->path.mnt)
  1480. mntget(link->mnt);
  1481. }
  1482. error = nd_alloc_stack(nd);
  1483. if (unlikely(error)) {
  1484. if (error == -ECHILD) {
  1485. if (unlikely(unlazy_link(nd, link, seq)))
  1486. return -ECHILD;
  1487. error = nd_alloc_stack(nd);
  1488. }
  1489. if (error) {
  1490. path_put(link);
  1491. return error;
  1492. }
  1493. }
  1494. last = nd->stack + nd->depth++;
  1495. last->link = *link;
  1496. last->cookie = NULL;
  1497. last->inode = inode;
  1498. last->seq = seq;
  1499. return 1;
  1500. }
  1501. /*
  1502. * Do we need to follow links? We _really_ want to be able
  1503. * to do this check without having to look at inode->i_op,
  1504. * so we keep a cache of "no, this doesn't need follow_link"
  1505. * for the common case.
  1506. */
  1507. static inline int should_follow_link(struct nameidata *nd, struct path *link,
  1508. int follow,
  1509. struct inode *inode, unsigned seq)
  1510. {
  1511. if (likely(!d_is_symlink(link->dentry)))
  1512. return 0;
  1513. if (!follow)
  1514. return 0;
  1515. return pick_link(nd, link, inode, seq);
  1516. }
  1517. enum {WALK_GET = 1, WALK_PUT = 2};
  1518. static int walk_component(struct nameidata *nd, int flags)
  1519. {
  1520. struct path path;
  1521. struct inode *inode;
  1522. unsigned seq;
  1523. int err;
  1524. /*
  1525. * "." and ".." are special - ".." especially so because it has
  1526. * to be able to know about the current root directory and
  1527. * parent relationships.
  1528. */
  1529. if (unlikely(nd->last_type != LAST_NORM)) {
  1530. err = handle_dots(nd, nd->last_type);
  1531. if (flags & WALK_PUT)
  1532. put_link(nd);
  1533. return err;
  1534. }
  1535. err = lookup_fast(nd, &path, &inode, &seq);
  1536. if (unlikely(err)) {
  1537. if (err < 0)
  1538. return err;
  1539. err = lookup_slow(nd, &path);
  1540. if (err < 0)
  1541. return err;
  1542. inode = d_backing_inode(path.dentry);
  1543. seq = 0; /* we are already out of RCU mode */
  1544. err = -ENOENT;
  1545. if (d_is_negative(path.dentry))
  1546. goto out_path_put;
  1547. }
  1548. if (flags & WALK_PUT)
  1549. put_link(nd);
  1550. err = should_follow_link(nd, &path, flags & WALK_GET, inode, seq);
  1551. if (unlikely(err))
  1552. return err;
  1553. path_to_nameidata(&path, nd);
  1554. nd->inode = inode;
  1555. nd->seq = seq;
  1556. return 0;
  1557. out_path_put:
  1558. path_to_nameidata(&path, nd);
  1559. return err;
  1560. }
  1561. /*
  1562. * We can do the critical dentry name comparison and hashing
  1563. * operations one word at a time, but we are limited to:
  1564. *
  1565. * - Architectures with fast unaligned word accesses. We could
  1566. * do a "get_unaligned()" if this helps and is sufficiently
  1567. * fast.
  1568. *
  1569. * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
  1570. * do not trap on the (extremely unlikely) case of a page
  1571. * crossing operation.
  1572. *
  1573. * - Furthermore, we need an efficient 64-bit compile for the
  1574. * 64-bit case in order to generate the "number of bytes in
  1575. * the final mask". Again, that could be replaced with a
  1576. * efficient population count instruction or similar.
  1577. */
  1578. #ifdef CONFIG_DCACHE_WORD_ACCESS
  1579. #include <asm/word-at-a-time.h>
  1580. #ifdef CONFIG_64BIT
  1581. static inline unsigned int fold_hash(unsigned long hash)
  1582. {
  1583. return hash_64(hash, 32);
  1584. }
  1585. #else /* 32-bit case */
  1586. #define fold_hash(x) (x)
  1587. #endif
  1588. unsigned int full_name_hash(const unsigned char *name, unsigned int len)
  1589. {
  1590. unsigned long a, mask;
  1591. unsigned long hash = 0;
  1592. for (;;) {
  1593. a = load_unaligned_zeropad(name);
  1594. if (len < sizeof(unsigned long))
  1595. break;
  1596. hash += a;
  1597. hash *= 9;
  1598. name += sizeof(unsigned long);
  1599. len -= sizeof(unsigned long);
  1600. if (!len)
  1601. goto done;
  1602. }
  1603. mask = bytemask_from_count(len);
  1604. hash += mask & a;
  1605. done:
  1606. return fold_hash(hash);
  1607. }
  1608. EXPORT_SYMBOL(full_name_hash);
  1609. /*
  1610. * Calculate the length and hash of the path component, and
  1611. * return the "hash_len" as the result.
  1612. */
  1613. static inline u64 hash_name(const char *name)
  1614. {
  1615. unsigned long a, b, adata, bdata, mask, hash, len;
  1616. const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
  1617. hash = a = 0;
  1618. len = -sizeof(unsigned long);
  1619. do {
  1620. hash = (hash + a) * 9;
  1621. len += sizeof(unsigned long);
  1622. a = load_unaligned_zeropad(name+len);
  1623. b = a ^ REPEAT_BYTE('/');
  1624. } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
  1625. adata = prep_zero_mask(a, adata, &constants);
  1626. bdata = prep_zero_mask(b, bdata, &constants);
  1627. mask = create_zero_mask(adata | bdata);
  1628. hash += a & zero_bytemask(mask);
  1629. len += find_zero(mask);
  1630. return hashlen_create(fold_hash(hash), len);
  1631. }
  1632. #else
  1633. unsigned int full_name_hash(const unsigned char *name, unsigned int len)
  1634. {
  1635. unsigned long hash = init_name_hash();
  1636. while (len--)
  1637. hash = partial_name_hash(*name++, hash);
  1638. return end_name_hash(hash);
  1639. }
  1640. EXPORT_SYMBOL(full_name_hash);
  1641. /*
  1642. * We know there's a real path component here of at least
  1643. * one character.
  1644. */
  1645. static inline u64 hash_name(const char *name)
  1646. {
  1647. unsigned long hash = init_name_hash();
  1648. unsigned long len = 0, c;
  1649. c = (unsigned char)*name;
  1650. do {
  1651. len++;
  1652. hash = partial_name_hash(c, hash);
  1653. c = (unsigned char)name[len];
  1654. } while (c && c != '/');
  1655. return hashlen_create(end_name_hash(hash), len);
  1656. }
  1657. #endif
  1658. /*
  1659. * Name resolution.
  1660. * This is the basic name resolution function, turning a pathname into
  1661. * the final dentry. We expect 'base' to be positive and a directory.
  1662. *
  1663. * Returns 0 and nd will have valid dentry and mnt on success.
  1664. * Returns error and drops reference to input namei data on failure.
  1665. */
  1666. static int link_path_walk(const char *name, struct nameidata *nd)
  1667. {
  1668. int err;
  1669. while (*name=='/')
  1670. name++;
  1671. if (!*name)
  1672. return 0;
  1673. /* At this point we know we have a real path component. */
  1674. for(;;) {
  1675. u64 hash_len;
  1676. int type;
  1677. err = may_lookup(nd);
  1678. if (err)
  1679. return err;
  1680. hash_len = hash_name(name);
  1681. type = LAST_NORM;
  1682. if (name[0] == '.') switch (hashlen_len(hash_len)) {
  1683. case 2:
  1684. if (name[1] == '.') {
  1685. type = LAST_DOTDOT;
  1686. nd->flags |= LOOKUP_JUMPED;
  1687. }
  1688. break;
  1689. case 1:
  1690. type = LAST_DOT;
  1691. }
  1692. if (likely(type == LAST_NORM)) {
  1693. struct dentry *parent = nd->path.dentry;
  1694. nd->flags &= ~LOOKUP_JUMPED;
  1695. if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
  1696. struct qstr this = { { .hash_len = hash_len }, .name = name };
  1697. err = parent->d_op->d_hash(parent, &this);
  1698. if (err < 0)
  1699. return err;
  1700. hash_len = this.hash_len;
  1701. name = this.name;
  1702. }
  1703. }
  1704. nd->last.hash_len = hash_len;
  1705. nd->last.name = name;
  1706. nd->last_type = type;
  1707. name += hashlen_len(hash_len);
  1708. if (!*name)
  1709. goto OK;
  1710. /*
  1711. * If it wasn't NUL, we know it was '/'. Skip that
  1712. * slash, and continue until no more slashes.
  1713. */
  1714. do {
  1715. name++;
  1716. } while (unlikely(*name == '/'));
  1717. if (unlikely(!*name)) {
  1718. OK:
  1719. /* pathname body, done */
  1720. if (!nd->depth)
  1721. return 0;
  1722. name = nd->stack[nd->depth - 1].name;
  1723. /* trailing symlink, done */
  1724. if (!name)
  1725. return 0;
  1726. /* last component of nested symlink */
  1727. err = walk_component(nd, WALK_GET | WALK_PUT);
  1728. } else {
  1729. err = walk_component(nd, WALK_GET);
  1730. }
  1731. if (err < 0)
  1732. return err;
  1733. if (err) {
  1734. const char *s = get_link(nd);
  1735. if (unlikely(IS_ERR(s)))
  1736. return PTR_ERR(s);
  1737. err = 0;
  1738. if (unlikely(!s)) {
  1739. /* jumped */
  1740. put_link(nd);
  1741. } else {
  1742. nd->stack[nd->depth - 1].name = name;
  1743. name = s;
  1744. continue;
  1745. }
  1746. }
  1747. if (unlikely(!d_can_lookup(nd->path.dentry)))
  1748. return -ENOTDIR;
  1749. }
  1750. }
  1751. static const char *path_init(struct nameidata *nd, unsigned flags)
  1752. {
  1753. int retval = 0;
  1754. const char *s = nd->name->name;
  1755. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1756. nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
  1757. nd->depth = 0;
  1758. nd->total_link_count = 0;
  1759. if (flags & LOOKUP_ROOT) {
  1760. struct dentry *root = nd->root.dentry;
  1761. struct inode *inode = root->d_inode;
  1762. if (*s) {
  1763. if (!d_can_lookup(root))
  1764. return ERR_PTR(-ENOTDIR);
  1765. retval = inode_permission(inode, MAY_EXEC);
  1766. if (retval)
  1767. return ERR_PTR(retval);
  1768. }
  1769. nd->path = nd->root;
  1770. nd->inode = inode;
  1771. if (flags & LOOKUP_RCU) {
  1772. rcu_read_lock();
  1773. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1774. nd->root_seq = nd->seq;
  1775. nd->m_seq = read_seqbegin(&mount_lock);
  1776. } else {
  1777. path_get(&nd->path);
  1778. }
  1779. return s;
  1780. }
  1781. nd->root.mnt = NULL;
  1782. nd->m_seq = read_seqbegin(&mount_lock);
  1783. if (*s == '/') {
  1784. if (flags & LOOKUP_RCU) {
  1785. rcu_read_lock();
  1786. set_root_rcu(nd);
  1787. nd->seq = nd->root_seq;
  1788. } else {
  1789. set_root(nd);
  1790. path_get(&nd->root);
  1791. }
  1792. nd->path = nd->root;
  1793. } else if (nd->dfd == AT_FDCWD) {
  1794. if (flags & LOOKUP_RCU) {
  1795. struct fs_struct *fs = current->fs;
  1796. unsigned seq;
  1797. rcu_read_lock();
  1798. do {
  1799. seq = read_seqcount_begin(&fs->seq);
  1800. nd->path = fs->pwd;
  1801. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1802. } while (read_seqcount_retry(&fs->seq, seq));
  1803. } else {
  1804. get_fs_pwd(current->fs, &nd->path);
  1805. }
  1806. } else {
  1807. /* Caller must check execute permissions on the starting path component */
  1808. struct fd f = fdget_raw(nd->dfd);
  1809. struct dentry *dentry;
  1810. if (!f.file)
  1811. return ERR_PTR(-EBADF);
  1812. dentry = f.file->f_path.dentry;
  1813. if (*s) {
  1814. if (!d_can_lookup(dentry)) {
  1815. fdput(f);
  1816. return ERR_PTR(-ENOTDIR);
  1817. }
  1818. }
  1819. nd->path = f.file->f_path;
  1820. if (flags & LOOKUP_RCU) {
  1821. rcu_read_lock();
  1822. nd->inode = nd->path.dentry->d_inode;
  1823. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  1824. } else {
  1825. path_get(&nd->path);
  1826. nd->inode = nd->path.dentry->d_inode;
  1827. }
  1828. fdput(f);
  1829. return s;
  1830. }
  1831. nd->inode = nd->path.dentry->d_inode;
  1832. if (!(flags & LOOKUP_RCU))
  1833. return s;
  1834. if (likely(!read_seqcount_retry(&nd->path.dentry->d_seq, nd->seq)))
  1835. return s;
  1836. if (!(nd->flags & LOOKUP_ROOT))
  1837. nd->root.mnt = NULL;
  1838. rcu_read_unlock();
  1839. return ERR_PTR(-ECHILD);
  1840. }
  1841. static const char *trailing_symlink(struct nameidata *nd)
  1842. {
  1843. const char *s;
  1844. int error = may_follow_link(nd);
  1845. if (unlikely(error))
  1846. return ERR_PTR(error);
  1847. nd->flags |= LOOKUP_PARENT;
  1848. nd->stack[0].name = NULL;
  1849. s = get_link(nd);
  1850. return s ? s : "";
  1851. }
  1852. static inline int lookup_last(struct nameidata *nd)
  1853. {
  1854. if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
  1855. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1856. nd->flags &= ~LOOKUP_PARENT;
  1857. return walk_component(nd,
  1858. nd->flags & LOOKUP_FOLLOW
  1859. ? nd->depth
  1860. ? WALK_PUT | WALK_GET
  1861. : WALK_GET
  1862. : 0);
  1863. }
  1864. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1865. static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
  1866. {
  1867. const char *s = path_init(nd, flags);
  1868. int err;
  1869. if (IS_ERR(s))
  1870. return PTR_ERR(s);
  1871. while (!(err = link_path_walk(s, nd))
  1872. && ((err = lookup_last(nd)) > 0)) {
  1873. s = trailing_symlink(nd);
  1874. if (IS_ERR(s)) {
  1875. err = PTR_ERR(s);
  1876. break;
  1877. }
  1878. }
  1879. if (!err)
  1880. err = complete_walk(nd);
  1881. if (!err && nd->flags & LOOKUP_DIRECTORY)
  1882. if (!d_can_lookup(nd->path.dentry))
  1883. err = -ENOTDIR;
  1884. if (!err) {
  1885. *path = nd->path;
  1886. nd->path.mnt = NULL;
  1887. nd->path.dentry = NULL;
  1888. }
  1889. terminate_walk(nd);
  1890. return err;
  1891. }
  1892. static int filename_lookup(int dfd, struct filename *name, unsigned flags,
  1893. struct path *path, struct path *root)
  1894. {
  1895. int retval;
  1896. struct nameidata nd;
  1897. if (IS_ERR(name))
  1898. return PTR_ERR(name);
  1899. if (unlikely(root)) {
  1900. nd.root = *root;
  1901. flags |= LOOKUP_ROOT;
  1902. }
  1903. set_nameidata(&nd, dfd, name);
  1904. retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
  1905. if (unlikely(retval == -ECHILD))
  1906. retval = path_lookupat(&nd, flags, path);
  1907. if (unlikely(retval == -ESTALE))
  1908. retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
  1909. if (likely(!retval))
  1910. audit_inode(name, path->dentry, flags & LOOKUP_PARENT);
  1911. restore_nameidata();
  1912. putname(name);
  1913. return retval;
  1914. }
  1915. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1916. static int path_parentat(struct nameidata *nd, unsigned flags,
  1917. struct path *parent)
  1918. {
  1919. const char *s = path_init(nd, flags);
  1920. int err;
  1921. if (IS_ERR(s))
  1922. return PTR_ERR(s);
  1923. err = link_path_walk(s, nd);
  1924. if (!err)
  1925. err = complete_walk(nd);
  1926. if (!err) {
  1927. *parent = nd->path;
  1928. nd->path.mnt = NULL;
  1929. nd->path.dentry = NULL;
  1930. }
  1931. terminate_walk(nd);
  1932. return err;
  1933. }
  1934. static struct filename *filename_parentat(int dfd, struct filename *name,
  1935. unsigned int flags, struct path *parent,
  1936. struct qstr *last, int *type)
  1937. {
  1938. int retval;
  1939. struct nameidata nd;
  1940. if (IS_ERR(name))
  1941. return name;
  1942. set_nameidata(&nd, dfd, name);
  1943. retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
  1944. if (unlikely(retval == -ECHILD))
  1945. retval = path_parentat(&nd, flags, parent);
  1946. if (unlikely(retval == -ESTALE))
  1947. retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
  1948. if (likely(!retval)) {
  1949. *last = nd.last;
  1950. *type = nd.last_type;
  1951. audit_inode(name, parent->dentry, LOOKUP_PARENT);
  1952. } else {
  1953. putname(name);
  1954. name = ERR_PTR(retval);
  1955. }
  1956. restore_nameidata();
  1957. return name;
  1958. }
  1959. /* does lookup, returns the object with parent locked */
  1960. struct dentry *kern_path_locked(const char *name, struct path *path)
  1961. {
  1962. struct filename *filename;
  1963. struct dentry *d;
  1964. struct qstr last;
  1965. int type;
  1966. filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
  1967. &last, &type);
  1968. if (IS_ERR(filename))
  1969. return ERR_CAST(filename);
  1970. if (unlikely(type != LAST_NORM)) {
  1971. path_put(path);
  1972. putname(filename);
  1973. return ERR_PTR(-EINVAL);
  1974. }
  1975. mutex_lock_nested(&path->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  1976. d = __lookup_hash(&last, path->dentry, 0);
  1977. if (IS_ERR(d)) {
  1978. mutex_unlock(&path->dentry->d_inode->i_mutex);
  1979. path_put(path);
  1980. }
  1981. putname(filename);
  1982. return d;
  1983. }
  1984. int kern_path(const char *name, unsigned int flags, struct path *path)
  1985. {
  1986. return filename_lookup(AT_FDCWD, getname_kernel(name),
  1987. flags, path, NULL);
  1988. }
  1989. EXPORT_SYMBOL(kern_path);
  1990. /**
  1991. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  1992. * @dentry: pointer to dentry of the base directory
  1993. * @mnt: pointer to vfs mount of the base directory
  1994. * @name: pointer to file name
  1995. * @flags: lookup flags
  1996. * @path: pointer to struct path to fill
  1997. */
  1998. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  1999. const char *name, unsigned int flags,
  2000. struct path *path)
  2001. {
  2002. struct path root = {.mnt = mnt, .dentry = dentry};
  2003. /* the first argument of filename_lookup() is ignored with root */
  2004. return filename_lookup(AT_FDCWD, getname_kernel(name),
  2005. flags , path, &root);
  2006. }
  2007. EXPORT_SYMBOL(vfs_path_lookup);
  2008. /**
  2009. * lookup_one_len - filesystem helper to lookup single pathname component
  2010. * @name: pathname component to lookup
  2011. * @base: base directory to lookup from
  2012. * @len: maximum length @len should be interpreted to
  2013. *
  2014. * Note that this routine is purely a helper for filesystem usage and should
  2015. * not be called by generic code.
  2016. */
  2017. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  2018. {
  2019. struct qstr this;
  2020. unsigned int c;
  2021. int err;
  2022. WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
  2023. this.name = name;
  2024. this.len = len;
  2025. this.hash = full_name_hash(name, len);
  2026. if (!len)
  2027. return ERR_PTR(-EACCES);
  2028. if (unlikely(name[0] == '.')) {
  2029. if (len < 2 || (len == 2 && name[1] == '.'))
  2030. return ERR_PTR(-EACCES);
  2031. }
  2032. while (len--) {
  2033. c = *(const unsigned char *)name++;
  2034. if (c == '/' || c == '\0')
  2035. return ERR_PTR(-EACCES);
  2036. }
  2037. /*
  2038. * See if the low-level filesystem might want
  2039. * to use its own hash..
  2040. */
  2041. if (base->d_flags & DCACHE_OP_HASH) {
  2042. int err = base->d_op->d_hash(base, &this);
  2043. if (err < 0)
  2044. return ERR_PTR(err);
  2045. }
  2046. err = inode_permission(base->d_inode, MAY_EXEC);
  2047. if (err)
  2048. return ERR_PTR(err);
  2049. return __lookup_hash(&this, base, 0);
  2050. }
  2051. EXPORT_SYMBOL(lookup_one_len);
  2052. int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
  2053. struct path *path, int *empty)
  2054. {
  2055. return filename_lookup(dfd, getname_flags(name, flags, empty),
  2056. flags, path, NULL);
  2057. }
  2058. EXPORT_SYMBOL(user_path_at_empty);
  2059. /*
  2060. * NB: most callers don't do anything directly with the reference to the
  2061. * to struct filename, but the nd->last pointer points into the name string
  2062. * allocated by getname. So we must hold the reference to it until all
  2063. * path-walking is complete.
  2064. */
  2065. static inline struct filename *
  2066. user_path_parent(int dfd, const char __user *path,
  2067. struct path *parent,
  2068. struct qstr *last,
  2069. int *type,
  2070. unsigned int flags)
  2071. {
  2072. /* only LOOKUP_REVAL is allowed in extra flags */
  2073. return filename_parentat(dfd, getname(path), flags & LOOKUP_REVAL,
  2074. parent, last, type);
  2075. }
  2076. /**
  2077. * mountpoint_last - look up last component for umount
  2078. * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
  2079. * @path: pointer to container for result
  2080. *
  2081. * This is a special lookup_last function just for umount. In this case, we
  2082. * need to resolve the path without doing any revalidation.
  2083. *
  2084. * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
  2085. * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
  2086. * in almost all cases, this lookup will be served out of the dcache. The only
  2087. * cases where it won't are if nd->last refers to a symlink or the path is
  2088. * bogus and it doesn't exist.
  2089. *
  2090. * Returns:
  2091. * -error: if there was an error during lookup. This includes -ENOENT if the
  2092. * lookup found a negative dentry. The nd->path reference will also be
  2093. * put in this case.
  2094. *
  2095. * 0: if we successfully resolved nd->path and found it to not to be a
  2096. * symlink that needs to be followed. "path" will also be populated.
  2097. * The nd->path reference will also be put.
  2098. *
  2099. * 1: if we successfully resolved nd->last and found it to be a symlink
  2100. * that needs to be followed. "path" will be populated with the path
  2101. * to the link, and nd->path will *not* be put.
  2102. */
  2103. static int
  2104. mountpoint_last(struct nameidata *nd, struct path *path)
  2105. {
  2106. int error = 0;
  2107. struct dentry *dentry;
  2108. struct dentry *dir = nd->path.dentry;
  2109. /* If we're in rcuwalk, drop out of it to handle last component */
  2110. if (nd->flags & LOOKUP_RCU) {
  2111. if (unlazy_walk(nd, NULL, 0))
  2112. return -ECHILD;
  2113. }
  2114. nd->flags &= ~LOOKUP_PARENT;
  2115. if (unlikely(nd->last_type != LAST_NORM)) {
  2116. error = handle_dots(nd, nd->last_type);
  2117. if (error)
  2118. return error;
  2119. dentry = dget(nd->path.dentry);
  2120. goto done;
  2121. }
  2122. mutex_lock(&dir->d_inode->i_mutex);
  2123. dentry = d_lookup(dir, &nd->last);
  2124. if (!dentry) {
  2125. /*
  2126. * No cached dentry. Mounted dentries are pinned in the cache,
  2127. * so that means that this dentry is probably a symlink or the
  2128. * path doesn't actually point to a mounted dentry.
  2129. */
  2130. dentry = d_alloc(dir, &nd->last);
  2131. if (!dentry) {
  2132. mutex_unlock(&dir->d_inode->i_mutex);
  2133. return -ENOMEM;
  2134. }
  2135. dentry = lookup_real(dir->d_inode, dentry, nd->flags);
  2136. if (IS_ERR(dentry)) {
  2137. mutex_unlock(&dir->d_inode->i_mutex);
  2138. return PTR_ERR(dentry);
  2139. }
  2140. }
  2141. mutex_unlock(&dir->d_inode->i_mutex);
  2142. done:
  2143. if (d_is_negative(dentry)) {
  2144. dput(dentry);
  2145. return -ENOENT;
  2146. }
  2147. if (nd->depth)
  2148. put_link(nd);
  2149. path->dentry = dentry;
  2150. path->mnt = nd->path.mnt;
  2151. error = should_follow_link(nd, path, nd->flags & LOOKUP_FOLLOW,
  2152. d_backing_inode(dentry), 0);
  2153. if (unlikely(error))
  2154. return error;
  2155. mntget(path->mnt);
  2156. follow_mount(path);
  2157. return 0;
  2158. }
  2159. /**
  2160. * path_mountpoint - look up a path to be umounted
  2161. * @nameidata: lookup context
  2162. * @flags: lookup flags
  2163. * @path: pointer to container for result
  2164. *
  2165. * Look up the given name, but don't attempt to revalidate the last component.
  2166. * Returns 0 and "path" will be valid on success; Returns error otherwise.
  2167. */
  2168. static int
  2169. path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
  2170. {
  2171. const char *s = path_init(nd, flags);
  2172. int err;
  2173. if (IS_ERR(s))
  2174. return PTR_ERR(s);
  2175. while (!(err = link_path_walk(s, nd)) &&
  2176. (err = mountpoint_last(nd, path)) > 0) {
  2177. s = trailing_symlink(nd);
  2178. if (IS_ERR(s)) {
  2179. err = PTR_ERR(s);
  2180. break;
  2181. }
  2182. }
  2183. terminate_walk(nd);
  2184. return err;
  2185. }
  2186. static int
  2187. filename_mountpoint(int dfd, struct filename *name, struct path *path,
  2188. unsigned int flags)
  2189. {
  2190. struct nameidata nd;
  2191. int error;
  2192. if (IS_ERR(name))
  2193. return PTR_ERR(name);
  2194. set_nameidata(&nd, dfd, name);
  2195. error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
  2196. if (unlikely(error == -ECHILD))
  2197. error = path_mountpoint(&nd, flags, path);
  2198. if (unlikely(error == -ESTALE))
  2199. error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
  2200. if (likely(!error))
  2201. audit_inode(name, path->dentry, 0);
  2202. restore_nameidata();
  2203. putname(name);
  2204. return error;
  2205. }
  2206. /**
  2207. * user_path_mountpoint_at - lookup a path from userland in order to umount it
  2208. * @dfd: directory file descriptor
  2209. * @name: pathname from userland
  2210. * @flags: lookup flags
  2211. * @path: pointer to container to hold result
  2212. *
  2213. * A umount is a special case for path walking. We're not actually interested
  2214. * in the inode in this situation, and ESTALE errors can be a problem. We
  2215. * simply want track down the dentry and vfsmount attached at the mountpoint
  2216. * and avoid revalidating the last component.
  2217. *
  2218. * Returns 0 and populates "path" on success.
  2219. */
  2220. int
  2221. user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
  2222. struct path *path)
  2223. {
  2224. return filename_mountpoint(dfd, getname(name), path, flags);
  2225. }
  2226. int
  2227. kern_path_mountpoint(int dfd, const char *name, struct path *path,
  2228. unsigned int flags)
  2229. {
  2230. return filename_mountpoint(dfd, getname_kernel(name), path, flags);
  2231. }
  2232. EXPORT_SYMBOL(kern_path_mountpoint);
  2233. int __check_sticky(struct inode *dir, struct inode *inode)
  2234. {
  2235. kuid_t fsuid = current_fsuid();
  2236. if (uid_eq(inode->i_uid, fsuid))
  2237. return 0;
  2238. if (uid_eq(dir->i_uid, fsuid))
  2239. return 0;
  2240. return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
  2241. }
  2242. EXPORT_SYMBOL(__check_sticky);
  2243. /*
  2244. * Check whether we can remove a link victim from directory dir, check
  2245. * whether the type of victim is right.
  2246. * 1. We can't do it if dir is read-only (done in permission())
  2247. * 2. We should have write and exec permissions on dir
  2248. * 3. We can't remove anything from append-only dir
  2249. * 4. We can't do anything with immutable dir (done in permission())
  2250. * 5. If the sticky bit on dir is set we should either
  2251. * a. be owner of dir, or
  2252. * b. be owner of victim, or
  2253. * c. have CAP_FOWNER capability
  2254. * 6. If the victim is append-only or immutable we can't do antyhing with
  2255. * links pointing to it.
  2256. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  2257. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  2258. * 9. We can't remove a root or mountpoint.
  2259. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  2260. * nfs_async_unlink().
  2261. */
  2262. static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
  2263. {
  2264. struct inode *inode = d_backing_inode(victim);
  2265. int error;
  2266. if (d_is_negative(victim))
  2267. return -ENOENT;
  2268. BUG_ON(!inode);
  2269. BUG_ON(victim->d_parent->d_inode != dir);
  2270. audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
  2271. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  2272. if (error)
  2273. return error;
  2274. if (IS_APPEND(dir))
  2275. return -EPERM;
  2276. if (check_sticky(dir, inode) || IS_APPEND(inode) ||
  2277. IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
  2278. return -EPERM;
  2279. if (isdir) {
  2280. if (!d_is_dir(victim))
  2281. return -ENOTDIR;
  2282. if (IS_ROOT(victim))
  2283. return -EBUSY;
  2284. } else if (d_is_dir(victim))
  2285. return -EISDIR;
  2286. if (IS_DEADDIR(dir))
  2287. return -ENOENT;
  2288. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  2289. return -EBUSY;
  2290. return 0;
  2291. }
  2292. /* Check whether we can create an object with dentry child in directory
  2293. * dir.
  2294. * 1. We can't do it if child already exists (open has special treatment for
  2295. * this case, but since we are inlined it's OK)
  2296. * 2. We can't do it if dir is read-only (done in permission())
  2297. * 3. We should have write and exec permissions on dir
  2298. * 4. We can't do it if dir is immutable (done in permission())
  2299. */
  2300. static inline int may_create(struct inode *dir, struct dentry *child)
  2301. {
  2302. audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
  2303. if (child->d_inode)
  2304. return -EEXIST;
  2305. if (IS_DEADDIR(dir))
  2306. return -ENOENT;
  2307. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  2308. }
  2309. /*
  2310. * p1 and p2 should be directories on the same fs.
  2311. */
  2312. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  2313. {
  2314. struct dentry *p;
  2315. if (p1 == p2) {
  2316. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  2317. return NULL;
  2318. }
  2319. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  2320. p = d_ancestor(p2, p1);
  2321. if (p) {
  2322. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  2323. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  2324. return p;
  2325. }
  2326. p = d_ancestor(p1, p2);
  2327. if (p) {
  2328. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  2329. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  2330. return p;
  2331. }
  2332. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  2333. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT2);
  2334. return NULL;
  2335. }
  2336. EXPORT_SYMBOL(lock_rename);
  2337. void unlock_rename(struct dentry *p1, struct dentry *p2)
  2338. {
  2339. mutex_unlock(&p1->d_inode->i_mutex);
  2340. if (p1 != p2) {
  2341. mutex_unlock(&p2->d_inode->i_mutex);
  2342. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  2343. }
  2344. }
  2345. EXPORT_SYMBOL(unlock_rename);
  2346. int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  2347. bool want_excl)
  2348. {
  2349. int error = may_create(dir, dentry);
  2350. if (error)
  2351. return error;
  2352. if (!dir->i_op->create)
  2353. return -EACCES; /* shouldn't it be ENOSYS? */
  2354. mode &= S_IALLUGO;
  2355. mode |= S_IFREG;
  2356. error = security_inode_create(dir, dentry, mode);
  2357. if (error)
  2358. return error;
  2359. error = dir->i_op->create(dir, dentry, mode, want_excl);
  2360. if (!error)
  2361. fsnotify_create(dir, dentry);
  2362. return error;
  2363. }
  2364. EXPORT_SYMBOL(vfs_create);
  2365. static int may_open(struct path *path, int acc_mode, int flag)
  2366. {
  2367. struct dentry *dentry = path->dentry;
  2368. struct inode *inode = dentry->d_inode;
  2369. int error;
  2370. /* O_PATH? */
  2371. if (!acc_mode)
  2372. return 0;
  2373. if (!inode)
  2374. return -ENOENT;
  2375. switch (inode->i_mode & S_IFMT) {
  2376. case S_IFLNK:
  2377. return -ELOOP;
  2378. case S_IFDIR:
  2379. if (acc_mode & MAY_WRITE)
  2380. return -EISDIR;
  2381. break;
  2382. case S_IFBLK:
  2383. case S_IFCHR:
  2384. if (path->mnt->mnt_flags & MNT_NODEV)
  2385. return -EACCES;
  2386. /*FALLTHRU*/
  2387. case S_IFIFO:
  2388. case S_IFSOCK:
  2389. flag &= ~O_TRUNC;
  2390. break;
  2391. }
  2392. error = inode_permission(inode, acc_mode);
  2393. if (error)
  2394. return error;
  2395. /*
  2396. * An append-only file must be opened in append mode for writing.
  2397. */
  2398. if (IS_APPEND(inode)) {
  2399. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  2400. return -EPERM;
  2401. if (flag & O_TRUNC)
  2402. return -EPERM;
  2403. }
  2404. /* O_NOATIME can only be set by the owner or superuser */
  2405. if (flag & O_NOATIME && !inode_owner_or_capable(inode))
  2406. return -EPERM;
  2407. return 0;
  2408. }
  2409. static int handle_truncate(struct file *filp)
  2410. {
  2411. struct path *path = &filp->f_path;
  2412. struct inode *inode = path->dentry->d_inode;
  2413. int error = get_write_access(inode);
  2414. if (error)
  2415. return error;
  2416. /*
  2417. * Refuse to truncate files with mandatory locks held on them.
  2418. */
  2419. error = locks_verify_locked(filp);
  2420. if (!error)
  2421. error = security_path_truncate(path);
  2422. if (!error) {
  2423. error = do_truncate(path->dentry, 0,
  2424. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  2425. filp);
  2426. }
  2427. put_write_access(inode);
  2428. return error;
  2429. }
  2430. static inline int open_to_namei_flags(int flag)
  2431. {
  2432. if ((flag & O_ACCMODE) == 3)
  2433. flag--;
  2434. return flag;
  2435. }
  2436. static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
  2437. {
  2438. int error = security_path_mknod(dir, dentry, mode, 0);
  2439. if (error)
  2440. return error;
  2441. error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
  2442. if (error)
  2443. return error;
  2444. return security_inode_create(dir->dentry->d_inode, dentry, mode);
  2445. }
  2446. /*
  2447. * Attempt to atomically look up, create and open a file from a negative
  2448. * dentry.
  2449. *
  2450. * Returns 0 if successful. The file will have been created and attached to
  2451. * @file by the filesystem calling finish_open().
  2452. *
  2453. * Returns 1 if the file was looked up only or didn't need creating. The
  2454. * caller will need to perform the open themselves. @path will have been
  2455. * updated to point to the new dentry. This may be negative.
  2456. *
  2457. * Returns an error code otherwise.
  2458. */
  2459. static int atomic_open(struct nameidata *nd, struct dentry *dentry,
  2460. struct path *path, struct file *file,
  2461. const struct open_flags *op,
  2462. bool got_write, bool need_lookup,
  2463. int *opened)
  2464. {
  2465. struct inode *dir = nd->path.dentry->d_inode;
  2466. unsigned open_flag = open_to_namei_flags(op->open_flag);
  2467. umode_t mode;
  2468. int error;
  2469. int acc_mode;
  2470. int create_error = 0;
  2471. struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
  2472. bool excl;
  2473. BUG_ON(dentry->d_inode);
  2474. /* Don't create child dentry for a dead directory. */
  2475. if (unlikely(IS_DEADDIR(dir))) {
  2476. error = -ENOENT;
  2477. goto out;
  2478. }
  2479. mode = op->mode;
  2480. if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
  2481. mode &= ~current_umask();
  2482. excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
  2483. if (excl)
  2484. open_flag &= ~O_TRUNC;
  2485. /*
  2486. * Checking write permission is tricky, bacuse we don't know if we are
  2487. * going to actually need it: O_CREAT opens should work as long as the
  2488. * file exists. But checking existence breaks atomicity. The trick is
  2489. * to check access and if not granted clear O_CREAT from the flags.
  2490. *
  2491. * Another problem is returing the "right" error value (e.g. for an
  2492. * O_EXCL open we want to return EEXIST not EROFS).
  2493. */
  2494. if (((open_flag & (O_CREAT | O_TRUNC)) ||
  2495. (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
  2496. if (!(open_flag & O_CREAT)) {
  2497. /*
  2498. * No O_CREATE -> atomicity not a requirement -> fall
  2499. * back to lookup + open
  2500. */
  2501. goto no_open;
  2502. } else if (open_flag & (O_EXCL | O_TRUNC)) {
  2503. /* Fall back and fail with the right error */
  2504. create_error = -EROFS;
  2505. goto no_open;
  2506. } else {
  2507. /* No side effects, safe to clear O_CREAT */
  2508. create_error = -EROFS;
  2509. open_flag &= ~O_CREAT;
  2510. }
  2511. }
  2512. if (open_flag & O_CREAT) {
  2513. error = may_o_create(&nd->path, dentry, mode);
  2514. if (error) {
  2515. create_error = error;
  2516. if (open_flag & O_EXCL)
  2517. goto no_open;
  2518. open_flag &= ~O_CREAT;
  2519. }
  2520. }
  2521. if (nd->flags & LOOKUP_DIRECTORY)
  2522. open_flag |= O_DIRECTORY;
  2523. file->f_path.dentry = DENTRY_NOT_SET;
  2524. file->f_path.mnt = nd->path.mnt;
  2525. error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
  2526. opened);
  2527. if (error < 0) {
  2528. if (create_error && error == -ENOENT)
  2529. error = create_error;
  2530. goto out;
  2531. }
  2532. if (error) { /* returned 1, that is */
  2533. if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
  2534. error = -EIO;
  2535. goto out;
  2536. }
  2537. if (file->f_path.dentry) {
  2538. dput(dentry);
  2539. dentry = file->f_path.dentry;
  2540. }
  2541. if (*opened & FILE_CREATED)
  2542. fsnotify_create(dir, dentry);
  2543. if (!dentry->d_inode) {
  2544. WARN_ON(*opened & FILE_CREATED);
  2545. if (create_error) {
  2546. error = create_error;
  2547. goto out;
  2548. }
  2549. } else {
  2550. if (excl && !(*opened & FILE_CREATED)) {
  2551. error = -EEXIST;
  2552. goto out;
  2553. }
  2554. }
  2555. goto looked_up;
  2556. }
  2557. /*
  2558. * We didn't have the inode before the open, so check open permission
  2559. * here.
  2560. */
  2561. acc_mode = op->acc_mode;
  2562. if (*opened & FILE_CREATED) {
  2563. WARN_ON(!(open_flag & O_CREAT));
  2564. fsnotify_create(dir, dentry);
  2565. acc_mode = MAY_OPEN;
  2566. }
  2567. error = may_open(&file->f_path, acc_mode, open_flag);
  2568. if (error)
  2569. fput(file);
  2570. out:
  2571. dput(dentry);
  2572. return error;
  2573. no_open:
  2574. if (need_lookup) {
  2575. dentry = lookup_real(dir, dentry, nd->flags);
  2576. if (IS_ERR(dentry))
  2577. return PTR_ERR(dentry);
  2578. if (create_error) {
  2579. int open_flag = op->open_flag;
  2580. error = create_error;
  2581. if ((open_flag & O_EXCL)) {
  2582. if (!dentry->d_inode)
  2583. goto out;
  2584. } else if (!dentry->d_inode) {
  2585. goto out;
  2586. } else if ((open_flag & O_TRUNC) &&
  2587. d_is_reg(dentry)) {
  2588. goto out;
  2589. }
  2590. /* will fail later, go on to get the right error */
  2591. }
  2592. }
  2593. looked_up:
  2594. path->dentry = dentry;
  2595. path->mnt = nd->path.mnt;
  2596. return 1;
  2597. }
  2598. /*
  2599. * Look up and maybe create and open the last component.
  2600. *
  2601. * Must be called with i_mutex held on parent.
  2602. *
  2603. * Returns 0 if the file was successfully atomically created (if necessary) and
  2604. * opened. In this case the file will be returned attached to @file.
  2605. *
  2606. * Returns 1 if the file was not completely opened at this time, though lookups
  2607. * and creations will have been performed and the dentry returned in @path will
  2608. * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
  2609. * specified then a negative dentry may be returned.
  2610. *
  2611. * An error code is returned otherwise.
  2612. *
  2613. * FILE_CREATE will be set in @*opened if the dentry was created and will be
  2614. * cleared otherwise prior to returning.
  2615. */
  2616. static int lookup_open(struct nameidata *nd, struct path *path,
  2617. struct file *file,
  2618. const struct open_flags *op,
  2619. bool got_write, int *opened)
  2620. {
  2621. struct dentry *dir = nd->path.dentry;
  2622. struct inode *dir_inode = dir->d_inode;
  2623. struct dentry *dentry;
  2624. int error;
  2625. bool need_lookup;
  2626. *opened &= ~FILE_CREATED;
  2627. dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
  2628. if (IS_ERR(dentry))
  2629. return PTR_ERR(dentry);
  2630. /* Cached positive dentry: will open in f_op->open */
  2631. if (!need_lookup && dentry->d_inode)
  2632. goto out_no_open;
  2633. if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
  2634. return atomic_open(nd, dentry, path, file, op, got_write,
  2635. need_lookup, opened);
  2636. }
  2637. if (need_lookup) {
  2638. BUG_ON(dentry->d_inode);
  2639. dentry = lookup_real(dir_inode, dentry, nd->flags);
  2640. if (IS_ERR(dentry))
  2641. return PTR_ERR(dentry);
  2642. }
  2643. /* Negative dentry, just create the file */
  2644. if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
  2645. umode_t mode = op->mode;
  2646. if (!IS_POSIXACL(dir->d_inode))
  2647. mode &= ~current_umask();
  2648. /*
  2649. * This write is needed to ensure that a
  2650. * rw->ro transition does not occur between
  2651. * the time when the file is created and when
  2652. * a permanent write count is taken through
  2653. * the 'struct file' in finish_open().
  2654. */
  2655. if (!got_write) {
  2656. error = -EROFS;
  2657. goto out_dput;
  2658. }
  2659. *opened |= FILE_CREATED;
  2660. error = security_path_mknod(&nd->path, dentry, mode, 0);
  2661. if (error)
  2662. goto out_dput;
  2663. error = vfs_create(dir->d_inode, dentry, mode,
  2664. nd->flags & LOOKUP_EXCL);
  2665. if (error)
  2666. goto out_dput;
  2667. }
  2668. out_no_open:
  2669. path->dentry = dentry;
  2670. path->mnt = nd->path.mnt;
  2671. return 1;
  2672. out_dput:
  2673. dput(dentry);
  2674. return error;
  2675. }
  2676. /*
  2677. * Handle the last step of open()
  2678. */
  2679. static int do_last(struct nameidata *nd,
  2680. struct file *file, const struct open_flags *op,
  2681. int *opened)
  2682. {
  2683. struct dentry *dir = nd->path.dentry;
  2684. int open_flag = op->open_flag;
  2685. bool will_truncate = (open_flag & O_TRUNC) != 0;
  2686. bool got_write = false;
  2687. int acc_mode = op->acc_mode;
  2688. unsigned seq;
  2689. struct inode *inode;
  2690. struct path save_parent = { .dentry = NULL, .mnt = NULL };
  2691. struct path path;
  2692. bool retried = false;
  2693. int error;
  2694. nd->flags &= ~LOOKUP_PARENT;
  2695. nd->flags |= op->intent;
  2696. if (nd->last_type != LAST_NORM) {
  2697. error = handle_dots(nd, nd->last_type);
  2698. if (unlikely(error))
  2699. return error;
  2700. goto finish_open;
  2701. }
  2702. if (!(open_flag & O_CREAT)) {
  2703. if (nd->last.name[nd->last.len])
  2704. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  2705. /* we _can_ be in RCU mode here */
  2706. error = lookup_fast(nd, &path, &inode, &seq);
  2707. if (likely(!error))
  2708. goto finish_lookup;
  2709. if (error < 0)
  2710. return error;
  2711. BUG_ON(nd->inode != dir->d_inode);
  2712. } else {
  2713. /* create side of things */
  2714. /*
  2715. * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
  2716. * has been cleared when we got to the last component we are
  2717. * about to look up
  2718. */
  2719. error = complete_walk(nd);
  2720. if (error)
  2721. return error;
  2722. audit_inode(nd->name, dir, LOOKUP_PARENT);
  2723. /* trailing slashes? */
  2724. if (unlikely(nd->last.name[nd->last.len]))
  2725. return -EISDIR;
  2726. }
  2727. retry_lookup:
  2728. if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
  2729. error = mnt_want_write(nd->path.mnt);
  2730. if (!error)
  2731. got_write = true;
  2732. /*
  2733. * do _not_ fail yet - we might not need that or fail with
  2734. * a different error; let lookup_open() decide; we'll be
  2735. * dropping this one anyway.
  2736. */
  2737. }
  2738. mutex_lock(&dir->d_inode->i_mutex);
  2739. error = lookup_open(nd, &path, file, op, got_write, opened);
  2740. mutex_unlock(&dir->d_inode->i_mutex);
  2741. if (error <= 0) {
  2742. if (error)
  2743. goto out;
  2744. if ((*opened & FILE_CREATED) ||
  2745. !S_ISREG(file_inode(file)->i_mode))
  2746. will_truncate = false;
  2747. audit_inode(nd->name, file->f_path.dentry, 0);
  2748. goto opened;
  2749. }
  2750. if (*opened & FILE_CREATED) {
  2751. /* Don't check for write permission, don't truncate */
  2752. open_flag &= ~O_TRUNC;
  2753. will_truncate = false;
  2754. acc_mode = MAY_OPEN;
  2755. path_to_nameidata(&path, nd);
  2756. goto finish_open_created;
  2757. }
  2758. /*
  2759. * create/update audit record if it already exists.
  2760. */
  2761. if (d_is_positive(path.dentry))
  2762. audit_inode(nd->name, path.dentry, 0);
  2763. /*
  2764. * If atomic_open() acquired write access it is dropped now due to
  2765. * possible mount and symlink following (this might be optimized away if
  2766. * necessary...)
  2767. */
  2768. if (got_write) {
  2769. mnt_drop_write(nd->path.mnt);
  2770. got_write = false;
  2771. }
  2772. if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
  2773. path_to_nameidata(&path, nd);
  2774. return -EEXIST;
  2775. }
  2776. error = follow_managed(&path, nd);
  2777. if (unlikely(error < 0))
  2778. return error;
  2779. BUG_ON(nd->flags & LOOKUP_RCU);
  2780. inode = d_backing_inode(path.dentry);
  2781. seq = 0; /* out of RCU mode, so the value doesn't matter */
  2782. if (unlikely(d_is_negative(path.dentry))) {
  2783. path_to_nameidata(&path, nd);
  2784. return -ENOENT;
  2785. }
  2786. finish_lookup:
  2787. if (nd->depth)
  2788. put_link(nd);
  2789. error = should_follow_link(nd, &path, nd->flags & LOOKUP_FOLLOW,
  2790. inode, seq);
  2791. if (unlikely(error))
  2792. return error;
  2793. if (unlikely(d_is_symlink(path.dentry)) && !(open_flag & O_PATH)) {
  2794. path_to_nameidata(&path, nd);
  2795. return -ELOOP;
  2796. }
  2797. if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path.mnt) {
  2798. path_to_nameidata(&path, nd);
  2799. } else {
  2800. save_parent.dentry = nd->path.dentry;
  2801. save_parent.mnt = mntget(path.mnt);
  2802. nd->path.dentry = path.dentry;
  2803. }
  2804. nd->inode = inode;
  2805. nd->seq = seq;
  2806. /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
  2807. finish_open:
  2808. error = complete_walk(nd);
  2809. if (error) {
  2810. path_put(&save_parent);
  2811. return error;
  2812. }
  2813. audit_inode(nd->name, nd->path.dentry, 0);
  2814. error = -EISDIR;
  2815. if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
  2816. goto out;
  2817. error = -ENOTDIR;
  2818. if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
  2819. goto out;
  2820. if (!d_is_reg(nd->path.dentry))
  2821. will_truncate = false;
  2822. if (will_truncate) {
  2823. error = mnt_want_write(nd->path.mnt);
  2824. if (error)
  2825. goto out;
  2826. got_write = true;
  2827. }
  2828. finish_open_created:
  2829. error = may_open(&nd->path, acc_mode, open_flag);
  2830. if (error)
  2831. goto out;
  2832. BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
  2833. error = vfs_open(&nd->path, file, current_cred());
  2834. if (!error) {
  2835. *opened |= FILE_OPENED;
  2836. } else {
  2837. if (error == -EOPENSTALE)
  2838. goto stale_open;
  2839. goto out;
  2840. }
  2841. opened:
  2842. error = open_check_o_direct(file);
  2843. if (error)
  2844. goto exit_fput;
  2845. error = ima_file_check(file, op->acc_mode, *opened);
  2846. if (error)
  2847. goto exit_fput;
  2848. if (will_truncate) {
  2849. error = handle_truncate(file);
  2850. if (error)
  2851. goto exit_fput;
  2852. }
  2853. out:
  2854. if (got_write)
  2855. mnt_drop_write(nd->path.mnt);
  2856. path_put(&save_parent);
  2857. return error;
  2858. exit_fput:
  2859. fput(file);
  2860. goto out;
  2861. stale_open:
  2862. /* If no saved parent or already retried then can't retry */
  2863. if (!save_parent.dentry || retried)
  2864. goto out;
  2865. BUG_ON(save_parent.dentry != dir);
  2866. path_put(&nd->path);
  2867. nd->path = save_parent;
  2868. nd->inode = dir->d_inode;
  2869. save_parent.mnt = NULL;
  2870. save_parent.dentry = NULL;
  2871. if (got_write) {
  2872. mnt_drop_write(nd->path.mnt);
  2873. got_write = false;
  2874. }
  2875. retried = true;
  2876. goto retry_lookup;
  2877. }
  2878. static int do_tmpfile(struct nameidata *nd, unsigned flags,
  2879. const struct open_flags *op,
  2880. struct file *file, int *opened)
  2881. {
  2882. static const struct qstr name = QSTR_INIT("/", 1);
  2883. struct dentry *child;
  2884. struct inode *dir;
  2885. struct path path;
  2886. int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
  2887. if (unlikely(error))
  2888. return error;
  2889. error = mnt_want_write(path.mnt);
  2890. if (unlikely(error))
  2891. goto out;
  2892. dir = path.dentry->d_inode;
  2893. /* we want directory to be writable */
  2894. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  2895. if (error)
  2896. goto out2;
  2897. if (!dir->i_op->tmpfile) {
  2898. error = -EOPNOTSUPP;
  2899. goto out2;
  2900. }
  2901. child = d_alloc(path.dentry, &name);
  2902. if (unlikely(!child)) {
  2903. error = -ENOMEM;
  2904. goto out2;
  2905. }
  2906. dput(path.dentry);
  2907. path.dentry = child;
  2908. error = dir->i_op->tmpfile(dir, child, op->mode);
  2909. if (error)
  2910. goto out2;
  2911. audit_inode(nd->name, child, 0);
  2912. /* Don't check for other permissions, the inode was just created */
  2913. error = may_open(&path, MAY_OPEN, op->open_flag);
  2914. if (error)
  2915. goto out2;
  2916. file->f_path.mnt = path.mnt;
  2917. error = finish_open(file, child, NULL, opened);
  2918. if (error)
  2919. goto out2;
  2920. error = open_check_o_direct(file);
  2921. if (error) {
  2922. fput(file);
  2923. } else if (!(op->open_flag & O_EXCL)) {
  2924. struct inode *inode = file_inode(file);
  2925. spin_lock(&inode->i_lock);
  2926. inode->i_state |= I_LINKABLE;
  2927. spin_unlock(&inode->i_lock);
  2928. }
  2929. out2:
  2930. mnt_drop_write(path.mnt);
  2931. out:
  2932. path_put(&path);
  2933. return error;
  2934. }
  2935. static struct file *path_openat(struct nameidata *nd,
  2936. const struct open_flags *op, unsigned flags)
  2937. {
  2938. const char *s;
  2939. struct file *file;
  2940. int opened = 0;
  2941. int error;
  2942. file = get_empty_filp();
  2943. if (IS_ERR(file))
  2944. return file;
  2945. file->f_flags = op->open_flag;
  2946. if (unlikely(file->f_flags & __O_TMPFILE)) {
  2947. error = do_tmpfile(nd, flags, op, file, &opened);
  2948. goto out2;
  2949. }
  2950. s = path_init(nd, flags);
  2951. if (IS_ERR(s)) {
  2952. put_filp(file);
  2953. return ERR_CAST(s);
  2954. }
  2955. while (!(error = link_path_walk(s, nd)) &&
  2956. (error = do_last(nd, file, op, &opened)) > 0) {
  2957. nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
  2958. s = trailing_symlink(nd);
  2959. if (IS_ERR(s)) {
  2960. error = PTR_ERR(s);
  2961. break;
  2962. }
  2963. }
  2964. terminate_walk(nd);
  2965. out2:
  2966. if (!(opened & FILE_OPENED)) {
  2967. BUG_ON(!error);
  2968. put_filp(file);
  2969. }
  2970. if (unlikely(error)) {
  2971. if (error == -EOPENSTALE) {
  2972. if (flags & LOOKUP_RCU)
  2973. error = -ECHILD;
  2974. else
  2975. error = -ESTALE;
  2976. }
  2977. file = ERR_PTR(error);
  2978. }
  2979. return file;
  2980. }
  2981. struct file *do_filp_open(int dfd, struct filename *pathname,
  2982. const struct open_flags *op)
  2983. {
  2984. struct nameidata nd;
  2985. int flags = op->lookup_flags;
  2986. struct file *filp;
  2987. set_nameidata(&nd, dfd, pathname);
  2988. filp = path_openat(&nd, op, flags | LOOKUP_RCU);
  2989. if (unlikely(filp == ERR_PTR(-ECHILD)))
  2990. filp = path_openat(&nd, op, flags);
  2991. if (unlikely(filp == ERR_PTR(-ESTALE)))
  2992. filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
  2993. restore_nameidata();
  2994. return filp;
  2995. }
  2996. struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  2997. const char *name, const struct open_flags *op)
  2998. {
  2999. struct nameidata nd;
  3000. struct file *file;
  3001. struct filename *filename;
  3002. int flags = op->lookup_flags | LOOKUP_ROOT;
  3003. nd.root.mnt = mnt;
  3004. nd.root.dentry = dentry;
  3005. if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
  3006. return ERR_PTR(-ELOOP);
  3007. filename = getname_kernel(name);
  3008. if (unlikely(IS_ERR(filename)))
  3009. return ERR_CAST(filename);
  3010. set_nameidata(&nd, -1, filename);
  3011. file = path_openat(&nd, op, flags | LOOKUP_RCU);
  3012. if (unlikely(file == ERR_PTR(-ECHILD)))
  3013. file = path_openat(&nd, op, flags);
  3014. if (unlikely(file == ERR_PTR(-ESTALE)))
  3015. file = path_openat(&nd, op, flags | LOOKUP_REVAL);
  3016. restore_nameidata();
  3017. putname(filename);
  3018. return file;
  3019. }
  3020. static struct dentry *filename_create(int dfd, struct filename *name,
  3021. struct path *path, unsigned int lookup_flags)
  3022. {
  3023. struct dentry *dentry = ERR_PTR(-EEXIST);
  3024. struct qstr last;
  3025. int type;
  3026. int err2;
  3027. int error;
  3028. bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
  3029. /*
  3030. * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
  3031. * other flags passed in are ignored!
  3032. */
  3033. lookup_flags &= LOOKUP_REVAL;
  3034. name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
  3035. if (IS_ERR(name))
  3036. return ERR_CAST(name);
  3037. /*
  3038. * Yucky last component or no last component at all?
  3039. * (foo/., foo/.., /////)
  3040. */
  3041. if (unlikely(type != LAST_NORM))
  3042. goto out;
  3043. /* don't fail immediately if it's r/o, at least try to report other errors */
  3044. err2 = mnt_want_write(path->mnt);
  3045. /*
  3046. * Do the final lookup.
  3047. */
  3048. lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  3049. mutex_lock_nested(&path->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  3050. dentry = __lookup_hash(&last, path->dentry, lookup_flags);
  3051. if (IS_ERR(dentry))
  3052. goto unlock;
  3053. error = -EEXIST;
  3054. if (d_is_positive(dentry))
  3055. goto fail;
  3056. /*
  3057. * Special case - lookup gave negative, but... we had foo/bar/
  3058. * From the vfs_mknod() POV we just have a negative dentry -
  3059. * all is fine. Let's be bastards - you had / on the end, you've
  3060. * been asking for (non-existent) directory. -ENOENT for you.
  3061. */
  3062. if (unlikely(!is_dir && last.name[last.len])) {
  3063. error = -ENOENT;
  3064. goto fail;
  3065. }
  3066. if (unlikely(err2)) {
  3067. error = err2;
  3068. goto fail;
  3069. }
  3070. putname(name);
  3071. return dentry;
  3072. fail:
  3073. dput(dentry);
  3074. dentry = ERR_PTR(error);
  3075. unlock:
  3076. mutex_unlock(&path->dentry->d_inode->i_mutex);
  3077. if (!err2)
  3078. mnt_drop_write(path->mnt);
  3079. out:
  3080. path_put(path);
  3081. putname(name);
  3082. return dentry;
  3083. }
  3084. struct dentry *kern_path_create(int dfd, const char *pathname,
  3085. struct path *path, unsigned int lookup_flags)
  3086. {
  3087. return filename_create(dfd, getname_kernel(pathname),
  3088. path, lookup_flags);
  3089. }
  3090. EXPORT_SYMBOL(kern_path_create);
  3091. void done_path_create(struct path *path, struct dentry *dentry)
  3092. {
  3093. dput(dentry);
  3094. mutex_unlock(&path->dentry->d_inode->i_mutex);
  3095. mnt_drop_write(path->mnt);
  3096. path_put(path);
  3097. }
  3098. EXPORT_SYMBOL(done_path_create);
  3099. inline struct dentry *user_path_create(int dfd, const char __user *pathname,
  3100. struct path *path, unsigned int lookup_flags)
  3101. {
  3102. return filename_create(dfd, getname(pathname), path, lookup_flags);
  3103. }
  3104. EXPORT_SYMBOL(user_path_create);
  3105. int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  3106. {
  3107. int error = may_create(dir, dentry);
  3108. if (error)
  3109. return error;
  3110. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  3111. return -EPERM;
  3112. if (!dir->i_op->mknod)
  3113. return -EPERM;
  3114. error = devcgroup_inode_mknod(mode, dev);
  3115. if (error)
  3116. return error;
  3117. error = security_inode_mknod(dir, dentry, mode, dev);
  3118. if (error)
  3119. return error;
  3120. error = dir->i_op->mknod(dir, dentry, mode, dev);
  3121. if (!error)
  3122. fsnotify_create(dir, dentry);
  3123. return error;
  3124. }
  3125. EXPORT_SYMBOL(vfs_mknod);
  3126. static int may_mknod(umode_t mode)
  3127. {
  3128. switch (mode & S_IFMT) {
  3129. case S_IFREG:
  3130. case S_IFCHR:
  3131. case S_IFBLK:
  3132. case S_IFIFO:
  3133. case S_IFSOCK:
  3134. case 0: /* zero mode translates to S_IFREG */
  3135. return 0;
  3136. case S_IFDIR:
  3137. return -EPERM;
  3138. default:
  3139. return -EINVAL;
  3140. }
  3141. }
  3142. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
  3143. unsigned, dev)
  3144. {
  3145. struct dentry *dentry;
  3146. struct path path;
  3147. int error;
  3148. unsigned int lookup_flags = 0;
  3149. error = may_mknod(mode);
  3150. if (error)
  3151. return error;
  3152. retry:
  3153. dentry = user_path_create(dfd, filename, &path, lookup_flags);
  3154. if (IS_ERR(dentry))
  3155. return PTR_ERR(dentry);
  3156. if (!IS_POSIXACL(path.dentry->d_inode))
  3157. mode &= ~current_umask();
  3158. error = security_path_mknod(&path, dentry, mode, dev);
  3159. if (error)
  3160. goto out;
  3161. switch (mode & S_IFMT) {
  3162. case 0: case S_IFREG:
  3163. error = vfs_create(path.dentry->d_inode,dentry,mode,true);
  3164. break;
  3165. case S_IFCHR: case S_IFBLK:
  3166. error = vfs_mknod(path.dentry->d_inode,dentry,mode,
  3167. new_decode_dev(dev));
  3168. break;
  3169. case S_IFIFO: case S_IFSOCK:
  3170. error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
  3171. break;
  3172. }
  3173. out:
  3174. done_path_create(&path, dentry);
  3175. if (retry_estale(error, lookup_flags)) {
  3176. lookup_flags |= LOOKUP_REVAL;
  3177. goto retry;
  3178. }
  3179. return error;
  3180. }
  3181. SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
  3182. {
  3183. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  3184. }
  3185. int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  3186. {
  3187. int error = may_create(dir, dentry);
  3188. unsigned max_links = dir->i_sb->s_max_links;
  3189. if (error)
  3190. return error;
  3191. if (!dir->i_op->mkdir)
  3192. return -EPERM;
  3193. mode &= (S_IRWXUGO|S_ISVTX);
  3194. error = security_inode_mkdir(dir, dentry, mode);
  3195. if (error)
  3196. return error;
  3197. if (max_links && dir->i_nlink >= max_links)
  3198. return -EMLINK;
  3199. error = dir->i_op->mkdir(dir, dentry, mode);
  3200. if (!error)
  3201. fsnotify_mkdir(dir, dentry);
  3202. return error;
  3203. }
  3204. EXPORT_SYMBOL(vfs_mkdir);
  3205. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
  3206. {
  3207. struct dentry *dentry;
  3208. struct path path;
  3209. int error;
  3210. unsigned int lookup_flags = LOOKUP_DIRECTORY;
  3211. retry:
  3212. dentry = user_path_create(dfd, pathname, &path, lookup_flags);
  3213. if (IS_ERR(dentry))
  3214. return PTR_ERR(dentry);
  3215. if (!IS_POSIXACL(path.dentry->d_inode))
  3216. mode &= ~current_umask();
  3217. error = security_path_mkdir(&path, dentry, mode);
  3218. if (!error)
  3219. error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
  3220. done_path_create(&path, dentry);
  3221. if (retry_estale(error, lookup_flags)) {
  3222. lookup_flags |= LOOKUP_REVAL;
  3223. goto retry;
  3224. }
  3225. return error;
  3226. }
  3227. SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
  3228. {
  3229. return sys_mkdirat(AT_FDCWD, pathname, mode);
  3230. }
  3231. /*
  3232. * The dentry_unhash() helper will try to drop the dentry early: we
  3233. * should have a usage count of 1 if we're the only user of this
  3234. * dentry, and if that is true (possibly after pruning the dcache),
  3235. * then we drop the dentry now.
  3236. *
  3237. * A low-level filesystem can, if it choses, legally
  3238. * do a
  3239. *
  3240. * if (!d_unhashed(dentry))
  3241. * return -EBUSY;
  3242. *
  3243. * if it cannot handle the case of removing a directory
  3244. * that is still in use by something else..
  3245. */
  3246. void dentry_unhash(struct dentry *dentry)
  3247. {
  3248. shrink_dcache_parent(dentry);
  3249. spin_lock(&dentry->d_lock);
  3250. if (dentry->d_lockref.count == 1)
  3251. __d_drop(dentry);
  3252. spin_unlock(&dentry->d_lock);
  3253. }
  3254. EXPORT_SYMBOL(dentry_unhash);
  3255. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  3256. {
  3257. int error = may_delete(dir, dentry, 1);
  3258. if (error)
  3259. return error;
  3260. if (!dir->i_op->rmdir)
  3261. return -EPERM;
  3262. dget(dentry);
  3263. mutex_lock(&dentry->d_inode->i_mutex);
  3264. error = -EBUSY;
  3265. if (is_local_mountpoint(dentry))
  3266. goto out;
  3267. error = security_inode_rmdir(dir, dentry);
  3268. if (error)
  3269. goto out;
  3270. shrink_dcache_parent(dentry);
  3271. error = dir->i_op->rmdir(dir, dentry);
  3272. if (error)
  3273. goto out;
  3274. dentry->d_inode->i_flags |= S_DEAD;
  3275. dont_mount(dentry);
  3276. detach_mounts(dentry);
  3277. out:
  3278. mutex_unlock(&dentry->d_inode->i_mutex);
  3279. dput(dentry);
  3280. if (!error)
  3281. d_delete(dentry);
  3282. return error;
  3283. }
  3284. EXPORT_SYMBOL(vfs_rmdir);
  3285. static long do_rmdir(int dfd, const char __user *pathname)
  3286. {
  3287. int error = 0;
  3288. struct filename *name;
  3289. struct dentry *dentry;
  3290. struct path path;
  3291. struct qstr last;
  3292. int type;
  3293. unsigned int lookup_flags = 0;
  3294. retry:
  3295. name = user_path_parent(dfd, pathname,
  3296. &path, &last, &type, lookup_flags);
  3297. if (IS_ERR(name))
  3298. return PTR_ERR(name);
  3299. switch (type) {
  3300. case LAST_DOTDOT:
  3301. error = -ENOTEMPTY;
  3302. goto exit1;
  3303. case LAST_DOT:
  3304. error = -EINVAL;
  3305. goto exit1;
  3306. case LAST_ROOT:
  3307. error = -EBUSY;
  3308. goto exit1;
  3309. }
  3310. error = mnt_want_write(path.mnt);
  3311. if (error)
  3312. goto exit1;
  3313. mutex_lock_nested(&path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  3314. dentry = __lookup_hash(&last, path.dentry, lookup_flags);
  3315. error = PTR_ERR(dentry);
  3316. if (IS_ERR(dentry))
  3317. goto exit2;
  3318. if (!dentry->d_inode) {
  3319. error = -ENOENT;
  3320. goto exit3;
  3321. }
  3322. error = security_path_rmdir(&path, dentry);
  3323. if (error)
  3324. goto exit3;
  3325. error = vfs_rmdir(path.dentry->d_inode, dentry);
  3326. exit3:
  3327. dput(dentry);
  3328. exit2:
  3329. mutex_unlock(&path.dentry->d_inode->i_mutex);
  3330. mnt_drop_write(path.mnt);
  3331. exit1:
  3332. path_put(&path);
  3333. putname(name);
  3334. if (retry_estale(error, lookup_flags)) {
  3335. lookup_flags |= LOOKUP_REVAL;
  3336. goto retry;
  3337. }
  3338. return error;
  3339. }
  3340. SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
  3341. {
  3342. return do_rmdir(AT_FDCWD, pathname);
  3343. }
  3344. /**
  3345. * vfs_unlink - unlink a filesystem object
  3346. * @dir: parent directory
  3347. * @dentry: victim
  3348. * @delegated_inode: returns victim inode, if the inode is delegated.
  3349. *
  3350. * The caller must hold dir->i_mutex.
  3351. *
  3352. * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
  3353. * return a reference to the inode in delegated_inode. The caller
  3354. * should then break the delegation on that inode and retry. Because
  3355. * breaking a delegation may take a long time, the caller should drop
  3356. * dir->i_mutex before doing so.
  3357. *
  3358. * Alternatively, a caller may pass NULL for delegated_inode. This may
  3359. * be appropriate for callers that expect the underlying filesystem not
  3360. * to be NFS exported.
  3361. */
  3362. int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
  3363. {
  3364. struct inode *target = dentry->d_inode;
  3365. int error = may_delete(dir, dentry, 0);
  3366. if (error)
  3367. return error;
  3368. if (!dir->i_op->unlink)
  3369. return -EPERM;
  3370. mutex_lock(&target->i_mutex);
  3371. if (is_local_mountpoint(dentry))
  3372. error = -EBUSY;
  3373. else {
  3374. error = security_inode_unlink(dir, dentry);
  3375. if (!error) {
  3376. error = try_break_deleg(target, delegated_inode);
  3377. if (error)
  3378. goto out;
  3379. error = dir->i_op->unlink(dir, dentry);
  3380. if (!error) {
  3381. dont_mount(dentry);
  3382. detach_mounts(dentry);
  3383. }
  3384. }
  3385. }
  3386. out:
  3387. mutex_unlock(&target->i_mutex);
  3388. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  3389. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  3390. fsnotify_link_count(target);
  3391. d_delete(dentry);
  3392. }
  3393. return error;
  3394. }
  3395. EXPORT_SYMBOL(vfs_unlink);
  3396. /*
  3397. * Make sure that the actual truncation of the file will occur outside its
  3398. * directory's i_mutex. Truncate can take a long time if there is a lot of
  3399. * writeout happening, and we don't want to prevent access to the directory
  3400. * while waiting on the I/O.
  3401. */
  3402. static long do_unlinkat(int dfd, const char __user *pathname)
  3403. {
  3404. int error;
  3405. struct filename *name;
  3406. struct dentry *dentry;
  3407. struct path path;
  3408. struct qstr last;
  3409. int type;
  3410. struct inode *inode = NULL;
  3411. struct inode *delegated_inode = NULL;
  3412. unsigned int lookup_flags = 0;
  3413. retry:
  3414. name = user_path_parent(dfd, pathname,
  3415. &path, &last, &type, lookup_flags);
  3416. if (IS_ERR(name))
  3417. return PTR_ERR(name);
  3418. error = -EISDIR;
  3419. if (type != LAST_NORM)
  3420. goto exit1;
  3421. error = mnt_want_write(path.mnt);
  3422. if (error)
  3423. goto exit1;
  3424. retry_deleg:
  3425. mutex_lock_nested(&path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  3426. dentry = __lookup_hash(&last, path.dentry, lookup_flags);
  3427. error = PTR_ERR(dentry);
  3428. if (!IS_ERR(dentry)) {
  3429. /* Why not before? Because we want correct error value */
  3430. if (last.name[last.len])
  3431. goto slashes;
  3432. inode = dentry->d_inode;
  3433. if (d_is_negative(dentry))
  3434. goto slashes;
  3435. ihold(inode);
  3436. error = security_path_unlink(&path, dentry);
  3437. if (error)
  3438. goto exit2;
  3439. error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
  3440. exit2:
  3441. dput(dentry);
  3442. }
  3443. mutex_unlock(&path.dentry->d_inode->i_mutex);
  3444. if (inode)
  3445. iput(inode); /* truncate the inode here */
  3446. inode = NULL;
  3447. if (delegated_inode) {
  3448. error = break_deleg_wait(&delegated_inode);
  3449. if (!error)
  3450. goto retry_deleg;
  3451. }
  3452. mnt_drop_write(path.mnt);
  3453. exit1:
  3454. path_put(&path);
  3455. putname(name);
  3456. if (retry_estale(error, lookup_flags)) {
  3457. lookup_flags |= LOOKUP_REVAL;
  3458. inode = NULL;
  3459. goto retry;
  3460. }
  3461. return error;
  3462. slashes:
  3463. if (d_is_negative(dentry))
  3464. error = -ENOENT;
  3465. else if (d_is_dir(dentry))
  3466. error = -EISDIR;
  3467. else
  3468. error = -ENOTDIR;
  3469. goto exit2;
  3470. }
  3471. SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
  3472. {
  3473. if ((flag & ~AT_REMOVEDIR) != 0)
  3474. return -EINVAL;
  3475. if (flag & AT_REMOVEDIR)
  3476. return do_rmdir(dfd, pathname);
  3477. return do_unlinkat(dfd, pathname);
  3478. }
  3479. SYSCALL_DEFINE1(unlink, const char __user *, pathname)
  3480. {
  3481. return do_unlinkat(AT_FDCWD, pathname);
  3482. }
  3483. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  3484. {
  3485. int error = may_create(dir, dentry);
  3486. if (error)
  3487. return error;
  3488. if (!dir->i_op->symlink)
  3489. return -EPERM;
  3490. error = security_inode_symlink(dir, dentry, oldname);
  3491. if (error)
  3492. return error;
  3493. error = dir->i_op->symlink(dir, dentry, oldname);
  3494. if (!error)
  3495. fsnotify_create(dir, dentry);
  3496. return error;
  3497. }
  3498. EXPORT_SYMBOL(vfs_symlink);
  3499. SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  3500. int, newdfd, const char __user *, newname)
  3501. {
  3502. int error;
  3503. struct filename *from;
  3504. struct dentry *dentry;
  3505. struct path path;
  3506. unsigned int lookup_flags = 0;
  3507. from = getname(oldname);
  3508. if (IS_ERR(from))
  3509. return PTR_ERR(from);
  3510. retry:
  3511. dentry = user_path_create(newdfd, newname, &path, lookup_flags);
  3512. error = PTR_ERR(dentry);
  3513. if (IS_ERR(dentry))
  3514. goto out_putname;
  3515. error = security_path_symlink(&path, dentry, from->name);
  3516. if (!error)
  3517. error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
  3518. done_path_create(&path, dentry);
  3519. if (retry_estale(error, lookup_flags)) {
  3520. lookup_flags |= LOOKUP_REVAL;
  3521. goto retry;
  3522. }
  3523. out_putname:
  3524. putname(from);
  3525. return error;
  3526. }
  3527. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  3528. {
  3529. return sys_symlinkat(oldname, AT_FDCWD, newname);
  3530. }
  3531. /**
  3532. * vfs_link - create a new link
  3533. * @old_dentry: object to be linked
  3534. * @dir: new parent
  3535. * @new_dentry: where to create the new link
  3536. * @delegated_inode: returns inode needing a delegation break
  3537. *
  3538. * The caller must hold dir->i_mutex
  3539. *
  3540. * If vfs_link discovers a delegation on the to-be-linked file in need
  3541. * of breaking, it will return -EWOULDBLOCK and return a reference to the
  3542. * inode in delegated_inode. The caller should then break the delegation
  3543. * and retry. Because breaking a delegation may take a long time, the
  3544. * caller should drop the i_mutex before doing so.
  3545. *
  3546. * Alternatively, a caller may pass NULL for delegated_inode. This may
  3547. * be appropriate for callers that expect the underlying filesystem not
  3548. * to be NFS exported.
  3549. */
  3550. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
  3551. {
  3552. struct inode *inode = old_dentry->d_inode;
  3553. unsigned max_links = dir->i_sb->s_max_links;
  3554. int error;
  3555. if (!inode)
  3556. return -ENOENT;
  3557. error = may_create(dir, new_dentry);
  3558. if (error)
  3559. return error;
  3560. if (dir->i_sb != inode->i_sb)
  3561. return -EXDEV;
  3562. /*
  3563. * A link to an append-only or immutable file cannot be created.
  3564. */
  3565. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  3566. return -EPERM;
  3567. if (!dir->i_op->link)
  3568. return -EPERM;
  3569. if (S_ISDIR(inode->i_mode))
  3570. return -EPERM;
  3571. error = security_inode_link(old_dentry, dir, new_dentry);
  3572. if (error)
  3573. return error;
  3574. mutex_lock(&inode->i_mutex);
  3575. /* Make sure we don't allow creating hardlink to an unlinked file */
  3576. if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
  3577. error = -ENOENT;
  3578. else if (max_links && inode->i_nlink >= max_links)
  3579. error = -EMLINK;
  3580. else {
  3581. error = try_break_deleg(inode, delegated_inode);
  3582. if (!error)
  3583. error = dir->i_op->link(old_dentry, dir, new_dentry);
  3584. }
  3585. if (!error && (inode->i_state & I_LINKABLE)) {
  3586. spin_lock(&inode->i_lock);
  3587. inode->i_state &= ~I_LINKABLE;
  3588. spin_unlock(&inode->i_lock);
  3589. }
  3590. mutex_unlock(&inode->i_mutex);
  3591. if (!error)
  3592. fsnotify_link(dir, inode, new_dentry);
  3593. return error;
  3594. }
  3595. EXPORT_SYMBOL(vfs_link);
  3596. /*
  3597. * Hardlinks are often used in delicate situations. We avoid
  3598. * security-related surprises by not following symlinks on the
  3599. * newname. --KAB
  3600. *
  3601. * We don't follow them on the oldname either to be compatible
  3602. * with linux 2.0, and to avoid hard-linking to directories
  3603. * and other special files. --ADM
  3604. */
  3605. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  3606. int, newdfd, const char __user *, newname, int, flags)
  3607. {
  3608. struct dentry *new_dentry;
  3609. struct path old_path, new_path;
  3610. struct inode *delegated_inode = NULL;
  3611. int how = 0;
  3612. int error;
  3613. if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  3614. return -EINVAL;
  3615. /*
  3616. * To use null names we require CAP_DAC_READ_SEARCH
  3617. * This ensures that not everyone will be able to create
  3618. * handlink using the passed filedescriptor.
  3619. */
  3620. if (flags & AT_EMPTY_PATH) {
  3621. if (!capable(CAP_DAC_READ_SEARCH))
  3622. return -ENOENT;
  3623. how = LOOKUP_EMPTY;
  3624. }
  3625. if (flags & AT_SYMLINK_FOLLOW)
  3626. how |= LOOKUP_FOLLOW;
  3627. retry:
  3628. error = user_path_at(olddfd, oldname, how, &old_path);
  3629. if (error)
  3630. return error;
  3631. new_dentry = user_path_create(newdfd, newname, &new_path,
  3632. (how & LOOKUP_REVAL));
  3633. error = PTR_ERR(new_dentry);
  3634. if (IS_ERR(new_dentry))
  3635. goto out;
  3636. error = -EXDEV;
  3637. if (old_path.mnt != new_path.mnt)
  3638. goto out_dput;
  3639. error = may_linkat(&old_path);
  3640. if (unlikely(error))
  3641. goto out_dput;
  3642. error = security_path_link(old_path.dentry, &new_path, new_dentry);
  3643. if (error)
  3644. goto out_dput;
  3645. error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
  3646. out_dput:
  3647. done_path_create(&new_path, new_dentry);
  3648. if (delegated_inode) {
  3649. error = break_deleg_wait(&delegated_inode);
  3650. if (!error) {
  3651. path_put(&old_path);
  3652. goto retry;
  3653. }
  3654. }
  3655. if (retry_estale(error, how)) {
  3656. path_put(&old_path);
  3657. how |= LOOKUP_REVAL;
  3658. goto retry;
  3659. }
  3660. out:
  3661. path_put(&old_path);
  3662. return error;
  3663. }
  3664. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  3665. {
  3666. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  3667. }
  3668. /**
  3669. * vfs_rename - rename a filesystem object
  3670. * @old_dir: parent of source
  3671. * @old_dentry: source
  3672. * @new_dir: parent of destination
  3673. * @new_dentry: destination
  3674. * @delegated_inode: returns an inode needing a delegation break
  3675. * @flags: rename flags
  3676. *
  3677. * The caller must hold multiple mutexes--see lock_rename()).
  3678. *
  3679. * If vfs_rename discovers a delegation in need of breaking at either
  3680. * the source or destination, it will return -EWOULDBLOCK and return a
  3681. * reference to the inode in delegated_inode. The caller should then
  3682. * break the delegation and retry. Because breaking a delegation may
  3683. * take a long time, the caller should drop all locks before doing
  3684. * so.
  3685. *
  3686. * Alternatively, a caller may pass NULL for delegated_inode. This may
  3687. * be appropriate for callers that expect the underlying filesystem not
  3688. * to be NFS exported.
  3689. *
  3690. * The worst of all namespace operations - renaming directory. "Perverted"
  3691. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  3692. * Problems:
  3693. * a) we can get into loop creation.
  3694. * b) race potential - two innocent renames can create a loop together.
  3695. * That's where 4.4 screws up. Current fix: serialization on
  3696. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  3697. * story.
  3698. * c) we have to lock _four_ objects - parents and victim (if it exists),
  3699. * and source (if it is not a directory).
  3700. * And that - after we got ->i_mutex on parents (until then we don't know
  3701. * whether the target exists). Solution: try to be smart with locking
  3702. * order for inodes. We rely on the fact that tree topology may change
  3703. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  3704. * move will be locked. Thus we can rank directories by the tree
  3705. * (ancestors first) and rank all non-directories after them.
  3706. * That works since everybody except rename does "lock parent, lookup,
  3707. * lock child" and rename is under ->s_vfs_rename_mutex.
  3708. * HOWEVER, it relies on the assumption that any object with ->lookup()
  3709. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  3710. * we'd better make sure that there's no link(2) for them.
  3711. * d) conversion from fhandle to dentry may come in the wrong moment - when
  3712. * we are removing the target. Solution: we will have to grab ->i_mutex
  3713. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  3714. * ->i_mutex on parents, which works but leads to some truly excessive
  3715. * locking].
  3716. */
  3717. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  3718. struct inode *new_dir, struct dentry *new_dentry,
  3719. struct inode **delegated_inode, unsigned int flags)
  3720. {
  3721. int error;
  3722. bool is_dir = d_is_dir(old_dentry);
  3723. const unsigned char *old_name;
  3724. struct inode *source = old_dentry->d_inode;
  3725. struct inode *target = new_dentry->d_inode;
  3726. bool new_is_dir = false;
  3727. unsigned max_links = new_dir->i_sb->s_max_links;
  3728. if (source == target)
  3729. return 0;
  3730. error = may_delete(old_dir, old_dentry, is_dir);
  3731. if (error)
  3732. return error;
  3733. if (!target) {
  3734. error = may_create(new_dir, new_dentry);
  3735. } else {
  3736. new_is_dir = d_is_dir(new_dentry);
  3737. if (!(flags & RENAME_EXCHANGE))
  3738. error = may_delete(new_dir, new_dentry, is_dir);
  3739. else
  3740. error = may_delete(new_dir, new_dentry, new_is_dir);
  3741. }
  3742. if (error)
  3743. return error;
  3744. if (!old_dir->i_op->rename && !old_dir->i_op->rename2)
  3745. return -EPERM;
  3746. if (flags && !old_dir->i_op->rename2)
  3747. return -EINVAL;
  3748. /*
  3749. * If we are going to change the parent - check write permissions,
  3750. * we'll need to flip '..'.
  3751. */
  3752. if (new_dir != old_dir) {
  3753. if (is_dir) {
  3754. error = inode_permission(source, MAY_WRITE);
  3755. if (error)
  3756. return error;
  3757. }
  3758. if ((flags & RENAME_EXCHANGE) && new_is_dir) {
  3759. error = inode_permission(target, MAY_WRITE);
  3760. if (error)
  3761. return error;
  3762. }
  3763. }
  3764. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
  3765. flags);
  3766. if (error)
  3767. return error;
  3768. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  3769. dget(new_dentry);
  3770. if (!is_dir || (flags & RENAME_EXCHANGE))
  3771. lock_two_nondirectories(source, target);
  3772. else if (target)
  3773. mutex_lock(&target->i_mutex);
  3774. error = -EBUSY;
  3775. if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
  3776. goto out;
  3777. if (max_links && new_dir != old_dir) {
  3778. error = -EMLINK;
  3779. if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
  3780. goto out;
  3781. if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
  3782. old_dir->i_nlink >= max_links)
  3783. goto out;
  3784. }
  3785. if (is_dir && !(flags & RENAME_EXCHANGE) && target)
  3786. shrink_dcache_parent(new_dentry);
  3787. if (!is_dir) {
  3788. error = try_break_deleg(source, delegated_inode);
  3789. if (error)
  3790. goto out;
  3791. }
  3792. if (target && !new_is_dir) {
  3793. error = try_break_deleg(target, delegated_inode);
  3794. if (error)
  3795. goto out;
  3796. }
  3797. if (!old_dir->i_op->rename2) {
  3798. error = old_dir->i_op->rename(old_dir, old_dentry,
  3799. new_dir, new_dentry);
  3800. } else {
  3801. WARN_ON(old_dir->i_op->rename != NULL);
  3802. error = old_dir->i_op->rename2(old_dir, old_dentry,
  3803. new_dir, new_dentry, flags);
  3804. }
  3805. if (error)
  3806. goto out;
  3807. if (!(flags & RENAME_EXCHANGE) && target) {
  3808. if (is_dir)
  3809. target->i_flags |= S_DEAD;
  3810. dont_mount(new_dentry);
  3811. detach_mounts(new_dentry);
  3812. }
  3813. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
  3814. if (!(flags & RENAME_EXCHANGE))
  3815. d_move(old_dentry, new_dentry);
  3816. else
  3817. d_exchange(old_dentry, new_dentry);
  3818. }
  3819. out:
  3820. if (!is_dir || (flags & RENAME_EXCHANGE))
  3821. unlock_two_nondirectories(source, target);
  3822. else if (target)
  3823. mutex_unlock(&target->i_mutex);
  3824. dput(new_dentry);
  3825. if (!error) {
  3826. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  3827. !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
  3828. if (flags & RENAME_EXCHANGE) {
  3829. fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
  3830. new_is_dir, NULL, new_dentry);
  3831. }
  3832. }
  3833. fsnotify_oldname_free(old_name);
  3834. return error;
  3835. }
  3836. EXPORT_SYMBOL(vfs_rename);
  3837. SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
  3838. int, newdfd, const char __user *, newname, unsigned int, flags)
  3839. {
  3840. struct dentry *old_dentry, *new_dentry;
  3841. struct dentry *trap;
  3842. struct path old_path, new_path;
  3843. struct qstr old_last, new_last;
  3844. int old_type, new_type;
  3845. struct inode *delegated_inode = NULL;
  3846. struct filename *from;
  3847. struct filename *to;
  3848. unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
  3849. bool should_retry = false;
  3850. int error;
  3851. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  3852. return -EINVAL;
  3853. if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
  3854. (flags & RENAME_EXCHANGE))
  3855. return -EINVAL;
  3856. if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
  3857. return -EPERM;
  3858. if (flags & RENAME_EXCHANGE)
  3859. target_flags = 0;
  3860. retry:
  3861. from = user_path_parent(olddfd, oldname,
  3862. &old_path, &old_last, &old_type, lookup_flags);
  3863. if (IS_ERR(from)) {
  3864. error = PTR_ERR(from);
  3865. goto exit;
  3866. }
  3867. to = user_path_parent(newdfd, newname,
  3868. &new_path, &new_last, &new_type, lookup_flags);
  3869. if (IS_ERR(to)) {
  3870. error = PTR_ERR(to);
  3871. goto exit1;
  3872. }
  3873. error = -EXDEV;
  3874. if (old_path.mnt != new_path.mnt)
  3875. goto exit2;
  3876. error = -EBUSY;
  3877. if (old_type != LAST_NORM)
  3878. goto exit2;
  3879. if (flags & RENAME_NOREPLACE)
  3880. error = -EEXIST;
  3881. if (new_type != LAST_NORM)
  3882. goto exit2;
  3883. error = mnt_want_write(old_path.mnt);
  3884. if (error)
  3885. goto exit2;
  3886. retry_deleg:
  3887. trap = lock_rename(new_path.dentry, old_path.dentry);
  3888. old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
  3889. error = PTR_ERR(old_dentry);
  3890. if (IS_ERR(old_dentry))
  3891. goto exit3;
  3892. /* source must exist */
  3893. error = -ENOENT;
  3894. if (d_is_negative(old_dentry))
  3895. goto exit4;
  3896. new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
  3897. error = PTR_ERR(new_dentry);
  3898. if (IS_ERR(new_dentry))
  3899. goto exit4;
  3900. error = -EEXIST;
  3901. if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
  3902. goto exit5;
  3903. if (flags & RENAME_EXCHANGE) {
  3904. error = -ENOENT;
  3905. if (d_is_negative(new_dentry))
  3906. goto exit5;
  3907. if (!d_is_dir(new_dentry)) {
  3908. error = -ENOTDIR;
  3909. if (new_last.name[new_last.len])
  3910. goto exit5;
  3911. }
  3912. }
  3913. /* unless the source is a directory trailing slashes give -ENOTDIR */
  3914. if (!d_is_dir(old_dentry)) {
  3915. error = -ENOTDIR;
  3916. if (old_last.name[old_last.len])
  3917. goto exit5;
  3918. if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
  3919. goto exit5;
  3920. }
  3921. /* source should not be ancestor of target */
  3922. error = -EINVAL;
  3923. if (old_dentry == trap)
  3924. goto exit5;
  3925. /* target should not be an ancestor of source */
  3926. if (!(flags & RENAME_EXCHANGE))
  3927. error = -ENOTEMPTY;
  3928. if (new_dentry == trap)
  3929. goto exit5;
  3930. error = security_path_rename(&old_path, old_dentry,
  3931. &new_path, new_dentry, flags);
  3932. if (error)
  3933. goto exit5;
  3934. error = vfs_rename(old_path.dentry->d_inode, old_dentry,
  3935. new_path.dentry->d_inode, new_dentry,
  3936. &delegated_inode, flags);
  3937. exit5:
  3938. dput(new_dentry);
  3939. exit4:
  3940. dput(old_dentry);
  3941. exit3:
  3942. unlock_rename(new_path.dentry, old_path.dentry);
  3943. if (delegated_inode) {
  3944. error = break_deleg_wait(&delegated_inode);
  3945. if (!error)
  3946. goto retry_deleg;
  3947. }
  3948. mnt_drop_write(old_path.mnt);
  3949. exit2:
  3950. if (retry_estale(error, lookup_flags))
  3951. should_retry = true;
  3952. path_put(&new_path);
  3953. putname(to);
  3954. exit1:
  3955. path_put(&old_path);
  3956. putname(from);
  3957. if (should_retry) {
  3958. should_retry = false;
  3959. lookup_flags |= LOOKUP_REVAL;
  3960. goto retry;
  3961. }
  3962. exit:
  3963. return error;
  3964. }
  3965. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  3966. int, newdfd, const char __user *, newname)
  3967. {
  3968. return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
  3969. }
  3970. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  3971. {
  3972. return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  3973. }
  3974. int vfs_whiteout(struct inode *dir, struct dentry *dentry)
  3975. {
  3976. int error = may_create(dir, dentry);
  3977. if (error)
  3978. return error;
  3979. if (!dir->i_op->mknod)
  3980. return -EPERM;
  3981. return dir->i_op->mknod(dir, dentry,
  3982. S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
  3983. }
  3984. EXPORT_SYMBOL(vfs_whiteout);
  3985. int readlink_copy(char __user *buffer, int buflen, const char *link)
  3986. {
  3987. int len = PTR_ERR(link);
  3988. if (IS_ERR(link))
  3989. goto out;
  3990. len = strlen(link);
  3991. if (len > (unsigned) buflen)
  3992. len = buflen;
  3993. if (copy_to_user(buffer, link, len))
  3994. len = -EFAULT;
  3995. out:
  3996. return len;
  3997. }
  3998. EXPORT_SYMBOL(readlink_copy);
  3999. /*
  4000. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  4001. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  4002. * using) it for any given inode is up to filesystem.
  4003. */
  4004. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  4005. {
  4006. void *cookie;
  4007. struct inode *inode = d_inode(dentry);
  4008. const char *link = inode->i_link;
  4009. int res;
  4010. if (!link) {
  4011. link = inode->i_op->follow_link(dentry, &cookie);
  4012. if (IS_ERR(link))
  4013. return PTR_ERR(link);
  4014. }
  4015. res = readlink_copy(buffer, buflen, link);
  4016. if (inode->i_op->put_link)
  4017. inode->i_op->put_link(inode, cookie);
  4018. return res;
  4019. }
  4020. EXPORT_SYMBOL(generic_readlink);
  4021. /* get the link contents into pagecache */
  4022. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  4023. {
  4024. char *kaddr;
  4025. struct page *page;
  4026. struct address_space *mapping = dentry->d_inode->i_mapping;
  4027. page = read_mapping_page(mapping, 0, NULL);
  4028. if (IS_ERR(page))
  4029. return (char*)page;
  4030. *ppage = page;
  4031. kaddr = kmap(page);
  4032. nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
  4033. return kaddr;
  4034. }
  4035. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  4036. {
  4037. struct page *page = NULL;
  4038. int res = readlink_copy(buffer, buflen, page_getlink(dentry, &page));
  4039. if (page) {
  4040. kunmap(page);
  4041. page_cache_release(page);
  4042. }
  4043. return res;
  4044. }
  4045. EXPORT_SYMBOL(page_readlink);
  4046. const char *page_follow_link_light(struct dentry *dentry, void **cookie)
  4047. {
  4048. struct page *page = NULL;
  4049. char *res = page_getlink(dentry, &page);
  4050. if (!IS_ERR(res))
  4051. *cookie = page;
  4052. return res;
  4053. }
  4054. EXPORT_SYMBOL(page_follow_link_light);
  4055. void page_put_link(struct inode *unused, void *cookie)
  4056. {
  4057. struct page *page = cookie;
  4058. kunmap(page);
  4059. page_cache_release(page);
  4060. }
  4061. EXPORT_SYMBOL(page_put_link);
  4062. /*
  4063. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  4064. */
  4065. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  4066. {
  4067. struct address_space *mapping = inode->i_mapping;
  4068. struct page *page;
  4069. void *fsdata;
  4070. int err;
  4071. char *kaddr;
  4072. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  4073. if (nofs)
  4074. flags |= AOP_FLAG_NOFS;
  4075. retry:
  4076. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  4077. flags, &page, &fsdata);
  4078. if (err)
  4079. goto fail;
  4080. kaddr = kmap_atomic(page);
  4081. memcpy(kaddr, symname, len-1);
  4082. kunmap_atomic(kaddr);
  4083. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  4084. page, fsdata);
  4085. if (err < 0)
  4086. goto fail;
  4087. if (err < len-1)
  4088. goto retry;
  4089. mark_inode_dirty(inode);
  4090. return 0;
  4091. fail:
  4092. return err;
  4093. }
  4094. EXPORT_SYMBOL(__page_symlink);
  4095. int page_symlink(struct inode *inode, const char *symname, int len)
  4096. {
  4097. return __page_symlink(inode, symname, len,
  4098. !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
  4099. }
  4100. EXPORT_SYMBOL(page_symlink);
  4101. const struct inode_operations page_symlink_inode_operations = {
  4102. .readlink = generic_readlink,
  4103. .follow_link = page_follow_link_light,
  4104. .put_link = page_put_link,
  4105. };
  4106. EXPORT_SYMBOL(page_symlink_inode_operations);