namei.c 86 KB

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