namei.c 67 KB

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