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 /* Prevent vservers from escaping chroot() barriers */
169 if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN))
172 if (mask & MAY_WRITE) {
174 * Nobody gets write access to a read-only fs.
176 if (IS_RDONLY(inode) &&
177 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
181 * Nobody gets write access to an immutable file.
183 if (IS_IMMUTABLE(inode))
187 if (current->fsuid == inode->i_uid)
189 else if (in_group_p(inode->i_gid))
193 * If the DACs are ok we don't need any capability check.
195 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
199 * Read/write DACs are always overridable.
200 * Executable DACs are overridable if at least one exec bit is set.
202 if (!(mask & MAY_EXEC) ||
203 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
204 if (capable(CAP_DAC_OVERRIDE))
208 * Searching includes executable on directories, else just read.
210 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
211 if (capable(CAP_DAC_READ_SEARCH))
217 static inline int xid_permission(struct inode *inode, int mask, struct nameidata *nd)
219 if (inode->i_xid == 0)
221 if (vx_check(inode->i_xid, VX_ADMIN|VX_WATCH|VX_IDENT))
224 printk("VSW: xid=%d denied access to %p[#%d,%lu] »%*s«.\n",
225 vx_current_xid(), inode, inode->i_xid, inode->i_ino,
226 nd->dentry->d_name.len, nd->dentry->d_name.name);
231 int permission(struct inode * inode,int mask, struct nameidata *nd)
235 umode_t mode = inode->i_mode;
237 /* Ordinary permission routines do not understand MAY_APPEND. */
238 submask = mask & ~MAY_APPEND;
240 if (nd && (mask & MAY_WRITE) && MNT_IS_RDONLY(nd->mnt) &&
241 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
244 if ((retval = xid_permission(inode, mask, nd)))
246 if (inode->i_op && inode->i_op->permission)
247 retval = inode->i_op->permission(inode, submask, nd);
249 retval = vfs_permission(inode, submask);
253 return security_inode_permission(inode, mask, nd);
257 * get_write_access() gets write permission for a file.
258 * put_write_access() releases this write permission.
259 * This is used for regular files.
260 * We cannot support write (and maybe mmap read-write shared) accesses and
261 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
262 * can have the following values:
263 * 0: no writers, no VM_DENYWRITE mappings
264 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
265 * > 0: (i_writecount) users are writing to the file.
267 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
268 * except for the cases where we don't hold i_writecount yet. Then we need to
269 * use {get,deny}_write_access() - these functions check the sign and refuse
270 * to do the change if sign is wrong. Exclusion between them is provided by
271 * the inode->i_lock spinlock.
274 int get_write_access(struct inode * inode)
276 spin_lock(&inode->i_lock);
277 if (atomic_read(&inode->i_writecount) < 0) {
278 spin_unlock(&inode->i_lock);
281 atomic_inc(&inode->i_writecount);
282 spin_unlock(&inode->i_lock);
287 int deny_write_access(struct file * file)
289 struct inode *inode = file->f_dentry->d_inode;
291 spin_lock(&inode->i_lock);
292 if (atomic_read(&inode->i_writecount) > 0) {
293 spin_unlock(&inode->i_lock);
296 atomic_dec(&inode->i_writecount);
297 spin_unlock(&inode->i_lock);
302 void path_release(struct nameidata *nd)
309 * umount() mustn't call path_release()/mntput() as that would clear
312 void path_release_on_umount(struct nameidata *nd)
319 * Internal lookup() using the new generic dcache.
322 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
324 struct dentry * dentry = __d_lookup(parent, name);
326 /* lockess __d_lookup may fail due to concurrent d_move()
327 * in some unrelated directory, so try with d_lookup
330 dentry = d_lookup(parent, name);
332 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
333 if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
342 * Short-cut version of permission(), for calling by
343 * path_walk(), when dcache lock is held. Combines parts
344 * of permission() and vfs_permission(), and tests ONLY for
345 * MAY_EXEC permission.
347 * If appropriate, check DAC only. If not appropriate, or
348 * short-cut DAC fails, then call permission() to do more
349 * complete permission check.
351 static inline int exec_permission_lite(struct inode *inode,
352 struct nameidata *nd)
354 umode_t mode = inode->i_mode;
356 if (inode->i_op && inode->i_op->permission)
359 if (current->fsuid == inode->i_uid)
361 else if (in_group_p(inode->i_gid))
367 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
370 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
373 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
378 return security_inode_permission(inode, MAY_EXEC, nd);
382 * This is called when everything else fails, and we actually have
383 * to go to the low-level filesystem to find out what we should do..
385 * We get the directory semaphore, and after getting that we also
386 * make sure that nobody added the entry to the dcache in the meantime..
389 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
391 struct dentry * result;
392 struct inode *dir = parent->d_inode;
396 * First re-do the cached lookup just in case it was created
397 * while we waited for the directory semaphore..
399 * FIXME! This could use version numbering or similar to
400 * avoid unnecessary cache lookups.
402 * The "dcache_lock" is purely to protect the RCU list walker
403 * from concurrent renames at this point (we mustn't get false
404 * negatives from the RCU list walk here, unlike the optimistic
407 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
409 result = d_lookup(parent, name);
411 struct dentry * dentry = d_alloc(parent, name);
412 result = ERR_PTR(-ENOMEM);
414 result = dir->i_op->lookup(dir, dentry, nd);
425 * Uhhuh! Nasty case: the cache was re-populated while
426 * we waited on the semaphore. Need to revalidate.
429 if (result->d_op && result->d_op->d_revalidate) {
430 if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
432 result = ERR_PTR(-ENOENT);
438 static int __emul_lookup_dentry(const char *, struct nameidata *);
442 walk_init_root(const char *name, struct nameidata *nd)
444 read_lock(¤t->fs->lock);
445 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
446 nd->mnt = mntget(current->fs->altrootmnt);
447 nd->dentry = dget(current->fs->altroot);
448 read_unlock(¤t->fs->lock);
449 if (__emul_lookup_dentry(name,nd))
451 read_lock(¤t->fs->lock);
453 nd->mnt = mntget(current->fs->rootmnt);
454 nd->dentry = dget(current->fs->root);
455 read_unlock(¤t->fs->lock);
459 static inline int __vfs_follow_link(struct nameidata *nd, const char *link)
468 if (!walk_init_root(link, nd))
469 /* weird __emul_prefix() stuff did it */
472 res = link_path_walk(link, nd);
474 if (nd->depth || res || nd->last_type!=LAST_NORM)
477 * If it is an iterative symlinks resolution in open_namei() we
478 * have to copy the last component. And all that crap because of
479 * bloody create() on broken symlinks. Furrfu...
482 if (unlikely(!name)) {
486 strcpy(name, nd->last.name);
487 nd->last.name = name;
491 return PTR_ERR(link);
495 * This limits recursive symlink follows to 8, while
496 * limiting consecutive symlinks to 40.
498 * Without that kind of total limit, nasty chains of consecutive
499 * symlinks can cause almost arbitrarily long lookups.
501 static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd)
504 if (current->link_count >= MAX_NESTED_LINKS)
506 if (current->total_link_count >= 40)
508 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
510 err = security_inode_follow_link(dentry, nd);
513 current->link_count++;
514 current->total_link_count++;
516 touch_atime(nd->mnt, dentry);
517 nd_set_link(nd, NULL);
518 err = dentry->d_inode->i_op->follow_link(dentry, nd);
520 char *s = nd_get_link(nd);
522 err = __vfs_follow_link(nd, s);
523 if (dentry->d_inode->i_op->put_link)
524 dentry->d_inode->i_op->put_link(dentry, nd);
526 current->link_count--;
534 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
536 struct vfsmount *parent;
537 struct dentry *mountpoint;
538 spin_lock(&vfsmount_lock);
539 parent=(*mnt)->mnt_parent;
540 if (parent == *mnt) {
541 spin_unlock(&vfsmount_lock);
545 mountpoint=dget((*mnt)->mnt_mountpoint);
546 spin_unlock(&vfsmount_lock);
548 *dentry = mountpoint;
554 /* no need for dcache_lock, as serialization is taken care in
557 static int follow_mount(struct vfsmount **mnt, struct dentry **dentry)
560 while (d_mountpoint(*dentry)) {
561 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
567 *dentry = dget(mounted->mnt_root);
573 /* no need for dcache_lock, as serialization is taken care in
576 static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
578 struct vfsmount *mounted;
580 mounted = lookup_mnt(*mnt, *dentry);
585 *dentry = dget(mounted->mnt_root);
591 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
593 return __follow_down(mnt,dentry);
596 static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry)
599 struct vfsmount *parent;
600 struct dentry *old = *dentry;
602 read_lock(¤t->fs->lock);
603 if (*dentry == current->fs->root &&
604 *mnt == current->fs->rootmnt) {
605 read_unlock(¤t->fs->lock);
608 read_unlock(¤t->fs->lock);
609 spin_lock(&dcache_lock);
610 if (*dentry != (*mnt)->mnt_root) {
611 *dentry = dget((*dentry)->d_parent);
612 spin_unlock(&dcache_lock);
616 spin_unlock(&dcache_lock);
617 spin_lock(&vfsmount_lock);
618 parent = (*mnt)->mnt_parent;
619 if (parent == *mnt) {
620 spin_unlock(&vfsmount_lock);
624 *dentry = dget((*mnt)->mnt_mountpoint);
625 spin_unlock(&vfsmount_lock);
630 follow_mount(mnt, dentry);
634 struct vfsmount *mnt;
635 struct dentry *dentry;
639 * It's more convoluted than I'd like it to be, but... it's still fairly
640 * small and for now I'd prefer to have fast path as straight as possible.
641 * It _is_ time-critical.
643 static int do_lookup(struct nameidata *nd, struct qstr *name,
646 struct vfsmount *mnt = nd->mnt;
647 struct dentry *dentry = __d_lookup(nd->dentry, name);
651 if (dentry->d_op && dentry->d_op->d_revalidate)
652 goto need_revalidate;
655 path->dentry = dentry;
659 dentry = real_lookup(nd->dentry, name, nd);
665 if (dentry->d_op->d_revalidate(dentry, nd))
667 if (d_invalidate(dentry))
673 return PTR_ERR(dentry);
679 * This is the basic name resolution function, turning a pathname
680 * into the final dentry.
682 * We expect 'base' to be positive and a directory.
684 int fastcall link_path_walk(const char * name, struct nameidata *nd)
689 unsigned int lookup_flags = nd->flags;
691 atomic = (lookup_flags & LOOKUP_ATOMIC);
698 inode = nd->dentry->d_inode;
700 lookup_flags = LOOKUP_FOLLOW;
702 /* At this point we know we have a real path component. */
708 err = exec_permission_lite(inode, nd);
709 if (err == -EAGAIN) {
710 err = permission(inode, MAY_EXEC, nd);
716 c = *(const unsigned char *)name;
718 hash = init_name_hash();
721 hash = partial_name_hash(c, hash);
722 c = *(const unsigned char *)name;
723 } while (c && (c != '/'));
724 this.len = name - (const char *) this.name;
725 this.hash = end_name_hash(hash);
727 /* remove trailing slashes? */
730 while (*++name == '/');
732 goto last_with_slashes;
735 * "." and ".." are special - ".." especially so because it has
736 * to be able to know about the current root directory and
737 * parent relationships.
739 if (this.name[0] == '.') switch (this.len) {
743 if (this.name[1] != '.')
745 follow_dotdot(&nd->mnt, &nd->dentry);
746 inode = nd->dentry->d_inode;
752 * See if the low-level filesystem might want
753 * to use its own hash..
755 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
756 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
760 err = -EWOULDBLOCKIO;
763 nd->flags |= LOOKUP_CONTINUE;
764 /* This does the actual lookups.. */
765 err = do_lookup(nd, &this, &next);
768 /* Check mountpoints.. */
769 follow_mount(&next.mnt, &next.dentry);
772 inode = next.dentry->d_inode;
779 if (inode->i_op->follow_link) {
781 err = do_follow_link(next.dentry, nd);
787 inode = nd->dentry->d_inode;
796 nd->dentry = next.dentry;
799 if (!inode->i_op->lookup)
802 /* here ends the main loop */
805 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
807 nd->flags &= ~LOOKUP_CONTINUE;
808 if (lookup_flags & LOOKUP_PARENT)
810 if (this.name[0] == '.') switch (this.len) {
814 if (this.name[1] != '.')
816 follow_dotdot(&nd->mnt, &nd->dentry);
817 inode = nd->dentry->d_inode;
822 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
823 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
827 err = -EWOULDBLOCKIO;
830 err = do_lookup(nd, &this, &next);
833 follow_mount(&next.mnt, &next.dentry);
834 inode = next.dentry->d_inode;
835 if ((lookup_flags & LOOKUP_FOLLOW)
836 && inode && inode->i_op && inode->i_op->follow_link) {
838 err = do_follow_link(next.dentry, nd);
843 inode = nd->dentry->d_inode;
847 nd->dentry = next.dentry;
852 if (lookup_flags & LOOKUP_DIRECTORY) {
854 if (!inode->i_op || !inode->i_op->lookup)
860 nd->last_type = LAST_NORM;
861 if (this.name[0] != '.')
864 nd->last_type = LAST_DOT;
865 else if (this.len == 2 && this.name[1] == '.')
866 nd->last_type = LAST_DOTDOT;
871 * We bypassed the ordinary revalidation routines.
872 * We may need to check the cached dentry for staleness.
874 if (nd->dentry && nd->dentry->d_sb &&
875 (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
877 /* Note: we do not d_invalidate() */
878 if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
892 int fastcall path_walk(const char * name, struct nameidata *nd)
894 current->total_link_count = 0;
895 return link_path_walk(name, nd);
899 /* returns 1 if everything is done */
900 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
902 if (path_walk(name, nd))
903 return 0; /* something went wrong... */
905 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
906 struct dentry *old_dentry = nd->dentry;
907 struct vfsmount *old_mnt = nd->mnt;
908 struct qstr last = nd->last;
909 int last_type = nd->last_type;
911 * NAME was not found in alternate root or it's a directory. Try to find
912 * it in the normal root:
914 nd->last_type = LAST_ROOT;
915 read_lock(¤t->fs->lock);
916 nd->mnt = mntget(current->fs->rootmnt);
917 nd->dentry = dget(current->fs->root);
918 read_unlock(¤t->fs->lock);
919 if (path_walk(name, nd) == 0) {
920 if (nd->dentry->d_inode) {
927 nd->dentry = old_dentry;
930 nd->last_type = last_type;
935 void set_fs_altroot(void)
937 char *emul = __emul_prefix();
939 struct vfsmount *mnt = NULL, *oldmnt;
940 struct dentry *dentry = NULL, *olddentry;
945 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
951 write_lock(¤t->fs->lock);
952 oldmnt = current->fs->altrootmnt;
953 olddentry = current->fs->altroot;
954 current->fs->altrootmnt = mnt;
955 current->fs->altroot = dentry;
956 write_unlock(¤t->fs->lock);
963 int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
967 nd->last_type = LAST_ROOT; /* if there are only slashes... */
971 read_lock(¤t->fs->lock);
973 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
974 nd->mnt = mntget(current->fs->altrootmnt);
975 nd->dentry = dget(current->fs->altroot);
976 read_unlock(¤t->fs->lock);
977 if (__emul_lookup_dentry(name,nd))
979 read_lock(¤t->fs->lock);
981 nd->mnt = mntget(current->fs->rootmnt);
982 nd->dentry = dget(current->fs->root);
984 nd->mnt = mntget(current->fs->pwdmnt);
985 nd->dentry = dget(current->fs->pwd);
987 read_unlock(¤t->fs->lock);
988 current->total_link_count = 0;
989 retval = link_path_walk(name, nd);
990 if (unlikely(current->audit_context
991 && nd && nd->dentry && nd->dentry->d_inode))
993 nd->dentry->d_inode->i_ino,
994 nd->dentry->d_inode->i_rdev);
999 * Restricted form of lookup. Doesn't follow links, single-component only,
1000 * needs parent already locked. Doesn't follow mounts.
1003 static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
1005 struct dentry * dentry;
1006 struct inode *inode;
1009 inode = base->d_inode;
1010 err = permission(inode, MAY_EXEC, nd);
1011 dentry = ERR_PTR(err);
1016 * See if the low-level filesystem might want
1017 * to use its own hash..
1019 if (base->d_op && base->d_op->d_hash) {
1020 err = base->d_op->d_hash(base, name);
1021 dentry = ERR_PTR(err);
1026 dentry = cached_lookup(base, name, nd);
1028 struct dentry *new = d_alloc(base, name);
1029 dentry = ERR_PTR(-ENOMEM);
1032 dentry = inode->i_op->lookup(inode, new, nd);
1042 struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
1044 return __lookup_hash(name, base, NULL);
1048 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
1059 hash = init_name_hash();
1061 c = *(const unsigned char *)name++;
1062 if (c == '/' || c == '\0')
1064 hash = partial_name_hash(c, hash);
1066 this.hash = end_name_hash(hash);
1068 return lookup_hash(&this, base);
1070 return ERR_PTR(-EACCES);
1076 * is used by most simple commands to get the inode of a specified name.
1077 * Open, link etc use their own routines, but this is enough for things
1080 * namei exists in two versions: namei/lnamei. The only difference is
1081 * that namei follows links, while lnamei does not.
1084 int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1086 char *tmp = getname(name);
1087 int err = PTR_ERR(tmp);
1090 err = path_lookup(tmp, flags, nd);
1097 * It's inline, so penalty for filesystems that don't use sticky bit is
1100 static inline int check_sticky(struct inode *dir, struct inode *inode)
1102 if (!(dir->i_mode & S_ISVTX))
1104 if (inode->i_uid == current->fsuid)
1106 if (dir->i_uid == current->fsuid)
1108 return !capable(CAP_FOWNER);
1112 * Check whether we can remove a link victim from directory dir, check
1113 * whether the type of victim is right.
1114 * 1. We can't do it if dir is read-only (done in permission())
1115 * 2. We should have write and exec permissions on dir
1116 * 3. We can't remove anything from append-only dir
1117 * 4. We can't do anything with immutable dir (done in permission())
1118 * 5. If the sticky bit on dir is set we should either
1119 * a. be owner of dir, or
1120 * b. be owner of victim, or
1121 * c. have CAP_FOWNER capability
1122 * 6. If the victim is append-only or immutable we can't do antyhing with
1123 * links pointing to it.
1124 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1125 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1126 * 9. We can't remove a root or mountpoint.
1127 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1128 * nfs_async_unlink().
1130 static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1133 if (!victim->d_inode)
1135 if (victim->d_parent->d_inode != dir)
1138 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1143 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1144 IS_IXORUNLINK(victim->d_inode))
1147 if (!S_ISDIR(victim->d_inode->i_mode))
1149 if (IS_ROOT(victim))
1151 } else if (S_ISDIR(victim->d_inode->i_mode))
1153 if (IS_DEADDIR(dir))
1155 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1160 /* Check whether we can create an object with dentry child in directory
1162 * 1. We can't do it if child already exists (open has special treatment for
1163 * this case, but since we are inlined it's OK)
1164 * 2. We can't do it if dir is read-only (done in permission())
1165 * 3. We should have write and exec permissions on dir
1166 * 4. We can't do it if dir is immutable (done in permission())
1168 static inline int may_create(struct inode *dir, struct dentry *child,
1169 struct nameidata *nd)
1173 if (IS_DEADDIR(dir))
1175 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1178 static inline int mnt_may_create(struct vfsmount *mnt, struct inode *dir, struct dentry *child) {
1181 if (IS_DEADDIR(dir))
1183 if (mnt->mnt_flags & MNT_RDONLY)
1188 static inline int mnt_may_unlink(struct vfsmount *mnt, struct inode *dir, struct dentry *child) {
1189 if (!child->d_inode)
1191 if (mnt->mnt_flags & MNT_RDONLY)
1197 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1200 * O_DIRECTORY translates into forcing a directory lookup.
1202 static inline int lookup_flags(unsigned int f)
1204 unsigned long retval = LOOKUP_FOLLOW;
1207 retval &= ~LOOKUP_FOLLOW;
1209 if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1210 retval &= ~LOOKUP_FOLLOW;
1212 if (f & O_DIRECTORY)
1213 retval |= LOOKUP_DIRECTORY;
1214 if (f & O_ATOMICLOOKUP)
1215 retval |= LOOKUP_ATOMIC;
1221 * p1 and p2 should be directories on the same fs.
1223 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1228 down(&p1->d_inode->i_sem);
1232 down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1234 for (p = p1; p->d_parent != p; p = p->d_parent) {
1235 if (p->d_parent == p2) {
1236 down(&p2->d_inode->i_sem);
1237 down(&p1->d_inode->i_sem);
1242 for (p = p2; p->d_parent != p; p = p->d_parent) {
1243 if (p->d_parent == p1) {
1244 down(&p1->d_inode->i_sem);
1245 down(&p2->d_inode->i_sem);
1250 down(&p1->d_inode->i_sem);
1251 down(&p2->d_inode->i_sem);
1255 void unlock_rename(struct dentry *p1, struct dentry *p2)
1257 up(&p1->d_inode->i_sem);
1259 up(&p2->d_inode->i_sem);
1260 up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1264 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1265 struct nameidata *nd)
1267 int error = may_create(dir, dentry, nd);
1272 if (!dir->i_op || !dir->i_op->create)
1273 return -EACCES; /* shouldn't it be ENOSYS? */
1276 error = security_inode_create(dir, dentry, mode);
1280 error = dir->i_op->create(dir, dentry, mode, nd);
1282 inode_dir_notify(dir, DN_CREATE);
1283 security_inode_post_create(dir, dentry, mode);
1288 int may_open(struct nameidata *nd, int acc_mode, int flag)
1290 struct dentry *dentry = nd->dentry;
1291 struct inode *inode = dentry->d_inode;
1297 if (S_ISLNK(inode->i_mode))
1300 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1303 error = permission(inode, acc_mode, nd);
1308 * FIFO's, sockets and device files are special: they don't
1309 * actually live on the filesystem itself, and as such you
1310 * can write to them even if the filesystem is read-only.
1312 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1314 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1315 if (nd->mnt->mnt_flags & MNT_NODEV)
1319 } else if ((IS_RDONLY(inode) || (nd && MNT_IS_RDONLY(nd->mnt)))
1320 && (flag & FMODE_WRITE))
1323 * An append-only file must be opened in append mode for writing.
1325 if (IS_APPEND(inode)) {
1326 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1332 /* O_NOATIME can only be set by the owner or superuser */
1333 if (flag & O_NOATIME)
1334 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1338 * Ensure there are no outstanding leases on the file.
1340 error = break_lease(inode, flag);
1344 if (flag & O_TRUNC) {
1345 error = get_write_access(inode);
1350 * Refuse to truncate files with mandatory locks held on them.
1352 error = locks_verify_locked(inode);
1356 error = do_truncate(dentry, 0);
1358 put_write_access(inode);
1362 if (flag & FMODE_WRITE)
1371 * namei for open - this is in fact almost the whole open-routine.
1373 * Note that the low bits of "flag" aren't the same as in the open
1374 * system call - they are 00 - no permissions needed
1375 * 01 - read permission needed
1376 * 10 - write permission needed
1377 * 11 - read/write permissions needed
1378 * which is a lot more logical, and also allows the "no perm" needed
1379 * for symlinks (where the permissions are checked later).
1382 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1384 int acc_mode, error = 0;
1385 struct dentry *dentry;
1389 acc_mode = ACC_MODE(flag);
1391 /* Allow the LSM permission hook to distinguish append
1392 access from general write access. */
1393 if (flag & O_APPEND)
1394 acc_mode |= MAY_APPEND;
1396 /* Fill in the open() intent data */
1397 nd->intent.open.flags = flag;
1398 nd->intent.open.create_mode = mode;
1401 * The simplest case - just a plain lookup.
1403 if (!(flag & O_CREAT)) {
1404 error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd);
1411 * Create - we need to know the parent.
1413 error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd);
1418 * We have the parent and last component. First of all, check
1419 * that we are not asked to creat(2) an obvious directory - that
1423 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1427 nd->flags &= ~LOOKUP_PARENT;
1428 down(&dir->d_inode->i_sem);
1429 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1432 error = PTR_ERR(dentry);
1433 if (IS_ERR(dentry)) {
1434 up(&dir->d_inode->i_sem);
1438 /* Negative dentry, just create the file */
1439 if (!dentry->d_inode) {
1440 if (!IS_POSIXACL(dir->d_inode))
1441 mode &= ~current->fs->umask;
1442 error = vfs_create(dir->d_inode, dentry, mode, nd);
1443 up(&dir->d_inode->i_sem);
1445 nd->dentry = dentry;
1448 /* Don't check for write permission, don't truncate */
1455 * It already exists.
1457 up(&dir->d_inode->i_sem);
1463 if (d_mountpoint(dentry)) {
1465 if (flag & O_NOFOLLOW)
1467 while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry));
1470 if (!dentry->d_inode)
1472 if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
1476 nd->dentry = dentry;
1478 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
1481 error = may_open(nd, acc_mode, flag);
1494 if (flag & O_NOFOLLOW)
1497 * This is subtle. Instead of calling do_follow_link() we do the
1498 * thing by hands. The reason is that this way we have zero link_count
1499 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1500 * After that we have the parent and last component, i.e.
1501 * we are in the same situation as after the first path_walk().
1502 * Well, almost - if the last component is normal we get its copy
1503 * stored in nd->last.name and we will have to putname() it when we
1504 * are done. Procfs-like symlinks just set LAST_BIND.
1506 nd->flags |= LOOKUP_PARENT;
1507 error = security_inode_follow_link(dentry, nd);
1510 touch_atime(nd->mnt, dentry);
1511 nd_set_link(nd, NULL);
1512 error = dentry->d_inode->i_op->follow_link(dentry, nd);
1514 char *s = nd_get_link(nd);
1516 error = __vfs_follow_link(nd, s);
1517 if (dentry->d_inode->i_op->put_link)
1518 dentry->d_inode->i_op->put_link(dentry, nd);
1523 nd->flags &= ~LOOKUP_PARENT;
1524 if (nd->last_type == LAST_BIND) {
1525 dentry = nd->dentry;
1529 if (nd->last_type != LAST_NORM)
1531 if (nd->last.name[nd->last.len]) {
1532 putname(nd->last.name);
1537 putname(nd->last.name);
1541 down(&dir->d_inode->i_sem);
1542 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1543 putname(nd->last.name);
1548 * lookup_create - lookup a dentry, creating it if it doesn't exist
1549 * @nd: nameidata info
1550 * @is_dir: directory flag
1552 * Simple function to lookup and return a dentry and create it
1553 * if it doesn't exist. Is SMP-safe.
1555 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1557 struct dentry *dentry;
1560 down(&nd->dentry->d_inode->i_sem);
1562 if (nd->last_type != LAST_NORM)
1564 nd->flags &= ~LOOKUP_PARENT;
1565 dentry = lookup_hash(&nd->last, nd->dentry);
1568 error = mnt_may_create(nd->mnt, nd->dentry->d_inode, dentry);
1572 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1579 return ERR_PTR(error);
1582 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1584 int error = may_create(dir, dentry, NULL);
1589 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1592 if (!dir->i_op || !dir->i_op->mknod)
1595 error = security_inode_mknod(dir, dentry, mode, dev);
1600 error = dir->i_op->mknod(dir, dentry, mode, dev);
1602 inode_dir_notify(dir, DN_CREATE);
1603 security_inode_post_mknod(dir, dentry, mode, dev);
1608 asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
1612 struct dentry * dentry;
1613 struct nameidata nd;
1617 tmp = getname(filename);
1619 return PTR_ERR(tmp);
1621 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1624 dentry = lookup_create(&nd, 0);
1625 error = PTR_ERR(dentry);
1627 if (!IS_POSIXACL(nd.dentry->d_inode))
1628 mode &= ~current->fs->umask;
1629 if (!IS_ERR(dentry)) {
1630 switch (mode & S_IFMT) {
1631 case 0: case S_IFREG:
1632 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1634 case S_IFCHR: case S_IFBLK:
1635 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1636 new_decode_dev(dev));
1638 case S_IFIFO: case S_IFSOCK:
1639 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1649 up(&nd.dentry->d_inode->i_sem);
1657 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1659 int error = may_create(dir, dentry, NULL);
1664 if (!dir->i_op || !dir->i_op->mkdir)
1667 mode &= (S_IRWXUGO|S_ISVTX);
1668 error = security_inode_mkdir(dir, dentry, mode);
1673 error = dir->i_op->mkdir(dir, dentry, mode);
1675 inode_dir_notify(dir, DN_CREATE);
1676 security_inode_post_mkdir(dir,dentry, mode);
1681 asmlinkage long sys_mkdir(const char __user * pathname, int mode)
1686 tmp = getname(pathname);
1687 error = PTR_ERR(tmp);
1689 struct dentry *dentry;
1690 struct nameidata nd;
1692 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1695 dentry = lookup_create(&nd, 1);
1696 error = PTR_ERR(dentry);
1697 if (!IS_ERR(dentry)) {
1698 if (!IS_POSIXACL(nd.dentry->d_inode))
1699 mode &= ~current->fs->umask;
1700 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1703 up(&nd.dentry->d_inode->i_sem);
1713 * We try to drop the dentry early: we should have
1714 * a usage count of 2 if we're the only user of this
1715 * dentry, and if that is true (possibly after pruning
1716 * the dcache), then we drop the dentry now.
1718 * A low-level filesystem can, if it choses, legally
1721 * if (!d_unhashed(dentry))
1724 * if it cannot handle the case of removing a directory
1725 * that is still in use by something else..
1727 static void d_unhash(struct dentry *dentry)
1730 spin_lock(&dcache_lock);
1731 switch (atomic_read(&dentry->d_count)) {
1733 spin_unlock(&dcache_lock);
1734 shrink_dcache_parent(dentry);
1735 spin_lock(&dcache_lock);
1736 if (atomic_read(&dentry->d_count) != 2)
1741 spin_unlock(&dcache_lock);
1744 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1746 int error = may_delete(dir, dentry, 1);
1751 if (!dir->i_op || !dir->i_op->rmdir)
1756 down(&dentry->d_inode->i_sem);
1758 if (d_mountpoint(dentry))
1761 error = security_inode_rmdir(dir, dentry);
1763 error = dir->i_op->rmdir(dir, dentry);
1765 dentry->d_inode->i_flags |= S_DEAD;
1768 up(&dentry->d_inode->i_sem);
1770 inode_dir_notify(dir, DN_DELETE);
1778 asmlinkage long sys_rmdir(const char __user * pathname)
1782 struct dentry *dentry;
1783 struct nameidata nd;
1785 name = getname(pathname);
1787 return PTR_ERR(name);
1789 error = path_lookup(name, LOOKUP_PARENT, &nd);
1793 switch(nd.last_type) {
1804 down(&nd.dentry->d_inode->i_sem);
1805 dentry = lookup_hash(&nd.last, nd.dentry);
1806 error = PTR_ERR(dentry);
1807 if (!IS_ERR(dentry)) {
1808 error = mnt_may_unlink(nd.mnt, nd.dentry->d_inode, dentry);
1811 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1815 up(&nd.dentry->d_inode->i_sem);
1823 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1825 int error = may_delete(dir, dentry, 0);
1830 if (!dir->i_op || !dir->i_op->unlink)
1835 down(&dentry->d_inode->i_sem);
1836 if (d_mountpoint(dentry))
1839 error = security_inode_unlink(dir, dentry);
1841 error = dir->i_op->unlink(dir, dentry);
1843 up(&dentry->d_inode->i_sem);
1845 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1846 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1848 inode_dir_notify(dir, DN_DELETE);
1854 * Make sure that the actual truncation of the file will occur outside its
1855 * directory's i_sem. Truncate can take a long time if there is a lot of
1856 * writeout happening, and we don't want to prevent access to the directory
1857 * while waiting on the I/O.
1859 asmlinkage long sys_unlink(const char __user * pathname)
1863 struct dentry *dentry;
1864 struct nameidata nd;
1865 struct inode *inode = NULL;
1867 name = getname(pathname);
1869 return PTR_ERR(name);
1871 error = path_lookup(name, LOOKUP_PARENT, &nd);
1875 if (nd.last_type != LAST_NORM)
1877 down(&nd.dentry->d_inode->i_sem);
1878 dentry = lookup_hash(&nd.last, nd.dentry);
1879 error = PTR_ERR(dentry);
1880 if (!IS_ERR(dentry)) {
1881 /* Why not before? Because we want correct error value */
1882 if (nd.last.name[nd.last.len])
1884 error = mnt_may_unlink(nd.mnt, nd.dentry->d_inode, dentry);
1887 inode = dentry->d_inode;
1889 atomic_inc(&inode->i_count);
1890 error = vfs_unlink(nd.dentry->d_inode, dentry);
1894 up(&nd.dentry->d_inode->i_sem);
1901 iput(inode); /* truncate the inode here */
1905 error = !dentry->d_inode ? -ENOENT :
1906 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1910 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
1912 int error = may_create(dir, dentry, NULL);
1917 if (!dir->i_op || !dir->i_op->symlink)
1920 error = security_inode_symlink(dir, dentry, oldname);
1925 error = dir->i_op->symlink(dir, dentry, oldname);
1927 inode_dir_notify(dir, DN_CREATE);
1928 security_inode_post_symlink(dir, dentry, oldname);
1933 asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
1939 from = getname(oldname);
1941 return PTR_ERR(from);
1942 to = getname(newname);
1943 error = PTR_ERR(to);
1945 struct dentry *dentry;
1946 struct nameidata nd;
1948 error = path_lookup(to, LOOKUP_PARENT, &nd);
1951 dentry = lookup_create(&nd, 0);
1952 error = PTR_ERR(dentry);
1953 if (!IS_ERR(dentry)) {
1954 error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
1957 up(&nd.dentry->d_inode->i_sem);
1966 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1968 struct inode *inode = old_dentry->d_inode;
1974 error = may_create(dir, new_dentry, NULL);
1978 if (dir->i_sb != inode->i_sb)
1982 * A link to an append-only or immutable file cannot be created.
1984 if (IS_APPEND(inode) || IS_IXORUNLINK(inode))
1986 if (!dir->i_op || !dir->i_op->link)
1988 if (S_ISDIR(old_dentry->d_inode->i_mode))
1991 error = security_inode_link(old_dentry, dir, new_dentry);
1995 down(&old_dentry->d_inode->i_sem);
1997 error = dir->i_op->link(old_dentry, dir, new_dentry);
1998 up(&old_dentry->d_inode->i_sem);
2000 inode_dir_notify(dir, DN_CREATE);
2001 security_inode_post_link(old_dentry, dir, new_dentry);
2007 * Hardlinks are often used in delicate situations. We avoid
2008 * security-related surprises by not following symlinks on the
2011 * We don't follow them on the oldname either to be compatible
2012 * with linux 2.0, and to avoid hard-linking to directories
2013 * and other special files. --ADM
2015 asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
2017 struct dentry *new_dentry;
2018 struct nameidata nd, old_nd;
2022 to = getname(newname);
2026 error = __user_walk(oldname, 0, &old_nd);
2029 error = path_lookup(to, LOOKUP_PARENT, &nd);
2033 * We allow hard-links to be created to a bind-mount as long
2034 * as the bind-mount is not read-only. Checking for cross-dev
2035 * links is subsumed by the superblock check in vfs_link().
2038 if (MNT_IS_RDONLY(old_nd.mnt))
2040 new_dentry = lookup_create(&nd, 0);
2041 error = PTR_ERR(new_dentry);
2042 if (!IS_ERR(new_dentry)) {
2043 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
2046 up(&nd.dentry->d_inode->i_sem);
2050 path_release(&old_nd);
2058 * The worst of all namespace operations - renaming directory. "Perverted"
2059 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2061 * a) we can get into loop creation. Check is done in is_subdir().
2062 * b) race potential - two innocent renames can create a loop together.
2063 * That's where 4.4 screws up. Current fix: serialization on
2064 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
2066 * c) we have to lock _three_ objects - parents and victim (if it exists).
2067 * And that - after we got ->i_sem on parents (until then we don't know
2068 * whether the target exists). Solution: try to be smart with locking
2069 * order for inodes. We rely on the fact that tree topology may change
2070 * only under ->s_vfs_rename_sem _and_ that parent of the object we
2071 * move will be locked. Thus we can rank directories by the tree
2072 * (ancestors first) and rank all non-directories after them.
2073 * That works since everybody except rename does "lock parent, lookup,
2074 * lock child" and rename is under ->s_vfs_rename_sem.
2075 * HOWEVER, it relies on the assumption that any object with ->lookup()
2076 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2077 * we'd better make sure that there's no link(2) for them.
2078 * d) some filesystems don't support opened-but-unlinked directories,
2079 * either because of layout or because they are not ready to deal with
2080 * all cases correctly. The latter will be fixed (taking this sort of
2081 * stuff into VFS), but the former is not going away. Solution: the same
2082 * trick as in rmdir().
2083 * e) conversion from fhandle to dentry may come in the wrong moment - when
2084 * we are removing the target. Solution: we will have to grab ->i_sem
2085 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2086 * ->i_sem on parents, which works but leads to some truely excessive
2089 int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2090 struct inode *new_dir, struct dentry *new_dentry)
2093 struct inode *target;
2096 * If we are going to change the parent - check write permissions,
2097 * we'll need to flip '..'.
2099 if (new_dir != old_dir) {
2100 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2105 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2109 target = new_dentry->d_inode;
2111 down(&target->i_sem);
2112 d_unhash(new_dentry);
2114 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2117 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2120 target->i_flags |= S_DEAD;
2122 if (d_unhashed(new_dentry))
2123 d_rehash(new_dentry);
2127 d_move(old_dentry,new_dentry);
2128 security_inode_post_rename(old_dir, old_dentry,
2129 new_dir, new_dentry);
2134 int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2135 struct inode *new_dir, struct dentry *new_dentry)
2137 struct inode *target;
2140 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2145 target = new_dentry->d_inode;
2147 down(&target->i_sem);
2148 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2151 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2153 /* The following d_move() should become unconditional */
2154 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
2155 d_move(old_dentry, new_dentry);
2156 security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
2164 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2165 struct inode *new_dir, struct dentry *new_dentry)
2168 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2170 if (old_dentry->d_inode == new_dentry->d_inode)
2173 error = may_delete(old_dir, old_dentry, is_dir);
2177 if (!new_dentry->d_inode)
2178 error = may_create(new_dir, new_dentry, NULL);
2180 error = may_delete(new_dir, new_dentry, is_dir);
2184 if (!old_dir->i_op || !old_dir->i_op->rename)
2187 DQUOT_INIT(old_dir);
2188 DQUOT_INIT(new_dir);
2191 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2193 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2195 if (old_dir == new_dir)
2196 inode_dir_notify(old_dir, DN_RENAME);
2198 inode_dir_notify(old_dir, DN_DELETE);
2199 inode_dir_notify(new_dir, DN_CREATE);
2205 static inline int do_rename(const char * oldname, const char * newname)
2208 struct dentry * old_dir, * new_dir;
2209 struct dentry * old_dentry, *new_dentry;
2210 struct dentry * trap;
2211 struct nameidata oldnd, newnd;
2213 error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2217 error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2222 if (oldnd.mnt != newnd.mnt)
2225 old_dir = oldnd.dentry;
2227 if (oldnd.last_type != LAST_NORM)
2230 new_dir = newnd.dentry;
2231 if (newnd.last_type != LAST_NORM)
2234 trap = lock_rename(new_dir, old_dir);
2236 old_dentry = lookup_hash(&oldnd.last, old_dir);
2237 error = PTR_ERR(old_dentry);
2238 if (IS_ERR(old_dentry))
2240 /* source must exist */
2242 if (!old_dentry->d_inode)
2244 /* unless the source is a directory trailing slashes give -ENOTDIR */
2245 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2247 if (oldnd.last.name[oldnd.last.len])
2249 if (newnd.last.name[newnd.last.len])
2252 /* source should not be ancestor of target */
2254 if (old_dentry == trap)
2257 if (MNT_IS_RDONLY(newnd.mnt))
2259 new_dentry = lookup_hash(&newnd.last, new_dir);
2260 error = PTR_ERR(new_dentry);
2261 if (IS_ERR(new_dentry))
2263 /* target should not be an ancestor of source */
2265 if (new_dentry == trap)
2268 error = vfs_rename(old_dir->d_inode, old_dentry,
2269 new_dir->d_inode, new_dentry);
2275 unlock_rename(new_dir, old_dir);
2277 path_release(&newnd);
2279 path_release(&oldnd);
2284 asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
2290 from = getname(oldname);
2292 return PTR_ERR(from);
2293 to = getname(newname);
2294 error = PTR_ERR(to);
2296 error = do_rename(from,to);
2303 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2307 len = PTR_ERR(link);
2312 if (len > (unsigned) buflen)
2314 if (copy_to_user(buffer, link, len))
2321 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2322 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2323 * using) it for any given inode is up to filesystem.
2325 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2327 struct nameidata nd;
2330 res = dentry->d_inode->i_op->follow_link(dentry, &nd);
2332 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2333 if (dentry->d_inode->i_op->put_link)
2334 dentry->d_inode->i_op->put_link(dentry, &nd);
2339 int vfs_follow_link(struct nameidata *nd, const char *link)
2341 return __vfs_follow_link(nd, link);
2344 /* get the link contents into pagecache */
2345 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2348 struct address_space *mapping = dentry->d_inode->i_mapping;
2349 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2353 wait_on_page_locked(page);
2354 if (!PageUptodate(page))
2360 page_cache_release(page);
2361 return ERR_PTR(-EIO);
2367 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2369 struct page *page = NULL;
2370 char *s = page_getlink(dentry, &page);
2371 int res = vfs_readlink(dentry,buffer,buflen,s);
2374 page_cache_release(page);
2379 int page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2382 nd_set_link(nd, page_getlink(dentry, &page));
2386 void page_put_link(struct dentry *dentry, struct nameidata *nd)
2388 if (!IS_ERR(nd_get_link(nd))) {
2390 page = find_get_page(dentry->d_inode->i_mapping, 0);
2394 page_cache_release(page);
2395 page_cache_release(page);
2399 int page_follow_link(struct dentry *dentry, struct nameidata *nd)
2401 struct page *page = NULL;
2402 char *s = page_getlink(dentry, &page);
2403 int res = __vfs_follow_link(nd, s);
2406 page_cache_release(page);
2411 int page_symlink(struct inode *inode, const char *symname, int len)
2413 struct address_space *mapping = inode->i_mapping;
2414 struct page *page = grab_cache_page(mapping, 0);
2420 err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2423 kaddr = kmap_atomic(page, KM_USER0);
2424 memcpy(kaddr, symname, len-1);
2425 kunmap_atomic(kaddr, KM_USER0);
2426 mapping->a_ops->commit_write(NULL, page, 0, len-1);
2428 * Notice that we are _not_ going to block here - end of page is
2429 * unmapped, so this will only try to map the rest of page, see
2430 * that it is unmapped (typically even will not look into inode -
2431 * ->i_size will be enough for everything) and zero it out.
2432 * OTOH it's obviously correct and should make the page up-to-date.
2434 if (!PageUptodate(page)) {
2435 err = mapping->a_ops->readpage(NULL, page);
2436 wait_on_page_locked(page);
2440 page_cache_release(page);
2443 mark_inode_dirty(inode);
2447 page_cache_release(page);
2452 struct inode_operations page_symlink_inode_operations = {
2453 .readlink = generic_readlink,
2454 .follow_link = page_follow_link_light,
2455 .put_link = page_put_link,
2458 EXPORT_SYMBOL(__user_walk);
2459 EXPORT_SYMBOL(follow_down);
2460 EXPORT_SYMBOL(follow_up);
2461 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2462 EXPORT_SYMBOL(getname);
2463 EXPORT_SYMBOL(lock_rename);
2464 EXPORT_SYMBOL(lookup_create);
2465 EXPORT_SYMBOL(lookup_hash);
2466 EXPORT_SYMBOL(lookup_one_len);
2467 EXPORT_SYMBOL(page_follow_link);
2468 EXPORT_SYMBOL(page_follow_link_light);
2469 EXPORT_SYMBOL(page_put_link);
2470 EXPORT_SYMBOL(page_readlink);
2471 EXPORT_SYMBOL(page_symlink);
2472 EXPORT_SYMBOL(page_symlink_inode_operations);
2473 EXPORT_SYMBOL(path_lookup);
2474 EXPORT_SYMBOL(path_release);
2475 EXPORT_SYMBOL(path_walk);
2476 EXPORT_SYMBOL(permission);
2477 EXPORT_SYMBOL(unlock_rename);
2478 EXPORT_SYMBOL(vfs_create);
2479 EXPORT_SYMBOL(vfs_follow_link);
2480 EXPORT_SYMBOL(vfs_link);
2481 EXPORT_SYMBOL(vfs_mkdir);
2482 EXPORT_SYMBOL(vfs_mknod);
2483 EXPORT_SYMBOL(vfs_permission);
2484 EXPORT_SYMBOL(vfs_readlink);
2485 EXPORT_SYMBOL(vfs_rename);
2486 EXPORT_SYMBOL(vfs_rmdir);
2487 EXPORT_SYMBOL(vfs_symlink);
2488 EXPORT_SYMBOL(vfs_unlink);
2489 EXPORT_SYMBOL(generic_readlink);