namei.c 82 KB

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