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/fsnotify.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 <asm/namei.h>
32 #include <asm/uaccess.h>
34 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
36 /* [Feb-1997 T. Schoebel-Theuer]
37 * Fundamental changes in the pathname lookup mechanisms (namei)
38 * were necessary because of omirr. The reason is that omirr needs
39 * to know the _real_ pathname, not the user-supplied one, in case
40 * of symlinks (and also when transname replacements occur).
42 * The new code replaces the old recursive symlink resolution with
43 * an iterative one (in case of non-nested symlink chains). It does
44 * this with calls to <fs>_follow_link().
45 * As a side effect, dir_namei(), _namei() and follow_link() are now
46 * replaced with a single function lookup_dentry() that can handle all
47 * the special cases of the former code.
49 * With the new dcache, the pathname is stored at each inode, at least as
50 * long as the refcount of the inode is positive. As a side effect, the
51 * size of the dcache depends on the inode cache and thus is dynamic.
53 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
54 * resolution to correspond with current state of the code.
56 * Note that the symlink resolution is not *completely* iterative.
57 * There is still a significant amount of tail- and mid- recursion in
58 * the algorithm. Also, note that <fs>_readlink() is not used in
59 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
60 * may return different results than <fs>_follow_link(). Many virtual
61 * filesystems (including /proc) exhibit this behavior.
64 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
65 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
66 * and the name already exists in form of a symlink, try to create the new
67 * name indicated by the symlink. The old code always complained that the
68 * name already exists, due to not following the symlink even if its target
69 * is nonexistent. The new semantics affects also mknod() and link() when
70 * the name is a symlink pointing to a non-existant name.
72 * I don't know which semantics is the right one, since I have no access
73 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
74 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
75 * "old" one. Personally, I think the new semantics is much more logical.
76 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
77 * file does succeed in both HP-UX and SunOs, but not in Solaris
78 * and in the old Linux semantics.
81 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
82 * semantics. See the comments in "open_namei" and "do_link" below.
84 * [10-Sep-98 Alan Modra] Another symlink change.
87 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
88 * inside the path - always follow.
89 * in the last component in creation/removal/renaming - never follow.
90 * if LOOKUP_FOLLOW passed - follow.
91 * if the pathname has trailing slashes - follow.
92 * otherwise - don't follow.
93 * (applied in that order).
95 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
96 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
97 * During the 2.4 we need to fix the userland stuff depending on it -
98 * hopefully we will be able to get rid of that wart in 2.5. So far only
99 * XEmacs seems to be relying on it...
102 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
103 * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives
104 * any extra contention...
107 /* In order to reduce some races, while at the same time doing additional
108 * checking and hopefully speeding things up, we copy filenames to the
109 * kernel data space before using them..
111 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
112 * PATH_MAX includes the nul terminator --RR.
114 static inline int do_getname(const char __user *filename, char *page)
117 unsigned long len = PATH_MAX;
119 if (!segment_eq(get_fs(), KERNEL_DS)) {
120 if ((unsigned long) filename >= TASK_SIZE)
122 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
123 len = TASK_SIZE - (unsigned long) filename;
126 retval = strncpy_from_user(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 audit_getname(result);
155 #ifdef CONFIG_AUDITSYSCALL
156 void putname(const char *name)
158 if (unlikely(current->audit_context))
163 EXPORT_SYMBOL(putname);
168 * generic_permission - check for access rights on a Posix-like filesystem
169 * @inode: inode to check access rights for
170 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
171 * @check_acl: optional callback to check for Posix ACLs
173 * Used to check for read/write/execute permissions on a file.
174 * We use "fsuid" for this, letting us set arbitrary permissions
175 * for filesystem access without changing the "normal" uids which
176 * are used for other things..
178 int generic_permission(struct inode *inode, int mask,
179 int (*check_acl)(struct inode *inode, int mask))
181 umode_t mode = inode->i_mode;
183 if (current->fsuid == inode->i_uid)
186 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
187 int error = check_acl(inode, mask);
188 if (error == -EACCES)
189 goto check_capabilities;
190 else if (error != -EAGAIN)
194 if (in_group_p(inode->i_gid))
199 * If the DACs are ok we don't need any capability check.
201 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
206 * Read/write DACs are always overridable.
207 * Executable DACs are overridable if at least one exec bit is set.
209 if (!(mask & MAY_EXEC) ||
210 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
211 if (capable(CAP_DAC_OVERRIDE))
215 * Searching includes executable on directories, else just read.
217 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
218 if (capable(CAP_DAC_READ_SEARCH))
224 int permission(struct inode *inode, int mask, struct nameidata *nd)
228 if (mask & MAY_WRITE) {
229 umode_t mode = inode->i_mode;
232 * Nobody gets write access to a read-only fs.
234 if (IS_RDONLY(inode) &&
235 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
239 * Nobody gets write access to an immutable file.
241 if (IS_IMMUTABLE(inode))
246 /* Ordinary permission routines do not understand MAY_APPEND. */
247 submask = mask & ~MAY_APPEND;
248 if (inode->i_op && inode->i_op->permission)
249 retval = inode->i_op->permission(inode, submask, nd);
251 retval = generic_permission(inode, submask, NULL);
255 return security_inode_permission(inode, mask, nd);
259 * get_write_access() gets write permission for a file.
260 * put_write_access() releases this write permission.
261 * This is used for regular files.
262 * We cannot support write (and maybe mmap read-write shared) accesses and
263 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
264 * can have the following values:
265 * 0: no writers, no VM_DENYWRITE mappings
266 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
267 * > 0: (i_writecount) users are writing to the file.
269 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
270 * except for the cases where we don't hold i_writecount yet. Then we need to
271 * use {get,deny}_write_access() - these functions check the sign and refuse
272 * to do the change if sign is wrong. Exclusion between them is provided by
273 * the inode->i_lock spinlock.
276 int get_write_access(struct inode * inode)
278 spin_lock(&inode->i_lock);
279 if (atomic_read(&inode->i_writecount) < 0) {
280 spin_unlock(&inode->i_lock);
283 atomic_inc(&inode->i_writecount);
284 spin_unlock(&inode->i_lock);
289 int deny_write_access(struct file * file)
291 struct inode *inode = file->f_dentry->d_inode;
293 spin_lock(&inode->i_lock);
294 if (atomic_read(&inode->i_writecount) > 0) {
295 spin_unlock(&inode->i_lock);
298 atomic_dec(&inode->i_writecount);
299 spin_unlock(&inode->i_lock);
304 void path_release(struct nameidata *nd)
311 * umount() mustn't call path_release()/mntput() as that would clear
314 void path_release_on_umount(struct nameidata *nd)
317 mntput_no_expire(nd->mnt);
321 * Internal lookup() using the new generic dcache.
324 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
326 struct dentry * dentry = __d_lookup(parent, name);
328 /* lockess __d_lookup may fail due to concurrent d_move()
329 * in some unrelated directory, so try with d_lookup
332 dentry = d_lookup(parent, name);
334 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
335 if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
344 * Short-cut version of permission(), for calling by
345 * path_walk(), when dcache lock is held. Combines parts
346 * of permission() and generic_permission(), and tests ONLY for
347 * MAY_EXEC permission.
349 * If appropriate, check DAC only. If not appropriate, or
350 * short-cut DAC fails, then call permission() to do more
351 * complete permission check.
353 static inline int exec_permission_lite(struct inode *inode,
354 struct nameidata *nd)
356 umode_t mode = inode->i_mode;
358 if (inode->i_op && inode->i_op->permission)
361 if (current->fsuid == inode->i_uid)
363 else if (in_group_p(inode->i_gid))
369 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
372 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
375 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
380 return security_inode_permission(inode, MAY_EXEC, nd);
384 * This is called when everything else fails, and we actually have
385 * to go to the low-level filesystem to find out what we should do..
387 * We get the directory semaphore, and after getting that we also
388 * make sure that nobody added the entry to the dcache in the meantime..
391 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
393 struct dentry * result;
394 struct inode *dir = parent->d_inode;
398 * First re-do the cached lookup just in case it was created
399 * while we waited for the directory semaphore..
401 * FIXME! This could use version numbering or similar to
402 * avoid unnecessary cache lookups.
404 * The "dcache_lock" is purely to protect the RCU list walker
405 * from concurrent renames at this point (we mustn't get false
406 * negatives from the RCU list walk here, unlike the optimistic
409 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
411 result = d_lookup(parent, name);
413 struct dentry * dentry = d_alloc(parent, name);
414 result = ERR_PTR(-ENOMEM);
416 result = dir->i_op->lookup(dir, dentry, nd);
427 * Uhhuh! Nasty case: the cache was re-populated while
428 * we waited on the semaphore. Need to revalidate.
431 if (result->d_op && result->d_op->d_revalidate) {
432 if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
434 result = ERR_PTR(-ENOENT);
440 static int __emul_lookup_dentry(const char *, struct nameidata *);
444 walk_init_root(const char *name, struct nameidata *nd)
446 read_lock(¤t->fs->lock);
447 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
448 nd->mnt = mntget(current->fs->altrootmnt);
449 nd->dentry = dget(current->fs->altroot);
450 read_unlock(¤t->fs->lock);
451 if (__emul_lookup_dentry(name,nd))
453 read_lock(¤t->fs->lock);
455 nd->mnt = mntget(current->fs->rootmnt);
456 nd->dentry = dget(current->fs->root);
457 read_unlock(¤t->fs->lock);
461 static inline int __vfs_follow_link(struct nameidata *nd, const char *link)
470 if (!walk_init_root(link, nd))
471 /* weird __emul_prefix() stuff did it */
474 res = link_path_walk(link, nd);
476 if (nd->depth || res || nd->last_type!=LAST_NORM)
479 * If it is an iterative symlinks resolution in open_namei() we
480 * have to copy the last component. And all that crap because of
481 * bloody create() on broken symlinks. Furrfu...
484 if (unlikely(!name)) {
488 strcpy(name, nd->last.name);
489 nd->last.name = name;
493 return PTR_ERR(link);
497 struct vfsmount *mnt;
498 struct dentry *dentry;
501 static inline int __do_follow_link(struct path *path, struct nameidata *nd)
505 struct dentry *dentry = path->dentry;
507 touch_atime(path->mnt, dentry);
508 nd_set_link(nd, NULL);
510 if (path->mnt == nd->mnt)
512 cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
513 error = PTR_ERR(cookie);
514 if (!IS_ERR(cookie)) {
515 char *s = nd_get_link(nd);
518 error = __vfs_follow_link(nd, s);
519 if (dentry->d_inode->i_op->put_link)
520 dentry->d_inode->i_op->put_link(dentry, nd, cookie);
529 * This limits recursive symlink follows to 8, while
530 * limiting consecutive symlinks to 40.
532 * Without that kind of total limit, nasty chains of consecutive
533 * symlinks can cause almost arbitrarily long lookups.
535 static inline int do_follow_link(struct path *path, struct nameidata *nd)
538 if (current->link_count >= MAX_NESTED_LINKS)
540 if (current->total_link_count >= 40)
542 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
544 err = security_inode_follow_link(path->dentry, nd);
547 current->link_count++;
548 current->total_link_count++;
550 err = __do_follow_link(path, nd);
551 current->link_count--;
556 if (path->mnt != nd->mnt)
562 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
564 struct vfsmount *parent;
565 struct dentry *mountpoint;
566 spin_lock(&vfsmount_lock);
567 parent=(*mnt)->mnt_parent;
568 if (parent == *mnt) {
569 spin_unlock(&vfsmount_lock);
573 mountpoint=dget((*mnt)->mnt_mountpoint);
574 spin_unlock(&vfsmount_lock);
576 *dentry = mountpoint;
582 /* no need for dcache_lock, as serialization is taken care in
585 static int __follow_mount(struct path *path)
588 while (d_mountpoint(path->dentry)) {
589 struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
596 path->dentry = dget(mounted->mnt_root);
602 static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
604 while (d_mountpoint(*dentry)) {
605 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
611 *dentry = dget(mounted->mnt_root);
615 /* no need for dcache_lock, as serialization is taken care in
618 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
620 struct vfsmount *mounted;
622 mounted = lookup_mnt(*mnt, *dentry);
627 *dentry = dget(mounted->mnt_root);
633 static inline void follow_dotdot(struct nameidata *nd)
636 struct vfsmount *parent;
637 struct dentry *old = nd->dentry;
639 read_lock(¤t->fs->lock);
640 if (nd->dentry == current->fs->root &&
641 nd->mnt == current->fs->rootmnt) {
642 read_unlock(¤t->fs->lock);
645 read_unlock(¤t->fs->lock);
646 spin_lock(&dcache_lock);
647 if (nd->dentry != nd->mnt->mnt_root) {
648 nd->dentry = dget(nd->dentry->d_parent);
649 spin_unlock(&dcache_lock);
653 spin_unlock(&dcache_lock);
654 spin_lock(&vfsmount_lock);
655 parent = nd->mnt->mnt_parent;
656 if (parent == nd->mnt) {
657 spin_unlock(&vfsmount_lock);
661 nd->dentry = dget(nd->mnt->mnt_mountpoint);
662 spin_unlock(&vfsmount_lock);
667 follow_mount(&nd->mnt, &nd->dentry);
671 * It's more convoluted than I'd like it to be, but... it's still fairly
672 * small and for now I'd prefer to have fast path as straight as possible.
673 * It _is_ time-critical.
675 static int do_lookup(struct nameidata *nd, struct qstr *name,
678 struct vfsmount *mnt = nd->mnt;
679 struct dentry *dentry = __d_lookup(nd->dentry, name);
683 if (dentry->d_op && dentry->d_op->d_revalidate)
684 goto need_revalidate;
687 path->dentry = dentry;
688 __follow_mount(path);
692 dentry = real_lookup(nd->dentry, name, nd);
698 if (dentry->d_op->d_revalidate(dentry, nd))
700 if (d_invalidate(dentry))
706 return PTR_ERR(dentry);
711 * This is the basic name resolution function, turning a pathname into
712 * the final dentry. We expect 'base' to be positive and a directory.
714 * Returns 0 and nd will have valid dentry and mnt on success.
715 * Returns error and drops reference to input namei data on failure.
717 static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
722 unsigned int lookup_flags = nd->flags;
729 inode = nd->dentry->d_inode;
731 lookup_flags = LOOKUP_FOLLOW;
733 /* At this point we know we have a real path component. */
739 err = exec_permission_lite(inode, nd);
740 if (err == -EAGAIN) {
741 err = permission(inode, MAY_EXEC, nd);
747 c = *(const unsigned char *)name;
749 hash = init_name_hash();
752 hash = partial_name_hash(c, hash);
753 c = *(const unsigned char *)name;
754 } while (c && (c != '/'));
755 this.len = name - (const char *) this.name;
756 this.hash = end_name_hash(hash);
758 /* remove trailing slashes? */
761 while (*++name == '/');
763 goto last_with_slashes;
766 * "." and ".." are special - ".." especially so because it has
767 * to be able to know about the current root directory and
768 * parent relationships.
770 if (this.name[0] == '.') switch (this.len) {
774 if (this.name[1] != '.')
777 inode = nd->dentry->d_inode;
783 * See if the low-level filesystem might want
784 * to use its own hash..
786 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
787 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
791 nd->flags |= LOOKUP_CONTINUE;
792 /* This does the actual lookups.. */
793 err = do_lookup(nd, &this, &next);
798 inode = next.dentry->d_inode;
805 if (inode->i_op->follow_link) {
806 err = do_follow_link(&next, nd);
810 inode = nd->dentry->d_inode;
818 if (nd->mnt != next.mnt)
821 nd->dentry = next.dentry;
824 if (!inode->i_op->lookup)
827 /* here ends the main loop */
830 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
832 nd->flags &= ~LOOKUP_CONTINUE;
833 if (lookup_flags & LOOKUP_PARENT)
835 if (this.name[0] == '.') switch (this.len) {
839 if (this.name[1] != '.')
842 inode = nd->dentry->d_inode;
847 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
848 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
852 err = do_lookup(nd, &this, &next);
855 inode = next.dentry->d_inode;
856 if ((lookup_flags & LOOKUP_FOLLOW)
857 && inode && inode->i_op && inode->i_op->follow_link) {
858 err = do_follow_link(&next, nd);
861 inode = nd->dentry->d_inode;
864 if (nd->mnt != next.mnt)
867 nd->dentry = next.dentry;
872 if (lookup_flags & LOOKUP_DIRECTORY) {
874 if (!inode->i_op || !inode->i_op->lookup)
880 nd->last_type = LAST_NORM;
881 if (this.name[0] != '.')
884 nd->last_type = LAST_DOT;
885 else if (this.len == 2 && this.name[1] == '.')
886 nd->last_type = LAST_DOTDOT;
891 * We bypassed the ordinary revalidation routines.
892 * We may need to check the cached dentry for staleness.
894 if (nd->dentry && nd->dentry->d_sb &&
895 (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
897 /* Note: we do not d_invalidate() */
898 if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
905 if (nd->mnt != next.mnt)
915 * Wrapper to retry pathname resolution whenever the underlying
916 * file system returns an ESTALE.
918 * Retry the whole path once, forcing real lookup requests
919 * instead of relying on the dcache.
921 int fastcall link_path_walk(const char *name, struct nameidata *nd)
923 struct nameidata save = *nd;
926 /* make sure the stuff we saved doesn't go away */
930 result = __link_path_walk(name, nd);
931 if (result == -ESTALE) {
935 nd->flags |= LOOKUP_REVAL;
936 result = __link_path_walk(name, nd);
945 int fastcall path_walk(const char * name, struct nameidata *nd)
947 current->total_link_count = 0;
948 return link_path_walk(name, nd);
952 * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
953 * everything is done. Returns 0 and drops input nd, if lookup failed;
955 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
957 if (path_walk(name, nd))
958 return 0; /* something went wrong... */
960 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
961 struct dentry *old_dentry = nd->dentry;
962 struct vfsmount *old_mnt = nd->mnt;
963 struct qstr last = nd->last;
964 int last_type = nd->last_type;
966 * NAME was not found in alternate root or it's a directory. Try to find
967 * it in the normal root:
969 nd->last_type = LAST_ROOT;
970 read_lock(¤t->fs->lock);
971 nd->mnt = mntget(current->fs->rootmnt);
972 nd->dentry = dget(current->fs->root);
973 read_unlock(¤t->fs->lock);
974 if (path_walk(name, nd) == 0) {
975 if (nd->dentry->d_inode) {
982 nd->dentry = old_dentry;
985 nd->last_type = last_type;
990 void set_fs_altroot(void)
992 char *emul = __emul_prefix();
994 struct vfsmount *mnt = NULL, *oldmnt;
995 struct dentry *dentry = NULL, *olddentry;
1000 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
1006 write_lock(¤t->fs->lock);
1007 oldmnt = current->fs->altrootmnt;
1008 olddentry = current->fs->altroot;
1009 current->fs->altrootmnt = mnt;
1010 current->fs->altroot = dentry;
1011 write_unlock(¤t->fs->lock);
1018 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1019 int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
1023 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1027 read_lock(¤t->fs->lock);
1029 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
1030 nd->mnt = mntget(current->fs->altrootmnt);
1031 nd->dentry = dget(current->fs->altroot);
1032 read_unlock(¤t->fs->lock);
1033 if (__emul_lookup_dentry(name,nd))
1034 goto out; /* found in altroot */
1035 read_lock(¤t->fs->lock);
1037 nd->mnt = mntget(current->fs->rootmnt);
1038 nd->dentry = dget(current->fs->root);
1040 nd->mnt = mntget(current->fs->pwdmnt);
1041 nd->dentry = dget(current->fs->pwd);
1043 read_unlock(¤t->fs->lock);
1044 current->total_link_count = 0;
1045 retval = link_path_walk(name, nd);
1047 if (unlikely(current->audit_context
1048 && nd && nd->dentry && nd->dentry->d_inode))
1049 audit_inode(name, nd->dentry->d_inode);
1054 * Restricted form of lookup. Doesn't follow links, single-component only,
1055 * needs parent already locked. Doesn't follow mounts.
1058 static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
1060 struct dentry * dentry;
1061 struct inode *inode;
1064 inode = base->d_inode;
1065 err = permission(inode, MAY_EXEC, nd);
1066 dentry = ERR_PTR(err);
1071 * See if the low-level filesystem might want
1072 * to use its own hash..
1074 if (base->d_op && base->d_op->d_hash) {
1075 err = base->d_op->d_hash(base, name);
1076 dentry = ERR_PTR(err);
1081 dentry = cached_lookup(base, name, nd);
1083 struct dentry *new = d_alloc(base, name);
1084 dentry = ERR_PTR(-ENOMEM);
1087 dentry = inode->i_op->lookup(inode, new, nd);
1097 struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
1099 return __lookup_hash(name, base, NULL);
1103 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
1114 hash = init_name_hash();
1116 c = *(const unsigned char *)name++;
1117 if (c == '/' || c == '\0')
1119 hash = partial_name_hash(c, hash);
1121 this.hash = end_name_hash(hash);
1123 return lookup_hash(&this, base);
1125 return ERR_PTR(-EACCES);
1131 * is used by most simple commands to get the inode of a specified name.
1132 * Open, link etc use their own routines, but this is enough for things
1135 * namei exists in two versions: namei/lnamei. The only difference is
1136 * that namei follows links, while lnamei does not.
1139 int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1141 char *tmp = getname(name);
1142 int err = PTR_ERR(tmp);
1145 err = path_lookup(tmp, flags, nd);
1152 * It's inline, so penalty for filesystems that don't use sticky bit is
1155 static inline int check_sticky(struct inode *dir, struct inode *inode)
1157 if (!(dir->i_mode & S_ISVTX))
1159 if (inode->i_uid == current->fsuid)
1161 if (dir->i_uid == current->fsuid)
1163 return !capable(CAP_FOWNER);
1167 * Check whether we can remove a link victim from directory dir, check
1168 * whether the type of victim is right.
1169 * 1. We can't do it if dir is read-only (done in permission())
1170 * 2. We should have write and exec permissions on dir
1171 * 3. We can't remove anything from append-only dir
1172 * 4. We can't do anything with immutable dir (done in permission())
1173 * 5. If the sticky bit on dir is set we should either
1174 * a. be owner of dir, or
1175 * b. be owner of victim, or
1176 * c. have CAP_FOWNER capability
1177 * 6. If the victim is append-only or immutable we can't do antyhing with
1178 * links pointing to it.
1179 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1180 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1181 * 9. We can't remove a root or mountpoint.
1182 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1183 * nfs_async_unlink().
1185 static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1189 if (!victim->d_inode)
1192 BUG_ON(victim->d_parent->d_inode != dir);
1194 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1199 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1200 IS_IMMUTABLE(victim->d_inode))
1203 if (!S_ISDIR(victim->d_inode->i_mode))
1205 if (IS_ROOT(victim))
1207 } else if (S_ISDIR(victim->d_inode->i_mode))
1209 if (IS_DEADDIR(dir))
1211 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1216 /* Check whether we can create an object with dentry child in directory
1218 * 1. We can't do it if child already exists (open has special treatment for
1219 * this case, but since we are inlined it's OK)
1220 * 2. We can't do it if dir is read-only (done in permission())
1221 * 3. We should have write and exec permissions on dir
1222 * 4. We can't do it if dir is immutable (done in permission())
1224 static inline int may_create(struct inode *dir, struct dentry *child,
1225 struct nameidata *nd)
1229 if (IS_DEADDIR(dir))
1231 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1235 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1238 * O_DIRECTORY translates into forcing a directory lookup.
1240 static inline int lookup_flags(unsigned int f)
1242 unsigned long retval = LOOKUP_FOLLOW;
1245 retval &= ~LOOKUP_FOLLOW;
1247 if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1248 retval &= ~LOOKUP_FOLLOW;
1250 if (f & O_DIRECTORY)
1251 retval |= LOOKUP_DIRECTORY;
1257 * p1 and p2 should be directories on the same fs.
1259 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1264 down(&p1->d_inode->i_sem);
1268 down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1270 for (p = p1; p->d_parent != p; p = p->d_parent) {
1271 if (p->d_parent == p2) {
1272 down(&p2->d_inode->i_sem);
1273 down(&p1->d_inode->i_sem);
1278 for (p = p2; p->d_parent != p; p = p->d_parent) {
1279 if (p->d_parent == p1) {
1280 down(&p1->d_inode->i_sem);
1281 down(&p2->d_inode->i_sem);
1286 down(&p1->d_inode->i_sem);
1287 down(&p2->d_inode->i_sem);
1291 void unlock_rename(struct dentry *p1, struct dentry *p2)
1293 up(&p1->d_inode->i_sem);
1295 up(&p2->d_inode->i_sem);
1296 up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1300 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1301 struct nameidata *nd)
1303 int error = may_create(dir, dentry, nd);
1308 if (!dir->i_op || !dir->i_op->create)
1309 return -EACCES; /* shouldn't it be ENOSYS? */
1312 error = security_inode_create(dir, dentry, mode);
1316 error = dir->i_op->create(dir, dentry, mode, nd);
1318 fsnotify_create(dir, dentry->d_name.name);
1319 security_inode_post_create(dir, dentry, mode);
1324 int may_open(struct nameidata *nd, int acc_mode, int flag)
1326 struct dentry *dentry = nd->dentry;
1327 struct inode *inode = dentry->d_inode;
1333 if (S_ISLNK(inode->i_mode))
1336 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1339 error = permission(inode, acc_mode, nd);
1344 * FIFO's, sockets and device files are special: they don't
1345 * actually live on the filesystem itself, and as such you
1346 * can write to them even if the filesystem is read-only.
1348 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1350 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1351 if (nd->mnt->mnt_flags & MNT_NODEV)
1355 } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
1358 * An append-only file must be opened in append mode for writing.
1360 if (IS_APPEND(inode)) {
1361 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1367 /* O_NOATIME can only be set by the owner or superuser */
1368 if (flag & O_NOATIME)
1369 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1373 * Ensure there are no outstanding leases on the file.
1375 error = break_lease(inode, flag);
1379 if (flag & O_TRUNC) {
1380 error = get_write_access(inode);
1385 * Refuse to truncate files with mandatory locks held on them.
1387 error = locks_verify_locked(inode);
1391 error = do_truncate(dentry, 0);
1393 put_write_access(inode);
1397 if (flag & FMODE_WRITE)
1406 * namei for open - this is in fact almost the whole open-routine.
1408 * Note that the low bits of "flag" aren't the same as in the open
1409 * system call - they are 00 - no permissions needed
1410 * 01 - read permission needed
1411 * 10 - write permission needed
1412 * 11 - read/write permissions needed
1413 * which is a lot more logical, and also allows the "no perm" needed
1414 * for symlinks (where the permissions are checked later).
1417 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1419 int acc_mode, error = 0;
1424 acc_mode = ACC_MODE(flag);
1426 /* Allow the LSM permission hook to distinguish append
1427 access from general write access. */
1428 if (flag & O_APPEND)
1429 acc_mode |= MAY_APPEND;
1431 /* Fill in the open() intent data */
1432 nd->intent.open.flags = flag;
1433 nd->intent.open.create_mode = mode;
1436 * The simplest case - just a plain lookup.
1438 if (!(flag & O_CREAT)) {
1439 error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd);
1446 * Create - we need to know the parent.
1448 error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd);
1453 * We have the parent and last component. First of all, check
1454 * that we are not asked to creat(2) an obvious directory - that
1458 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1462 nd->flags &= ~LOOKUP_PARENT;
1463 down(&dir->d_inode->i_sem);
1464 path.dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1468 error = PTR_ERR(path.dentry);
1469 if (IS_ERR(path.dentry)) {
1470 up(&dir->d_inode->i_sem);
1474 /* Negative dentry, just create the file */
1475 if (!path.dentry->d_inode) {
1476 if (!IS_POSIXACL(dir->d_inode))
1477 mode &= ~current->fs->umask;
1478 error = vfs_create(dir->d_inode, path.dentry, mode, nd);
1479 up(&dir->d_inode->i_sem);
1481 nd->dentry = path.dentry;
1484 /* Don't check for write permission, don't truncate */
1491 * It already exists.
1493 up(&dir->d_inode->i_sem);
1499 if (__follow_mount(&path)) {
1501 if (flag & O_NOFOLLOW)
1505 if (!path.dentry->d_inode)
1507 if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
1511 nd->dentry = path.dentry;
1512 if (nd->mnt != path.mnt)
1516 if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
1519 error = may_open(nd, acc_mode, flag);
1526 if (nd->mnt != path.mnt)
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(path.dentry, nd);
1550 error = __do_follow_link(&path, nd);
1553 nd->flags &= ~LOOKUP_PARENT;
1554 if (nd->last_type == LAST_BIND)
1557 if (nd->last_type != LAST_NORM)
1559 if (nd->last.name[nd->last.len]) {
1560 putname(nd->last.name);
1565 putname(nd->last.name);
1569 down(&dir->d_inode->i_sem);
1570 path.dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1572 putname(nd->last.name);
1577 * lookup_create - lookup a dentry, creating it if it doesn't exist
1578 * @nd: nameidata info
1579 * @is_dir: directory flag
1581 * Simple function to lookup and return a dentry and create it
1582 * if it doesn't exist. Is SMP-safe.
1584 * Returns with nd->dentry->d_inode->i_sem locked.
1586 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1588 struct dentry *dentry = ERR_PTR(-EEXIST);
1590 down(&nd->dentry->d_inode->i_sem);
1592 * Yucky last component or no last component at all?
1593 * (foo/., foo/.., /////)
1595 if (nd->last_type != LAST_NORM)
1597 nd->flags &= ~LOOKUP_PARENT;
1600 * Do the final lookup.
1602 dentry = lookup_hash(&nd->last, nd->dentry);
1607 * Special case - lookup gave negative, but... we had foo/bar/
1608 * From the vfs_mknod() POV we just have a negative dentry -
1609 * all is fine. Let's be bastards - you had / on the end, you've
1610 * been asking for (non-existent) directory. -ENOENT for you.
1612 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1617 dentry = ERR_PTR(-ENOENT);
1621 EXPORT_SYMBOL_GPL(lookup_create);
1623 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1625 int error = may_create(dir, dentry, NULL);
1630 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1633 if (!dir->i_op || !dir->i_op->mknod)
1636 error = security_inode_mknod(dir, dentry, mode, dev);
1641 error = dir->i_op->mknod(dir, dentry, mode, dev);
1643 fsnotify_create(dir, dentry->d_name.name);
1644 security_inode_post_mknod(dir, dentry, mode, dev);
1649 asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
1653 struct dentry * dentry;
1654 struct nameidata nd;
1658 tmp = getname(filename);
1660 return PTR_ERR(tmp);
1662 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1665 dentry = lookup_create(&nd, 0);
1666 error = PTR_ERR(dentry);
1668 if (!IS_POSIXACL(nd.dentry->d_inode))
1669 mode &= ~current->fs->umask;
1670 if (!IS_ERR(dentry)) {
1671 switch (mode & S_IFMT) {
1672 case 0: case S_IFREG:
1673 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1675 case S_IFCHR: case S_IFBLK:
1676 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1677 new_decode_dev(dev));
1679 case S_IFIFO: case S_IFSOCK:
1680 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1690 up(&nd.dentry->d_inode->i_sem);
1698 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1700 int error = may_create(dir, dentry, NULL);
1705 if (!dir->i_op || !dir->i_op->mkdir)
1708 mode &= (S_IRWXUGO|S_ISVTX);
1709 error = security_inode_mkdir(dir, dentry, mode);
1714 error = dir->i_op->mkdir(dir, dentry, mode);
1716 fsnotify_mkdir(dir, dentry->d_name.name);
1717 security_inode_post_mkdir(dir,dentry, mode);
1722 asmlinkage long sys_mkdir(const char __user * pathname, int mode)
1727 tmp = getname(pathname);
1728 error = PTR_ERR(tmp);
1730 struct dentry *dentry;
1731 struct nameidata nd;
1733 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1736 dentry = lookup_create(&nd, 1);
1737 error = PTR_ERR(dentry);
1738 if (!IS_ERR(dentry)) {
1739 if (!IS_POSIXACL(nd.dentry->d_inode))
1740 mode &= ~current->fs->umask;
1741 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1744 up(&nd.dentry->d_inode->i_sem);
1754 * We try to drop the dentry early: we should have
1755 * a usage count of 2 if we're the only user of this
1756 * dentry, and if that is true (possibly after pruning
1757 * the dcache), then we drop the dentry now.
1759 * A low-level filesystem can, if it choses, legally
1762 * if (!d_unhashed(dentry))
1765 * if it cannot handle the case of removing a directory
1766 * that is still in use by something else..
1768 void dentry_unhash(struct dentry *dentry)
1771 if (atomic_read(&dentry->d_count))
1772 shrink_dcache_parent(dentry);
1773 spin_lock(&dcache_lock);
1774 spin_lock(&dentry->d_lock);
1775 if (atomic_read(&dentry->d_count) == 2)
1777 spin_unlock(&dentry->d_lock);
1778 spin_unlock(&dcache_lock);
1781 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1783 int error = may_delete(dir, dentry, 1);
1788 if (!dir->i_op || !dir->i_op->rmdir)
1793 down(&dentry->d_inode->i_sem);
1794 dentry_unhash(dentry);
1795 if (d_mountpoint(dentry))
1798 error = security_inode_rmdir(dir, dentry);
1800 error = dir->i_op->rmdir(dir, dentry);
1802 dentry->d_inode->i_flags |= S_DEAD;
1805 up(&dentry->d_inode->i_sem);
1814 asmlinkage long sys_rmdir(const char __user * pathname)
1818 struct dentry *dentry;
1819 struct nameidata nd;
1821 name = getname(pathname);
1823 return PTR_ERR(name);
1825 error = path_lookup(name, LOOKUP_PARENT, &nd);
1829 switch(nd.last_type) {
1840 down(&nd.dentry->d_inode->i_sem);
1841 dentry = lookup_hash(&nd.last, nd.dentry);
1842 error = PTR_ERR(dentry);
1843 if (!IS_ERR(dentry)) {
1844 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1847 up(&nd.dentry->d_inode->i_sem);
1855 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1857 int error = may_delete(dir, dentry, 0);
1862 if (!dir->i_op || !dir->i_op->unlink)
1867 down(&dentry->d_inode->i_sem);
1868 if (d_mountpoint(dentry))
1871 error = security_inode_unlink(dir, dentry);
1873 error = dir->i_op->unlink(dir, dentry);
1875 up(&dentry->d_inode->i_sem);
1877 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1878 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1886 * Make sure that the actual truncation of the file will occur outside its
1887 * directory's i_sem. Truncate can take a long time if there is a lot of
1888 * writeout happening, and we don't want to prevent access to the directory
1889 * while waiting on the I/O.
1891 asmlinkage long sys_unlink(const char __user * pathname)
1895 struct dentry *dentry;
1896 struct nameidata nd;
1897 struct inode *inode = NULL;
1899 name = getname(pathname);
1901 return PTR_ERR(name);
1903 error = path_lookup(name, LOOKUP_PARENT, &nd);
1907 if (nd.last_type != LAST_NORM)
1909 down(&nd.dentry->d_inode->i_sem);
1910 dentry = lookup_hash(&nd.last, nd.dentry);
1911 error = PTR_ERR(dentry);
1912 if (!IS_ERR(dentry)) {
1913 /* Why not before? Because we want correct error value */
1914 if (nd.last.name[nd.last.len])
1916 inode = dentry->d_inode;
1918 atomic_inc(&inode->i_count);
1919 error = vfs_unlink(nd.dentry->d_inode, dentry);
1923 up(&nd.dentry->d_inode->i_sem);
1925 iput(inode); /* truncate the inode here */
1933 error = !dentry->d_inode ? -ENOENT :
1934 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1938 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
1940 int error = may_create(dir, dentry, NULL);
1945 if (!dir->i_op || !dir->i_op->symlink)
1948 error = security_inode_symlink(dir, dentry, oldname);
1953 error = dir->i_op->symlink(dir, dentry, oldname);
1955 fsnotify_create(dir, dentry->d_name.name);
1956 security_inode_post_symlink(dir, dentry, oldname);
1961 asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
1967 from = getname(oldname);
1969 return PTR_ERR(from);
1970 to = getname(newname);
1971 error = PTR_ERR(to);
1973 struct dentry *dentry;
1974 struct nameidata nd;
1976 error = path_lookup(to, LOOKUP_PARENT, &nd);
1979 dentry = lookup_create(&nd, 0);
1980 error = PTR_ERR(dentry);
1981 if (!IS_ERR(dentry)) {
1982 error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
1985 up(&nd.dentry->d_inode->i_sem);
1994 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1996 struct inode *inode = old_dentry->d_inode;
2002 error = may_create(dir, new_dentry, NULL);
2006 if (dir->i_sb != inode->i_sb)
2010 * A link to an append-only or immutable file cannot be created.
2012 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2014 if (!dir->i_op || !dir->i_op->link)
2016 if (S_ISDIR(old_dentry->d_inode->i_mode))
2019 error = security_inode_link(old_dentry, dir, new_dentry);
2023 down(&old_dentry->d_inode->i_sem);
2025 error = dir->i_op->link(old_dentry, dir, new_dentry);
2026 up(&old_dentry->d_inode->i_sem);
2028 fsnotify_create(dir, new_dentry->d_name.name);
2029 security_inode_post_link(old_dentry, dir, new_dentry);
2035 * Hardlinks are often used in delicate situations. We avoid
2036 * security-related surprises by not following symlinks on the
2039 * We don't follow them on the oldname either to be compatible
2040 * with linux 2.0, and to avoid hard-linking to directories
2041 * and other special files. --ADM
2043 asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
2045 struct dentry *new_dentry;
2046 struct nameidata nd, old_nd;
2050 to = getname(newname);
2054 error = __user_walk(oldname, 0, &old_nd);
2057 error = path_lookup(to, LOOKUP_PARENT, &nd);
2061 if (old_nd.mnt != nd.mnt)
2063 new_dentry = lookup_create(&nd, 0);
2064 error = PTR_ERR(new_dentry);
2065 if (!IS_ERR(new_dentry)) {
2066 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
2069 up(&nd.dentry->d_inode->i_sem);
2073 path_release(&old_nd);
2081 * The worst of all namespace operations - renaming directory. "Perverted"
2082 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2084 * a) we can get into loop creation. Check is done in is_subdir().
2085 * b) race potential - two innocent renames can create a loop together.
2086 * That's where 4.4 screws up. Current fix: serialization on
2087 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
2089 * c) we have to lock _three_ objects - parents and victim (if it exists).
2090 * And that - after we got ->i_sem on parents (until then we don't know
2091 * whether the target exists). Solution: try to be smart with locking
2092 * order for inodes. We rely on the fact that tree topology may change
2093 * only under ->s_vfs_rename_sem _and_ that parent of the object we
2094 * move will be locked. Thus we can rank directories by the tree
2095 * (ancestors first) and rank all non-directories after them.
2096 * That works since everybody except rename does "lock parent, lookup,
2097 * lock child" and rename is under ->s_vfs_rename_sem.
2098 * HOWEVER, it relies on the assumption that any object with ->lookup()
2099 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2100 * we'd better make sure that there's no link(2) for them.
2101 * d) some filesystems don't support opened-but-unlinked directories,
2102 * either because of layout or because they are not ready to deal with
2103 * all cases correctly. The latter will be fixed (taking this sort of
2104 * stuff into VFS), but the former is not going away. Solution: the same
2105 * trick as in rmdir().
2106 * e) conversion from fhandle to dentry may come in the wrong moment - when
2107 * we are removing the target. Solution: we will have to grab ->i_sem
2108 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2109 * ->i_sem on parents, which works but leads to some truely excessive
2112 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2113 struct inode *new_dir, struct dentry *new_dentry)
2116 struct inode *target;
2119 * If we are going to change the parent - check write permissions,
2120 * we'll need to flip '..'.
2122 if (new_dir != old_dir) {
2123 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2128 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2132 target = new_dentry->d_inode;
2134 down(&target->i_sem);
2135 dentry_unhash(new_dentry);
2137 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2140 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2143 target->i_flags |= S_DEAD;
2145 if (d_unhashed(new_dentry))
2146 d_rehash(new_dentry);
2150 d_move(old_dentry,new_dentry);
2151 security_inode_post_rename(old_dir, old_dentry,
2152 new_dir, new_dentry);
2157 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2158 struct inode *new_dir, struct dentry *new_dentry)
2160 struct inode *target;
2163 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2168 target = new_dentry->d_inode;
2170 down(&target->i_sem);
2171 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2174 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2176 /* The following d_move() should become unconditional */
2177 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
2178 d_move(old_dentry, new_dentry);
2179 security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
2187 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2188 struct inode *new_dir, struct dentry *new_dentry)
2191 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2192 const char *old_name;
2194 if (old_dentry->d_inode == new_dentry->d_inode)
2197 error = may_delete(old_dir, old_dentry, is_dir);
2201 if (!new_dentry->d_inode)
2202 error = may_create(new_dir, new_dentry, NULL);
2204 error = may_delete(new_dir, new_dentry, is_dir);
2208 if (!old_dir->i_op || !old_dir->i_op->rename)
2211 DQUOT_INIT(old_dir);
2212 DQUOT_INIT(new_dir);
2214 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
2217 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2219 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2221 const char *new_name = old_dentry->d_name.name;
2222 fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
2223 new_dentry->d_inode, old_dentry->d_inode);
2225 fsnotify_oldname_free(old_name);
2230 static inline int do_rename(const char * oldname, const char * newname)
2233 struct dentry * old_dir, * new_dir;
2234 struct dentry * old_dentry, *new_dentry;
2235 struct dentry * trap;
2236 struct nameidata oldnd, newnd;
2238 error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2242 error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2247 if (oldnd.mnt != newnd.mnt)
2250 old_dir = oldnd.dentry;
2252 if (oldnd.last_type != LAST_NORM)
2255 new_dir = newnd.dentry;
2256 if (newnd.last_type != LAST_NORM)
2259 trap = lock_rename(new_dir, old_dir);
2261 old_dentry = lookup_hash(&oldnd.last, old_dir);
2262 error = PTR_ERR(old_dentry);
2263 if (IS_ERR(old_dentry))
2265 /* source must exist */
2267 if (!old_dentry->d_inode)
2269 /* unless the source is a directory trailing slashes give -ENOTDIR */
2270 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2272 if (oldnd.last.name[oldnd.last.len])
2274 if (newnd.last.name[newnd.last.len])
2277 /* source should not be ancestor of target */
2279 if (old_dentry == trap)
2281 new_dentry = lookup_hash(&newnd.last, new_dir);
2282 error = PTR_ERR(new_dentry);
2283 if (IS_ERR(new_dentry))
2285 /* target should not be an ancestor of source */
2287 if (new_dentry == trap)
2290 error = vfs_rename(old_dir->d_inode, old_dentry,
2291 new_dir->d_inode, new_dentry);
2297 unlock_rename(new_dir, old_dir);
2299 path_release(&newnd);
2301 path_release(&oldnd);
2306 asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
2312 from = getname(oldname);
2314 return PTR_ERR(from);
2315 to = getname(newname);
2316 error = PTR_ERR(to);
2318 error = do_rename(from,to);
2325 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2329 len = PTR_ERR(link);
2334 if (len > (unsigned) buflen)
2336 if (copy_to_user(buffer, link, len))
2343 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2344 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2345 * using) it for any given inode is up to filesystem.
2347 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2349 struct nameidata nd;
2353 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
2354 if (!IS_ERR(cookie)) {
2355 int res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2356 if (dentry->d_inode->i_op->put_link)
2357 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2358 cookie = ERR_PTR(res);
2360 return PTR_ERR(cookie);
2363 int vfs_follow_link(struct nameidata *nd, const char *link)
2365 return __vfs_follow_link(nd, link);
2368 /* get the link contents into pagecache */
2369 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2372 struct address_space *mapping = dentry->d_inode->i_mapping;
2373 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2377 wait_on_page_locked(page);
2378 if (!PageUptodate(page))
2384 page_cache_release(page);
2385 return ERR_PTR(-EIO);
2391 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2393 struct page *page = NULL;
2394 char *s = page_getlink(dentry, &page);
2395 int res = vfs_readlink(dentry,buffer,buflen,s);
2398 page_cache_release(page);
2403 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2405 struct page *page = NULL;
2406 nd_set_link(nd, page_getlink(dentry, &page));
2410 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
2412 struct page *page = cookie;
2416 page_cache_release(page);
2420 int page_symlink(struct inode *inode, const char *symname, int len)
2422 struct address_space *mapping = inode->i_mapping;
2423 struct page *page = grab_cache_page(mapping, 0);
2429 err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2432 kaddr = kmap_atomic(page, KM_USER0);
2433 memcpy(kaddr, symname, len-1);
2434 kunmap_atomic(kaddr, KM_USER0);
2435 mapping->a_ops->commit_write(NULL, page, 0, len-1);
2437 * Notice that we are _not_ going to block here - end of page is
2438 * unmapped, so this will only try to map the rest of page, see
2439 * that it is unmapped (typically even will not look into inode -
2440 * ->i_size will be enough for everything) and zero it out.
2441 * OTOH it's obviously correct and should make the page up-to-date.
2443 if (!PageUptodate(page)) {
2444 err = mapping->a_ops->readpage(NULL, page);
2445 wait_on_page_locked(page);
2449 page_cache_release(page);
2452 mark_inode_dirty(inode);
2456 page_cache_release(page);
2461 struct inode_operations page_symlink_inode_operations = {
2462 .readlink = generic_readlink,
2463 .follow_link = page_follow_link_light,
2464 .put_link = page_put_link,
2467 EXPORT_SYMBOL(__user_walk);
2468 EXPORT_SYMBOL(follow_down);
2469 EXPORT_SYMBOL(follow_up);
2470 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2471 EXPORT_SYMBOL(getname);
2472 EXPORT_SYMBOL(lock_rename);
2473 EXPORT_SYMBOL(lookup_hash);
2474 EXPORT_SYMBOL(lookup_one_len);
2475 EXPORT_SYMBOL(page_follow_link_light);
2476 EXPORT_SYMBOL(page_put_link);
2477 EXPORT_SYMBOL(page_readlink);
2478 EXPORT_SYMBOL(page_symlink);
2479 EXPORT_SYMBOL(page_symlink_inode_operations);
2480 EXPORT_SYMBOL(path_lookup);
2481 EXPORT_SYMBOL(path_release);
2482 EXPORT_SYMBOL(path_walk);
2483 EXPORT_SYMBOL(permission);
2484 EXPORT_SYMBOL(unlock_rename);
2485 EXPORT_SYMBOL(vfs_create);
2486 EXPORT_SYMBOL(vfs_follow_link);
2487 EXPORT_SYMBOL(vfs_link);
2488 EXPORT_SYMBOL(vfs_mkdir);
2489 EXPORT_SYMBOL(vfs_mknod);
2490 EXPORT_SYMBOL(generic_permission);
2491 EXPORT_SYMBOL(vfs_readlink);
2492 EXPORT_SYMBOL(vfs_rename);
2493 EXPORT_SYMBOL(vfs_rmdir);
2494 EXPORT_SYMBOL(vfs_symlink);
2495 EXPORT_SYMBOL(vfs_unlink);
2496 EXPORT_SYMBOL(dentry_unhash);
2497 EXPORT_SYMBOL(generic_readlink);