namei.c 86 KB

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