namei.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289
  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. * @path: pointer to struct path to fill
  1404. */
  1405. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  1406. const char *name, unsigned int flags,
  1407. struct path *path)
  1408. {
  1409. struct nameidata nd;
  1410. int err;
  1411. nd.root.dentry = dentry;
  1412. nd.root.mnt = mnt;
  1413. BUG_ON(flags & LOOKUP_PARENT);
  1414. /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
  1415. err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
  1416. if (!err)
  1417. *path = nd.path;
  1418. return err;
  1419. }
  1420. static struct dentry *__lookup_hash(struct qstr *name,
  1421. struct dentry *base, struct nameidata *nd)
  1422. {
  1423. struct inode *inode = base->d_inode;
  1424. struct dentry *dentry;
  1425. int err;
  1426. err = inode_permission(inode, MAY_EXEC);
  1427. if (err)
  1428. return ERR_PTR(err);
  1429. /*
  1430. * Don't bother with __d_lookup: callers are for creat as
  1431. * well as unlink, so a lot of the time it would cost
  1432. * a double lookup.
  1433. */
  1434. dentry = d_lookup(base, name);
  1435. if (dentry && d_need_lookup(dentry)) {
  1436. /*
  1437. * __lookup_hash is called with the parent dir's i_mutex already
  1438. * held, so we are good to go here.
  1439. */
  1440. dentry = d_inode_lookup(base, dentry, nd);
  1441. if (IS_ERR(dentry))
  1442. return dentry;
  1443. }
  1444. if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1445. int status = d_revalidate(dentry, nd);
  1446. if (unlikely(status <= 0)) {
  1447. /*
  1448. * The dentry failed validation.
  1449. * If d_revalidate returned 0 attempt to invalidate
  1450. * the dentry otherwise d_revalidate is asking us
  1451. * to return a fail status.
  1452. */
  1453. if (status < 0) {
  1454. dput(dentry);
  1455. return ERR_PTR(status);
  1456. } else if (!d_invalidate(dentry)) {
  1457. dput(dentry);
  1458. dentry = NULL;
  1459. }
  1460. }
  1461. }
  1462. if (!dentry)
  1463. dentry = d_alloc_and_lookup(base, name, nd);
  1464. return dentry;
  1465. }
  1466. /*
  1467. * Restricted form of lookup. Doesn't follow links, single-component only,
  1468. * needs parent already locked. Doesn't follow mounts.
  1469. * SMP-safe.
  1470. */
  1471. static struct dentry *lookup_hash(struct nameidata *nd)
  1472. {
  1473. return __lookup_hash(&nd->last, nd->path.dentry, nd);
  1474. }
  1475. /**
  1476. * lookup_one_len - filesystem helper to lookup single pathname component
  1477. * @name: pathname component to lookup
  1478. * @base: base directory to lookup from
  1479. * @len: maximum length @len should be interpreted to
  1480. *
  1481. * Note that this routine is purely a helper for filesystem usage and should
  1482. * not be called by generic code. Also note that by using this function the
  1483. * nameidata argument is passed to the filesystem methods and a filesystem
  1484. * using this helper needs to be prepared for that.
  1485. */
  1486. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1487. {
  1488. struct qstr this;
  1489. unsigned long hash;
  1490. unsigned int c;
  1491. WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
  1492. this.name = name;
  1493. this.len = len;
  1494. if (!len)
  1495. return ERR_PTR(-EACCES);
  1496. hash = init_name_hash();
  1497. while (len--) {
  1498. c = *(const unsigned char *)name++;
  1499. if (c == '/' || c == '\0')
  1500. return ERR_PTR(-EACCES);
  1501. hash = partial_name_hash(c, hash);
  1502. }
  1503. this.hash = end_name_hash(hash);
  1504. /*
  1505. * See if the low-level filesystem might want
  1506. * to use its own hash..
  1507. */
  1508. if (base->d_flags & DCACHE_OP_HASH) {
  1509. int err = base->d_op->d_hash(base, base->d_inode, &this);
  1510. if (err < 0)
  1511. return ERR_PTR(err);
  1512. }
  1513. return __lookup_hash(&this, base, NULL);
  1514. }
  1515. int user_path_at(int dfd, const char __user *name, unsigned flags,
  1516. struct path *path)
  1517. {
  1518. struct nameidata nd;
  1519. char *tmp = getname_flags(name, flags);
  1520. int err = PTR_ERR(tmp);
  1521. if (!IS_ERR(tmp)) {
  1522. BUG_ON(flags & LOOKUP_PARENT);
  1523. err = do_path_lookup(dfd, tmp, flags, &nd);
  1524. putname(tmp);
  1525. if (!err)
  1526. *path = nd.path;
  1527. }
  1528. return err;
  1529. }
  1530. static int user_path_parent(int dfd, const char __user *path,
  1531. struct nameidata *nd, char **name)
  1532. {
  1533. char *s = getname(path);
  1534. int error;
  1535. if (IS_ERR(s))
  1536. return PTR_ERR(s);
  1537. error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
  1538. if (error)
  1539. putname(s);
  1540. else
  1541. *name = s;
  1542. return error;
  1543. }
  1544. /*
  1545. * It's inline, so penalty for filesystems that don't use sticky bit is
  1546. * minimal.
  1547. */
  1548. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1549. {
  1550. uid_t fsuid = current_fsuid();
  1551. if (!(dir->i_mode & S_ISVTX))
  1552. return 0;
  1553. if (current_user_ns() != inode_userns(inode))
  1554. goto other_userns;
  1555. if (inode->i_uid == fsuid)
  1556. return 0;
  1557. if (dir->i_uid == fsuid)
  1558. return 0;
  1559. other_userns:
  1560. return !ns_capable(inode_userns(inode), CAP_FOWNER);
  1561. }
  1562. /*
  1563. * Check whether we can remove a link victim from directory dir, check
  1564. * whether the type of victim is right.
  1565. * 1. We can't do it if dir is read-only (done in permission())
  1566. * 2. We should have write and exec permissions on dir
  1567. * 3. We can't remove anything from append-only dir
  1568. * 4. We can't do anything with immutable dir (done in permission())
  1569. * 5. If the sticky bit on dir is set we should either
  1570. * a. be owner of dir, or
  1571. * b. be owner of victim, or
  1572. * c. have CAP_FOWNER capability
  1573. * 6. If the victim is append-only or immutable we can't do antyhing with
  1574. * links pointing to it.
  1575. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1576. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1577. * 9. We can't remove a root or mountpoint.
  1578. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1579. * nfs_async_unlink().
  1580. */
  1581. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1582. {
  1583. int error;
  1584. if (!victim->d_inode)
  1585. return -ENOENT;
  1586. BUG_ON(victim->d_parent->d_inode != dir);
  1587. audit_inode_child(victim, dir);
  1588. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1589. if (error)
  1590. return error;
  1591. if (IS_APPEND(dir))
  1592. return -EPERM;
  1593. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1594. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  1595. return -EPERM;
  1596. if (isdir) {
  1597. if (!S_ISDIR(victim->d_inode->i_mode))
  1598. return -ENOTDIR;
  1599. if (IS_ROOT(victim))
  1600. return -EBUSY;
  1601. } else if (S_ISDIR(victim->d_inode->i_mode))
  1602. return -EISDIR;
  1603. if (IS_DEADDIR(dir))
  1604. return -ENOENT;
  1605. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1606. return -EBUSY;
  1607. return 0;
  1608. }
  1609. /* Check whether we can create an object with dentry child in directory
  1610. * dir.
  1611. * 1. We can't do it if child already exists (open has special treatment for
  1612. * this case, but since we are inlined it's OK)
  1613. * 2. We can't do it if dir is read-only (done in permission())
  1614. * 3. We should have write and exec permissions on dir
  1615. * 4. We can't do it if dir is immutable (done in permission())
  1616. */
  1617. static inline int may_create(struct inode *dir, struct dentry *child)
  1618. {
  1619. if (child->d_inode)
  1620. return -EEXIST;
  1621. if (IS_DEADDIR(dir))
  1622. return -ENOENT;
  1623. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1624. }
  1625. /*
  1626. * p1 and p2 should be directories on the same fs.
  1627. */
  1628. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1629. {
  1630. struct dentry *p;
  1631. if (p1 == p2) {
  1632. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1633. return NULL;
  1634. }
  1635. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1636. p = d_ancestor(p2, p1);
  1637. if (p) {
  1638. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1639. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1640. return p;
  1641. }
  1642. p = d_ancestor(p1, p2);
  1643. if (p) {
  1644. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1645. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1646. return p;
  1647. }
  1648. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1649. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1650. return NULL;
  1651. }
  1652. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1653. {
  1654. mutex_unlock(&p1->d_inode->i_mutex);
  1655. if (p1 != p2) {
  1656. mutex_unlock(&p2->d_inode->i_mutex);
  1657. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1658. }
  1659. }
  1660. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1661. struct nameidata *nd)
  1662. {
  1663. int error = may_create(dir, dentry);
  1664. if (error)
  1665. return error;
  1666. if (!dir->i_op->create)
  1667. return -EACCES; /* shouldn't it be ENOSYS? */
  1668. mode &= S_IALLUGO;
  1669. mode |= S_IFREG;
  1670. error = security_inode_create(dir, dentry, mode);
  1671. if (error)
  1672. return error;
  1673. error = dir->i_op->create(dir, dentry, mode, nd);
  1674. if (!error)
  1675. fsnotify_create(dir, dentry);
  1676. return error;
  1677. }
  1678. static int may_open(struct path *path, int acc_mode, int flag)
  1679. {
  1680. struct dentry *dentry = path->dentry;
  1681. struct inode *inode = dentry->d_inode;
  1682. int error;
  1683. /* O_PATH? */
  1684. if (!acc_mode)
  1685. return 0;
  1686. if (!inode)
  1687. return -ENOENT;
  1688. switch (inode->i_mode & S_IFMT) {
  1689. case S_IFLNK:
  1690. return -ELOOP;
  1691. case S_IFDIR:
  1692. if (acc_mode & MAY_WRITE)
  1693. return -EISDIR;
  1694. break;
  1695. case S_IFBLK:
  1696. case S_IFCHR:
  1697. if (path->mnt->mnt_flags & MNT_NODEV)
  1698. return -EACCES;
  1699. /*FALLTHRU*/
  1700. case S_IFIFO:
  1701. case S_IFSOCK:
  1702. flag &= ~O_TRUNC;
  1703. break;
  1704. }
  1705. error = inode_permission(inode, acc_mode);
  1706. if (error)
  1707. return error;
  1708. /*
  1709. * An append-only file must be opened in append mode for writing.
  1710. */
  1711. if (IS_APPEND(inode)) {
  1712. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  1713. return -EPERM;
  1714. if (flag & O_TRUNC)
  1715. return -EPERM;
  1716. }
  1717. /* O_NOATIME can only be set by the owner or superuser */
  1718. if (flag & O_NOATIME && !inode_owner_or_capable(inode))
  1719. return -EPERM;
  1720. /*
  1721. * Ensure there are no outstanding leases on the file.
  1722. */
  1723. return break_lease(inode, flag);
  1724. }
  1725. static int handle_truncate(struct file *filp)
  1726. {
  1727. struct path *path = &filp->f_path;
  1728. struct inode *inode = path->dentry->d_inode;
  1729. int error = get_write_access(inode);
  1730. if (error)
  1731. return error;
  1732. /*
  1733. * Refuse to truncate files with mandatory locks held on them.
  1734. */
  1735. error = locks_verify_locked(inode);
  1736. if (!error)
  1737. error = security_path_truncate(path);
  1738. if (!error) {
  1739. error = do_truncate(path->dentry, 0,
  1740. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  1741. filp);
  1742. }
  1743. put_write_access(inode);
  1744. return error;
  1745. }
  1746. static inline int open_to_namei_flags(int flag)
  1747. {
  1748. if ((flag & O_ACCMODE) == 3)
  1749. flag--;
  1750. return flag;
  1751. }
  1752. /*
  1753. * Handle the last step of open()
  1754. */
  1755. static struct file *do_last(struct nameidata *nd, struct path *path,
  1756. const struct open_flags *op, const char *pathname)
  1757. {
  1758. struct dentry *dir = nd->path.dentry;
  1759. struct dentry *dentry;
  1760. int open_flag = op->open_flag;
  1761. int will_truncate = open_flag & O_TRUNC;
  1762. int want_write = 0;
  1763. int acc_mode = op->acc_mode;
  1764. struct file *filp;
  1765. int error;
  1766. nd->flags &= ~LOOKUP_PARENT;
  1767. nd->flags |= op->intent;
  1768. switch (nd->last_type) {
  1769. case LAST_DOTDOT:
  1770. case LAST_DOT:
  1771. error = handle_dots(nd, nd->last_type);
  1772. if (error)
  1773. return ERR_PTR(error);
  1774. /* fallthrough */
  1775. case LAST_ROOT:
  1776. error = complete_walk(nd);
  1777. if (error)
  1778. return ERR_PTR(error);
  1779. audit_inode(pathname, nd->path.dentry);
  1780. if (open_flag & O_CREAT) {
  1781. error = -EISDIR;
  1782. goto exit;
  1783. }
  1784. goto ok;
  1785. case LAST_BIND:
  1786. error = complete_walk(nd);
  1787. if (error)
  1788. return ERR_PTR(error);
  1789. audit_inode(pathname, dir);
  1790. goto ok;
  1791. }
  1792. if (!(open_flag & O_CREAT)) {
  1793. int symlink_ok = 0;
  1794. if (nd->last.name[nd->last.len])
  1795. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1796. if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
  1797. symlink_ok = 1;
  1798. /* we _can_ be in RCU mode here */
  1799. error = walk_component(nd, path, &nd->last, LAST_NORM,
  1800. !symlink_ok);
  1801. if (error < 0)
  1802. return ERR_PTR(error);
  1803. if (error) /* symlink */
  1804. return NULL;
  1805. /* sayonara */
  1806. error = complete_walk(nd);
  1807. if (error)
  1808. return ERR_PTR(-ECHILD);
  1809. error = -ENOTDIR;
  1810. if (nd->flags & LOOKUP_DIRECTORY) {
  1811. if (!nd->inode->i_op->lookup)
  1812. goto exit;
  1813. }
  1814. audit_inode(pathname, nd->path.dentry);
  1815. goto ok;
  1816. }
  1817. /* create side of things */
  1818. error = complete_walk(nd);
  1819. if (error)
  1820. return ERR_PTR(error);
  1821. audit_inode(pathname, dir);
  1822. error = -EISDIR;
  1823. /* trailing slashes? */
  1824. if (nd->last.name[nd->last.len])
  1825. goto exit;
  1826. mutex_lock(&dir->d_inode->i_mutex);
  1827. dentry = lookup_hash(nd);
  1828. error = PTR_ERR(dentry);
  1829. if (IS_ERR(dentry)) {
  1830. mutex_unlock(&dir->d_inode->i_mutex);
  1831. goto exit;
  1832. }
  1833. path->dentry = dentry;
  1834. path->mnt = nd->path.mnt;
  1835. /* Negative dentry, just create the file */
  1836. if (!dentry->d_inode) {
  1837. int mode = op->mode;
  1838. if (!IS_POSIXACL(dir->d_inode))
  1839. mode &= ~current_umask();
  1840. /*
  1841. * This write is needed to ensure that a
  1842. * rw->ro transition does not occur between
  1843. * the time when the file is created and when
  1844. * a permanent write count is taken through
  1845. * the 'struct file' in nameidata_to_filp().
  1846. */
  1847. error = mnt_want_write(nd->path.mnt);
  1848. if (error)
  1849. goto exit_mutex_unlock;
  1850. want_write = 1;
  1851. /* Don't check for write permission, don't truncate */
  1852. open_flag &= ~O_TRUNC;
  1853. will_truncate = 0;
  1854. acc_mode = MAY_OPEN;
  1855. error = security_path_mknod(&nd->path, dentry, mode, 0);
  1856. if (error)
  1857. goto exit_mutex_unlock;
  1858. error = vfs_create(dir->d_inode, dentry, mode, nd);
  1859. if (error)
  1860. goto exit_mutex_unlock;
  1861. mutex_unlock(&dir->d_inode->i_mutex);
  1862. dput(nd->path.dentry);
  1863. nd->path.dentry = dentry;
  1864. goto common;
  1865. }
  1866. /*
  1867. * It already exists.
  1868. */
  1869. mutex_unlock(&dir->d_inode->i_mutex);
  1870. audit_inode(pathname, path->dentry);
  1871. error = -EEXIST;
  1872. if (open_flag & O_EXCL)
  1873. goto exit_dput;
  1874. error = follow_managed(path, nd->flags);
  1875. if (error < 0)
  1876. goto exit_dput;
  1877. error = -ENOENT;
  1878. if (!path->dentry->d_inode)
  1879. goto exit_dput;
  1880. if (path->dentry->d_inode->i_op->follow_link)
  1881. return NULL;
  1882. path_to_nameidata(path, nd);
  1883. nd->inode = path->dentry->d_inode;
  1884. error = -EISDIR;
  1885. if (S_ISDIR(nd->inode->i_mode))
  1886. goto exit;
  1887. ok:
  1888. if (!S_ISREG(nd->inode->i_mode))
  1889. will_truncate = 0;
  1890. if (will_truncate) {
  1891. error = mnt_want_write(nd->path.mnt);
  1892. if (error)
  1893. goto exit;
  1894. want_write = 1;
  1895. }
  1896. common:
  1897. error = may_open(&nd->path, acc_mode, open_flag);
  1898. if (error)
  1899. goto exit;
  1900. filp = nameidata_to_filp(nd);
  1901. if (!IS_ERR(filp)) {
  1902. error = ima_file_check(filp, op->acc_mode);
  1903. if (error) {
  1904. fput(filp);
  1905. filp = ERR_PTR(error);
  1906. }
  1907. }
  1908. if (!IS_ERR(filp)) {
  1909. if (will_truncate) {
  1910. error = handle_truncate(filp);
  1911. if (error) {
  1912. fput(filp);
  1913. filp = ERR_PTR(error);
  1914. }
  1915. }
  1916. }
  1917. out:
  1918. if (want_write)
  1919. mnt_drop_write(nd->path.mnt);
  1920. path_put(&nd->path);
  1921. return filp;
  1922. exit_mutex_unlock:
  1923. mutex_unlock(&dir->d_inode->i_mutex);
  1924. exit_dput:
  1925. path_put_conditional(path, nd);
  1926. exit:
  1927. filp = ERR_PTR(error);
  1928. goto out;
  1929. }
  1930. static struct file *path_openat(int dfd, const char *pathname,
  1931. struct nameidata *nd, const struct open_flags *op, int flags)
  1932. {
  1933. struct file *base = NULL;
  1934. struct file *filp;
  1935. struct path path;
  1936. int error;
  1937. filp = get_empty_filp();
  1938. if (!filp)
  1939. return ERR_PTR(-ENFILE);
  1940. filp->f_flags = op->open_flag;
  1941. nd->intent.open.file = filp;
  1942. nd->intent.open.flags = open_to_namei_flags(op->open_flag);
  1943. nd->intent.open.create_mode = op->mode;
  1944. error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
  1945. if (unlikely(error))
  1946. goto out_filp;
  1947. current->total_link_count = 0;
  1948. error = link_path_walk(pathname, nd);
  1949. if (unlikely(error))
  1950. goto out_filp;
  1951. filp = do_last(nd, &path, op, pathname);
  1952. while (unlikely(!filp)) { /* trailing symlink */
  1953. struct path link = path;
  1954. void *cookie;
  1955. if (!(nd->flags & LOOKUP_FOLLOW)) {
  1956. path_put_conditional(&path, nd);
  1957. path_put(&nd->path);
  1958. filp = ERR_PTR(-ELOOP);
  1959. break;
  1960. }
  1961. nd->flags |= LOOKUP_PARENT;
  1962. nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
  1963. error = follow_link(&link, nd, &cookie);
  1964. if (unlikely(error))
  1965. filp = ERR_PTR(error);
  1966. else
  1967. filp = do_last(nd, &path, op, pathname);
  1968. put_link(nd, &link, cookie);
  1969. }
  1970. out:
  1971. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
  1972. path_put(&nd->root);
  1973. if (base)
  1974. fput(base);
  1975. release_open_intent(nd);
  1976. return filp;
  1977. out_filp:
  1978. filp = ERR_PTR(error);
  1979. goto out;
  1980. }
  1981. struct file *do_filp_open(int dfd, const char *pathname,
  1982. const struct open_flags *op, int flags)
  1983. {
  1984. struct nameidata nd;
  1985. struct file *filp;
  1986. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
  1987. if (unlikely(filp == ERR_PTR(-ECHILD)))
  1988. filp = path_openat(dfd, pathname, &nd, op, flags);
  1989. if (unlikely(filp == ERR_PTR(-ESTALE)))
  1990. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
  1991. return filp;
  1992. }
  1993. struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  1994. const char *name, const struct open_flags *op, int flags)
  1995. {
  1996. struct nameidata nd;
  1997. struct file *file;
  1998. nd.root.mnt = mnt;
  1999. nd.root.dentry = dentry;
  2000. flags |= LOOKUP_ROOT;
  2001. if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
  2002. return ERR_PTR(-ELOOP);
  2003. file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
  2004. if (unlikely(file == ERR_PTR(-ECHILD)))
  2005. file = path_openat(-1, name, &nd, op, flags);
  2006. if (unlikely(file == ERR_PTR(-ESTALE)))
  2007. file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
  2008. return file;
  2009. }
  2010. struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
  2011. {
  2012. struct dentry *dentry = ERR_PTR(-EEXIST);
  2013. struct nameidata nd;
  2014. int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
  2015. if (error)
  2016. return ERR_PTR(error);
  2017. /*
  2018. * Yucky last component or no last component at all?
  2019. * (foo/., foo/.., /////)
  2020. */
  2021. if (nd.last_type != LAST_NORM)
  2022. goto out;
  2023. nd.flags &= ~LOOKUP_PARENT;
  2024. nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  2025. nd.intent.open.flags = O_EXCL;
  2026. /*
  2027. * Do the final lookup.
  2028. */
  2029. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  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. goto fail;
  2045. }
  2046. *path = nd.path;
  2047. return dentry;
  2048. eexist:
  2049. dput(dentry);
  2050. dentry = ERR_PTR(-EEXIST);
  2051. fail:
  2052. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2053. out:
  2054. path_put(&nd.path);
  2055. return dentry;
  2056. }
  2057. EXPORT_SYMBOL(kern_path_create);
  2058. struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
  2059. {
  2060. char *tmp = getname(pathname);
  2061. struct dentry *res;
  2062. if (IS_ERR(tmp))
  2063. return ERR_CAST(tmp);
  2064. res = kern_path_create(dfd, tmp, path, is_dir);
  2065. putname(tmp);
  2066. return res;
  2067. }
  2068. EXPORT_SYMBOL(user_path_create);
  2069. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  2070. {
  2071. int error = may_create(dir, dentry);
  2072. if (error)
  2073. return error;
  2074. if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
  2075. !ns_capable(inode_userns(dir), CAP_MKNOD))
  2076. return -EPERM;
  2077. if (!dir->i_op->mknod)
  2078. return -EPERM;
  2079. error = devcgroup_inode_mknod(mode, dev);
  2080. if (error)
  2081. return error;
  2082. error = security_inode_mknod(dir, dentry, mode, dev);
  2083. if (error)
  2084. return error;
  2085. error = dir->i_op->mknod(dir, dentry, mode, dev);
  2086. if (!error)
  2087. fsnotify_create(dir, dentry);
  2088. return error;
  2089. }
  2090. static int may_mknod(mode_t mode)
  2091. {
  2092. switch (mode & S_IFMT) {
  2093. case S_IFREG:
  2094. case S_IFCHR:
  2095. case S_IFBLK:
  2096. case S_IFIFO:
  2097. case S_IFSOCK:
  2098. case 0: /* zero mode translates to S_IFREG */
  2099. return 0;
  2100. case S_IFDIR:
  2101. return -EPERM;
  2102. default:
  2103. return -EINVAL;
  2104. }
  2105. }
  2106. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
  2107. unsigned, dev)
  2108. {
  2109. struct dentry *dentry;
  2110. struct path path;
  2111. int error;
  2112. if (S_ISDIR(mode))
  2113. return -EPERM;
  2114. dentry = user_path_create(dfd, filename, &path, 0);
  2115. if (IS_ERR(dentry))
  2116. return PTR_ERR(dentry);
  2117. if (!IS_POSIXACL(path.dentry->d_inode))
  2118. mode &= ~current_umask();
  2119. error = may_mknod(mode);
  2120. if (error)
  2121. goto out_dput;
  2122. error = mnt_want_write(path.mnt);
  2123. if (error)
  2124. goto out_dput;
  2125. error = security_path_mknod(&path, dentry, mode, dev);
  2126. if (error)
  2127. goto out_drop_write;
  2128. switch (mode & S_IFMT) {
  2129. case 0: case S_IFREG:
  2130. error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
  2131. break;
  2132. case S_IFCHR: case S_IFBLK:
  2133. error = vfs_mknod(path.dentry->d_inode,dentry,mode,
  2134. new_decode_dev(dev));
  2135. break;
  2136. case S_IFIFO: case S_IFSOCK:
  2137. error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
  2138. break;
  2139. }
  2140. out_drop_write:
  2141. mnt_drop_write(path.mnt);
  2142. out_dput:
  2143. dput(dentry);
  2144. mutex_unlock(&path.dentry->d_inode->i_mutex);
  2145. path_put(&path);
  2146. return error;
  2147. }
  2148. SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
  2149. {
  2150. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  2151. }
  2152. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  2153. {
  2154. int error = may_create(dir, dentry);
  2155. if (error)
  2156. return error;
  2157. if (!dir->i_op->mkdir)
  2158. return -EPERM;
  2159. mode &= (S_IRWXUGO|S_ISVTX);
  2160. error = security_inode_mkdir(dir, dentry, mode);
  2161. if (error)
  2162. return error;
  2163. error = dir->i_op->mkdir(dir, dentry, mode);
  2164. if (!error)
  2165. fsnotify_mkdir(dir, dentry);
  2166. return error;
  2167. }
  2168. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
  2169. {
  2170. struct dentry *dentry;
  2171. struct path path;
  2172. int error;
  2173. dentry = user_path_create(dfd, pathname, &path, 1);
  2174. if (IS_ERR(dentry))
  2175. return PTR_ERR(dentry);
  2176. if (!IS_POSIXACL(path.dentry->d_inode))
  2177. mode &= ~current_umask();
  2178. error = mnt_want_write(path.mnt);
  2179. if (error)
  2180. goto out_dput;
  2181. error = security_path_mkdir(&path, dentry, mode);
  2182. if (error)
  2183. goto out_drop_write;
  2184. error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
  2185. out_drop_write:
  2186. mnt_drop_write(path.mnt);
  2187. out_dput:
  2188. dput(dentry);
  2189. mutex_unlock(&path.dentry->d_inode->i_mutex);
  2190. path_put(&path);
  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. struct dentry *dentry;
  2412. struct path path;
  2413. from = getname(oldname);
  2414. if (IS_ERR(from))
  2415. return PTR_ERR(from);
  2416. dentry = user_path_create(newdfd, newname, &path, 0);
  2417. error = PTR_ERR(dentry);
  2418. if (IS_ERR(dentry))
  2419. goto out_putname;
  2420. error = mnt_want_write(path.mnt);
  2421. if (error)
  2422. goto out_dput;
  2423. error = security_path_symlink(&path, dentry, from);
  2424. if (error)
  2425. goto out_drop_write;
  2426. error = vfs_symlink(path.dentry->d_inode, dentry, from);
  2427. out_drop_write:
  2428. mnt_drop_write(path.mnt);
  2429. out_dput:
  2430. dput(dentry);
  2431. mutex_unlock(&path.dentry->d_inode->i_mutex);
  2432. path_put(&path);
  2433. out_putname:
  2434. putname(from);
  2435. return error;
  2436. }
  2437. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  2438. {
  2439. return sys_symlinkat(oldname, AT_FDCWD, newname);
  2440. }
  2441. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2442. {
  2443. struct inode *inode = old_dentry->d_inode;
  2444. int error;
  2445. if (!inode)
  2446. return -ENOENT;
  2447. error = may_create(dir, new_dentry);
  2448. if (error)
  2449. return error;
  2450. if (dir->i_sb != inode->i_sb)
  2451. return -EXDEV;
  2452. /*
  2453. * A link to an append-only or immutable file cannot be created.
  2454. */
  2455. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2456. return -EPERM;
  2457. if (!dir->i_op->link)
  2458. return -EPERM;
  2459. if (S_ISDIR(inode->i_mode))
  2460. return -EPERM;
  2461. error = security_inode_link(old_dentry, dir, new_dentry);
  2462. if (error)
  2463. return error;
  2464. mutex_lock(&inode->i_mutex);
  2465. /* Make sure we don't allow creating hardlink to an unlinked file */
  2466. if (inode->i_nlink == 0)
  2467. error = -ENOENT;
  2468. else
  2469. error = dir->i_op->link(old_dentry, dir, new_dentry);
  2470. mutex_unlock(&inode->i_mutex);
  2471. if (!error)
  2472. fsnotify_link(dir, inode, new_dentry);
  2473. return error;
  2474. }
  2475. /*
  2476. * Hardlinks are often used in delicate situations. We avoid
  2477. * security-related surprises by not following symlinks on the
  2478. * newname. --KAB
  2479. *
  2480. * We don't follow them on the oldname either to be compatible
  2481. * with linux 2.0, and to avoid hard-linking to directories
  2482. * and other special files. --ADM
  2483. */
  2484. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  2485. int, newdfd, const char __user *, newname, int, flags)
  2486. {
  2487. struct dentry *new_dentry;
  2488. struct path old_path, new_path;
  2489. int how = 0;
  2490. int error;
  2491. if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  2492. return -EINVAL;
  2493. /*
  2494. * To use null names we require CAP_DAC_READ_SEARCH
  2495. * This ensures that not everyone will be able to create
  2496. * handlink using the passed filedescriptor.
  2497. */
  2498. if (flags & AT_EMPTY_PATH) {
  2499. if (!capable(CAP_DAC_READ_SEARCH))
  2500. return -ENOENT;
  2501. how = LOOKUP_EMPTY;
  2502. }
  2503. if (flags & AT_SYMLINK_FOLLOW)
  2504. how |= LOOKUP_FOLLOW;
  2505. error = user_path_at(olddfd, oldname, how, &old_path);
  2506. if (error)
  2507. return error;
  2508. new_dentry = user_path_create(newdfd, newname, &new_path, 0);
  2509. error = PTR_ERR(new_dentry);
  2510. if (IS_ERR(new_dentry))
  2511. goto out;
  2512. error = -EXDEV;
  2513. if (old_path.mnt != new_path.mnt)
  2514. goto out_dput;
  2515. error = mnt_want_write(new_path.mnt);
  2516. if (error)
  2517. goto out_dput;
  2518. error = security_path_link(old_path.dentry, &new_path, new_dentry);
  2519. if (error)
  2520. goto out_drop_write;
  2521. error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
  2522. out_drop_write:
  2523. mnt_drop_write(new_path.mnt);
  2524. out_dput:
  2525. dput(new_dentry);
  2526. mutex_unlock(&new_path.dentry->d_inode->i_mutex);
  2527. path_put(&new_path);
  2528. out:
  2529. path_put(&old_path);
  2530. return error;
  2531. }
  2532. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  2533. {
  2534. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  2535. }
  2536. /*
  2537. * The worst of all namespace operations - renaming directory. "Perverted"
  2538. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  2539. * Problems:
  2540. * a) we can get into loop creation. Check is done in is_subdir().
  2541. * b) race potential - two innocent renames can create a loop together.
  2542. * That's where 4.4 screws up. Current fix: serialization on
  2543. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  2544. * story.
  2545. * c) we have to lock _three_ objects - parents and victim (if it exists).
  2546. * And that - after we got ->i_mutex on parents (until then we don't know
  2547. * whether the target exists). Solution: try to be smart with locking
  2548. * order for inodes. We rely on the fact that tree topology may change
  2549. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  2550. * move will be locked. Thus we can rank directories by the tree
  2551. * (ancestors first) and rank all non-directories after them.
  2552. * That works since everybody except rename does "lock parent, lookup,
  2553. * lock child" and rename is under ->s_vfs_rename_mutex.
  2554. * HOWEVER, it relies on the assumption that any object with ->lookup()
  2555. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  2556. * we'd better make sure that there's no link(2) for them.
  2557. * d) conversion from fhandle to dentry may come in the wrong moment - when
  2558. * we are removing the target. Solution: we will have to grab ->i_mutex
  2559. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  2560. * ->i_mutex on parents, which works but leads to some truly excessive
  2561. * locking].
  2562. */
  2563. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  2564. struct inode *new_dir, struct dentry *new_dentry)
  2565. {
  2566. int error = 0;
  2567. struct inode *target = new_dentry->d_inode;
  2568. /*
  2569. * If we are going to change the parent - check write permissions,
  2570. * we'll need to flip '..'.
  2571. */
  2572. if (new_dir != old_dir) {
  2573. error = inode_permission(old_dentry->d_inode, MAY_WRITE);
  2574. if (error)
  2575. return error;
  2576. }
  2577. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2578. if (error)
  2579. return error;
  2580. if (target)
  2581. mutex_lock(&target->i_mutex);
  2582. error = -EBUSY;
  2583. if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
  2584. goto out;
  2585. if (target)
  2586. shrink_dcache_parent(new_dentry);
  2587. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2588. if (error)
  2589. goto out;
  2590. if (target) {
  2591. target->i_flags |= S_DEAD;
  2592. dont_mount(new_dentry);
  2593. }
  2594. out:
  2595. if (target)
  2596. mutex_unlock(&target->i_mutex);
  2597. if (!error)
  2598. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2599. d_move(old_dentry,new_dentry);
  2600. return error;
  2601. }
  2602. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2603. struct inode *new_dir, struct dentry *new_dentry)
  2604. {
  2605. struct inode *target = new_dentry->d_inode;
  2606. int error;
  2607. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2608. if (error)
  2609. return error;
  2610. dget(new_dentry);
  2611. if (target)
  2612. mutex_lock(&target->i_mutex);
  2613. error = -EBUSY;
  2614. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2615. goto out;
  2616. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2617. if (error)
  2618. goto out;
  2619. if (target)
  2620. dont_mount(new_dentry);
  2621. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2622. d_move(old_dentry, new_dentry);
  2623. out:
  2624. if (target)
  2625. mutex_unlock(&target->i_mutex);
  2626. dput(new_dentry);
  2627. return error;
  2628. }
  2629. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2630. struct inode *new_dir, struct dentry *new_dentry)
  2631. {
  2632. int error;
  2633. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2634. const unsigned char *old_name;
  2635. if (old_dentry->d_inode == new_dentry->d_inode)
  2636. return 0;
  2637. error = may_delete(old_dir, old_dentry, is_dir);
  2638. if (error)
  2639. return error;
  2640. if (!new_dentry->d_inode)
  2641. error = may_create(new_dir, new_dentry);
  2642. else
  2643. error = may_delete(new_dir, new_dentry, is_dir);
  2644. if (error)
  2645. return error;
  2646. if (!old_dir->i_op->rename)
  2647. return -EPERM;
  2648. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2649. if (is_dir)
  2650. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2651. else
  2652. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2653. if (!error)
  2654. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  2655. new_dentry->d_inode, old_dentry);
  2656. fsnotify_oldname_free(old_name);
  2657. return error;
  2658. }
  2659. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  2660. int, newdfd, const char __user *, newname)
  2661. {
  2662. struct dentry *old_dir, *new_dir;
  2663. struct dentry *old_dentry, *new_dentry;
  2664. struct dentry *trap;
  2665. struct nameidata oldnd, newnd;
  2666. char *from;
  2667. char *to;
  2668. int error;
  2669. error = user_path_parent(olddfd, oldname, &oldnd, &from);
  2670. if (error)
  2671. goto exit;
  2672. error = user_path_parent(newdfd, newname, &newnd, &to);
  2673. if (error)
  2674. goto exit1;
  2675. error = -EXDEV;
  2676. if (oldnd.path.mnt != newnd.path.mnt)
  2677. goto exit2;
  2678. old_dir = oldnd.path.dentry;
  2679. error = -EBUSY;
  2680. if (oldnd.last_type != LAST_NORM)
  2681. goto exit2;
  2682. new_dir = newnd.path.dentry;
  2683. if (newnd.last_type != LAST_NORM)
  2684. goto exit2;
  2685. oldnd.flags &= ~LOOKUP_PARENT;
  2686. newnd.flags &= ~LOOKUP_PARENT;
  2687. newnd.flags |= LOOKUP_RENAME_TARGET;
  2688. trap = lock_rename(new_dir, old_dir);
  2689. old_dentry = lookup_hash(&oldnd);
  2690. error = PTR_ERR(old_dentry);
  2691. if (IS_ERR(old_dentry))
  2692. goto exit3;
  2693. /* source must exist */
  2694. error = -ENOENT;
  2695. if (!old_dentry->d_inode)
  2696. goto exit4;
  2697. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2698. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2699. error = -ENOTDIR;
  2700. if (oldnd.last.name[oldnd.last.len])
  2701. goto exit4;
  2702. if (newnd.last.name[newnd.last.len])
  2703. goto exit4;
  2704. }
  2705. /* source should not be ancestor of target */
  2706. error = -EINVAL;
  2707. if (old_dentry == trap)
  2708. goto exit4;
  2709. new_dentry = lookup_hash(&newnd);
  2710. error = PTR_ERR(new_dentry);
  2711. if (IS_ERR(new_dentry))
  2712. goto exit4;
  2713. /* target should not be an ancestor of source */
  2714. error = -ENOTEMPTY;
  2715. if (new_dentry == trap)
  2716. goto exit5;
  2717. error = mnt_want_write(oldnd.path.mnt);
  2718. if (error)
  2719. goto exit5;
  2720. error = security_path_rename(&oldnd.path, old_dentry,
  2721. &newnd.path, new_dentry);
  2722. if (error)
  2723. goto exit6;
  2724. error = vfs_rename(old_dir->d_inode, old_dentry,
  2725. new_dir->d_inode, new_dentry);
  2726. exit6:
  2727. mnt_drop_write(oldnd.path.mnt);
  2728. exit5:
  2729. dput(new_dentry);
  2730. exit4:
  2731. dput(old_dentry);
  2732. exit3:
  2733. unlock_rename(new_dir, old_dir);
  2734. exit2:
  2735. path_put(&newnd.path);
  2736. putname(to);
  2737. exit1:
  2738. path_put(&oldnd.path);
  2739. putname(from);
  2740. exit:
  2741. return error;
  2742. }
  2743. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  2744. {
  2745. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  2746. }
  2747. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2748. {
  2749. int len;
  2750. len = PTR_ERR(link);
  2751. if (IS_ERR(link))
  2752. goto out;
  2753. len = strlen(link);
  2754. if (len > (unsigned) buflen)
  2755. len = buflen;
  2756. if (copy_to_user(buffer, link, len))
  2757. len = -EFAULT;
  2758. out:
  2759. return len;
  2760. }
  2761. /*
  2762. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2763. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2764. * using) it for any given inode is up to filesystem.
  2765. */
  2766. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2767. {
  2768. struct nameidata nd;
  2769. void *cookie;
  2770. int res;
  2771. nd.depth = 0;
  2772. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2773. if (IS_ERR(cookie))
  2774. return PTR_ERR(cookie);
  2775. res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2776. if (dentry->d_inode->i_op->put_link)
  2777. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  2778. return res;
  2779. }
  2780. int vfs_follow_link(struct nameidata *nd, const char *link)
  2781. {
  2782. return __vfs_follow_link(nd, link);
  2783. }
  2784. /* get the link contents into pagecache */
  2785. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2786. {
  2787. char *kaddr;
  2788. struct page *page;
  2789. struct address_space *mapping = dentry->d_inode->i_mapping;
  2790. page = read_mapping_page(mapping, 0, NULL);
  2791. if (IS_ERR(page))
  2792. return (char*)page;
  2793. *ppage = page;
  2794. kaddr = kmap(page);
  2795. nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
  2796. return kaddr;
  2797. }
  2798. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2799. {
  2800. struct page *page = NULL;
  2801. char *s = page_getlink(dentry, &page);
  2802. int res = vfs_readlink(dentry,buffer,buflen,s);
  2803. if (page) {
  2804. kunmap(page);
  2805. page_cache_release(page);
  2806. }
  2807. return res;
  2808. }
  2809. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2810. {
  2811. struct page *page = NULL;
  2812. nd_set_link(nd, page_getlink(dentry, &page));
  2813. return page;
  2814. }
  2815. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2816. {
  2817. struct page *page = cookie;
  2818. if (page) {
  2819. kunmap(page);
  2820. page_cache_release(page);
  2821. }
  2822. }
  2823. /*
  2824. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  2825. */
  2826. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  2827. {
  2828. struct address_space *mapping = inode->i_mapping;
  2829. struct page *page;
  2830. void *fsdata;
  2831. int err;
  2832. char *kaddr;
  2833. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  2834. if (nofs)
  2835. flags |= AOP_FLAG_NOFS;
  2836. retry:
  2837. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  2838. flags, &page, &fsdata);
  2839. if (err)
  2840. goto fail;
  2841. kaddr = kmap_atomic(page, KM_USER0);
  2842. memcpy(kaddr, symname, len-1);
  2843. kunmap_atomic(kaddr, KM_USER0);
  2844. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  2845. page, fsdata);
  2846. if (err < 0)
  2847. goto fail;
  2848. if (err < len-1)
  2849. goto retry;
  2850. mark_inode_dirty(inode);
  2851. return 0;
  2852. fail:
  2853. return err;
  2854. }
  2855. int page_symlink(struct inode *inode, const char *symname, int len)
  2856. {
  2857. return __page_symlink(inode, symname, len,
  2858. !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
  2859. }
  2860. const struct inode_operations page_symlink_inode_operations = {
  2861. .readlink = generic_readlink,
  2862. .follow_link = page_follow_link_light,
  2863. .put_link = page_put_link,
  2864. };
  2865. EXPORT_SYMBOL(user_path_at);
  2866. EXPORT_SYMBOL(follow_down_one);
  2867. EXPORT_SYMBOL(follow_down);
  2868. EXPORT_SYMBOL(follow_up);
  2869. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  2870. EXPORT_SYMBOL(getname);
  2871. EXPORT_SYMBOL(lock_rename);
  2872. EXPORT_SYMBOL(lookup_one_len);
  2873. EXPORT_SYMBOL(page_follow_link_light);
  2874. EXPORT_SYMBOL(page_put_link);
  2875. EXPORT_SYMBOL(page_readlink);
  2876. EXPORT_SYMBOL(__page_symlink);
  2877. EXPORT_SYMBOL(page_symlink);
  2878. EXPORT_SYMBOL(page_symlink_inode_operations);
  2879. EXPORT_SYMBOL(kern_path_parent);
  2880. EXPORT_SYMBOL(kern_path);
  2881. EXPORT_SYMBOL(vfs_path_lookup);
  2882. EXPORT_SYMBOL(inode_permission);
  2883. EXPORT_SYMBOL(unlock_rename);
  2884. EXPORT_SYMBOL(vfs_create);
  2885. EXPORT_SYMBOL(vfs_follow_link);
  2886. EXPORT_SYMBOL(vfs_link);
  2887. EXPORT_SYMBOL(vfs_mkdir);
  2888. EXPORT_SYMBOL(vfs_mknod);
  2889. EXPORT_SYMBOL(generic_permission);
  2890. EXPORT_SYMBOL(vfs_readlink);
  2891. EXPORT_SYMBOL(vfs_rename);
  2892. EXPORT_SYMBOL(vfs_rmdir);
  2893. EXPORT_SYMBOL(vfs_symlink);
  2894. EXPORT_SYMBOL(vfs_unlink);
  2895. EXPORT_SYMBOL(dentry_unhash);
  2896. EXPORT_SYMBOL(generic_readlink);