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/syscalls.h>
29 #include <linux/mount.h>
30 #include <linux/audit.h>
31 #include <linux/proc_fs.h>
32 #include <linux/vserver/inode.h>
33 #include <linux/vserver/debug.h>
35 #include <asm/namei.h>
36 #include <asm/uaccess.h>
38 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
40 /* [Feb-1997 T. Schoebel-Theuer]
41 * Fundamental changes in the pathname lookup mechanisms (namei)
42 * were necessary because of omirr. The reason is that omirr needs
43 * to know the _real_ pathname, not the user-supplied one, in case
44 * of symlinks (and also when transname replacements occur).
46 * The new code replaces the old recursive symlink resolution with
47 * an iterative one (in case of non-nested symlink chains). It does
48 * this with calls to <fs>_follow_link().
49 * As a side effect, dir_namei(), _namei() and follow_link() are now
50 * replaced with a single function lookup_dentry() that can handle all
51 * the special cases of the former code.
53 * With the new dcache, the pathname is stored at each inode, at least as
54 * long as the refcount of the inode is positive. As a side effect, the
55 * size of the dcache depends on the inode cache and thus is dynamic.
57 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
58 * resolution to correspond with current state of the code.
60 * Note that the symlink resolution is not *completely* iterative.
61 * There is still a significant amount of tail- and mid- recursion in
62 * the algorithm. Also, note that <fs>_readlink() is not used in
63 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
64 * may return different results than <fs>_follow_link(). Many virtual
65 * filesystems (including /proc) exhibit this behavior.
68 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
69 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
70 * and the name already exists in form of a symlink, try to create the new
71 * name indicated by the symlink. The old code always complained that the
72 * name already exists, due to not following the symlink even if its target
73 * is nonexistent. The new semantics affects also mknod() and link() when
74 * the name is a symlink pointing to a non-existant name.
76 * I don't know which semantics is the right one, since I have no access
77 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
78 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
79 * "old" one. Personally, I think the new semantics is much more logical.
80 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
81 * file does succeed in both HP-UX and SunOs, but not in Solaris
82 * and in the old Linux semantics.
85 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
86 * semantics. See the comments in "open_namei" and "do_link" below.
88 * [10-Sep-98 Alan Modra] Another symlink change.
91 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
92 * inside the path - always follow.
93 * in the last component in creation/removal/renaming - never follow.
94 * if LOOKUP_FOLLOW passed - follow.
95 * if the pathname has trailing slashes - follow.
96 * otherwise - don't follow.
97 * (applied in that order).
99 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
100 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
101 * During the 2.4 we need to fix the userland stuff depending on it -
102 * hopefully we will be able to get rid of that wart in 2.5. So far only
103 * XEmacs seems to be relying on it...
106 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
107 * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives
108 * any extra contention...
111 /* In order to reduce some races, while at the same time doing additional
112 * checking and hopefully speeding things up, we copy filenames to the
113 * kernel data space before using them..
115 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
116 * PATH_MAX includes the nul terminator --RR.
118 static inline int do_getname(const char __user *filename, char *page)
121 unsigned long len = PATH_MAX;
123 if ((unsigned long) filename >= TASK_SIZE) {
124 if (!segment_eq(get_fs(), KERNEL_DS))
126 } else if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
127 len = TASK_SIZE - (unsigned long) filename;
129 retval = strncpy_from_user((char *)page, filename, len);
133 return -ENAMETOOLONG;
139 char * getname(const char __user * filename)
143 result = ERR_PTR(-ENOMEM);
146 int retval = do_getname(filename, tmp);
151 result = ERR_PTR(retval);
154 if (unlikely(current->audit_context) && !IS_ERR(result) && result)
155 audit_getname(result);
160 * generic_permission - check for access rights on a Posix-like filesystem
161 * @inode: inode to check access rights for
162 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
163 * @check_acl: optional callback to check for Posix ACLs
165 * Used to check for read/write/execute permissions on a file.
166 * We use "fsuid" for this, letting us set arbitrary permissions
167 * for filesystem access without changing the "normal" uids which
168 * are used for other things..
170 int generic_permission(struct inode *inode, int mask,
171 int (*check_acl)(struct inode *inode, int mask))
173 umode_t mode = inode->i_mode;
175 /* Prevent vservers from escaping chroot() barriers */
176 if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN))
179 if (mask & MAY_WRITE) {
181 * Nobody gets write access to a read-only fs.
183 if (IS_RDONLY(inode) &&
184 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
188 * Nobody gets write access to an immutable file.
190 if (IS_IMMUTABLE(inode))
194 if (current->fsuid == inode->i_uid)
197 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
198 int error = check_acl(inode, mask);
199 if (error == -EACCES)
200 goto check_capabilities;
201 else if (error != -EAGAIN)
205 if (in_group_p(inode->i_gid))
210 * If the DACs are ok we don't need any capability check.
212 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
217 * Read/write DACs are always overridable.
218 * Executable DACs are overridable if at least one exec bit is set.
220 if (!(mask & MAY_EXEC) ||
221 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
222 if (capable(CAP_DAC_OVERRIDE))
226 * Searching includes executable on directories, else just read.
228 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
229 if (capable(CAP_DAC_READ_SEARCH))
235 static inline int xid_permission(struct inode *inode, int mask, struct nameidata *nd)
237 if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN)) {
238 vxwprintk(1, "xid=%d did hit the barrier.",
242 if (inode->i_xid == 0)
244 if (vx_check(inode->i_xid, VX_ADMIN|VX_WATCH|VX_IDENT))
247 vxwprintk(1, "xid=%d denied access to %p[#%d,%lu] »%s«.",
248 vx_current_xid(), inode, inode->i_xid, inode->i_ino,
249 vxd_path(nd->dentry, nd->mnt));
253 int permission(struct inode * inode,int mask, struct nameidata *nd)
257 umode_t mode = inode->i_mode;
259 /* Ordinary permission routines do not understand MAY_APPEND. */
260 submask = mask & ~MAY_APPEND;
262 if (nd && (mask & MAY_WRITE) && MNT_IS_RDONLY(nd->mnt) &&
263 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
266 if ((retval = xid_permission(inode, mask, nd)))
269 if (inode->i_op && inode->i_op->permission)
270 retval = inode->i_op->permission(inode, submask, nd);
272 retval = generic_permission(inode, submask, NULL);
276 return security_inode_permission(inode, mask, nd);
280 * get_write_access() gets write permission for a file.
281 * put_write_access() releases this write permission.
282 * This is used for regular files.
283 * We cannot support write (and maybe mmap read-write shared) accesses and
284 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
285 * can have the following values:
286 * 0: no writers, no VM_DENYWRITE mappings
287 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
288 * > 0: (i_writecount) users are writing to the file.
290 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
291 * except for the cases where we don't hold i_writecount yet. Then we need to
292 * use {get,deny}_write_access() - these functions check the sign and refuse
293 * to do the change if sign is wrong. Exclusion between them is provided by
294 * the inode->i_lock spinlock.
297 int get_write_access(struct inode * inode)
299 spin_lock(&inode->i_lock);
300 if (atomic_read(&inode->i_writecount) < 0) {
301 spin_unlock(&inode->i_lock);
304 atomic_inc(&inode->i_writecount);
305 spin_unlock(&inode->i_lock);
310 int deny_write_access(struct file * file)
312 struct inode *inode = file->f_dentry->d_inode;
314 spin_lock(&inode->i_lock);
315 if (atomic_read(&inode->i_writecount) > 0) {
316 spin_unlock(&inode->i_lock);
319 atomic_dec(&inode->i_writecount);
320 spin_unlock(&inode->i_lock);
325 void path_release(struct nameidata *nd)
332 * umount() mustn't call path_release()/mntput() as that would clear
335 void path_release_on_umount(struct nameidata *nd)
342 * Internal lookup() using the new generic dcache.
345 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
347 struct dentry * dentry = __d_lookup(parent, name);
349 /* lockess __d_lookup may fail due to concurrent d_move()
350 * in some unrelated directory, so try with d_lookup
353 dentry = d_lookup(parent, name);
355 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
356 if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
365 * Short-cut version of permission(), for calling by
366 * path_walk(), when dcache lock is held. Combines parts
367 * of permission() and generic_permission(), and tests ONLY for
368 * MAY_EXEC permission.
370 * If appropriate, check DAC only. If not appropriate, or
371 * short-cut DAC fails, then call permission() to do more
372 * complete permission check.
374 static inline int exec_permission_lite(struct inode *inode,
375 struct nameidata *nd)
377 umode_t mode = inode->i_mode;
379 if (inode->i_op && inode->i_op->permission)
382 if (current->fsuid == inode->i_uid)
384 else if (in_group_p(inode->i_gid))
390 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
393 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
396 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
401 return security_inode_permission(inode, MAY_EXEC, nd);
405 * This is called when everything else fails, and we actually have
406 * to go to the low-level filesystem to find out what we should do..
408 * We get the directory semaphore, and after getting that we also
409 * make sure that nobody added the entry to the dcache in the meantime..
412 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
414 struct dentry * result;
415 struct inode *dir = parent->d_inode;
419 * First re-do the cached lookup just in case it was created
420 * while we waited for the directory semaphore..
422 * FIXME! This could use version numbering or similar to
423 * avoid unnecessary cache lookups.
425 * The "dcache_lock" is purely to protect the RCU list walker
426 * from concurrent renames at this point (we mustn't get false
427 * negatives from the RCU list walk here, unlike the optimistic
430 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
432 result = d_lookup(parent, name);
434 struct dentry * dentry = d_alloc(parent, name);
435 result = ERR_PTR(-ENOMEM);
437 result = dir->i_op->lookup(dir, dentry, nd);
448 * Uhhuh! Nasty case: the cache was re-populated while
449 * we waited on the semaphore. Need to revalidate.
452 if (result->d_op && result->d_op->d_revalidate) {
453 if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
455 result = ERR_PTR(-ENOENT);
461 static int __emul_lookup_dentry(const char *, struct nameidata *);
465 walk_init_root(const char *name, struct nameidata *nd)
467 read_lock(¤t->fs->lock);
468 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
469 nd->mnt = mntget(current->fs->altrootmnt);
470 nd->dentry = dget(current->fs->altroot);
471 read_unlock(¤t->fs->lock);
472 if (__emul_lookup_dentry(name,nd))
474 read_lock(¤t->fs->lock);
476 nd->mnt = mntget(current->fs->rootmnt);
477 nd->dentry = dget(current->fs->root);
478 read_unlock(¤t->fs->lock);
482 static inline int __vfs_follow_link(struct nameidata *nd, const char *link)
491 if (!walk_init_root(link, nd))
492 /* weird __emul_prefix() stuff did it */
495 res = link_path_walk(link, nd);
497 if (nd->depth || res || nd->last_type!=LAST_NORM)
500 * If it is an iterative symlinks resolution in open_namei() we
501 * have to copy the last component. And all that crap because of
502 * bloody create() on broken symlinks. Furrfu...
505 if (unlikely(!name)) {
509 strcpy(name, nd->last.name);
510 nd->last.name = name;
514 return PTR_ERR(link);
518 * This limits recursive symlink follows to 8, while
519 * limiting consecutive symlinks to 40.
521 * Without that kind of total limit, nasty chains of consecutive
522 * symlinks can cause almost arbitrarily long lookups.
524 static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd)
527 if (current->link_count >= MAX_NESTED_LINKS)
529 if (current->total_link_count >= 40)
531 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
533 err = security_inode_follow_link(dentry, nd);
536 current->link_count++;
537 current->total_link_count++;
539 touch_atime(nd->mnt, dentry);
540 nd_set_link(nd, NULL);
541 err = dentry->d_inode->i_op->follow_link(dentry, nd);
543 char *s = nd_get_link(nd);
545 err = __vfs_follow_link(nd, s);
546 if (dentry->d_inode->i_op->put_link)
547 dentry->d_inode->i_op->put_link(dentry, nd);
549 current->link_count--;
557 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
559 struct vfsmount *parent;
560 struct dentry *mountpoint;
561 spin_lock(&vfsmount_lock);
562 parent=(*mnt)->mnt_parent;
563 if (parent == *mnt) {
564 spin_unlock(&vfsmount_lock);
568 mountpoint=dget((*mnt)->mnt_mountpoint);
569 spin_unlock(&vfsmount_lock);
571 *dentry = mountpoint;
577 /* no need for dcache_lock, as serialization is taken care in
580 static int follow_mount(struct vfsmount **mnt, struct dentry **dentry)
583 while (d_mountpoint(*dentry)) {
584 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
590 *dentry = dget(mounted->mnt_root);
596 /* no need for dcache_lock, as serialization is taken care in
599 static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
601 struct vfsmount *mounted;
603 mounted = lookup_mnt(*mnt, *dentry);
608 *dentry = dget(mounted->mnt_root);
614 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
616 return __follow_down(mnt,dentry);
619 static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry)
622 struct vfsmount *parent;
623 struct dentry *old = *dentry;
625 read_lock(¤t->fs->lock);
626 if (*dentry == current->fs->root &&
627 *mnt == current->fs->rootmnt) {
628 read_unlock(¤t->fs->lock);
631 read_unlock(¤t->fs->lock);
632 spin_lock(&dcache_lock);
633 if (*dentry != (*mnt)->mnt_root) {
634 *dentry = dget((*dentry)->d_parent);
635 spin_unlock(&dcache_lock);
639 spin_unlock(&dcache_lock);
640 spin_lock(&vfsmount_lock);
641 parent = (*mnt)->mnt_parent;
642 if (parent == *mnt) {
643 spin_unlock(&vfsmount_lock);
647 *dentry = dget((*mnt)->mnt_mountpoint);
648 spin_unlock(&vfsmount_lock);
653 follow_mount(mnt, dentry);
657 struct vfsmount *mnt;
658 struct dentry *dentry;
662 * It's more convoluted than I'd like it to be, but... it's still fairly
663 * small and for now I'd prefer to have fast path as straight as possible.
664 * It _is_ time-critical.
666 static int do_lookup(struct nameidata *nd, struct qstr *name,
667 struct path *path, int atomic)
669 struct vfsmount *mnt = nd->mnt;
670 struct dentry *dentry = __d_lookup(nd->dentry, name);
675 if (dentry->d_op && dentry->d_op->d_revalidate)
676 goto need_revalidate;
677 inode = dentry->d_inode;
680 if (!vx_check(inode->i_xid, VX_WATCH|VX_HOSTID|VX_IDENT))
682 if (inode->i_sb->s_magic == PROC_SUPER_MAGIC) {
683 struct proc_dir_entry *de = PDE(inode);
685 if (de && !vx_hide_check(0, de->vx_flags))
690 path->dentry = dentry;
693 vxwprintk(1, "xid=%d did lookup hidden %p[#%d,%lu] »%s«.",
694 vx_current_xid(), inode, inode->i_xid, inode->i_ino,
695 vxd_path(dentry, mnt));
701 return -EWOULDBLOCKIO;
702 dentry = real_lookup(nd->dentry, name, nd);
709 return -EWOULDBLOCKIO;
710 if (dentry->d_op->d_revalidate(dentry, nd))
712 if (d_invalidate(dentry))
718 return PTR_ERR(dentry);
724 * This is the basic name resolution function, turning a pathname
725 * into the final dentry.
727 * We expect 'base' to be positive and a directory.
729 int fastcall link_path_walk(const char * name, struct nameidata *nd)
734 unsigned int lookup_flags = nd->flags;
736 atomic = (lookup_flags & LOOKUP_ATOMIC);
743 inode = nd->dentry->d_inode;
745 lookup_flags = LOOKUP_FOLLOW;
747 /* At this point we know we have a real path component. */
753 err = exec_permission_lite(inode, nd);
754 if (err == -EAGAIN) {
755 err = permission(inode, MAY_EXEC, nd);
761 c = *(const unsigned char *)name;
763 hash = init_name_hash();
766 hash = partial_name_hash(c, hash);
767 c = *(const unsigned char *)name;
768 } while (c && (c != '/'));
769 this.len = name - (const char *) this.name;
770 this.hash = end_name_hash(hash);
772 /* remove trailing slashes? */
775 while (*++name == '/');
777 goto last_with_slashes;
780 * "." and ".." are special - ".." especially so because it has
781 * to be able to know about the current root directory and
782 * parent relationships.
784 if (this.name[0] == '.') switch (this.len) {
788 if (this.name[1] != '.')
790 follow_dotdot(&nd->mnt, &nd->dentry);
791 inode = nd->dentry->d_inode;
797 * See if the low-level filesystem might want
798 * to use its own hash..
800 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
801 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
805 nd->flags |= LOOKUP_CONTINUE;
806 /* This does the actual lookups.. */
807 err = do_lookup(nd, &this, &next, atomic);
810 /* Check mountpoints.. */
811 follow_mount(&next.mnt, &next.dentry);
814 inode = next.dentry->d_inode;
821 if (inode->i_op->follow_link) {
823 err = do_follow_link(next.dentry, nd);
829 inode = nd->dentry->d_inode;
838 nd->dentry = next.dentry;
841 if (!inode->i_op->lookup)
844 /* here ends the main loop */
847 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
849 nd->flags &= ~LOOKUP_CONTINUE;
850 if (lookup_flags & LOOKUP_PARENT)
852 if (this.name[0] == '.') switch (this.len) {
856 if (this.name[1] != '.')
858 follow_dotdot(&nd->mnt, &nd->dentry);
859 inode = nd->dentry->d_inode;
864 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
865 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
869 err = do_lookup(nd, &this, &next, atomic);
872 follow_mount(&next.mnt, &next.dentry);
873 inode = next.dentry->d_inode;
874 if ((lookup_flags & LOOKUP_FOLLOW)
875 && inode && inode->i_op && inode->i_op->follow_link) {
877 err = do_follow_link(next.dentry, nd);
882 inode = nd->dentry->d_inode;
886 nd->dentry = next.dentry;
891 if (lookup_flags & LOOKUP_DIRECTORY) {
893 if (!inode->i_op || !inode->i_op->lookup)
899 nd->last_type = LAST_NORM;
900 if (this.name[0] != '.')
903 nd->last_type = LAST_DOT;
904 else if (this.len == 2 && this.name[1] == '.')
905 nd->last_type = LAST_DOTDOT;
910 * We bypassed the ordinary revalidation routines.
911 * We may need to check the cached dentry for staleness.
913 if (nd->dentry && nd->dentry->d_sb &&
914 (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
916 /* Note: we do not d_invalidate() */
917 if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
931 int fastcall path_walk(const char * name, struct nameidata *nd)
933 current->total_link_count = 0;
934 return link_path_walk(name, nd);
938 /* returns 1 if everything is done */
939 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
941 if (path_walk(name, nd))
942 return 0; /* something went wrong... */
944 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
945 struct dentry *old_dentry = nd->dentry;
946 struct vfsmount *old_mnt = nd->mnt;
947 struct qstr last = nd->last;
948 int last_type = nd->last_type;
950 * NAME was not found in alternate root or it's a directory. Try to find
951 * it in the normal root:
953 nd->last_type = LAST_ROOT;
954 read_lock(¤t->fs->lock);
955 nd->mnt = mntget(current->fs->rootmnt);
956 nd->dentry = dget(current->fs->root);
957 read_unlock(¤t->fs->lock);
958 if (path_walk(name, nd) == 0) {
959 if (nd->dentry->d_inode) {
966 nd->dentry = old_dentry;
969 nd->last_type = last_type;
974 void set_fs_altroot(void)
976 char *emul = __emul_prefix();
978 struct vfsmount *mnt = NULL, *oldmnt;
979 struct dentry *dentry = NULL, *olddentry;
984 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
990 write_lock(¤t->fs->lock);
991 oldmnt = current->fs->altrootmnt;
992 olddentry = current->fs->altroot;
993 current->fs->altrootmnt = mnt;
994 current->fs->altroot = dentry;
995 write_unlock(¤t->fs->lock);
1002 int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
1006 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1010 read_lock(¤t->fs->lock);
1012 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
1013 nd->mnt = mntget(current->fs->altrootmnt);
1014 nd->dentry = dget(current->fs->altroot);
1015 read_unlock(¤t->fs->lock);
1016 if (__emul_lookup_dentry(name,nd))
1018 read_lock(¤t->fs->lock);
1020 nd->mnt = mntget(current->fs->rootmnt);
1021 nd->dentry = dget(current->fs->root);
1023 nd->mnt = mntget(current->fs->pwdmnt);
1024 nd->dentry = dget(current->fs->pwd);
1026 read_unlock(¤t->fs->lock);
1027 current->total_link_count = 0;
1028 retval = link_path_walk(name, nd);
1029 if (unlikely(current->audit_context
1030 && nd && nd->dentry && nd->dentry->d_inode))
1032 nd->dentry->d_inode->i_ino,
1033 nd->dentry->d_inode->i_rdev);
1038 * Restricted form of lookup. Doesn't follow links, single-component only,
1039 * needs parent already locked. Doesn't follow mounts.
1042 static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
1044 struct dentry * dentry;
1045 struct inode *inode;
1048 inode = base->d_inode;
1049 err = permission(inode, MAY_EXEC, nd);
1050 dentry = ERR_PTR(err);
1055 * See if the low-level filesystem might want
1056 * to use its own hash..
1058 if (base->d_op && base->d_op->d_hash) {
1059 err = base->d_op->d_hash(base, name);
1060 dentry = ERR_PTR(err);
1065 dentry = cached_lookup(base, name, nd);
1067 struct dentry *new = d_alloc(base, name);
1068 dentry = ERR_PTR(-ENOMEM);
1071 dentry = inode->i_op->lookup(inode, new, nd);
1081 struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
1083 return __lookup_hash(name, base, NULL);
1087 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
1098 hash = init_name_hash();
1100 c = *(const unsigned char *)name++;
1101 if (c == '/' || c == '\0')
1103 hash = partial_name_hash(c, hash);
1105 this.hash = end_name_hash(hash);
1107 return lookup_hash(&this, base);
1109 return ERR_PTR(-EACCES);
1115 * is used by most simple commands to get the inode of a specified name.
1116 * Open, link etc use their own routines, but this is enough for things
1119 * namei exists in two versions: namei/lnamei. The only difference is
1120 * that namei follows links, while lnamei does not.
1123 int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1125 char *tmp = getname(name);
1126 int err = PTR_ERR(tmp);
1129 err = path_lookup(tmp, flags, nd);
1136 * It's inline, so penalty for filesystems that don't use sticky bit is
1139 static inline int check_sticky(struct inode *dir, struct inode *inode)
1141 if (!(dir->i_mode & S_ISVTX))
1143 if (inode->i_uid == current->fsuid)
1145 if (dir->i_uid == current->fsuid)
1147 return !capable(CAP_FOWNER);
1151 * Check whether we can remove a link victim from directory dir, check
1152 * whether the type of victim is right.
1153 * 1. We can't do it if dir is read-only (done in permission())
1154 * 2. We should have write and exec permissions on dir
1155 * 3. We can't remove anything from append-only dir
1156 * 4. We can't do anything with immutable dir (done in permission())
1157 * 5. If the sticky bit on dir is set we should either
1158 * a. be owner of dir, or
1159 * b. be owner of victim, or
1160 * c. have CAP_FOWNER capability
1161 * 6. If the victim is append-only or immutable we can't do antyhing with
1162 * links pointing to it.
1163 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1164 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1165 * 9. We can't remove a root or mountpoint.
1166 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1167 * nfs_async_unlink().
1169 static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1173 if (!victim->d_inode)
1176 BUG_ON(victim->d_parent->d_inode != dir);
1178 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1183 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1184 IS_IXORUNLINK(victim->d_inode))
1187 if (!S_ISDIR(victim->d_inode->i_mode))
1189 if (IS_ROOT(victim))
1191 } else if (S_ISDIR(victim->d_inode->i_mode))
1193 if (IS_DEADDIR(dir))
1195 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1200 /* Check whether we can create an object with dentry child in directory
1202 * 1. We can't do it if child already exists (open has special treatment for
1203 * this case, but since we are inlined it's OK)
1204 * 2. We can't do it if dir is read-only (done in permission())
1205 * 3. We should have write and exec permissions on dir
1206 * 4. We can't do it if dir is immutable (done in permission())
1208 static inline int may_create(struct inode *dir, struct dentry *child,
1209 struct nameidata *nd)
1213 if (IS_DEADDIR(dir))
1215 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1218 static inline int mnt_may_create(struct vfsmount *mnt, struct inode *dir, struct dentry *child) {
1221 if (IS_DEADDIR(dir))
1223 if (mnt->mnt_flags & MNT_RDONLY)
1228 static inline int mnt_may_unlink(struct vfsmount *mnt, struct inode *dir, struct dentry *child) {
1229 if (!child->d_inode)
1231 if (mnt->mnt_flags & MNT_RDONLY)
1237 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1240 * O_DIRECTORY translates into forcing a directory lookup.
1242 static inline int lookup_flags(unsigned int f)
1244 unsigned long retval = LOOKUP_FOLLOW;
1247 retval &= ~LOOKUP_FOLLOW;
1249 if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1250 retval &= ~LOOKUP_FOLLOW;
1252 if (f & O_DIRECTORY)
1253 retval |= LOOKUP_DIRECTORY;
1254 if (f & O_ATOMICLOOKUP)
1255 retval |= LOOKUP_ATOMIC;
1261 * p1 and p2 should be directories on the same fs.
1263 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1268 down(&p1->d_inode->i_sem);
1272 down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1274 for (p = p1; p->d_parent != p; p = p->d_parent) {
1275 if (p->d_parent == p2) {
1276 down(&p2->d_inode->i_sem);
1277 down(&p1->d_inode->i_sem);
1282 for (p = p2; p->d_parent != p; p = p->d_parent) {
1283 if (p->d_parent == p1) {
1284 down(&p1->d_inode->i_sem);
1285 down(&p2->d_inode->i_sem);
1290 down(&p1->d_inode->i_sem);
1291 down(&p2->d_inode->i_sem);
1295 void unlock_rename(struct dentry *p1, struct dentry *p2)
1297 up(&p1->d_inode->i_sem);
1299 up(&p2->d_inode->i_sem);
1300 up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1304 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1305 struct nameidata *nd)
1307 int error = may_create(dir, dentry, nd);
1312 if (!dir->i_op || !dir->i_op->create)
1313 return -EACCES; /* shouldn't it be ENOSYS? */
1316 error = security_inode_create(dir, dentry, mode);
1320 error = dir->i_op->create(dir, dentry, mode, nd);
1322 inode_dir_notify(dir, DN_CREATE);
1323 security_inode_post_create(dir, dentry, mode);
1328 int may_open(struct nameidata *nd, int acc_mode, int flag)
1330 struct dentry *dentry = nd->dentry;
1331 struct inode *inode = dentry->d_inode;
1337 if (S_ISLNK(inode->i_mode))
1340 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1343 error = permission(inode, acc_mode, nd);
1348 * FIFO's, sockets and device files are special: they don't
1349 * actually live on the filesystem itself, and as such you
1350 * can write to them even if the filesystem is read-only.
1352 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1354 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1355 if (nd->mnt->mnt_flags & MNT_NODEV)
1359 } else if ((IS_RDONLY(inode) || (nd && MNT_IS_RDONLY(nd->mnt)))
1360 && (flag & FMODE_WRITE))
1363 * An append-only file must be opened in append mode for writing.
1365 if (IS_APPEND(inode)) {
1366 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1372 /* O_NOATIME can only be set by the owner or superuser */
1373 if (flag & O_NOATIME)
1374 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1378 * Ensure there are no outstanding leases on the file.
1380 error = break_lease(inode, flag);
1384 if (flag & O_TRUNC) {
1385 error = get_write_access(inode);
1390 * Refuse to truncate files with mandatory locks held on them.
1392 error = locks_verify_locked(inode);
1396 error = do_truncate(dentry, 0);
1398 put_write_access(inode);
1402 if (flag & FMODE_WRITE)
1411 * namei for open - this is in fact almost the whole open-routine.
1413 * Note that the low bits of "flag" aren't the same as in the open
1414 * system call - they are 00 - no permissions needed
1415 * 01 - read permission needed
1416 * 10 - write permission needed
1417 * 11 - read/write permissions needed
1418 * which is a lot more logical, and also allows the "no perm" needed
1419 * for symlinks (where the permissions are checked later).
1422 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1424 int acc_mode, error = 0;
1425 struct dentry *dentry;
1429 acc_mode = ACC_MODE(flag);
1431 /* Allow the LSM permission hook to distinguish append
1432 access from general write access. */
1433 if (flag & O_APPEND)
1434 acc_mode |= MAY_APPEND;
1436 /* Fill in the open() intent data */
1437 nd->intent.open.flags = flag;
1438 nd->intent.open.create_mode = mode;
1441 * The simplest case - just a plain lookup.
1443 if (!(flag & O_CREAT)) {
1444 error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd);
1451 * Create - we need to know the parent.
1453 error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd);
1458 * We have the parent and last component. First of all, check
1459 * that we are not asked to creat(2) an obvious directory - that
1463 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1467 nd->flags &= ~LOOKUP_PARENT;
1468 down(&dir->d_inode->i_sem);
1469 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1472 error = PTR_ERR(dentry);
1473 if (IS_ERR(dentry)) {
1474 up(&dir->d_inode->i_sem);
1478 /* Negative dentry, just create the file */
1479 if (!dentry->d_inode) {
1480 if (!IS_POSIXACL(dir->d_inode))
1481 mode &= ~current->fs->umask;
1482 error = vfs_create(dir->d_inode, dentry, mode, nd);
1483 up(&dir->d_inode->i_sem);
1485 nd->dentry = dentry;
1488 /* Don't check for write permission, don't truncate */
1495 * It already exists.
1497 up(&dir->d_inode->i_sem);
1503 if (d_mountpoint(dentry)) {
1505 if (flag & O_NOFOLLOW)
1507 while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry));
1510 if (!dentry->d_inode)
1512 if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
1516 nd->dentry = dentry;
1518 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
1521 error = may_open(nd, acc_mode, flag);
1534 if (flag & O_NOFOLLOW)
1537 * This is subtle. Instead of calling do_follow_link() we do the
1538 * thing by hands. The reason is that this way we have zero link_count
1539 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1540 * After that we have the parent and last component, i.e.
1541 * we are in the same situation as after the first path_walk().
1542 * Well, almost - if the last component is normal we get its copy
1543 * stored in nd->last.name and we will have to putname() it when we
1544 * are done. Procfs-like symlinks just set LAST_BIND.
1546 nd->flags |= LOOKUP_PARENT;
1547 error = security_inode_follow_link(dentry, nd);
1550 touch_atime(nd->mnt, dentry);
1551 nd_set_link(nd, NULL);
1552 error = dentry->d_inode->i_op->follow_link(dentry, nd);
1554 char *s = nd_get_link(nd);
1556 error = __vfs_follow_link(nd, s);
1557 if (dentry->d_inode->i_op->put_link)
1558 dentry->d_inode->i_op->put_link(dentry, nd);
1563 nd->flags &= ~LOOKUP_PARENT;
1564 if (nd->last_type == LAST_BIND) {
1565 dentry = nd->dentry;
1569 if (nd->last_type != LAST_NORM)
1571 if (nd->last.name[nd->last.len]) {
1572 putname(nd->last.name);
1577 putname(nd->last.name);
1581 down(&dir->d_inode->i_sem);
1582 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1583 putname(nd->last.name);
1588 * lookup_create - lookup a dentry, creating it if it doesn't exist
1589 * @nd: nameidata info
1590 * @is_dir: directory flag
1592 * Simple function to lookup and return a dentry and create it
1593 * if it doesn't exist. Is SMP-safe.
1595 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1597 struct dentry *dentry;
1600 down(&nd->dentry->d_inode->i_sem);
1602 if (nd->last_type != LAST_NORM)
1604 nd->flags &= ~LOOKUP_PARENT;
1605 dentry = lookup_hash(&nd->last, nd->dentry);
1608 error = mnt_may_create(nd->mnt, nd->dentry->d_inode, dentry);
1612 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1619 return ERR_PTR(error);
1622 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1624 int error = may_create(dir, dentry, NULL);
1629 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1632 if (!dir->i_op || !dir->i_op->mknod)
1635 error = security_inode_mknod(dir, dentry, mode, dev);
1640 error = dir->i_op->mknod(dir, dentry, mode, dev);
1642 inode_dir_notify(dir, DN_CREATE);
1643 security_inode_post_mknod(dir, dentry, mode, dev);
1648 asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
1652 struct dentry * dentry;
1653 struct nameidata nd;
1657 tmp = getname(filename);
1659 return PTR_ERR(tmp);
1661 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1664 dentry = lookup_create(&nd, 0);
1665 error = PTR_ERR(dentry);
1667 if (!IS_POSIXACL(nd.dentry->d_inode))
1668 mode &= ~current->fs->umask;
1669 if (!IS_ERR(dentry)) {
1670 switch (mode & S_IFMT) {
1671 case 0: case S_IFREG:
1672 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1674 case S_IFCHR: case S_IFBLK:
1675 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1676 new_decode_dev(dev));
1678 case S_IFIFO: case S_IFSOCK:
1679 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1689 up(&nd.dentry->d_inode->i_sem);
1697 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1699 int error = may_create(dir, dentry, NULL);
1704 if (!dir->i_op || !dir->i_op->mkdir)
1707 mode &= (S_IRWXUGO|S_ISVTX);
1708 error = security_inode_mkdir(dir, dentry, mode);
1713 error = dir->i_op->mkdir(dir, dentry, mode);
1715 inode_dir_notify(dir, DN_CREATE);
1716 security_inode_post_mkdir(dir,dentry, mode);
1721 asmlinkage long sys_mkdir(const char __user * pathname, int mode)
1726 tmp = getname(pathname);
1727 error = PTR_ERR(tmp);
1729 struct dentry *dentry;
1730 struct nameidata nd;
1732 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1735 dentry = lookup_create(&nd, 1);
1736 error = PTR_ERR(dentry);
1737 if (!IS_ERR(dentry)) {
1738 if (!IS_POSIXACL(nd.dentry->d_inode))
1739 mode &= ~current->fs->umask;
1740 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1743 up(&nd.dentry->d_inode->i_sem);
1753 * We try to drop the dentry early: we should have
1754 * a usage count of 2 if we're the only user of this
1755 * dentry, and if that is true (possibly after pruning
1756 * the dcache), then we drop the dentry now.
1758 * A low-level filesystem can, if it choses, legally
1761 * if (!d_unhashed(dentry))
1764 * if it cannot handle the case of removing a directory
1765 * that is still in use by something else..
1767 void dentry_unhash(struct dentry *dentry)
1770 spin_lock(&dcache_lock);
1771 switch (atomic_read(&dentry->d_count)) {
1773 spin_unlock(&dcache_lock);
1774 shrink_dcache_parent(dentry);
1775 spin_lock(&dcache_lock);
1776 if (atomic_read(&dentry->d_count) != 2)
1781 spin_unlock(&dcache_lock);
1784 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1786 int error = may_delete(dir, dentry, 1);
1791 if (!dir->i_op || !dir->i_op->rmdir)
1796 down(&dentry->d_inode->i_sem);
1797 dentry_unhash(dentry);
1798 if (d_mountpoint(dentry))
1801 error = security_inode_rmdir(dir, dentry);
1803 error = dir->i_op->rmdir(dir, dentry);
1805 dentry->d_inode->i_flags |= S_DEAD;
1808 up(&dentry->d_inode->i_sem);
1810 inode_dir_notify(dir, DN_DELETE);
1818 asmlinkage long sys_rmdir(const char __user * pathname)
1822 struct dentry *dentry;
1823 struct nameidata nd;
1825 name = getname(pathname);
1827 return PTR_ERR(name);
1829 error = path_lookup(name, LOOKUP_PARENT, &nd);
1833 switch(nd.last_type) {
1844 down(&nd.dentry->d_inode->i_sem);
1845 dentry = lookup_hash(&nd.last, nd.dentry);
1846 error = PTR_ERR(dentry);
1847 if (!IS_ERR(dentry)) {
1848 error = mnt_may_unlink(nd.mnt, nd.dentry->d_inode, dentry);
1851 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1855 up(&nd.dentry->d_inode->i_sem);
1863 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1865 int error = may_delete(dir, dentry, 0);
1870 if (!dir->i_op || !dir->i_op->unlink)
1875 down(&dentry->d_inode->i_sem);
1876 if (d_mountpoint(dentry))
1879 error = security_inode_unlink(dir, dentry);
1881 error = dir->i_op->unlink(dir, dentry);
1883 up(&dentry->d_inode->i_sem);
1885 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1886 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1888 inode_dir_notify(dir, DN_DELETE);
1894 * Make sure that the actual truncation of the file will occur outside its
1895 * directory's i_sem. Truncate can take a long time if there is a lot of
1896 * writeout happening, and we don't want to prevent access to the directory
1897 * while waiting on the I/O.
1899 asmlinkage long sys_unlink(const char __user * pathname)
1903 struct dentry *dentry;
1904 struct nameidata nd;
1905 struct inode *inode = NULL;
1907 name = getname(pathname);
1909 return PTR_ERR(name);
1911 error = path_lookup(name, LOOKUP_PARENT, &nd);
1915 if (nd.last_type != LAST_NORM)
1917 down(&nd.dentry->d_inode->i_sem);
1918 dentry = lookup_hash(&nd.last, nd.dentry);
1919 error = PTR_ERR(dentry);
1920 if (!IS_ERR(dentry)) {
1921 /* Why not before? Because we want correct error value */
1922 if (nd.last.name[nd.last.len])
1924 error = mnt_may_unlink(nd.mnt, nd.dentry->d_inode, dentry);
1927 inode = dentry->d_inode;
1929 atomic_inc(&inode->i_count);
1930 error = vfs_unlink(nd.dentry->d_inode, dentry);
1934 up(&nd.dentry->d_inode->i_sem);
1936 iput(inode); /* truncate the inode here */
1944 error = !dentry->d_inode ? -ENOENT :
1945 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1949 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
1951 int error = may_create(dir, dentry, NULL);
1956 if (!dir->i_op || !dir->i_op->symlink)
1959 error = security_inode_symlink(dir, dentry, oldname);
1964 error = dir->i_op->symlink(dir, dentry, oldname);
1966 inode_dir_notify(dir, DN_CREATE);
1967 security_inode_post_symlink(dir, dentry, oldname);
1972 asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
1978 from = getname(oldname);
1980 return PTR_ERR(from);
1981 to = getname(newname);
1982 error = PTR_ERR(to);
1984 struct dentry *dentry;
1985 struct nameidata nd;
1987 error = path_lookup(to, LOOKUP_PARENT, &nd);
1990 dentry = lookup_create(&nd, 0);
1991 error = PTR_ERR(dentry);
1992 if (!IS_ERR(dentry)) {
1993 error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
1996 up(&nd.dentry->d_inode->i_sem);
2005 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2007 struct inode *inode = old_dentry->d_inode;
2013 error = may_create(dir, new_dentry, NULL);
2017 if (dir->i_sb != inode->i_sb)
2021 * A link to an append-only or immutable file cannot be created.
2023 if (IS_APPEND(inode) || IS_IXORUNLINK(inode))
2025 if (!dir->i_op || !dir->i_op->link)
2027 if (S_ISDIR(old_dentry->d_inode->i_mode))
2030 error = security_inode_link(old_dentry, dir, new_dentry);
2034 down(&old_dentry->d_inode->i_sem);
2036 error = dir->i_op->link(old_dentry, dir, new_dentry);
2037 up(&old_dentry->d_inode->i_sem);
2039 inode_dir_notify(dir, DN_CREATE);
2040 security_inode_post_link(old_dentry, dir, new_dentry);
2046 * Hardlinks are often used in delicate situations. We avoid
2047 * security-related surprises by not following symlinks on the
2050 * We don't follow them on the oldname either to be compatible
2051 * with linux 2.0, and to avoid hard-linking to directories
2052 * and other special files. --ADM
2054 asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
2056 struct dentry *new_dentry;
2057 struct nameidata nd, old_nd;
2061 to = getname(newname);
2065 error = __user_walk(oldname, 0, &old_nd);
2068 error = path_lookup(to, LOOKUP_PARENT, &nd);
2072 * We allow hard-links to be created to a bind-mount as long
2073 * as the bind-mount is not read-only. Checking for cross-dev
2074 * links is subsumed by the superblock check in vfs_link().
2077 if (MNT_IS_RDONLY(old_nd.mnt))
2079 new_dentry = lookup_create(&nd, 0);
2080 error = PTR_ERR(new_dentry);
2081 if (!IS_ERR(new_dentry)) {
2082 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
2085 up(&nd.dentry->d_inode->i_sem);
2089 path_release(&old_nd);
2097 * The worst of all namespace operations - renaming directory. "Perverted"
2098 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2100 * a) we can get into loop creation. Check is done in is_subdir().
2101 * b) race potential - two innocent renames can create a loop together.
2102 * That's where 4.4 screws up. Current fix: serialization on
2103 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
2105 * c) we have to lock _three_ objects - parents and victim (if it exists).
2106 * And that - after we got ->i_sem on parents (until then we don't know
2107 * whether the target exists). Solution: try to be smart with locking
2108 * order for inodes. We rely on the fact that tree topology may change
2109 * only under ->s_vfs_rename_sem _and_ that parent of the object we
2110 * move will be locked. Thus we can rank directories by the tree
2111 * (ancestors first) and rank all non-directories after them.
2112 * That works since everybody except rename does "lock parent, lookup,
2113 * lock child" and rename is under ->s_vfs_rename_sem.
2114 * HOWEVER, it relies on the assumption that any object with ->lookup()
2115 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2116 * we'd better make sure that there's no link(2) for them.
2117 * d) some filesystems don't support opened-but-unlinked directories,
2118 * either because of layout or because they are not ready to deal with
2119 * all cases correctly. The latter will be fixed (taking this sort of
2120 * stuff into VFS), but the former is not going away. Solution: the same
2121 * trick as in rmdir().
2122 * e) conversion from fhandle to dentry may come in the wrong moment - when
2123 * we are removing the target. Solution: we will have to grab ->i_sem
2124 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2125 * ->i_sem on parents, which works but leads to some truely excessive
2128 int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2129 struct inode *new_dir, struct dentry *new_dentry)
2132 struct inode *target;
2135 * If we are going to change the parent - check write permissions,
2136 * we'll need to flip '..'.
2138 if (new_dir != old_dir) {
2139 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2144 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2148 target = new_dentry->d_inode;
2150 down(&target->i_sem);
2151 dentry_unhash(new_dentry);
2153 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2156 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2159 target->i_flags |= S_DEAD;
2161 if (d_unhashed(new_dentry))
2162 d_rehash(new_dentry);
2166 d_move(old_dentry,new_dentry);
2167 security_inode_post_rename(old_dir, old_dentry,
2168 new_dir, new_dentry);
2173 int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2174 struct inode *new_dir, struct dentry *new_dentry)
2176 struct inode *target;
2179 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2184 target = new_dentry->d_inode;
2186 down(&target->i_sem);
2187 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2190 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2192 /* The following d_move() should become unconditional */
2193 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
2194 d_move(old_dentry, new_dentry);
2195 security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
2203 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2204 struct inode *new_dir, struct dentry *new_dentry)
2207 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2209 if (old_dentry->d_inode == new_dentry->d_inode)
2212 error = may_delete(old_dir, old_dentry, is_dir);
2216 if (!new_dentry->d_inode)
2217 error = may_create(new_dir, new_dentry, NULL);
2219 error = may_delete(new_dir, new_dentry, is_dir);
2223 if (!old_dir->i_op || !old_dir->i_op->rename)
2226 DQUOT_INIT(old_dir);
2227 DQUOT_INIT(new_dir);
2230 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2232 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2234 if (old_dir == new_dir)
2235 inode_dir_notify(old_dir, DN_RENAME);
2237 inode_dir_notify(old_dir, DN_DELETE);
2238 inode_dir_notify(new_dir, DN_CREATE);
2244 static inline int do_rename(const char * oldname, const char * newname)
2247 struct dentry * old_dir, * new_dir;
2248 struct dentry * old_dentry, *new_dentry;
2249 struct dentry * trap;
2250 struct nameidata oldnd, newnd;
2252 error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2256 error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2261 if (oldnd.mnt != newnd.mnt)
2264 old_dir = oldnd.dentry;
2266 if (oldnd.last_type != LAST_NORM)
2269 new_dir = newnd.dentry;
2270 if (newnd.last_type != LAST_NORM)
2273 trap = lock_rename(new_dir, old_dir);
2275 old_dentry = lookup_hash(&oldnd.last, old_dir);
2276 error = PTR_ERR(old_dentry);
2277 if (IS_ERR(old_dentry))
2279 /* source must exist */
2281 if (!old_dentry->d_inode)
2283 /* unless the source is a directory trailing slashes give -ENOTDIR */
2284 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2286 if (oldnd.last.name[oldnd.last.len])
2288 if (newnd.last.name[newnd.last.len])
2291 /* source should not be ancestor of target */
2293 if (old_dentry == trap)
2296 if (MNT_IS_RDONLY(newnd.mnt))
2298 new_dentry = lookup_hash(&newnd.last, new_dir);
2299 error = PTR_ERR(new_dentry);
2300 if (IS_ERR(new_dentry))
2302 /* target should not be an ancestor of source */
2304 if (new_dentry == trap)
2307 error = vfs_rename(old_dir->d_inode, old_dentry,
2308 new_dir->d_inode, new_dentry);
2314 unlock_rename(new_dir, old_dir);
2316 path_release(&newnd);
2318 path_release(&oldnd);
2323 asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
2329 from = getname(oldname);
2331 return PTR_ERR(from);
2332 to = getname(newname);
2333 error = PTR_ERR(to);
2335 error = do_rename(from,to);
2342 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2346 len = PTR_ERR(link);
2351 if (len > (unsigned) buflen)
2353 if (copy_to_user(buffer, link, len))
2360 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2361 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2362 * using) it for any given inode is up to filesystem.
2364 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2366 struct nameidata nd;
2369 res = dentry->d_inode->i_op->follow_link(dentry, &nd);
2371 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2372 if (dentry->d_inode->i_op->put_link)
2373 dentry->d_inode->i_op->put_link(dentry, &nd);
2378 int vfs_follow_link(struct nameidata *nd, const char *link)
2380 return __vfs_follow_link(nd, link);
2383 /* get the link contents into pagecache */
2384 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2387 struct address_space *mapping = dentry->d_inode->i_mapping;
2388 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2392 wait_on_page_locked(page);
2393 if (!PageUptodate(page))
2399 page_cache_release(page);
2400 return ERR_PTR(-EIO);
2406 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2408 struct page *page = NULL;
2409 char *s = page_getlink(dentry, &page);
2410 int res = vfs_readlink(dentry,buffer,buflen,s);
2413 page_cache_release(page);
2418 int page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2421 nd_set_link(nd, page_getlink(dentry, &page));
2425 void page_put_link(struct dentry *dentry, struct nameidata *nd)
2427 if (!IS_ERR(nd_get_link(nd))) {
2429 page = find_get_page(dentry->d_inode->i_mapping, 0);
2433 page_cache_release(page);
2434 page_cache_release(page);
2438 int page_symlink(struct inode *inode, const char *symname, int len)
2440 struct address_space *mapping = inode->i_mapping;
2441 struct page *page = grab_cache_page(mapping, 0);
2447 err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2450 kaddr = kmap_atomic(page, KM_USER0);
2451 memcpy(kaddr, symname, len-1);
2452 kunmap_atomic(kaddr, KM_USER0);
2453 mapping->a_ops->commit_write(NULL, page, 0, len-1);
2455 * Notice that we are _not_ going to block here - end of page is
2456 * unmapped, so this will only try to map the rest of page, see
2457 * that it is unmapped (typically even will not look into inode -
2458 * ->i_size will be enough for everything) and zero it out.
2459 * OTOH it's obviously correct and should make the page up-to-date.
2461 if (!PageUptodate(page)) {
2462 err = mapping->a_ops->readpage(NULL, page);
2463 wait_on_page_locked(page);
2467 page_cache_release(page);
2470 mark_inode_dirty(inode);
2474 page_cache_release(page);
2479 struct inode_operations page_symlink_inode_operations = {
2480 .readlink = generic_readlink,
2481 .follow_link = page_follow_link_light,
2482 .put_link = page_put_link,
2485 EXPORT_SYMBOL(__user_walk);
2486 EXPORT_SYMBOL(follow_down);
2487 EXPORT_SYMBOL(follow_up);
2488 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2489 EXPORT_SYMBOL(getname);
2490 EXPORT_SYMBOL(lock_rename);
2491 EXPORT_SYMBOL(lookup_hash);
2492 EXPORT_SYMBOL(lookup_one_len);
2493 EXPORT_SYMBOL(page_follow_link_light);
2494 EXPORT_SYMBOL(page_put_link);
2495 EXPORT_SYMBOL(page_readlink);
2496 EXPORT_SYMBOL(page_symlink);
2497 EXPORT_SYMBOL(page_symlink_inode_operations);
2498 EXPORT_SYMBOL(path_lookup);
2499 EXPORT_SYMBOL(path_release);
2500 EXPORT_SYMBOL(path_walk);
2501 EXPORT_SYMBOL(permission);
2502 EXPORT_SYMBOL(unlock_rename);
2503 EXPORT_SYMBOL(vfs_create);
2504 EXPORT_SYMBOL(vfs_follow_link);
2505 EXPORT_SYMBOL(vfs_link);
2506 EXPORT_SYMBOL(vfs_mkdir);
2507 EXPORT_SYMBOL(vfs_mknod);
2508 EXPORT_SYMBOL(generic_permission);
2509 EXPORT_SYMBOL(vfs_readlink);
2510 EXPORT_SYMBOL(vfs_rename);
2511 EXPORT_SYMBOL(vfs_rmdir);
2512 EXPORT_SYMBOL(vfs_symlink);
2513 EXPORT_SYMBOL(vfs_unlink);
2514 EXPORT_SYMBOL(dentry_unhash);
2515 EXPORT_SYMBOL(generic_readlink);