namei.c 82 KB

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