namei.c 60 KB

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