namei.c 111 KB

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