namei.c 79 KB

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