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 <asm/namei.h>
31 #include <asm/uaccess.h>
33 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
35 /* [Feb-1997 T. Schoebel-Theuer]
36 * Fundamental changes in the pathname lookup mechanisms (namei)
37 * were necessary because of omirr. The reason is that omirr needs
38 * to know the _real_ pathname, not the user-supplied one, in case
39 * of symlinks (and also when transname replacements occur).
41 * The new code replaces the old recursive symlink resolution with
42 * an iterative one (in case of non-nested symlink chains). It does
43 * this with calls to <fs>_follow_link().
44 * As a side effect, dir_namei(), _namei() and follow_link() are now
45 * replaced with a single function lookup_dentry() that can handle all
46 * the special cases of the former code.
48 * With the new dcache, the pathname is stored at each inode, at least as
49 * long as the refcount of the inode is positive. As a side effect, the
50 * size of the dcache depends on the inode cache and thus is dynamic.
52 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
53 * resolution to correspond with current state of the code.
55 * Note that the symlink resolution is not *completely* iterative.
56 * There is still a significant amount of tail- and mid- recursion in
57 * the algorithm. Also, note that <fs>_readlink() is not used in
58 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
59 * may return different results than <fs>_follow_link(). Many virtual
60 * filesystems (including /proc) exhibit this behavior.
63 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
64 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
65 * and the name already exists in form of a symlink, try to create the new
66 * name indicated by the symlink. The old code always complained that the
67 * name already exists, due to not following the symlink even if its target
68 * is nonexistent. The new semantics affects also mknod() and link() when
69 * the name is a symlink pointing to a non-existant name.
71 * I don't know which semantics is the right one, since I have no access
72 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
73 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
74 * "old" one. Personally, I think the new semantics is much more logical.
75 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
76 * file does succeed in both HP-UX and SunOs, but not in Solaris
77 * and in the old Linux semantics.
80 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
81 * semantics. See the comments in "open_namei" and "do_link" below.
83 * [10-Sep-98 Alan Modra] Another symlink change.
86 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
87 * inside the path - always follow.
88 * in the last component in creation/removal/renaming - never follow.
89 * if LOOKUP_FOLLOW passed - follow.
90 * if the pathname has trailing slashes - follow.
91 * otherwise - don't follow.
92 * (applied in that order).
94 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
95 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
96 * During the 2.4 we need to fix the userland stuff depending on it -
97 * hopefully we will be able to get rid of that wart in 2.5. So far only
98 * XEmacs seems to be relying on it...
101 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
102 * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives
103 * any extra contention...
106 /* In order to reduce some races, while at the same time doing additional
107 * checking and hopefully speeding things up, we copy filenames to the
108 * kernel data space before using them..
110 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
111 * PATH_MAX includes the nul terminator --RR.
113 static inline int do_getname(const char __user *filename, char *page)
116 unsigned long len = PATH_MAX;
118 if ((unsigned long) filename >= TASK_SIZE) {
119 if (!segment_eq(get_fs(), KERNEL_DS))
121 } else if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
122 len = TASK_SIZE - (unsigned long) filename;
124 retval = strncpy_from_user((char *)page, filename, len);
128 return -ENAMETOOLONG;
134 char * getname(const char __user * filename)
138 result = ERR_PTR(-ENOMEM);
141 int retval = do_getname(filename, tmp);
146 result = ERR_PTR(retval);
149 if (unlikely(current->audit_context) && !IS_ERR(result) && result)
150 audit_getname(result);
157 * is used to check for read/write/execute permissions on a file.
158 * We use "fsuid" for this, letting us set arbitrary permissions
159 * for filesystem access without changing the "normal" uids which
160 * are used for other things..
162 int vfs_permission(struct inode * inode, int mask)
164 umode_t mode = inode->i_mode;
166 if (mask & MAY_WRITE) {
168 * Nobody gets write access to a read-only fs.
170 if (IS_RDONLY(inode) &&
171 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
175 * Nobody gets write access to an immutable file.
177 if (IS_IMMUTABLE(inode))
181 if (current->fsuid == inode->i_uid)
183 else if (in_group_p(inode->i_gid))
187 * If the DACs are ok we don't need any capability check.
189 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
193 * Read/write DACs are always overridable.
194 * Executable DACs are overridable if at least one exec bit is set.
196 if (!(mask & MAY_EXEC) ||
197 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
198 if (capable(CAP_DAC_OVERRIDE))
202 * Searching includes executable on directories, else just read.
204 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
205 if (capable(CAP_DAC_READ_SEARCH))
211 int permission(struct inode * inode,int mask, struct nameidata *nd)
216 /* Ordinary permission routines do not understand MAY_APPEND. */
217 submask = mask & ~MAY_APPEND;
219 if (inode->i_op && inode->i_op->permission)
220 retval = inode->i_op->permission(inode, submask, nd);
222 retval = vfs_permission(inode, submask);
226 return security_inode_permission(inode, mask, nd);
230 * get_write_access() gets write permission for a file.
231 * put_write_access() releases this write permission.
232 * This is used for regular files.
233 * We cannot support write (and maybe mmap read-write shared) accesses and
234 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
235 * can have the following values:
236 * 0: no writers, no VM_DENYWRITE mappings
237 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
238 * > 0: (i_writecount) users are writing to the file.
240 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
241 * except for the cases where we don't hold i_writecount yet. Then we need to
242 * use {get,deny}_write_access() - these functions check the sign and refuse
243 * to do the change if sign is wrong. Exclusion between them is provided by
244 * the inode->i_lock spinlock.
247 int get_write_access(struct inode * inode)
249 spin_lock(&inode->i_lock);
250 if (atomic_read(&inode->i_writecount) < 0) {
251 spin_unlock(&inode->i_lock);
254 atomic_inc(&inode->i_writecount);
255 spin_unlock(&inode->i_lock);
260 int deny_write_access(struct file * file)
262 struct inode *inode = file->f_dentry->d_inode;
264 spin_lock(&inode->i_lock);
265 if (atomic_read(&inode->i_writecount) > 0) {
266 spin_unlock(&inode->i_lock);
269 atomic_dec(&inode->i_writecount);
270 spin_unlock(&inode->i_lock);
275 void path_release(struct nameidata *nd)
282 * umount() mustn't call path_release()/mntput() as that would clear
285 void path_release_on_umount(struct nameidata *nd)
292 * Internal lookup() using the new generic dcache.
295 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
297 struct dentry * dentry = __d_lookup(parent, name);
299 /* lockess __d_lookup may fail due to concurrent d_move()
300 * in some unrelated directory, so try with d_lookup
303 dentry = d_lookup(parent, name);
305 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
306 if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
315 * Short-cut version of permission(), for calling by
316 * path_walk(), when dcache lock is held. Combines parts
317 * of permission() and vfs_permission(), and tests ONLY for
318 * MAY_EXEC permission.
320 * If appropriate, check DAC only. If not appropriate, or
321 * short-cut DAC fails, then call permission() to do more
322 * complete permission check.
324 static inline int exec_permission_lite(struct inode *inode,
325 struct nameidata *nd)
327 umode_t mode = inode->i_mode;
329 if (inode->i_op && inode->i_op->permission)
332 if (current->fsuid == inode->i_uid)
334 else if (in_group_p(inode->i_gid))
340 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
343 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
346 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
351 return security_inode_permission(inode, MAY_EXEC, nd);
355 * This is called when everything else fails, and we actually have
356 * to go to the low-level filesystem to find out what we should do..
358 * We get the directory semaphore, and after getting that we also
359 * make sure that nobody added the entry to the dcache in the meantime..
362 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
364 struct dentry * result;
365 struct inode *dir = parent->d_inode;
369 * First re-do the cached lookup just in case it was created
370 * while we waited for the directory semaphore..
372 * FIXME! This could use version numbering or similar to
373 * avoid unnecessary cache lookups.
375 * The "dcache_lock" is purely to protect the RCU list walker
376 * from concurrent renames at this point (we mustn't get false
377 * negatives from the RCU list walk here, unlike the optimistic
380 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
382 result = d_lookup(parent, name);
384 struct dentry * dentry = d_alloc(parent, name);
385 result = ERR_PTR(-ENOMEM);
387 result = dir->i_op->lookup(dir, dentry, nd);
398 * Uhhuh! Nasty case: the cache was re-populated while
399 * we waited on the semaphore. Need to revalidate.
402 if (result->d_op && result->d_op->d_revalidate) {
403 if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
405 result = ERR_PTR(-ENOENT);
411 static int __emul_lookup_dentry(const char *, struct nameidata *);
415 walk_init_root(const char *name, struct nameidata *nd)
417 read_lock(¤t->fs->lock);
418 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
419 nd->mnt = mntget(current->fs->altrootmnt);
420 nd->dentry = dget(current->fs->altroot);
421 read_unlock(¤t->fs->lock);
422 if (__emul_lookup_dentry(name,nd))
424 read_lock(¤t->fs->lock);
426 nd->mnt = mntget(current->fs->rootmnt);
427 nd->dentry = dget(current->fs->root);
428 read_unlock(¤t->fs->lock);
432 static inline int __vfs_follow_link(struct nameidata *nd, const char *link)
441 if (!walk_init_root(link, nd))
442 /* weird __emul_prefix() stuff did it */
445 res = link_path_walk(link, nd);
447 if (nd->depth || res || nd->last_type!=LAST_NORM)
450 * If it is an iterative symlinks resolution in open_namei() we
451 * have to copy the last component. And all that crap because of
452 * bloody create() on broken symlinks. Furrfu...
455 if (unlikely(!name)) {
459 strcpy(name, nd->last.name);
460 nd->last.name = name;
464 return PTR_ERR(link);
468 * This limits recursive symlink follows to 8, while
469 * limiting consecutive symlinks to 40.
471 * Without that kind of total limit, nasty chains of consecutive
472 * symlinks can cause almost arbitrarily long lookups.
474 static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd)
477 if (current->link_count >= MAX_NESTED_LINKS)
479 if (current->total_link_count >= 40)
481 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
483 err = security_inode_follow_link(dentry, nd);
486 current->link_count++;
487 current->total_link_count++;
489 touch_atime(nd->mnt, dentry);
490 nd_set_link(nd, NULL);
491 err = dentry->d_inode->i_op->follow_link(dentry, nd);
493 char *s = nd_get_link(nd);
495 err = __vfs_follow_link(nd, s);
496 if (dentry->d_inode->i_op->put_link)
497 dentry->d_inode->i_op->put_link(dentry, nd);
499 current->link_count--;
507 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
509 struct vfsmount *parent;
510 struct dentry *mountpoint;
511 spin_lock(&vfsmount_lock);
512 parent=(*mnt)->mnt_parent;
513 if (parent == *mnt) {
514 spin_unlock(&vfsmount_lock);
518 mountpoint=dget((*mnt)->mnt_mountpoint);
519 spin_unlock(&vfsmount_lock);
521 *dentry = mountpoint;
527 /* no need for dcache_lock, as serialization is taken care in
530 static int follow_mount(struct vfsmount **mnt, struct dentry **dentry)
533 while (d_mountpoint(*dentry)) {
534 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
540 *dentry = dget(mounted->mnt_root);
546 /* no need for dcache_lock, as serialization is taken care in
549 static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
551 struct vfsmount *mounted;
553 mounted = lookup_mnt(*mnt, *dentry);
558 *dentry = dget(mounted->mnt_root);
564 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
566 return __follow_down(mnt,dentry);
569 static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry)
572 struct vfsmount *parent;
573 struct dentry *old = *dentry;
575 read_lock(¤t->fs->lock);
576 if (*dentry == current->fs->root &&
577 *mnt == current->fs->rootmnt) {
578 read_unlock(¤t->fs->lock);
581 read_unlock(¤t->fs->lock);
582 spin_lock(&dcache_lock);
583 if (*dentry != (*mnt)->mnt_root) {
584 *dentry = dget((*dentry)->d_parent);
585 spin_unlock(&dcache_lock);
589 spin_unlock(&dcache_lock);
590 spin_lock(&vfsmount_lock);
591 parent = (*mnt)->mnt_parent;
592 if (parent == *mnt) {
593 spin_unlock(&vfsmount_lock);
597 *dentry = dget((*mnt)->mnt_mountpoint);
598 spin_unlock(&vfsmount_lock);
603 follow_mount(mnt, dentry);
607 struct vfsmount *mnt;
608 struct dentry *dentry;
612 * It's more convoluted than I'd like it to be, but... it's still fairly
613 * small and for now I'd prefer to have fast path as straight as possible.
614 * It _is_ time-critical.
616 static int do_lookup(struct nameidata *nd, struct qstr *name,
619 struct vfsmount *mnt = nd->mnt;
620 struct dentry *dentry = __d_lookup(nd->dentry, name);
624 if (dentry->d_op && dentry->d_op->d_revalidate)
625 goto need_revalidate;
628 path->dentry = dentry;
632 dentry = real_lookup(nd->dentry, name, nd);
638 if (dentry->d_op->d_revalidate(dentry, nd))
640 if (d_invalidate(dentry))
646 return PTR_ERR(dentry);
652 * This is the basic name resolution function, turning a pathname
653 * into the final dentry.
655 * We expect 'base' to be positive and a directory.
657 int fastcall link_path_walk(const char * name, struct nameidata *nd)
662 unsigned int lookup_flags = nd->flags;
669 inode = nd->dentry->d_inode;
671 lookup_flags = LOOKUP_FOLLOW;
673 /* At this point we know we have a real path component. */
679 err = exec_permission_lite(inode, nd);
680 if (err == -EAGAIN) {
681 err = permission(inode, MAY_EXEC, nd);
687 c = *(const unsigned char *)name;
689 hash = init_name_hash();
692 hash = partial_name_hash(c, hash);
693 c = *(const unsigned char *)name;
694 } while (c && (c != '/'));
695 this.len = name - (const char *) this.name;
696 this.hash = end_name_hash(hash);
698 /* remove trailing slashes? */
701 while (*++name == '/');
703 goto last_with_slashes;
706 * "." and ".." are special - ".." especially so because it has
707 * to be able to know about the current root directory and
708 * parent relationships.
710 if (this.name[0] == '.') switch (this.len) {
714 if (this.name[1] != '.')
716 follow_dotdot(&nd->mnt, &nd->dentry);
717 inode = nd->dentry->d_inode;
723 * See if the low-level filesystem might want
724 * to use its own hash..
726 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
727 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
731 nd->flags |= LOOKUP_CONTINUE;
732 /* This does the actual lookups.. */
733 err = do_lookup(nd, &this, &next);
736 /* Check mountpoints.. */
737 follow_mount(&next.mnt, &next.dentry);
740 inode = next.dentry->d_inode;
747 if (inode->i_op->follow_link) {
749 err = do_follow_link(next.dentry, nd);
755 inode = nd->dentry->d_inode;
764 nd->dentry = next.dentry;
767 if (!inode->i_op->lookup)
770 /* here ends the main loop */
773 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
775 nd->flags &= ~LOOKUP_CONTINUE;
776 if (lookup_flags & LOOKUP_PARENT)
778 if (this.name[0] == '.') switch (this.len) {
782 if (this.name[1] != '.')
784 follow_dotdot(&nd->mnt, &nd->dentry);
785 inode = nd->dentry->d_inode;
790 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
791 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
795 err = do_lookup(nd, &this, &next);
798 follow_mount(&next.mnt, &next.dentry);
799 inode = next.dentry->d_inode;
800 if ((lookup_flags & LOOKUP_FOLLOW)
801 && inode && inode->i_op && inode->i_op->follow_link) {
803 err = do_follow_link(next.dentry, nd);
808 inode = nd->dentry->d_inode;
812 nd->dentry = next.dentry;
817 if (lookup_flags & LOOKUP_DIRECTORY) {
819 if (!inode->i_op || !inode->i_op->lookup)
825 nd->last_type = LAST_NORM;
826 if (this.name[0] != '.')
829 nd->last_type = LAST_DOT;
830 else if (this.len == 2 && this.name[1] == '.')
831 nd->last_type = LAST_DOTDOT;
836 * We bypassed the ordinary revalidation routines.
837 * We may need to check the cached dentry for staleness.
839 if (nd->dentry && nd->dentry->d_sb &&
840 (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
842 /* Note: we do not d_invalidate() */
843 if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
857 int fastcall path_walk(const char * name, struct nameidata *nd)
859 current->total_link_count = 0;
860 return link_path_walk(name, nd);
864 /* returns 1 if everything is done */
865 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
867 if (path_walk(name, nd))
868 return 0; /* something went wrong... */
870 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
871 struct nameidata nd_root;
873 * NAME was not found in alternate root or it's a directory. Try to find
874 * it in the normal root:
876 nd_root.last_type = LAST_ROOT;
877 nd_root.flags = nd->flags;
879 memcpy(&nd_root.intent, &nd->intent, sizeof(nd_root.intent));
880 read_lock(¤t->fs->lock);
881 nd_root.mnt = mntget(current->fs->rootmnt);
882 nd_root.dentry = dget(current->fs->root);
883 read_unlock(¤t->fs->lock);
884 if (path_walk(name, &nd_root))
886 if (nd_root.dentry->d_inode) {
888 nd->dentry = nd_root.dentry;
889 nd->mnt = nd_root.mnt;
890 nd->last = nd_root.last;
893 path_release(&nd_root);
898 void set_fs_altroot(void)
900 char *emul = __emul_prefix();
902 struct vfsmount *mnt = NULL, *oldmnt;
903 struct dentry *dentry = NULL, *olddentry;
908 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
914 write_lock(¤t->fs->lock);
915 oldmnt = current->fs->altrootmnt;
916 olddentry = current->fs->altroot;
917 current->fs->altrootmnt = mnt;
918 current->fs->altroot = dentry;
919 write_unlock(¤t->fs->lock);
926 int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
930 nd->last_type = LAST_ROOT; /* if there are only slashes... */
934 read_lock(¤t->fs->lock);
936 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
937 nd->mnt = mntget(current->fs->altrootmnt);
938 nd->dentry = dget(current->fs->altroot);
939 read_unlock(¤t->fs->lock);
940 if (__emul_lookup_dentry(name,nd))
942 read_lock(¤t->fs->lock);
944 nd->mnt = mntget(current->fs->rootmnt);
945 nd->dentry = dget(current->fs->root);
948 nd->mnt = mntget(current->fs->pwdmnt);
949 nd->dentry = dget(current->fs->pwd);
951 read_unlock(¤t->fs->lock);
952 current->total_link_count = 0;
953 retval = link_path_walk(name, nd);
954 if (unlikely(current->audit_context
955 && nd && nd->dentry && nd->dentry->d_inode))
957 nd->dentry->d_inode->i_ino,
958 nd->dentry->d_inode->i_rdev);
963 * Restricted form of lookup. Doesn't follow links, single-component only,
964 * needs parent already locked. Doesn't follow mounts.
967 static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
969 struct dentry * dentry;
973 inode = base->d_inode;
974 err = permission(inode, MAY_EXEC, nd);
975 dentry = ERR_PTR(err);
980 * See if the low-level filesystem might want
981 * to use its own hash..
983 if (base->d_op && base->d_op->d_hash) {
984 err = base->d_op->d_hash(base, name);
985 dentry = ERR_PTR(err);
990 dentry = cached_lookup(base, name, nd);
992 struct dentry *new = d_alloc(base, name);
993 dentry = ERR_PTR(-ENOMEM);
996 dentry = inode->i_op->lookup(inode, new, nd);
1006 struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
1008 return __lookup_hash(name, base, NULL);
1012 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
1023 hash = init_name_hash();
1025 c = *(const unsigned char *)name++;
1026 if (c == '/' || c == '\0')
1028 hash = partial_name_hash(c, hash);
1030 this.hash = end_name_hash(hash);
1032 return lookup_hash(&this, base);
1034 return ERR_PTR(-EACCES);
1040 * is used by most simple commands to get the inode of a specified name.
1041 * Open, link etc use their own routines, but this is enough for things
1044 * namei exists in two versions: namei/lnamei. The only difference is
1045 * that namei follows links, while lnamei does not.
1048 int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1050 char *tmp = getname(name);
1051 int err = PTR_ERR(tmp);
1054 err = path_lookup(tmp, flags, nd);
1061 * It's inline, so penalty for filesystems that don't use sticky bit is
1064 static inline int check_sticky(struct inode *dir, struct inode *inode)
1066 if (!(dir->i_mode & S_ISVTX))
1068 if (inode->i_uid == current->fsuid)
1070 if (dir->i_uid == current->fsuid)
1072 return !capable(CAP_FOWNER);
1076 * Check whether we can remove a link victim from directory dir, check
1077 * whether the type of victim is right.
1078 * 1. We can't do it if dir is read-only (done in permission())
1079 * 2. We should have write and exec permissions on dir
1080 * 3. We can't remove anything from append-only dir
1081 * 4. We can't do anything with immutable dir (done in permission())
1082 * 5. If the sticky bit on dir is set we should either
1083 * a. be owner of dir, or
1084 * b. be owner of victim, or
1085 * c. have CAP_FOWNER capability
1086 * 6. If the victim is append-only or immutable we can't do antyhing with
1087 * links pointing to it.
1088 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1089 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1090 * 9. We can't remove a root or mountpoint.
1091 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1092 * nfs_async_unlink().
1094 static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1097 if (!victim->d_inode || victim->d_parent->d_inode != dir)
1099 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1104 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1105 IS_IMMUTABLE(victim->d_inode))
1108 if (!S_ISDIR(victim->d_inode->i_mode))
1110 if (IS_ROOT(victim))
1112 } else if (S_ISDIR(victim->d_inode->i_mode))
1114 if (IS_DEADDIR(dir))
1116 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1121 /* Check whether we can create an object with dentry child in directory
1123 * 1. We can't do it if child already exists (open has special treatment for
1124 * this case, but since we are inlined it's OK)
1125 * 2. We can't do it if dir is read-only (done in permission())
1126 * 3. We should have write and exec permissions on dir
1127 * 4. We can't do it if dir is immutable (done in permission())
1129 static inline int may_create(struct inode *dir, struct dentry *child,
1130 struct nameidata *nd)
1134 if (IS_DEADDIR(dir))
1136 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1140 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1143 * O_DIRECTORY translates into forcing a directory lookup.
1145 static inline int lookup_flags(unsigned int f)
1147 unsigned long retval = LOOKUP_FOLLOW;
1150 retval &= ~LOOKUP_FOLLOW;
1152 if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1153 retval &= ~LOOKUP_FOLLOW;
1155 if (f & O_DIRECTORY)
1156 retval |= LOOKUP_DIRECTORY;
1162 * p1 and p2 should be directories on the same fs.
1164 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1168 if (p1->d_inode == p2->d_inode) {
1169 down(&p1->d_inode->i_sem);
1173 down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1175 for (p = p1; p->d_parent != p; p = p->d_parent) {
1176 if (p->d_parent == p2) {
1177 down(&p2->d_inode->i_sem);
1178 down(&p1->d_inode->i_sem);
1183 for (p = p2; p->d_parent != p; p = p->d_parent) {
1184 if (p->d_parent == p1) {
1185 down(&p1->d_inode->i_sem);
1186 down(&p2->d_inode->i_sem);
1191 down(&p1->d_inode->i_sem);
1192 down(&p2->d_inode->i_sem);
1196 void unlock_rename(struct dentry *p1, struct dentry *p2)
1198 up(&p1->d_inode->i_sem);
1199 if (p1->d_inode != p2->d_inode) {
1200 up(&p2->d_inode->i_sem);
1201 up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1205 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1206 struct nameidata *nd)
1208 int error = may_create(dir, dentry, nd);
1213 if (!dir->i_op || !dir->i_op->create)
1214 return -EACCES; /* shouldn't it be ENOSYS? */
1217 error = security_inode_create(dir, dentry, mode);
1221 error = dir->i_op->create(dir, dentry, mode, nd);
1223 inode_dir_notify(dir, DN_CREATE);
1224 security_inode_post_create(dir, dentry, mode);
1229 int may_open(struct nameidata *nd, int acc_mode, int flag)
1231 struct dentry *dentry = nd->dentry;
1232 struct inode *inode = dentry->d_inode;
1238 if (S_ISLNK(inode->i_mode))
1241 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1244 error = permission(inode, acc_mode, nd);
1249 * FIFO's, sockets and device files are special: they don't
1250 * actually live on the filesystem itself, and as such you
1251 * can write to them even if the filesystem is read-only.
1253 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1255 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1256 if (nd->mnt->mnt_flags & MNT_NODEV)
1260 } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
1263 * An append-only file must be opened in append mode for writing.
1265 if (IS_APPEND(inode)) {
1266 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1272 /* O_NOATIME can only be set by the owner or superuser */
1273 if (flag & O_NOATIME)
1274 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1278 * Ensure there are no outstanding leases on the file.
1280 error = break_lease(inode, flag);
1284 if (flag & O_TRUNC) {
1285 error = get_write_access(inode);
1290 * Refuse to truncate files with mandatory locks held on them.
1292 error = locks_verify_locked(inode);
1296 error = do_truncate(dentry, 0);
1298 put_write_access(inode);
1302 if (flag & FMODE_WRITE)
1311 * namei for open - this is in fact almost the whole open-routine.
1313 * Note that the low bits of "flag" aren't the same as in the open
1314 * system call - they are 00 - no permissions needed
1315 * 01 - read permission needed
1316 * 10 - write permission needed
1317 * 11 - read/write permissions needed
1318 * which is a lot more logical, and also allows the "no perm" needed
1319 * for symlinks (where the permissions are checked later).
1322 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1324 int acc_mode, error = 0;
1325 struct dentry *dentry;
1329 acc_mode = ACC_MODE(flag);
1331 /* Allow the LSM permission hook to distinguish append
1332 access from general write access. */
1333 if (flag & O_APPEND)
1334 acc_mode |= MAY_APPEND;
1336 /* Fill in the open() intent data */
1337 nd->intent.open.flags = flag;
1338 nd->intent.open.create_mode = mode;
1341 * The simplest case - just a plain lookup.
1343 if (!(flag & O_CREAT)) {
1344 error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd);
1351 * Create - we need to know the parent.
1353 error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd);
1358 * We have the parent and last component. First of all, check
1359 * that we are not asked to creat(2) an obvious directory - that
1363 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1367 nd->flags &= ~LOOKUP_PARENT;
1368 down(&dir->d_inode->i_sem);
1369 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1372 error = PTR_ERR(dentry);
1373 if (IS_ERR(dentry)) {
1374 up(&dir->d_inode->i_sem);
1378 /* Negative dentry, just create the file */
1379 if (!dentry->d_inode) {
1380 if (!IS_POSIXACL(dir->d_inode))
1381 mode &= ~current->fs->umask;
1382 error = vfs_create(dir->d_inode, dentry, mode, nd);
1383 up(&dir->d_inode->i_sem);
1385 nd->dentry = dentry;
1388 /* Don't check for write permission, don't truncate */
1395 * It already exists.
1397 up(&dir->d_inode->i_sem);
1403 if (d_mountpoint(dentry)) {
1405 if (flag & O_NOFOLLOW)
1407 while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry));
1410 if (!dentry->d_inode)
1412 if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
1416 nd->dentry = dentry;
1418 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
1421 error = may_open(nd, acc_mode, flag);
1434 if (flag & O_NOFOLLOW)
1437 * This is subtle. Instead of calling do_follow_link() we do the
1438 * thing by hands. The reason is that this way we have zero link_count
1439 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1440 * After that we have the parent and last component, i.e.
1441 * we are in the same situation as after the first path_walk().
1442 * Well, almost - if the last component is normal we get its copy
1443 * stored in nd->last.name and we will have to putname() it when we
1444 * are done. Procfs-like symlinks just set LAST_BIND.
1446 nd->flags |= LOOKUP_PARENT;
1447 error = security_inode_follow_link(dentry, nd);
1450 touch_atime(nd->mnt, dentry);
1451 nd_set_link(nd, NULL);
1452 error = dentry->d_inode->i_op->follow_link(dentry, nd);
1454 char *s = nd_get_link(nd);
1456 error = __vfs_follow_link(nd, s);
1457 if (dentry->d_inode->i_op->put_link)
1458 dentry->d_inode->i_op->put_link(dentry, nd);
1463 nd->flags &= ~LOOKUP_PARENT;
1464 if (nd->last_type == LAST_BIND) {
1465 dentry = nd->dentry;
1469 if (nd->last_type != LAST_NORM)
1471 if (nd->last.name[nd->last.len]) {
1472 putname(nd->last.name);
1477 putname(nd->last.name);
1481 down(&dir->d_inode->i_sem);
1482 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1483 putname(nd->last.name);
1488 * lookup_create - lookup a dentry, creating it if it doesn't exist
1489 * @nd: nameidata info
1490 * @is_dir: directory flag
1492 * Simple function to lookup and return a dentry and create it
1493 * if it doesn't exist. Is SMP-safe.
1495 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1497 struct dentry *dentry;
1499 down(&nd->dentry->d_inode->i_sem);
1500 dentry = ERR_PTR(-EEXIST);
1501 if (nd->last_type != LAST_NORM)
1503 nd->flags &= ~LOOKUP_PARENT;
1504 dentry = lookup_hash(&nd->last, nd->dentry);
1507 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1512 dentry = ERR_PTR(-ENOENT);
1517 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1519 int error = may_create(dir, dentry, NULL);
1524 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1527 if (!dir->i_op || !dir->i_op->mknod)
1530 error = security_inode_mknod(dir, dentry, mode, dev);
1535 error = dir->i_op->mknod(dir, dentry, mode, dev);
1537 inode_dir_notify(dir, DN_CREATE);
1538 security_inode_post_mknod(dir, dentry, mode, dev);
1543 asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
1547 struct dentry * dentry;
1548 struct nameidata nd;
1552 tmp = getname(filename);
1554 return PTR_ERR(tmp);
1556 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1559 dentry = lookup_create(&nd, 0);
1560 error = PTR_ERR(dentry);
1562 if (!IS_POSIXACL(nd.dentry->d_inode))
1563 mode &= ~current->fs->umask;
1564 if (!IS_ERR(dentry)) {
1565 switch (mode & S_IFMT) {
1566 case 0: case S_IFREG:
1567 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1569 case S_IFCHR: case S_IFBLK:
1570 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1571 new_decode_dev(dev));
1573 case S_IFIFO: case S_IFSOCK:
1574 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1584 up(&nd.dentry->d_inode->i_sem);
1592 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1594 int error = may_create(dir, dentry, NULL);
1599 if (!dir->i_op || !dir->i_op->mkdir)
1602 mode &= (S_IRWXUGO|S_ISVTX);
1603 error = security_inode_mkdir(dir, dentry, mode);
1608 error = dir->i_op->mkdir(dir, dentry, mode);
1610 inode_dir_notify(dir, DN_CREATE);
1611 security_inode_post_mkdir(dir,dentry, mode);
1616 asmlinkage long sys_mkdir(const char __user * pathname, int mode)
1621 tmp = getname(pathname);
1622 error = PTR_ERR(tmp);
1624 struct dentry *dentry;
1625 struct nameidata nd;
1627 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1630 dentry = lookup_create(&nd, 1);
1631 error = PTR_ERR(dentry);
1632 if (!IS_ERR(dentry)) {
1633 if (!IS_POSIXACL(nd.dentry->d_inode))
1634 mode &= ~current->fs->umask;
1635 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1638 up(&nd.dentry->d_inode->i_sem);
1648 * We try to drop the dentry early: we should have
1649 * a usage count of 2 if we're the only user of this
1650 * dentry, and if that is true (possibly after pruning
1651 * the dcache), then we drop the dentry now.
1653 * A low-level filesystem can, if it choses, legally
1656 * if (!d_unhashed(dentry))
1659 * if it cannot handle the case of removing a directory
1660 * that is still in use by something else..
1662 void dentry_unhash(struct dentry *dentry)
1665 spin_lock(&dcache_lock);
1666 switch (atomic_read(&dentry->d_count)) {
1668 spin_unlock(&dcache_lock);
1669 shrink_dcache_parent(dentry);
1670 spin_lock(&dcache_lock);
1671 if (atomic_read(&dentry->d_count) != 2)
1676 spin_unlock(&dcache_lock);
1679 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1681 int error = may_delete(dir, dentry, 1);
1686 if (!dir->i_op || !dir->i_op->rmdir)
1691 down(&dentry->d_inode->i_sem);
1692 dentry_unhash(dentry);
1693 if (d_mountpoint(dentry))
1696 error = security_inode_rmdir(dir, dentry);
1698 error = dir->i_op->rmdir(dir, dentry);
1700 dentry->d_inode->i_flags |= S_DEAD;
1703 up(&dentry->d_inode->i_sem);
1705 inode_dir_notify(dir, DN_DELETE);
1713 asmlinkage long sys_rmdir(const char __user * pathname)
1717 struct dentry *dentry;
1718 struct nameidata nd;
1720 name = getname(pathname);
1722 return PTR_ERR(name);
1724 error = path_lookup(name, LOOKUP_PARENT, &nd);
1728 switch(nd.last_type) {
1739 down(&nd.dentry->d_inode->i_sem);
1740 dentry = lookup_hash(&nd.last, nd.dentry);
1741 error = PTR_ERR(dentry);
1742 if (!IS_ERR(dentry)) {
1743 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1746 up(&nd.dentry->d_inode->i_sem);
1754 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1756 int error = may_delete(dir, dentry, 0);
1761 if (!dir->i_op || !dir->i_op->unlink)
1766 down(&dentry->d_inode->i_sem);
1767 if (d_mountpoint(dentry))
1770 error = security_inode_unlink(dir, dentry);
1772 error = dir->i_op->unlink(dir, dentry);
1774 up(&dentry->d_inode->i_sem);
1776 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1777 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1779 inode_dir_notify(dir, DN_DELETE);
1785 * Make sure that the actual truncation of the file will occur outside its
1786 * directory's i_sem. Truncate can take a long time if there is a lot of
1787 * writeout happening, and we don't want to prevent access to the directory
1788 * while waiting on the I/O.
1790 asmlinkage long sys_unlink(const char __user * pathname)
1794 struct dentry *dentry;
1795 struct nameidata nd;
1796 struct inode *inode = NULL;
1798 name = getname(pathname);
1800 return PTR_ERR(name);
1802 error = path_lookup(name, LOOKUP_PARENT, &nd);
1806 if (nd.last_type != LAST_NORM)
1808 down(&nd.dentry->d_inode->i_sem);
1809 dentry = lookup_hash(&nd.last, nd.dentry);
1810 error = PTR_ERR(dentry);
1811 if (!IS_ERR(dentry)) {
1812 /* Why not before? Because we want correct error value */
1813 if (nd.last.name[nd.last.len])
1815 inode = dentry->d_inode;
1817 atomic_inc(&inode->i_count);
1818 error = vfs_unlink(nd.dentry->d_inode, dentry);
1822 up(&nd.dentry->d_inode->i_sem);
1829 iput(inode); /* truncate the inode here */
1833 error = !dentry->d_inode ? -ENOENT :
1834 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1838 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
1840 int error = may_create(dir, dentry, NULL);
1845 if (!dir->i_op || !dir->i_op->symlink)
1848 error = security_inode_symlink(dir, dentry, oldname);
1853 error = dir->i_op->symlink(dir, dentry, oldname);
1855 inode_dir_notify(dir, DN_CREATE);
1856 security_inode_post_symlink(dir, dentry, oldname);
1861 asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
1867 from = getname(oldname);
1869 return PTR_ERR(from);
1870 to = getname(newname);
1871 error = PTR_ERR(to);
1873 struct dentry *dentry;
1874 struct nameidata nd;
1876 error = path_lookup(to, LOOKUP_PARENT, &nd);
1879 dentry = lookup_create(&nd, 0);
1880 error = PTR_ERR(dentry);
1881 if (!IS_ERR(dentry)) {
1882 error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
1885 up(&nd.dentry->d_inode->i_sem);
1894 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1896 struct inode *inode = old_dentry->d_inode;
1902 error = may_create(dir, new_dentry, NULL);
1906 if (dir->i_sb != inode->i_sb)
1910 * A link to an append-only or immutable file cannot be created.
1912 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1914 if (!dir->i_op || !dir->i_op->link)
1916 if (S_ISDIR(old_dentry->d_inode->i_mode))
1919 error = security_inode_link(old_dentry, dir, new_dentry);
1923 down(&old_dentry->d_inode->i_sem);
1925 error = dir->i_op->link(old_dentry, dir, new_dentry);
1926 up(&old_dentry->d_inode->i_sem);
1928 inode_dir_notify(dir, DN_CREATE);
1929 security_inode_post_link(old_dentry, dir, new_dentry);
1935 * Hardlinks are often used in delicate situations. We avoid
1936 * security-related surprises by not following symlinks on the
1939 * We don't follow them on the oldname either to be compatible
1940 * with linux 2.0, and to avoid hard-linking to directories
1941 * and other special files. --ADM
1943 asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
1945 struct dentry *new_dentry;
1946 struct nameidata nd, old_nd;
1950 to = getname(newname);
1954 error = __user_walk(oldname, 0, &old_nd);
1957 error = path_lookup(to, LOOKUP_PARENT, &nd);
1961 if (old_nd.mnt != nd.mnt)
1963 new_dentry = lookup_create(&nd, 0);
1964 error = PTR_ERR(new_dentry);
1965 if (!IS_ERR(new_dentry)) {
1966 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
1969 up(&nd.dentry->d_inode->i_sem);
1973 path_release(&old_nd);
1981 * The worst of all namespace operations - renaming directory. "Perverted"
1982 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
1984 * a) we can get into loop creation. Check is done in is_subdir().
1985 * b) race potential - two innocent renames can create a loop together.
1986 * That's where 4.4 screws up. Current fix: serialization on
1987 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
1989 * c) we have to lock _three_ objects - parents and victim (if it exists).
1990 * And that - after we got ->i_sem on parents (until then we don't know
1991 * whether the target exists). Solution: try to be smart with locking
1992 * order for inodes. We rely on the fact that tree topology may change
1993 * only under ->s_vfs_rename_sem _and_ that parent of the object we
1994 * move will be locked. Thus we can rank directories by the tree
1995 * (ancestors first) and rank all non-directories after them.
1996 * That works since everybody except rename does "lock parent, lookup,
1997 * lock child" and rename is under ->s_vfs_rename_sem.
1998 * HOWEVER, it relies on the assumption that any object with ->lookup()
1999 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2000 * we'd better make sure that there's no link(2) for them.
2001 * d) some filesystems don't support opened-but-unlinked directories,
2002 * either because of layout or because they are not ready to deal with
2003 * all cases correctly. The latter will be fixed (taking this sort of
2004 * stuff into VFS), but the former is not going away. Solution: the same
2005 * trick as in rmdir().
2006 * e) conversion from fhandle to dentry may come in the wrong moment - when
2007 * we are removing the target. Solution: we will have to grab ->i_sem
2008 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2009 * ->i_sem on parents, which works but leads to some truely excessive
2012 int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2013 struct inode *new_dir, struct dentry *new_dentry)
2016 struct inode *target;
2019 * If we are going to change the parent - check write permissions,
2020 * we'll need to flip '..'.
2022 if (new_dir != old_dir) {
2023 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2028 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2032 target = new_dentry->d_inode;
2034 down(&target->i_sem);
2035 dentry_unhash(new_dentry);
2037 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2040 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2043 target->i_flags |= S_DEAD;
2045 if (d_unhashed(new_dentry))
2046 d_rehash(new_dentry);
2050 d_move(old_dentry,new_dentry);
2051 security_inode_post_rename(old_dir, old_dentry,
2052 new_dir, new_dentry);
2057 int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2058 struct inode *new_dir, struct dentry *new_dentry)
2060 struct inode *target;
2063 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2068 target = new_dentry->d_inode;
2070 down(&target->i_sem);
2071 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2074 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2076 /* The following d_move() should become unconditional */
2077 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
2078 d_move(old_dentry, new_dentry);
2079 security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
2087 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2088 struct inode *new_dir, struct dentry *new_dentry)
2091 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2093 if (old_dentry->d_inode == new_dentry->d_inode)
2096 error = may_delete(old_dir, old_dentry, is_dir);
2100 if (!new_dentry->d_inode)
2101 error = may_create(new_dir, new_dentry, NULL);
2103 error = may_delete(new_dir, new_dentry, is_dir);
2107 if (!old_dir->i_op || !old_dir->i_op->rename)
2110 DQUOT_INIT(old_dir);
2111 DQUOT_INIT(new_dir);
2114 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2116 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2118 if (old_dir == new_dir)
2119 inode_dir_notify(old_dir, DN_RENAME);
2121 inode_dir_notify(old_dir, DN_DELETE);
2122 inode_dir_notify(new_dir, DN_CREATE);
2128 static inline int do_rename(const char * oldname, const char * newname)
2131 struct dentry * old_dir, * new_dir;
2132 struct dentry * old_dentry, *new_dentry;
2133 struct dentry * trap;
2134 struct nameidata oldnd, newnd;
2136 error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2140 error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2145 if (oldnd.mnt != newnd.mnt)
2148 old_dir = oldnd.dentry;
2150 if (oldnd.last_type != LAST_NORM)
2153 new_dir = newnd.dentry;
2154 if (newnd.last_type != LAST_NORM)
2157 trap = lock_rename(new_dir, old_dir);
2159 old_dentry = lookup_hash(&oldnd.last, old_dir);
2160 error = PTR_ERR(old_dentry);
2161 if (IS_ERR(old_dentry))
2163 /* source must exist */
2165 if (!old_dentry->d_inode)
2167 /* unless the source is a directory trailing slashes give -ENOTDIR */
2168 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2170 if (oldnd.last.name[oldnd.last.len])
2172 if (newnd.last.name[newnd.last.len])
2175 /* source should not be ancestor of target */
2177 if (old_dentry == trap)
2179 new_dentry = lookup_hash(&newnd.last, new_dir);
2180 error = PTR_ERR(new_dentry);
2181 if (IS_ERR(new_dentry))
2183 /* target should not be an ancestor of source */
2185 if (new_dentry == trap)
2188 error = vfs_rename(old_dir->d_inode, old_dentry,
2189 new_dir->d_inode, new_dentry);
2195 unlock_rename(new_dir, old_dir);
2197 path_release(&newnd);
2199 path_release(&oldnd);
2204 asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
2210 from = getname(oldname);
2212 return PTR_ERR(from);
2213 to = getname(newname);
2214 error = PTR_ERR(to);
2216 error = do_rename(from,to);
2223 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2227 len = PTR_ERR(link);
2232 if (len > (unsigned) buflen)
2234 if (copy_to_user(buffer, link, len))
2241 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2242 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2243 * using) it for any given inode is up to filesystem.
2245 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2247 struct nameidata nd;
2250 res = dentry->d_inode->i_op->follow_link(dentry, &nd);
2252 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2253 if (dentry->d_inode->i_op->put_link)
2254 dentry->d_inode->i_op->put_link(dentry, &nd);
2259 int vfs_follow_link(struct nameidata *nd, const char *link)
2261 return __vfs_follow_link(nd, link);
2264 /* get the link contents into pagecache */
2265 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2268 struct address_space *mapping = dentry->d_inode->i_mapping;
2269 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2273 wait_on_page_locked(page);
2274 if (!PageUptodate(page))
2280 page_cache_release(page);
2281 return ERR_PTR(-EIO);
2287 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2289 struct page *page = NULL;
2290 char *s = page_getlink(dentry, &page);
2291 int res = vfs_readlink(dentry,buffer,buflen,s);
2294 page_cache_release(page);
2299 int page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2302 char *s = page_getlink(dentry, &page);
2310 void page_put_link(struct dentry *dentry, struct nameidata *nd)
2312 if (!IS_ERR(nd_get_link(nd))) {
2314 page = find_get_page(dentry->d_inode->i_mapping, 0);
2318 page_cache_release(page);
2319 page_cache_release(page);
2323 int page_follow_link(struct dentry *dentry, struct nameidata *nd)
2325 struct page *page = NULL;
2326 char *s = page_getlink(dentry, &page);
2327 int res = __vfs_follow_link(nd, s);
2330 page_cache_release(page);
2335 int page_symlink(struct inode *inode, const char *symname, int len)
2337 struct address_space *mapping = inode->i_mapping;
2338 struct page *page = grab_cache_page(mapping, 0);
2344 err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2347 kaddr = kmap_atomic(page, KM_USER0);
2348 memcpy(kaddr, symname, len-1);
2349 kunmap_atomic(kaddr, KM_USER0);
2350 mapping->a_ops->commit_write(NULL, page, 0, len-1);
2352 * Notice that we are _not_ going to block here - end of page is
2353 * unmapped, so this will only try to map the rest of page, see
2354 * that it is unmapped (typically even will not look into inode -
2355 * ->i_size will be enough for everything) and zero it out.
2356 * OTOH it's obviously correct and should make the page up-to-date.
2358 if (!PageUptodate(page)) {
2359 err = mapping->a_ops->readpage(NULL, page);
2360 wait_on_page_locked(page);
2364 page_cache_release(page);
2367 mark_inode_dirty(inode);
2371 page_cache_release(page);
2376 struct inode_operations page_symlink_inode_operations = {
2377 .readlink = generic_readlink,
2378 .follow_link = page_follow_link_light,
2379 .put_link = page_put_link,
2382 EXPORT_SYMBOL(__user_walk);
2383 EXPORT_SYMBOL(follow_down);
2384 EXPORT_SYMBOL(follow_up);
2385 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2386 EXPORT_SYMBOL(getname);
2387 EXPORT_SYMBOL(lock_rename);
2388 EXPORT_SYMBOL(lookup_create);
2389 EXPORT_SYMBOL(lookup_hash);
2390 EXPORT_SYMBOL(lookup_one_len);
2391 EXPORT_SYMBOL(page_follow_link);
2392 EXPORT_SYMBOL(page_follow_link_light);
2393 EXPORT_SYMBOL(page_put_link);
2394 EXPORT_SYMBOL(page_readlink);
2395 EXPORT_SYMBOL(page_symlink);
2396 EXPORT_SYMBOL(page_symlink_inode_operations);
2397 EXPORT_SYMBOL(path_lookup);
2398 EXPORT_SYMBOL(path_release);
2399 EXPORT_SYMBOL(path_walk);
2400 EXPORT_SYMBOL(permission);
2401 EXPORT_SYMBOL(unlock_rename);
2402 EXPORT_SYMBOL(vfs_create);
2403 EXPORT_SYMBOL(vfs_follow_link);
2404 EXPORT_SYMBOL(vfs_link);
2405 EXPORT_SYMBOL(vfs_mkdir);
2406 EXPORT_SYMBOL(vfs_mknod);
2407 EXPORT_SYMBOL(vfs_permission);
2408 EXPORT_SYMBOL(vfs_readlink);
2409 EXPORT_SYMBOL(vfs_rename);
2410 EXPORT_SYMBOL(vfs_rmdir);
2411 EXPORT_SYMBOL(vfs_symlink);
2412 EXPORT_SYMBOL(vfs_unlink);
2413 EXPORT_SYMBOL(dentry_unhash);
2414 EXPORT_SYMBOL(generic_readlink);