namei.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587
  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/quotaops.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/fsnotify.h>
  22. #include <linux/smp_lock.h>
  23. #include <linux/personality.h>
  24. #include <linux/security.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/mount.h>
  27. #include <linux/audit.h>
  28. #include <linux/capability.h>
  29. #include <linux/file.h>
  30. #include <asm/namei.h>
  31. #include <asm/uaccess.h>
  32. #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
  33. /* [Feb-1997 T. Schoebel-Theuer]
  34. * Fundamental changes in the pathname lookup mechanisms (namei)
  35. * were necessary because of omirr. The reason is that omirr needs
  36. * to know the _real_ pathname, not the user-supplied one, in case
  37. * of symlinks (and also when transname replacements occur).
  38. *
  39. * The new code replaces the old recursive symlink resolution with
  40. * an iterative one (in case of non-nested symlink chains). It does
  41. * this with calls to <fs>_follow_link().
  42. * As a side effect, dir_namei(), _namei() and follow_link() are now
  43. * replaced with a single function lookup_dentry() that can handle all
  44. * the special cases of the former code.
  45. *
  46. * With the new dcache, the pathname is stored at each inode, at least as
  47. * long as the refcount of the inode is positive. As a side effect, the
  48. * size of the dcache depends on the inode cache and thus is dynamic.
  49. *
  50. * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
  51. * resolution to correspond with current state of the code.
  52. *
  53. * Note that the symlink resolution is not *completely* iterative.
  54. * There is still a significant amount of tail- and mid- recursion in
  55. * the algorithm. Also, note that <fs>_readlink() is not used in
  56. * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
  57. * may return different results than <fs>_follow_link(). Many virtual
  58. * filesystems (including /proc) exhibit this behavior.
  59. */
  60. /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
  61. * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
  62. * and the name already exists in form of a symlink, try to create the new
  63. * name indicated by the symlink. The old code always complained that the
  64. * name already exists, due to not following the symlink even if its target
  65. * is nonexistent. The new semantics affects also mknod() and link() when
  66. * the name is a symlink pointing to a non-existant name.
  67. *
  68. * I don't know which semantics is the right one, since I have no access
  69. * to standards. But I found by trial that HP-UX 9.0 has the full "new"
  70. * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
  71. * "old" one. Personally, I think the new semantics is much more logical.
  72. * Note that "ln old new" where "new" is a symlink pointing to a non-existing
  73. * file does succeed in both HP-UX and SunOs, but not in Solaris
  74. * and in the old Linux semantics.
  75. */
  76. /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
  77. * semantics. See the comments in "open_namei" and "do_link" below.
  78. *
  79. * [10-Sep-98 Alan Modra] Another symlink change.
  80. */
  81. /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
  82. * inside the path - always follow.
  83. * in the last component in creation/removal/renaming - never follow.
  84. * if LOOKUP_FOLLOW passed - follow.
  85. * if the pathname has trailing slashes - follow.
  86. * otherwise - don't follow.
  87. * (applied in that order).
  88. *
  89. * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
  90. * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
  91. * During the 2.4 we need to fix the userland stuff depending on it -
  92. * hopefully we will be able to get rid of that wart in 2.5. So far only
  93. * XEmacs seems to be relying on it...
  94. */
  95. /*
  96. * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
  97. * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives
  98. * any extra contention...
  99. */
  100. /* In order to reduce some races, while at the same time doing additional
  101. * checking and hopefully speeding things up, we copy filenames to the
  102. * kernel data space before using them..
  103. *
  104. * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
  105. * PATH_MAX includes the nul terminator --RR.
  106. */
  107. static int do_getname(const char __user *filename, char *page)
  108. {
  109. int retval;
  110. unsigned long len = PATH_MAX;
  111. if (!segment_eq(get_fs(), KERNEL_DS)) {
  112. if ((unsigned long) filename >= TASK_SIZE)
  113. return -EFAULT;
  114. if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
  115. len = TASK_SIZE - (unsigned long) filename;
  116. }
  117. retval = strncpy_from_user(page, filename, len);
  118. if (retval > 0) {
  119. if (retval < len)
  120. return 0;
  121. return -ENAMETOOLONG;
  122. } else if (!retval)
  123. retval = -ENOENT;
  124. return retval;
  125. }
  126. char * getname(const char __user * filename)
  127. {
  128. char *tmp, *result;
  129. result = ERR_PTR(-ENOMEM);
  130. tmp = __getname();
  131. if (tmp) {
  132. int retval = do_getname(filename, tmp);
  133. result = tmp;
  134. if (retval < 0) {
  135. __putname(tmp);
  136. result = ERR_PTR(retval);
  137. }
  138. }
  139. audit_getname(result);
  140. return result;
  141. }
  142. #ifdef CONFIG_AUDITSYSCALL
  143. void putname(const char *name)
  144. {
  145. if (unlikely(current->audit_context))
  146. audit_putname(name);
  147. else
  148. __putname(name);
  149. }
  150. EXPORT_SYMBOL(putname);
  151. #endif
  152. /**
  153. * generic_permission - check for access rights on a Posix-like filesystem
  154. * @inode: inode to check access rights for
  155. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  156. * @check_acl: optional callback to check for Posix ACLs
  157. *
  158. * Used to check for read/write/execute permissions on a file.
  159. * We use "fsuid" for this, letting us set arbitrary permissions
  160. * for filesystem access without changing the "normal" uids which
  161. * are used for other things..
  162. */
  163. int generic_permission(struct inode *inode, int mask,
  164. int (*check_acl)(struct inode *inode, int mask))
  165. {
  166. umode_t mode = inode->i_mode;
  167. if (current->fsuid == inode->i_uid)
  168. mode >>= 6;
  169. else {
  170. if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
  171. int error = check_acl(inode, mask);
  172. if (error == -EACCES)
  173. goto check_capabilities;
  174. else if (error != -EAGAIN)
  175. return error;
  176. }
  177. if (in_group_p(inode->i_gid))
  178. mode >>= 3;
  179. }
  180. /*
  181. * If the DACs are ok we don't need any capability check.
  182. */
  183. if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
  184. return 0;
  185. check_capabilities:
  186. /*
  187. * Read/write DACs are always overridable.
  188. * Executable DACs are overridable if at least one exec bit is set.
  189. */
  190. if (!(mask & MAY_EXEC) ||
  191. (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
  192. if (capable(CAP_DAC_OVERRIDE))
  193. return 0;
  194. /*
  195. * Searching includes executable on directories, else just read.
  196. */
  197. if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
  198. if (capable(CAP_DAC_READ_SEARCH))
  199. return 0;
  200. return -EACCES;
  201. }
  202. int permission(struct inode *inode, int mask, struct nameidata *nd)
  203. {
  204. int retval, submask;
  205. if (mask & MAY_WRITE) {
  206. umode_t mode = inode->i_mode;
  207. /*
  208. * Nobody gets write access to a read-only fs.
  209. */
  210. if (IS_RDONLY(inode) &&
  211. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  212. return -EROFS;
  213. /*
  214. * Nobody gets write access to an immutable file.
  215. */
  216. if (IS_IMMUTABLE(inode))
  217. return -EACCES;
  218. }
  219. /* Ordinary permission routines do not understand MAY_APPEND. */
  220. submask = mask & ~MAY_APPEND;
  221. if (inode->i_op && inode->i_op->permission)
  222. retval = inode->i_op->permission(inode, submask, nd);
  223. else
  224. retval = generic_permission(inode, submask, NULL);
  225. if (retval)
  226. return retval;
  227. return security_inode_permission(inode, mask, nd);
  228. }
  229. /**
  230. * vfs_permission - check for access rights to a given path
  231. * @nd: lookup result that describes the path
  232. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  233. *
  234. * Used to check for read/write/execute permissions on a path.
  235. * We use "fsuid" for this, letting us set arbitrary permissions
  236. * for filesystem access without changing the "normal" uids which
  237. * are used for other things.
  238. */
  239. int vfs_permission(struct nameidata *nd, int mask)
  240. {
  241. return permission(nd->dentry->d_inode, mask, nd);
  242. }
  243. /**
  244. * file_permission - check for additional access rights to a given file
  245. * @file: file to check access rights for
  246. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  247. *
  248. * Used to check for read/write/execute permissions on an already opened
  249. * file.
  250. *
  251. * Note:
  252. * Do not use this function in new code. All access checks should
  253. * be done using vfs_permission().
  254. */
  255. int file_permission(struct file *file, int mask)
  256. {
  257. return permission(file->f_dentry->d_inode, mask, NULL);
  258. }
  259. /*
  260. * get_write_access() gets write permission for a file.
  261. * put_write_access() releases this write permission.
  262. * This is used for regular files.
  263. * We cannot support write (and maybe mmap read-write shared) accesses and
  264. * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
  265. * can have the following values:
  266. * 0: no writers, no VM_DENYWRITE mappings
  267. * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
  268. * > 0: (i_writecount) users are writing to the file.
  269. *
  270. * Normally we operate on that counter with atomic_{inc,dec} and it's safe
  271. * except for the cases where we don't hold i_writecount yet. Then we need to
  272. * use {get,deny}_write_access() - these functions check the sign and refuse
  273. * to do the change if sign is wrong. Exclusion between them is provided by
  274. * the inode->i_lock spinlock.
  275. */
  276. int get_write_access(struct inode * inode)
  277. {
  278. spin_lock(&inode->i_lock);
  279. if (atomic_read(&inode->i_writecount) < 0) {
  280. spin_unlock(&inode->i_lock);
  281. return -ETXTBSY;
  282. }
  283. atomic_inc(&inode->i_writecount);
  284. spin_unlock(&inode->i_lock);
  285. return 0;
  286. }
  287. int deny_write_access(struct file * file)
  288. {
  289. struct inode *inode = file->f_dentry->d_inode;
  290. spin_lock(&inode->i_lock);
  291. if (atomic_read(&inode->i_writecount) > 0) {
  292. spin_unlock(&inode->i_lock);
  293. return -ETXTBSY;
  294. }
  295. atomic_dec(&inode->i_writecount);
  296. spin_unlock(&inode->i_lock);
  297. return 0;
  298. }
  299. void path_release(struct nameidata *nd)
  300. {
  301. dput(nd->dentry);
  302. mntput(nd->mnt);
  303. }
  304. /*
  305. * umount() mustn't call path_release()/mntput() as that would clear
  306. * mnt_expiry_mark
  307. */
  308. void path_release_on_umount(struct nameidata *nd)
  309. {
  310. dput(nd->dentry);
  311. mntput_no_expire(nd->mnt);
  312. }
  313. /**
  314. * release_open_intent - free up open intent resources
  315. * @nd: pointer to nameidata
  316. */
  317. void release_open_intent(struct nameidata *nd)
  318. {
  319. if (nd->intent.open.file->f_dentry == NULL)
  320. put_filp(nd->intent.open.file);
  321. else
  322. fput(nd->intent.open.file);
  323. }
  324. /*
  325. * Internal lookup() using the new generic dcache.
  326. * SMP-safe
  327. */
  328. static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
  329. {
  330. struct dentry * dentry = __d_lookup(parent, name);
  331. /* lockess __d_lookup may fail due to concurrent d_move()
  332. * in some unrelated directory, so try with d_lookup
  333. */
  334. if (!dentry)
  335. dentry = d_lookup(parent, name);
  336. if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
  337. if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
  338. dput(dentry);
  339. dentry = NULL;
  340. }
  341. }
  342. return dentry;
  343. }
  344. /*
  345. * Short-cut version of permission(), for calling by
  346. * path_walk(), when dcache lock is held. Combines parts
  347. * of permission() and generic_permission(), and tests ONLY for
  348. * MAY_EXEC permission.
  349. *
  350. * If appropriate, check DAC only. If not appropriate, or
  351. * short-cut DAC fails, then call permission() to do more
  352. * complete permission check.
  353. */
  354. static int exec_permission_lite(struct inode *inode,
  355. struct nameidata *nd)
  356. {
  357. umode_t mode = inode->i_mode;
  358. if (inode->i_op && inode->i_op->permission)
  359. return -EAGAIN;
  360. if (current->fsuid == inode->i_uid)
  361. mode >>= 6;
  362. else if (in_group_p(inode->i_gid))
  363. mode >>= 3;
  364. if (mode & MAY_EXEC)
  365. goto ok;
  366. if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
  367. goto ok;
  368. if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
  369. goto ok;
  370. if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
  371. goto ok;
  372. return -EACCES;
  373. ok:
  374. return security_inode_permission(inode, MAY_EXEC, nd);
  375. }
  376. /*
  377. * This is called when everything else fails, and we actually have
  378. * to go to the low-level filesystem to find out what we should do..
  379. *
  380. * We get the directory semaphore, and after getting that we also
  381. * make sure that nobody added the entry to the dcache in the meantime..
  382. * SMP-safe
  383. */
  384. static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
  385. {
  386. struct dentry * result;
  387. struct inode *dir = parent->d_inode;
  388. mutex_lock(&dir->i_mutex);
  389. /*
  390. * First re-do the cached lookup just in case it was created
  391. * while we waited for the directory semaphore..
  392. *
  393. * FIXME! This could use version numbering or similar to
  394. * avoid unnecessary cache lookups.
  395. *
  396. * The "dcache_lock" is purely to protect the RCU list walker
  397. * from concurrent renames at this point (we mustn't get false
  398. * negatives from the RCU list walk here, unlike the optimistic
  399. * fast walk).
  400. *
  401. * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
  402. */
  403. result = d_lookup(parent, name);
  404. if (!result) {
  405. struct dentry * dentry = d_alloc(parent, name);
  406. result = ERR_PTR(-ENOMEM);
  407. if (dentry) {
  408. result = dir->i_op->lookup(dir, dentry, nd);
  409. if (result)
  410. dput(dentry);
  411. else
  412. result = dentry;
  413. }
  414. mutex_unlock(&dir->i_mutex);
  415. return result;
  416. }
  417. /*
  418. * Uhhuh! Nasty case: the cache was re-populated while
  419. * we waited on the semaphore. Need to revalidate.
  420. */
  421. mutex_unlock(&dir->i_mutex);
  422. if (result->d_op && result->d_op->d_revalidate) {
  423. if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
  424. dput(result);
  425. result = ERR_PTR(-ENOENT);
  426. }
  427. }
  428. return result;
  429. }
  430. static int __emul_lookup_dentry(const char *, struct nameidata *);
  431. /* SMP-safe */
  432. static __always_inline int
  433. walk_init_root(const char *name, struct nameidata *nd)
  434. {
  435. read_lock(&current->fs->lock);
  436. if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
  437. nd->mnt = mntget(current->fs->altrootmnt);
  438. nd->dentry = dget(current->fs->altroot);
  439. read_unlock(&current->fs->lock);
  440. if (__emul_lookup_dentry(name,nd))
  441. return 0;
  442. read_lock(&current->fs->lock);
  443. }
  444. nd->mnt = mntget(current->fs->rootmnt);
  445. nd->dentry = dget(current->fs->root);
  446. read_unlock(&current->fs->lock);
  447. return 1;
  448. }
  449. static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
  450. {
  451. int res = 0;
  452. char *name;
  453. if (IS_ERR(link))
  454. goto fail;
  455. if (*link == '/') {
  456. path_release(nd);
  457. if (!walk_init_root(link, nd))
  458. /* weird __emul_prefix() stuff did it */
  459. goto out;
  460. }
  461. res = link_path_walk(link, nd);
  462. out:
  463. if (nd->depth || res || nd->last_type!=LAST_NORM)
  464. return res;
  465. /*
  466. * If it is an iterative symlinks resolution in open_namei() we
  467. * have to copy the last component. And all that crap because of
  468. * bloody create() on broken symlinks. Furrfu...
  469. */
  470. name = __getname();
  471. if (unlikely(!name)) {
  472. path_release(nd);
  473. return -ENOMEM;
  474. }
  475. strcpy(name, nd->last.name);
  476. nd->last.name = name;
  477. return 0;
  478. fail:
  479. path_release(nd);
  480. return PTR_ERR(link);
  481. }
  482. struct path {
  483. struct vfsmount *mnt;
  484. struct dentry *dentry;
  485. };
  486. static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
  487. {
  488. int error;
  489. void *cookie;
  490. struct dentry *dentry = path->dentry;
  491. touch_atime(path->mnt, dentry);
  492. nd_set_link(nd, NULL);
  493. if (path->mnt == nd->mnt)
  494. mntget(path->mnt);
  495. cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
  496. error = PTR_ERR(cookie);
  497. if (!IS_ERR(cookie)) {
  498. char *s = nd_get_link(nd);
  499. error = 0;
  500. if (s)
  501. error = __vfs_follow_link(nd, s);
  502. if (dentry->d_inode->i_op->put_link)
  503. dentry->d_inode->i_op->put_link(dentry, nd, cookie);
  504. }
  505. dput(dentry);
  506. mntput(path->mnt);
  507. return error;
  508. }
  509. static inline void dput_path(struct path *path, struct nameidata *nd)
  510. {
  511. dput(path->dentry);
  512. if (path->mnt != nd->mnt)
  513. mntput(path->mnt);
  514. }
  515. static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
  516. {
  517. dput(nd->dentry);
  518. if (nd->mnt != path->mnt)
  519. mntput(nd->mnt);
  520. nd->mnt = path->mnt;
  521. nd->dentry = path->dentry;
  522. }
  523. /*
  524. * This limits recursive symlink follows to 8, while
  525. * limiting consecutive symlinks to 40.
  526. *
  527. * Without that kind of total limit, nasty chains of consecutive
  528. * symlinks can cause almost arbitrarily long lookups.
  529. */
  530. static inline int do_follow_link(struct path *path, struct nameidata *nd)
  531. {
  532. int err = -ELOOP;
  533. if (current->link_count >= MAX_NESTED_LINKS)
  534. goto loop;
  535. if (current->total_link_count >= 40)
  536. goto loop;
  537. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  538. cond_resched();
  539. err = security_inode_follow_link(path->dentry, nd);
  540. if (err)
  541. goto loop;
  542. current->link_count++;
  543. current->total_link_count++;
  544. nd->depth++;
  545. err = __do_follow_link(path, nd);
  546. current->link_count--;
  547. nd->depth--;
  548. return err;
  549. loop:
  550. dput_path(path, nd);
  551. path_release(nd);
  552. return err;
  553. }
  554. int follow_up(struct vfsmount **mnt, struct dentry **dentry)
  555. {
  556. struct vfsmount *parent;
  557. struct dentry *mountpoint;
  558. spin_lock(&vfsmount_lock);
  559. parent=(*mnt)->mnt_parent;
  560. if (parent == *mnt) {
  561. spin_unlock(&vfsmount_lock);
  562. return 0;
  563. }
  564. mntget(parent);
  565. mountpoint=dget((*mnt)->mnt_mountpoint);
  566. spin_unlock(&vfsmount_lock);
  567. dput(*dentry);
  568. *dentry = mountpoint;
  569. mntput(*mnt);
  570. *mnt = parent;
  571. return 1;
  572. }
  573. /* no need for dcache_lock, as serialization is taken care in
  574. * namespace.c
  575. */
  576. static int __follow_mount(struct path *path)
  577. {
  578. int res = 0;
  579. while (d_mountpoint(path->dentry)) {
  580. struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
  581. if (!mounted)
  582. break;
  583. dput(path->dentry);
  584. if (res)
  585. mntput(path->mnt);
  586. path->mnt = mounted;
  587. path->dentry = dget(mounted->mnt_root);
  588. res = 1;
  589. }
  590. return res;
  591. }
  592. static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
  593. {
  594. while (d_mountpoint(*dentry)) {
  595. struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
  596. if (!mounted)
  597. break;
  598. dput(*dentry);
  599. mntput(*mnt);
  600. *mnt = mounted;
  601. *dentry = dget(mounted->mnt_root);
  602. }
  603. }
  604. /* no need for dcache_lock, as serialization is taken care in
  605. * namespace.c
  606. */
  607. int follow_down(struct vfsmount **mnt, struct dentry **dentry)
  608. {
  609. struct vfsmount *mounted;
  610. mounted = lookup_mnt(*mnt, *dentry);
  611. if (mounted) {
  612. dput(*dentry);
  613. mntput(*mnt);
  614. *mnt = mounted;
  615. *dentry = dget(mounted->mnt_root);
  616. return 1;
  617. }
  618. return 0;
  619. }
  620. static __always_inline void follow_dotdot(struct nameidata *nd)
  621. {
  622. while(1) {
  623. struct vfsmount *parent;
  624. struct dentry *old = nd->dentry;
  625. read_lock(&current->fs->lock);
  626. if (nd->dentry == current->fs->root &&
  627. nd->mnt == current->fs->rootmnt) {
  628. read_unlock(&current->fs->lock);
  629. break;
  630. }
  631. read_unlock(&current->fs->lock);
  632. spin_lock(&dcache_lock);
  633. if (nd->dentry != nd->mnt->mnt_root) {
  634. nd->dentry = dget(nd->dentry->d_parent);
  635. spin_unlock(&dcache_lock);
  636. dput(old);
  637. break;
  638. }
  639. spin_unlock(&dcache_lock);
  640. spin_lock(&vfsmount_lock);
  641. parent = nd->mnt->mnt_parent;
  642. if (parent == nd->mnt) {
  643. spin_unlock(&vfsmount_lock);
  644. break;
  645. }
  646. mntget(parent);
  647. nd->dentry = dget(nd->mnt->mnt_mountpoint);
  648. spin_unlock(&vfsmount_lock);
  649. dput(old);
  650. mntput(nd->mnt);
  651. nd->mnt = parent;
  652. }
  653. follow_mount(&nd->mnt, &nd->dentry);
  654. }
  655. /*
  656. * It's more convoluted than I'd like it to be, but... it's still fairly
  657. * small and for now I'd prefer to have fast path as straight as possible.
  658. * It _is_ time-critical.
  659. */
  660. static int do_lookup(struct nameidata *nd, struct qstr *name,
  661. struct path *path)
  662. {
  663. struct vfsmount *mnt = nd->mnt;
  664. struct dentry *dentry = __d_lookup(nd->dentry, name);
  665. if (!dentry)
  666. goto need_lookup;
  667. if (dentry->d_op && dentry->d_op->d_revalidate)
  668. goto need_revalidate;
  669. done:
  670. path->mnt = mnt;
  671. path->dentry = dentry;
  672. __follow_mount(path);
  673. return 0;
  674. need_lookup:
  675. dentry = real_lookup(nd->dentry, name, nd);
  676. if (IS_ERR(dentry))
  677. goto fail;
  678. goto done;
  679. need_revalidate:
  680. if (dentry->d_op->d_revalidate(dentry, nd))
  681. goto done;
  682. if (d_invalidate(dentry))
  683. goto done;
  684. dput(dentry);
  685. goto need_lookup;
  686. fail:
  687. return PTR_ERR(dentry);
  688. }
  689. /*
  690. * Name resolution.
  691. * This is the basic name resolution function, turning a pathname into
  692. * the final dentry. We expect 'base' to be positive and a directory.
  693. *
  694. * Returns 0 and nd will have valid dentry and mnt on success.
  695. * Returns error and drops reference to input namei data on failure.
  696. */
  697. static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
  698. {
  699. struct path next;
  700. struct inode *inode;
  701. int err;
  702. unsigned int lookup_flags = nd->flags;
  703. while (*name=='/')
  704. name++;
  705. if (!*name)
  706. goto return_reval;
  707. inode = nd->dentry->d_inode;
  708. if (nd->depth)
  709. lookup_flags = LOOKUP_FOLLOW;
  710. /* At this point we know we have a real path component. */
  711. for(;;) {
  712. unsigned long hash;
  713. struct qstr this;
  714. unsigned int c;
  715. nd->flags |= LOOKUP_CONTINUE;
  716. err = exec_permission_lite(inode, nd);
  717. if (err == -EAGAIN)
  718. err = vfs_permission(nd, MAY_EXEC);
  719. if (err)
  720. break;
  721. this.name = name;
  722. c = *(const unsigned char *)name;
  723. hash = init_name_hash();
  724. do {
  725. name++;
  726. hash = partial_name_hash(c, hash);
  727. c = *(const unsigned char *)name;
  728. } while (c && (c != '/'));
  729. this.len = name - (const char *) this.name;
  730. this.hash = end_name_hash(hash);
  731. /* remove trailing slashes? */
  732. if (!c)
  733. goto last_component;
  734. while (*++name == '/');
  735. if (!*name)
  736. goto last_with_slashes;
  737. /*
  738. * "." and ".." are special - ".." especially so because it has
  739. * to be able to know about the current root directory and
  740. * parent relationships.
  741. */
  742. if (this.name[0] == '.') switch (this.len) {
  743. default:
  744. break;
  745. case 2:
  746. if (this.name[1] != '.')
  747. break;
  748. follow_dotdot(nd);
  749. inode = nd->dentry->d_inode;
  750. /* fallthrough */
  751. case 1:
  752. continue;
  753. }
  754. /*
  755. * See if the low-level filesystem might want
  756. * to use its own hash..
  757. */
  758. if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
  759. err = nd->dentry->d_op->d_hash(nd->dentry, &this);
  760. if (err < 0)
  761. break;
  762. }
  763. /* This does the actual lookups.. */
  764. err = do_lookup(nd, &this, &next);
  765. if (err)
  766. break;
  767. err = -ENOENT;
  768. inode = next.dentry->d_inode;
  769. if (!inode)
  770. goto out_dput;
  771. err = -ENOTDIR;
  772. if (!inode->i_op)
  773. goto out_dput;
  774. if (inode->i_op->follow_link) {
  775. err = do_follow_link(&next, nd);
  776. if (err)
  777. goto return_err;
  778. err = -ENOENT;
  779. inode = nd->dentry->d_inode;
  780. if (!inode)
  781. break;
  782. err = -ENOTDIR;
  783. if (!inode->i_op)
  784. break;
  785. } else
  786. path_to_nameidata(&next, nd);
  787. err = -ENOTDIR;
  788. if (!inode->i_op->lookup)
  789. break;
  790. continue;
  791. /* here ends the main loop */
  792. last_with_slashes:
  793. lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  794. last_component:
  795. nd->flags &= ~LOOKUP_CONTINUE;
  796. if (lookup_flags & LOOKUP_PARENT)
  797. goto lookup_parent;
  798. if (this.name[0] == '.') switch (this.len) {
  799. default:
  800. break;
  801. case 2:
  802. if (this.name[1] != '.')
  803. break;
  804. follow_dotdot(nd);
  805. inode = nd->dentry->d_inode;
  806. /* fallthrough */
  807. case 1:
  808. goto return_reval;
  809. }
  810. if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
  811. err = nd->dentry->d_op->d_hash(nd->dentry, &this);
  812. if (err < 0)
  813. break;
  814. }
  815. err = do_lookup(nd, &this, &next);
  816. if (err)
  817. break;
  818. inode = next.dentry->d_inode;
  819. if ((lookup_flags & LOOKUP_FOLLOW)
  820. && inode && inode->i_op && inode->i_op->follow_link) {
  821. err = do_follow_link(&next, nd);
  822. if (err)
  823. goto return_err;
  824. inode = nd->dentry->d_inode;
  825. } else
  826. path_to_nameidata(&next, nd);
  827. err = -ENOENT;
  828. if (!inode)
  829. break;
  830. if (lookup_flags & LOOKUP_DIRECTORY) {
  831. err = -ENOTDIR;
  832. if (!inode->i_op || !inode->i_op->lookup)
  833. break;
  834. }
  835. goto return_base;
  836. lookup_parent:
  837. nd->last = this;
  838. nd->last_type = LAST_NORM;
  839. if (this.name[0] != '.')
  840. goto return_base;
  841. if (this.len == 1)
  842. nd->last_type = LAST_DOT;
  843. else if (this.len == 2 && this.name[1] == '.')
  844. nd->last_type = LAST_DOTDOT;
  845. else
  846. goto return_base;
  847. return_reval:
  848. /*
  849. * We bypassed the ordinary revalidation routines.
  850. * We may need to check the cached dentry for staleness.
  851. */
  852. if (nd->dentry && nd->dentry->d_sb &&
  853. (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
  854. err = -ESTALE;
  855. /* Note: we do not d_invalidate() */
  856. if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
  857. break;
  858. }
  859. return_base:
  860. return 0;
  861. out_dput:
  862. dput_path(&next, nd);
  863. break;
  864. }
  865. path_release(nd);
  866. return_err:
  867. return err;
  868. }
  869. /*
  870. * Wrapper to retry pathname resolution whenever the underlying
  871. * file system returns an ESTALE.
  872. *
  873. * Retry the whole path once, forcing real lookup requests
  874. * instead of relying on the dcache.
  875. */
  876. int fastcall link_path_walk(const char *name, struct nameidata *nd)
  877. {
  878. struct nameidata save = *nd;
  879. int result;
  880. /* make sure the stuff we saved doesn't go away */
  881. dget(save.dentry);
  882. mntget(save.mnt);
  883. result = __link_path_walk(name, nd);
  884. if (result == -ESTALE) {
  885. *nd = save;
  886. dget(nd->dentry);
  887. mntget(nd->mnt);
  888. nd->flags |= LOOKUP_REVAL;
  889. result = __link_path_walk(name, nd);
  890. }
  891. dput(save.dentry);
  892. mntput(save.mnt);
  893. return result;
  894. }
  895. int fastcall path_walk(const char * name, struct nameidata *nd)
  896. {
  897. current->total_link_count = 0;
  898. return link_path_walk(name, nd);
  899. }
  900. /*
  901. * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
  902. * everything is done. Returns 0 and drops input nd, if lookup failed;
  903. */
  904. static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
  905. {
  906. if (path_walk(name, nd))
  907. return 0; /* something went wrong... */
  908. if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
  909. struct dentry *old_dentry = nd->dentry;
  910. struct vfsmount *old_mnt = nd->mnt;
  911. struct qstr last = nd->last;
  912. int last_type = nd->last_type;
  913. /*
  914. * NAME was not found in alternate root or it's a directory. Try to find
  915. * it in the normal root:
  916. */
  917. nd->last_type = LAST_ROOT;
  918. read_lock(&current->fs->lock);
  919. nd->mnt = mntget(current->fs->rootmnt);
  920. nd->dentry = dget(current->fs->root);
  921. read_unlock(&current->fs->lock);
  922. if (path_walk(name, nd) == 0) {
  923. if (nd->dentry->d_inode) {
  924. dput(old_dentry);
  925. mntput(old_mnt);
  926. return 1;
  927. }
  928. path_release(nd);
  929. }
  930. nd->dentry = old_dentry;
  931. nd->mnt = old_mnt;
  932. nd->last = last;
  933. nd->last_type = last_type;
  934. }
  935. return 1;
  936. }
  937. void set_fs_altroot(void)
  938. {
  939. char *emul = __emul_prefix();
  940. struct nameidata nd;
  941. struct vfsmount *mnt = NULL, *oldmnt;
  942. struct dentry *dentry = NULL, *olddentry;
  943. int err;
  944. if (!emul)
  945. goto set_it;
  946. err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
  947. if (!err) {
  948. mnt = nd.mnt;
  949. dentry = nd.dentry;
  950. }
  951. set_it:
  952. write_lock(&current->fs->lock);
  953. oldmnt = current->fs->altrootmnt;
  954. olddentry = current->fs->altroot;
  955. current->fs->altrootmnt = mnt;
  956. current->fs->altroot = dentry;
  957. write_unlock(&current->fs->lock);
  958. if (olddentry) {
  959. dput(olddentry);
  960. mntput(oldmnt);
  961. }
  962. }
  963. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  964. int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
  965. {
  966. int retval = 0;
  967. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  968. nd->flags = flags;
  969. nd->depth = 0;
  970. read_lock(&current->fs->lock);
  971. if (*name=='/') {
  972. if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
  973. nd->mnt = mntget(current->fs->altrootmnt);
  974. nd->dentry = dget(current->fs->altroot);
  975. read_unlock(&current->fs->lock);
  976. if (__emul_lookup_dentry(name,nd))
  977. goto out; /* found in altroot */
  978. read_lock(&current->fs->lock);
  979. }
  980. nd->mnt = mntget(current->fs->rootmnt);
  981. nd->dentry = dget(current->fs->root);
  982. } else {
  983. nd->mnt = mntget(current->fs->pwdmnt);
  984. nd->dentry = dget(current->fs->pwd);
  985. }
  986. read_unlock(&current->fs->lock);
  987. current->total_link_count = 0;
  988. retval = link_path_walk(name, nd);
  989. out:
  990. if (unlikely(current->audit_context
  991. && nd && nd->dentry && nd->dentry->d_inode))
  992. audit_inode(name, nd->dentry->d_inode, flags);
  993. return retval;
  994. }
  995. static int __path_lookup_intent_open(const char *name, unsigned int lookup_flags,
  996. struct nameidata *nd, int open_flags, int create_mode)
  997. {
  998. struct file *filp = get_empty_filp();
  999. int err;
  1000. if (filp == NULL)
  1001. return -ENFILE;
  1002. nd->intent.open.file = filp;
  1003. nd->intent.open.flags = open_flags;
  1004. nd->intent.open.create_mode = create_mode;
  1005. err = path_lookup(name, lookup_flags|LOOKUP_OPEN, nd);
  1006. if (IS_ERR(nd->intent.open.file)) {
  1007. if (err == 0) {
  1008. err = PTR_ERR(nd->intent.open.file);
  1009. path_release(nd);
  1010. }
  1011. } else if (err != 0)
  1012. release_open_intent(nd);
  1013. return err;
  1014. }
  1015. /**
  1016. * path_lookup_open - lookup a file path with open intent
  1017. * @name: pointer to file name
  1018. * @lookup_flags: lookup intent flags
  1019. * @nd: pointer to nameidata
  1020. * @open_flags: open intent flags
  1021. */
  1022. int path_lookup_open(const char *name, unsigned int lookup_flags,
  1023. struct nameidata *nd, int open_flags)
  1024. {
  1025. return __path_lookup_intent_open(name, lookup_flags, nd,
  1026. open_flags, 0);
  1027. }
  1028. /**
  1029. * path_lookup_create - lookup a file path with open + create intent
  1030. * @name: pointer to file name
  1031. * @lookup_flags: lookup intent flags
  1032. * @nd: pointer to nameidata
  1033. * @open_flags: open intent flags
  1034. * @create_mode: create intent flags
  1035. */
  1036. static int path_lookup_create(const char *name, unsigned int lookup_flags,
  1037. struct nameidata *nd, int open_flags,
  1038. int create_mode)
  1039. {
  1040. return __path_lookup_intent_open(name, lookup_flags|LOOKUP_CREATE, nd,
  1041. open_flags, create_mode);
  1042. }
  1043. int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags,
  1044. struct nameidata *nd, int open_flags)
  1045. {
  1046. char *tmp = getname(name);
  1047. int err = PTR_ERR(tmp);
  1048. if (!IS_ERR(tmp)) {
  1049. err = __path_lookup_intent_open(tmp, lookup_flags, nd, open_flags, 0);
  1050. putname(tmp);
  1051. }
  1052. return err;
  1053. }
  1054. /*
  1055. * Restricted form of lookup. Doesn't follow links, single-component only,
  1056. * needs parent already locked. Doesn't follow mounts.
  1057. * SMP-safe.
  1058. */
  1059. static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
  1060. {
  1061. struct dentry * dentry;
  1062. struct inode *inode;
  1063. int err;
  1064. inode = base->d_inode;
  1065. err = permission(inode, MAY_EXEC, nd);
  1066. dentry = ERR_PTR(err);
  1067. if (err)
  1068. goto out;
  1069. /*
  1070. * See if the low-level filesystem might want
  1071. * to use its own hash..
  1072. */
  1073. if (base->d_op && base->d_op->d_hash) {
  1074. err = base->d_op->d_hash(base, name);
  1075. dentry = ERR_PTR(err);
  1076. if (err < 0)
  1077. goto out;
  1078. }
  1079. dentry = cached_lookup(base, name, nd);
  1080. if (!dentry) {
  1081. struct dentry *new = d_alloc(base, name);
  1082. dentry = ERR_PTR(-ENOMEM);
  1083. if (!new)
  1084. goto out;
  1085. dentry = inode->i_op->lookup(inode, new, nd);
  1086. if (!dentry)
  1087. dentry = new;
  1088. else
  1089. dput(new);
  1090. }
  1091. out:
  1092. return dentry;
  1093. }
  1094. struct dentry * lookup_hash(struct nameidata *nd)
  1095. {
  1096. return __lookup_hash(&nd->last, nd->dentry, nd);
  1097. }
  1098. /* SMP-safe */
  1099. struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
  1100. {
  1101. unsigned long hash;
  1102. struct qstr this;
  1103. unsigned int c;
  1104. this.name = name;
  1105. this.len = len;
  1106. if (!len)
  1107. goto access;
  1108. hash = init_name_hash();
  1109. while (len--) {
  1110. c = *(const unsigned char *)name++;
  1111. if (c == '/' || c == '\0')
  1112. goto access;
  1113. hash = partial_name_hash(c, hash);
  1114. }
  1115. this.hash = end_name_hash(hash);
  1116. return __lookup_hash(&this, base, NULL);
  1117. access:
  1118. return ERR_PTR(-EACCES);
  1119. }
  1120. /*
  1121. * namei()
  1122. *
  1123. * is used by most simple commands to get the inode of a specified name.
  1124. * Open, link etc use their own routines, but this is enough for things
  1125. * like 'chmod' etc.
  1126. *
  1127. * namei exists in two versions: namei/lnamei. The only difference is
  1128. * that namei follows links, while lnamei does not.
  1129. * SMP-safe
  1130. */
  1131. int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
  1132. {
  1133. char *tmp = getname(name);
  1134. int err = PTR_ERR(tmp);
  1135. if (!IS_ERR(tmp)) {
  1136. err = path_lookup(tmp, flags, nd);
  1137. putname(tmp);
  1138. }
  1139. return err;
  1140. }
  1141. /*
  1142. * It's inline, so penalty for filesystems that don't use sticky bit is
  1143. * minimal.
  1144. */
  1145. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1146. {
  1147. if (!(dir->i_mode & S_ISVTX))
  1148. return 0;
  1149. if (inode->i_uid == current->fsuid)
  1150. return 0;
  1151. if (dir->i_uid == current->fsuid)
  1152. return 0;
  1153. return !capable(CAP_FOWNER);
  1154. }
  1155. /*
  1156. * Check whether we can remove a link victim from directory dir, check
  1157. * whether the type of victim is right.
  1158. * 1. We can't do it if dir is read-only (done in permission())
  1159. * 2. We should have write and exec permissions on dir
  1160. * 3. We can't remove anything from append-only dir
  1161. * 4. We can't do anything with immutable dir (done in permission())
  1162. * 5. If the sticky bit on dir is set we should either
  1163. * a. be owner of dir, or
  1164. * b. be owner of victim, or
  1165. * c. have CAP_FOWNER capability
  1166. * 6. If the victim is append-only or immutable we can't do antyhing with
  1167. * links pointing to it.
  1168. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1169. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1170. * 9. We can't remove a root or mountpoint.
  1171. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1172. * nfs_async_unlink().
  1173. */
  1174. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1175. {
  1176. int error;
  1177. if (!victim->d_inode)
  1178. return -ENOENT;
  1179. BUG_ON(victim->d_parent->d_inode != dir);
  1180. error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
  1181. if (error)
  1182. return error;
  1183. if (IS_APPEND(dir))
  1184. return -EPERM;
  1185. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1186. IS_IMMUTABLE(victim->d_inode))
  1187. return -EPERM;
  1188. if (isdir) {
  1189. if (!S_ISDIR(victim->d_inode->i_mode))
  1190. return -ENOTDIR;
  1191. if (IS_ROOT(victim))
  1192. return -EBUSY;
  1193. } else if (S_ISDIR(victim->d_inode->i_mode))
  1194. return -EISDIR;
  1195. if (IS_DEADDIR(dir))
  1196. return -ENOENT;
  1197. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1198. return -EBUSY;
  1199. return 0;
  1200. }
  1201. /* Check whether we can create an object with dentry child in directory
  1202. * dir.
  1203. * 1. We can't do it if child already exists (open has special treatment for
  1204. * this case, but since we are inlined it's OK)
  1205. * 2. We can't do it if dir is read-only (done in permission())
  1206. * 3. We should have write and exec permissions on dir
  1207. * 4. We can't do it if dir is immutable (done in permission())
  1208. */
  1209. static inline int may_create(struct inode *dir, struct dentry *child,
  1210. struct nameidata *nd)
  1211. {
  1212. if (child->d_inode)
  1213. return -EEXIST;
  1214. if (IS_DEADDIR(dir))
  1215. return -ENOENT;
  1216. return permission(dir,MAY_WRITE | MAY_EXEC, nd);
  1217. }
  1218. /*
  1219. * O_DIRECTORY translates into forcing a directory lookup.
  1220. */
  1221. static inline int lookup_flags(unsigned int f)
  1222. {
  1223. unsigned long retval = LOOKUP_FOLLOW;
  1224. if (f & O_NOFOLLOW)
  1225. retval &= ~LOOKUP_FOLLOW;
  1226. if (f & O_DIRECTORY)
  1227. retval |= LOOKUP_DIRECTORY;
  1228. return retval;
  1229. }
  1230. /*
  1231. * p1 and p2 should be directories on the same fs.
  1232. */
  1233. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1234. {
  1235. struct dentry *p;
  1236. if (p1 == p2) {
  1237. mutex_lock(&p1->d_inode->i_mutex);
  1238. return NULL;
  1239. }
  1240. down(&p1->d_inode->i_sb->s_vfs_rename_sem);
  1241. for (p = p1; p->d_parent != p; p = p->d_parent) {
  1242. if (p->d_parent == p2) {
  1243. mutex_lock(&p2->d_inode->i_mutex);
  1244. mutex_lock(&p1->d_inode->i_mutex);
  1245. return p;
  1246. }
  1247. }
  1248. for (p = p2; p->d_parent != p; p = p->d_parent) {
  1249. if (p->d_parent == p1) {
  1250. mutex_lock(&p1->d_inode->i_mutex);
  1251. mutex_lock(&p2->d_inode->i_mutex);
  1252. return p;
  1253. }
  1254. }
  1255. mutex_lock(&p1->d_inode->i_mutex);
  1256. mutex_lock(&p2->d_inode->i_mutex);
  1257. return NULL;
  1258. }
  1259. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1260. {
  1261. mutex_unlock(&p1->d_inode->i_mutex);
  1262. if (p1 != p2) {
  1263. mutex_unlock(&p2->d_inode->i_mutex);
  1264. up(&p1->d_inode->i_sb->s_vfs_rename_sem);
  1265. }
  1266. }
  1267. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1268. struct nameidata *nd)
  1269. {
  1270. int error = may_create(dir, dentry, nd);
  1271. if (error)
  1272. return error;
  1273. if (!dir->i_op || !dir->i_op->create)
  1274. return -EACCES; /* shouldn't it be ENOSYS? */
  1275. mode &= S_IALLUGO;
  1276. mode |= S_IFREG;
  1277. error = security_inode_create(dir, dentry, mode);
  1278. if (error)
  1279. return error;
  1280. DQUOT_INIT(dir);
  1281. error = dir->i_op->create(dir, dentry, mode, nd);
  1282. if (!error)
  1283. fsnotify_create(dir, dentry->d_name.name);
  1284. return error;
  1285. }
  1286. int may_open(struct nameidata *nd, int acc_mode, int flag)
  1287. {
  1288. struct dentry *dentry = nd->dentry;
  1289. struct inode *inode = dentry->d_inode;
  1290. int error;
  1291. if (!inode)
  1292. return -ENOENT;
  1293. if (S_ISLNK(inode->i_mode))
  1294. return -ELOOP;
  1295. if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
  1296. return -EISDIR;
  1297. error = vfs_permission(nd, acc_mode);
  1298. if (error)
  1299. return error;
  1300. /*
  1301. * FIFO's, sockets and device files are special: they don't
  1302. * actually live on the filesystem itself, and as such you
  1303. * can write to them even if the filesystem is read-only.
  1304. */
  1305. if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  1306. flag &= ~O_TRUNC;
  1307. } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
  1308. if (nd->mnt->mnt_flags & MNT_NODEV)
  1309. return -EACCES;
  1310. flag &= ~O_TRUNC;
  1311. } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
  1312. return -EROFS;
  1313. /*
  1314. * An append-only file must be opened in append mode for writing.
  1315. */
  1316. if (IS_APPEND(inode)) {
  1317. if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
  1318. return -EPERM;
  1319. if (flag & O_TRUNC)
  1320. return -EPERM;
  1321. }
  1322. /* O_NOATIME can only be set by the owner or superuser */
  1323. if (flag & O_NOATIME)
  1324. if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
  1325. return -EPERM;
  1326. /*
  1327. * Ensure there are no outstanding leases on the file.
  1328. */
  1329. error = break_lease(inode, flag);
  1330. if (error)
  1331. return error;
  1332. if (flag & O_TRUNC) {
  1333. error = get_write_access(inode);
  1334. if (error)
  1335. return error;
  1336. /*
  1337. * Refuse to truncate files with mandatory locks held on them.
  1338. */
  1339. error = locks_verify_locked(inode);
  1340. if (!error) {
  1341. DQUOT_INIT(inode);
  1342. error = do_truncate(dentry, 0, ATTR_MTIME|ATTR_CTIME, NULL);
  1343. }
  1344. put_write_access(inode);
  1345. if (error)
  1346. return error;
  1347. } else
  1348. if (flag & FMODE_WRITE)
  1349. DQUOT_INIT(inode);
  1350. return 0;
  1351. }
  1352. /*
  1353. * open_namei()
  1354. *
  1355. * namei for open - this is in fact almost the whole open-routine.
  1356. *
  1357. * Note that the low bits of "flag" aren't the same as in the open
  1358. * system call - they are 00 - no permissions needed
  1359. * 01 - read permission needed
  1360. * 10 - write permission needed
  1361. * 11 - read/write permissions needed
  1362. * which is a lot more logical, and also allows the "no perm" needed
  1363. * for symlinks (where the permissions are checked later).
  1364. * SMP-safe
  1365. */
  1366. int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
  1367. {
  1368. int acc_mode, error;
  1369. struct path path;
  1370. struct dentry *dir;
  1371. int count = 0;
  1372. acc_mode = ACC_MODE(flag);
  1373. /* O_TRUNC implies we need access checks for write permissions */
  1374. if (flag & O_TRUNC)
  1375. acc_mode |= MAY_WRITE;
  1376. /* Allow the LSM permission hook to distinguish append
  1377. access from general write access. */
  1378. if (flag & O_APPEND)
  1379. acc_mode |= MAY_APPEND;
  1380. /*
  1381. * The simplest case - just a plain lookup.
  1382. */
  1383. if (!(flag & O_CREAT)) {
  1384. error = path_lookup_open(pathname, lookup_flags(flag), nd, flag);
  1385. if (error)
  1386. return error;
  1387. goto ok;
  1388. }
  1389. /*
  1390. * Create - we need to know the parent.
  1391. */
  1392. error = path_lookup_create(pathname, LOOKUP_PARENT, nd, flag, mode);
  1393. if (error)
  1394. return error;
  1395. /*
  1396. * We have the parent and last component. First of all, check
  1397. * that we are not asked to creat(2) an obvious directory - that
  1398. * will not do.
  1399. */
  1400. error = -EISDIR;
  1401. if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
  1402. goto exit;
  1403. dir = nd->dentry;
  1404. nd->flags &= ~LOOKUP_PARENT;
  1405. mutex_lock(&dir->d_inode->i_mutex);
  1406. path.dentry = lookup_hash(nd);
  1407. path.mnt = nd->mnt;
  1408. do_last:
  1409. error = PTR_ERR(path.dentry);
  1410. if (IS_ERR(path.dentry)) {
  1411. mutex_unlock(&dir->d_inode->i_mutex);
  1412. goto exit;
  1413. }
  1414. /* Negative dentry, just create the file */
  1415. if (!path.dentry->d_inode) {
  1416. if (!IS_POSIXACL(dir->d_inode))
  1417. mode &= ~current->fs->umask;
  1418. error = vfs_create(dir->d_inode, path.dentry, mode, nd);
  1419. mutex_unlock(&dir->d_inode->i_mutex);
  1420. dput(nd->dentry);
  1421. nd->dentry = path.dentry;
  1422. if (error)
  1423. goto exit;
  1424. /* Don't check for write permission, don't truncate */
  1425. acc_mode = 0;
  1426. flag &= ~O_TRUNC;
  1427. goto ok;
  1428. }
  1429. /*
  1430. * It already exists.
  1431. */
  1432. mutex_unlock(&dir->d_inode->i_mutex);
  1433. error = -EEXIST;
  1434. if (flag & O_EXCL)
  1435. goto exit_dput;
  1436. if (__follow_mount(&path)) {
  1437. error = -ELOOP;
  1438. if (flag & O_NOFOLLOW)
  1439. goto exit_dput;
  1440. }
  1441. error = -ENOENT;
  1442. if (!path.dentry->d_inode)
  1443. goto exit_dput;
  1444. if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
  1445. goto do_link;
  1446. path_to_nameidata(&path, nd);
  1447. error = -EISDIR;
  1448. if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
  1449. goto exit;
  1450. ok:
  1451. error = may_open(nd, acc_mode, flag);
  1452. if (error)
  1453. goto exit;
  1454. return 0;
  1455. exit_dput:
  1456. dput_path(&path, nd);
  1457. exit:
  1458. if (!IS_ERR(nd->intent.open.file))
  1459. release_open_intent(nd);
  1460. path_release(nd);
  1461. return error;
  1462. do_link:
  1463. error = -ELOOP;
  1464. if (flag & O_NOFOLLOW)
  1465. goto exit_dput;
  1466. /*
  1467. * This is subtle. Instead of calling do_follow_link() we do the
  1468. * thing by hands. The reason is that this way we have zero link_count
  1469. * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
  1470. * After that we have the parent and last component, i.e.
  1471. * we are in the same situation as after the first path_walk().
  1472. * Well, almost - if the last component is normal we get its copy
  1473. * stored in nd->last.name and we will have to putname() it when we
  1474. * are done. Procfs-like symlinks just set LAST_BIND.
  1475. */
  1476. nd->flags |= LOOKUP_PARENT;
  1477. error = security_inode_follow_link(path.dentry, nd);
  1478. if (error)
  1479. goto exit_dput;
  1480. error = __do_follow_link(&path, nd);
  1481. if (error)
  1482. return error;
  1483. nd->flags &= ~LOOKUP_PARENT;
  1484. if (nd->last_type == LAST_BIND)
  1485. goto ok;
  1486. error = -EISDIR;
  1487. if (nd->last_type != LAST_NORM)
  1488. goto exit;
  1489. if (nd->last.name[nd->last.len]) {
  1490. __putname(nd->last.name);
  1491. goto exit;
  1492. }
  1493. error = -ELOOP;
  1494. if (count++==32) {
  1495. __putname(nd->last.name);
  1496. goto exit;
  1497. }
  1498. dir = nd->dentry;
  1499. mutex_lock(&dir->d_inode->i_mutex);
  1500. path.dentry = lookup_hash(nd);
  1501. path.mnt = nd->mnt;
  1502. __putname(nd->last.name);
  1503. goto do_last;
  1504. }
  1505. /**
  1506. * lookup_create - lookup a dentry, creating it if it doesn't exist
  1507. * @nd: nameidata info
  1508. * @is_dir: directory flag
  1509. *
  1510. * Simple function to lookup and return a dentry and create it
  1511. * if it doesn't exist. Is SMP-safe.
  1512. *
  1513. * Returns with nd->dentry->d_inode->i_mutex locked.
  1514. */
  1515. struct dentry *lookup_create(struct nameidata *nd, int is_dir)
  1516. {
  1517. struct dentry *dentry = ERR_PTR(-EEXIST);
  1518. mutex_lock(&nd->dentry->d_inode->i_mutex);
  1519. /*
  1520. * Yucky last component or no last component at all?
  1521. * (foo/., foo/.., /////)
  1522. */
  1523. if (nd->last_type != LAST_NORM)
  1524. goto fail;
  1525. nd->flags &= ~LOOKUP_PARENT;
  1526. /*
  1527. * Do the final lookup.
  1528. */
  1529. dentry = lookup_hash(nd);
  1530. if (IS_ERR(dentry))
  1531. goto fail;
  1532. /*
  1533. * Special case - lookup gave negative, but... we had foo/bar/
  1534. * From the vfs_mknod() POV we just have a negative dentry -
  1535. * all is fine. Let's be bastards - you had / on the end, you've
  1536. * been asking for (non-existent) directory. -ENOENT for you.
  1537. */
  1538. if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
  1539. goto enoent;
  1540. return dentry;
  1541. enoent:
  1542. dput(dentry);
  1543. dentry = ERR_PTR(-ENOENT);
  1544. fail:
  1545. return dentry;
  1546. }
  1547. EXPORT_SYMBOL_GPL(lookup_create);
  1548. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1549. {
  1550. int error = may_create(dir, dentry, NULL);
  1551. if (error)
  1552. return error;
  1553. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  1554. return -EPERM;
  1555. if (!dir->i_op || !dir->i_op->mknod)
  1556. return -EPERM;
  1557. error = security_inode_mknod(dir, dentry, mode, dev);
  1558. if (error)
  1559. return error;
  1560. DQUOT_INIT(dir);
  1561. error = dir->i_op->mknod(dir, dentry, mode, dev);
  1562. if (!error)
  1563. fsnotify_create(dir, dentry->d_name.name);
  1564. return error;
  1565. }
  1566. asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
  1567. {
  1568. int error = 0;
  1569. char * tmp;
  1570. struct dentry * dentry;
  1571. struct nameidata nd;
  1572. if (S_ISDIR(mode))
  1573. return -EPERM;
  1574. tmp = getname(filename);
  1575. if (IS_ERR(tmp))
  1576. return PTR_ERR(tmp);
  1577. error = path_lookup(tmp, LOOKUP_PARENT, &nd);
  1578. if (error)
  1579. goto out;
  1580. dentry = lookup_create(&nd, 0);
  1581. error = PTR_ERR(dentry);
  1582. if (!IS_POSIXACL(nd.dentry->d_inode))
  1583. mode &= ~current->fs->umask;
  1584. if (!IS_ERR(dentry)) {
  1585. switch (mode & S_IFMT) {
  1586. case 0: case S_IFREG:
  1587. error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
  1588. break;
  1589. case S_IFCHR: case S_IFBLK:
  1590. error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
  1591. new_decode_dev(dev));
  1592. break;
  1593. case S_IFIFO: case S_IFSOCK:
  1594. error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
  1595. break;
  1596. case S_IFDIR:
  1597. error = -EPERM;
  1598. break;
  1599. default:
  1600. error = -EINVAL;
  1601. }
  1602. dput(dentry);
  1603. }
  1604. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1605. path_release(&nd);
  1606. out:
  1607. putname(tmp);
  1608. return error;
  1609. }
  1610. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1611. {
  1612. int error = may_create(dir, dentry, NULL);
  1613. if (error)
  1614. return error;
  1615. if (!dir->i_op || !dir->i_op->mkdir)
  1616. return -EPERM;
  1617. mode &= (S_IRWXUGO|S_ISVTX);
  1618. error = security_inode_mkdir(dir, dentry, mode);
  1619. if (error)
  1620. return error;
  1621. DQUOT_INIT(dir);
  1622. error = dir->i_op->mkdir(dir, dentry, mode);
  1623. if (!error)
  1624. fsnotify_mkdir(dir, dentry->d_name.name);
  1625. return error;
  1626. }
  1627. asmlinkage long sys_mkdir(const char __user * pathname, int mode)
  1628. {
  1629. int error = 0;
  1630. char * tmp;
  1631. tmp = getname(pathname);
  1632. error = PTR_ERR(tmp);
  1633. if (!IS_ERR(tmp)) {
  1634. struct dentry *dentry;
  1635. struct nameidata nd;
  1636. error = path_lookup(tmp, LOOKUP_PARENT, &nd);
  1637. if (error)
  1638. goto out;
  1639. dentry = lookup_create(&nd, 1);
  1640. error = PTR_ERR(dentry);
  1641. if (!IS_ERR(dentry)) {
  1642. if (!IS_POSIXACL(nd.dentry->d_inode))
  1643. mode &= ~current->fs->umask;
  1644. error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
  1645. dput(dentry);
  1646. }
  1647. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1648. path_release(&nd);
  1649. out:
  1650. putname(tmp);
  1651. }
  1652. return error;
  1653. }
  1654. /*
  1655. * We try to drop the dentry early: we should have
  1656. * a usage count of 2 if we're the only user of this
  1657. * dentry, and if that is true (possibly after pruning
  1658. * the dcache), then we drop the dentry now.
  1659. *
  1660. * A low-level filesystem can, if it choses, legally
  1661. * do a
  1662. *
  1663. * if (!d_unhashed(dentry))
  1664. * return -EBUSY;
  1665. *
  1666. * if it cannot handle the case of removing a directory
  1667. * that is still in use by something else..
  1668. */
  1669. void dentry_unhash(struct dentry *dentry)
  1670. {
  1671. dget(dentry);
  1672. if (atomic_read(&dentry->d_count))
  1673. shrink_dcache_parent(dentry);
  1674. spin_lock(&dcache_lock);
  1675. spin_lock(&dentry->d_lock);
  1676. if (atomic_read(&dentry->d_count) == 2)
  1677. __d_drop(dentry);
  1678. spin_unlock(&dentry->d_lock);
  1679. spin_unlock(&dcache_lock);
  1680. }
  1681. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  1682. {
  1683. int error = may_delete(dir, dentry, 1);
  1684. if (error)
  1685. return error;
  1686. if (!dir->i_op || !dir->i_op->rmdir)
  1687. return -EPERM;
  1688. DQUOT_INIT(dir);
  1689. mutex_lock(&dentry->d_inode->i_mutex);
  1690. dentry_unhash(dentry);
  1691. if (d_mountpoint(dentry))
  1692. error = -EBUSY;
  1693. else {
  1694. error = security_inode_rmdir(dir, dentry);
  1695. if (!error) {
  1696. error = dir->i_op->rmdir(dir, dentry);
  1697. if (!error)
  1698. dentry->d_inode->i_flags |= S_DEAD;
  1699. }
  1700. }
  1701. mutex_unlock(&dentry->d_inode->i_mutex);
  1702. if (!error) {
  1703. d_delete(dentry);
  1704. }
  1705. dput(dentry);
  1706. return error;
  1707. }
  1708. asmlinkage long sys_rmdir(const char __user * pathname)
  1709. {
  1710. int error = 0;
  1711. char * name;
  1712. struct dentry *dentry;
  1713. struct nameidata nd;
  1714. name = getname(pathname);
  1715. if(IS_ERR(name))
  1716. return PTR_ERR(name);
  1717. error = path_lookup(name, LOOKUP_PARENT, &nd);
  1718. if (error)
  1719. goto exit;
  1720. switch(nd.last_type) {
  1721. case LAST_DOTDOT:
  1722. error = -ENOTEMPTY;
  1723. goto exit1;
  1724. case LAST_DOT:
  1725. error = -EINVAL;
  1726. goto exit1;
  1727. case LAST_ROOT:
  1728. error = -EBUSY;
  1729. goto exit1;
  1730. }
  1731. mutex_lock(&nd.dentry->d_inode->i_mutex);
  1732. dentry = lookup_hash(&nd);
  1733. error = PTR_ERR(dentry);
  1734. if (!IS_ERR(dentry)) {
  1735. error = vfs_rmdir(nd.dentry->d_inode, dentry);
  1736. dput(dentry);
  1737. }
  1738. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1739. exit1:
  1740. path_release(&nd);
  1741. exit:
  1742. putname(name);
  1743. return error;
  1744. }
  1745. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  1746. {
  1747. int error = may_delete(dir, dentry, 0);
  1748. if (error)
  1749. return error;
  1750. if (!dir->i_op || !dir->i_op->unlink)
  1751. return -EPERM;
  1752. DQUOT_INIT(dir);
  1753. mutex_lock(&dentry->d_inode->i_mutex);
  1754. if (d_mountpoint(dentry))
  1755. error = -EBUSY;
  1756. else {
  1757. error = security_inode_unlink(dir, dentry);
  1758. if (!error)
  1759. error = dir->i_op->unlink(dir, dentry);
  1760. }
  1761. mutex_unlock(&dentry->d_inode->i_mutex);
  1762. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  1763. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  1764. d_delete(dentry);
  1765. }
  1766. return error;
  1767. }
  1768. /*
  1769. * Make sure that the actual truncation of the file will occur outside its
  1770. * directory's i_mutex. Truncate can take a long time if there is a lot of
  1771. * writeout happening, and we don't want to prevent access to the directory
  1772. * while waiting on the I/O.
  1773. */
  1774. asmlinkage long sys_unlink(const char __user * pathname)
  1775. {
  1776. int error = 0;
  1777. char * name;
  1778. struct dentry *dentry;
  1779. struct nameidata nd;
  1780. struct inode *inode = NULL;
  1781. name = getname(pathname);
  1782. if(IS_ERR(name))
  1783. return PTR_ERR(name);
  1784. error = path_lookup(name, LOOKUP_PARENT, &nd);
  1785. if (error)
  1786. goto exit;
  1787. error = -EISDIR;
  1788. if (nd.last_type != LAST_NORM)
  1789. goto exit1;
  1790. mutex_lock(&nd.dentry->d_inode->i_mutex);
  1791. dentry = lookup_hash(&nd);
  1792. error = PTR_ERR(dentry);
  1793. if (!IS_ERR(dentry)) {
  1794. /* Why not before? Because we want correct error value */
  1795. if (nd.last.name[nd.last.len])
  1796. goto slashes;
  1797. inode = dentry->d_inode;
  1798. if (inode)
  1799. atomic_inc(&inode->i_count);
  1800. error = vfs_unlink(nd.dentry->d_inode, dentry);
  1801. exit2:
  1802. dput(dentry);
  1803. }
  1804. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1805. if (inode)
  1806. iput(inode); /* truncate the inode here */
  1807. exit1:
  1808. path_release(&nd);
  1809. exit:
  1810. putname(name);
  1811. return error;
  1812. slashes:
  1813. error = !dentry->d_inode ? -ENOENT :
  1814. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  1815. goto exit2;
  1816. }
  1817. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
  1818. {
  1819. int error = may_create(dir, dentry, NULL);
  1820. if (error)
  1821. return error;
  1822. if (!dir->i_op || !dir->i_op->symlink)
  1823. return -EPERM;
  1824. error = security_inode_symlink(dir, dentry, oldname);
  1825. if (error)
  1826. return error;
  1827. DQUOT_INIT(dir);
  1828. error = dir->i_op->symlink(dir, dentry, oldname);
  1829. if (!error)
  1830. fsnotify_create(dir, dentry->d_name.name);
  1831. return error;
  1832. }
  1833. asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
  1834. {
  1835. int error = 0;
  1836. char * from;
  1837. char * to;
  1838. from = getname(oldname);
  1839. if(IS_ERR(from))
  1840. return PTR_ERR(from);
  1841. to = getname(newname);
  1842. error = PTR_ERR(to);
  1843. if (!IS_ERR(to)) {
  1844. struct dentry *dentry;
  1845. struct nameidata nd;
  1846. error = path_lookup(to, LOOKUP_PARENT, &nd);
  1847. if (error)
  1848. goto out;
  1849. dentry = lookup_create(&nd, 0);
  1850. error = PTR_ERR(dentry);
  1851. if (!IS_ERR(dentry)) {
  1852. error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
  1853. dput(dentry);
  1854. }
  1855. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1856. path_release(&nd);
  1857. out:
  1858. putname(to);
  1859. }
  1860. putname(from);
  1861. return error;
  1862. }
  1863. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  1864. {
  1865. struct inode *inode = old_dentry->d_inode;
  1866. int error;
  1867. if (!inode)
  1868. return -ENOENT;
  1869. error = may_create(dir, new_dentry, NULL);
  1870. if (error)
  1871. return error;
  1872. if (dir->i_sb != inode->i_sb)
  1873. return -EXDEV;
  1874. /*
  1875. * A link to an append-only or immutable file cannot be created.
  1876. */
  1877. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1878. return -EPERM;
  1879. if (!dir->i_op || !dir->i_op->link)
  1880. return -EPERM;
  1881. if (S_ISDIR(old_dentry->d_inode->i_mode))
  1882. return -EPERM;
  1883. error = security_inode_link(old_dentry, dir, new_dentry);
  1884. if (error)
  1885. return error;
  1886. mutex_lock(&old_dentry->d_inode->i_mutex);
  1887. DQUOT_INIT(dir);
  1888. error = dir->i_op->link(old_dentry, dir, new_dentry);
  1889. mutex_unlock(&old_dentry->d_inode->i_mutex);
  1890. if (!error)
  1891. fsnotify_create(dir, new_dentry->d_name.name);
  1892. return error;
  1893. }
  1894. /*
  1895. * Hardlinks are often used in delicate situations. We avoid
  1896. * security-related surprises by not following symlinks on the
  1897. * newname. --KAB
  1898. *
  1899. * We don't follow them on the oldname either to be compatible
  1900. * with linux 2.0, and to avoid hard-linking to directories
  1901. * and other special files. --ADM
  1902. */
  1903. asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
  1904. {
  1905. struct dentry *new_dentry;
  1906. struct nameidata nd, old_nd;
  1907. int error;
  1908. char * to;
  1909. to = getname(newname);
  1910. if (IS_ERR(to))
  1911. return PTR_ERR(to);
  1912. error = __user_walk(oldname, 0, &old_nd);
  1913. if (error)
  1914. goto exit;
  1915. error = path_lookup(to, LOOKUP_PARENT, &nd);
  1916. if (error)
  1917. goto out;
  1918. error = -EXDEV;
  1919. if (old_nd.mnt != nd.mnt)
  1920. goto out_release;
  1921. new_dentry = lookup_create(&nd, 0);
  1922. error = PTR_ERR(new_dentry);
  1923. if (!IS_ERR(new_dentry)) {
  1924. error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
  1925. dput(new_dentry);
  1926. }
  1927. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1928. out_release:
  1929. path_release(&nd);
  1930. out:
  1931. path_release(&old_nd);
  1932. exit:
  1933. putname(to);
  1934. return error;
  1935. }
  1936. /*
  1937. * The worst of all namespace operations - renaming directory. "Perverted"
  1938. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  1939. * Problems:
  1940. * a) we can get into loop creation. Check is done in is_subdir().
  1941. * b) race potential - two innocent renames can create a loop together.
  1942. * That's where 4.4 screws up. Current fix: serialization on
  1943. * sb->s_vfs_rename_sem. We might be more accurate, but that's another
  1944. * story.
  1945. * c) we have to lock _three_ objects - parents and victim (if it exists).
  1946. * And that - after we got ->i_mutex on parents (until then we don't know
  1947. * whether the target exists). Solution: try to be smart with locking
  1948. * order for inodes. We rely on the fact that tree topology may change
  1949. * only under ->s_vfs_rename_sem _and_ that parent of the object we
  1950. * move will be locked. Thus we can rank directories by the tree
  1951. * (ancestors first) and rank all non-directories after them.
  1952. * That works since everybody except rename does "lock parent, lookup,
  1953. * lock child" and rename is under ->s_vfs_rename_sem.
  1954. * HOWEVER, it relies on the assumption that any object with ->lookup()
  1955. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  1956. * we'd better make sure that there's no link(2) for them.
  1957. * d) some filesystems don't support opened-but-unlinked directories,
  1958. * either because of layout or because they are not ready to deal with
  1959. * all cases correctly. The latter will be fixed (taking this sort of
  1960. * stuff into VFS), but the former is not going away. Solution: the same
  1961. * trick as in rmdir().
  1962. * e) conversion from fhandle to dentry may come in the wrong moment - when
  1963. * we are removing the target. Solution: we will have to grab ->i_mutex
  1964. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  1965. * ->i_mutex on parents, which works but leads to some truely excessive
  1966. * locking].
  1967. */
  1968. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  1969. struct inode *new_dir, struct dentry *new_dentry)
  1970. {
  1971. int error = 0;
  1972. struct inode *target;
  1973. /*
  1974. * If we are going to change the parent - check write permissions,
  1975. * we'll need to flip '..'.
  1976. */
  1977. if (new_dir != old_dir) {
  1978. error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
  1979. if (error)
  1980. return error;
  1981. }
  1982. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  1983. if (error)
  1984. return error;
  1985. target = new_dentry->d_inode;
  1986. if (target) {
  1987. mutex_lock(&target->i_mutex);
  1988. dentry_unhash(new_dentry);
  1989. }
  1990. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  1991. error = -EBUSY;
  1992. else
  1993. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  1994. if (target) {
  1995. if (!error)
  1996. target->i_flags |= S_DEAD;
  1997. mutex_unlock(&target->i_mutex);
  1998. if (d_unhashed(new_dentry))
  1999. d_rehash(new_dentry);
  2000. dput(new_dentry);
  2001. }
  2002. if (!error)
  2003. d_move(old_dentry,new_dentry);
  2004. return error;
  2005. }
  2006. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2007. struct inode *new_dir, struct dentry *new_dentry)
  2008. {
  2009. struct inode *target;
  2010. int error;
  2011. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2012. if (error)
  2013. return error;
  2014. dget(new_dentry);
  2015. target = new_dentry->d_inode;
  2016. if (target)
  2017. mutex_lock(&target->i_mutex);
  2018. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2019. error = -EBUSY;
  2020. else
  2021. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2022. if (!error) {
  2023. /* The following d_move() should become unconditional */
  2024. if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
  2025. d_move(old_dentry, new_dentry);
  2026. }
  2027. if (target)
  2028. mutex_unlock(&target->i_mutex);
  2029. dput(new_dentry);
  2030. return error;
  2031. }
  2032. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2033. struct inode *new_dir, struct dentry *new_dentry)
  2034. {
  2035. int error;
  2036. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2037. const char *old_name;
  2038. if (old_dentry->d_inode == new_dentry->d_inode)
  2039. return 0;
  2040. error = may_delete(old_dir, old_dentry, is_dir);
  2041. if (error)
  2042. return error;
  2043. if (!new_dentry->d_inode)
  2044. error = may_create(new_dir, new_dentry, NULL);
  2045. else
  2046. error = may_delete(new_dir, new_dentry, is_dir);
  2047. if (error)
  2048. return error;
  2049. if (!old_dir->i_op || !old_dir->i_op->rename)
  2050. return -EPERM;
  2051. DQUOT_INIT(old_dir);
  2052. DQUOT_INIT(new_dir);
  2053. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2054. if (is_dir)
  2055. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2056. else
  2057. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2058. if (!error) {
  2059. const char *new_name = old_dentry->d_name.name;
  2060. fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
  2061. new_dentry->d_inode, old_dentry->d_inode);
  2062. }
  2063. fsnotify_oldname_free(old_name);
  2064. return error;
  2065. }
  2066. static int do_rename(const char * oldname, const char * newname)
  2067. {
  2068. int error = 0;
  2069. struct dentry * old_dir, * new_dir;
  2070. struct dentry * old_dentry, *new_dentry;
  2071. struct dentry * trap;
  2072. struct nameidata oldnd, newnd;
  2073. error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
  2074. if (error)
  2075. goto exit;
  2076. error = path_lookup(newname, LOOKUP_PARENT, &newnd);
  2077. if (error)
  2078. goto exit1;
  2079. error = -EXDEV;
  2080. if (oldnd.mnt != newnd.mnt)
  2081. goto exit2;
  2082. old_dir = oldnd.dentry;
  2083. error = -EBUSY;
  2084. if (oldnd.last_type != LAST_NORM)
  2085. goto exit2;
  2086. new_dir = newnd.dentry;
  2087. if (newnd.last_type != LAST_NORM)
  2088. goto exit2;
  2089. trap = lock_rename(new_dir, old_dir);
  2090. old_dentry = lookup_hash(&oldnd);
  2091. error = PTR_ERR(old_dentry);
  2092. if (IS_ERR(old_dentry))
  2093. goto exit3;
  2094. /* source must exist */
  2095. error = -ENOENT;
  2096. if (!old_dentry->d_inode)
  2097. goto exit4;
  2098. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2099. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2100. error = -ENOTDIR;
  2101. if (oldnd.last.name[oldnd.last.len])
  2102. goto exit4;
  2103. if (newnd.last.name[newnd.last.len])
  2104. goto exit4;
  2105. }
  2106. /* source should not be ancestor of target */
  2107. error = -EINVAL;
  2108. if (old_dentry == trap)
  2109. goto exit4;
  2110. new_dentry = lookup_hash(&newnd);
  2111. error = PTR_ERR(new_dentry);
  2112. if (IS_ERR(new_dentry))
  2113. goto exit4;
  2114. /* target should not be an ancestor of source */
  2115. error = -ENOTEMPTY;
  2116. if (new_dentry == trap)
  2117. goto exit5;
  2118. error = vfs_rename(old_dir->d_inode, old_dentry,
  2119. new_dir->d_inode, new_dentry);
  2120. exit5:
  2121. dput(new_dentry);
  2122. exit4:
  2123. dput(old_dentry);
  2124. exit3:
  2125. unlock_rename(new_dir, old_dir);
  2126. exit2:
  2127. path_release(&newnd);
  2128. exit1:
  2129. path_release(&oldnd);
  2130. exit:
  2131. return error;
  2132. }
  2133. asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
  2134. {
  2135. int error;
  2136. char * from;
  2137. char * to;
  2138. from = getname(oldname);
  2139. if(IS_ERR(from))
  2140. return PTR_ERR(from);
  2141. to = getname(newname);
  2142. error = PTR_ERR(to);
  2143. if (!IS_ERR(to)) {
  2144. error = do_rename(from,to);
  2145. putname(to);
  2146. }
  2147. putname(from);
  2148. return error;
  2149. }
  2150. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2151. {
  2152. int len;
  2153. len = PTR_ERR(link);
  2154. if (IS_ERR(link))
  2155. goto out;
  2156. len = strlen(link);
  2157. if (len > (unsigned) buflen)
  2158. len = buflen;
  2159. if (copy_to_user(buffer, link, len))
  2160. len = -EFAULT;
  2161. out:
  2162. return len;
  2163. }
  2164. /*
  2165. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2166. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2167. * using) it for any given inode is up to filesystem.
  2168. */
  2169. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2170. {
  2171. struct nameidata nd;
  2172. void *cookie;
  2173. nd.depth = 0;
  2174. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2175. if (!IS_ERR(cookie)) {
  2176. int res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2177. if (dentry->d_inode->i_op->put_link)
  2178. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  2179. cookie = ERR_PTR(res);
  2180. }
  2181. return PTR_ERR(cookie);
  2182. }
  2183. int vfs_follow_link(struct nameidata *nd, const char *link)
  2184. {
  2185. return __vfs_follow_link(nd, link);
  2186. }
  2187. /* get the link contents into pagecache */
  2188. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2189. {
  2190. struct page * page;
  2191. struct address_space *mapping = dentry->d_inode->i_mapping;
  2192. page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
  2193. NULL);
  2194. if (IS_ERR(page))
  2195. goto sync_fail;
  2196. wait_on_page_locked(page);
  2197. if (!PageUptodate(page))
  2198. goto async_fail;
  2199. *ppage = page;
  2200. return kmap(page);
  2201. async_fail:
  2202. page_cache_release(page);
  2203. return ERR_PTR(-EIO);
  2204. sync_fail:
  2205. return (char*)page;
  2206. }
  2207. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2208. {
  2209. struct page *page = NULL;
  2210. char *s = page_getlink(dentry, &page);
  2211. int res = vfs_readlink(dentry,buffer,buflen,s);
  2212. if (page) {
  2213. kunmap(page);
  2214. page_cache_release(page);
  2215. }
  2216. return res;
  2217. }
  2218. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2219. {
  2220. struct page *page = NULL;
  2221. nd_set_link(nd, page_getlink(dentry, &page));
  2222. return page;
  2223. }
  2224. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2225. {
  2226. struct page *page = cookie;
  2227. if (page) {
  2228. kunmap(page);
  2229. page_cache_release(page);
  2230. }
  2231. }
  2232. int page_symlink(struct inode *inode, const char *symname, int len)
  2233. {
  2234. struct address_space *mapping = inode->i_mapping;
  2235. struct page *page = grab_cache_page(mapping, 0);
  2236. int err = -ENOMEM;
  2237. char *kaddr;
  2238. if (!page)
  2239. goto fail;
  2240. err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
  2241. if (err)
  2242. goto fail_map;
  2243. kaddr = kmap_atomic(page, KM_USER0);
  2244. memcpy(kaddr, symname, len-1);
  2245. kunmap_atomic(kaddr, KM_USER0);
  2246. mapping->a_ops->commit_write(NULL, page, 0, len-1);
  2247. /*
  2248. * Notice that we are _not_ going to block here - end of page is
  2249. * unmapped, so this will only try to map the rest of page, see
  2250. * that it is unmapped (typically even will not look into inode -
  2251. * ->i_size will be enough for everything) and zero it out.
  2252. * OTOH it's obviously correct and should make the page up-to-date.
  2253. */
  2254. if (!PageUptodate(page)) {
  2255. err = mapping->a_ops->readpage(NULL, page);
  2256. wait_on_page_locked(page);
  2257. } else {
  2258. unlock_page(page);
  2259. }
  2260. page_cache_release(page);
  2261. if (err < 0)
  2262. goto fail;
  2263. mark_inode_dirty(inode);
  2264. return 0;
  2265. fail_map:
  2266. unlock_page(page);
  2267. page_cache_release(page);
  2268. fail:
  2269. return err;
  2270. }
  2271. struct inode_operations page_symlink_inode_operations = {
  2272. .readlink = generic_readlink,
  2273. .follow_link = page_follow_link_light,
  2274. .put_link = page_put_link,
  2275. };
  2276. EXPORT_SYMBOL(__user_walk);
  2277. EXPORT_SYMBOL(follow_down);
  2278. EXPORT_SYMBOL(follow_up);
  2279. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  2280. EXPORT_SYMBOL(getname);
  2281. EXPORT_SYMBOL(lock_rename);
  2282. EXPORT_SYMBOL(lookup_hash);
  2283. EXPORT_SYMBOL(lookup_one_len);
  2284. EXPORT_SYMBOL(page_follow_link_light);
  2285. EXPORT_SYMBOL(page_put_link);
  2286. EXPORT_SYMBOL(page_readlink);
  2287. EXPORT_SYMBOL(page_symlink);
  2288. EXPORT_SYMBOL(page_symlink_inode_operations);
  2289. EXPORT_SYMBOL(path_lookup);
  2290. EXPORT_SYMBOL(path_release);
  2291. EXPORT_SYMBOL(path_walk);
  2292. EXPORT_SYMBOL(permission);
  2293. EXPORT_SYMBOL(vfs_permission);
  2294. EXPORT_SYMBOL(file_permission);
  2295. EXPORT_SYMBOL(unlock_rename);
  2296. EXPORT_SYMBOL(vfs_create);
  2297. EXPORT_SYMBOL(vfs_follow_link);
  2298. EXPORT_SYMBOL(vfs_link);
  2299. EXPORT_SYMBOL(vfs_mkdir);
  2300. EXPORT_SYMBOL(vfs_mknod);
  2301. EXPORT_SYMBOL(generic_permission);
  2302. EXPORT_SYMBOL(vfs_readlink);
  2303. EXPORT_SYMBOL(vfs_rename);
  2304. EXPORT_SYMBOL(vfs_rmdir);
  2305. EXPORT_SYMBOL(vfs_symlink);
  2306. EXPORT_SYMBOL(vfs_unlink);
  2307. EXPORT_SYMBOL(dentry_unhash);
  2308. EXPORT_SYMBOL(generic_readlink);