namei.c 60 KB

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