namei.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325
  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_CONTINUE))
  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_CONTINUE | 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_CONTINUE))
  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. unsigned int lookup_flags = nd->flags;
  1149. while (*name=='/')
  1150. name++;
  1151. if (!*name)
  1152. return 0;
  1153. /* At this point we know we have a real path component. */
  1154. for(;;) {
  1155. unsigned long hash;
  1156. struct qstr this;
  1157. unsigned int c;
  1158. int type;
  1159. nd->flags |= LOOKUP_CONTINUE;
  1160. err = may_lookup(nd);
  1161. if (err)
  1162. break;
  1163. this.name = name;
  1164. c = *(const unsigned char *)name;
  1165. hash = init_name_hash();
  1166. do {
  1167. name++;
  1168. hash = partial_name_hash(c, hash);
  1169. c = *(const unsigned char *)name;
  1170. } while (c && (c != '/'));
  1171. this.len = name - (const char *) this.name;
  1172. this.hash = end_name_hash(hash);
  1173. type = LAST_NORM;
  1174. if (this.name[0] == '.') switch (this.len) {
  1175. case 2:
  1176. if (this.name[1] == '.') {
  1177. type = LAST_DOTDOT;
  1178. nd->flags |= LOOKUP_JUMPED;
  1179. }
  1180. break;
  1181. case 1:
  1182. type = LAST_DOT;
  1183. }
  1184. if (likely(type == LAST_NORM)) {
  1185. struct dentry *parent = nd->path.dentry;
  1186. nd->flags &= ~LOOKUP_JUMPED;
  1187. if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
  1188. err = parent->d_op->d_hash(parent, nd->inode,
  1189. &this);
  1190. if (err < 0)
  1191. break;
  1192. }
  1193. }
  1194. /* remove trailing slashes? */
  1195. if (!c)
  1196. goto last_component;
  1197. while (*++name == '/');
  1198. if (!*name)
  1199. goto last_component;
  1200. err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
  1201. if (err < 0)
  1202. return err;
  1203. if (err) {
  1204. err = nested_symlink(&next, nd);
  1205. if (err)
  1206. return err;
  1207. }
  1208. err = -ENOTDIR;
  1209. if (!nd->inode->i_op->lookup)
  1210. break;
  1211. continue;
  1212. /* here ends the main loop */
  1213. last_component:
  1214. /* Clear LOOKUP_CONTINUE iff it was previously unset */
  1215. nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
  1216. nd->last = this;
  1217. nd->last_type = type;
  1218. return 0;
  1219. }
  1220. terminate_walk(nd);
  1221. return err;
  1222. }
  1223. static int path_init(int dfd, const char *name, unsigned int flags,
  1224. struct nameidata *nd, struct file **fp)
  1225. {
  1226. int retval = 0;
  1227. int fput_needed;
  1228. struct file *file;
  1229. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1230. nd->flags = flags | LOOKUP_JUMPED;
  1231. nd->depth = 0;
  1232. if (flags & LOOKUP_ROOT) {
  1233. struct inode *inode = nd->root.dentry->d_inode;
  1234. if (*name) {
  1235. if (!inode->i_op->lookup)
  1236. return -ENOTDIR;
  1237. retval = inode_permission(inode, MAY_EXEC);
  1238. if (retval)
  1239. return retval;
  1240. }
  1241. nd->path = nd->root;
  1242. nd->inode = inode;
  1243. if (flags & LOOKUP_RCU) {
  1244. br_read_lock(vfsmount_lock);
  1245. rcu_read_lock();
  1246. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1247. } else {
  1248. path_get(&nd->path);
  1249. }
  1250. return 0;
  1251. }
  1252. nd->root.mnt = NULL;
  1253. if (*name=='/') {
  1254. if (flags & LOOKUP_RCU) {
  1255. br_read_lock(vfsmount_lock);
  1256. rcu_read_lock();
  1257. set_root_rcu(nd);
  1258. } else {
  1259. set_root(nd);
  1260. path_get(&nd->root);
  1261. }
  1262. nd->path = nd->root;
  1263. } else if (dfd == AT_FDCWD) {
  1264. if (flags & LOOKUP_RCU) {
  1265. struct fs_struct *fs = current->fs;
  1266. unsigned seq;
  1267. br_read_lock(vfsmount_lock);
  1268. rcu_read_lock();
  1269. do {
  1270. seq = read_seqcount_begin(&fs->seq);
  1271. nd->path = fs->pwd;
  1272. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1273. } while (read_seqcount_retry(&fs->seq, seq));
  1274. } else {
  1275. get_fs_pwd(current->fs, &nd->path);
  1276. }
  1277. } else {
  1278. struct dentry *dentry;
  1279. file = fget_raw_light(dfd, &fput_needed);
  1280. retval = -EBADF;
  1281. if (!file)
  1282. goto out_fail;
  1283. dentry = file->f_path.dentry;
  1284. if (*name) {
  1285. retval = -ENOTDIR;
  1286. if (!S_ISDIR(dentry->d_inode->i_mode))
  1287. goto fput_fail;
  1288. retval = inode_permission(dentry->d_inode, MAY_EXEC);
  1289. if (retval)
  1290. goto fput_fail;
  1291. }
  1292. nd->path = file->f_path;
  1293. if (flags & LOOKUP_RCU) {
  1294. if (fput_needed)
  1295. *fp = file;
  1296. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1297. br_read_lock(vfsmount_lock);
  1298. rcu_read_lock();
  1299. } else {
  1300. path_get(&file->f_path);
  1301. fput_light(file, fput_needed);
  1302. }
  1303. }
  1304. nd->inode = nd->path.dentry->d_inode;
  1305. return 0;
  1306. fput_fail:
  1307. fput_light(file, fput_needed);
  1308. out_fail:
  1309. return retval;
  1310. }
  1311. static inline int lookup_last(struct nameidata *nd, struct path *path)
  1312. {
  1313. if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
  1314. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1315. nd->flags &= ~LOOKUP_PARENT;
  1316. return walk_component(nd, path, &nd->last, nd->last_type,
  1317. nd->flags & LOOKUP_FOLLOW);
  1318. }
  1319. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1320. static int path_lookupat(int dfd, const char *name,
  1321. unsigned int flags, struct nameidata *nd)
  1322. {
  1323. struct file *base = NULL;
  1324. struct path path;
  1325. int err;
  1326. /*
  1327. * Path walking is largely split up into 2 different synchronisation
  1328. * schemes, rcu-walk and ref-walk (explained in
  1329. * Documentation/filesystems/path-lookup.txt). These share much of the
  1330. * path walk code, but some things particularly setup, cleanup, and
  1331. * following mounts are sufficiently divergent that functions are
  1332. * duplicated. Typically there is a function foo(), and its RCU
  1333. * analogue, foo_rcu().
  1334. *
  1335. * -ECHILD is the error number of choice (just to avoid clashes) that
  1336. * is returned if some aspect of an rcu-walk fails. Such an error must
  1337. * be handled by restarting a traditional ref-walk (which will always
  1338. * be able to complete).
  1339. */
  1340. err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
  1341. if (unlikely(err))
  1342. return err;
  1343. current->total_link_count = 0;
  1344. err = link_path_walk(name, nd);
  1345. if (!err && !(flags & LOOKUP_PARENT)) {
  1346. err = lookup_last(nd, &path);
  1347. while (err > 0) {
  1348. void *cookie;
  1349. struct path link = path;
  1350. nd->flags |= LOOKUP_PARENT;
  1351. err = follow_link(&link, nd, &cookie);
  1352. if (!err)
  1353. err = lookup_last(nd, &path);
  1354. put_link(nd, &link, cookie);
  1355. }
  1356. }
  1357. if (!err)
  1358. err = complete_walk(nd);
  1359. if (!err && nd->flags & LOOKUP_DIRECTORY) {
  1360. if (!nd->inode->i_op->lookup) {
  1361. path_put(&nd->path);
  1362. err = -ENOTDIR;
  1363. }
  1364. }
  1365. if (base)
  1366. fput(base);
  1367. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  1368. path_put(&nd->root);
  1369. nd->root.mnt = NULL;
  1370. }
  1371. return err;
  1372. }
  1373. static int do_path_lookup(int dfd, const char *name,
  1374. unsigned int flags, struct nameidata *nd)
  1375. {
  1376. int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
  1377. if (unlikely(retval == -ECHILD))
  1378. retval = path_lookupat(dfd, name, flags, nd);
  1379. if (unlikely(retval == -ESTALE))
  1380. retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
  1381. if (likely(!retval)) {
  1382. if (unlikely(!audit_dummy_context())) {
  1383. if (nd->path.dentry && nd->inode)
  1384. audit_inode(name, nd->path.dentry);
  1385. }
  1386. }
  1387. return retval;
  1388. }
  1389. int kern_path_parent(const char *name, struct nameidata *nd)
  1390. {
  1391. return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
  1392. }
  1393. int kern_path(const char *name, unsigned int flags, struct path *path)
  1394. {
  1395. struct nameidata nd;
  1396. int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
  1397. if (!res)
  1398. *path = nd.path;
  1399. return res;
  1400. }
  1401. /**
  1402. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  1403. * @dentry: pointer to dentry of the base directory
  1404. * @mnt: pointer to vfs mount of the base directory
  1405. * @name: pointer to file name
  1406. * @flags: lookup flags
  1407. * @nd: pointer to nameidata
  1408. */
  1409. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  1410. const char *name, unsigned int flags,
  1411. struct nameidata *nd)
  1412. {
  1413. nd->root.dentry = dentry;
  1414. nd->root.mnt = mnt;
  1415. /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
  1416. return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
  1417. }
  1418. static struct dentry *__lookup_hash(struct qstr *name,
  1419. struct dentry *base, struct nameidata *nd)
  1420. {
  1421. struct inode *inode = base->d_inode;
  1422. struct dentry *dentry;
  1423. int err;
  1424. err = inode_permission(inode, MAY_EXEC);
  1425. if (err)
  1426. return ERR_PTR(err);
  1427. /*
  1428. * Don't bother with __d_lookup: callers are for creat as
  1429. * well as unlink, so a lot of the time it would cost
  1430. * a double lookup.
  1431. */
  1432. dentry = d_lookup(base, name);
  1433. if (dentry && d_need_lookup(dentry)) {
  1434. /*
  1435. * __lookup_hash is called with the parent dir's i_mutex already
  1436. * held, so we are good to go here.
  1437. */
  1438. dentry = d_inode_lookup(base, dentry, nd);
  1439. if (IS_ERR(dentry))
  1440. return dentry;
  1441. }
  1442. if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1443. int status = d_revalidate(dentry, nd);
  1444. if (unlikely(status <= 0)) {
  1445. /*
  1446. * The dentry failed validation.
  1447. * If d_revalidate returned 0 attempt to invalidate
  1448. * the dentry otherwise d_revalidate is asking us
  1449. * to return a fail status.
  1450. */
  1451. if (status < 0) {
  1452. dput(dentry);
  1453. return ERR_PTR(status);
  1454. } else if (!d_invalidate(dentry)) {
  1455. dput(dentry);
  1456. dentry = NULL;
  1457. }
  1458. }
  1459. }
  1460. if (!dentry)
  1461. dentry = d_alloc_and_lookup(base, name, nd);
  1462. return dentry;
  1463. }
  1464. /*
  1465. * Restricted form of lookup. Doesn't follow links, single-component only,
  1466. * needs parent already locked. Doesn't follow mounts.
  1467. * SMP-safe.
  1468. */
  1469. static struct dentry *lookup_hash(struct nameidata *nd)
  1470. {
  1471. return __lookup_hash(&nd->last, nd->path.dentry, nd);
  1472. }
  1473. /**
  1474. * lookup_one_len - filesystem helper to lookup single pathname component
  1475. * @name: pathname component to lookup
  1476. * @base: base directory to lookup from
  1477. * @len: maximum length @len should be interpreted to
  1478. *
  1479. * Note that this routine is purely a helper for filesystem usage and should
  1480. * not be called by generic code. Also note that by using this function the
  1481. * nameidata argument is passed to the filesystem methods and a filesystem
  1482. * using this helper needs to be prepared for that.
  1483. */
  1484. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1485. {
  1486. struct qstr this;
  1487. unsigned long hash;
  1488. unsigned int c;
  1489. WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
  1490. this.name = name;
  1491. this.len = len;
  1492. if (!len)
  1493. return ERR_PTR(-EACCES);
  1494. hash = init_name_hash();
  1495. while (len--) {
  1496. c = *(const unsigned char *)name++;
  1497. if (c == '/' || c == '\0')
  1498. return ERR_PTR(-EACCES);
  1499. hash = partial_name_hash(c, hash);
  1500. }
  1501. this.hash = end_name_hash(hash);
  1502. /*
  1503. * See if the low-level filesystem might want
  1504. * to use its own hash..
  1505. */
  1506. if (base->d_flags & DCACHE_OP_HASH) {
  1507. int err = base->d_op->d_hash(base, base->d_inode, &this);
  1508. if (err < 0)
  1509. return ERR_PTR(err);
  1510. }
  1511. return __lookup_hash(&this, base, NULL);
  1512. }
  1513. int user_path_at(int dfd, const char __user *name, unsigned flags,
  1514. struct path *path)
  1515. {
  1516. struct nameidata nd;
  1517. char *tmp = getname_flags(name, flags);
  1518. int err = PTR_ERR(tmp);
  1519. if (!IS_ERR(tmp)) {
  1520. BUG_ON(flags & LOOKUP_PARENT);
  1521. err = do_path_lookup(dfd, tmp, flags, &nd);
  1522. putname(tmp);
  1523. if (!err)
  1524. *path = nd.path;
  1525. }
  1526. return err;
  1527. }
  1528. static int user_path_parent(int dfd, const char __user *path,
  1529. struct nameidata *nd, char **name)
  1530. {
  1531. char *s = getname(path);
  1532. int error;
  1533. if (IS_ERR(s))
  1534. return PTR_ERR(s);
  1535. error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
  1536. if (error)
  1537. putname(s);
  1538. else
  1539. *name = s;
  1540. return error;
  1541. }
  1542. /*
  1543. * It's inline, so penalty for filesystems that don't use sticky bit is
  1544. * minimal.
  1545. */
  1546. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1547. {
  1548. uid_t fsuid = current_fsuid();
  1549. if (!(dir->i_mode & S_ISVTX))
  1550. return 0;
  1551. if (current_user_ns() != inode_userns(inode))
  1552. goto other_userns;
  1553. if (inode->i_uid == fsuid)
  1554. return 0;
  1555. if (dir->i_uid == fsuid)
  1556. return 0;
  1557. other_userns:
  1558. return !ns_capable(inode_userns(inode), CAP_FOWNER);
  1559. }
  1560. /*
  1561. * Check whether we can remove a link victim from directory dir, check
  1562. * whether the type of victim is right.
  1563. * 1. We can't do it if dir is read-only (done in permission())
  1564. * 2. We should have write and exec permissions on dir
  1565. * 3. We can't remove anything from append-only dir
  1566. * 4. We can't do anything with immutable dir (done in permission())
  1567. * 5. If the sticky bit on dir is set we should either
  1568. * a. be owner of dir, or
  1569. * b. be owner of victim, or
  1570. * c. have CAP_FOWNER capability
  1571. * 6. If the victim is append-only or immutable we can't do antyhing with
  1572. * links pointing to it.
  1573. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1574. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1575. * 9. We can't remove a root or mountpoint.
  1576. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1577. * nfs_async_unlink().
  1578. */
  1579. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1580. {
  1581. int error;
  1582. if (!victim->d_inode)
  1583. return -ENOENT;
  1584. BUG_ON(victim->d_parent->d_inode != dir);
  1585. audit_inode_child(victim, dir);
  1586. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1587. if (error)
  1588. return error;
  1589. if (IS_APPEND(dir))
  1590. return -EPERM;
  1591. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1592. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  1593. return -EPERM;
  1594. if (isdir) {
  1595. if (!S_ISDIR(victim->d_inode->i_mode))
  1596. return -ENOTDIR;
  1597. if (IS_ROOT(victim))
  1598. return -EBUSY;
  1599. } else if (S_ISDIR(victim->d_inode->i_mode))
  1600. return -EISDIR;
  1601. if (IS_DEADDIR(dir))
  1602. return -ENOENT;
  1603. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1604. return -EBUSY;
  1605. return 0;
  1606. }
  1607. /* Check whether we can create an object with dentry child in directory
  1608. * dir.
  1609. * 1. We can't do it if child already exists (open has special treatment for
  1610. * this case, but since we are inlined it's OK)
  1611. * 2. We can't do it if dir is read-only (done in permission())
  1612. * 3. We should have write and exec permissions on dir
  1613. * 4. We can't do it if dir is immutable (done in permission())
  1614. */
  1615. static inline int may_create(struct inode *dir, struct dentry *child)
  1616. {
  1617. if (child->d_inode)
  1618. return -EEXIST;
  1619. if (IS_DEADDIR(dir))
  1620. return -ENOENT;
  1621. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1622. }
  1623. /*
  1624. * p1 and p2 should be directories on the same fs.
  1625. */
  1626. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1627. {
  1628. struct dentry *p;
  1629. if (p1 == p2) {
  1630. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1631. return NULL;
  1632. }
  1633. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1634. p = d_ancestor(p2, p1);
  1635. if (p) {
  1636. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1637. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1638. return p;
  1639. }
  1640. p = d_ancestor(p1, p2);
  1641. if (p) {
  1642. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1643. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1644. return p;
  1645. }
  1646. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1647. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1648. return NULL;
  1649. }
  1650. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1651. {
  1652. mutex_unlock(&p1->d_inode->i_mutex);
  1653. if (p1 != p2) {
  1654. mutex_unlock(&p2->d_inode->i_mutex);
  1655. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1656. }
  1657. }
  1658. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1659. struct nameidata *nd)
  1660. {
  1661. int error = may_create(dir, dentry);
  1662. if (error)
  1663. return error;
  1664. if (!dir->i_op->create)
  1665. return -EACCES; /* shouldn't it be ENOSYS? */
  1666. mode &= S_IALLUGO;
  1667. mode |= S_IFREG;
  1668. error = security_inode_create(dir, dentry, mode);
  1669. if (error)
  1670. return error;
  1671. error = dir->i_op->create(dir, dentry, mode, nd);
  1672. if (!error)
  1673. fsnotify_create(dir, dentry);
  1674. return error;
  1675. }
  1676. static int may_open(struct path *path, int acc_mode, int flag)
  1677. {
  1678. struct dentry *dentry = path->dentry;
  1679. struct inode *inode = dentry->d_inode;
  1680. int error;
  1681. /* O_PATH? */
  1682. if (!acc_mode)
  1683. return 0;
  1684. if (!inode)
  1685. return -ENOENT;
  1686. switch (inode->i_mode & S_IFMT) {
  1687. case S_IFLNK:
  1688. return -ELOOP;
  1689. case S_IFDIR:
  1690. if (acc_mode & MAY_WRITE)
  1691. return -EISDIR;
  1692. break;
  1693. case S_IFBLK:
  1694. case S_IFCHR:
  1695. if (path->mnt->mnt_flags & MNT_NODEV)
  1696. return -EACCES;
  1697. /*FALLTHRU*/
  1698. case S_IFIFO:
  1699. case S_IFSOCK:
  1700. flag &= ~O_TRUNC;
  1701. break;
  1702. }
  1703. error = inode_permission(inode, acc_mode);
  1704. if (error)
  1705. return error;
  1706. /*
  1707. * An append-only file must be opened in append mode for writing.
  1708. */
  1709. if (IS_APPEND(inode)) {
  1710. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  1711. return -EPERM;
  1712. if (flag & O_TRUNC)
  1713. return -EPERM;
  1714. }
  1715. /* O_NOATIME can only be set by the owner or superuser */
  1716. if (flag & O_NOATIME && !inode_owner_or_capable(inode))
  1717. return -EPERM;
  1718. /*
  1719. * Ensure there are no outstanding leases on the file.
  1720. */
  1721. return break_lease(inode, flag);
  1722. }
  1723. static int handle_truncate(struct file *filp)
  1724. {
  1725. struct path *path = &filp->f_path;
  1726. struct inode *inode = path->dentry->d_inode;
  1727. int error = get_write_access(inode);
  1728. if (error)
  1729. return error;
  1730. /*
  1731. * Refuse to truncate files with mandatory locks held on them.
  1732. */
  1733. error = locks_verify_locked(inode);
  1734. if (!error)
  1735. error = security_path_truncate(path);
  1736. if (!error) {
  1737. error = do_truncate(path->dentry, 0,
  1738. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  1739. filp);
  1740. }
  1741. put_write_access(inode);
  1742. return error;
  1743. }
  1744. /*
  1745. * Note that while the flag value (low two bits) for sys_open means:
  1746. * 00 - read-only
  1747. * 01 - write-only
  1748. * 10 - read-write
  1749. * 11 - special
  1750. * it is changed into
  1751. * 00 - no permissions needed
  1752. * 01 - read-permission
  1753. * 10 - write-permission
  1754. * 11 - read-write
  1755. * for the internal routines (ie open_namei()/follow_link() etc)
  1756. * This is more logical, and also allows the 00 "no perm needed"
  1757. * to be used for symlinks (where the permissions are checked
  1758. * later).
  1759. *
  1760. */
  1761. static inline int open_to_namei_flags(int flag)
  1762. {
  1763. if ((flag+1) & O_ACCMODE)
  1764. flag++;
  1765. return flag;
  1766. }
  1767. /*
  1768. * Handle the last step of open()
  1769. */
  1770. static struct file *do_last(struct nameidata *nd, struct path *path,
  1771. const struct open_flags *op, const char *pathname)
  1772. {
  1773. struct dentry *dir = nd->path.dentry;
  1774. struct dentry *dentry;
  1775. int open_flag = op->open_flag;
  1776. int will_truncate = open_flag & O_TRUNC;
  1777. int want_write = 0;
  1778. int acc_mode = op->acc_mode;
  1779. struct file *filp;
  1780. int error;
  1781. nd->flags &= ~LOOKUP_PARENT;
  1782. nd->flags |= op->intent;
  1783. switch (nd->last_type) {
  1784. case LAST_DOTDOT:
  1785. case LAST_DOT:
  1786. error = handle_dots(nd, nd->last_type);
  1787. if (error)
  1788. return ERR_PTR(error);
  1789. /* fallthrough */
  1790. case LAST_ROOT:
  1791. error = complete_walk(nd);
  1792. if (error)
  1793. return ERR_PTR(error);
  1794. audit_inode(pathname, nd->path.dentry);
  1795. if (open_flag & O_CREAT) {
  1796. error = -EISDIR;
  1797. goto exit;
  1798. }
  1799. goto ok;
  1800. case LAST_BIND:
  1801. error = complete_walk(nd);
  1802. if (error)
  1803. return ERR_PTR(error);
  1804. audit_inode(pathname, dir);
  1805. goto ok;
  1806. }
  1807. if (!(open_flag & O_CREAT)) {
  1808. int symlink_ok = 0;
  1809. if (nd->last.name[nd->last.len])
  1810. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1811. if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
  1812. symlink_ok = 1;
  1813. /* we _can_ be in RCU mode here */
  1814. error = walk_component(nd, path, &nd->last, LAST_NORM,
  1815. !symlink_ok);
  1816. if (error < 0)
  1817. return ERR_PTR(error);
  1818. if (error) /* symlink */
  1819. return NULL;
  1820. /* sayonara */
  1821. error = complete_walk(nd);
  1822. if (error)
  1823. return ERR_PTR(-ECHILD);
  1824. error = -ENOTDIR;
  1825. if (nd->flags & LOOKUP_DIRECTORY) {
  1826. if (!nd->inode->i_op->lookup)
  1827. goto exit;
  1828. }
  1829. audit_inode(pathname, nd->path.dentry);
  1830. goto ok;
  1831. }
  1832. /* create side of things */
  1833. error = complete_walk(nd);
  1834. if (error)
  1835. return ERR_PTR(error);
  1836. audit_inode(pathname, dir);
  1837. error = -EISDIR;
  1838. /* trailing slashes? */
  1839. if (nd->last.name[nd->last.len])
  1840. goto exit;
  1841. mutex_lock(&dir->d_inode->i_mutex);
  1842. dentry = lookup_hash(nd);
  1843. error = PTR_ERR(dentry);
  1844. if (IS_ERR(dentry)) {
  1845. mutex_unlock(&dir->d_inode->i_mutex);
  1846. goto exit;
  1847. }
  1848. path->dentry = dentry;
  1849. path->mnt = nd->path.mnt;
  1850. /* Negative dentry, just create the file */
  1851. if (!dentry->d_inode) {
  1852. int mode = op->mode;
  1853. if (!IS_POSIXACL(dir->d_inode))
  1854. mode &= ~current_umask();
  1855. /*
  1856. * This write is needed to ensure that a
  1857. * rw->ro transition does not occur between
  1858. * the time when the file is created and when
  1859. * a permanent write count is taken through
  1860. * the 'struct file' in nameidata_to_filp().
  1861. */
  1862. error = mnt_want_write(nd->path.mnt);
  1863. if (error)
  1864. goto exit_mutex_unlock;
  1865. want_write = 1;
  1866. /* Don't check for write permission, don't truncate */
  1867. open_flag &= ~O_TRUNC;
  1868. will_truncate = 0;
  1869. acc_mode = MAY_OPEN;
  1870. error = security_path_mknod(&nd->path, dentry, mode, 0);
  1871. if (error)
  1872. goto exit_mutex_unlock;
  1873. error = vfs_create(dir->d_inode, dentry, mode, nd);
  1874. if (error)
  1875. goto exit_mutex_unlock;
  1876. mutex_unlock(&dir->d_inode->i_mutex);
  1877. dput(nd->path.dentry);
  1878. nd->path.dentry = dentry;
  1879. goto common;
  1880. }
  1881. /*
  1882. * It already exists.
  1883. */
  1884. mutex_unlock(&dir->d_inode->i_mutex);
  1885. audit_inode(pathname, path->dentry);
  1886. error = -EEXIST;
  1887. if (open_flag & O_EXCL)
  1888. goto exit_dput;
  1889. error = follow_managed(path, nd->flags);
  1890. if (error < 0)
  1891. goto exit_dput;
  1892. error = -ENOENT;
  1893. if (!path->dentry->d_inode)
  1894. goto exit_dput;
  1895. if (path->dentry->d_inode->i_op->follow_link)
  1896. return NULL;
  1897. path_to_nameidata(path, nd);
  1898. nd->inode = path->dentry->d_inode;
  1899. error = -EISDIR;
  1900. if (S_ISDIR(nd->inode->i_mode))
  1901. goto exit;
  1902. ok:
  1903. if (!S_ISREG(nd->inode->i_mode))
  1904. will_truncate = 0;
  1905. if (will_truncate) {
  1906. error = mnt_want_write(nd->path.mnt);
  1907. if (error)
  1908. goto exit;
  1909. want_write = 1;
  1910. }
  1911. common:
  1912. error = may_open(&nd->path, acc_mode, open_flag);
  1913. if (error)
  1914. goto exit;
  1915. filp = nameidata_to_filp(nd);
  1916. if (!IS_ERR(filp)) {
  1917. error = ima_file_check(filp, op->acc_mode);
  1918. if (error) {
  1919. fput(filp);
  1920. filp = ERR_PTR(error);
  1921. }
  1922. }
  1923. if (!IS_ERR(filp)) {
  1924. if (will_truncate) {
  1925. error = handle_truncate(filp);
  1926. if (error) {
  1927. fput(filp);
  1928. filp = ERR_PTR(error);
  1929. }
  1930. }
  1931. }
  1932. out:
  1933. if (want_write)
  1934. mnt_drop_write(nd->path.mnt);
  1935. path_put(&nd->path);
  1936. return filp;
  1937. exit_mutex_unlock:
  1938. mutex_unlock(&dir->d_inode->i_mutex);
  1939. exit_dput:
  1940. path_put_conditional(path, nd);
  1941. exit:
  1942. filp = ERR_PTR(error);
  1943. goto out;
  1944. }
  1945. static struct file *path_openat(int dfd, const char *pathname,
  1946. struct nameidata *nd, const struct open_flags *op, int flags)
  1947. {
  1948. struct file *base = NULL;
  1949. struct file *filp;
  1950. struct path path;
  1951. int error;
  1952. filp = get_empty_filp();
  1953. if (!filp)
  1954. return ERR_PTR(-ENFILE);
  1955. filp->f_flags = op->open_flag;
  1956. nd->intent.open.file = filp;
  1957. nd->intent.open.flags = open_to_namei_flags(op->open_flag);
  1958. nd->intent.open.create_mode = op->mode;
  1959. error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
  1960. if (unlikely(error))
  1961. goto out_filp;
  1962. current->total_link_count = 0;
  1963. error = link_path_walk(pathname, nd);
  1964. if (unlikely(error))
  1965. goto out_filp;
  1966. filp = do_last(nd, &path, op, pathname);
  1967. while (unlikely(!filp)) { /* trailing symlink */
  1968. struct path link = path;
  1969. void *cookie;
  1970. if (!(nd->flags & LOOKUP_FOLLOW)) {
  1971. path_put_conditional(&path, nd);
  1972. path_put(&nd->path);
  1973. filp = ERR_PTR(-ELOOP);
  1974. break;
  1975. }
  1976. nd->flags |= LOOKUP_PARENT;
  1977. nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
  1978. error = follow_link(&link, nd, &cookie);
  1979. if (unlikely(error))
  1980. filp = ERR_PTR(error);
  1981. else
  1982. filp = do_last(nd, &path, op, pathname);
  1983. put_link(nd, &link, cookie);
  1984. }
  1985. out:
  1986. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
  1987. path_put(&nd->root);
  1988. if (base)
  1989. fput(base);
  1990. release_open_intent(nd);
  1991. return filp;
  1992. out_filp:
  1993. filp = ERR_PTR(error);
  1994. goto out;
  1995. }
  1996. struct file *do_filp_open(int dfd, const char *pathname,
  1997. const struct open_flags *op, int flags)
  1998. {
  1999. struct nameidata nd;
  2000. struct file *filp;
  2001. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
  2002. if (unlikely(filp == ERR_PTR(-ECHILD)))
  2003. filp = path_openat(dfd, pathname, &nd, op, flags);
  2004. if (unlikely(filp == ERR_PTR(-ESTALE)))
  2005. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
  2006. return filp;
  2007. }
  2008. struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  2009. const char *name, const struct open_flags *op, int flags)
  2010. {
  2011. struct nameidata nd;
  2012. struct file *file;
  2013. nd.root.mnt = mnt;
  2014. nd.root.dentry = dentry;
  2015. flags |= LOOKUP_ROOT;
  2016. if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
  2017. return ERR_PTR(-ELOOP);
  2018. file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
  2019. if (unlikely(file == ERR_PTR(-ECHILD)))
  2020. file = path_openat(-1, name, &nd, op, flags);
  2021. if (unlikely(file == ERR_PTR(-ESTALE)))
  2022. file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
  2023. return file;
  2024. }
  2025. /**
  2026. * lookup_create - lookup a dentry, creating it if it doesn't exist
  2027. * @nd: nameidata info
  2028. * @is_dir: directory flag
  2029. *
  2030. * Simple function to lookup and return a dentry and create it
  2031. * if it doesn't exist. Is SMP-safe.
  2032. *
  2033. * Returns with nd->path.dentry->d_inode->i_mutex locked.
  2034. */
  2035. struct dentry *lookup_create(struct nameidata *nd, int is_dir)
  2036. {
  2037. struct dentry *dentry = ERR_PTR(-EEXIST);
  2038. mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2039. /*
  2040. * Yucky last component or no last component at all?
  2041. * (foo/., foo/.., /////)
  2042. */
  2043. if (nd->last_type != LAST_NORM)
  2044. goto fail;
  2045. nd->flags &= ~LOOKUP_PARENT;
  2046. nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  2047. nd->intent.open.flags = O_EXCL;
  2048. /*
  2049. * Do the final lookup.
  2050. */
  2051. dentry = lookup_hash(nd);
  2052. if (IS_ERR(dentry))
  2053. goto fail;
  2054. if (dentry->d_inode)
  2055. goto eexist;
  2056. /*
  2057. * Special case - lookup gave negative, but... we had foo/bar/
  2058. * From the vfs_mknod() POV we just have a negative dentry -
  2059. * all is fine. Let's be bastards - you had / on the end, you've
  2060. * been asking for (non-existent) directory. -ENOENT for you.
  2061. */
  2062. if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
  2063. dput(dentry);
  2064. dentry = ERR_PTR(-ENOENT);
  2065. }
  2066. return dentry;
  2067. eexist:
  2068. dput(dentry);
  2069. dentry = ERR_PTR(-EEXIST);
  2070. fail:
  2071. return dentry;
  2072. }
  2073. EXPORT_SYMBOL_GPL(lookup_create);
  2074. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  2075. {
  2076. int error = may_create(dir, dentry);
  2077. if (error)
  2078. return error;
  2079. if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
  2080. !ns_capable(inode_userns(dir), CAP_MKNOD))
  2081. return -EPERM;
  2082. if (!dir->i_op->mknod)
  2083. return -EPERM;
  2084. error = devcgroup_inode_mknod(mode, dev);
  2085. if (error)
  2086. return error;
  2087. error = security_inode_mknod(dir, dentry, mode, dev);
  2088. if (error)
  2089. return error;
  2090. error = dir->i_op->mknod(dir, dentry, mode, dev);
  2091. if (!error)
  2092. fsnotify_create(dir, dentry);
  2093. return error;
  2094. }
  2095. static int may_mknod(mode_t mode)
  2096. {
  2097. switch (mode & S_IFMT) {
  2098. case S_IFREG:
  2099. case S_IFCHR:
  2100. case S_IFBLK:
  2101. case S_IFIFO:
  2102. case S_IFSOCK:
  2103. case 0: /* zero mode translates to S_IFREG */
  2104. return 0;
  2105. case S_IFDIR:
  2106. return -EPERM;
  2107. default:
  2108. return -EINVAL;
  2109. }
  2110. }
  2111. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
  2112. unsigned, dev)
  2113. {
  2114. int error;
  2115. char *tmp;
  2116. struct dentry *dentry;
  2117. struct nameidata nd;
  2118. if (S_ISDIR(mode))
  2119. return -EPERM;
  2120. error = user_path_parent(dfd, filename, &nd, &tmp);
  2121. if (error)
  2122. return error;
  2123. dentry = lookup_create(&nd, 0);
  2124. if (IS_ERR(dentry)) {
  2125. error = PTR_ERR(dentry);
  2126. goto out_unlock;
  2127. }
  2128. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  2129. mode &= ~current_umask();
  2130. error = may_mknod(mode);
  2131. if (error)
  2132. goto out_dput;
  2133. error = mnt_want_write(nd.path.mnt);
  2134. if (error)
  2135. goto out_dput;
  2136. error = security_path_mknod(&nd.path, dentry, mode, dev);
  2137. if (error)
  2138. goto out_drop_write;
  2139. switch (mode & S_IFMT) {
  2140. case 0: case S_IFREG:
  2141. error = vfs_create(nd.path.dentry->d_inode,dentry,mode,NULL);
  2142. break;
  2143. case S_IFCHR: case S_IFBLK:
  2144. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
  2145. new_decode_dev(dev));
  2146. break;
  2147. case S_IFIFO: case S_IFSOCK:
  2148. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
  2149. break;
  2150. }
  2151. out_drop_write:
  2152. mnt_drop_write(nd.path.mnt);
  2153. out_dput:
  2154. dput(dentry);
  2155. out_unlock:
  2156. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2157. path_put(&nd.path);
  2158. putname(tmp);
  2159. return error;
  2160. }
  2161. SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
  2162. {
  2163. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  2164. }
  2165. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  2166. {
  2167. int error = may_create(dir, dentry);
  2168. if (error)
  2169. return error;
  2170. if (!dir->i_op->mkdir)
  2171. return -EPERM;
  2172. mode &= (S_IRWXUGO|S_ISVTX);
  2173. error = security_inode_mkdir(dir, dentry, mode);
  2174. if (error)
  2175. return error;
  2176. error = dir->i_op->mkdir(dir, dentry, mode);
  2177. if (!error)
  2178. fsnotify_mkdir(dir, dentry);
  2179. return error;
  2180. }
  2181. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
  2182. {
  2183. int error = 0;
  2184. char * tmp;
  2185. struct dentry *dentry;
  2186. struct nameidata nd;
  2187. error = user_path_parent(dfd, pathname, &nd, &tmp);
  2188. if (error)
  2189. goto out_err;
  2190. dentry = lookup_create(&nd, 1);
  2191. error = PTR_ERR(dentry);
  2192. if (IS_ERR(dentry))
  2193. goto out_unlock;
  2194. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  2195. mode &= ~current_umask();
  2196. error = mnt_want_write(nd.path.mnt);
  2197. if (error)
  2198. goto out_dput;
  2199. error = security_path_mkdir(&nd.path, dentry, mode);
  2200. if (error)
  2201. goto out_drop_write;
  2202. error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
  2203. out_drop_write:
  2204. mnt_drop_write(nd.path.mnt);
  2205. out_dput:
  2206. dput(dentry);
  2207. out_unlock:
  2208. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2209. path_put(&nd.path);
  2210. putname(tmp);
  2211. out_err:
  2212. return error;
  2213. }
  2214. SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
  2215. {
  2216. return sys_mkdirat(AT_FDCWD, pathname, mode);
  2217. }
  2218. /*
  2219. * The dentry_unhash() helper will try to drop the dentry early: we
  2220. * should have a usage count of 2 if we're the only user of this
  2221. * dentry, and if that is true (possibly after pruning the dcache),
  2222. * then we drop the dentry now.
  2223. *
  2224. * A low-level filesystem can, if it choses, legally
  2225. * do a
  2226. *
  2227. * if (!d_unhashed(dentry))
  2228. * return -EBUSY;
  2229. *
  2230. * if it cannot handle the case of removing a directory
  2231. * that is still in use by something else..
  2232. */
  2233. void dentry_unhash(struct dentry *dentry)
  2234. {
  2235. shrink_dcache_parent(dentry);
  2236. spin_lock(&dentry->d_lock);
  2237. if (dentry->d_count == 1)
  2238. __d_drop(dentry);
  2239. spin_unlock(&dentry->d_lock);
  2240. }
  2241. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  2242. {
  2243. int error = may_delete(dir, dentry, 1);
  2244. if (error)
  2245. return error;
  2246. if (!dir->i_op->rmdir)
  2247. return -EPERM;
  2248. mutex_lock(&dentry->d_inode->i_mutex);
  2249. error = -EBUSY;
  2250. if (d_mountpoint(dentry))
  2251. goto out;
  2252. error = security_inode_rmdir(dir, dentry);
  2253. if (error)
  2254. goto out;
  2255. shrink_dcache_parent(dentry);
  2256. error = dir->i_op->rmdir(dir, dentry);
  2257. if (error)
  2258. goto out;
  2259. dentry->d_inode->i_flags |= S_DEAD;
  2260. dont_mount(dentry);
  2261. out:
  2262. mutex_unlock(&dentry->d_inode->i_mutex);
  2263. if (!error)
  2264. d_delete(dentry);
  2265. return error;
  2266. }
  2267. static long do_rmdir(int dfd, const char __user *pathname)
  2268. {
  2269. int error = 0;
  2270. char * name;
  2271. struct dentry *dentry;
  2272. struct nameidata nd;
  2273. error = user_path_parent(dfd, pathname, &nd, &name);
  2274. if (error)
  2275. return error;
  2276. switch(nd.last_type) {
  2277. case LAST_DOTDOT:
  2278. error = -ENOTEMPTY;
  2279. goto exit1;
  2280. case LAST_DOT:
  2281. error = -EINVAL;
  2282. goto exit1;
  2283. case LAST_ROOT:
  2284. error = -EBUSY;
  2285. goto exit1;
  2286. }
  2287. nd.flags &= ~LOOKUP_PARENT;
  2288. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2289. dentry = lookup_hash(&nd);
  2290. error = PTR_ERR(dentry);
  2291. if (IS_ERR(dentry))
  2292. goto exit2;
  2293. if (!dentry->d_inode) {
  2294. error = -ENOENT;
  2295. goto exit3;
  2296. }
  2297. error = mnt_want_write(nd.path.mnt);
  2298. if (error)
  2299. goto exit3;
  2300. error = security_path_rmdir(&nd.path, dentry);
  2301. if (error)
  2302. goto exit4;
  2303. error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
  2304. exit4:
  2305. mnt_drop_write(nd.path.mnt);
  2306. exit3:
  2307. dput(dentry);
  2308. exit2:
  2309. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2310. exit1:
  2311. path_put(&nd.path);
  2312. putname(name);
  2313. return error;
  2314. }
  2315. SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
  2316. {
  2317. return do_rmdir(AT_FDCWD, pathname);
  2318. }
  2319. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  2320. {
  2321. int error = may_delete(dir, dentry, 0);
  2322. if (error)
  2323. return error;
  2324. if (!dir->i_op->unlink)
  2325. return -EPERM;
  2326. mutex_lock(&dentry->d_inode->i_mutex);
  2327. if (d_mountpoint(dentry))
  2328. error = -EBUSY;
  2329. else {
  2330. error = security_inode_unlink(dir, dentry);
  2331. if (!error) {
  2332. error = dir->i_op->unlink(dir, dentry);
  2333. if (!error)
  2334. dont_mount(dentry);
  2335. }
  2336. }
  2337. mutex_unlock(&dentry->d_inode->i_mutex);
  2338. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  2339. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  2340. fsnotify_link_count(dentry->d_inode);
  2341. d_delete(dentry);
  2342. }
  2343. return error;
  2344. }
  2345. /*
  2346. * Make sure that the actual truncation of the file will occur outside its
  2347. * directory's i_mutex. Truncate can take a long time if there is a lot of
  2348. * writeout happening, and we don't want to prevent access to the directory
  2349. * while waiting on the I/O.
  2350. */
  2351. static long do_unlinkat(int dfd, const char __user *pathname)
  2352. {
  2353. int error;
  2354. char *name;
  2355. struct dentry *dentry;
  2356. struct nameidata nd;
  2357. struct inode *inode = NULL;
  2358. error = user_path_parent(dfd, pathname, &nd, &name);
  2359. if (error)
  2360. return error;
  2361. error = -EISDIR;
  2362. if (nd.last_type != LAST_NORM)
  2363. goto exit1;
  2364. nd.flags &= ~LOOKUP_PARENT;
  2365. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2366. dentry = lookup_hash(&nd);
  2367. error = PTR_ERR(dentry);
  2368. if (!IS_ERR(dentry)) {
  2369. /* Why not before? Because we want correct error value */
  2370. if (nd.last.name[nd.last.len])
  2371. goto slashes;
  2372. inode = dentry->d_inode;
  2373. if (!inode)
  2374. goto slashes;
  2375. ihold(inode);
  2376. error = mnt_want_write(nd.path.mnt);
  2377. if (error)
  2378. goto exit2;
  2379. error = security_path_unlink(&nd.path, dentry);
  2380. if (error)
  2381. goto exit3;
  2382. error = vfs_unlink(nd.path.dentry->d_inode, dentry);
  2383. exit3:
  2384. mnt_drop_write(nd.path.mnt);
  2385. exit2:
  2386. dput(dentry);
  2387. }
  2388. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2389. if (inode)
  2390. iput(inode); /* truncate the inode here */
  2391. exit1:
  2392. path_put(&nd.path);
  2393. putname(name);
  2394. return error;
  2395. slashes:
  2396. error = !dentry->d_inode ? -ENOENT :
  2397. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  2398. goto exit2;
  2399. }
  2400. SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
  2401. {
  2402. if ((flag & ~AT_REMOVEDIR) != 0)
  2403. return -EINVAL;
  2404. if (flag & AT_REMOVEDIR)
  2405. return do_rmdir(dfd, pathname);
  2406. return do_unlinkat(dfd, pathname);
  2407. }
  2408. SYSCALL_DEFINE1(unlink, const char __user *, pathname)
  2409. {
  2410. return do_unlinkat(AT_FDCWD, pathname);
  2411. }
  2412. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  2413. {
  2414. int error = may_create(dir, dentry);
  2415. if (error)
  2416. return error;
  2417. if (!dir->i_op->symlink)
  2418. return -EPERM;
  2419. error = security_inode_symlink(dir, dentry, oldname);
  2420. if (error)
  2421. return error;
  2422. error = dir->i_op->symlink(dir, dentry, oldname);
  2423. if (!error)
  2424. fsnotify_create(dir, dentry);
  2425. return error;
  2426. }
  2427. SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  2428. int, newdfd, const char __user *, newname)
  2429. {
  2430. int error;
  2431. char *from;
  2432. char *to;
  2433. struct dentry *dentry;
  2434. struct nameidata nd;
  2435. from = getname(oldname);
  2436. if (IS_ERR(from))
  2437. return PTR_ERR(from);
  2438. error = user_path_parent(newdfd, newname, &nd, &to);
  2439. if (error)
  2440. goto out_putname;
  2441. dentry = lookup_create(&nd, 0);
  2442. error = PTR_ERR(dentry);
  2443. if (IS_ERR(dentry))
  2444. goto out_unlock;
  2445. error = mnt_want_write(nd.path.mnt);
  2446. if (error)
  2447. goto out_dput;
  2448. error = security_path_symlink(&nd.path, dentry, from);
  2449. if (error)
  2450. goto out_drop_write;
  2451. error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
  2452. out_drop_write:
  2453. mnt_drop_write(nd.path.mnt);
  2454. out_dput:
  2455. dput(dentry);
  2456. out_unlock:
  2457. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2458. path_put(&nd.path);
  2459. putname(to);
  2460. out_putname:
  2461. putname(from);
  2462. return error;
  2463. }
  2464. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  2465. {
  2466. return sys_symlinkat(oldname, AT_FDCWD, newname);
  2467. }
  2468. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2469. {
  2470. struct inode *inode = old_dentry->d_inode;
  2471. int error;
  2472. if (!inode)
  2473. return -ENOENT;
  2474. error = may_create(dir, new_dentry);
  2475. if (error)
  2476. return error;
  2477. if (dir->i_sb != inode->i_sb)
  2478. return -EXDEV;
  2479. /*
  2480. * A link to an append-only or immutable file cannot be created.
  2481. */
  2482. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2483. return -EPERM;
  2484. if (!dir->i_op->link)
  2485. return -EPERM;
  2486. if (S_ISDIR(inode->i_mode))
  2487. return -EPERM;
  2488. error = security_inode_link(old_dentry, dir, new_dentry);
  2489. if (error)
  2490. return error;
  2491. mutex_lock(&inode->i_mutex);
  2492. /* Make sure we don't allow creating hardlink to an unlinked file */
  2493. if (inode->i_nlink == 0)
  2494. error = -ENOENT;
  2495. else
  2496. error = dir->i_op->link(old_dentry, dir, new_dentry);
  2497. mutex_unlock(&inode->i_mutex);
  2498. if (!error)
  2499. fsnotify_link(dir, inode, new_dentry);
  2500. return error;
  2501. }
  2502. /*
  2503. * Hardlinks are often used in delicate situations. We avoid
  2504. * security-related surprises by not following symlinks on the
  2505. * newname. --KAB
  2506. *
  2507. * We don't follow them on the oldname either to be compatible
  2508. * with linux 2.0, and to avoid hard-linking to directories
  2509. * and other special files. --ADM
  2510. */
  2511. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  2512. int, newdfd, const char __user *, newname, int, flags)
  2513. {
  2514. struct dentry *new_dentry;
  2515. struct nameidata nd;
  2516. struct path old_path;
  2517. int how = 0;
  2518. int error;
  2519. char *to;
  2520. if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  2521. return -EINVAL;
  2522. /*
  2523. * To use null names we require CAP_DAC_READ_SEARCH
  2524. * This ensures that not everyone will be able to create
  2525. * handlink using the passed filedescriptor.
  2526. */
  2527. if (flags & AT_EMPTY_PATH) {
  2528. if (!capable(CAP_DAC_READ_SEARCH))
  2529. return -ENOENT;
  2530. how = LOOKUP_EMPTY;
  2531. }
  2532. if (flags & AT_SYMLINK_FOLLOW)
  2533. how |= LOOKUP_FOLLOW;
  2534. error = user_path_at(olddfd, oldname, how, &old_path);
  2535. if (error)
  2536. return error;
  2537. error = user_path_parent(newdfd, newname, &nd, &to);
  2538. if (error)
  2539. goto out;
  2540. error = -EXDEV;
  2541. if (old_path.mnt != nd.path.mnt)
  2542. goto out_release;
  2543. new_dentry = lookup_create(&nd, 0);
  2544. error = PTR_ERR(new_dentry);
  2545. if (IS_ERR(new_dentry))
  2546. goto out_unlock;
  2547. error = mnt_want_write(nd.path.mnt);
  2548. if (error)
  2549. goto out_dput;
  2550. error = security_path_link(old_path.dentry, &nd.path, new_dentry);
  2551. if (error)
  2552. goto out_drop_write;
  2553. error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
  2554. out_drop_write:
  2555. mnt_drop_write(nd.path.mnt);
  2556. out_dput:
  2557. dput(new_dentry);
  2558. out_unlock:
  2559. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2560. out_release:
  2561. path_put(&nd.path);
  2562. putname(to);
  2563. out:
  2564. path_put(&old_path);
  2565. return error;
  2566. }
  2567. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  2568. {
  2569. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  2570. }
  2571. /*
  2572. * The worst of all namespace operations - renaming directory. "Perverted"
  2573. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  2574. * Problems:
  2575. * a) we can get into loop creation. Check is done in is_subdir().
  2576. * b) race potential - two innocent renames can create a loop together.
  2577. * That's where 4.4 screws up. Current fix: serialization on
  2578. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  2579. * story.
  2580. * c) we have to lock _three_ objects - parents and victim (if it exists).
  2581. * And that - after we got ->i_mutex on parents (until then we don't know
  2582. * whether the target exists). Solution: try to be smart with locking
  2583. * order for inodes. We rely on the fact that tree topology may change
  2584. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  2585. * move will be locked. Thus we can rank directories by the tree
  2586. * (ancestors first) and rank all non-directories after them.
  2587. * That works since everybody except rename does "lock parent, lookup,
  2588. * lock child" and rename is under ->s_vfs_rename_mutex.
  2589. * HOWEVER, it relies on the assumption that any object with ->lookup()
  2590. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  2591. * we'd better make sure that there's no link(2) for them.
  2592. * d) conversion from fhandle to dentry may come in the wrong moment - when
  2593. * we are removing the target. Solution: we will have to grab ->i_mutex
  2594. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  2595. * ->i_mutex on parents, which works but leads to some truly excessive
  2596. * locking].
  2597. */
  2598. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  2599. struct inode *new_dir, struct dentry *new_dentry)
  2600. {
  2601. int error = 0;
  2602. struct inode *target = new_dentry->d_inode;
  2603. /*
  2604. * If we are going to change the parent - check write permissions,
  2605. * we'll need to flip '..'.
  2606. */
  2607. if (new_dir != old_dir) {
  2608. error = inode_permission(old_dentry->d_inode, MAY_WRITE);
  2609. if (error)
  2610. return error;
  2611. }
  2612. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2613. if (error)
  2614. return error;
  2615. if (target)
  2616. mutex_lock(&target->i_mutex);
  2617. error = -EBUSY;
  2618. if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
  2619. goto out;
  2620. if (target)
  2621. shrink_dcache_parent(new_dentry);
  2622. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2623. if (error)
  2624. goto out;
  2625. if (target) {
  2626. target->i_flags |= S_DEAD;
  2627. dont_mount(new_dentry);
  2628. }
  2629. out:
  2630. if (target)
  2631. mutex_unlock(&target->i_mutex);
  2632. if (!error)
  2633. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2634. d_move(old_dentry,new_dentry);
  2635. return error;
  2636. }
  2637. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2638. struct inode *new_dir, struct dentry *new_dentry)
  2639. {
  2640. struct inode *target = new_dentry->d_inode;
  2641. int error;
  2642. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2643. if (error)
  2644. return error;
  2645. dget(new_dentry);
  2646. if (target)
  2647. mutex_lock(&target->i_mutex);
  2648. error = -EBUSY;
  2649. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2650. goto out;
  2651. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2652. if (error)
  2653. goto out;
  2654. if (target)
  2655. dont_mount(new_dentry);
  2656. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2657. d_move(old_dentry, new_dentry);
  2658. out:
  2659. if (target)
  2660. mutex_unlock(&target->i_mutex);
  2661. dput(new_dentry);
  2662. return error;
  2663. }
  2664. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2665. struct inode *new_dir, struct dentry *new_dentry)
  2666. {
  2667. int error;
  2668. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2669. const unsigned char *old_name;
  2670. if (old_dentry->d_inode == new_dentry->d_inode)
  2671. return 0;
  2672. error = may_delete(old_dir, old_dentry, is_dir);
  2673. if (error)
  2674. return error;
  2675. if (!new_dentry->d_inode)
  2676. error = may_create(new_dir, new_dentry);
  2677. else
  2678. error = may_delete(new_dir, new_dentry, is_dir);
  2679. if (error)
  2680. return error;
  2681. if (!old_dir->i_op->rename)
  2682. return -EPERM;
  2683. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2684. if (is_dir)
  2685. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2686. else
  2687. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2688. if (!error)
  2689. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  2690. new_dentry->d_inode, old_dentry);
  2691. fsnotify_oldname_free(old_name);
  2692. return error;
  2693. }
  2694. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  2695. int, newdfd, const char __user *, newname)
  2696. {
  2697. struct dentry *old_dir, *new_dir;
  2698. struct dentry *old_dentry, *new_dentry;
  2699. struct dentry *trap;
  2700. struct nameidata oldnd, newnd;
  2701. char *from;
  2702. char *to;
  2703. int error;
  2704. error = user_path_parent(olddfd, oldname, &oldnd, &from);
  2705. if (error)
  2706. goto exit;
  2707. error = user_path_parent(newdfd, newname, &newnd, &to);
  2708. if (error)
  2709. goto exit1;
  2710. error = -EXDEV;
  2711. if (oldnd.path.mnt != newnd.path.mnt)
  2712. goto exit2;
  2713. old_dir = oldnd.path.dentry;
  2714. error = -EBUSY;
  2715. if (oldnd.last_type != LAST_NORM)
  2716. goto exit2;
  2717. new_dir = newnd.path.dentry;
  2718. if (newnd.last_type != LAST_NORM)
  2719. goto exit2;
  2720. oldnd.flags &= ~LOOKUP_PARENT;
  2721. newnd.flags &= ~LOOKUP_PARENT;
  2722. newnd.flags |= LOOKUP_RENAME_TARGET;
  2723. trap = lock_rename(new_dir, old_dir);
  2724. old_dentry = lookup_hash(&oldnd);
  2725. error = PTR_ERR(old_dentry);
  2726. if (IS_ERR(old_dentry))
  2727. goto exit3;
  2728. /* source must exist */
  2729. error = -ENOENT;
  2730. if (!old_dentry->d_inode)
  2731. goto exit4;
  2732. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2733. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2734. error = -ENOTDIR;
  2735. if (oldnd.last.name[oldnd.last.len])
  2736. goto exit4;
  2737. if (newnd.last.name[newnd.last.len])
  2738. goto exit4;
  2739. }
  2740. /* source should not be ancestor of target */
  2741. error = -EINVAL;
  2742. if (old_dentry == trap)
  2743. goto exit4;
  2744. new_dentry = lookup_hash(&newnd);
  2745. error = PTR_ERR(new_dentry);
  2746. if (IS_ERR(new_dentry))
  2747. goto exit4;
  2748. /* target should not be an ancestor of source */
  2749. error = -ENOTEMPTY;
  2750. if (new_dentry == trap)
  2751. goto exit5;
  2752. error = mnt_want_write(oldnd.path.mnt);
  2753. if (error)
  2754. goto exit5;
  2755. error = security_path_rename(&oldnd.path, old_dentry,
  2756. &newnd.path, new_dentry);
  2757. if (error)
  2758. goto exit6;
  2759. error = vfs_rename(old_dir->d_inode, old_dentry,
  2760. new_dir->d_inode, new_dentry);
  2761. exit6:
  2762. mnt_drop_write(oldnd.path.mnt);
  2763. exit5:
  2764. dput(new_dentry);
  2765. exit4:
  2766. dput(old_dentry);
  2767. exit3:
  2768. unlock_rename(new_dir, old_dir);
  2769. exit2:
  2770. path_put(&newnd.path);
  2771. putname(to);
  2772. exit1:
  2773. path_put(&oldnd.path);
  2774. putname(from);
  2775. exit:
  2776. return error;
  2777. }
  2778. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  2779. {
  2780. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  2781. }
  2782. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2783. {
  2784. int len;
  2785. len = PTR_ERR(link);
  2786. if (IS_ERR(link))
  2787. goto out;
  2788. len = strlen(link);
  2789. if (len > (unsigned) buflen)
  2790. len = buflen;
  2791. if (copy_to_user(buffer, link, len))
  2792. len = -EFAULT;
  2793. out:
  2794. return len;
  2795. }
  2796. /*
  2797. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2798. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2799. * using) it for any given inode is up to filesystem.
  2800. */
  2801. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2802. {
  2803. struct nameidata nd;
  2804. void *cookie;
  2805. int res;
  2806. nd.depth = 0;
  2807. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2808. if (IS_ERR(cookie))
  2809. return PTR_ERR(cookie);
  2810. res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2811. if (dentry->d_inode->i_op->put_link)
  2812. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  2813. return res;
  2814. }
  2815. int vfs_follow_link(struct nameidata *nd, const char *link)
  2816. {
  2817. return __vfs_follow_link(nd, link);
  2818. }
  2819. /* get the link contents into pagecache */
  2820. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2821. {
  2822. char *kaddr;
  2823. struct page *page;
  2824. struct address_space *mapping = dentry->d_inode->i_mapping;
  2825. page = read_mapping_page(mapping, 0, NULL);
  2826. if (IS_ERR(page))
  2827. return (char*)page;
  2828. *ppage = page;
  2829. kaddr = kmap(page);
  2830. nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
  2831. return kaddr;
  2832. }
  2833. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2834. {
  2835. struct page *page = NULL;
  2836. char *s = page_getlink(dentry, &page);
  2837. int res = vfs_readlink(dentry,buffer,buflen,s);
  2838. if (page) {
  2839. kunmap(page);
  2840. page_cache_release(page);
  2841. }
  2842. return res;
  2843. }
  2844. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2845. {
  2846. struct page *page = NULL;
  2847. nd_set_link(nd, page_getlink(dentry, &page));
  2848. return page;
  2849. }
  2850. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2851. {
  2852. struct page *page = cookie;
  2853. if (page) {
  2854. kunmap(page);
  2855. page_cache_release(page);
  2856. }
  2857. }
  2858. /*
  2859. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  2860. */
  2861. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  2862. {
  2863. struct address_space *mapping = inode->i_mapping;
  2864. struct page *page;
  2865. void *fsdata;
  2866. int err;
  2867. char *kaddr;
  2868. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  2869. if (nofs)
  2870. flags |= AOP_FLAG_NOFS;
  2871. retry:
  2872. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  2873. flags, &page, &fsdata);
  2874. if (err)
  2875. goto fail;
  2876. kaddr = kmap_atomic(page, KM_USER0);
  2877. memcpy(kaddr, symname, len-1);
  2878. kunmap_atomic(kaddr, KM_USER0);
  2879. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  2880. page, fsdata);
  2881. if (err < 0)
  2882. goto fail;
  2883. if (err < len-1)
  2884. goto retry;
  2885. mark_inode_dirty(inode);
  2886. return 0;
  2887. fail:
  2888. return err;
  2889. }
  2890. int page_symlink(struct inode *inode, const char *symname, int len)
  2891. {
  2892. return __page_symlink(inode, symname, len,
  2893. !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
  2894. }
  2895. const struct inode_operations page_symlink_inode_operations = {
  2896. .readlink = generic_readlink,
  2897. .follow_link = page_follow_link_light,
  2898. .put_link = page_put_link,
  2899. };
  2900. EXPORT_SYMBOL(user_path_at);
  2901. EXPORT_SYMBOL(follow_down_one);
  2902. EXPORT_SYMBOL(follow_down);
  2903. EXPORT_SYMBOL(follow_up);
  2904. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  2905. EXPORT_SYMBOL(getname);
  2906. EXPORT_SYMBOL(lock_rename);
  2907. EXPORT_SYMBOL(lookup_one_len);
  2908. EXPORT_SYMBOL(page_follow_link_light);
  2909. EXPORT_SYMBOL(page_put_link);
  2910. EXPORT_SYMBOL(page_readlink);
  2911. EXPORT_SYMBOL(__page_symlink);
  2912. EXPORT_SYMBOL(page_symlink);
  2913. EXPORT_SYMBOL(page_symlink_inode_operations);
  2914. EXPORT_SYMBOL(kern_path_parent);
  2915. EXPORT_SYMBOL(kern_path);
  2916. EXPORT_SYMBOL(vfs_path_lookup);
  2917. EXPORT_SYMBOL(inode_permission);
  2918. EXPORT_SYMBOL(unlock_rename);
  2919. EXPORT_SYMBOL(vfs_create);
  2920. EXPORT_SYMBOL(vfs_follow_link);
  2921. EXPORT_SYMBOL(vfs_link);
  2922. EXPORT_SYMBOL(vfs_mkdir);
  2923. EXPORT_SYMBOL(vfs_mknod);
  2924. EXPORT_SYMBOL(generic_permission);
  2925. EXPORT_SYMBOL(vfs_readlink);
  2926. EXPORT_SYMBOL(vfs_rename);
  2927. EXPORT_SYMBOL(vfs_rmdir);
  2928. EXPORT_SYMBOL(vfs_symlink);
  2929. EXPORT_SYMBOL(vfs_unlink);
  2930. EXPORT_SYMBOL(dentry_unhash);
  2931. EXPORT_SYMBOL(generic_readlink);