namei.c 112 KB

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