namei.c 98 KB

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