namei.c 119 KB

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