namei.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549
  1. /*
  2. * linux/fs/namei.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * Some corrections by tytso.
  8. */
  9. /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
  10. * lookup logic.
  11. */
  12. /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/export.h>
  16. #include <linux/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, NULL);
  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);
  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. static struct dentry *__lookup_hash(struct qstr *name,
  988. struct dentry *base, struct nameidata *nd)
  989. {
  990. struct dentry *dentry;
  991. /*
  992. * Don't bother with __d_lookup: callers are for creat as
  993. * well as unlink, so a lot of the time it would cost
  994. * a double lookup.
  995. */
  996. dentry = d_lookup(base, name);
  997. if (dentry && d_need_lookup(dentry)) {
  998. /*
  999. * __lookup_hash is called with the parent dir's i_mutex already
  1000. * held, so we are good to go here.
  1001. */
  1002. return d_inode_lookup(base, dentry, nd);
  1003. }
  1004. if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1005. int status = d_revalidate(dentry, nd);
  1006. if (unlikely(status <= 0)) {
  1007. /*
  1008. * The dentry failed validation.
  1009. * If d_revalidate returned 0 attempt to invalidate
  1010. * the dentry otherwise d_revalidate is asking us
  1011. * to return a fail status.
  1012. */
  1013. if (status < 0) {
  1014. dput(dentry);
  1015. return ERR_PTR(status);
  1016. } else if (!d_invalidate(dentry)) {
  1017. dput(dentry);
  1018. dentry = NULL;
  1019. }
  1020. }
  1021. }
  1022. if (!dentry)
  1023. dentry = d_alloc_and_lookup(base, name, nd);
  1024. return dentry;
  1025. }
  1026. /*
  1027. * It's more convoluted than I'd like it to be, but... it's still fairly
  1028. * small and for now I'd prefer to have fast path as straight as possible.
  1029. * It _is_ time-critical.
  1030. */
  1031. static int do_lookup(struct nameidata *nd, struct qstr *name,
  1032. struct path *path, struct inode **inode)
  1033. {
  1034. struct vfsmount *mnt = nd->path.mnt;
  1035. struct dentry *dentry, *parent = nd->path.dentry;
  1036. int need_reval = 1;
  1037. int status = 1;
  1038. int err;
  1039. /*
  1040. * Rename seqlock is not required here because in the off chance
  1041. * of a false negative due to a concurrent rename, we're going to
  1042. * do the non-racy lookup, below.
  1043. */
  1044. if (nd->flags & LOOKUP_RCU) {
  1045. unsigned seq;
  1046. *inode = nd->inode;
  1047. dentry = __d_lookup_rcu(parent, name, &seq, inode);
  1048. if (!dentry)
  1049. goto unlazy;
  1050. /* Memory barrier in read_seqcount_begin of child is enough */
  1051. if (__read_seqcount_retry(&parent->d_seq, nd->seq))
  1052. return -ECHILD;
  1053. nd->seq = seq;
  1054. if (unlikely(d_need_lookup(dentry)))
  1055. goto unlazy;
  1056. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1057. status = d_revalidate(dentry, nd);
  1058. if (unlikely(status <= 0)) {
  1059. if (status != -ECHILD)
  1060. need_reval = 0;
  1061. goto unlazy;
  1062. }
  1063. }
  1064. path->mnt = mnt;
  1065. path->dentry = dentry;
  1066. if (unlikely(!__follow_mount_rcu(nd, path, inode)))
  1067. goto unlazy;
  1068. if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
  1069. goto unlazy;
  1070. return 0;
  1071. unlazy:
  1072. if (unlazy_walk(nd, dentry))
  1073. return -ECHILD;
  1074. } else {
  1075. dentry = __d_lookup(parent, name);
  1076. }
  1077. if (dentry && unlikely(d_need_lookup(dentry))) {
  1078. dput(dentry);
  1079. dentry = NULL;
  1080. }
  1081. retry:
  1082. if (unlikely(!dentry)) {
  1083. struct inode *dir = parent->d_inode;
  1084. BUG_ON(nd->inode != dir);
  1085. mutex_lock(&dir->i_mutex);
  1086. dentry = __lookup_hash(name, parent, nd);
  1087. mutex_unlock(&dir->i_mutex);
  1088. if (IS_ERR(dentry))
  1089. return PTR_ERR(dentry);
  1090. goto done;
  1091. }
  1092. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
  1093. status = d_revalidate(dentry, nd);
  1094. if (unlikely(status <= 0)) {
  1095. if (status < 0) {
  1096. dput(dentry);
  1097. return status;
  1098. }
  1099. if (!d_invalidate(dentry)) {
  1100. dput(dentry);
  1101. dentry = NULL;
  1102. goto retry;
  1103. }
  1104. }
  1105. done:
  1106. path->mnt = mnt;
  1107. path->dentry = dentry;
  1108. err = follow_managed(path, nd->flags);
  1109. if (unlikely(err < 0)) {
  1110. path_put_conditional(path, nd);
  1111. return err;
  1112. }
  1113. if (err)
  1114. nd->flags |= LOOKUP_JUMPED;
  1115. *inode = path->dentry->d_inode;
  1116. return 0;
  1117. }
  1118. static inline int may_lookup(struct nameidata *nd)
  1119. {
  1120. if (nd->flags & LOOKUP_RCU) {
  1121. int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
  1122. if (err != -ECHILD)
  1123. return err;
  1124. if (unlazy_walk(nd, NULL))
  1125. return -ECHILD;
  1126. }
  1127. return inode_permission(nd->inode, MAY_EXEC);
  1128. }
  1129. static inline int handle_dots(struct nameidata *nd, int type)
  1130. {
  1131. if (type == LAST_DOTDOT) {
  1132. if (nd->flags & LOOKUP_RCU) {
  1133. if (follow_dotdot_rcu(nd))
  1134. return -ECHILD;
  1135. } else
  1136. follow_dotdot(nd);
  1137. }
  1138. return 0;
  1139. }
  1140. static void terminate_walk(struct nameidata *nd)
  1141. {
  1142. if (!(nd->flags & LOOKUP_RCU)) {
  1143. path_put(&nd->path);
  1144. } else {
  1145. nd->flags &= ~LOOKUP_RCU;
  1146. if (!(nd->flags & LOOKUP_ROOT))
  1147. nd->root.mnt = NULL;
  1148. rcu_read_unlock();
  1149. br_read_unlock(vfsmount_lock);
  1150. }
  1151. }
  1152. /*
  1153. * Do we need to follow links? We _really_ want to be able
  1154. * to do this check without having to look at inode->i_op,
  1155. * so we keep a cache of "no, this doesn't need follow_link"
  1156. * for the common case.
  1157. */
  1158. static inline int should_follow_link(struct inode *inode, int follow)
  1159. {
  1160. if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
  1161. if (likely(inode->i_op->follow_link))
  1162. return follow;
  1163. /* This gets set once for the inode lifetime */
  1164. spin_lock(&inode->i_lock);
  1165. inode->i_opflags |= IOP_NOFOLLOW;
  1166. spin_unlock(&inode->i_lock);
  1167. }
  1168. return 0;
  1169. }
  1170. static inline int walk_component(struct nameidata *nd, struct path *path,
  1171. struct qstr *name, int type, int follow)
  1172. {
  1173. struct inode *inode;
  1174. int err;
  1175. /*
  1176. * "." and ".." are special - ".." especially so because it has
  1177. * to be able to know about the current root directory and
  1178. * parent relationships.
  1179. */
  1180. if (unlikely(type != LAST_NORM))
  1181. return handle_dots(nd, type);
  1182. err = do_lookup(nd, name, path, &inode);
  1183. if (unlikely(err)) {
  1184. terminate_walk(nd);
  1185. return err;
  1186. }
  1187. if (!inode) {
  1188. path_to_nameidata(path, nd);
  1189. terminate_walk(nd);
  1190. return -ENOENT;
  1191. }
  1192. if (should_follow_link(inode, follow)) {
  1193. if (nd->flags & LOOKUP_RCU) {
  1194. if (unlikely(unlazy_walk(nd, path->dentry))) {
  1195. terminate_walk(nd);
  1196. return -ECHILD;
  1197. }
  1198. }
  1199. BUG_ON(inode != path->dentry->d_inode);
  1200. return 1;
  1201. }
  1202. path_to_nameidata(path, nd);
  1203. nd->inode = inode;
  1204. return 0;
  1205. }
  1206. /*
  1207. * This limits recursive symlink follows to 8, while
  1208. * limiting consecutive symlinks to 40.
  1209. *
  1210. * Without that kind of total limit, nasty chains of consecutive
  1211. * symlinks can cause almost arbitrarily long lookups.
  1212. */
  1213. static inline int nested_symlink(struct path *path, struct nameidata *nd)
  1214. {
  1215. int res;
  1216. if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
  1217. path_put_conditional(path, nd);
  1218. path_put(&nd->path);
  1219. return -ELOOP;
  1220. }
  1221. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  1222. nd->depth++;
  1223. current->link_count++;
  1224. do {
  1225. struct path link = *path;
  1226. void *cookie;
  1227. res = follow_link(&link, nd, &cookie);
  1228. if (!res)
  1229. res = walk_component(nd, path, &nd->last,
  1230. nd->last_type, LOOKUP_FOLLOW);
  1231. put_link(nd, &link, cookie);
  1232. } while (res > 0);
  1233. current->link_count--;
  1234. nd->depth--;
  1235. return res;
  1236. }
  1237. /*
  1238. * We really don't want to look at inode->i_op->lookup
  1239. * when we don't have to. So we keep a cache bit in
  1240. * the inode ->i_opflags field that says "yes, we can
  1241. * do lookup on this inode".
  1242. */
  1243. static inline int can_lookup(struct inode *inode)
  1244. {
  1245. if (likely(inode->i_opflags & IOP_LOOKUP))
  1246. return 1;
  1247. if (likely(!inode->i_op->lookup))
  1248. return 0;
  1249. /* We do this once for the lifetime of the inode */
  1250. spin_lock(&inode->i_lock);
  1251. inode->i_opflags |= IOP_LOOKUP;
  1252. spin_unlock(&inode->i_lock);
  1253. return 1;
  1254. }
  1255. /*
  1256. * We can do the critical dentry name comparison and hashing
  1257. * operations one word at a time, but we are limited to:
  1258. *
  1259. * - Architectures with fast unaligned word accesses. We could
  1260. * do a "get_unaligned()" if this helps and is sufficiently
  1261. * fast.
  1262. *
  1263. * - Little-endian machines (so that we can generate the mask
  1264. * of low bytes efficiently). Again, we *could* do a byte
  1265. * swapping load on big-endian architectures if that is not
  1266. * expensive enough to make the optimization worthless.
  1267. *
  1268. * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
  1269. * do not trap on the (extremely unlikely) case of a page
  1270. * crossing operation.
  1271. *
  1272. * - Furthermore, we need an efficient 64-bit compile for the
  1273. * 64-bit case in order to generate the "number of bytes in
  1274. * the final mask". Again, that could be replaced with a
  1275. * efficient population count instruction or similar.
  1276. */
  1277. #ifdef CONFIG_DCACHE_WORD_ACCESS
  1278. #ifdef CONFIG_64BIT
  1279. /*
  1280. * Jan Achrenius on G+: microoptimized version of
  1281. * the simpler "(mask & ONEBYTES) * ONEBYTES >> 56"
  1282. * that works for the bytemasks without having to
  1283. * mask them first.
  1284. */
  1285. static inline long count_masked_bytes(unsigned long mask)
  1286. {
  1287. return mask*0x0001020304050608ul >> 56;
  1288. }
  1289. static inline unsigned int fold_hash(unsigned long hash)
  1290. {
  1291. hash += hash >> (8*sizeof(int));
  1292. return hash;
  1293. }
  1294. #else /* 32-bit case */
  1295. /* Carl Chatfield / Jan Achrenius G+ version for 32-bit */
  1296. static inline long count_masked_bytes(long mask)
  1297. {
  1298. /* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
  1299. long a = (0x0ff0001+mask) >> 23;
  1300. /* Fix the 1 for 00 case */
  1301. return a & mask;
  1302. }
  1303. #define fold_hash(x) (x)
  1304. #endif
  1305. unsigned int full_name_hash(const unsigned char *name, unsigned int len)
  1306. {
  1307. unsigned long a, mask;
  1308. unsigned long hash = 0;
  1309. for (;;) {
  1310. a = *(unsigned long *)name;
  1311. if (len < sizeof(unsigned long))
  1312. break;
  1313. hash += a;
  1314. hash *= 9;
  1315. name += sizeof(unsigned long);
  1316. len -= sizeof(unsigned long);
  1317. if (!len)
  1318. goto done;
  1319. }
  1320. mask = ~(~0ul << len*8);
  1321. hash += mask & a;
  1322. done:
  1323. return fold_hash(hash);
  1324. }
  1325. EXPORT_SYMBOL(full_name_hash);
  1326. #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
  1327. #define ONEBYTES REPEAT_BYTE(0x01)
  1328. #define SLASHBYTES REPEAT_BYTE('/')
  1329. #define HIGHBITS REPEAT_BYTE(0x80)
  1330. /* Return the high bit set in the first byte that is a zero */
  1331. static inline unsigned long has_zero(unsigned long a)
  1332. {
  1333. return ((a - ONEBYTES) & ~a) & HIGHBITS;
  1334. }
  1335. /*
  1336. * Calculate the length and hash of the path component, and
  1337. * return the length of the component;
  1338. */
  1339. static inline unsigned long hash_name(const char *name, unsigned int *hashp)
  1340. {
  1341. unsigned long a, mask, hash, len;
  1342. hash = a = 0;
  1343. len = -sizeof(unsigned long);
  1344. do {
  1345. hash = (hash + a) * 9;
  1346. len += sizeof(unsigned long);
  1347. a = *(unsigned long *)(name+len);
  1348. /* Do we have any NUL or '/' bytes in this word? */
  1349. mask = has_zero(a) | has_zero(a ^ SLASHBYTES);
  1350. } while (!mask);
  1351. /* The mask *below* the first high bit set */
  1352. mask = (mask - 1) & ~mask;
  1353. mask >>= 7;
  1354. hash += a & mask;
  1355. *hashp = fold_hash(hash);
  1356. return len + count_masked_bytes(mask);
  1357. }
  1358. #else
  1359. unsigned int full_name_hash(const unsigned char *name, unsigned int len)
  1360. {
  1361. unsigned long hash = init_name_hash();
  1362. while (len--)
  1363. hash = partial_name_hash(*name++, hash);
  1364. return end_name_hash(hash);
  1365. }
  1366. EXPORT_SYMBOL(full_name_hash);
  1367. /*
  1368. * We know there's a real path component here of at least
  1369. * one character.
  1370. */
  1371. static inline unsigned long hash_name(const char *name, unsigned int *hashp)
  1372. {
  1373. unsigned long hash = init_name_hash();
  1374. unsigned long len = 0, c;
  1375. c = (unsigned char)*name;
  1376. do {
  1377. len++;
  1378. hash = partial_name_hash(c, hash);
  1379. c = (unsigned char)name[len];
  1380. } while (c && c != '/');
  1381. *hashp = end_name_hash(hash);
  1382. return len;
  1383. }
  1384. #endif
  1385. /*
  1386. * Name resolution.
  1387. * This is the basic name resolution function, turning a pathname into
  1388. * the final dentry. We expect 'base' to be positive and a directory.
  1389. *
  1390. * Returns 0 and nd will have valid dentry and mnt on success.
  1391. * Returns error and drops reference to input namei data on failure.
  1392. */
  1393. static int link_path_walk(const char *name, struct nameidata *nd)
  1394. {
  1395. struct path next;
  1396. int err;
  1397. while (*name=='/')
  1398. name++;
  1399. if (!*name)
  1400. return 0;
  1401. /* At this point we know we have a real path component. */
  1402. for(;;) {
  1403. struct qstr this;
  1404. long len;
  1405. int type;
  1406. err = may_lookup(nd);
  1407. if (err)
  1408. break;
  1409. len = hash_name(name, &this.hash);
  1410. this.name = name;
  1411. this.len = len;
  1412. type = LAST_NORM;
  1413. if (name[0] == '.') switch (len) {
  1414. case 2:
  1415. if (name[1] == '.') {
  1416. type = LAST_DOTDOT;
  1417. nd->flags |= LOOKUP_JUMPED;
  1418. }
  1419. break;
  1420. case 1:
  1421. type = LAST_DOT;
  1422. }
  1423. if (likely(type == LAST_NORM)) {
  1424. struct dentry *parent = nd->path.dentry;
  1425. nd->flags &= ~LOOKUP_JUMPED;
  1426. if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
  1427. err = parent->d_op->d_hash(parent, nd->inode,
  1428. &this);
  1429. if (err < 0)
  1430. break;
  1431. }
  1432. }
  1433. if (!name[len])
  1434. goto last_component;
  1435. /*
  1436. * If it wasn't NUL, we know it was '/'. Skip that
  1437. * slash, and continue until no more slashes.
  1438. */
  1439. do {
  1440. len++;
  1441. } while (unlikely(name[len] == '/'));
  1442. if (!name[len])
  1443. goto last_component;
  1444. name += len;
  1445. err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
  1446. if (err < 0)
  1447. return err;
  1448. if (err) {
  1449. err = nested_symlink(&next, nd);
  1450. if (err)
  1451. return err;
  1452. }
  1453. if (can_lookup(nd->inode))
  1454. continue;
  1455. err = -ENOTDIR;
  1456. break;
  1457. /* here ends the main loop */
  1458. last_component:
  1459. nd->last = this;
  1460. nd->last_type = type;
  1461. return 0;
  1462. }
  1463. terminate_walk(nd);
  1464. return err;
  1465. }
  1466. static int path_init(int dfd, const char *name, unsigned int flags,
  1467. struct nameidata *nd, struct file **fp)
  1468. {
  1469. int retval = 0;
  1470. int fput_needed;
  1471. struct file *file;
  1472. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1473. nd->flags = flags | LOOKUP_JUMPED;
  1474. nd->depth = 0;
  1475. if (flags & LOOKUP_ROOT) {
  1476. struct inode *inode = nd->root.dentry->d_inode;
  1477. if (*name) {
  1478. if (!inode->i_op->lookup)
  1479. return -ENOTDIR;
  1480. retval = inode_permission(inode, MAY_EXEC);
  1481. if (retval)
  1482. return retval;
  1483. }
  1484. nd->path = nd->root;
  1485. nd->inode = inode;
  1486. if (flags & LOOKUP_RCU) {
  1487. br_read_lock(vfsmount_lock);
  1488. rcu_read_lock();
  1489. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1490. } else {
  1491. path_get(&nd->path);
  1492. }
  1493. return 0;
  1494. }
  1495. nd->root.mnt = NULL;
  1496. if (*name=='/') {
  1497. if (flags & LOOKUP_RCU) {
  1498. br_read_lock(vfsmount_lock);
  1499. rcu_read_lock();
  1500. set_root_rcu(nd);
  1501. } else {
  1502. set_root(nd);
  1503. path_get(&nd->root);
  1504. }
  1505. nd->path = nd->root;
  1506. } else if (dfd == AT_FDCWD) {
  1507. if (flags & LOOKUP_RCU) {
  1508. struct fs_struct *fs = current->fs;
  1509. unsigned seq;
  1510. br_read_lock(vfsmount_lock);
  1511. rcu_read_lock();
  1512. do {
  1513. seq = read_seqcount_begin(&fs->seq);
  1514. nd->path = fs->pwd;
  1515. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1516. } while (read_seqcount_retry(&fs->seq, seq));
  1517. } else {
  1518. get_fs_pwd(current->fs, &nd->path);
  1519. }
  1520. } else {
  1521. struct dentry *dentry;
  1522. file = fget_raw_light(dfd, &fput_needed);
  1523. retval = -EBADF;
  1524. if (!file)
  1525. goto out_fail;
  1526. dentry = file->f_path.dentry;
  1527. if (*name) {
  1528. retval = -ENOTDIR;
  1529. if (!S_ISDIR(dentry->d_inode->i_mode))
  1530. goto fput_fail;
  1531. retval = inode_permission(dentry->d_inode, MAY_EXEC);
  1532. if (retval)
  1533. goto fput_fail;
  1534. }
  1535. nd->path = file->f_path;
  1536. if (flags & LOOKUP_RCU) {
  1537. if (fput_needed)
  1538. *fp = file;
  1539. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1540. br_read_lock(vfsmount_lock);
  1541. rcu_read_lock();
  1542. } else {
  1543. path_get(&file->f_path);
  1544. fput_light(file, fput_needed);
  1545. }
  1546. }
  1547. nd->inode = nd->path.dentry->d_inode;
  1548. return 0;
  1549. fput_fail:
  1550. fput_light(file, fput_needed);
  1551. out_fail:
  1552. return retval;
  1553. }
  1554. static inline int lookup_last(struct nameidata *nd, struct path *path)
  1555. {
  1556. if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
  1557. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1558. nd->flags &= ~LOOKUP_PARENT;
  1559. return walk_component(nd, path, &nd->last, nd->last_type,
  1560. nd->flags & LOOKUP_FOLLOW);
  1561. }
  1562. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1563. static int path_lookupat(int dfd, const char *name,
  1564. unsigned int flags, struct nameidata *nd)
  1565. {
  1566. struct file *base = NULL;
  1567. struct path path;
  1568. int err;
  1569. /*
  1570. * Path walking is largely split up into 2 different synchronisation
  1571. * schemes, rcu-walk and ref-walk (explained in
  1572. * Documentation/filesystems/path-lookup.txt). These share much of the
  1573. * path walk code, but some things particularly setup, cleanup, and
  1574. * following mounts are sufficiently divergent that functions are
  1575. * duplicated. Typically there is a function foo(), and its RCU
  1576. * analogue, foo_rcu().
  1577. *
  1578. * -ECHILD is the error number of choice (just to avoid clashes) that
  1579. * is returned if some aspect of an rcu-walk fails. Such an error must
  1580. * be handled by restarting a traditional ref-walk (which will always
  1581. * be able to complete).
  1582. */
  1583. err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
  1584. if (unlikely(err))
  1585. return err;
  1586. current->total_link_count = 0;
  1587. err = link_path_walk(name, nd);
  1588. if (!err && !(flags & LOOKUP_PARENT)) {
  1589. err = lookup_last(nd, &path);
  1590. while (err > 0) {
  1591. void *cookie;
  1592. struct path link = path;
  1593. nd->flags |= LOOKUP_PARENT;
  1594. err = follow_link(&link, nd, &cookie);
  1595. if (!err)
  1596. err = lookup_last(nd, &path);
  1597. put_link(nd, &link, cookie);
  1598. }
  1599. }
  1600. if (!err)
  1601. err = complete_walk(nd);
  1602. if (!err && nd->flags & LOOKUP_DIRECTORY) {
  1603. if (!nd->inode->i_op->lookup) {
  1604. path_put(&nd->path);
  1605. err = -ENOTDIR;
  1606. }
  1607. }
  1608. if (base)
  1609. fput(base);
  1610. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  1611. path_put(&nd->root);
  1612. nd->root.mnt = NULL;
  1613. }
  1614. return err;
  1615. }
  1616. static int do_path_lookup(int dfd, const char *name,
  1617. unsigned int flags, struct nameidata *nd)
  1618. {
  1619. int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
  1620. if (unlikely(retval == -ECHILD))
  1621. retval = path_lookupat(dfd, name, flags, nd);
  1622. if (unlikely(retval == -ESTALE))
  1623. retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
  1624. if (likely(!retval)) {
  1625. if (unlikely(!audit_dummy_context())) {
  1626. if (nd->path.dentry && nd->inode)
  1627. audit_inode(name, nd->path.dentry);
  1628. }
  1629. }
  1630. return retval;
  1631. }
  1632. int kern_path_parent(const char *name, struct nameidata *nd)
  1633. {
  1634. return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
  1635. }
  1636. int kern_path(const char *name, unsigned int flags, struct path *path)
  1637. {
  1638. struct nameidata nd;
  1639. int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
  1640. if (!res)
  1641. *path = nd.path;
  1642. return res;
  1643. }
  1644. /**
  1645. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  1646. * @dentry: pointer to dentry of the base directory
  1647. * @mnt: pointer to vfs mount of the base directory
  1648. * @name: pointer to file name
  1649. * @flags: lookup flags
  1650. * @path: pointer to struct path to fill
  1651. */
  1652. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  1653. const char *name, unsigned int flags,
  1654. struct path *path)
  1655. {
  1656. struct nameidata nd;
  1657. int err;
  1658. nd.root.dentry = dentry;
  1659. nd.root.mnt = mnt;
  1660. BUG_ON(flags & LOOKUP_PARENT);
  1661. /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
  1662. err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
  1663. if (!err)
  1664. *path = nd.path;
  1665. return err;
  1666. }
  1667. /*
  1668. * Restricted form of lookup. Doesn't follow links, single-component only,
  1669. * needs parent already locked. Doesn't follow mounts.
  1670. * SMP-safe.
  1671. */
  1672. static struct dentry *lookup_hash(struct nameidata *nd)
  1673. {
  1674. return __lookup_hash(&nd->last, nd->path.dentry, nd);
  1675. }
  1676. /**
  1677. * lookup_one_len - filesystem helper to lookup single pathname component
  1678. * @name: pathname component to lookup
  1679. * @base: base directory to lookup from
  1680. * @len: maximum length @len should be interpreted to
  1681. *
  1682. * Note that this routine is purely a helper for filesystem usage and should
  1683. * not be called by generic code. Also note that by using this function the
  1684. * nameidata argument is passed to the filesystem methods and a filesystem
  1685. * using this helper needs to be prepared for that.
  1686. */
  1687. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1688. {
  1689. struct qstr this;
  1690. unsigned int c;
  1691. int err;
  1692. WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
  1693. this.name = name;
  1694. this.len = len;
  1695. this.hash = full_name_hash(name, len);
  1696. if (!len)
  1697. return ERR_PTR(-EACCES);
  1698. while (len--) {
  1699. c = *(const unsigned char *)name++;
  1700. if (c == '/' || c == '\0')
  1701. return ERR_PTR(-EACCES);
  1702. }
  1703. /*
  1704. * See if the low-level filesystem might want
  1705. * to use its own hash..
  1706. */
  1707. if (base->d_flags & DCACHE_OP_HASH) {
  1708. int err = base->d_op->d_hash(base, base->d_inode, &this);
  1709. if (err < 0)
  1710. return ERR_PTR(err);
  1711. }
  1712. err = inode_permission(base->d_inode, MAY_EXEC);
  1713. if (err)
  1714. return ERR_PTR(err);
  1715. return __lookup_hash(&this, base, NULL);
  1716. }
  1717. int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
  1718. struct path *path, int *empty)
  1719. {
  1720. struct nameidata nd;
  1721. char *tmp = getname_flags(name, flags, empty);
  1722. int err = PTR_ERR(tmp);
  1723. if (!IS_ERR(tmp)) {
  1724. BUG_ON(flags & LOOKUP_PARENT);
  1725. err = do_path_lookup(dfd, tmp, flags, &nd);
  1726. putname(tmp);
  1727. if (!err)
  1728. *path = nd.path;
  1729. }
  1730. return err;
  1731. }
  1732. int user_path_at(int dfd, const char __user *name, unsigned flags,
  1733. struct path *path)
  1734. {
  1735. return user_path_at_empty(dfd, name, flags, path, NULL);
  1736. }
  1737. static int user_path_parent(int dfd, const char __user *path,
  1738. struct nameidata *nd, char **name)
  1739. {
  1740. char *s = getname(path);
  1741. int error;
  1742. if (IS_ERR(s))
  1743. return PTR_ERR(s);
  1744. error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
  1745. if (error)
  1746. putname(s);
  1747. else
  1748. *name = s;
  1749. return error;
  1750. }
  1751. /*
  1752. * It's inline, so penalty for filesystems that don't use sticky bit is
  1753. * minimal.
  1754. */
  1755. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1756. {
  1757. uid_t fsuid = current_fsuid();
  1758. if (!(dir->i_mode & S_ISVTX))
  1759. return 0;
  1760. if (current_user_ns() != inode_userns(inode))
  1761. goto other_userns;
  1762. if (inode->i_uid == fsuid)
  1763. return 0;
  1764. if (dir->i_uid == fsuid)
  1765. return 0;
  1766. other_userns:
  1767. return !ns_capable(inode_userns(inode), CAP_FOWNER);
  1768. }
  1769. /*
  1770. * Check whether we can remove a link victim from directory dir, check
  1771. * whether the type of victim is right.
  1772. * 1. We can't do it if dir is read-only (done in permission())
  1773. * 2. We should have write and exec permissions on dir
  1774. * 3. We can't remove anything from append-only dir
  1775. * 4. We can't do anything with immutable dir (done in permission())
  1776. * 5. If the sticky bit on dir is set we should either
  1777. * a. be owner of dir, or
  1778. * b. be owner of victim, or
  1779. * c. have CAP_FOWNER capability
  1780. * 6. If the victim is append-only or immutable we can't do antyhing with
  1781. * links pointing to it.
  1782. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1783. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1784. * 9. We can't remove a root or mountpoint.
  1785. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1786. * nfs_async_unlink().
  1787. */
  1788. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1789. {
  1790. int error;
  1791. if (!victim->d_inode)
  1792. return -ENOENT;
  1793. BUG_ON(victim->d_parent->d_inode != dir);
  1794. audit_inode_child(victim, dir);
  1795. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1796. if (error)
  1797. return error;
  1798. if (IS_APPEND(dir))
  1799. return -EPERM;
  1800. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1801. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  1802. return -EPERM;
  1803. if (isdir) {
  1804. if (!S_ISDIR(victim->d_inode->i_mode))
  1805. return -ENOTDIR;
  1806. if (IS_ROOT(victim))
  1807. return -EBUSY;
  1808. } else if (S_ISDIR(victim->d_inode->i_mode))
  1809. return -EISDIR;
  1810. if (IS_DEADDIR(dir))
  1811. return -ENOENT;
  1812. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1813. return -EBUSY;
  1814. return 0;
  1815. }
  1816. /* Check whether we can create an object with dentry child in directory
  1817. * dir.
  1818. * 1. We can't do it if child already exists (open has special treatment for
  1819. * this case, but since we are inlined it's OK)
  1820. * 2. We can't do it if dir is read-only (done in permission())
  1821. * 3. We should have write and exec permissions on dir
  1822. * 4. We can't do it if dir is immutable (done in permission())
  1823. */
  1824. static inline int may_create(struct inode *dir, struct dentry *child)
  1825. {
  1826. if (child->d_inode)
  1827. return -EEXIST;
  1828. if (IS_DEADDIR(dir))
  1829. return -ENOENT;
  1830. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1831. }
  1832. /*
  1833. * p1 and p2 should be directories on the same fs.
  1834. */
  1835. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1836. {
  1837. struct dentry *p;
  1838. if (p1 == p2) {
  1839. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1840. return NULL;
  1841. }
  1842. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1843. p = d_ancestor(p2, p1);
  1844. if (p) {
  1845. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1846. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1847. return p;
  1848. }
  1849. p = d_ancestor(p1, p2);
  1850. if (p) {
  1851. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1852. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1853. return p;
  1854. }
  1855. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1856. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1857. return NULL;
  1858. }
  1859. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1860. {
  1861. mutex_unlock(&p1->d_inode->i_mutex);
  1862. if (p1 != p2) {
  1863. mutex_unlock(&p2->d_inode->i_mutex);
  1864. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1865. }
  1866. }
  1867. int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  1868. struct nameidata *nd)
  1869. {
  1870. int error = may_create(dir, dentry);
  1871. if (error)
  1872. return error;
  1873. if (!dir->i_op->create)
  1874. return -EACCES; /* shouldn't it be ENOSYS? */
  1875. mode &= S_IALLUGO;
  1876. mode |= S_IFREG;
  1877. error = security_inode_create(dir, dentry, mode);
  1878. if (error)
  1879. return error;
  1880. error = dir->i_op->create(dir, dentry, mode, nd);
  1881. if (!error)
  1882. fsnotify_create(dir, dentry);
  1883. return error;
  1884. }
  1885. static int may_open(struct path *path, int acc_mode, int flag)
  1886. {
  1887. struct dentry *dentry = path->dentry;
  1888. struct inode *inode = dentry->d_inode;
  1889. int error;
  1890. /* O_PATH? */
  1891. if (!acc_mode)
  1892. return 0;
  1893. if (!inode)
  1894. return -ENOENT;
  1895. switch (inode->i_mode & S_IFMT) {
  1896. case S_IFLNK:
  1897. return -ELOOP;
  1898. case S_IFDIR:
  1899. if (acc_mode & MAY_WRITE)
  1900. return -EISDIR;
  1901. break;
  1902. case S_IFBLK:
  1903. case S_IFCHR:
  1904. if (path->mnt->mnt_flags & MNT_NODEV)
  1905. return -EACCES;
  1906. /*FALLTHRU*/
  1907. case S_IFIFO:
  1908. case S_IFSOCK:
  1909. flag &= ~O_TRUNC;
  1910. break;
  1911. }
  1912. error = inode_permission(inode, acc_mode);
  1913. if (error)
  1914. return error;
  1915. /*
  1916. * An append-only file must be opened in append mode for writing.
  1917. */
  1918. if (IS_APPEND(inode)) {
  1919. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  1920. return -EPERM;
  1921. if (flag & O_TRUNC)
  1922. return -EPERM;
  1923. }
  1924. /* O_NOATIME can only be set by the owner or superuser */
  1925. if (flag & O_NOATIME && !inode_owner_or_capable(inode))
  1926. return -EPERM;
  1927. return 0;
  1928. }
  1929. static int handle_truncate(struct file *filp)
  1930. {
  1931. struct path *path = &filp->f_path;
  1932. struct inode *inode = path->dentry->d_inode;
  1933. int error = get_write_access(inode);
  1934. if (error)
  1935. return error;
  1936. /*
  1937. * Refuse to truncate files with mandatory locks held on them.
  1938. */
  1939. error = locks_verify_locked(inode);
  1940. if (!error)
  1941. error = security_path_truncate(path);
  1942. if (!error) {
  1943. error = do_truncate(path->dentry, 0,
  1944. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  1945. filp);
  1946. }
  1947. put_write_access(inode);
  1948. return error;
  1949. }
  1950. static inline int open_to_namei_flags(int flag)
  1951. {
  1952. if ((flag & O_ACCMODE) == 3)
  1953. flag--;
  1954. return flag;
  1955. }
  1956. /*
  1957. * Handle the last step of open()
  1958. */
  1959. static struct file *do_last(struct nameidata *nd, struct path *path,
  1960. const struct open_flags *op, const char *pathname)
  1961. {
  1962. struct dentry *dir = nd->path.dentry;
  1963. struct dentry *dentry;
  1964. int open_flag = op->open_flag;
  1965. int will_truncate = open_flag & O_TRUNC;
  1966. int want_write = 0;
  1967. int acc_mode = op->acc_mode;
  1968. struct file *filp;
  1969. int error;
  1970. nd->flags &= ~LOOKUP_PARENT;
  1971. nd->flags |= op->intent;
  1972. switch (nd->last_type) {
  1973. case LAST_DOTDOT:
  1974. case LAST_DOT:
  1975. error = handle_dots(nd, nd->last_type);
  1976. if (error)
  1977. return ERR_PTR(error);
  1978. /* fallthrough */
  1979. case LAST_ROOT:
  1980. error = complete_walk(nd);
  1981. if (error)
  1982. return ERR_PTR(error);
  1983. audit_inode(pathname, nd->path.dentry);
  1984. if (open_flag & O_CREAT) {
  1985. error = -EISDIR;
  1986. goto exit;
  1987. }
  1988. goto ok;
  1989. case LAST_BIND:
  1990. error = complete_walk(nd);
  1991. if (error)
  1992. return ERR_PTR(error);
  1993. audit_inode(pathname, dir);
  1994. goto ok;
  1995. }
  1996. if (!(open_flag & O_CREAT)) {
  1997. int symlink_ok = 0;
  1998. if (nd->last.name[nd->last.len])
  1999. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  2000. if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
  2001. symlink_ok = 1;
  2002. /* we _can_ be in RCU mode here */
  2003. error = walk_component(nd, path, &nd->last, LAST_NORM,
  2004. !symlink_ok);
  2005. if (error < 0)
  2006. return ERR_PTR(error);
  2007. if (error) /* symlink */
  2008. return NULL;
  2009. /* sayonara */
  2010. error = complete_walk(nd);
  2011. if (error)
  2012. return ERR_PTR(error);
  2013. error = -ENOTDIR;
  2014. if (nd->flags & LOOKUP_DIRECTORY) {
  2015. if (!nd->inode->i_op->lookup)
  2016. goto exit;
  2017. }
  2018. audit_inode(pathname, nd->path.dentry);
  2019. goto ok;
  2020. }
  2021. /* create side of things */
  2022. /*
  2023. * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED has been
  2024. * cleared when we got to the last component we are about to look up
  2025. */
  2026. error = complete_walk(nd);
  2027. if (error)
  2028. return ERR_PTR(error);
  2029. audit_inode(pathname, dir);
  2030. error = -EISDIR;
  2031. /* trailing slashes? */
  2032. if (nd->last.name[nd->last.len])
  2033. goto exit;
  2034. mutex_lock(&dir->d_inode->i_mutex);
  2035. dentry = lookup_hash(nd);
  2036. error = PTR_ERR(dentry);
  2037. if (IS_ERR(dentry)) {
  2038. mutex_unlock(&dir->d_inode->i_mutex);
  2039. goto exit;
  2040. }
  2041. path->dentry = dentry;
  2042. path->mnt = nd->path.mnt;
  2043. /* Negative dentry, just create the file */
  2044. if (!dentry->d_inode) {
  2045. umode_t mode = op->mode;
  2046. if (!IS_POSIXACL(dir->d_inode))
  2047. mode &= ~current_umask();
  2048. /*
  2049. * This write is needed to ensure that a
  2050. * rw->ro transition does not occur between
  2051. * the time when the file is created and when
  2052. * a permanent write count is taken through
  2053. * the 'struct file' in nameidata_to_filp().
  2054. */
  2055. error = mnt_want_write(nd->path.mnt);
  2056. if (error)
  2057. goto exit_mutex_unlock;
  2058. want_write = 1;
  2059. /* Don't check for write permission, don't truncate */
  2060. open_flag &= ~O_TRUNC;
  2061. will_truncate = 0;
  2062. acc_mode = MAY_OPEN;
  2063. error = security_path_mknod(&nd->path, dentry, mode, 0);
  2064. if (error)
  2065. goto exit_mutex_unlock;
  2066. error = vfs_create(dir->d_inode, dentry, mode, nd);
  2067. if (error)
  2068. goto exit_mutex_unlock;
  2069. mutex_unlock(&dir->d_inode->i_mutex);
  2070. dput(nd->path.dentry);
  2071. nd->path.dentry = dentry;
  2072. goto common;
  2073. }
  2074. /*
  2075. * It already exists.
  2076. */
  2077. mutex_unlock(&dir->d_inode->i_mutex);
  2078. audit_inode(pathname, path->dentry);
  2079. error = -EEXIST;
  2080. if (open_flag & O_EXCL)
  2081. goto exit_dput;
  2082. error = follow_managed(path, nd->flags);
  2083. if (error < 0)
  2084. goto exit_dput;
  2085. if (error)
  2086. nd->flags |= LOOKUP_JUMPED;
  2087. error = -ENOENT;
  2088. if (!path->dentry->d_inode)
  2089. goto exit_dput;
  2090. if (path->dentry->d_inode->i_op->follow_link)
  2091. return NULL;
  2092. path_to_nameidata(path, nd);
  2093. nd->inode = path->dentry->d_inode;
  2094. /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
  2095. error = complete_walk(nd);
  2096. if (error)
  2097. return ERR_PTR(error);
  2098. error = -EISDIR;
  2099. if (S_ISDIR(nd->inode->i_mode))
  2100. goto exit;
  2101. ok:
  2102. if (!S_ISREG(nd->inode->i_mode))
  2103. will_truncate = 0;
  2104. if (will_truncate) {
  2105. error = mnt_want_write(nd->path.mnt);
  2106. if (error)
  2107. goto exit;
  2108. want_write = 1;
  2109. }
  2110. common:
  2111. error = may_open(&nd->path, acc_mode, open_flag);
  2112. if (error)
  2113. goto exit;
  2114. filp = nameidata_to_filp(nd);
  2115. if (!IS_ERR(filp)) {
  2116. error = ima_file_check(filp, op->acc_mode);
  2117. if (error) {
  2118. fput(filp);
  2119. filp = ERR_PTR(error);
  2120. }
  2121. }
  2122. if (!IS_ERR(filp)) {
  2123. if (will_truncate) {
  2124. error = handle_truncate(filp);
  2125. if (error) {
  2126. fput(filp);
  2127. filp = ERR_PTR(error);
  2128. }
  2129. }
  2130. }
  2131. out:
  2132. if (want_write)
  2133. mnt_drop_write(nd->path.mnt);
  2134. path_put(&nd->path);
  2135. return filp;
  2136. exit_mutex_unlock:
  2137. mutex_unlock(&dir->d_inode->i_mutex);
  2138. exit_dput:
  2139. path_put_conditional(path, nd);
  2140. exit:
  2141. filp = ERR_PTR(error);
  2142. goto out;
  2143. }
  2144. static struct file *path_openat(int dfd, const char *pathname,
  2145. struct nameidata *nd, const struct open_flags *op, int flags)
  2146. {
  2147. struct file *base = NULL;
  2148. struct file *filp;
  2149. struct path path;
  2150. int error;
  2151. filp = get_empty_filp();
  2152. if (!filp)
  2153. return ERR_PTR(-ENFILE);
  2154. filp->f_flags = op->open_flag;
  2155. nd->intent.open.file = filp;
  2156. nd->intent.open.flags = open_to_namei_flags(op->open_flag);
  2157. nd->intent.open.create_mode = op->mode;
  2158. error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
  2159. if (unlikely(error))
  2160. goto out_filp;
  2161. current->total_link_count = 0;
  2162. error = link_path_walk(pathname, nd);
  2163. if (unlikely(error))
  2164. goto out_filp;
  2165. filp = do_last(nd, &path, op, pathname);
  2166. while (unlikely(!filp)) { /* trailing symlink */
  2167. struct path link = path;
  2168. void *cookie;
  2169. if (!(nd->flags & LOOKUP_FOLLOW)) {
  2170. path_put_conditional(&path, nd);
  2171. path_put(&nd->path);
  2172. filp = ERR_PTR(-ELOOP);
  2173. break;
  2174. }
  2175. nd->flags |= LOOKUP_PARENT;
  2176. nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
  2177. error = follow_link(&link, nd, &cookie);
  2178. if (unlikely(error))
  2179. filp = ERR_PTR(error);
  2180. else
  2181. filp = do_last(nd, &path, op, pathname);
  2182. put_link(nd, &link, cookie);
  2183. }
  2184. out:
  2185. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
  2186. path_put(&nd->root);
  2187. if (base)
  2188. fput(base);
  2189. release_open_intent(nd);
  2190. return filp;
  2191. out_filp:
  2192. filp = ERR_PTR(error);
  2193. goto out;
  2194. }
  2195. struct file *do_filp_open(int dfd, const char *pathname,
  2196. const struct open_flags *op, int flags)
  2197. {
  2198. struct nameidata nd;
  2199. struct file *filp;
  2200. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
  2201. if (unlikely(filp == ERR_PTR(-ECHILD)))
  2202. filp = path_openat(dfd, pathname, &nd, op, flags);
  2203. if (unlikely(filp == ERR_PTR(-ESTALE)))
  2204. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
  2205. return filp;
  2206. }
  2207. struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  2208. const char *name, const struct open_flags *op, int flags)
  2209. {
  2210. struct nameidata nd;
  2211. struct file *file;
  2212. nd.root.mnt = mnt;
  2213. nd.root.dentry = dentry;
  2214. flags |= LOOKUP_ROOT;
  2215. if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
  2216. return ERR_PTR(-ELOOP);
  2217. file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
  2218. if (unlikely(file == ERR_PTR(-ECHILD)))
  2219. file = path_openat(-1, name, &nd, op, flags);
  2220. if (unlikely(file == ERR_PTR(-ESTALE)))
  2221. file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
  2222. return file;
  2223. }
  2224. struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
  2225. {
  2226. struct dentry *dentry = ERR_PTR(-EEXIST);
  2227. struct nameidata nd;
  2228. int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
  2229. if (error)
  2230. return ERR_PTR(error);
  2231. /*
  2232. * Yucky last component or no last component at all?
  2233. * (foo/., foo/.., /////)
  2234. */
  2235. if (nd.last_type != LAST_NORM)
  2236. goto out;
  2237. nd.flags &= ~LOOKUP_PARENT;
  2238. nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  2239. nd.intent.open.flags = O_EXCL;
  2240. /*
  2241. * Do the final lookup.
  2242. */
  2243. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2244. dentry = lookup_hash(&nd);
  2245. if (IS_ERR(dentry))
  2246. goto fail;
  2247. if (dentry->d_inode)
  2248. goto eexist;
  2249. /*
  2250. * Special case - lookup gave negative, but... we had foo/bar/
  2251. * From the vfs_mknod() POV we just have a negative dentry -
  2252. * all is fine. Let's be bastards - you had / on the end, you've
  2253. * been asking for (non-existent) directory. -ENOENT for you.
  2254. */
  2255. if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
  2256. dput(dentry);
  2257. dentry = ERR_PTR(-ENOENT);
  2258. goto fail;
  2259. }
  2260. *path = nd.path;
  2261. return dentry;
  2262. eexist:
  2263. dput(dentry);
  2264. dentry = ERR_PTR(-EEXIST);
  2265. fail:
  2266. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2267. out:
  2268. path_put(&nd.path);
  2269. return dentry;
  2270. }
  2271. EXPORT_SYMBOL(kern_path_create);
  2272. struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
  2273. {
  2274. char *tmp = getname(pathname);
  2275. struct dentry *res;
  2276. if (IS_ERR(tmp))
  2277. return ERR_CAST(tmp);
  2278. res = kern_path_create(dfd, tmp, path, is_dir);
  2279. putname(tmp);
  2280. return res;
  2281. }
  2282. EXPORT_SYMBOL(user_path_create);
  2283. int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  2284. {
  2285. int error = may_create(dir, dentry);
  2286. if (error)
  2287. return error;
  2288. if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
  2289. !ns_capable(inode_userns(dir), CAP_MKNOD))
  2290. return -EPERM;
  2291. if (!dir->i_op->mknod)
  2292. return -EPERM;
  2293. error = devcgroup_inode_mknod(mode, dev);
  2294. if (error)
  2295. return error;
  2296. error = security_inode_mknod(dir, dentry, mode, dev);
  2297. if (error)
  2298. return error;
  2299. error = dir->i_op->mknod(dir, dentry, mode, dev);
  2300. if (!error)
  2301. fsnotify_create(dir, dentry);
  2302. return error;
  2303. }
  2304. static int may_mknod(umode_t mode)
  2305. {
  2306. switch (mode & S_IFMT) {
  2307. case S_IFREG:
  2308. case S_IFCHR:
  2309. case S_IFBLK:
  2310. case S_IFIFO:
  2311. case S_IFSOCK:
  2312. case 0: /* zero mode translates to S_IFREG */
  2313. return 0;
  2314. case S_IFDIR:
  2315. return -EPERM;
  2316. default:
  2317. return -EINVAL;
  2318. }
  2319. }
  2320. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
  2321. unsigned, dev)
  2322. {
  2323. struct dentry *dentry;
  2324. struct path path;
  2325. int error;
  2326. if (S_ISDIR(mode))
  2327. return -EPERM;
  2328. dentry = user_path_create(dfd, filename, &path, 0);
  2329. if (IS_ERR(dentry))
  2330. return PTR_ERR(dentry);
  2331. if (!IS_POSIXACL(path.dentry->d_inode))
  2332. mode &= ~current_umask();
  2333. error = may_mknod(mode);
  2334. if (error)
  2335. goto out_dput;
  2336. error = mnt_want_write(path.mnt);
  2337. if (error)
  2338. goto out_dput;
  2339. error = security_path_mknod(&path, dentry, mode, dev);
  2340. if (error)
  2341. goto out_drop_write;
  2342. switch (mode & S_IFMT) {
  2343. case 0: case S_IFREG:
  2344. error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
  2345. break;
  2346. case S_IFCHR: case S_IFBLK:
  2347. error = vfs_mknod(path.dentry->d_inode,dentry,mode,
  2348. new_decode_dev(dev));
  2349. break;
  2350. case S_IFIFO: case S_IFSOCK:
  2351. error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
  2352. break;
  2353. }
  2354. out_drop_write:
  2355. mnt_drop_write(path.mnt);
  2356. out_dput:
  2357. dput(dentry);
  2358. mutex_unlock(&path.dentry->d_inode->i_mutex);
  2359. path_put(&path);
  2360. return error;
  2361. }
  2362. SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
  2363. {
  2364. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  2365. }
  2366. int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  2367. {
  2368. int error = may_create(dir, dentry);
  2369. unsigned max_links = dir->i_sb->s_max_links;
  2370. if (error)
  2371. return error;
  2372. if (!dir->i_op->mkdir)
  2373. return -EPERM;
  2374. mode &= (S_IRWXUGO|S_ISVTX);
  2375. error = security_inode_mkdir(dir, dentry, mode);
  2376. if (error)
  2377. return error;
  2378. if (max_links && dir->i_nlink >= max_links)
  2379. return -EMLINK;
  2380. error = dir->i_op->mkdir(dir, dentry, mode);
  2381. if (!error)
  2382. fsnotify_mkdir(dir, dentry);
  2383. return error;
  2384. }
  2385. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
  2386. {
  2387. struct dentry *dentry;
  2388. struct path path;
  2389. int error;
  2390. dentry = user_path_create(dfd, pathname, &path, 1);
  2391. if (IS_ERR(dentry))
  2392. return PTR_ERR(dentry);
  2393. if (!IS_POSIXACL(path.dentry->d_inode))
  2394. mode &= ~current_umask();
  2395. error = mnt_want_write(path.mnt);
  2396. if (error)
  2397. goto out_dput;
  2398. error = security_path_mkdir(&path, dentry, mode);
  2399. if (error)
  2400. goto out_drop_write;
  2401. error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
  2402. out_drop_write:
  2403. mnt_drop_write(path.mnt);
  2404. out_dput:
  2405. dput(dentry);
  2406. mutex_unlock(&path.dentry->d_inode->i_mutex);
  2407. path_put(&path);
  2408. return error;
  2409. }
  2410. SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
  2411. {
  2412. return sys_mkdirat(AT_FDCWD, pathname, mode);
  2413. }
  2414. /*
  2415. * The dentry_unhash() helper will try to drop the dentry early: we
  2416. * should have a usage count of 2 if we're the only user of this
  2417. * dentry, and if that is true (possibly after pruning the dcache),
  2418. * then we drop the dentry now.
  2419. *
  2420. * A low-level filesystem can, if it choses, legally
  2421. * do a
  2422. *
  2423. * if (!d_unhashed(dentry))
  2424. * return -EBUSY;
  2425. *
  2426. * if it cannot handle the case of removing a directory
  2427. * that is still in use by something else..
  2428. */
  2429. void dentry_unhash(struct dentry *dentry)
  2430. {
  2431. shrink_dcache_parent(dentry);
  2432. spin_lock(&dentry->d_lock);
  2433. if (dentry->d_count == 1)
  2434. __d_drop(dentry);
  2435. spin_unlock(&dentry->d_lock);
  2436. }
  2437. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  2438. {
  2439. int error = may_delete(dir, dentry, 1);
  2440. if (error)
  2441. return error;
  2442. if (!dir->i_op->rmdir)
  2443. return -EPERM;
  2444. dget(dentry);
  2445. mutex_lock(&dentry->d_inode->i_mutex);
  2446. error = -EBUSY;
  2447. if (d_mountpoint(dentry))
  2448. goto out;
  2449. error = security_inode_rmdir(dir, dentry);
  2450. if (error)
  2451. goto out;
  2452. shrink_dcache_parent(dentry);
  2453. error = dir->i_op->rmdir(dir, dentry);
  2454. if (error)
  2455. goto out;
  2456. dentry->d_inode->i_flags |= S_DEAD;
  2457. dont_mount(dentry);
  2458. out:
  2459. mutex_unlock(&dentry->d_inode->i_mutex);
  2460. dput(dentry);
  2461. if (!error)
  2462. d_delete(dentry);
  2463. return error;
  2464. }
  2465. static long do_rmdir(int dfd, const char __user *pathname)
  2466. {
  2467. int error = 0;
  2468. char * name;
  2469. struct dentry *dentry;
  2470. struct nameidata nd;
  2471. error = user_path_parent(dfd, pathname, &nd, &name);
  2472. if (error)
  2473. return error;
  2474. switch(nd.last_type) {
  2475. case LAST_DOTDOT:
  2476. error = -ENOTEMPTY;
  2477. goto exit1;
  2478. case LAST_DOT:
  2479. error = -EINVAL;
  2480. goto exit1;
  2481. case LAST_ROOT:
  2482. error = -EBUSY;
  2483. goto exit1;
  2484. }
  2485. nd.flags &= ~LOOKUP_PARENT;
  2486. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2487. dentry = lookup_hash(&nd);
  2488. error = PTR_ERR(dentry);
  2489. if (IS_ERR(dentry))
  2490. goto exit2;
  2491. if (!dentry->d_inode) {
  2492. error = -ENOENT;
  2493. goto exit3;
  2494. }
  2495. error = mnt_want_write(nd.path.mnt);
  2496. if (error)
  2497. goto exit3;
  2498. error = security_path_rmdir(&nd.path, dentry);
  2499. if (error)
  2500. goto exit4;
  2501. error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
  2502. exit4:
  2503. mnt_drop_write(nd.path.mnt);
  2504. exit3:
  2505. dput(dentry);
  2506. exit2:
  2507. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2508. exit1:
  2509. path_put(&nd.path);
  2510. putname(name);
  2511. return error;
  2512. }
  2513. SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
  2514. {
  2515. return do_rmdir(AT_FDCWD, pathname);
  2516. }
  2517. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  2518. {
  2519. int error = may_delete(dir, dentry, 0);
  2520. if (error)
  2521. return error;
  2522. if (!dir->i_op->unlink)
  2523. return -EPERM;
  2524. mutex_lock(&dentry->d_inode->i_mutex);
  2525. if (d_mountpoint(dentry))
  2526. error = -EBUSY;
  2527. else {
  2528. error = security_inode_unlink(dir, dentry);
  2529. if (!error) {
  2530. error = dir->i_op->unlink(dir, dentry);
  2531. if (!error)
  2532. dont_mount(dentry);
  2533. }
  2534. }
  2535. mutex_unlock(&dentry->d_inode->i_mutex);
  2536. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  2537. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  2538. fsnotify_link_count(dentry->d_inode);
  2539. d_delete(dentry);
  2540. }
  2541. return error;
  2542. }
  2543. /*
  2544. * Make sure that the actual truncation of the file will occur outside its
  2545. * directory's i_mutex. Truncate can take a long time if there is a lot of
  2546. * writeout happening, and we don't want to prevent access to the directory
  2547. * while waiting on the I/O.
  2548. */
  2549. static long do_unlinkat(int dfd, const char __user *pathname)
  2550. {
  2551. int error;
  2552. char *name;
  2553. struct dentry *dentry;
  2554. struct nameidata nd;
  2555. struct inode *inode = NULL;
  2556. error = user_path_parent(dfd, pathname, &nd, &name);
  2557. if (error)
  2558. return error;
  2559. error = -EISDIR;
  2560. if (nd.last_type != LAST_NORM)
  2561. goto exit1;
  2562. nd.flags &= ~LOOKUP_PARENT;
  2563. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2564. dentry = lookup_hash(&nd);
  2565. error = PTR_ERR(dentry);
  2566. if (!IS_ERR(dentry)) {
  2567. /* Why not before? Because we want correct error value */
  2568. if (nd.last.name[nd.last.len])
  2569. goto slashes;
  2570. inode = dentry->d_inode;
  2571. if (!inode)
  2572. goto slashes;
  2573. ihold(inode);
  2574. error = mnt_want_write(nd.path.mnt);
  2575. if (error)
  2576. goto exit2;
  2577. error = security_path_unlink(&nd.path, dentry);
  2578. if (error)
  2579. goto exit3;
  2580. error = vfs_unlink(nd.path.dentry->d_inode, dentry);
  2581. exit3:
  2582. mnt_drop_write(nd.path.mnt);
  2583. exit2:
  2584. dput(dentry);
  2585. }
  2586. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2587. if (inode)
  2588. iput(inode); /* truncate the inode here */
  2589. exit1:
  2590. path_put(&nd.path);
  2591. putname(name);
  2592. return error;
  2593. slashes:
  2594. error = !dentry->d_inode ? -ENOENT :
  2595. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  2596. goto exit2;
  2597. }
  2598. SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
  2599. {
  2600. if ((flag & ~AT_REMOVEDIR) != 0)
  2601. return -EINVAL;
  2602. if (flag & AT_REMOVEDIR)
  2603. return do_rmdir(dfd, pathname);
  2604. return do_unlinkat(dfd, pathname);
  2605. }
  2606. SYSCALL_DEFINE1(unlink, const char __user *, pathname)
  2607. {
  2608. return do_unlinkat(AT_FDCWD, pathname);
  2609. }
  2610. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  2611. {
  2612. int error = may_create(dir, dentry);
  2613. if (error)
  2614. return error;
  2615. if (!dir->i_op->symlink)
  2616. return -EPERM;
  2617. error = security_inode_symlink(dir, dentry, oldname);
  2618. if (error)
  2619. return error;
  2620. error = dir->i_op->symlink(dir, dentry, oldname);
  2621. if (!error)
  2622. fsnotify_create(dir, dentry);
  2623. return error;
  2624. }
  2625. SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  2626. int, newdfd, const char __user *, newname)
  2627. {
  2628. int error;
  2629. char *from;
  2630. struct dentry *dentry;
  2631. struct path path;
  2632. from = getname(oldname);
  2633. if (IS_ERR(from))
  2634. return PTR_ERR(from);
  2635. dentry = user_path_create(newdfd, newname, &path, 0);
  2636. error = PTR_ERR(dentry);
  2637. if (IS_ERR(dentry))
  2638. goto out_putname;
  2639. error = mnt_want_write(path.mnt);
  2640. if (error)
  2641. goto out_dput;
  2642. error = security_path_symlink(&path, dentry, from);
  2643. if (error)
  2644. goto out_drop_write;
  2645. error = vfs_symlink(path.dentry->d_inode, dentry, from);
  2646. out_drop_write:
  2647. mnt_drop_write(path.mnt);
  2648. out_dput:
  2649. dput(dentry);
  2650. mutex_unlock(&path.dentry->d_inode->i_mutex);
  2651. path_put(&path);
  2652. out_putname:
  2653. putname(from);
  2654. return error;
  2655. }
  2656. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  2657. {
  2658. return sys_symlinkat(oldname, AT_FDCWD, newname);
  2659. }
  2660. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2661. {
  2662. struct inode *inode = old_dentry->d_inode;
  2663. unsigned max_links = dir->i_sb->s_max_links;
  2664. int error;
  2665. if (!inode)
  2666. return -ENOENT;
  2667. error = may_create(dir, new_dentry);
  2668. if (error)
  2669. return error;
  2670. if (dir->i_sb != inode->i_sb)
  2671. return -EXDEV;
  2672. /*
  2673. * A link to an append-only or immutable file cannot be created.
  2674. */
  2675. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2676. return -EPERM;
  2677. if (!dir->i_op->link)
  2678. return -EPERM;
  2679. if (S_ISDIR(inode->i_mode))
  2680. return -EPERM;
  2681. error = security_inode_link(old_dentry, dir, new_dentry);
  2682. if (error)
  2683. return error;
  2684. mutex_lock(&inode->i_mutex);
  2685. /* Make sure we don't allow creating hardlink to an unlinked file */
  2686. if (inode->i_nlink == 0)
  2687. error = -ENOENT;
  2688. else if (max_links && inode->i_nlink >= max_links)
  2689. error = -EMLINK;
  2690. else
  2691. error = dir->i_op->link(old_dentry, dir, new_dentry);
  2692. mutex_unlock(&inode->i_mutex);
  2693. if (!error)
  2694. fsnotify_link(dir, inode, new_dentry);
  2695. return error;
  2696. }
  2697. /*
  2698. * Hardlinks are often used in delicate situations. We avoid
  2699. * security-related surprises by not following symlinks on the
  2700. * newname. --KAB
  2701. *
  2702. * We don't follow them on the oldname either to be compatible
  2703. * with linux 2.0, and to avoid hard-linking to directories
  2704. * and other special files. --ADM
  2705. */
  2706. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  2707. int, newdfd, const char __user *, newname, int, flags)
  2708. {
  2709. struct dentry *new_dentry;
  2710. struct path old_path, new_path;
  2711. int how = 0;
  2712. int error;
  2713. if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  2714. return -EINVAL;
  2715. /*
  2716. * To use null names we require CAP_DAC_READ_SEARCH
  2717. * This ensures that not everyone will be able to create
  2718. * handlink using the passed filedescriptor.
  2719. */
  2720. if (flags & AT_EMPTY_PATH) {
  2721. if (!capable(CAP_DAC_READ_SEARCH))
  2722. return -ENOENT;
  2723. how = LOOKUP_EMPTY;
  2724. }
  2725. if (flags & AT_SYMLINK_FOLLOW)
  2726. how |= LOOKUP_FOLLOW;
  2727. error = user_path_at(olddfd, oldname, how, &old_path);
  2728. if (error)
  2729. return error;
  2730. new_dentry = user_path_create(newdfd, newname, &new_path, 0);
  2731. error = PTR_ERR(new_dentry);
  2732. if (IS_ERR(new_dentry))
  2733. goto out;
  2734. error = -EXDEV;
  2735. if (old_path.mnt != new_path.mnt)
  2736. goto out_dput;
  2737. error = mnt_want_write(new_path.mnt);
  2738. if (error)
  2739. goto out_dput;
  2740. error = security_path_link(old_path.dentry, &new_path, new_dentry);
  2741. if (error)
  2742. goto out_drop_write;
  2743. error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
  2744. out_drop_write:
  2745. mnt_drop_write(new_path.mnt);
  2746. out_dput:
  2747. dput(new_dentry);
  2748. mutex_unlock(&new_path.dentry->d_inode->i_mutex);
  2749. path_put(&new_path);
  2750. out:
  2751. path_put(&old_path);
  2752. return error;
  2753. }
  2754. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  2755. {
  2756. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  2757. }
  2758. /*
  2759. * The worst of all namespace operations - renaming directory. "Perverted"
  2760. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  2761. * Problems:
  2762. * a) we can get into loop creation. Check is done in is_subdir().
  2763. * b) race potential - two innocent renames can create a loop together.
  2764. * That's where 4.4 screws up. Current fix: serialization on
  2765. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  2766. * story.
  2767. * c) we have to lock _three_ objects - parents and victim (if it exists).
  2768. * And that - after we got ->i_mutex on parents (until then we don't know
  2769. * whether the target exists). Solution: try to be smart with locking
  2770. * order for inodes. We rely on the fact that tree topology may change
  2771. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  2772. * move will be locked. Thus we can rank directories by the tree
  2773. * (ancestors first) and rank all non-directories after them.
  2774. * That works since everybody except rename does "lock parent, lookup,
  2775. * lock child" and rename is under ->s_vfs_rename_mutex.
  2776. * HOWEVER, it relies on the assumption that any object with ->lookup()
  2777. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  2778. * we'd better make sure that there's no link(2) for them.
  2779. * d) conversion from fhandle to dentry may come in the wrong moment - when
  2780. * we are removing the target. Solution: we will have to grab ->i_mutex
  2781. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  2782. * ->i_mutex on parents, which works but leads to some truly excessive
  2783. * locking].
  2784. */
  2785. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  2786. struct inode *new_dir, struct dentry *new_dentry)
  2787. {
  2788. int error = 0;
  2789. struct inode *target = new_dentry->d_inode;
  2790. unsigned max_links = new_dir->i_sb->s_max_links;
  2791. /*
  2792. * If we are going to change the parent - check write permissions,
  2793. * we'll need to flip '..'.
  2794. */
  2795. if (new_dir != old_dir) {
  2796. error = inode_permission(old_dentry->d_inode, MAY_WRITE);
  2797. if (error)
  2798. return error;
  2799. }
  2800. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2801. if (error)
  2802. return error;
  2803. dget(new_dentry);
  2804. if (target)
  2805. mutex_lock(&target->i_mutex);
  2806. error = -EBUSY;
  2807. if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
  2808. goto out;
  2809. error = -EMLINK;
  2810. if (max_links && !target && new_dir != old_dir &&
  2811. new_dir->i_nlink >= max_links)
  2812. goto out;
  2813. if (target)
  2814. shrink_dcache_parent(new_dentry);
  2815. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2816. if (error)
  2817. goto out;
  2818. if (target) {
  2819. target->i_flags |= S_DEAD;
  2820. dont_mount(new_dentry);
  2821. }
  2822. out:
  2823. if (target)
  2824. mutex_unlock(&target->i_mutex);
  2825. dput(new_dentry);
  2826. if (!error)
  2827. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2828. d_move(old_dentry,new_dentry);
  2829. return error;
  2830. }
  2831. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2832. struct inode *new_dir, struct dentry *new_dentry)
  2833. {
  2834. struct inode *target = new_dentry->d_inode;
  2835. int error;
  2836. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2837. if (error)
  2838. return error;
  2839. dget(new_dentry);
  2840. if (target)
  2841. mutex_lock(&target->i_mutex);
  2842. error = -EBUSY;
  2843. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2844. goto out;
  2845. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2846. if (error)
  2847. goto out;
  2848. if (target)
  2849. dont_mount(new_dentry);
  2850. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2851. d_move(old_dentry, new_dentry);
  2852. out:
  2853. if (target)
  2854. mutex_unlock(&target->i_mutex);
  2855. dput(new_dentry);
  2856. return error;
  2857. }
  2858. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2859. struct inode *new_dir, struct dentry *new_dentry)
  2860. {
  2861. int error;
  2862. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2863. const unsigned char *old_name;
  2864. if (old_dentry->d_inode == new_dentry->d_inode)
  2865. return 0;
  2866. error = may_delete(old_dir, old_dentry, is_dir);
  2867. if (error)
  2868. return error;
  2869. if (!new_dentry->d_inode)
  2870. error = may_create(new_dir, new_dentry);
  2871. else
  2872. error = may_delete(new_dir, new_dentry, is_dir);
  2873. if (error)
  2874. return error;
  2875. if (!old_dir->i_op->rename)
  2876. return -EPERM;
  2877. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2878. if (is_dir)
  2879. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2880. else
  2881. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2882. if (!error)
  2883. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  2884. new_dentry->d_inode, old_dentry);
  2885. fsnotify_oldname_free(old_name);
  2886. return error;
  2887. }
  2888. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  2889. int, newdfd, const char __user *, newname)
  2890. {
  2891. struct dentry *old_dir, *new_dir;
  2892. struct dentry *old_dentry, *new_dentry;
  2893. struct dentry *trap;
  2894. struct nameidata oldnd, newnd;
  2895. char *from;
  2896. char *to;
  2897. int error;
  2898. error = user_path_parent(olddfd, oldname, &oldnd, &from);
  2899. if (error)
  2900. goto exit;
  2901. error = user_path_parent(newdfd, newname, &newnd, &to);
  2902. if (error)
  2903. goto exit1;
  2904. error = -EXDEV;
  2905. if (oldnd.path.mnt != newnd.path.mnt)
  2906. goto exit2;
  2907. old_dir = oldnd.path.dentry;
  2908. error = -EBUSY;
  2909. if (oldnd.last_type != LAST_NORM)
  2910. goto exit2;
  2911. new_dir = newnd.path.dentry;
  2912. if (newnd.last_type != LAST_NORM)
  2913. goto exit2;
  2914. oldnd.flags &= ~LOOKUP_PARENT;
  2915. newnd.flags &= ~LOOKUP_PARENT;
  2916. newnd.flags |= LOOKUP_RENAME_TARGET;
  2917. trap = lock_rename(new_dir, old_dir);
  2918. old_dentry = lookup_hash(&oldnd);
  2919. error = PTR_ERR(old_dentry);
  2920. if (IS_ERR(old_dentry))
  2921. goto exit3;
  2922. /* source must exist */
  2923. error = -ENOENT;
  2924. if (!old_dentry->d_inode)
  2925. goto exit4;
  2926. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2927. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2928. error = -ENOTDIR;
  2929. if (oldnd.last.name[oldnd.last.len])
  2930. goto exit4;
  2931. if (newnd.last.name[newnd.last.len])
  2932. goto exit4;
  2933. }
  2934. /* source should not be ancestor of target */
  2935. error = -EINVAL;
  2936. if (old_dentry == trap)
  2937. goto exit4;
  2938. new_dentry = lookup_hash(&newnd);
  2939. error = PTR_ERR(new_dentry);
  2940. if (IS_ERR(new_dentry))
  2941. goto exit4;
  2942. /* target should not be an ancestor of source */
  2943. error = -ENOTEMPTY;
  2944. if (new_dentry == trap)
  2945. goto exit5;
  2946. error = mnt_want_write(oldnd.path.mnt);
  2947. if (error)
  2948. goto exit5;
  2949. error = security_path_rename(&oldnd.path, old_dentry,
  2950. &newnd.path, new_dentry);
  2951. if (error)
  2952. goto exit6;
  2953. error = vfs_rename(old_dir->d_inode, old_dentry,
  2954. new_dir->d_inode, new_dentry);
  2955. exit6:
  2956. mnt_drop_write(oldnd.path.mnt);
  2957. exit5:
  2958. dput(new_dentry);
  2959. exit4:
  2960. dput(old_dentry);
  2961. exit3:
  2962. unlock_rename(new_dir, old_dir);
  2963. exit2:
  2964. path_put(&newnd.path);
  2965. putname(to);
  2966. exit1:
  2967. path_put(&oldnd.path);
  2968. putname(from);
  2969. exit:
  2970. return error;
  2971. }
  2972. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  2973. {
  2974. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  2975. }
  2976. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2977. {
  2978. int len;
  2979. len = PTR_ERR(link);
  2980. if (IS_ERR(link))
  2981. goto out;
  2982. len = strlen(link);
  2983. if (len > (unsigned) buflen)
  2984. len = buflen;
  2985. if (copy_to_user(buffer, link, len))
  2986. len = -EFAULT;
  2987. out:
  2988. return len;
  2989. }
  2990. /*
  2991. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2992. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2993. * using) it for any given inode is up to filesystem.
  2994. */
  2995. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2996. {
  2997. struct nameidata nd;
  2998. void *cookie;
  2999. int res;
  3000. nd.depth = 0;
  3001. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  3002. if (IS_ERR(cookie))
  3003. return PTR_ERR(cookie);
  3004. res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  3005. if (dentry->d_inode->i_op->put_link)
  3006. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  3007. return res;
  3008. }
  3009. int vfs_follow_link(struct nameidata *nd, const char *link)
  3010. {
  3011. return __vfs_follow_link(nd, link);
  3012. }
  3013. /* get the link contents into pagecache */
  3014. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  3015. {
  3016. char *kaddr;
  3017. struct page *page;
  3018. struct address_space *mapping = dentry->d_inode->i_mapping;
  3019. page = read_mapping_page(mapping, 0, NULL);
  3020. if (IS_ERR(page))
  3021. return (char*)page;
  3022. *ppage = page;
  3023. kaddr = kmap(page);
  3024. nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
  3025. return kaddr;
  3026. }
  3027. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  3028. {
  3029. struct page *page = NULL;
  3030. char *s = page_getlink(dentry, &page);
  3031. int res = vfs_readlink(dentry,buffer,buflen,s);
  3032. if (page) {
  3033. kunmap(page);
  3034. page_cache_release(page);
  3035. }
  3036. return res;
  3037. }
  3038. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  3039. {
  3040. struct page *page = NULL;
  3041. nd_set_link(nd, page_getlink(dentry, &page));
  3042. return page;
  3043. }
  3044. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  3045. {
  3046. struct page *page = cookie;
  3047. if (page) {
  3048. kunmap(page);
  3049. page_cache_release(page);
  3050. }
  3051. }
  3052. /*
  3053. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  3054. */
  3055. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  3056. {
  3057. struct address_space *mapping = inode->i_mapping;
  3058. struct page *page;
  3059. void *fsdata;
  3060. int err;
  3061. char *kaddr;
  3062. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  3063. if (nofs)
  3064. flags |= AOP_FLAG_NOFS;
  3065. retry:
  3066. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  3067. flags, &page, &fsdata);
  3068. if (err)
  3069. goto fail;
  3070. kaddr = kmap_atomic(page);
  3071. memcpy(kaddr, symname, len-1);
  3072. kunmap_atomic(kaddr);
  3073. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  3074. page, fsdata);
  3075. if (err < 0)
  3076. goto fail;
  3077. if (err < len-1)
  3078. goto retry;
  3079. mark_inode_dirty(inode);
  3080. return 0;
  3081. fail:
  3082. return err;
  3083. }
  3084. int page_symlink(struct inode *inode, const char *symname, int len)
  3085. {
  3086. return __page_symlink(inode, symname, len,
  3087. !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
  3088. }
  3089. const struct inode_operations page_symlink_inode_operations = {
  3090. .readlink = generic_readlink,
  3091. .follow_link = page_follow_link_light,
  3092. .put_link = page_put_link,
  3093. };
  3094. EXPORT_SYMBOL(user_path_at);
  3095. EXPORT_SYMBOL(follow_down_one);
  3096. EXPORT_SYMBOL(follow_down);
  3097. EXPORT_SYMBOL(follow_up);
  3098. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  3099. EXPORT_SYMBOL(getname);
  3100. EXPORT_SYMBOL(lock_rename);
  3101. EXPORT_SYMBOL(lookup_one_len);
  3102. EXPORT_SYMBOL(page_follow_link_light);
  3103. EXPORT_SYMBOL(page_put_link);
  3104. EXPORT_SYMBOL(page_readlink);
  3105. EXPORT_SYMBOL(__page_symlink);
  3106. EXPORT_SYMBOL(page_symlink);
  3107. EXPORT_SYMBOL(page_symlink_inode_operations);
  3108. EXPORT_SYMBOL(kern_path);
  3109. EXPORT_SYMBOL(vfs_path_lookup);
  3110. EXPORT_SYMBOL(inode_permission);
  3111. EXPORT_SYMBOL(unlock_rename);
  3112. EXPORT_SYMBOL(vfs_create);
  3113. EXPORT_SYMBOL(vfs_follow_link);
  3114. EXPORT_SYMBOL(vfs_link);
  3115. EXPORT_SYMBOL(vfs_mkdir);
  3116. EXPORT_SYMBOL(vfs_mknod);
  3117. EXPORT_SYMBOL(generic_permission);
  3118. EXPORT_SYMBOL(vfs_readlink);
  3119. EXPORT_SYMBOL(vfs_rename);
  3120. EXPORT_SYMBOL(vfs_rmdir);
  3121. EXPORT_SYMBOL(vfs_symlink);
  3122. EXPORT_SYMBOL(vfs_unlink);
  3123. EXPORT_SYMBOL(dentry_unhash);
  3124. EXPORT_SYMBOL(generic_readlink);