4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Some corrections by tytso.
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
21 #include <linux/namei.h>
22 #include <linux/quotaops.h>
23 #include <linux/pagemap.h>
24 #include <linux/dnotify.h>
25 #include <linux/smp_lock.h>
26 #include <linux/personality.h>
27 #include <linux/security.h>
28 #include <linux/mount.h>
29 #include <linux/audit.h>
30 #include <linux/vs_base.h>
32 #include <asm/namei.h>
33 #include <asm/uaccess.h>
35 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
37 /* [Feb-1997 T. Schoebel-Theuer]
38 * Fundamental changes in the pathname lookup mechanisms (namei)
39 * were necessary because of omirr. The reason is that omirr needs
40 * to know the _real_ pathname, not the user-supplied one, in case
41 * of symlinks (and also when transname replacements occur).
43 * The new code replaces the old recursive symlink resolution with
44 * an iterative one (in case of non-nested symlink chains). It does
45 * this with calls to <fs>_follow_link().
46 * As a side effect, dir_namei(), _namei() and follow_link() are now
47 * replaced with a single function lookup_dentry() that can handle all
48 * the special cases of the former code.
50 * With the new dcache, the pathname is stored at each inode, at least as
51 * long as the refcount of the inode is positive. As a side effect, the
52 * size of the dcache depends on the inode cache and thus is dynamic.
54 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
55 * resolution to correspond with current state of the code.
57 * Note that the symlink resolution is not *completely* iterative.
58 * There is still a significant amount of tail- and mid- recursion in
59 * the algorithm. Also, note that <fs>_readlink() is not used in
60 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
61 * may return different results than <fs>_follow_link(). Many virtual
62 * filesystems (including /proc) exhibit this behavior.
65 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
66 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
67 * and the name already exists in form of a symlink, try to create the new
68 * name indicated by the symlink. The old code always complained that the
69 * name already exists, due to not following the symlink even if its target
70 * is nonexistent. The new semantics affects also mknod() and link() when
71 * the name is a symlink pointing to a non-existant name.
73 * I don't know which semantics is the right one, since I have no access
74 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
75 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
76 * "old" one. Personally, I think the new semantics is much more logical.
77 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
78 * file does succeed in both HP-UX and SunOs, but not in Solaris
79 * and in the old Linux semantics.
82 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
83 * semantics. See the comments in "open_namei" and "do_link" below.
85 * [10-Sep-98 Alan Modra] Another symlink change.
88 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
89 * inside the path - always follow.
90 * in the last component in creation/removal/renaming - never follow.
91 * if LOOKUP_FOLLOW passed - follow.
92 * if the pathname has trailing slashes - follow.
93 * otherwise - don't follow.
94 * (applied in that order).
96 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
97 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
98 * During the 2.4 we need to fix the userland stuff depending on it -
99 * hopefully we will be able to get rid of that wart in 2.5. So far only
100 * XEmacs seems to be relying on it...
103 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
104 * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives
105 * any extra contention...
108 /* In order to reduce some races, while at the same time doing additional
109 * checking and hopefully speeding things up, we copy filenames to the
110 * kernel data space before using them..
112 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
113 * PATH_MAX includes the nul terminator --RR.
115 static inline int do_getname(const char __user *filename, char *page)
118 unsigned long len = PATH_MAX;
120 if ((unsigned long) filename >= TASK_SIZE) {
121 if (!segment_eq(get_fs(), KERNEL_DS))
123 } else if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
124 len = TASK_SIZE - (unsigned long) filename;
126 retval = strncpy_from_user((char *)page, filename, len);
130 return -ENAMETOOLONG;
136 char * getname(const char __user * filename)
140 result = ERR_PTR(-ENOMEM);
143 int retval = do_getname(filename, tmp);
148 result = ERR_PTR(retval);
151 if (unlikely(current->audit_context) && !IS_ERR(result) && result)
152 audit_getname(result);
159 * is used to check for read/write/execute permissions on a file.
160 * We use "fsuid" for this, letting us set arbitrary permissions
161 * for filesystem access without changing the "normal" uids which
162 * are used for other things..
164 int vfs_permission(struct inode * inode, int mask)
166 umode_t mode = inode->i_mode;
168 if (mask & MAY_WRITE) {
170 * Nobody gets write access to a read-only fs.
172 if (IS_RDONLY(inode) &&
173 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
177 * Nobody gets write access to an immutable file.
179 if (IS_IMMUTABLE(inode))
183 if (current->fsuid == inode->i_uid)
185 else if (in_group_p(inode->i_gid))
189 * If the DACs are ok we don't need any capability check.
191 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
195 * Read/write DACs are always overridable.
196 * Executable DACs are overridable if at least one exec bit is set.
198 if (!(mask & MAY_EXEC) ||
199 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
200 if (capable(CAP_DAC_OVERRIDE))
204 * Searching includes executable on directories, else just read.
206 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
207 if (capable(CAP_DAC_READ_SEARCH))
213 static inline int xid_permission(struct inode *inode, int mask, struct nameidata *nd)
215 if (inode->i_xid == 0)
217 if (vx_check(inode->i_xid, VX_ADMIN|VX_WATCH|VX_IDENT))
220 printk("VSW: xid=%d denied access to %p[#%d,%lu] »%*s«.\n",
221 vx_current_xid(), inode, inode->i_xid, inode->i_ino,
222 nd->dentry->d_name.len, nd->dentry->d_name.name);
227 int permission(struct inode * inode,int mask, struct nameidata *nd)
231 umode_t mode = inode->i_mode;
233 /* Ordinary permission routines do not understand MAY_APPEND. */
234 submask = mask & ~MAY_APPEND;
236 if (nd && (mask & MAY_WRITE) && MNT_IS_RDONLY(nd->mnt) &&
237 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
240 if ((retval = xid_permission(inode, mask, nd)))
242 if (inode->i_op && inode->i_op->permission)
243 retval = inode->i_op->permission(inode, submask, nd);
245 retval = vfs_permission(inode, submask);
249 return security_inode_permission(inode, mask, nd);
253 * get_write_access() gets write permission for a file.
254 * put_write_access() releases this write permission.
255 * This is used for regular files.
256 * We cannot support write (and maybe mmap read-write shared) accesses and
257 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
258 * can have the following values:
259 * 0: no writers, no VM_DENYWRITE mappings
260 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
261 * > 0: (i_writecount) users are writing to the file.
263 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
264 * except for the cases where we don't hold i_writecount yet. Then we need to
265 * use {get,deny}_write_access() - these functions check the sign and refuse
266 * to do the change if sign is wrong. Exclusion between them is provided by
267 * the inode->i_lock spinlock.
270 int get_write_access(struct inode * inode)
272 spin_lock(&inode->i_lock);
273 if (atomic_read(&inode->i_writecount) < 0) {
274 spin_unlock(&inode->i_lock);
277 atomic_inc(&inode->i_writecount);
278 spin_unlock(&inode->i_lock);
283 int deny_write_access(struct file * file)
285 struct inode *inode = file->f_dentry->d_inode;
287 spin_lock(&inode->i_lock);
288 if (atomic_read(&inode->i_writecount) > 0) {
289 spin_unlock(&inode->i_lock);
292 atomic_dec(&inode->i_writecount);
293 spin_unlock(&inode->i_lock);
298 void path_release(struct nameidata *nd)
305 * umount() mustn't call path_release()/mntput() as that would clear
308 void path_release_on_umount(struct nameidata *nd)
315 * Internal lookup() using the new generic dcache.
318 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
320 struct dentry * dentry = __d_lookup(parent, name);
322 /* lockess __d_lookup may fail due to concurrent d_move()
323 * in some unrelated directory, so try with d_lookup
326 dentry = d_lookup(parent, name);
328 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
329 if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
338 * Short-cut version of permission(), for calling by
339 * path_walk(), when dcache lock is held. Combines parts
340 * of permission() and vfs_permission(), and tests ONLY for
341 * MAY_EXEC permission.
343 * If appropriate, check DAC only. If not appropriate, or
344 * short-cut DAC fails, then call permission() to do more
345 * complete permission check.
347 static inline int exec_permission_lite(struct inode *inode,
348 struct nameidata *nd)
350 umode_t mode = inode->i_mode;
352 if (inode->i_op && inode->i_op->permission)
355 if (current->fsuid == inode->i_uid)
357 else if (in_group_p(inode->i_gid))
363 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
366 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
369 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
374 return security_inode_permission(inode, MAY_EXEC, nd);
378 * This is called when everything else fails, and we actually have
379 * to go to the low-level filesystem to find out what we should do..
381 * We get the directory semaphore, and after getting that we also
382 * make sure that nobody added the entry to the dcache in the meantime..
385 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
387 struct dentry * result;
388 struct inode *dir = parent->d_inode;
392 * First re-do the cached lookup just in case it was created
393 * while we waited for the directory semaphore..
395 * FIXME! This could use version numbering or similar to
396 * avoid unnecessary cache lookups.
398 * The "dcache_lock" is purely to protect the RCU list walker
399 * from concurrent renames at this point (we mustn't get false
400 * negatives from the RCU list walk here, unlike the optimistic
403 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
405 result = d_lookup(parent, name);
407 struct dentry * dentry = d_alloc(parent, name);
408 result = ERR_PTR(-ENOMEM);
410 result = dir->i_op->lookup(dir, dentry, nd);
421 * Uhhuh! Nasty case: the cache was re-populated while
422 * we waited on the semaphore. Need to revalidate.
425 if (result->d_op && result->d_op->d_revalidate) {
426 if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
428 result = ERR_PTR(-ENOENT);
434 static int __emul_lookup_dentry(const char *, struct nameidata *);
438 walk_init_root(const char *name, struct nameidata *nd)
440 read_lock(¤t->fs->lock);
441 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
442 nd->mnt = mntget(current->fs->altrootmnt);
443 nd->dentry = dget(current->fs->altroot);
444 read_unlock(¤t->fs->lock);
445 if (__emul_lookup_dentry(name,nd))
447 read_lock(¤t->fs->lock);
449 nd->mnt = mntget(current->fs->rootmnt);
450 nd->dentry = dget(current->fs->root);
451 read_unlock(¤t->fs->lock);
455 static inline int __vfs_follow_link(struct nameidata *nd, const char *link)
464 if (!walk_init_root(link, nd))
465 /* weird __emul_prefix() stuff did it */
468 res = link_path_walk(link, nd);
470 if (nd->depth || res || nd->last_type!=LAST_NORM)
473 * If it is an iterative symlinks resolution in open_namei() we
474 * have to copy the last component. And all that crap because of
475 * bloody create() on broken symlinks. Furrfu...
478 if (unlikely(!name)) {
482 strcpy(name, nd->last.name);
483 nd->last.name = name;
487 return PTR_ERR(link);
491 * This limits recursive symlink follows to 8, while
492 * limiting consecutive symlinks to 40.
494 * Without that kind of total limit, nasty chains of consecutive
495 * symlinks can cause almost arbitrarily long lookups.
497 static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd)
500 if (current->link_count >= MAX_NESTED_LINKS)
502 if (current->total_link_count >= 40)
504 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
506 err = security_inode_follow_link(dentry, nd);
509 current->link_count++;
510 current->total_link_count++;
512 touch_atime(nd->mnt, dentry);
513 nd_set_link(nd, NULL);
514 err = dentry->d_inode->i_op->follow_link(dentry, nd);
516 char *s = nd_get_link(nd);
518 err = __vfs_follow_link(nd, s);
519 if (dentry->d_inode->i_op->put_link)
520 dentry->d_inode->i_op->put_link(dentry, nd);
522 current->link_count--;
530 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
532 struct vfsmount *parent;
533 struct dentry *mountpoint;
534 spin_lock(&vfsmount_lock);
535 parent=(*mnt)->mnt_parent;
536 if (parent == *mnt) {
537 spin_unlock(&vfsmount_lock);
541 mountpoint=dget((*mnt)->mnt_mountpoint);
542 spin_unlock(&vfsmount_lock);
544 *dentry = mountpoint;
550 /* no need for dcache_lock, as serialization is taken care in
553 static int follow_mount(struct vfsmount **mnt, struct dentry **dentry)
556 while (d_mountpoint(*dentry)) {
557 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
563 *dentry = dget(mounted->mnt_root);
569 /* no need for dcache_lock, as serialization is taken care in
572 static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
574 struct vfsmount *mounted;
576 mounted = lookup_mnt(*mnt, *dentry);
581 *dentry = dget(mounted->mnt_root);
587 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
589 return __follow_down(mnt,dentry);
592 static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry)
595 struct vfsmount *parent;
596 struct dentry *old = *dentry;
598 read_lock(¤t->fs->lock);
599 if (*dentry == current->fs->root &&
600 *mnt == current->fs->rootmnt) {
601 read_unlock(¤t->fs->lock);
604 read_unlock(¤t->fs->lock);
605 spin_lock(&dcache_lock);
606 if (*dentry != (*mnt)->mnt_root) {
607 *dentry = dget((*dentry)->d_parent);
608 spin_unlock(&dcache_lock);
612 spin_unlock(&dcache_lock);
613 spin_lock(&vfsmount_lock);
614 parent = (*mnt)->mnt_parent;
615 if (parent == *mnt) {
616 spin_unlock(&vfsmount_lock);
620 *dentry = dget((*mnt)->mnt_mountpoint);
621 spin_unlock(&vfsmount_lock);
626 follow_mount(mnt, dentry);
630 struct vfsmount *mnt;
631 struct dentry *dentry;
635 * It's more convoluted than I'd like it to be, but... it's still fairly
636 * small and for now I'd prefer to have fast path as straight as possible.
637 * It _is_ time-critical.
639 static int do_lookup(struct nameidata *nd, struct qstr *name,
642 struct vfsmount *mnt = nd->mnt;
643 struct dentry *dentry = __d_lookup(nd->dentry, name);
647 if (dentry->d_op && dentry->d_op->d_revalidate)
648 goto need_revalidate;
651 path->dentry = dentry;
655 dentry = real_lookup(nd->dentry, name, nd);
661 if (dentry->d_op->d_revalidate(dentry, nd))
663 if (d_invalidate(dentry))
669 return PTR_ERR(dentry);
675 * This is the basic name resolution function, turning a pathname
676 * into the final dentry.
678 * We expect 'base' to be positive and a directory.
680 int fastcall link_path_walk(const char * name, struct nameidata *nd)
685 unsigned int lookup_flags = nd->flags;
687 atomic = (lookup_flags & LOOKUP_ATOMIC);
694 inode = nd->dentry->d_inode;
696 lookup_flags = LOOKUP_FOLLOW;
698 /* At this point we know we have a real path component. */
704 err = exec_permission_lite(inode, nd);
705 if (err == -EAGAIN) {
706 err = permission(inode, MAY_EXEC, nd);
712 c = *(const unsigned char *)name;
714 hash = init_name_hash();
717 hash = partial_name_hash(c, hash);
718 c = *(const unsigned char *)name;
719 } while (c && (c != '/'));
720 this.len = name - (const char *) this.name;
721 this.hash = end_name_hash(hash);
723 /* remove trailing slashes? */
726 while (*++name == '/');
728 goto last_with_slashes;
731 * "." and ".." are special - ".." especially so because it has
732 * to be able to know about the current root directory and
733 * parent relationships.
735 if (this.name[0] == '.') switch (this.len) {
739 if (this.name[1] != '.')
741 follow_dotdot(&nd->mnt, &nd->dentry);
742 inode = nd->dentry->d_inode;
748 * See if the low-level filesystem might want
749 * to use its own hash..
751 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
752 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
756 err = -EWOULDBLOCKIO;
759 nd->flags |= LOOKUP_CONTINUE;
760 /* This does the actual lookups.. */
761 err = do_lookup(nd, &this, &next);
764 /* Check mountpoints.. */
765 follow_mount(&next.mnt, &next.dentry);
768 inode = next.dentry->d_inode;
775 if (inode->i_op->follow_link) {
777 err = do_follow_link(next.dentry, nd);
783 inode = nd->dentry->d_inode;
792 nd->dentry = next.dentry;
795 if (!inode->i_op->lookup)
798 /* here ends the main loop */
801 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
803 nd->flags &= ~LOOKUP_CONTINUE;
804 if (lookup_flags & LOOKUP_PARENT)
806 if (this.name[0] == '.') switch (this.len) {
810 if (this.name[1] != '.')
812 follow_dotdot(&nd->mnt, &nd->dentry);
813 inode = nd->dentry->d_inode;
818 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
819 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
823 err = -EWOULDBLOCKIO;
826 err = do_lookup(nd, &this, &next);
829 follow_mount(&next.mnt, &next.dentry);
830 inode = next.dentry->d_inode;
831 if ((lookup_flags & LOOKUP_FOLLOW)
832 && inode && inode->i_op && inode->i_op->follow_link) {
834 err = do_follow_link(next.dentry, nd);
839 inode = nd->dentry->d_inode;
843 nd->dentry = next.dentry;
848 if (lookup_flags & LOOKUP_DIRECTORY) {
850 if (!inode->i_op || !inode->i_op->lookup)
856 nd->last_type = LAST_NORM;
857 if (this.name[0] != '.')
860 nd->last_type = LAST_DOT;
861 else if (this.len == 2 && this.name[1] == '.')
862 nd->last_type = LAST_DOTDOT;
867 * We bypassed the ordinary revalidation routines.
868 * We may need to check the cached dentry for staleness.
870 if (nd->dentry && nd->dentry->d_sb &&
871 (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
873 /* Note: we do not d_invalidate() */
874 if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
888 int fastcall path_walk(const char * name, struct nameidata *nd)
890 current->total_link_count = 0;
891 return link_path_walk(name, nd);
895 /* returns 1 if everything is done */
896 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
898 if (path_walk(name, nd))
899 return 0; /* something went wrong... */
901 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
902 struct dentry *old_dentry = nd->dentry;
903 struct vfsmount *old_mnt = nd->mnt;
904 struct qstr last = nd->last;
905 int last_type = nd->last_type;
907 * NAME was not found in alternate root or it's a directory. Try to find
908 * it in the normal root:
910 nd->last_type = LAST_ROOT;
911 read_lock(¤t->fs->lock);
912 nd->mnt = mntget(current->fs->rootmnt);
913 nd->dentry = dget(current->fs->root);
914 read_unlock(¤t->fs->lock);
915 if (path_walk(name, nd) == 0) {
916 if (nd->dentry->d_inode) {
923 nd->dentry = old_dentry;
926 nd->last_type = last_type;
931 void set_fs_altroot(void)
933 char *emul = __emul_prefix();
935 struct vfsmount *mnt = NULL, *oldmnt;
936 struct dentry *dentry = NULL, *olddentry;
941 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
947 write_lock(¤t->fs->lock);
948 oldmnt = current->fs->altrootmnt;
949 olddentry = current->fs->altroot;
950 current->fs->altrootmnt = mnt;
951 current->fs->altroot = dentry;
952 write_unlock(¤t->fs->lock);
959 int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
963 nd->last_type = LAST_ROOT; /* if there are only slashes... */
967 read_lock(¤t->fs->lock);
969 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
970 nd->mnt = mntget(current->fs->altrootmnt);
971 nd->dentry = dget(current->fs->altroot);
972 read_unlock(¤t->fs->lock);
973 if (__emul_lookup_dentry(name,nd))
975 read_lock(¤t->fs->lock);
977 nd->mnt = mntget(current->fs->rootmnt);
978 nd->dentry = dget(current->fs->root);
980 nd->mnt = mntget(current->fs->pwdmnt);
981 nd->dentry = dget(current->fs->pwd);
983 read_unlock(¤t->fs->lock);
984 current->total_link_count = 0;
985 retval = link_path_walk(name, nd);
986 if (unlikely(current->audit_context
987 && nd && nd->dentry && nd->dentry->d_inode))
989 nd->dentry->d_inode->i_ino,
990 nd->dentry->d_inode->i_rdev);
995 * Restricted form of lookup. Doesn't follow links, single-component only,
996 * needs parent already locked. Doesn't follow mounts.
999 static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
1001 struct dentry * dentry;
1002 struct inode *inode;
1005 inode = base->d_inode;
1006 err = permission(inode, MAY_EXEC, nd);
1007 dentry = ERR_PTR(err);
1012 * See if the low-level filesystem might want
1013 * to use its own hash..
1015 if (base->d_op && base->d_op->d_hash) {
1016 err = base->d_op->d_hash(base, name);
1017 dentry = ERR_PTR(err);
1022 dentry = cached_lookup(base, name, nd);
1024 struct dentry *new = d_alloc(base, name);
1025 dentry = ERR_PTR(-ENOMEM);
1028 dentry = inode->i_op->lookup(inode, new, nd);
1038 struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
1040 return __lookup_hash(name, base, NULL);
1044 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
1055 hash = init_name_hash();
1057 c = *(const unsigned char *)name++;
1058 if (c == '/' || c == '\0')
1060 hash = partial_name_hash(c, hash);
1062 this.hash = end_name_hash(hash);
1064 return lookup_hash(&this, base);
1066 return ERR_PTR(-EACCES);
1072 * is used by most simple commands to get the inode of a specified name.
1073 * Open, link etc use their own routines, but this is enough for things
1076 * namei exists in two versions: namei/lnamei. The only difference is
1077 * that namei follows links, while lnamei does not.
1080 int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1082 char *tmp = getname(name);
1083 int err = PTR_ERR(tmp);
1086 err = path_lookup(tmp, flags, nd);
1093 * It's inline, so penalty for filesystems that don't use sticky bit is
1096 static inline int check_sticky(struct inode *dir, struct inode *inode)
1098 if (!(dir->i_mode & S_ISVTX))
1100 if (inode->i_uid == current->fsuid)
1102 if (dir->i_uid == current->fsuid)
1104 return !capable(CAP_FOWNER);
1108 * Check whether we can remove a link victim from directory dir, check
1109 * whether the type of victim is right.
1110 * 1. We can't do it if dir is read-only (done in permission())
1111 * 2. We should have write and exec permissions on dir
1112 * 3. We can't remove anything from append-only dir
1113 * 4. We can't do anything with immutable dir (done in permission())
1114 * 5. If the sticky bit on dir is set we should either
1115 * a. be owner of dir, or
1116 * b. be owner of victim, or
1117 * c. have CAP_FOWNER capability
1118 * 6. If the victim is append-only or immutable we can't do antyhing with
1119 * links pointing to it.
1120 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1121 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1122 * 9. We can't remove a root or mountpoint.
1123 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1124 * nfs_async_unlink().
1126 static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1129 if (!victim->d_inode)
1131 if (victim->d_parent->d_inode != dir)
1134 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1139 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1140 IS_IXORUNLINK(victim->d_inode))
1143 if (!S_ISDIR(victim->d_inode->i_mode))
1145 if (IS_ROOT(victim))
1147 } else if (S_ISDIR(victim->d_inode->i_mode))
1149 if (IS_DEADDIR(dir))
1151 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1156 /* Check whether we can create an object with dentry child in directory
1158 * 1. We can't do it if child already exists (open has special treatment for
1159 * this case, but since we are inlined it's OK)
1160 * 2. We can't do it if dir is read-only (done in permission())
1161 * 3. We should have write and exec permissions on dir
1162 * 4. We can't do it if dir is immutable (done in permission())
1164 static inline int may_create(struct inode *dir, struct dentry *child,
1165 struct nameidata *nd)
1169 if (IS_DEADDIR(dir))
1171 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1174 static inline int mnt_may_create(struct vfsmount *mnt, struct inode *dir, struct dentry *child) {
1177 if (IS_DEADDIR(dir))
1179 if (mnt->mnt_flags & MNT_RDONLY)
1184 static inline int mnt_may_unlink(struct vfsmount *mnt, struct inode *dir, struct dentry *child) {
1185 if (!child->d_inode)
1187 if (mnt->mnt_flags & MNT_RDONLY)
1193 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1196 * O_DIRECTORY translates into forcing a directory lookup.
1198 static inline int lookup_flags(unsigned int f)
1200 unsigned long retval = LOOKUP_FOLLOW;
1203 retval &= ~LOOKUP_FOLLOW;
1205 if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1206 retval &= ~LOOKUP_FOLLOW;
1208 if (f & O_DIRECTORY)
1209 retval |= LOOKUP_DIRECTORY;
1210 if (f & O_ATOMICLOOKUP)
1211 retval |= LOOKUP_ATOMIC;
1217 * p1 and p2 should be directories on the same fs.
1219 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1224 down(&p1->d_inode->i_sem);
1228 down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1230 for (p = p1; p->d_parent != p; p = p->d_parent) {
1231 if (p->d_parent == p2) {
1232 down(&p2->d_inode->i_sem);
1233 down(&p1->d_inode->i_sem);
1238 for (p = p2; p->d_parent != p; p = p->d_parent) {
1239 if (p->d_parent == p1) {
1240 down(&p1->d_inode->i_sem);
1241 down(&p2->d_inode->i_sem);
1246 down(&p1->d_inode->i_sem);
1247 down(&p2->d_inode->i_sem);
1251 void unlock_rename(struct dentry *p1, struct dentry *p2)
1253 up(&p1->d_inode->i_sem);
1255 up(&p2->d_inode->i_sem);
1256 up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1260 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1261 struct nameidata *nd)
1263 int error = may_create(dir, dentry, nd);
1268 if (!dir->i_op || !dir->i_op->create)
1269 return -EACCES; /* shouldn't it be ENOSYS? */
1272 error = security_inode_create(dir, dentry, mode);
1276 error = dir->i_op->create(dir, dentry, mode, nd);
1278 inode_dir_notify(dir, DN_CREATE);
1279 security_inode_post_create(dir, dentry, mode);
1284 int may_open(struct nameidata *nd, int acc_mode, int flag)
1286 struct dentry *dentry = nd->dentry;
1287 struct inode *inode = dentry->d_inode;
1293 if (S_ISLNK(inode->i_mode))
1296 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1299 error = permission(inode, acc_mode, nd);
1304 * FIFO's, sockets and device files are special: they don't
1305 * actually live on the filesystem itself, and as such you
1306 * can write to them even if the filesystem is read-only.
1308 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1310 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1311 if (nd->mnt->mnt_flags & MNT_NODEV)
1315 } else if ((IS_RDONLY(inode) || (nd && MNT_IS_RDONLY(nd->mnt)))
1316 && (flag & FMODE_WRITE))
1319 * An append-only file must be opened in append mode for writing.
1321 if (IS_APPEND(inode)) {
1322 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1328 /* O_NOATIME can only be set by the owner or superuser */
1329 if (flag & O_NOATIME)
1330 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1334 * Ensure there are no outstanding leases on the file.
1336 error = break_lease(inode, flag);
1340 if (flag & O_TRUNC) {
1341 error = get_write_access(inode);
1346 * Refuse to truncate files with mandatory locks held on them.
1348 error = locks_verify_locked(inode);
1352 error = do_truncate(dentry, 0);
1354 put_write_access(inode);
1358 if (flag & FMODE_WRITE)
1367 * namei for open - this is in fact almost the whole open-routine.
1369 * Note that the low bits of "flag" aren't the same as in the open
1370 * system call - they are 00 - no permissions needed
1371 * 01 - read permission needed
1372 * 10 - write permission needed
1373 * 11 - read/write permissions needed
1374 * which is a lot more logical, and also allows the "no perm" needed
1375 * for symlinks (where the permissions are checked later).
1378 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1380 int acc_mode, error = 0;
1381 struct dentry *dentry;
1385 acc_mode = ACC_MODE(flag);
1387 /* Allow the LSM permission hook to distinguish append
1388 access from general write access. */
1389 if (flag & O_APPEND)
1390 acc_mode |= MAY_APPEND;
1392 /* Fill in the open() intent data */
1393 nd->intent.open.flags = flag;
1394 nd->intent.open.create_mode = mode;
1397 * The simplest case - just a plain lookup.
1399 if (!(flag & O_CREAT)) {
1400 error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd);
1407 * Create - we need to know the parent.
1409 error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd);
1414 * We have the parent and last component. First of all, check
1415 * that we are not asked to creat(2) an obvious directory - that
1419 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1423 nd->flags &= ~LOOKUP_PARENT;
1424 down(&dir->d_inode->i_sem);
1425 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1428 error = PTR_ERR(dentry);
1429 if (IS_ERR(dentry)) {
1430 up(&dir->d_inode->i_sem);
1434 /* Negative dentry, just create the file */
1435 if (!dentry->d_inode) {
1436 if (!IS_POSIXACL(dir->d_inode))
1437 mode &= ~current->fs->umask;
1438 error = vfs_create(dir->d_inode, dentry, mode, nd);
1439 up(&dir->d_inode->i_sem);
1441 nd->dentry = dentry;
1444 /* Don't check for write permission, don't truncate */
1451 * It already exists.
1453 up(&dir->d_inode->i_sem);
1459 if (d_mountpoint(dentry)) {
1461 if (flag & O_NOFOLLOW)
1463 while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry));
1466 if (!dentry->d_inode)
1468 if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
1472 nd->dentry = dentry;
1474 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
1477 error = may_open(nd, acc_mode, flag);
1490 if (flag & O_NOFOLLOW)
1493 * This is subtle. Instead of calling do_follow_link() we do the
1494 * thing by hands. The reason is that this way we have zero link_count
1495 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1496 * After that we have the parent and last component, i.e.
1497 * we are in the same situation as after the first path_walk().
1498 * Well, almost - if the last component is normal we get its copy
1499 * stored in nd->last.name and we will have to putname() it when we
1500 * are done. Procfs-like symlinks just set LAST_BIND.
1502 nd->flags |= LOOKUP_PARENT;
1503 error = security_inode_follow_link(dentry, nd);
1506 touch_atime(nd->mnt, dentry);
1507 nd_set_link(nd, NULL);
1508 error = dentry->d_inode->i_op->follow_link(dentry, nd);
1510 char *s = nd_get_link(nd);
1512 error = __vfs_follow_link(nd, s);
1513 if (dentry->d_inode->i_op->put_link)
1514 dentry->d_inode->i_op->put_link(dentry, nd);
1519 nd->flags &= ~LOOKUP_PARENT;
1520 if (nd->last_type == LAST_BIND) {
1521 dentry = nd->dentry;
1525 if (nd->last_type != LAST_NORM)
1527 if (nd->last.name[nd->last.len]) {
1528 putname(nd->last.name);
1533 putname(nd->last.name);
1537 down(&dir->d_inode->i_sem);
1538 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1539 putname(nd->last.name);
1544 * lookup_create - lookup a dentry, creating it if it doesn't exist
1545 * @nd: nameidata info
1546 * @is_dir: directory flag
1548 * Simple function to lookup and return a dentry and create it
1549 * if it doesn't exist. Is SMP-safe.
1551 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1553 struct dentry *dentry;
1556 down(&nd->dentry->d_inode->i_sem);
1558 if (nd->last_type != LAST_NORM)
1560 nd->flags &= ~LOOKUP_PARENT;
1561 dentry = lookup_hash(&nd->last, nd->dentry);
1564 error = mnt_may_create(nd->mnt, nd->dentry->d_inode, dentry);
1568 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1575 return ERR_PTR(error);
1578 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1580 int error = may_create(dir, dentry, NULL);
1585 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1588 if (!dir->i_op || !dir->i_op->mknod)
1591 error = security_inode_mknod(dir, dentry, mode, dev);
1596 error = dir->i_op->mknod(dir, dentry, mode, dev);
1598 inode_dir_notify(dir, DN_CREATE);
1599 security_inode_post_mknod(dir, dentry, mode, dev);
1604 asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
1608 struct dentry * dentry;
1609 struct nameidata nd;
1613 tmp = getname(filename);
1615 return PTR_ERR(tmp);
1617 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1620 dentry = lookup_create(&nd, 0);
1621 error = PTR_ERR(dentry);
1623 if (!IS_POSIXACL(nd.dentry->d_inode))
1624 mode &= ~current->fs->umask;
1625 if (!IS_ERR(dentry)) {
1626 switch (mode & S_IFMT) {
1627 case 0: case S_IFREG:
1628 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1630 case S_IFCHR: case S_IFBLK:
1631 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1632 new_decode_dev(dev));
1634 case S_IFIFO: case S_IFSOCK:
1635 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1645 up(&nd.dentry->d_inode->i_sem);
1653 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1655 int error = may_create(dir, dentry, NULL);
1660 if (!dir->i_op || !dir->i_op->mkdir)
1663 mode &= (S_IRWXUGO|S_ISVTX);
1664 error = security_inode_mkdir(dir, dentry, mode);
1669 error = dir->i_op->mkdir(dir, dentry, mode);
1671 inode_dir_notify(dir, DN_CREATE);
1672 security_inode_post_mkdir(dir,dentry, mode);
1677 asmlinkage long sys_mkdir(const char __user * pathname, int mode)
1682 tmp = getname(pathname);
1683 error = PTR_ERR(tmp);
1685 struct dentry *dentry;
1686 struct nameidata nd;
1688 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1691 dentry = lookup_create(&nd, 1);
1692 error = PTR_ERR(dentry);
1693 if (!IS_ERR(dentry)) {
1694 if (!IS_POSIXACL(nd.dentry->d_inode))
1695 mode &= ~current->fs->umask;
1696 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1699 up(&nd.dentry->d_inode->i_sem);
1709 * We try to drop the dentry early: we should have
1710 * a usage count of 2 if we're the only user of this
1711 * dentry, and if that is true (possibly after pruning
1712 * the dcache), then we drop the dentry now.
1714 * A low-level filesystem can, if it choses, legally
1717 * if (!d_unhashed(dentry))
1720 * if it cannot handle the case of removing a directory
1721 * that is still in use by something else..
1723 static void d_unhash(struct dentry *dentry)
1726 spin_lock(&dcache_lock);
1727 switch (atomic_read(&dentry->d_count)) {
1729 spin_unlock(&dcache_lock);
1730 shrink_dcache_parent(dentry);
1731 spin_lock(&dcache_lock);
1732 if (atomic_read(&dentry->d_count) != 2)
1737 spin_unlock(&dcache_lock);
1740 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1742 int error = may_delete(dir, dentry, 1);
1747 if (!dir->i_op || !dir->i_op->rmdir)
1752 down(&dentry->d_inode->i_sem);
1754 if (d_mountpoint(dentry))
1757 error = security_inode_rmdir(dir, dentry);
1759 error = dir->i_op->rmdir(dir, dentry);
1761 dentry->d_inode->i_flags |= S_DEAD;
1764 up(&dentry->d_inode->i_sem);
1766 inode_dir_notify(dir, DN_DELETE);
1774 asmlinkage long sys_rmdir(const char __user * pathname)
1778 struct dentry *dentry;
1779 struct nameidata nd;
1781 name = getname(pathname);
1783 return PTR_ERR(name);
1785 error = path_lookup(name, LOOKUP_PARENT, &nd);
1789 switch(nd.last_type) {
1800 down(&nd.dentry->d_inode->i_sem);
1801 dentry = lookup_hash(&nd.last, nd.dentry);
1802 error = PTR_ERR(dentry);
1803 if (!IS_ERR(dentry)) {
1804 error = mnt_may_unlink(nd.mnt, nd.dentry->d_inode, dentry);
1807 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1811 up(&nd.dentry->d_inode->i_sem);
1819 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1821 int error = may_delete(dir, dentry, 0);
1826 if (!dir->i_op || !dir->i_op->unlink)
1831 down(&dentry->d_inode->i_sem);
1832 if (d_mountpoint(dentry))
1835 error = security_inode_unlink(dir, dentry);
1837 error = dir->i_op->unlink(dir, dentry);
1839 up(&dentry->d_inode->i_sem);
1841 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1842 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1844 inode_dir_notify(dir, DN_DELETE);
1850 * Make sure that the actual truncation of the file will occur outside its
1851 * directory's i_sem. Truncate can take a long time if there is a lot of
1852 * writeout happening, and we don't want to prevent access to the directory
1853 * while waiting on the I/O.
1855 asmlinkage long sys_unlink(const char __user * pathname)
1859 struct dentry *dentry;
1860 struct nameidata nd;
1861 struct inode *inode = NULL;
1863 name = getname(pathname);
1865 return PTR_ERR(name);
1867 error = path_lookup(name, LOOKUP_PARENT, &nd);
1871 if (nd.last_type != LAST_NORM)
1873 down(&nd.dentry->d_inode->i_sem);
1874 dentry = lookup_hash(&nd.last, nd.dentry);
1875 error = PTR_ERR(dentry);
1876 if (!IS_ERR(dentry)) {
1877 /* Why not before? Because we want correct error value */
1878 if (nd.last.name[nd.last.len])
1880 error = mnt_may_unlink(nd.mnt, nd.dentry->d_inode, dentry);
1883 inode = dentry->d_inode;
1885 atomic_inc(&inode->i_count);
1886 error = vfs_unlink(nd.dentry->d_inode, dentry);
1890 up(&nd.dentry->d_inode->i_sem);
1897 iput(inode); /* truncate the inode here */
1901 error = !dentry->d_inode ? -ENOENT :
1902 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1906 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
1908 int error = may_create(dir, dentry, NULL);
1913 if (!dir->i_op || !dir->i_op->symlink)
1916 error = security_inode_symlink(dir, dentry, oldname);
1921 error = dir->i_op->symlink(dir, dentry, oldname);
1923 inode_dir_notify(dir, DN_CREATE);
1924 security_inode_post_symlink(dir, dentry, oldname);
1929 asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
1935 from = getname(oldname);
1937 return PTR_ERR(from);
1938 to = getname(newname);
1939 error = PTR_ERR(to);
1941 struct dentry *dentry;
1942 struct nameidata nd;
1944 error = path_lookup(to, LOOKUP_PARENT, &nd);
1947 dentry = lookup_create(&nd, 0);
1948 error = PTR_ERR(dentry);
1949 if (!IS_ERR(dentry)) {
1950 error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
1953 up(&nd.dentry->d_inode->i_sem);
1962 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1964 struct inode *inode = old_dentry->d_inode;
1970 error = may_create(dir, new_dentry, NULL);
1974 if (dir->i_sb != inode->i_sb)
1978 * A link to an append-only or immutable file cannot be created.
1980 if (IS_APPEND(inode) || IS_IXORUNLINK(inode))
1982 if (!dir->i_op || !dir->i_op->link)
1984 if (S_ISDIR(old_dentry->d_inode->i_mode))
1987 error = security_inode_link(old_dentry, dir, new_dentry);
1991 down(&old_dentry->d_inode->i_sem);
1993 error = dir->i_op->link(old_dentry, dir, new_dentry);
1994 up(&old_dentry->d_inode->i_sem);
1996 inode_dir_notify(dir, DN_CREATE);
1997 security_inode_post_link(old_dentry, dir, new_dentry);
2003 * Hardlinks are often used in delicate situations. We avoid
2004 * security-related surprises by not following symlinks on the
2007 * We don't follow them on the oldname either to be compatible
2008 * with linux 2.0, and to avoid hard-linking to directories
2009 * and other special files. --ADM
2011 asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
2013 struct dentry *new_dentry;
2014 struct nameidata nd, old_nd;
2018 to = getname(newname);
2022 error = __user_walk(oldname, 0, &old_nd);
2025 error = path_lookup(to, LOOKUP_PARENT, &nd);
2029 if (old_nd.mnt != nd.mnt)
2031 new_dentry = lookup_create(&nd, 0);
2032 error = PTR_ERR(new_dentry);
2033 if (!IS_ERR(new_dentry)) {
2034 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
2037 up(&nd.dentry->d_inode->i_sem);
2041 path_release(&old_nd);
2049 * The worst of all namespace operations - renaming directory. "Perverted"
2050 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2052 * a) we can get into loop creation. Check is done in is_subdir().
2053 * b) race potential - two innocent renames can create a loop together.
2054 * That's where 4.4 screws up. Current fix: serialization on
2055 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
2057 * c) we have to lock _three_ objects - parents and victim (if it exists).
2058 * And that - after we got ->i_sem on parents (until then we don't know
2059 * whether the target exists). Solution: try to be smart with locking
2060 * order for inodes. We rely on the fact that tree topology may change
2061 * only under ->s_vfs_rename_sem _and_ that parent of the object we
2062 * move will be locked. Thus we can rank directories by the tree
2063 * (ancestors first) and rank all non-directories after them.
2064 * That works since everybody except rename does "lock parent, lookup,
2065 * lock child" and rename is under ->s_vfs_rename_sem.
2066 * HOWEVER, it relies on the assumption that any object with ->lookup()
2067 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2068 * we'd better make sure that there's no link(2) for them.
2069 * d) some filesystems don't support opened-but-unlinked directories,
2070 * either because of layout or because they are not ready to deal with
2071 * all cases correctly. The latter will be fixed (taking this sort of
2072 * stuff into VFS), but the former is not going away. Solution: the same
2073 * trick as in rmdir().
2074 * e) conversion from fhandle to dentry may come in the wrong moment - when
2075 * we are removing the target. Solution: we will have to grab ->i_sem
2076 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2077 * ->i_sem on parents, which works but leads to some truely excessive
2080 int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2081 struct inode *new_dir, struct dentry *new_dentry)
2084 struct inode *target;
2087 * If we are going to change the parent - check write permissions,
2088 * we'll need to flip '..'.
2090 if (new_dir != old_dir) {
2091 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2096 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2100 target = new_dentry->d_inode;
2102 down(&target->i_sem);
2103 d_unhash(new_dentry);
2105 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2108 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2111 target->i_flags |= S_DEAD;
2113 if (d_unhashed(new_dentry))
2114 d_rehash(new_dentry);
2118 d_move(old_dentry,new_dentry);
2119 security_inode_post_rename(old_dir, old_dentry,
2120 new_dir, new_dentry);
2125 int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2126 struct inode *new_dir, struct dentry *new_dentry)
2128 struct inode *target;
2131 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2136 target = new_dentry->d_inode;
2138 down(&target->i_sem);
2139 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2142 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2144 /* The following d_move() should become unconditional */
2145 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
2146 d_move(old_dentry, new_dentry);
2147 security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
2155 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2156 struct inode *new_dir, struct dentry *new_dentry)
2159 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2161 if (old_dentry->d_inode == new_dentry->d_inode)
2164 error = may_delete(old_dir, old_dentry, is_dir);
2168 if (!new_dentry->d_inode)
2169 error = may_create(new_dir, new_dentry, NULL);
2171 error = may_delete(new_dir, new_dentry, is_dir);
2175 if (!old_dir->i_op || !old_dir->i_op->rename)
2178 DQUOT_INIT(old_dir);
2179 DQUOT_INIT(new_dir);
2182 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2184 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2186 if (old_dir == new_dir)
2187 inode_dir_notify(old_dir, DN_RENAME);
2189 inode_dir_notify(old_dir, DN_DELETE);
2190 inode_dir_notify(new_dir, DN_CREATE);
2196 static inline int do_rename(const char * oldname, const char * newname)
2199 struct dentry * old_dir, * new_dir;
2200 struct dentry * old_dentry, *new_dentry;
2201 struct dentry * trap;
2202 struct nameidata oldnd, newnd;
2204 error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2208 error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2213 if (oldnd.mnt != newnd.mnt)
2216 old_dir = oldnd.dentry;
2218 if (oldnd.last_type != LAST_NORM)
2221 new_dir = newnd.dentry;
2222 if (newnd.last_type != LAST_NORM)
2225 trap = lock_rename(new_dir, old_dir);
2227 old_dentry = lookup_hash(&oldnd.last, old_dir);
2228 error = PTR_ERR(old_dentry);
2229 if (IS_ERR(old_dentry))
2231 /* source must exist */
2233 if (!old_dentry->d_inode)
2235 /* unless the source is a directory trailing slashes give -ENOTDIR */
2236 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2238 if (oldnd.last.name[oldnd.last.len])
2240 if (newnd.last.name[newnd.last.len])
2243 /* source should not be ancestor of target */
2245 if (old_dentry == trap)
2248 if (MNT_IS_RDONLY(newnd.mnt))
2250 new_dentry = lookup_hash(&newnd.last, new_dir);
2251 error = PTR_ERR(new_dentry);
2252 if (IS_ERR(new_dentry))
2254 /* target should not be an ancestor of source */
2256 if (new_dentry == trap)
2259 error = vfs_rename(old_dir->d_inode, old_dentry,
2260 new_dir->d_inode, new_dentry);
2266 unlock_rename(new_dir, old_dir);
2268 path_release(&newnd);
2270 path_release(&oldnd);
2275 asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
2281 from = getname(oldname);
2283 return PTR_ERR(from);
2284 to = getname(newname);
2285 error = PTR_ERR(to);
2287 error = do_rename(from,to);
2294 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2298 len = PTR_ERR(link);
2303 if (len > (unsigned) buflen)
2305 if (copy_to_user(buffer, link, len))
2312 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2313 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2314 * using) it for any given inode is up to filesystem.
2316 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2318 struct nameidata nd;
2321 res = dentry->d_inode->i_op->follow_link(dentry, &nd);
2323 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2324 if (dentry->d_inode->i_op->put_link)
2325 dentry->d_inode->i_op->put_link(dentry, &nd);
2330 int vfs_follow_link(struct nameidata *nd, const char *link)
2332 return __vfs_follow_link(nd, link);
2335 /* get the link contents into pagecache */
2336 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2339 struct address_space *mapping = dentry->d_inode->i_mapping;
2340 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2344 wait_on_page_locked(page);
2345 if (!PageUptodate(page))
2351 page_cache_release(page);
2352 return ERR_PTR(-EIO);
2358 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2360 struct page *page = NULL;
2361 char *s = page_getlink(dentry, &page);
2362 int res = vfs_readlink(dentry,buffer,buflen,s);
2365 page_cache_release(page);
2370 int page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2373 nd_set_link(nd, page_getlink(dentry, &page));
2377 void page_put_link(struct dentry *dentry, struct nameidata *nd)
2379 if (!IS_ERR(nd_get_link(nd))) {
2381 page = find_get_page(dentry->d_inode->i_mapping, 0);
2385 page_cache_release(page);
2386 page_cache_release(page);
2390 int page_follow_link(struct dentry *dentry, struct nameidata *nd)
2392 struct page *page = NULL;
2393 char *s = page_getlink(dentry, &page);
2394 int res = __vfs_follow_link(nd, s);
2397 page_cache_release(page);
2402 int page_symlink(struct inode *inode, const char *symname, int len)
2404 struct address_space *mapping = inode->i_mapping;
2405 struct page *page = grab_cache_page(mapping, 0);
2411 err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2414 kaddr = kmap_atomic(page, KM_USER0);
2415 memcpy(kaddr, symname, len-1);
2416 kunmap_atomic(kaddr, KM_USER0);
2417 mapping->a_ops->commit_write(NULL, page, 0, len-1);
2419 * Notice that we are _not_ going to block here - end of page is
2420 * unmapped, so this will only try to map the rest of page, see
2421 * that it is unmapped (typically even will not look into inode -
2422 * ->i_size will be enough for everything) and zero it out.
2423 * OTOH it's obviously correct and should make the page up-to-date.
2425 if (!PageUptodate(page)) {
2426 err = mapping->a_ops->readpage(NULL, page);
2427 wait_on_page_locked(page);
2431 page_cache_release(page);
2434 mark_inode_dirty(inode);
2438 page_cache_release(page);
2443 struct inode_operations page_symlink_inode_operations = {
2444 .readlink = generic_readlink,
2445 .follow_link = page_follow_link_light,
2446 .put_link = page_put_link,
2449 EXPORT_SYMBOL(__user_walk);
2450 EXPORT_SYMBOL(follow_down);
2451 EXPORT_SYMBOL(follow_up);
2452 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2453 EXPORT_SYMBOL(getname);
2454 EXPORT_SYMBOL(lock_rename);
2455 EXPORT_SYMBOL(lookup_create);
2456 EXPORT_SYMBOL(lookup_hash);
2457 EXPORT_SYMBOL(lookup_one_len);
2458 EXPORT_SYMBOL(page_follow_link);
2459 EXPORT_SYMBOL(page_follow_link_light);
2460 EXPORT_SYMBOL(page_put_link);
2461 EXPORT_SYMBOL(page_readlink);
2462 EXPORT_SYMBOL(page_symlink);
2463 EXPORT_SYMBOL(page_symlink_inode_operations);
2464 EXPORT_SYMBOL(path_lookup);
2465 EXPORT_SYMBOL(path_release);
2466 EXPORT_SYMBOL(path_walk);
2467 EXPORT_SYMBOL(permission);
2468 EXPORT_SYMBOL(unlock_rename);
2469 EXPORT_SYMBOL(vfs_create);
2470 EXPORT_SYMBOL(vfs_follow_link);
2471 EXPORT_SYMBOL(vfs_link);
2472 EXPORT_SYMBOL(vfs_mkdir);
2473 EXPORT_SYMBOL(vfs_mknod);
2474 EXPORT_SYMBOL(vfs_permission);
2475 EXPORT_SYMBOL(vfs_readlink);
2476 EXPORT_SYMBOL(vfs_rename);
2477 EXPORT_SYMBOL(vfs_rmdir);
2478 EXPORT_SYMBOL(vfs_symlink);
2479 EXPORT_SYMBOL(vfs_unlink);
2480 EXPORT_SYMBOL(generic_readlink);