namei.c 112 KB

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