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/slab.h>
20 #include <linux/namei.h>
21 #include <linux/quotaops.h>
22 #include <linux/pagemap.h>
23 #include <linux/dnotify.h>
24 #include <linux/smp_lock.h>
25 #include <linux/personality.h>
26 #include <linux/security.h>
27 #include <linux/mount.h>
28 #include <asm/namei.h>
29 #include <asm/uaccess.h>
31 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
33 /* [Feb-1997 T. Schoebel-Theuer]
34 * Fundamental changes in the pathname lookup mechanisms (namei)
35 * were necessary because of omirr. The reason is that omirr needs
36 * to know the _real_ pathname, not the user-supplied one, in case
37 * of symlinks (and also when transname replacements occur).
39 * The new code replaces the old recursive symlink resolution with
40 * an iterative one (in case of non-nested symlink chains). It does
41 * this with calls to <fs>_follow_link().
42 * As a side effect, dir_namei(), _namei() and follow_link() are now
43 * replaced with a single function lookup_dentry() that can handle all
44 * the special cases of the former code.
46 * With the new dcache, the pathname is stored at each inode, at least as
47 * long as the refcount of the inode is positive. As a side effect, the
48 * size of the dcache depends on the inode cache and thus is dynamic.
50 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
51 * resolution to correspond with current state of the code.
53 * Note that the symlink resolution is not *completely* iterative.
54 * There is still a significant amount of tail- and mid- recursion in
55 * the algorithm. Also, note that <fs>_readlink() is not used in
56 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
57 * may return different results than <fs>_follow_link(). Many virtual
58 * filesystems (including /proc) exhibit this behavior.
61 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
62 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
63 * and the name already exists in form of a symlink, try to create the new
64 * name indicated by the symlink. The old code always complained that the
65 * name already exists, due to not following the symlink even if its target
66 * is nonexistent. The new semantics affects also mknod() and link() when
67 * the name is a symlink pointing to a non-existant name.
69 * I don't know which semantics is the right one, since I have no access
70 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
71 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
72 * "old" one. Personally, I think the new semantics is much more logical.
73 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
74 * file does succeed in both HP-UX and SunOs, but not in Solaris
75 * and in the old Linux semantics.
78 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
79 * semantics. See the comments in "open_namei" and "do_link" below.
81 * [10-Sep-98 Alan Modra] Another symlink change.
84 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
85 * inside the path - always follow.
86 * in the last component in creation/removal/renaming - never follow.
87 * if LOOKUP_FOLLOW passed - follow.
88 * if the pathname has trailing slashes - follow.
89 * otherwise - don't follow.
90 * (applied in that order).
92 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
93 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
94 * During the 2.4 we need to fix the userland stuff depending on it -
95 * hopefully we will be able to get rid of that wart in 2.5. So far only
96 * XEmacs seems to be relying on it...
99 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
100 * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives
101 * any extra contention...
104 /* In order to reduce some races, while at the same time doing additional
105 * checking and hopefully speeding things up, we copy filenames to the
106 * kernel data space before using them..
108 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
109 * PATH_MAX includes the nul terminator --RR.
111 static inline int do_getname(const char __user *filename, char *page)
114 unsigned long len = PATH_MAX;
116 if ((unsigned long) filename >= TASK_SIZE) {
117 if (!segment_eq(get_fs(), KERNEL_DS))
119 } else if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
120 len = TASK_SIZE - (unsigned long) filename;
122 retval = strncpy_from_user((char *)page, filename, len);
126 return -ENAMETOOLONG;
132 char * getname(const char __user * filename)
136 result = ERR_PTR(-ENOMEM);
139 int retval = do_getname(filename, tmp);
144 result = ERR_PTR(retval);
153 * is used to check for read/write/execute permissions on a file.
154 * We use "fsuid" for this, letting us set arbitrary permissions
155 * for filesystem access without changing the "normal" uids which
156 * are used for other things..
158 int vfs_permission(struct inode * inode, int mask)
160 umode_t mode = inode->i_mode;
162 if (mask & MAY_WRITE) {
164 * Nobody gets write access to a read-only fs.
166 if (IS_RDONLY(inode) &&
167 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
171 * Nobody gets write access to an immutable file.
173 if (IS_IMMUTABLE(inode))
177 if (current->fsuid == inode->i_uid)
179 else if (in_group_p(inode->i_gid))
183 * If the DACs are ok we don't need any capability check.
185 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
189 * Read/write DACs are always overridable.
190 * Executable DACs are overridable if at least one exec bit is set.
192 if ((mask & (MAY_READ|MAY_WRITE)) || (inode->i_mode & S_IXUGO))
193 if (capable(CAP_DAC_OVERRIDE))
197 * Searching includes executable on directories, else just read.
199 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
200 if (capable(CAP_DAC_READ_SEARCH))
206 int permission(struct inode * inode,int mask, struct nameidata *nd)
211 /* Ordinary permission routines do not understand MAY_APPEND. */
212 submask = mask & ~MAY_APPEND;
214 if (inode->i_op && inode->i_op->permission)
215 retval = inode->i_op->permission(inode, submask, nd);
217 retval = vfs_permission(inode, submask);
221 return security_inode_permission(inode, mask);
225 * get_write_access() gets write permission for a file.
226 * put_write_access() releases this write permission.
227 * This is used for regular files.
228 * We cannot support write (and maybe mmap read-write shared) accesses and
229 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
230 * can have the following values:
231 * 0: no writers, no VM_DENYWRITE mappings
232 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
233 * > 0: (i_writecount) users are writing to the file.
235 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
236 * except for the cases where we don't hold i_writecount yet. Then we need to
237 * use {get,deny}_write_access() - these functions check the sign and refuse
238 * to do the change if sign is wrong. Exclusion between them is provided by
239 * spinlock (arbitration_lock) and I'll rip the second arsehole to the first
240 * who will try to move it in struct inode - just leave it here.
242 static spinlock_t arbitration_lock = SPIN_LOCK_UNLOCKED;
243 int get_write_access(struct inode * inode)
245 spin_lock(&arbitration_lock);
246 if (atomic_read(&inode->i_writecount) < 0) {
247 spin_unlock(&arbitration_lock);
250 atomic_inc(&inode->i_writecount);
251 spin_unlock(&arbitration_lock);
254 int deny_write_access(struct file * file)
256 spin_lock(&arbitration_lock);
257 if (atomic_read(&file->f_dentry->d_inode->i_writecount) > 0) {
258 spin_unlock(&arbitration_lock);
261 atomic_dec(&file->f_dentry->d_inode->i_writecount);
262 spin_unlock(&arbitration_lock);
266 void path_release(struct nameidata *nd)
273 * Internal lookup() using the new generic dcache.
276 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
278 struct dentry * dentry = __d_lookup(parent, name);
280 /* lockess __d_lookup may fail due to concurrent d_move()
281 * in some unrelated directory, so try with d_lookup
284 dentry = d_lookup(parent, name);
286 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
287 if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
296 * Short-cut version of permission(), for calling by
297 * path_walk(), when dcache lock is held. Combines parts
298 * of permission() and vfs_permission(), and tests ONLY for
299 * MAY_EXEC permission.
301 * If appropriate, check DAC only. If not appropriate, or
302 * short-cut DAC fails, then call permission() to do more
303 * complete permission check.
305 static inline int exec_permission_lite(struct inode *inode)
307 umode_t mode = inode->i_mode;
309 if ((inode->i_op && inode->i_op->permission))
312 if (current->fsuid == inode->i_uid)
314 else if (in_group_p(inode->i_gid))
320 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
323 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
328 return security_inode_permission(inode, MAY_EXEC);
332 * This is called when everything else fails, and we actually have
333 * to go to the low-level filesystem to find out what we should do..
335 * We get the directory semaphore, and after getting that we also
336 * make sure that nobody added the entry to the dcache in the meantime..
339 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
341 struct dentry * result;
342 struct inode *dir = parent->d_inode;
346 * First re-do the cached lookup just in case it was created
347 * while we waited for the directory semaphore..
349 * FIXME! This could use version numbering or similar to
350 * avoid unnecessary cache lookups.
352 * The "dcache_lock" is purely to protect the RCU list walker
353 * from concurrent renames at this point (we mustn't get false
354 * negatives from the RCU list walk here, unlike the optimistic
357 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
359 result = d_lookup(parent, name);
361 struct dentry * dentry = d_alloc(parent, name);
362 result = ERR_PTR(-ENOMEM);
364 result = dir->i_op->lookup(dir, dentry, nd);
375 * Uhhuh! Nasty case: the cache was re-populated while
376 * we waited on the semaphore. Need to revalidate.
379 if (result->d_op && result->d_op->d_revalidate) {
380 if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
382 result = ERR_PTR(-ENOENT);
389 * This limits recursive symlink follows to 8, while
390 * limiting consecutive symlinks to 40.
392 * Without that kind of total limit, nasty chains of consecutive
393 * symlinks can cause almost arbitrarily long lookups.
395 static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd)
398 if (current->link_count >= 5)
400 if (current->total_link_count >= 40)
403 err = security_inode_follow_link(dentry, nd);
406 current->link_count++;
407 current->total_link_count++;
408 update_atime(dentry->d_inode);
409 err = dentry->d_inode->i_op->follow_link(dentry, nd);
410 current->link_count--;
417 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
419 struct vfsmount *parent;
420 struct dentry *mountpoint;
421 spin_lock(&dcache_lock);
422 parent=(*mnt)->mnt_parent;
423 if (parent == *mnt) {
424 spin_unlock(&dcache_lock);
428 mountpoint=dget((*mnt)->mnt_mountpoint);
429 spin_unlock(&dcache_lock);
431 *dentry = mountpoint;
437 /* no need for dcache_lock, as serialization is taken care in
440 static int follow_mount(struct vfsmount **mnt, struct dentry **dentry)
443 while (d_mountpoint(*dentry)) {
444 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
449 mntput(mounted->mnt_parent);
450 *dentry = dget(mounted->mnt_root);
456 /* no need for dcache_lock, as serialization is taken care in
459 static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
461 struct vfsmount *mounted;
463 mounted = lookup_mnt(*mnt, *dentry);
467 mntput(mounted->mnt_parent);
468 *dentry = dget(mounted->mnt_root);
474 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
476 return __follow_down(mnt,dentry);
479 static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry)
482 struct vfsmount *parent;
483 struct dentry *old = *dentry;
485 read_lock(¤t->fs->lock);
486 if (*dentry == current->fs->root &&
487 *mnt == current->fs->rootmnt) {
488 read_unlock(¤t->fs->lock);
491 read_unlock(¤t->fs->lock);
492 spin_lock(&dcache_lock);
493 if (*dentry != (*mnt)->mnt_root) {
494 *dentry = dget((*dentry)->d_parent);
495 spin_unlock(&dcache_lock);
499 parent = (*mnt)->mnt_parent;
500 if (parent == *mnt) {
501 spin_unlock(&dcache_lock);
505 *dentry = dget((*mnt)->mnt_mountpoint);
506 spin_unlock(&dcache_lock);
511 follow_mount(mnt, dentry);
515 struct vfsmount *mnt;
516 struct dentry *dentry;
520 * It's more convoluted than I'd like it to be, but... it's still fairly
521 * small and for now I'd prefer to have fast path as straight as possible.
522 * It _is_ time-critical.
524 static int do_lookup(struct nameidata *nd, struct qstr *name,
527 struct vfsmount *mnt = nd->mnt;
528 struct dentry *dentry = __d_lookup(nd->dentry, name);
532 if (dentry->d_op && dentry->d_op->d_revalidate)
533 goto need_revalidate;
536 path->dentry = dentry;
540 dentry = real_lookup(nd->dentry, name, nd);
546 if (dentry->d_op->d_revalidate(dentry, nd))
548 if (d_invalidate(dentry))
554 return PTR_ERR(dentry);
560 * This is the basic name resolution function, turning a pathname
561 * into the final dentry.
563 * We expect 'base' to be positive and a directory.
565 int link_path_walk(const char * name, struct nameidata *nd)
570 unsigned int lookup_flags = nd->flags;
577 inode = nd->dentry->d_inode;
578 if (current->link_count)
579 lookup_flags = LOOKUP_FOLLOW;
581 /* At this point we know we have a real path component. */
587 err = exec_permission_lite(inode);
588 if (err == -EAGAIN) {
589 err = permission(inode, MAY_EXEC, nd);
595 c = *(const unsigned char *)name;
597 hash = init_name_hash();
600 hash = partial_name_hash(c, hash);
601 c = *(const unsigned char *)name;
602 } while (c && (c != '/'));
603 this.len = name - (const char *) this.name;
604 this.hash = end_name_hash(hash);
606 /* remove trailing slashes? */
609 while (*++name == '/');
611 goto last_with_slashes;
614 * "." and ".." are special - ".." especially so because it has
615 * to be able to know about the current root directory and
616 * parent relationships.
618 if (this.name[0] == '.') switch (this.len) {
622 if (this.name[1] != '.')
624 follow_dotdot(&nd->mnt, &nd->dentry);
625 inode = nd->dentry->d_inode;
631 * See if the low-level filesystem might want
632 * to use its own hash..
634 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
635 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
639 nd->flags |= LOOKUP_CONTINUE;
640 /* This does the actual lookups.. */
641 err = do_lookup(nd, &this, &next);
644 /* Check mountpoints.. */
645 follow_mount(&next.mnt, &next.dentry);
648 inode = next.dentry->d_inode;
655 if (inode->i_op->follow_link) {
657 err = do_follow_link(next.dentry, nd);
663 inode = nd->dentry->d_inode;
672 nd->dentry = next.dentry;
675 if (!inode->i_op->lookup)
678 /* here ends the main loop */
681 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
683 nd->flags &= ~LOOKUP_CONTINUE;
684 if (lookup_flags & LOOKUP_PARENT)
686 if (this.name[0] == '.') switch (this.len) {
690 if (this.name[1] != '.')
692 follow_dotdot(&nd->mnt, &nd->dentry);
693 inode = nd->dentry->d_inode;
698 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
699 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
703 err = do_lookup(nd, &this, &next);
706 follow_mount(&next.mnt, &next.dentry);
707 inode = next.dentry->d_inode;
708 if ((lookup_flags & LOOKUP_FOLLOW)
709 && inode && inode->i_op && inode->i_op->follow_link) {
711 err = do_follow_link(next.dentry, nd);
716 inode = nd->dentry->d_inode;
720 nd->dentry = next.dentry;
725 if (lookup_flags & LOOKUP_DIRECTORY) {
727 if (!inode->i_op || !inode->i_op->lookup)
733 nd->last_type = LAST_NORM;
734 if (this.name[0] != '.')
737 nd->last_type = LAST_DOT;
738 else if (this.len == 2 && this.name[1] == '.')
739 nd->last_type = LAST_DOTDOT;
751 int path_walk(const char * name, struct nameidata *nd)
753 current->total_link_count = 0;
754 return link_path_walk(name, nd);
758 /* returns 1 if everything is done */
759 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
761 if (path_walk(name, nd))
762 return 0; /* something went wrong... */
764 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
765 struct nameidata nd_root;
767 * NAME was not found in alternate root or it's a directory. Try to find
768 * it in the normal root:
770 nd_root.last_type = LAST_ROOT;
771 nd_root.flags = nd->flags;
772 memcpy(&nd_root.intent, &nd->intent, sizeof(nd_root.intent));
773 read_lock(¤t->fs->lock);
774 nd_root.mnt = mntget(current->fs->rootmnt);
775 nd_root.dentry = dget(current->fs->root);
776 read_unlock(¤t->fs->lock);
777 if (path_walk(name, &nd_root))
779 if (nd_root.dentry->d_inode) {
781 nd->dentry = nd_root.dentry;
782 nd->mnt = nd_root.mnt;
783 nd->last = nd_root.last;
786 path_release(&nd_root);
791 void set_fs_altroot(void)
793 char *emul = __emul_prefix();
795 struct vfsmount *mnt = NULL, *oldmnt;
796 struct dentry *dentry = NULL, *olddentry;
801 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
807 write_lock(¤t->fs->lock);
808 oldmnt = current->fs->altrootmnt;
809 olddentry = current->fs->altroot;
810 current->fs->altrootmnt = mnt;
811 current->fs->altroot = dentry;
812 write_unlock(¤t->fs->lock);
821 walk_init_root(const char *name, struct nameidata *nd)
823 read_lock(¤t->fs->lock);
824 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
825 nd->mnt = mntget(current->fs->altrootmnt);
826 nd->dentry = dget(current->fs->altroot);
827 read_unlock(¤t->fs->lock);
828 if (__emul_lookup_dentry(name,nd))
830 read_lock(¤t->fs->lock);
832 nd->mnt = mntget(current->fs->rootmnt);
833 nd->dentry = dget(current->fs->root);
834 read_unlock(¤t->fs->lock);
838 int path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
840 nd->last_type = LAST_ROOT; /* if there are only slashes... */
843 read_lock(¤t->fs->lock);
845 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
846 nd->mnt = mntget(current->fs->altrootmnt);
847 nd->dentry = dget(current->fs->altroot);
848 read_unlock(¤t->fs->lock);
849 if (__emul_lookup_dentry(name,nd))
851 read_lock(¤t->fs->lock);
853 nd->mnt = mntget(current->fs->rootmnt);
854 nd->dentry = dget(current->fs->root);
857 nd->mnt = mntget(current->fs->pwdmnt);
858 nd->dentry = dget(current->fs->pwd);
860 read_unlock(¤t->fs->lock);
861 current->total_link_count = 0;
862 return link_path_walk(name, nd);
866 * Restricted form of lookup. Doesn't follow links, single-component only,
867 * needs parent already locked. Doesn't follow mounts.
870 static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
872 struct dentry * dentry;
876 inode = base->d_inode;
877 err = permission(inode, MAY_EXEC, nd);
878 dentry = ERR_PTR(err);
883 * See if the low-level filesystem might want
884 * to use its own hash..
886 if (base->d_op && base->d_op->d_hash) {
887 err = base->d_op->d_hash(base, name);
888 dentry = ERR_PTR(err);
893 dentry = cached_lookup(base, name, nd);
895 struct dentry *new = d_alloc(base, name);
896 dentry = ERR_PTR(-ENOMEM);
899 dentry = inode->i_op->lookup(inode, new, nd);
909 struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
911 return __lookup_hash(name, base, NULL);
915 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
926 hash = init_name_hash();
928 c = *(const unsigned char *)name++;
929 if (c == '/' || c == '\0')
931 hash = partial_name_hash(c, hash);
933 this.hash = end_name_hash(hash);
935 return lookup_hash(&this, base);
937 return ERR_PTR(-EACCES);
943 * is used by most simple commands to get the inode of a specified name.
944 * Open, link etc use their own routines, but this is enough for things
947 * namei exists in two versions: namei/lnamei. The only difference is
948 * that namei follows links, while lnamei does not.
951 int __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
953 char *tmp = getname(name);
954 int err = PTR_ERR(tmp);
957 err = path_lookup(tmp, flags, nd);
964 * It's inline, so penalty for filesystems that don't use sticky bit is
967 static inline int check_sticky(struct inode *dir, struct inode *inode)
969 if (!(dir->i_mode & S_ISVTX))
971 if (inode->i_uid == current->fsuid)
973 if (dir->i_uid == current->fsuid)
975 return !capable(CAP_FOWNER);
979 * Check whether we can remove a link victim from directory dir, check
980 * whether the type of victim is right.
981 * 1. We can't do it if dir is read-only (done in permission())
982 * 2. We should have write and exec permissions on dir
983 * 3. We can't remove anything from append-only dir
984 * 4. We can't do anything with immutable dir (done in permission())
985 * 5. If the sticky bit on dir is set we should either
986 * a. be owner of dir, or
987 * b. be owner of victim, or
988 * c. have CAP_FOWNER capability
989 * 6. If the victim is append-only or immutable we can't do antyhing with
990 * links pointing to it.
991 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
992 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
993 * 9. We can't remove a root or mountpoint.
994 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
995 * nfs_async_unlink().
997 static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1000 if (!victim->d_inode || victim->d_parent->d_inode != dir)
1002 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1007 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1008 IS_IMMUTABLE(victim->d_inode))
1011 if (!S_ISDIR(victim->d_inode->i_mode))
1013 if (IS_ROOT(victim))
1015 } else if (S_ISDIR(victim->d_inode->i_mode))
1017 if (IS_DEADDIR(dir))
1019 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1024 /* Check whether we can create an object with dentry child in directory
1026 * 1. We can't do it if child already exists (open has special treatment for
1027 * this case, but since we are inlined it's OK)
1028 * 2. We can't do it if dir is read-only (done in permission())
1029 * 3. We should have write and exec permissions on dir
1030 * 4. We can't do it if dir is immutable (done in permission())
1032 static inline int may_create(struct inode *dir, struct dentry *child,
1033 struct nameidata *nd)
1037 if (IS_DEADDIR(dir))
1039 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1043 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1046 * O_DIRECTORY translates into forcing a directory lookup.
1048 static inline int lookup_flags(unsigned int f)
1050 unsigned long retval = LOOKUP_FOLLOW;
1053 retval &= ~LOOKUP_FOLLOW;
1055 if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1056 retval &= ~LOOKUP_FOLLOW;
1058 if (f & O_DIRECTORY)
1059 retval |= LOOKUP_DIRECTORY;
1065 * p1 and p2 should be directories on the same fs.
1067 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1072 down(&p1->d_inode->i_sem);
1076 down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1078 for (p = p1; p->d_parent != p; p = p->d_parent) {
1079 if (p->d_parent == p2) {
1080 down(&p2->d_inode->i_sem);
1081 down(&p1->d_inode->i_sem);
1086 for (p = p2; p->d_parent != p; p = p->d_parent) {
1087 if (p->d_parent == p1) {
1088 down(&p1->d_inode->i_sem);
1089 down(&p2->d_inode->i_sem);
1094 down(&p1->d_inode->i_sem);
1095 down(&p2->d_inode->i_sem);
1099 void unlock_rename(struct dentry *p1, struct dentry *p2)
1101 up(&p1->d_inode->i_sem);
1103 up(&p2->d_inode->i_sem);
1104 up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1108 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1109 struct nameidata *nd)
1111 int error = may_create(dir, dentry, nd);
1116 if (!dir->i_op || !dir->i_op->create)
1117 return -EACCES; /* shouldn't it be ENOSYS? */
1120 error = security_inode_create(dir, dentry, mode);
1124 error = dir->i_op->create(dir, dentry, mode, nd);
1126 inode_dir_notify(dir, DN_CREATE);
1127 security_inode_post_create(dir, dentry, mode);
1132 int may_open(struct nameidata *nd, int acc_mode, int flag)
1134 struct dentry *dentry = nd->dentry;
1135 struct inode *inode = dentry->d_inode;
1141 if (S_ISLNK(inode->i_mode))
1144 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1147 error = permission(inode, acc_mode, nd);
1152 * FIFO's, sockets and device files are special: they don't
1153 * actually live on the filesystem itself, and as such you
1154 * can write to them even if the filesystem is read-only.
1156 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1158 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1159 if (nd->mnt->mnt_flags & MNT_NODEV)
1163 } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
1166 * An append-only file must be opened in append mode for writing.
1168 if (IS_APPEND(inode)) {
1169 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1176 * Ensure there are no outstanding leases on the file.
1178 error = break_lease(inode, flag);
1182 if (flag & O_TRUNC) {
1183 error = get_write_access(inode);
1188 * Refuse to truncate files with mandatory locks held on them.
1190 error = locks_verify_locked(inode);
1194 error = do_truncate(dentry, 0);
1196 put_write_access(inode);
1200 if (flag & FMODE_WRITE)
1209 * namei for open - this is in fact almost the whole open-routine.
1211 * Note that the low bits of "flag" aren't the same as in the open
1212 * system call - they are 00 - no permissions needed
1213 * 01 - read permission needed
1214 * 10 - write permission needed
1215 * 11 - read/write permissions needed
1216 * which is a lot more logical, and also allows the "no perm" needed
1217 * for symlinks (where the permissions are checked later).
1220 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1222 int acc_mode, error = 0;
1223 struct dentry *dentry;
1227 acc_mode = ACC_MODE(flag);
1229 /* Allow the LSM permission hook to distinguish append
1230 access from general write access. */
1231 if (flag & O_APPEND)
1232 acc_mode |= MAY_APPEND;
1234 /* Fill in the open() intent data */
1235 nd->intent.open.flags = flag;
1236 nd->intent.open.create_mode = mode;
1239 * The simplest case - just a plain lookup.
1241 if (!(flag & O_CREAT)) {
1242 error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd);
1245 dentry = nd->dentry;
1250 * Create - we need to know the parent.
1252 error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd);
1257 * We have the parent and last component. First of all, check
1258 * that we are not asked to creat(2) an obvious directory - that
1262 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1266 nd->flags &= ~LOOKUP_PARENT;
1267 down(&dir->d_inode->i_sem);
1268 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1271 error = PTR_ERR(dentry);
1272 if (IS_ERR(dentry)) {
1273 up(&dir->d_inode->i_sem);
1277 /* Negative dentry, just create the file */
1278 if (!dentry->d_inode) {
1279 if (!IS_POSIXACL(dir->d_inode))
1280 mode &= ~current->fs->umask;
1281 error = vfs_create(dir->d_inode, dentry, mode, nd);
1282 up(&dir->d_inode->i_sem);
1284 nd->dentry = dentry;
1287 /* Don't check for write permission, don't truncate */
1294 * It already exists.
1296 up(&dir->d_inode->i_sem);
1302 if (d_mountpoint(dentry)) {
1304 if (flag & O_NOFOLLOW)
1306 while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry));
1309 if (!dentry->d_inode)
1311 if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
1315 nd->dentry = dentry;
1317 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
1320 error = may_open(nd, acc_mode, flag);
1333 if (flag & O_NOFOLLOW)
1336 * This is subtle. Instead of calling do_follow_link() we do the
1337 * thing by hands. The reason is that this way we have zero link_count
1338 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1339 * After that we have the parent and last component, i.e.
1340 * we are in the same situation as after the first path_walk().
1341 * Well, almost - if the last component is normal we get its copy
1342 * stored in nd->last.name and we will have to putname() it when we
1343 * are done. Procfs-like symlinks just set LAST_BIND.
1345 nd->flags |= LOOKUP_PARENT;
1346 error = security_inode_follow_link(dentry, nd);
1349 update_atime(dentry->d_inode);
1350 error = dentry->d_inode->i_op->follow_link(dentry, nd);
1354 nd->flags &= ~LOOKUP_PARENT;
1355 if (nd->last_type == LAST_BIND) {
1356 dentry = nd->dentry;
1360 if (nd->last_type != LAST_NORM)
1362 if (nd->last.name[nd->last.len]) {
1363 putname(nd->last.name);
1368 putname(nd->last.name);
1372 down(&dir->d_inode->i_sem);
1373 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1374 putname(nd->last.name);
1379 static struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1381 struct dentry *dentry;
1383 down(&nd->dentry->d_inode->i_sem);
1384 dentry = ERR_PTR(-EEXIST);
1385 if (nd->last_type != LAST_NORM)
1387 nd->flags &= ~LOOKUP_PARENT;
1388 dentry = lookup_hash(&nd->last, nd->dentry);
1391 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1396 dentry = ERR_PTR(-ENOENT);
1401 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1403 int error = may_create(dir, dentry, NULL);
1408 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1411 if (!dir->i_op || !dir->i_op->mknod)
1414 error = security_inode_mknod(dir, dentry, mode, dev);
1419 error = dir->i_op->mknod(dir, dentry, mode, dev);
1421 inode_dir_notify(dir, DN_CREATE);
1422 security_inode_post_mknod(dir, dentry, mode, dev);
1427 asmlinkage long sys_mknod(const char __user * filename, int mode, dev_t dev)
1431 struct dentry * dentry;
1432 struct nameidata nd;
1436 tmp = getname(filename);
1438 return PTR_ERR(tmp);
1440 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1443 dentry = lookup_create(&nd, 0);
1444 error = PTR_ERR(dentry);
1446 if (!IS_POSIXACL(nd.dentry->d_inode))
1447 mode &= ~current->fs->umask;
1448 if (!IS_ERR(dentry)) {
1449 switch (mode & S_IFMT) {
1450 case 0: case S_IFREG:
1451 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1453 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
1454 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,dev);
1464 up(&nd.dentry->d_inode->i_sem);
1472 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1474 int error = may_create(dir, dentry, NULL);
1479 if (!dir->i_op || !dir->i_op->mkdir)
1482 mode &= (S_IRWXUGO|S_ISVTX);
1483 error = security_inode_mkdir(dir, dentry, mode);
1488 error = dir->i_op->mkdir(dir, dentry, mode);
1490 inode_dir_notify(dir, DN_CREATE);
1491 security_inode_post_mkdir(dir,dentry, mode);
1496 asmlinkage long sys_mkdir(const char __user * pathname, int mode)
1501 tmp = getname(pathname);
1502 error = PTR_ERR(tmp);
1504 struct dentry *dentry;
1505 struct nameidata nd;
1507 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1510 dentry = lookup_create(&nd, 1);
1511 error = PTR_ERR(dentry);
1512 if (!IS_ERR(dentry)) {
1513 if (!IS_POSIXACL(nd.dentry->d_inode))
1514 mode &= ~current->fs->umask;
1515 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1518 up(&nd.dentry->d_inode->i_sem);
1528 * We try to drop the dentry early: we should have
1529 * a usage count of 2 if we're the only user of this
1530 * dentry, and if that is true (possibly after pruning
1531 * the dcache), then we drop the dentry now.
1533 * A low-level filesystem can, if it choses, legally
1536 * if (!d_unhashed(dentry))
1539 * if it cannot handle the case of removing a directory
1540 * that is still in use by something else..
1542 static void d_unhash(struct dentry *dentry)
1545 spin_lock(&dcache_lock);
1546 switch (atomic_read(&dentry->d_count)) {
1548 spin_unlock(&dcache_lock);
1549 shrink_dcache_parent(dentry);
1550 spin_lock(&dcache_lock);
1551 if (atomic_read(&dentry->d_count) != 2)
1556 spin_unlock(&dcache_lock);
1559 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1561 int error = may_delete(dir, dentry, 1);
1566 if (!dir->i_op || !dir->i_op->rmdir)
1571 down(&dentry->d_inode->i_sem);
1573 if (d_mountpoint(dentry))
1576 error = security_inode_rmdir(dir, dentry);
1578 error = dir->i_op->rmdir(dir, dentry);
1580 dentry->d_inode->i_flags |= S_DEAD;
1583 up(&dentry->d_inode->i_sem);
1585 inode_dir_notify(dir, DN_DELETE);
1593 asmlinkage long sys_rmdir(const char __user * pathname)
1597 struct dentry *dentry;
1598 struct nameidata nd;
1600 name = getname(pathname);
1602 return PTR_ERR(name);
1604 error = path_lookup(name, LOOKUP_PARENT, &nd);
1608 switch(nd.last_type) {
1619 down(&nd.dentry->d_inode->i_sem);
1620 dentry = lookup_hash(&nd.last, nd.dentry);
1621 error = PTR_ERR(dentry);
1622 if (!IS_ERR(dentry)) {
1623 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1626 up(&nd.dentry->d_inode->i_sem);
1634 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1636 int error = may_delete(dir, dentry, 0);
1641 if (!dir->i_op || !dir->i_op->unlink)
1646 down(&dentry->d_inode->i_sem);
1647 if (d_mountpoint(dentry))
1650 error = security_inode_unlink(dir, dentry);
1652 error = dir->i_op->unlink(dir, dentry);
1654 up(&dentry->d_inode->i_sem);
1656 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1657 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1659 inode_dir_notify(dir, DN_DELETE);
1665 * Make sure that the actual truncation of the file will occur outside its
1666 * directory's i_sem. Truncate can take a long time if there is a lot of
1667 * writeout happening, and we don't want to prevent access to the directory
1668 * while waiting on the I/O.
1670 asmlinkage long sys_unlink(const char __user * pathname)
1674 struct dentry *dentry;
1675 struct nameidata nd;
1676 struct inode *inode = NULL;
1678 name = getname(pathname);
1680 return PTR_ERR(name);
1682 error = path_lookup(name, LOOKUP_PARENT, &nd);
1686 if (nd.last_type != LAST_NORM)
1688 down(&nd.dentry->d_inode->i_sem);
1689 dentry = lookup_hash(&nd.last, nd.dentry);
1690 error = PTR_ERR(dentry);
1691 if (!IS_ERR(dentry)) {
1692 /* Why not before? Because we want correct error value */
1693 if (nd.last.name[nd.last.len])
1695 inode = dentry->d_inode;
1697 atomic_inc(&inode->i_count);
1698 error = vfs_unlink(nd.dentry->d_inode, dentry);
1702 up(&nd.dentry->d_inode->i_sem);
1709 iput(inode); /* truncate the inode here */
1713 error = !dentry->d_inode ? -ENOENT :
1714 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1718 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
1720 int error = may_create(dir, dentry, NULL);
1725 if (!dir->i_op || !dir->i_op->symlink)
1728 error = security_inode_symlink(dir, dentry, oldname);
1733 error = dir->i_op->symlink(dir, dentry, oldname);
1735 inode_dir_notify(dir, DN_CREATE);
1736 security_inode_post_symlink(dir, dentry, oldname);
1741 asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
1747 from = getname(oldname);
1749 return PTR_ERR(from);
1750 to = getname(newname);
1751 error = PTR_ERR(to);
1753 struct dentry *dentry;
1754 struct nameidata nd;
1756 error = path_lookup(to, LOOKUP_PARENT, &nd);
1759 dentry = lookup_create(&nd, 0);
1760 error = PTR_ERR(dentry);
1761 if (!IS_ERR(dentry)) {
1762 error = vfs_symlink(nd.dentry->d_inode, dentry, from);
1765 up(&nd.dentry->d_inode->i_sem);
1774 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1776 struct inode *inode = old_dentry->d_inode;
1782 error = may_create(dir, new_dentry, NULL);
1786 if (dir->i_sb != inode->i_sb)
1790 * A link to an append-only or immutable file cannot be created.
1792 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1794 if (!dir->i_op || !dir->i_op->link)
1796 if (S_ISDIR(old_dentry->d_inode->i_mode))
1799 error = security_inode_link(old_dentry, dir, new_dentry);
1803 down(&old_dentry->d_inode->i_sem);
1805 error = dir->i_op->link(old_dentry, dir, new_dentry);
1806 up(&old_dentry->d_inode->i_sem);
1808 inode_dir_notify(dir, DN_CREATE);
1809 security_inode_post_link(old_dentry, dir, new_dentry);
1815 * Hardlinks are often used in delicate situations. We avoid
1816 * security-related surprises by not following symlinks on the
1819 * We don't follow them on the oldname either to be compatible
1820 * with linux 2.0, and to avoid hard-linking to directories
1821 * and other special files. --ADM
1823 asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
1825 struct dentry *new_dentry;
1826 struct nameidata nd, old_nd;
1830 to = getname(newname);
1834 error = __user_walk(oldname, 0, &old_nd);
1837 error = path_lookup(to, LOOKUP_PARENT, &nd);
1841 if (old_nd.mnt != nd.mnt)
1843 new_dentry = lookup_create(&nd, 0);
1844 error = PTR_ERR(new_dentry);
1845 if (!IS_ERR(new_dentry)) {
1846 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
1849 up(&nd.dentry->d_inode->i_sem);
1853 path_release(&old_nd);
1861 * The worst of all namespace operations - renaming directory. "Perverted"
1862 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
1864 * a) we can get into loop creation. Check is done in is_subdir().
1865 * b) race potential - two innocent renames can create a loop together.
1866 * That's where 4.4 screws up. Current fix: serialization on
1867 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
1869 * c) we have to lock _three_ objects - parents and victim (if it exists).
1870 * And that - after we got ->i_sem on parents (until then we don't know
1871 * whether the target exists). Solution: try to be smart with locking
1872 * order for inodes. We rely on the fact that tree topology may change
1873 * only under ->s_vfs_rename_sem _and_ that parent of the object we
1874 * move will be locked. Thus we can rank directories by the tree
1875 * (ancestors first) and rank all non-directories after them.
1876 * That works since everybody except rename does "lock parent, lookup,
1877 * lock child" and rename is under ->s_vfs_rename_sem.
1878 * HOWEVER, it relies on the assumption that any object with ->lookup()
1879 * has no more than 1 dentry. If "hybrid" objects will ever appear,
1880 * we'd better make sure that there's no link(2) for them.
1881 * d) some filesystems don't support opened-but-unlinked directories,
1882 * either because of layout or because they are not ready to deal with
1883 * all cases correctly. The latter will be fixed (taking this sort of
1884 * stuff into VFS), but the former is not going away. Solution: the same
1885 * trick as in rmdir().
1886 * e) conversion from fhandle to dentry may come in the wrong moment - when
1887 * we are removing the target. Solution: we will have to grab ->i_sem
1888 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
1889 * ->i_sem on parents, which works but leads to some truely excessive
1892 int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
1893 struct inode *new_dir, struct dentry *new_dentry)
1896 struct inode *target;
1899 * If we are going to change the parent - check write permissions,
1900 * we'll need to flip '..'.
1902 if (new_dir != old_dir) {
1903 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
1908 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
1912 target = new_dentry->d_inode;
1914 down(&target->i_sem);
1915 d_unhash(new_dentry);
1917 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
1920 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1923 target->i_flags |= S_DEAD;
1925 if (d_unhashed(new_dentry))
1926 d_rehash(new_dentry);
1930 d_move(old_dentry,new_dentry);
1931 security_inode_post_rename(old_dir, old_dentry,
1932 new_dir, new_dentry);
1937 int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
1938 struct inode *new_dir, struct dentry *new_dentry)
1940 struct inode *target;
1943 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
1948 target = new_dentry->d_inode;
1950 down(&target->i_sem);
1951 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
1954 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1956 /* The following d_move() should become unconditional */
1957 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
1958 d_move(old_dentry, new_dentry);
1959 security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
1967 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1968 struct inode *new_dir, struct dentry *new_dentry)
1971 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
1973 if (old_dentry->d_inode == new_dentry->d_inode)
1976 error = may_delete(old_dir, old_dentry, is_dir);
1980 if (!new_dentry->d_inode)
1981 error = may_create(new_dir, new_dentry, NULL);
1983 error = may_delete(new_dir, new_dentry, is_dir);
1987 if (!old_dir->i_op || !old_dir->i_op->rename)
1990 DQUOT_INIT(old_dir);
1991 DQUOT_INIT(new_dir);
1994 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
1996 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
1998 if (old_dir == new_dir)
1999 inode_dir_notify(old_dir, DN_RENAME);
2001 inode_dir_notify(old_dir, DN_DELETE);
2002 inode_dir_notify(new_dir, DN_CREATE);
2008 static inline int do_rename(const char * oldname, const char * newname)
2011 struct dentry * old_dir, * new_dir;
2012 struct dentry * old_dentry, *new_dentry;
2013 struct dentry * trap;
2014 struct nameidata oldnd, newnd;
2016 error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2020 error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2025 if (oldnd.mnt != newnd.mnt)
2028 old_dir = oldnd.dentry;
2030 if (oldnd.last_type != LAST_NORM)
2033 new_dir = newnd.dentry;
2034 if (newnd.last_type != LAST_NORM)
2037 trap = lock_rename(new_dir, old_dir);
2039 old_dentry = lookup_hash(&oldnd.last, old_dir);
2040 error = PTR_ERR(old_dentry);
2041 if (IS_ERR(old_dentry))
2043 /* source must exist */
2045 if (!old_dentry->d_inode)
2047 /* unless the source is a directory trailing slashes give -ENOTDIR */
2048 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2050 if (oldnd.last.name[oldnd.last.len])
2052 if (newnd.last.name[newnd.last.len])
2055 /* source should not be ancestor of target */
2057 if (old_dentry == trap)
2059 new_dentry = lookup_hash(&newnd.last, new_dir);
2060 error = PTR_ERR(new_dentry);
2061 if (IS_ERR(new_dentry))
2063 /* target should not be an ancestor of source */
2065 if (new_dentry == trap)
2068 error = vfs_rename(old_dir->d_inode, old_dentry,
2069 new_dir->d_inode, new_dentry);
2075 unlock_rename(new_dir, old_dir);
2077 path_release(&newnd);
2079 path_release(&oldnd);
2084 asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
2090 from = getname(oldname);
2092 return PTR_ERR(from);
2093 to = getname(newname);
2094 error = PTR_ERR(to);
2096 error = do_rename(from,to);
2103 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2107 len = PTR_ERR(link);
2112 if (len > (unsigned) buflen)
2114 if (copy_to_user(buffer, link, len))
2121 __vfs_follow_link(struct nameidata *nd, const char *link)
2130 if (!walk_init_root(link, nd))
2131 /* weird __emul_prefix() stuff did it */
2134 res = link_path_walk(link, nd);
2136 if (current->link_count || res || nd->last_type!=LAST_NORM)
2139 * If it is an iterative symlinks resolution in open_namei() we
2140 * have to copy the last component. And all that crap because of
2141 * bloody create() on broken symlinks. Furrfu...
2144 if (unlikely(!name)) {
2148 strcpy(name, nd->last.name);
2149 nd->last.name = name;
2153 return PTR_ERR(link);
2156 int vfs_follow_link(struct nameidata *nd, const char *link)
2158 return __vfs_follow_link(nd, link);
2161 /* get the link contents into pagecache */
2162 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2165 struct address_space *mapping = dentry->d_inode->i_mapping;
2166 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2170 wait_on_page_locked(page);
2171 if (!PageUptodate(page))
2177 page_cache_release(page);
2178 return ERR_PTR(-EIO);
2184 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2186 struct page *page = NULL;
2187 char *s = page_getlink(dentry, &page);
2188 int res = vfs_readlink(dentry,buffer,buflen,s);
2191 page_cache_release(page);
2196 int page_follow_link(struct dentry *dentry, struct nameidata *nd)
2198 struct page *page = NULL;
2199 char *s = page_getlink(dentry, &page);
2200 int res = __vfs_follow_link(nd, s);
2203 page_cache_release(page);
2208 int page_symlink(struct inode *inode, const char *symname, int len)
2210 struct address_space *mapping = inode->i_mapping;
2211 struct page *page = grab_cache_page(mapping, 0);
2217 err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2220 kaddr = kmap_atomic(page, KM_USER0);
2221 memcpy(kaddr, symname, len-1);
2222 kunmap_atomic(kaddr, KM_USER0);
2223 mapping->a_ops->commit_write(NULL, page, 0, len-1);
2225 * Notice that we are _not_ going to block here - end of page is
2226 * unmapped, so this will only try to map the rest of page, see
2227 * that it is unmapped (typically even will not look into inode -
2228 * ->i_size will be enough for everything) and zero it out.
2229 * OTOH it's obviously correct and should make the page up-to-date.
2231 if (!PageUptodate(page)) {
2232 err = mapping->a_ops->readpage(NULL, page);
2233 wait_on_page_locked(page);
2237 page_cache_release(page);
2240 mark_inode_dirty(inode);
2244 page_cache_release(page);
2249 struct inode_operations page_symlink_inode_operations = {
2250 .readlink = page_readlink,
2251 .follow_link = page_follow_link,