namei.c 80 KB

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