namei.c 84 KB

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