namei.c 111 KB

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