namei.c 84 KB

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