namei.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458
  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. static inline int __do_follow_link(struct dentry *dentry, struct nameidata *nd)
  440. {
  441. int error;
  442. touch_atime(nd->mnt, dentry);
  443. nd_set_link(nd, NULL);
  444. error = dentry->d_inode->i_op->follow_link(dentry, nd);
  445. if (!error) {
  446. char *s = nd_get_link(nd);
  447. if (s)
  448. error = __vfs_follow_link(nd, s);
  449. if (dentry->d_inode->i_op->put_link)
  450. dentry->d_inode->i_op->put_link(dentry, nd);
  451. }
  452. return error;
  453. }
  454. /*
  455. * This limits recursive symlink follows to 8, while
  456. * limiting consecutive symlinks to 40.
  457. *
  458. * Without that kind of total limit, nasty chains of consecutive
  459. * symlinks can cause almost arbitrarily long lookups.
  460. */
  461. static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd)
  462. {
  463. int err = -ELOOP;
  464. if (current->link_count >= MAX_NESTED_LINKS)
  465. goto loop;
  466. if (current->total_link_count >= 40)
  467. goto loop;
  468. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  469. cond_resched();
  470. err = security_inode_follow_link(dentry, nd);
  471. if (err)
  472. goto loop;
  473. current->link_count++;
  474. current->total_link_count++;
  475. nd->depth++;
  476. err = __do_follow_link(dentry, nd);
  477. current->link_count--;
  478. nd->depth--;
  479. return err;
  480. loop:
  481. path_release(nd);
  482. return err;
  483. }
  484. int follow_up(struct vfsmount **mnt, struct dentry **dentry)
  485. {
  486. struct vfsmount *parent;
  487. struct dentry *mountpoint;
  488. spin_lock(&vfsmount_lock);
  489. parent=(*mnt)->mnt_parent;
  490. if (parent == *mnt) {
  491. spin_unlock(&vfsmount_lock);
  492. return 0;
  493. }
  494. mntget(parent);
  495. mountpoint=dget((*mnt)->mnt_mountpoint);
  496. spin_unlock(&vfsmount_lock);
  497. dput(*dentry);
  498. *dentry = mountpoint;
  499. mntput(*mnt);
  500. *mnt = parent;
  501. return 1;
  502. }
  503. /* no need for dcache_lock, as serialization is taken care in
  504. * namespace.c
  505. */
  506. static int follow_mount(struct vfsmount **mnt, struct dentry **dentry)
  507. {
  508. int res = 0;
  509. while (d_mountpoint(*dentry)) {
  510. struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
  511. if (!mounted)
  512. break;
  513. mntput(*mnt);
  514. *mnt = mounted;
  515. dput(*dentry);
  516. *dentry = dget(mounted->mnt_root);
  517. res = 1;
  518. }
  519. return res;
  520. }
  521. /* no need for dcache_lock, as serialization is taken care in
  522. * namespace.c
  523. */
  524. static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
  525. {
  526. struct vfsmount *mounted;
  527. mounted = lookup_mnt(*mnt, *dentry);
  528. if (mounted) {
  529. mntput(*mnt);
  530. *mnt = mounted;
  531. dput(*dentry);
  532. *dentry = dget(mounted->mnt_root);
  533. return 1;
  534. }
  535. return 0;
  536. }
  537. int follow_down(struct vfsmount **mnt, struct dentry **dentry)
  538. {
  539. return __follow_down(mnt,dentry);
  540. }
  541. static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry)
  542. {
  543. while(1) {
  544. struct vfsmount *parent;
  545. struct dentry *old = *dentry;
  546. read_lock(&current->fs->lock);
  547. if (*dentry == current->fs->root &&
  548. *mnt == current->fs->rootmnt) {
  549. read_unlock(&current->fs->lock);
  550. break;
  551. }
  552. read_unlock(&current->fs->lock);
  553. spin_lock(&dcache_lock);
  554. if (*dentry != (*mnt)->mnt_root) {
  555. *dentry = dget((*dentry)->d_parent);
  556. spin_unlock(&dcache_lock);
  557. dput(old);
  558. break;
  559. }
  560. spin_unlock(&dcache_lock);
  561. spin_lock(&vfsmount_lock);
  562. parent = (*mnt)->mnt_parent;
  563. if (parent == *mnt) {
  564. spin_unlock(&vfsmount_lock);
  565. break;
  566. }
  567. mntget(parent);
  568. *dentry = dget((*mnt)->mnt_mountpoint);
  569. spin_unlock(&vfsmount_lock);
  570. dput(old);
  571. mntput(*mnt);
  572. *mnt = parent;
  573. }
  574. follow_mount(mnt, dentry);
  575. }
  576. struct path {
  577. struct vfsmount *mnt;
  578. struct dentry *dentry;
  579. };
  580. /*
  581. * It's more convoluted than I'd like it to be, but... it's still fairly
  582. * small and for now I'd prefer to have fast path as straight as possible.
  583. * It _is_ time-critical.
  584. */
  585. static int do_lookup(struct nameidata *nd, struct qstr *name,
  586. struct path *path)
  587. {
  588. struct vfsmount *mnt = nd->mnt;
  589. struct dentry *dentry = __d_lookup(nd->dentry, name);
  590. if (!dentry)
  591. goto need_lookup;
  592. if (dentry->d_op && dentry->d_op->d_revalidate)
  593. goto need_revalidate;
  594. done:
  595. path->mnt = mnt;
  596. path->dentry = dentry;
  597. return 0;
  598. need_lookup:
  599. dentry = real_lookup(nd->dentry, name, nd);
  600. if (IS_ERR(dentry))
  601. goto fail;
  602. goto done;
  603. need_revalidate:
  604. if (dentry->d_op->d_revalidate(dentry, nd))
  605. goto done;
  606. if (d_invalidate(dentry))
  607. goto done;
  608. dput(dentry);
  609. goto need_lookup;
  610. fail:
  611. return PTR_ERR(dentry);
  612. }
  613. /*
  614. * Name resolution.
  615. * This is the basic name resolution function, turning a pathname into
  616. * the final dentry. We expect 'base' to be positive and a directory.
  617. *
  618. * Returns 0 and nd will have valid dentry and mnt on success.
  619. * Returns error and drops reference to input namei data on failure.
  620. */
  621. static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
  622. {
  623. struct path next;
  624. struct inode *inode;
  625. int err;
  626. unsigned int lookup_flags = nd->flags;
  627. while (*name=='/')
  628. name++;
  629. if (!*name)
  630. goto return_reval;
  631. inode = nd->dentry->d_inode;
  632. if (nd->depth)
  633. lookup_flags = LOOKUP_FOLLOW;
  634. /* At this point we know we have a real path component. */
  635. for(;;) {
  636. unsigned long hash;
  637. struct qstr this;
  638. unsigned int c;
  639. err = exec_permission_lite(inode, nd);
  640. if (err == -EAGAIN) {
  641. err = permission(inode, MAY_EXEC, nd);
  642. }
  643. if (err)
  644. break;
  645. this.name = name;
  646. c = *(const unsigned char *)name;
  647. hash = init_name_hash();
  648. do {
  649. name++;
  650. hash = partial_name_hash(c, hash);
  651. c = *(const unsigned char *)name;
  652. } while (c && (c != '/'));
  653. this.len = name - (const char *) this.name;
  654. this.hash = end_name_hash(hash);
  655. /* remove trailing slashes? */
  656. if (!c)
  657. goto last_component;
  658. while (*++name == '/');
  659. if (!*name)
  660. goto last_with_slashes;
  661. /*
  662. * "." and ".." are special - ".." especially so because it has
  663. * to be able to know about the current root directory and
  664. * parent relationships.
  665. */
  666. if (this.name[0] == '.') switch (this.len) {
  667. default:
  668. break;
  669. case 2:
  670. if (this.name[1] != '.')
  671. break;
  672. follow_dotdot(&nd->mnt, &nd->dentry);
  673. inode = nd->dentry->d_inode;
  674. /* fallthrough */
  675. case 1:
  676. continue;
  677. }
  678. /*
  679. * See if the low-level filesystem might want
  680. * to use its own hash..
  681. */
  682. if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
  683. err = nd->dentry->d_op->d_hash(nd->dentry, &this);
  684. if (err < 0)
  685. break;
  686. }
  687. nd->flags |= LOOKUP_CONTINUE;
  688. /* This does the actual lookups.. */
  689. err = do_lookup(nd, &this, &next);
  690. if (err)
  691. break;
  692. /* Check mountpoints.. */
  693. follow_mount(&next.mnt, &next.dentry);
  694. err = -ENOENT;
  695. inode = next.dentry->d_inode;
  696. if (!inode)
  697. goto out_dput;
  698. err = -ENOTDIR;
  699. if (!inode->i_op)
  700. goto out_dput;
  701. if (inode->i_op->follow_link) {
  702. mntget(next.mnt);
  703. err = do_follow_link(next.dentry, nd);
  704. dput(next.dentry);
  705. mntput(next.mnt);
  706. if (err)
  707. goto return_err;
  708. err = -ENOENT;
  709. inode = nd->dentry->d_inode;
  710. if (!inode)
  711. break;
  712. err = -ENOTDIR;
  713. if (!inode->i_op)
  714. break;
  715. } else {
  716. dput(nd->dentry);
  717. nd->mnt = next.mnt;
  718. nd->dentry = next.dentry;
  719. }
  720. err = -ENOTDIR;
  721. if (!inode->i_op->lookup)
  722. break;
  723. continue;
  724. /* here ends the main loop */
  725. last_with_slashes:
  726. lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  727. last_component:
  728. nd->flags &= ~LOOKUP_CONTINUE;
  729. if (lookup_flags & LOOKUP_PARENT)
  730. goto lookup_parent;
  731. if (this.name[0] == '.') switch (this.len) {
  732. default:
  733. break;
  734. case 2:
  735. if (this.name[1] != '.')
  736. break;
  737. follow_dotdot(&nd->mnt, &nd->dentry);
  738. inode = nd->dentry->d_inode;
  739. /* fallthrough */
  740. case 1:
  741. goto return_reval;
  742. }
  743. if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
  744. err = nd->dentry->d_op->d_hash(nd->dentry, &this);
  745. if (err < 0)
  746. break;
  747. }
  748. err = do_lookup(nd, &this, &next);
  749. if (err)
  750. break;
  751. follow_mount(&next.mnt, &next.dentry);
  752. inode = next.dentry->d_inode;
  753. if ((lookup_flags & LOOKUP_FOLLOW)
  754. && inode && inode->i_op && inode->i_op->follow_link) {
  755. mntget(next.mnt);
  756. err = do_follow_link(next.dentry, nd);
  757. dput(next.dentry);
  758. mntput(next.mnt);
  759. if (err)
  760. goto return_err;
  761. inode = nd->dentry->d_inode;
  762. } else {
  763. dput(nd->dentry);
  764. nd->mnt = next.mnt;
  765. nd->dentry = next.dentry;
  766. }
  767. err = -ENOENT;
  768. if (!inode)
  769. break;
  770. if (lookup_flags & LOOKUP_DIRECTORY) {
  771. err = -ENOTDIR;
  772. if (!inode->i_op || !inode->i_op->lookup)
  773. break;
  774. }
  775. goto return_base;
  776. lookup_parent:
  777. nd->last = this;
  778. nd->last_type = LAST_NORM;
  779. if (this.name[0] != '.')
  780. goto return_base;
  781. if (this.len == 1)
  782. nd->last_type = LAST_DOT;
  783. else if (this.len == 2 && this.name[1] == '.')
  784. nd->last_type = LAST_DOTDOT;
  785. else
  786. goto return_base;
  787. return_reval:
  788. /*
  789. * We bypassed the ordinary revalidation routines.
  790. * We may need to check the cached dentry for staleness.
  791. */
  792. if (nd->dentry && nd->dentry->d_sb &&
  793. (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
  794. err = -ESTALE;
  795. /* Note: we do not d_invalidate() */
  796. if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
  797. break;
  798. }
  799. return_base:
  800. return 0;
  801. out_dput:
  802. dput(next.dentry);
  803. break;
  804. }
  805. path_release(nd);
  806. return_err:
  807. return err;
  808. }
  809. /*
  810. * Wrapper to retry pathname resolution whenever the underlying
  811. * file system returns an ESTALE.
  812. *
  813. * Retry the whole path once, forcing real lookup requests
  814. * instead of relying on the dcache.
  815. */
  816. int fastcall link_path_walk(const char *name, struct nameidata *nd)
  817. {
  818. struct nameidata save = *nd;
  819. int result;
  820. /* make sure the stuff we saved doesn't go away */
  821. dget(save.dentry);
  822. mntget(save.mnt);
  823. result = __link_path_walk(name, nd);
  824. if (result == -ESTALE) {
  825. *nd = save;
  826. dget(nd->dentry);
  827. mntget(nd->mnt);
  828. nd->flags |= LOOKUP_REVAL;
  829. result = __link_path_walk(name, nd);
  830. }
  831. dput(save.dentry);
  832. mntput(save.mnt);
  833. return result;
  834. }
  835. int fastcall path_walk(const char * name, struct nameidata *nd)
  836. {
  837. current->total_link_count = 0;
  838. return link_path_walk(name, nd);
  839. }
  840. /*
  841. * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
  842. * everything is done. Returns 0 and drops input nd, if lookup failed;
  843. */
  844. static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
  845. {
  846. if (path_walk(name, nd))
  847. return 0; /* something went wrong... */
  848. if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
  849. struct dentry *old_dentry = nd->dentry;
  850. struct vfsmount *old_mnt = nd->mnt;
  851. struct qstr last = nd->last;
  852. int last_type = nd->last_type;
  853. /*
  854. * NAME was not found in alternate root or it's a directory. Try to find
  855. * it in the normal root:
  856. */
  857. nd->last_type = LAST_ROOT;
  858. read_lock(&current->fs->lock);
  859. nd->mnt = mntget(current->fs->rootmnt);
  860. nd->dentry = dget(current->fs->root);
  861. read_unlock(&current->fs->lock);
  862. if (path_walk(name, nd) == 0) {
  863. if (nd->dentry->d_inode) {
  864. dput(old_dentry);
  865. mntput(old_mnt);
  866. return 1;
  867. }
  868. path_release(nd);
  869. }
  870. nd->dentry = old_dentry;
  871. nd->mnt = old_mnt;
  872. nd->last = last;
  873. nd->last_type = last_type;
  874. }
  875. return 1;
  876. }
  877. void set_fs_altroot(void)
  878. {
  879. char *emul = __emul_prefix();
  880. struct nameidata nd;
  881. struct vfsmount *mnt = NULL, *oldmnt;
  882. struct dentry *dentry = NULL, *olddentry;
  883. int err;
  884. if (!emul)
  885. goto set_it;
  886. err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
  887. if (!err) {
  888. mnt = nd.mnt;
  889. dentry = nd.dentry;
  890. }
  891. set_it:
  892. write_lock(&current->fs->lock);
  893. oldmnt = current->fs->altrootmnt;
  894. olddentry = current->fs->altroot;
  895. current->fs->altrootmnt = mnt;
  896. current->fs->altroot = dentry;
  897. write_unlock(&current->fs->lock);
  898. if (olddentry) {
  899. dput(olddentry);
  900. mntput(oldmnt);
  901. }
  902. }
  903. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  904. int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
  905. {
  906. int retval = 0;
  907. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  908. nd->flags = flags;
  909. nd->depth = 0;
  910. read_lock(&current->fs->lock);
  911. if (*name=='/') {
  912. if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
  913. nd->mnt = mntget(current->fs->altrootmnt);
  914. nd->dentry = dget(current->fs->altroot);
  915. read_unlock(&current->fs->lock);
  916. if (__emul_lookup_dentry(name,nd))
  917. goto out; /* found in altroot */
  918. read_lock(&current->fs->lock);
  919. }
  920. nd->mnt = mntget(current->fs->rootmnt);
  921. nd->dentry = dget(current->fs->root);
  922. } else {
  923. nd->mnt = mntget(current->fs->pwdmnt);
  924. nd->dentry = dget(current->fs->pwd);
  925. }
  926. read_unlock(&current->fs->lock);
  927. current->total_link_count = 0;
  928. retval = link_path_walk(name, nd);
  929. out:
  930. if (unlikely(current->audit_context
  931. && nd && nd->dentry && nd->dentry->d_inode))
  932. audit_inode(name, nd->dentry->d_inode);
  933. return retval;
  934. }
  935. /*
  936. * Restricted form of lookup. Doesn't follow links, single-component only,
  937. * needs parent already locked. Doesn't follow mounts.
  938. * SMP-safe.
  939. */
  940. static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
  941. {
  942. struct dentry * dentry;
  943. struct inode *inode;
  944. int err;
  945. inode = base->d_inode;
  946. err = permission(inode, MAY_EXEC, nd);
  947. dentry = ERR_PTR(err);
  948. if (err)
  949. goto out;
  950. /*
  951. * See if the low-level filesystem might want
  952. * to use its own hash..
  953. */
  954. if (base->d_op && base->d_op->d_hash) {
  955. err = base->d_op->d_hash(base, name);
  956. dentry = ERR_PTR(err);
  957. if (err < 0)
  958. goto out;
  959. }
  960. dentry = cached_lookup(base, name, nd);
  961. if (!dentry) {
  962. struct dentry *new = d_alloc(base, name);
  963. dentry = ERR_PTR(-ENOMEM);
  964. if (!new)
  965. goto out;
  966. dentry = inode->i_op->lookup(inode, new, nd);
  967. if (!dentry)
  968. dentry = new;
  969. else
  970. dput(new);
  971. }
  972. out:
  973. return dentry;
  974. }
  975. struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
  976. {
  977. return __lookup_hash(name, base, NULL);
  978. }
  979. /* SMP-safe */
  980. struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
  981. {
  982. unsigned long hash;
  983. struct qstr this;
  984. unsigned int c;
  985. this.name = name;
  986. this.len = len;
  987. if (!len)
  988. goto access;
  989. hash = init_name_hash();
  990. while (len--) {
  991. c = *(const unsigned char *)name++;
  992. if (c == '/' || c == '\0')
  993. goto access;
  994. hash = partial_name_hash(c, hash);
  995. }
  996. this.hash = end_name_hash(hash);
  997. return lookup_hash(&this, base);
  998. access:
  999. return ERR_PTR(-EACCES);
  1000. }
  1001. /*
  1002. * namei()
  1003. *
  1004. * is used by most simple commands to get the inode of a specified name.
  1005. * Open, link etc use their own routines, but this is enough for things
  1006. * like 'chmod' etc.
  1007. *
  1008. * namei exists in two versions: namei/lnamei. The only difference is
  1009. * that namei follows links, while lnamei does not.
  1010. * SMP-safe
  1011. */
  1012. int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
  1013. {
  1014. char *tmp = getname(name);
  1015. int err = PTR_ERR(tmp);
  1016. if (!IS_ERR(tmp)) {
  1017. err = path_lookup(tmp, flags, nd);
  1018. putname(tmp);
  1019. }
  1020. return err;
  1021. }
  1022. /*
  1023. * It's inline, so penalty for filesystems that don't use sticky bit is
  1024. * minimal.
  1025. */
  1026. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1027. {
  1028. if (!(dir->i_mode & S_ISVTX))
  1029. return 0;
  1030. if (inode->i_uid == current->fsuid)
  1031. return 0;
  1032. if (dir->i_uid == current->fsuid)
  1033. return 0;
  1034. return !capable(CAP_FOWNER);
  1035. }
  1036. /*
  1037. * Check whether we can remove a link victim from directory dir, check
  1038. * whether the type of victim is right.
  1039. * 1. We can't do it if dir is read-only (done in permission())
  1040. * 2. We should have write and exec permissions on dir
  1041. * 3. We can't remove anything from append-only dir
  1042. * 4. We can't do anything with immutable dir (done in permission())
  1043. * 5. If the sticky bit on dir is set we should either
  1044. * a. be owner of dir, or
  1045. * b. be owner of victim, or
  1046. * c. have CAP_FOWNER capability
  1047. * 6. If the victim is append-only or immutable we can't do antyhing with
  1048. * links pointing to it.
  1049. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1050. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1051. * 9. We can't remove a root or mountpoint.
  1052. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1053. * nfs_async_unlink().
  1054. */
  1055. static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1056. {
  1057. int error;
  1058. if (!victim->d_inode)
  1059. return -ENOENT;
  1060. BUG_ON(victim->d_parent->d_inode != dir);
  1061. error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
  1062. if (error)
  1063. return error;
  1064. if (IS_APPEND(dir))
  1065. return -EPERM;
  1066. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1067. IS_IMMUTABLE(victim->d_inode))
  1068. return -EPERM;
  1069. if (isdir) {
  1070. if (!S_ISDIR(victim->d_inode->i_mode))
  1071. return -ENOTDIR;
  1072. if (IS_ROOT(victim))
  1073. return -EBUSY;
  1074. } else if (S_ISDIR(victim->d_inode->i_mode))
  1075. return -EISDIR;
  1076. if (IS_DEADDIR(dir))
  1077. return -ENOENT;
  1078. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1079. return -EBUSY;
  1080. return 0;
  1081. }
  1082. /* Check whether we can create an object with dentry child in directory
  1083. * dir.
  1084. * 1. We can't do it if child already exists (open has special treatment for
  1085. * this case, but since we are inlined it's OK)
  1086. * 2. We can't do it if dir is read-only (done in permission())
  1087. * 3. We should have write and exec permissions on dir
  1088. * 4. We can't do it if dir is immutable (done in permission())
  1089. */
  1090. static inline int may_create(struct inode *dir, struct dentry *child,
  1091. struct nameidata *nd)
  1092. {
  1093. if (child->d_inode)
  1094. return -EEXIST;
  1095. if (IS_DEADDIR(dir))
  1096. return -ENOENT;
  1097. return permission(dir,MAY_WRITE | MAY_EXEC, nd);
  1098. }
  1099. /*
  1100. * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
  1101. * reasons.
  1102. *
  1103. * O_DIRECTORY translates into forcing a directory lookup.
  1104. */
  1105. static inline int lookup_flags(unsigned int f)
  1106. {
  1107. unsigned long retval = LOOKUP_FOLLOW;
  1108. if (f & O_NOFOLLOW)
  1109. retval &= ~LOOKUP_FOLLOW;
  1110. if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  1111. retval &= ~LOOKUP_FOLLOW;
  1112. if (f & O_DIRECTORY)
  1113. retval |= LOOKUP_DIRECTORY;
  1114. return retval;
  1115. }
  1116. /*
  1117. * p1 and p2 should be directories on the same fs.
  1118. */
  1119. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1120. {
  1121. struct dentry *p;
  1122. if (p1 == p2) {
  1123. down(&p1->d_inode->i_sem);
  1124. return NULL;
  1125. }
  1126. down(&p1->d_inode->i_sb->s_vfs_rename_sem);
  1127. for (p = p1; p->d_parent != p; p = p->d_parent) {
  1128. if (p->d_parent == p2) {
  1129. down(&p2->d_inode->i_sem);
  1130. down(&p1->d_inode->i_sem);
  1131. return p;
  1132. }
  1133. }
  1134. for (p = p2; p->d_parent != p; p = p->d_parent) {
  1135. if (p->d_parent == p1) {
  1136. down(&p1->d_inode->i_sem);
  1137. down(&p2->d_inode->i_sem);
  1138. return p;
  1139. }
  1140. }
  1141. down(&p1->d_inode->i_sem);
  1142. down(&p2->d_inode->i_sem);
  1143. return NULL;
  1144. }
  1145. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1146. {
  1147. up(&p1->d_inode->i_sem);
  1148. if (p1 != p2) {
  1149. up(&p2->d_inode->i_sem);
  1150. up(&p1->d_inode->i_sb->s_vfs_rename_sem);
  1151. }
  1152. }
  1153. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1154. struct nameidata *nd)
  1155. {
  1156. int error = may_create(dir, dentry, nd);
  1157. if (error)
  1158. return error;
  1159. if (!dir->i_op || !dir->i_op->create)
  1160. return -EACCES; /* shouldn't it be ENOSYS? */
  1161. mode &= S_IALLUGO;
  1162. mode |= S_IFREG;
  1163. error = security_inode_create(dir, dentry, mode);
  1164. if (error)
  1165. return error;
  1166. DQUOT_INIT(dir);
  1167. error = dir->i_op->create(dir, dentry, mode, nd);
  1168. if (!error) {
  1169. inode_dir_notify(dir, DN_CREATE);
  1170. security_inode_post_create(dir, dentry, mode);
  1171. }
  1172. return error;
  1173. }
  1174. int may_open(struct nameidata *nd, int acc_mode, int flag)
  1175. {
  1176. struct dentry *dentry = nd->dentry;
  1177. struct inode *inode = dentry->d_inode;
  1178. int error;
  1179. if (!inode)
  1180. return -ENOENT;
  1181. if (S_ISLNK(inode->i_mode))
  1182. return -ELOOP;
  1183. if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
  1184. return -EISDIR;
  1185. error = permission(inode, acc_mode, nd);
  1186. if (error)
  1187. return error;
  1188. /*
  1189. * FIFO's, sockets and device files are special: they don't
  1190. * actually live on the filesystem itself, and as such you
  1191. * can write to them even if the filesystem is read-only.
  1192. */
  1193. if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  1194. flag &= ~O_TRUNC;
  1195. } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
  1196. if (nd->mnt->mnt_flags & MNT_NODEV)
  1197. return -EACCES;
  1198. flag &= ~O_TRUNC;
  1199. } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
  1200. return -EROFS;
  1201. /*
  1202. * An append-only file must be opened in append mode for writing.
  1203. */
  1204. if (IS_APPEND(inode)) {
  1205. if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
  1206. return -EPERM;
  1207. if (flag & O_TRUNC)
  1208. return -EPERM;
  1209. }
  1210. /* O_NOATIME can only be set by the owner or superuser */
  1211. if (flag & O_NOATIME)
  1212. if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
  1213. return -EPERM;
  1214. /*
  1215. * Ensure there are no outstanding leases on the file.
  1216. */
  1217. error = break_lease(inode, flag);
  1218. if (error)
  1219. return error;
  1220. if (flag & O_TRUNC) {
  1221. error = get_write_access(inode);
  1222. if (error)
  1223. return error;
  1224. /*
  1225. * Refuse to truncate files with mandatory locks held on them.
  1226. */
  1227. error = locks_verify_locked(inode);
  1228. if (!error) {
  1229. DQUOT_INIT(inode);
  1230. error = do_truncate(dentry, 0);
  1231. }
  1232. put_write_access(inode);
  1233. if (error)
  1234. return error;
  1235. } else
  1236. if (flag & FMODE_WRITE)
  1237. DQUOT_INIT(inode);
  1238. return 0;
  1239. }
  1240. /*
  1241. * open_namei()
  1242. *
  1243. * namei for open - this is in fact almost the whole open-routine.
  1244. *
  1245. * Note that the low bits of "flag" aren't the same as in the open
  1246. * system call - they are 00 - no permissions needed
  1247. * 01 - read permission needed
  1248. * 10 - write permission needed
  1249. * 11 - read/write permissions needed
  1250. * which is a lot more logical, and also allows the "no perm" needed
  1251. * for symlinks (where the permissions are checked later).
  1252. * SMP-safe
  1253. */
  1254. int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
  1255. {
  1256. int acc_mode, error = 0;
  1257. struct dentry *dentry;
  1258. struct dentry *dir;
  1259. int count = 0;
  1260. acc_mode = ACC_MODE(flag);
  1261. /* Allow the LSM permission hook to distinguish append
  1262. access from general write access. */
  1263. if (flag & O_APPEND)
  1264. acc_mode |= MAY_APPEND;
  1265. /* Fill in the open() intent data */
  1266. nd->intent.open.flags = flag;
  1267. nd->intent.open.create_mode = mode;
  1268. /*
  1269. * The simplest case - just a plain lookup.
  1270. */
  1271. if (!(flag & O_CREAT)) {
  1272. error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd);
  1273. if (error)
  1274. return error;
  1275. goto ok;
  1276. }
  1277. /*
  1278. * Create - we need to know the parent.
  1279. */
  1280. error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd);
  1281. if (error)
  1282. return error;
  1283. /*
  1284. * We have the parent and last component. First of all, check
  1285. * that we are not asked to creat(2) an obvious directory - that
  1286. * will not do.
  1287. */
  1288. error = -EISDIR;
  1289. if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
  1290. goto exit;
  1291. dir = nd->dentry;
  1292. nd->flags &= ~LOOKUP_PARENT;
  1293. down(&dir->d_inode->i_sem);
  1294. dentry = __lookup_hash(&nd->last, nd->dentry, nd);
  1295. do_last:
  1296. error = PTR_ERR(dentry);
  1297. if (IS_ERR(dentry)) {
  1298. up(&dir->d_inode->i_sem);
  1299. goto exit;
  1300. }
  1301. /* Negative dentry, just create the file */
  1302. if (!dentry->d_inode) {
  1303. if (!IS_POSIXACL(dir->d_inode))
  1304. mode &= ~current->fs->umask;
  1305. error = vfs_create(dir->d_inode, dentry, mode, nd);
  1306. up(&dir->d_inode->i_sem);
  1307. dput(nd->dentry);
  1308. nd->dentry = dentry;
  1309. if (error)
  1310. goto exit;
  1311. /* Don't check for write permission, don't truncate */
  1312. acc_mode = 0;
  1313. flag &= ~O_TRUNC;
  1314. goto ok;
  1315. }
  1316. /*
  1317. * It already exists.
  1318. */
  1319. up(&dir->d_inode->i_sem);
  1320. error = -EEXIST;
  1321. if (flag & O_EXCL)
  1322. goto exit_dput;
  1323. if (d_mountpoint(dentry)) {
  1324. error = -ELOOP;
  1325. if (flag & O_NOFOLLOW)
  1326. goto exit_dput;
  1327. while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry));
  1328. }
  1329. error = -ENOENT;
  1330. if (!dentry->d_inode)
  1331. goto exit_dput;
  1332. if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
  1333. goto do_link;
  1334. dput(nd->dentry);
  1335. nd->dentry = dentry;
  1336. error = -EISDIR;
  1337. if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
  1338. goto exit;
  1339. ok:
  1340. error = may_open(nd, acc_mode, flag);
  1341. if (error)
  1342. goto exit;
  1343. return 0;
  1344. exit_dput:
  1345. dput(dentry);
  1346. exit:
  1347. path_release(nd);
  1348. return error;
  1349. do_link:
  1350. error = -ELOOP;
  1351. if (flag & O_NOFOLLOW)
  1352. goto exit_dput;
  1353. /*
  1354. * This is subtle. Instead of calling do_follow_link() we do the
  1355. * thing by hands. The reason is that this way we have zero link_count
  1356. * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
  1357. * After that we have the parent and last component, i.e.
  1358. * we are in the same situation as after the first path_walk().
  1359. * Well, almost - if the last component is normal we get its copy
  1360. * stored in nd->last.name and we will have to putname() it when we
  1361. * are done. Procfs-like symlinks just set LAST_BIND.
  1362. */
  1363. nd->flags |= LOOKUP_PARENT;
  1364. error = security_inode_follow_link(dentry, nd);
  1365. if (error)
  1366. goto exit_dput;
  1367. error = __do_follow_link(dentry, nd);
  1368. dput(dentry);
  1369. if (error)
  1370. return error;
  1371. nd->flags &= ~LOOKUP_PARENT;
  1372. if (nd->last_type == LAST_BIND) {
  1373. dentry = nd->dentry;
  1374. goto ok;
  1375. }
  1376. error = -EISDIR;
  1377. if (nd->last_type != LAST_NORM)
  1378. goto exit;
  1379. if (nd->last.name[nd->last.len]) {
  1380. putname(nd->last.name);
  1381. goto exit;
  1382. }
  1383. error = -ELOOP;
  1384. if (count++==32) {
  1385. putname(nd->last.name);
  1386. goto exit;
  1387. }
  1388. dir = nd->dentry;
  1389. down(&dir->d_inode->i_sem);
  1390. dentry = __lookup_hash(&nd->last, nd->dentry, nd);
  1391. putname(nd->last.name);
  1392. goto do_last;
  1393. }
  1394. /**
  1395. * lookup_create - lookup a dentry, creating it if it doesn't exist
  1396. * @nd: nameidata info
  1397. * @is_dir: directory flag
  1398. *
  1399. * Simple function to lookup and return a dentry and create it
  1400. * if it doesn't exist. Is SMP-safe.
  1401. */
  1402. struct dentry *lookup_create(struct nameidata *nd, int is_dir)
  1403. {
  1404. struct dentry *dentry;
  1405. down(&nd->dentry->d_inode->i_sem);
  1406. dentry = ERR_PTR(-EEXIST);
  1407. if (nd->last_type != LAST_NORM)
  1408. goto fail;
  1409. nd->flags &= ~LOOKUP_PARENT;
  1410. dentry = lookup_hash(&nd->last, nd->dentry);
  1411. if (IS_ERR(dentry))
  1412. goto fail;
  1413. if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
  1414. goto enoent;
  1415. return dentry;
  1416. enoent:
  1417. dput(dentry);
  1418. dentry = ERR_PTR(-ENOENT);
  1419. fail:
  1420. return dentry;
  1421. }
  1422. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1423. {
  1424. int error = may_create(dir, dentry, NULL);
  1425. if (error)
  1426. return error;
  1427. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  1428. return -EPERM;
  1429. if (!dir->i_op || !dir->i_op->mknod)
  1430. return -EPERM;
  1431. error = security_inode_mknod(dir, dentry, mode, dev);
  1432. if (error)
  1433. return error;
  1434. DQUOT_INIT(dir);
  1435. error = dir->i_op->mknod(dir, dentry, mode, dev);
  1436. if (!error) {
  1437. inode_dir_notify(dir, DN_CREATE);
  1438. security_inode_post_mknod(dir, dentry, mode, dev);
  1439. }
  1440. return error;
  1441. }
  1442. asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
  1443. {
  1444. int error = 0;
  1445. char * tmp;
  1446. struct dentry * dentry;
  1447. struct nameidata nd;
  1448. if (S_ISDIR(mode))
  1449. return -EPERM;
  1450. tmp = getname(filename);
  1451. if (IS_ERR(tmp))
  1452. return PTR_ERR(tmp);
  1453. error = path_lookup(tmp, LOOKUP_PARENT, &nd);
  1454. if (error)
  1455. goto out;
  1456. dentry = lookup_create(&nd, 0);
  1457. error = PTR_ERR(dentry);
  1458. if (!IS_POSIXACL(nd.dentry->d_inode))
  1459. mode &= ~current->fs->umask;
  1460. if (!IS_ERR(dentry)) {
  1461. switch (mode & S_IFMT) {
  1462. case 0: case S_IFREG:
  1463. error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
  1464. break;
  1465. case S_IFCHR: case S_IFBLK:
  1466. error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
  1467. new_decode_dev(dev));
  1468. break;
  1469. case S_IFIFO: case S_IFSOCK:
  1470. error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
  1471. break;
  1472. case S_IFDIR:
  1473. error = -EPERM;
  1474. break;
  1475. default:
  1476. error = -EINVAL;
  1477. }
  1478. dput(dentry);
  1479. }
  1480. up(&nd.dentry->d_inode->i_sem);
  1481. path_release(&nd);
  1482. out:
  1483. putname(tmp);
  1484. return error;
  1485. }
  1486. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1487. {
  1488. int error = may_create(dir, dentry, NULL);
  1489. if (error)
  1490. return error;
  1491. if (!dir->i_op || !dir->i_op->mkdir)
  1492. return -EPERM;
  1493. mode &= (S_IRWXUGO|S_ISVTX);
  1494. error = security_inode_mkdir(dir, dentry, mode);
  1495. if (error)
  1496. return error;
  1497. DQUOT_INIT(dir);
  1498. error = dir->i_op->mkdir(dir, dentry, mode);
  1499. if (!error) {
  1500. inode_dir_notify(dir, DN_CREATE);
  1501. security_inode_post_mkdir(dir,dentry, mode);
  1502. }
  1503. return error;
  1504. }
  1505. asmlinkage long sys_mkdir(const char __user * pathname, int mode)
  1506. {
  1507. int error = 0;
  1508. char * tmp;
  1509. tmp = getname(pathname);
  1510. error = PTR_ERR(tmp);
  1511. if (!IS_ERR(tmp)) {
  1512. struct dentry *dentry;
  1513. struct nameidata nd;
  1514. error = path_lookup(tmp, LOOKUP_PARENT, &nd);
  1515. if (error)
  1516. goto out;
  1517. dentry = lookup_create(&nd, 1);
  1518. error = PTR_ERR(dentry);
  1519. if (!IS_ERR(dentry)) {
  1520. if (!IS_POSIXACL(nd.dentry->d_inode))
  1521. mode &= ~current->fs->umask;
  1522. error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
  1523. dput(dentry);
  1524. }
  1525. up(&nd.dentry->d_inode->i_sem);
  1526. path_release(&nd);
  1527. out:
  1528. putname(tmp);
  1529. }
  1530. return error;
  1531. }
  1532. /*
  1533. * We try to drop the dentry early: we should have
  1534. * a usage count of 2 if we're the only user of this
  1535. * dentry, and if that is true (possibly after pruning
  1536. * the dcache), then we drop the dentry now.
  1537. *
  1538. * A low-level filesystem can, if it choses, legally
  1539. * do a
  1540. *
  1541. * if (!d_unhashed(dentry))
  1542. * return -EBUSY;
  1543. *
  1544. * if it cannot handle the case of removing a directory
  1545. * that is still in use by something else..
  1546. */
  1547. void dentry_unhash(struct dentry *dentry)
  1548. {
  1549. dget(dentry);
  1550. if (atomic_read(&dentry->d_count))
  1551. shrink_dcache_parent(dentry);
  1552. spin_lock(&dcache_lock);
  1553. spin_lock(&dentry->d_lock);
  1554. if (atomic_read(&dentry->d_count) == 2)
  1555. __d_drop(dentry);
  1556. spin_unlock(&dentry->d_lock);
  1557. spin_unlock(&dcache_lock);
  1558. }
  1559. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  1560. {
  1561. int error = may_delete(dir, dentry, 1);
  1562. if (error)
  1563. return error;
  1564. if (!dir->i_op || !dir->i_op->rmdir)
  1565. return -EPERM;
  1566. DQUOT_INIT(dir);
  1567. down(&dentry->d_inode->i_sem);
  1568. dentry_unhash(dentry);
  1569. if (d_mountpoint(dentry))
  1570. error = -EBUSY;
  1571. else {
  1572. error = security_inode_rmdir(dir, dentry);
  1573. if (!error) {
  1574. error = dir->i_op->rmdir(dir, dentry);
  1575. if (!error)
  1576. dentry->d_inode->i_flags |= S_DEAD;
  1577. }
  1578. }
  1579. up(&dentry->d_inode->i_sem);
  1580. if (!error) {
  1581. inode_dir_notify(dir, DN_DELETE);
  1582. d_delete(dentry);
  1583. }
  1584. dput(dentry);
  1585. return error;
  1586. }
  1587. asmlinkage long sys_rmdir(const char __user * pathname)
  1588. {
  1589. int error = 0;
  1590. char * name;
  1591. struct dentry *dentry;
  1592. struct nameidata nd;
  1593. name = getname(pathname);
  1594. if(IS_ERR(name))
  1595. return PTR_ERR(name);
  1596. error = path_lookup(name, LOOKUP_PARENT, &nd);
  1597. if (error)
  1598. goto exit;
  1599. switch(nd.last_type) {
  1600. case LAST_DOTDOT:
  1601. error = -ENOTEMPTY;
  1602. goto exit1;
  1603. case LAST_DOT:
  1604. error = -EINVAL;
  1605. goto exit1;
  1606. case LAST_ROOT:
  1607. error = -EBUSY;
  1608. goto exit1;
  1609. }
  1610. down(&nd.dentry->d_inode->i_sem);
  1611. dentry = lookup_hash(&nd.last, nd.dentry);
  1612. error = PTR_ERR(dentry);
  1613. if (!IS_ERR(dentry)) {
  1614. error = vfs_rmdir(nd.dentry->d_inode, dentry);
  1615. dput(dentry);
  1616. }
  1617. up(&nd.dentry->d_inode->i_sem);
  1618. exit1:
  1619. path_release(&nd);
  1620. exit:
  1621. putname(name);
  1622. return error;
  1623. }
  1624. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  1625. {
  1626. int error = may_delete(dir, dentry, 0);
  1627. if (error)
  1628. return error;
  1629. if (!dir->i_op || !dir->i_op->unlink)
  1630. return -EPERM;
  1631. DQUOT_INIT(dir);
  1632. down(&dentry->d_inode->i_sem);
  1633. if (d_mountpoint(dentry))
  1634. error = -EBUSY;
  1635. else {
  1636. error = security_inode_unlink(dir, dentry);
  1637. if (!error)
  1638. error = dir->i_op->unlink(dir, dentry);
  1639. }
  1640. up(&dentry->d_inode->i_sem);
  1641. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  1642. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  1643. d_delete(dentry);
  1644. inode_dir_notify(dir, DN_DELETE);
  1645. }
  1646. return error;
  1647. }
  1648. /*
  1649. * Make sure that the actual truncation of the file will occur outside its
  1650. * directory's i_sem. Truncate can take a long time if there is a lot of
  1651. * writeout happening, and we don't want to prevent access to the directory
  1652. * while waiting on the I/O.
  1653. */
  1654. asmlinkage long sys_unlink(const char __user * pathname)
  1655. {
  1656. int error = 0;
  1657. char * name;
  1658. struct dentry *dentry;
  1659. struct nameidata nd;
  1660. struct inode *inode = NULL;
  1661. name = getname(pathname);
  1662. if(IS_ERR(name))
  1663. return PTR_ERR(name);
  1664. error = path_lookup(name, LOOKUP_PARENT, &nd);
  1665. if (error)
  1666. goto exit;
  1667. error = -EISDIR;
  1668. if (nd.last_type != LAST_NORM)
  1669. goto exit1;
  1670. down(&nd.dentry->d_inode->i_sem);
  1671. dentry = lookup_hash(&nd.last, nd.dentry);
  1672. error = PTR_ERR(dentry);
  1673. if (!IS_ERR(dentry)) {
  1674. /* Why not before? Because we want correct error value */
  1675. if (nd.last.name[nd.last.len])
  1676. goto slashes;
  1677. inode = dentry->d_inode;
  1678. if (inode)
  1679. atomic_inc(&inode->i_count);
  1680. error = vfs_unlink(nd.dentry->d_inode, dentry);
  1681. exit2:
  1682. dput(dentry);
  1683. }
  1684. up(&nd.dentry->d_inode->i_sem);
  1685. if (inode)
  1686. iput(inode); /* truncate the inode here */
  1687. exit1:
  1688. path_release(&nd);
  1689. exit:
  1690. putname(name);
  1691. return error;
  1692. slashes:
  1693. error = !dentry->d_inode ? -ENOENT :
  1694. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  1695. goto exit2;
  1696. }
  1697. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
  1698. {
  1699. int error = may_create(dir, dentry, NULL);
  1700. if (error)
  1701. return error;
  1702. if (!dir->i_op || !dir->i_op->symlink)
  1703. return -EPERM;
  1704. error = security_inode_symlink(dir, dentry, oldname);
  1705. if (error)
  1706. return error;
  1707. DQUOT_INIT(dir);
  1708. error = dir->i_op->symlink(dir, dentry, oldname);
  1709. if (!error) {
  1710. inode_dir_notify(dir, DN_CREATE);
  1711. security_inode_post_symlink(dir, dentry, oldname);
  1712. }
  1713. return error;
  1714. }
  1715. asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
  1716. {
  1717. int error = 0;
  1718. char * from;
  1719. char * to;
  1720. from = getname(oldname);
  1721. if(IS_ERR(from))
  1722. return PTR_ERR(from);
  1723. to = getname(newname);
  1724. error = PTR_ERR(to);
  1725. if (!IS_ERR(to)) {
  1726. struct dentry *dentry;
  1727. struct nameidata nd;
  1728. error = path_lookup(to, LOOKUP_PARENT, &nd);
  1729. if (error)
  1730. goto out;
  1731. dentry = lookup_create(&nd, 0);
  1732. error = PTR_ERR(dentry);
  1733. if (!IS_ERR(dentry)) {
  1734. error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
  1735. dput(dentry);
  1736. }
  1737. up(&nd.dentry->d_inode->i_sem);
  1738. path_release(&nd);
  1739. out:
  1740. putname(to);
  1741. }
  1742. putname(from);
  1743. return error;
  1744. }
  1745. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  1746. {
  1747. struct inode *inode = old_dentry->d_inode;
  1748. int error;
  1749. if (!inode)
  1750. return -ENOENT;
  1751. error = may_create(dir, new_dentry, NULL);
  1752. if (error)
  1753. return error;
  1754. if (dir->i_sb != inode->i_sb)
  1755. return -EXDEV;
  1756. /*
  1757. * A link to an append-only or immutable file cannot be created.
  1758. */
  1759. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1760. return -EPERM;
  1761. if (!dir->i_op || !dir->i_op->link)
  1762. return -EPERM;
  1763. if (S_ISDIR(old_dentry->d_inode->i_mode))
  1764. return -EPERM;
  1765. error = security_inode_link(old_dentry, dir, new_dentry);
  1766. if (error)
  1767. return error;
  1768. down(&old_dentry->d_inode->i_sem);
  1769. DQUOT_INIT(dir);
  1770. error = dir->i_op->link(old_dentry, dir, new_dentry);
  1771. up(&old_dentry->d_inode->i_sem);
  1772. if (!error) {
  1773. inode_dir_notify(dir, DN_CREATE);
  1774. security_inode_post_link(old_dentry, dir, new_dentry);
  1775. }
  1776. return error;
  1777. }
  1778. /*
  1779. * Hardlinks are often used in delicate situations. We avoid
  1780. * security-related surprises by not following symlinks on the
  1781. * newname. --KAB
  1782. *
  1783. * We don't follow them on the oldname either to be compatible
  1784. * with linux 2.0, and to avoid hard-linking to directories
  1785. * and other special files. --ADM
  1786. */
  1787. asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
  1788. {
  1789. struct dentry *new_dentry;
  1790. struct nameidata nd, old_nd;
  1791. int error;
  1792. char * to;
  1793. to = getname(newname);
  1794. if (IS_ERR(to))
  1795. return PTR_ERR(to);
  1796. error = __user_walk(oldname, 0, &old_nd);
  1797. if (error)
  1798. goto exit;
  1799. error = path_lookup(to, LOOKUP_PARENT, &nd);
  1800. if (error)
  1801. goto out;
  1802. error = -EXDEV;
  1803. if (old_nd.mnt != nd.mnt)
  1804. goto out_release;
  1805. new_dentry = lookup_create(&nd, 0);
  1806. error = PTR_ERR(new_dentry);
  1807. if (!IS_ERR(new_dentry)) {
  1808. error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
  1809. dput(new_dentry);
  1810. }
  1811. up(&nd.dentry->d_inode->i_sem);
  1812. out_release:
  1813. path_release(&nd);
  1814. out:
  1815. path_release(&old_nd);
  1816. exit:
  1817. putname(to);
  1818. return error;
  1819. }
  1820. /*
  1821. * The worst of all namespace operations - renaming directory. "Perverted"
  1822. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  1823. * Problems:
  1824. * a) we can get into loop creation. Check is done in is_subdir().
  1825. * b) race potential - two innocent renames can create a loop together.
  1826. * That's where 4.4 screws up. Current fix: serialization on
  1827. * sb->s_vfs_rename_sem. We might be more accurate, but that's another
  1828. * story.
  1829. * c) we have to lock _three_ objects - parents and victim (if it exists).
  1830. * And that - after we got ->i_sem on parents (until then we don't know
  1831. * whether the target exists). Solution: try to be smart with locking
  1832. * order for inodes. We rely on the fact that tree topology may change
  1833. * only under ->s_vfs_rename_sem _and_ that parent of the object we
  1834. * move will be locked. Thus we can rank directories by the tree
  1835. * (ancestors first) and rank all non-directories after them.
  1836. * That works since everybody except rename does "lock parent, lookup,
  1837. * lock child" and rename is under ->s_vfs_rename_sem.
  1838. * HOWEVER, it relies on the assumption that any object with ->lookup()
  1839. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  1840. * we'd better make sure that there's no link(2) for them.
  1841. * d) some filesystems don't support opened-but-unlinked directories,
  1842. * either because of layout or because they are not ready to deal with
  1843. * all cases correctly. The latter will be fixed (taking this sort of
  1844. * stuff into VFS), but the former is not going away. Solution: the same
  1845. * trick as in rmdir().
  1846. * e) conversion from fhandle to dentry may come in the wrong moment - when
  1847. * we are removing the target. Solution: we will have to grab ->i_sem
  1848. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  1849. * ->i_sem on parents, which works but leads to some truely excessive
  1850. * locking].
  1851. */
  1852. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  1853. struct inode *new_dir, struct dentry *new_dentry)
  1854. {
  1855. int error = 0;
  1856. struct inode *target;
  1857. /*
  1858. * If we are going to change the parent - check write permissions,
  1859. * we'll need to flip '..'.
  1860. */
  1861. if (new_dir != old_dir) {
  1862. error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
  1863. if (error)
  1864. return error;
  1865. }
  1866. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  1867. if (error)
  1868. return error;
  1869. target = new_dentry->d_inode;
  1870. if (target) {
  1871. down(&target->i_sem);
  1872. dentry_unhash(new_dentry);
  1873. }
  1874. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  1875. error = -EBUSY;
  1876. else
  1877. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  1878. if (target) {
  1879. if (!error)
  1880. target->i_flags |= S_DEAD;
  1881. up(&target->i_sem);
  1882. if (d_unhashed(new_dentry))
  1883. d_rehash(new_dentry);
  1884. dput(new_dentry);
  1885. }
  1886. if (!error) {
  1887. d_move(old_dentry,new_dentry);
  1888. security_inode_post_rename(old_dir, old_dentry,
  1889. new_dir, new_dentry);
  1890. }
  1891. return error;
  1892. }
  1893. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  1894. struct inode *new_dir, struct dentry *new_dentry)
  1895. {
  1896. struct inode *target;
  1897. int error;
  1898. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  1899. if (error)
  1900. return error;
  1901. dget(new_dentry);
  1902. target = new_dentry->d_inode;
  1903. if (target)
  1904. down(&target->i_sem);
  1905. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  1906. error = -EBUSY;
  1907. else
  1908. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  1909. if (!error) {
  1910. /* The following d_move() should become unconditional */
  1911. if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
  1912. d_move(old_dentry, new_dentry);
  1913. security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
  1914. }
  1915. if (target)
  1916. up(&target->i_sem);
  1917. dput(new_dentry);
  1918. return error;
  1919. }
  1920. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1921. struct inode *new_dir, struct dentry *new_dentry)
  1922. {
  1923. int error;
  1924. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  1925. if (old_dentry->d_inode == new_dentry->d_inode)
  1926. return 0;
  1927. error = may_delete(old_dir, old_dentry, is_dir);
  1928. if (error)
  1929. return error;
  1930. if (!new_dentry->d_inode)
  1931. error = may_create(new_dir, new_dentry, NULL);
  1932. else
  1933. error = may_delete(new_dir, new_dentry, is_dir);
  1934. if (error)
  1935. return error;
  1936. if (!old_dir->i_op || !old_dir->i_op->rename)
  1937. return -EPERM;
  1938. DQUOT_INIT(old_dir);
  1939. DQUOT_INIT(new_dir);
  1940. if (is_dir)
  1941. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  1942. else
  1943. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  1944. if (!error) {
  1945. if (old_dir == new_dir)
  1946. inode_dir_notify(old_dir, DN_RENAME);
  1947. else {
  1948. inode_dir_notify(old_dir, DN_DELETE);
  1949. inode_dir_notify(new_dir, DN_CREATE);
  1950. }
  1951. }
  1952. return error;
  1953. }
  1954. static inline int do_rename(const char * oldname, const char * newname)
  1955. {
  1956. int error = 0;
  1957. struct dentry * old_dir, * new_dir;
  1958. struct dentry * old_dentry, *new_dentry;
  1959. struct dentry * trap;
  1960. struct nameidata oldnd, newnd;
  1961. error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
  1962. if (error)
  1963. goto exit;
  1964. error = path_lookup(newname, LOOKUP_PARENT, &newnd);
  1965. if (error)
  1966. goto exit1;
  1967. error = -EXDEV;
  1968. if (oldnd.mnt != newnd.mnt)
  1969. goto exit2;
  1970. old_dir = oldnd.dentry;
  1971. error = -EBUSY;
  1972. if (oldnd.last_type != LAST_NORM)
  1973. goto exit2;
  1974. new_dir = newnd.dentry;
  1975. if (newnd.last_type != LAST_NORM)
  1976. goto exit2;
  1977. trap = lock_rename(new_dir, old_dir);
  1978. old_dentry = lookup_hash(&oldnd.last, old_dir);
  1979. error = PTR_ERR(old_dentry);
  1980. if (IS_ERR(old_dentry))
  1981. goto exit3;
  1982. /* source must exist */
  1983. error = -ENOENT;
  1984. if (!old_dentry->d_inode)
  1985. goto exit4;
  1986. /* unless the source is a directory trailing slashes give -ENOTDIR */
  1987. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  1988. error = -ENOTDIR;
  1989. if (oldnd.last.name[oldnd.last.len])
  1990. goto exit4;
  1991. if (newnd.last.name[newnd.last.len])
  1992. goto exit4;
  1993. }
  1994. /* source should not be ancestor of target */
  1995. error = -EINVAL;
  1996. if (old_dentry == trap)
  1997. goto exit4;
  1998. new_dentry = lookup_hash(&newnd.last, new_dir);
  1999. error = PTR_ERR(new_dentry);
  2000. if (IS_ERR(new_dentry))
  2001. goto exit4;
  2002. /* target should not be an ancestor of source */
  2003. error = -ENOTEMPTY;
  2004. if (new_dentry == trap)
  2005. goto exit5;
  2006. error = vfs_rename(old_dir->d_inode, old_dentry,
  2007. new_dir->d_inode, new_dentry);
  2008. exit5:
  2009. dput(new_dentry);
  2010. exit4:
  2011. dput(old_dentry);
  2012. exit3:
  2013. unlock_rename(new_dir, old_dir);
  2014. exit2:
  2015. path_release(&newnd);
  2016. exit1:
  2017. path_release(&oldnd);
  2018. exit:
  2019. return error;
  2020. }
  2021. asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
  2022. {
  2023. int error;
  2024. char * from;
  2025. char * to;
  2026. from = getname(oldname);
  2027. if(IS_ERR(from))
  2028. return PTR_ERR(from);
  2029. to = getname(newname);
  2030. error = PTR_ERR(to);
  2031. if (!IS_ERR(to)) {
  2032. error = do_rename(from,to);
  2033. putname(to);
  2034. }
  2035. putname(from);
  2036. return error;
  2037. }
  2038. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2039. {
  2040. int len;
  2041. len = PTR_ERR(link);
  2042. if (IS_ERR(link))
  2043. goto out;
  2044. len = strlen(link);
  2045. if (len > (unsigned) buflen)
  2046. len = buflen;
  2047. if (copy_to_user(buffer, link, len))
  2048. len = -EFAULT;
  2049. out:
  2050. return len;
  2051. }
  2052. /*
  2053. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2054. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2055. * using) it for any given inode is up to filesystem.
  2056. */
  2057. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2058. {
  2059. struct nameidata nd;
  2060. int res;
  2061. nd.depth = 0;
  2062. res = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2063. if (!res) {
  2064. res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2065. if (dentry->d_inode->i_op->put_link)
  2066. dentry->d_inode->i_op->put_link(dentry, &nd);
  2067. }
  2068. return res;
  2069. }
  2070. int vfs_follow_link(struct nameidata *nd, const char *link)
  2071. {
  2072. return __vfs_follow_link(nd, link);
  2073. }
  2074. /* get the link contents into pagecache */
  2075. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2076. {
  2077. struct page * page;
  2078. struct address_space *mapping = dentry->d_inode->i_mapping;
  2079. page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
  2080. NULL);
  2081. if (IS_ERR(page))
  2082. goto sync_fail;
  2083. wait_on_page_locked(page);
  2084. if (!PageUptodate(page))
  2085. goto async_fail;
  2086. *ppage = page;
  2087. return kmap(page);
  2088. async_fail:
  2089. page_cache_release(page);
  2090. return ERR_PTR(-EIO);
  2091. sync_fail:
  2092. return (char*)page;
  2093. }
  2094. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2095. {
  2096. struct page *page = NULL;
  2097. char *s = page_getlink(dentry, &page);
  2098. int res = vfs_readlink(dentry,buffer,buflen,s);
  2099. if (page) {
  2100. kunmap(page);
  2101. page_cache_release(page);
  2102. }
  2103. return res;
  2104. }
  2105. int page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2106. {
  2107. struct page *page;
  2108. nd_set_link(nd, page_getlink(dentry, &page));
  2109. return 0;
  2110. }
  2111. void page_put_link(struct dentry *dentry, struct nameidata *nd)
  2112. {
  2113. if (!IS_ERR(nd_get_link(nd))) {
  2114. struct page *page;
  2115. page = find_get_page(dentry->d_inode->i_mapping, 0);
  2116. if (!page)
  2117. BUG();
  2118. kunmap(page);
  2119. page_cache_release(page);
  2120. page_cache_release(page);
  2121. }
  2122. }
  2123. int page_symlink(struct inode *inode, const char *symname, int len)
  2124. {
  2125. struct address_space *mapping = inode->i_mapping;
  2126. struct page *page = grab_cache_page(mapping, 0);
  2127. int err = -ENOMEM;
  2128. char *kaddr;
  2129. if (!page)
  2130. goto fail;
  2131. err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
  2132. if (err)
  2133. goto fail_map;
  2134. kaddr = kmap_atomic(page, KM_USER0);
  2135. memcpy(kaddr, symname, len-1);
  2136. kunmap_atomic(kaddr, KM_USER0);
  2137. mapping->a_ops->commit_write(NULL, page, 0, len-1);
  2138. /*
  2139. * Notice that we are _not_ going to block here - end of page is
  2140. * unmapped, so this will only try to map the rest of page, see
  2141. * that it is unmapped (typically even will not look into inode -
  2142. * ->i_size will be enough for everything) and zero it out.
  2143. * OTOH it's obviously correct and should make the page up-to-date.
  2144. */
  2145. if (!PageUptodate(page)) {
  2146. err = mapping->a_ops->readpage(NULL, page);
  2147. wait_on_page_locked(page);
  2148. } else {
  2149. unlock_page(page);
  2150. }
  2151. page_cache_release(page);
  2152. if (err < 0)
  2153. goto fail;
  2154. mark_inode_dirty(inode);
  2155. return 0;
  2156. fail_map:
  2157. unlock_page(page);
  2158. page_cache_release(page);
  2159. fail:
  2160. return err;
  2161. }
  2162. struct inode_operations page_symlink_inode_operations = {
  2163. .readlink = generic_readlink,
  2164. .follow_link = page_follow_link_light,
  2165. .put_link = page_put_link,
  2166. };
  2167. EXPORT_SYMBOL(__user_walk);
  2168. EXPORT_SYMBOL(follow_down);
  2169. EXPORT_SYMBOL(follow_up);
  2170. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  2171. EXPORT_SYMBOL(getname);
  2172. EXPORT_SYMBOL(lock_rename);
  2173. EXPORT_SYMBOL(lookup_hash);
  2174. EXPORT_SYMBOL(lookup_one_len);
  2175. EXPORT_SYMBOL(page_follow_link_light);
  2176. EXPORT_SYMBOL(page_put_link);
  2177. EXPORT_SYMBOL(page_readlink);
  2178. EXPORT_SYMBOL(page_symlink);
  2179. EXPORT_SYMBOL(page_symlink_inode_operations);
  2180. EXPORT_SYMBOL(path_lookup);
  2181. EXPORT_SYMBOL(path_release);
  2182. EXPORT_SYMBOL(path_walk);
  2183. EXPORT_SYMBOL(permission);
  2184. EXPORT_SYMBOL(unlock_rename);
  2185. EXPORT_SYMBOL(vfs_create);
  2186. EXPORT_SYMBOL(vfs_follow_link);
  2187. EXPORT_SYMBOL(vfs_link);
  2188. EXPORT_SYMBOL(vfs_mkdir);
  2189. EXPORT_SYMBOL(vfs_mknod);
  2190. EXPORT_SYMBOL(generic_permission);
  2191. EXPORT_SYMBOL(vfs_readlink);
  2192. EXPORT_SYMBOL(vfs_rename);
  2193. EXPORT_SYMBOL(vfs_rmdir);
  2194. EXPORT_SYMBOL(vfs_symlink);
  2195. EXPORT_SYMBOL(vfs_unlink);
  2196. EXPORT_SYMBOL(dentry_unhash);
  2197. EXPORT_SYMBOL(generic_readlink);