4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Some corrections by tytso.
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
21 #include <linux/namei.h>
22 #include <linux/quotaops.h>
23 #include <linux/pagemap.h>
24 #include <linux/dnotify.h>
25 #include <linux/smp_lock.h>
26 #include <linux/personality.h>
27 #include <linux/security.h>
28 #include <linux/mount.h>
29 #include <linux/audit.h>
30 #include <linux/vs_base.h>
32 #include <asm/namei.h>
33 #include <asm/uaccess.h>
35 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
37 /* [Feb-1997 T. Schoebel-Theuer]
38 * Fundamental changes in the pathname lookup mechanisms (namei)
39 * were necessary because of omirr. The reason is that omirr needs
40 * to know the _real_ pathname, not the user-supplied one, in case
41 * of symlinks (and also when transname replacements occur).
43 * The new code replaces the old recursive symlink resolution with
44 * an iterative one (in case of non-nested symlink chains). It does
45 * this with calls to <fs>_follow_link().
46 * As a side effect, dir_namei(), _namei() and follow_link() are now
47 * replaced with a single function lookup_dentry() that can handle all
48 * the special cases of the former code.
50 * With the new dcache, the pathname is stored at each inode, at least as
51 * long as the refcount of the inode is positive. As a side effect, the
52 * size of the dcache depends on the inode cache and thus is dynamic.
54 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
55 * resolution to correspond with current state of the code.
57 * Note that the symlink resolution is not *completely* iterative.
58 * There is still a significant amount of tail- and mid- recursion in
59 * the algorithm. Also, note that <fs>_readlink() is not used in
60 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
61 * may return different results than <fs>_follow_link(). Many virtual
62 * filesystems (including /proc) exhibit this behavior.
65 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
66 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
67 * and the name already exists in form of a symlink, try to create the new
68 * name indicated by the symlink. The old code always complained that the
69 * name already exists, due to not following the symlink even if its target
70 * is nonexistent. The new semantics affects also mknod() and link() when
71 * the name is a symlink pointing to a non-existant name.
73 * I don't know which semantics is the right one, since I have no access
74 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
75 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
76 * "old" one. Personally, I think the new semantics is much more logical.
77 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
78 * file does succeed in both HP-UX and SunOs, but not in Solaris
79 * and in the old Linux semantics.
82 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
83 * semantics. See the comments in "open_namei" and "do_link" below.
85 * [10-Sep-98 Alan Modra] Another symlink change.
88 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
89 * inside the path - always follow.
90 * in the last component in creation/removal/renaming - never follow.
91 * if LOOKUP_FOLLOW passed - follow.
92 * if the pathname has trailing slashes - follow.
93 * otherwise - don't follow.
94 * (applied in that order).
96 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
97 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
98 * During the 2.4 we need to fix the userland stuff depending on it -
99 * hopefully we will be able to get rid of that wart in 2.5. So far only
100 * XEmacs seems to be relying on it...
103 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
104 * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives
105 * any extra contention...
108 /* In order to reduce some races, while at the same time doing additional
109 * checking and hopefully speeding things up, we copy filenames to the
110 * kernel data space before using them..
112 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
113 * PATH_MAX includes the nul terminator --RR.
115 static inline int do_getname(const char __user *filename, char *page)
118 unsigned long len = PATH_MAX;
120 if ((unsigned long) filename >= TASK_SIZE) {
121 if (!segment_eq(get_fs(), KERNEL_DS))
123 } else if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
124 len = TASK_SIZE - (unsigned long) filename;
126 retval = strncpy_from_user((char *)page, filename, len);
130 return -ENAMETOOLONG;
136 char * getname(const char __user * filename)
140 result = ERR_PTR(-ENOMEM);
143 int retval = do_getname(filename, tmp);
148 result = ERR_PTR(retval);
151 if (unlikely(current->audit_context) && !IS_ERR(result) && result)
152 audit_getname(result);
159 * is used to check for read/write/execute permissions on a file.
160 * We use "fsuid" for this, letting us set arbitrary permissions
161 * for filesystem access without changing the "normal" uids which
162 * are used for other things..
164 int vfs_permission(struct inode * inode, int mask)
166 umode_t mode = inode->i_mode;
168 if (mask & MAY_WRITE) {
170 * Nobody gets write access to a read-only fs.
172 if (IS_RDONLY(inode) &&
173 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
177 * Nobody gets write access to an immutable file.
179 if (IS_IMMUTABLE(inode))
183 if (current->fsuid == inode->i_uid)
185 else if (in_group_p(inode->i_gid))
189 * If the DACs are ok we don't need any capability check.
191 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
195 * Read/write DACs are always overridable.
196 * Executable DACs are overridable if at least one exec bit is set.
198 if (!(mask & MAY_EXEC) ||
199 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
200 if (capable(CAP_DAC_OVERRIDE))
204 * Searching includes executable on directories, else just read.
206 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
207 if (capable(CAP_DAC_READ_SEARCH))
213 static inline int xid_permission(struct inode *inode, int mask, struct nameidata *nd)
215 if (inode->i_xid == 0)
217 if (vx_check(inode->i_xid, VX_ADMIN|VX_WATCH|VX_IDENT))
220 printk("VSW: xid=%d denied access to %p[#%d,%lu] »%*s«.\n",
221 vx_current_xid(), inode, inode->i_xid, inode->i_ino,
222 nd->dentry->d_name.len, nd->dentry->d_name.name);
227 int permission(struct inode * inode,int mask, struct nameidata *nd)
232 /* Ordinary permission routines do not understand MAY_APPEND. */
233 submask = mask & ~MAY_APPEND;
235 if ((retval = xid_permission(inode, mask, nd)))
237 if (inode->i_op && inode->i_op->permission)
238 retval = inode->i_op->permission(inode, submask, nd);
240 retval = vfs_permission(inode, submask);
244 return security_inode_permission(inode, mask, nd);
248 * get_write_access() gets write permission for a file.
249 * put_write_access() releases this write permission.
250 * This is used for regular files.
251 * We cannot support write (and maybe mmap read-write shared) accesses and
252 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
253 * can have the following values:
254 * 0: no writers, no VM_DENYWRITE mappings
255 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
256 * > 0: (i_writecount) users are writing to the file.
258 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
259 * except for the cases where we don't hold i_writecount yet. Then we need to
260 * use {get,deny}_write_access() - these functions check the sign and refuse
261 * to do the change if sign is wrong. Exclusion between them is provided by
262 * the inode->i_lock spinlock.
265 int get_write_access(struct inode * inode)
267 spin_lock(&inode->i_lock);
268 if (atomic_read(&inode->i_writecount) < 0) {
269 spin_unlock(&inode->i_lock);
272 atomic_inc(&inode->i_writecount);
273 spin_unlock(&inode->i_lock);
278 int deny_write_access(struct file * file)
280 struct inode *inode = file->f_dentry->d_inode;
282 spin_lock(&inode->i_lock);
283 if (atomic_read(&inode->i_writecount) > 0) {
284 spin_unlock(&inode->i_lock);
287 atomic_dec(&inode->i_writecount);
288 spin_unlock(&inode->i_lock);
293 void path_release(struct nameidata *nd)
300 * umount() mustn't call path_release()/mntput() as that would clear
303 void path_release_on_umount(struct nameidata *nd)
310 * Internal lookup() using the new generic dcache.
313 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
315 struct dentry * dentry = __d_lookup(parent, name);
317 /* lockess __d_lookup may fail due to concurrent d_move()
318 * in some unrelated directory, so try with d_lookup
321 dentry = d_lookup(parent, name);
323 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
324 if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
333 * Short-cut version of permission(), for calling by
334 * path_walk(), when dcache lock is held. Combines parts
335 * of permission() and vfs_permission(), and tests ONLY for
336 * MAY_EXEC permission.
338 * If appropriate, check DAC only. If not appropriate, or
339 * short-cut DAC fails, then call permission() to do more
340 * complete permission check.
342 static inline int exec_permission_lite(struct inode *inode,
343 struct nameidata *nd)
345 umode_t mode = inode->i_mode;
347 if (inode->i_op && inode->i_op->permission)
350 if (current->fsuid == inode->i_uid)
352 else if (in_group_p(inode->i_gid))
358 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
361 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
364 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
369 return security_inode_permission(inode, MAY_EXEC, nd);
373 * This is called when everything else fails, and we actually have
374 * to go to the low-level filesystem to find out what we should do..
376 * We get the directory semaphore, and after getting that we also
377 * make sure that nobody added the entry to the dcache in the meantime..
380 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
382 struct dentry * result;
383 struct inode *dir = parent->d_inode;
387 * First re-do the cached lookup just in case it was created
388 * while we waited for the directory semaphore..
390 * FIXME! This could use version numbering or similar to
391 * avoid unnecessary cache lookups.
393 * The "dcache_lock" is purely to protect the RCU list walker
394 * from concurrent renames at this point (we mustn't get false
395 * negatives from the RCU list walk here, unlike the optimistic
398 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
400 result = d_lookup(parent, name);
402 struct dentry * dentry = d_alloc(parent, name);
403 result = ERR_PTR(-ENOMEM);
405 result = dir->i_op->lookup(dir, dentry, nd);
416 * Uhhuh! Nasty case: the cache was re-populated while
417 * we waited on the semaphore. Need to revalidate.
420 if (result->d_op && result->d_op->d_revalidate) {
421 if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
423 result = ERR_PTR(-ENOENT);
429 static int __emul_lookup_dentry(const char *, struct nameidata *);
433 walk_init_root(const char *name, struct nameidata *nd)
435 read_lock(¤t->fs->lock);
436 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
437 nd->mnt = mntget(current->fs->altrootmnt);
438 nd->dentry = dget(current->fs->altroot);
439 read_unlock(¤t->fs->lock);
440 if (__emul_lookup_dentry(name,nd))
442 read_lock(¤t->fs->lock);
444 nd->mnt = mntget(current->fs->rootmnt);
445 nd->dentry = dget(current->fs->root);
446 read_unlock(¤t->fs->lock);
450 static inline int __vfs_follow_link(struct nameidata *nd, const char *link)
459 if (!walk_init_root(link, nd))
460 /* weird __emul_prefix() stuff did it */
463 res = link_path_walk(link, nd);
465 if (nd->depth || res || nd->last_type!=LAST_NORM)
468 * If it is an iterative symlinks resolution in open_namei() we
469 * have to copy the last component. And all that crap because of
470 * bloody create() on broken symlinks. Furrfu...
473 if (unlikely(!name)) {
477 strcpy(name, nd->last.name);
478 nd->last.name = name;
482 return PTR_ERR(link);
486 * This limits recursive symlink follows to 8, while
487 * limiting consecutive symlinks to 40.
489 * Without that kind of total limit, nasty chains of consecutive
490 * symlinks can cause almost arbitrarily long lookups.
492 static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd)
495 if (current->link_count >= MAX_NESTED_LINKS)
497 if (current->total_link_count >= 40)
499 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
501 err = security_inode_follow_link(dentry, nd);
504 current->link_count++;
505 current->total_link_count++;
507 touch_atime(nd->mnt, dentry);
508 nd_set_link(nd, NULL);
509 err = dentry->d_inode->i_op->follow_link(dentry, nd);
511 char *s = nd_get_link(nd);
513 err = __vfs_follow_link(nd, s);
514 if (dentry->d_inode->i_op->put_link)
515 dentry->d_inode->i_op->put_link(dentry, nd);
517 current->link_count--;
525 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
527 struct vfsmount *parent;
528 struct dentry *mountpoint;
529 spin_lock(&vfsmount_lock);
530 parent=(*mnt)->mnt_parent;
531 if (parent == *mnt) {
532 spin_unlock(&vfsmount_lock);
536 mountpoint=dget((*mnt)->mnt_mountpoint);
537 spin_unlock(&vfsmount_lock);
539 *dentry = mountpoint;
545 /* no need for dcache_lock, as serialization is taken care in
548 static int follow_mount(struct vfsmount **mnt, struct dentry **dentry)
551 while (d_mountpoint(*dentry)) {
552 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
558 *dentry = dget(mounted->mnt_root);
564 /* no need for dcache_lock, as serialization is taken care in
567 static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
569 struct vfsmount *mounted;
571 mounted = lookup_mnt(*mnt, *dentry);
576 *dentry = dget(mounted->mnt_root);
582 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
584 return __follow_down(mnt,dentry);
587 static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry)
590 struct vfsmount *parent;
591 struct dentry *old = *dentry;
593 read_lock(¤t->fs->lock);
594 if (*dentry == current->fs->root &&
595 *mnt == current->fs->rootmnt) {
596 read_unlock(¤t->fs->lock);
599 read_unlock(¤t->fs->lock);
600 spin_lock(&dcache_lock);
601 if (*dentry != (*mnt)->mnt_root) {
602 *dentry = dget((*dentry)->d_parent);
603 spin_unlock(&dcache_lock);
607 spin_unlock(&dcache_lock);
608 spin_lock(&vfsmount_lock);
609 parent = (*mnt)->mnt_parent;
610 if (parent == *mnt) {
611 spin_unlock(&vfsmount_lock);
615 *dentry = dget((*mnt)->mnt_mountpoint);
616 spin_unlock(&vfsmount_lock);
621 follow_mount(mnt, dentry);
625 struct vfsmount *mnt;
626 struct dentry *dentry;
630 * It's more convoluted than I'd like it to be, but... it's still fairly
631 * small and for now I'd prefer to have fast path as straight as possible.
632 * It _is_ time-critical.
634 static int do_lookup(struct nameidata *nd, struct qstr *name,
637 struct vfsmount *mnt = nd->mnt;
638 struct dentry *dentry = __d_lookup(nd->dentry, name);
642 if (dentry->d_op && dentry->d_op->d_revalidate)
643 goto need_revalidate;
646 path->dentry = dentry;
650 dentry = real_lookup(nd->dentry, name, nd);
656 if (dentry->d_op->d_revalidate(dentry, nd))
658 if (d_invalidate(dentry))
664 return PTR_ERR(dentry);
670 * This is the basic name resolution function, turning a pathname
671 * into the final dentry.
673 * We expect 'base' to be positive and a directory.
675 int fastcall link_path_walk(const char * name, struct nameidata *nd)
680 unsigned int lookup_flags = nd->flags;
687 inode = nd->dentry->d_inode;
689 lookup_flags = LOOKUP_FOLLOW;
691 /* At this point we know we have a real path component. */
697 err = exec_permission_lite(inode, nd);
698 if (err == -EAGAIN) {
699 err = permission(inode, MAY_EXEC, nd);
705 c = *(const unsigned char *)name;
707 hash = init_name_hash();
710 hash = partial_name_hash(c, hash);
711 c = *(const unsigned char *)name;
712 } while (c && (c != '/'));
713 this.len = name - (const char *) this.name;
714 this.hash = end_name_hash(hash);
716 /* remove trailing slashes? */
719 while (*++name == '/');
721 goto last_with_slashes;
724 * "." and ".." are special - ".." especially so because it has
725 * to be able to know about the current root directory and
726 * parent relationships.
728 if (this.name[0] == '.') switch (this.len) {
732 if (this.name[1] != '.')
734 follow_dotdot(&nd->mnt, &nd->dentry);
735 inode = nd->dentry->d_inode;
741 * See if the low-level filesystem might want
742 * to use its own hash..
744 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
745 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
749 nd->flags |= LOOKUP_CONTINUE;
750 /* This does the actual lookups.. */
751 err = do_lookup(nd, &this, &next);
754 /* Check mountpoints.. */
755 follow_mount(&next.mnt, &next.dentry);
758 inode = next.dentry->d_inode;
765 if (inode->i_op->follow_link) {
767 err = do_follow_link(next.dentry, nd);
773 inode = nd->dentry->d_inode;
782 nd->dentry = next.dentry;
785 if (!inode->i_op->lookup)
788 /* here ends the main loop */
791 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
793 nd->flags &= ~LOOKUP_CONTINUE;
794 if (lookup_flags & LOOKUP_PARENT)
796 if (this.name[0] == '.') switch (this.len) {
800 if (this.name[1] != '.')
802 follow_dotdot(&nd->mnt, &nd->dentry);
803 inode = nd->dentry->d_inode;
808 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
809 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
813 err = do_lookup(nd, &this, &next);
816 follow_mount(&next.mnt, &next.dentry);
817 inode = next.dentry->d_inode;
818 if ((lookup_flags & LOOKUP_FOLLOW)
819 && inode && inode->i_op && inode->i_op->follow_link) {
821 err = do_follow_link(next.dentry, nd);
826 inode = nd->dentry->d_inode;
830 nd->dentry = next.dentry;
835 if (lookup_flags & LOOKUP_DIRECTORY) {
837 if (!inode->i_op || !inode->i_op->lookup)
843 nd->last_type = LAST_NORM;
844 if (this.name[0] != '.')
847 nd->last_type = LAST_DOT;
848 else if (this.len == 2 && this.name[1] == '.')
849 nd->last_type = LAST_DOTDOT;
854 * We bypassed the ordinary revalidation routines.
855 * We may need to check the cached dentry for staleness.
857 if (nd->dentry && nd->dentry->d_sb &&
858 (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
860 /* Note: we do not d_invalidate() */
861 if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
875 int fastcall path_walk(const char * name, struct nameidata *nd)
877 current->total_link_count = 0;
878 return link_path_walk(name, nd);
882 /* returns 1 if everything is done */
883 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
885 if (path_walk(name, nd))
886 return 0; /* something went wrong... */
888 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
889 struct nameidata nd_root;
891 * NAME was not found in alternate root or it's a directory. Try to find
892 * it in the normal root:
894 nd_root.last_type = LAST_ROOT;
895 nd_root.flags = nd->flags;
897 memcpy(&nd_root.intent, &nd->intent, sizeof(nd_root.intent));
898 read_lock(¤t->fs->lock);
899 nd_root.mnt = mntget(current->fs->rootmnt);
900 nd_root.dentry = dget(current->fs->root);
901 read_unlock(¤t->fs->lock);
902 if (path_walk(name, &nd_root))
904 if (nd_root.dentry->d_inode) {
906 nd->dentry = nd_root.dentry;
907 nd->mnt = nd_root.mnt;
908 nd->last = nd_root.last;
911 path_release(&nd_root);
916 void set_fs_altroot(void)
918 char *emul = __emul_prefix();
920 struct vfsmount *mnt = NULL, *oldmnt;
921 struct dentry *dentry = NULL, *olddentry;
926 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
932 write_lock(¤t->fs->lock);
933 oldmnt = current->fs->altrootmnt;
934 olddentry = current->fs->altroot;
935 current->fs->altrootmnt = mnt;
936 current->fs->altroot = dentry;
937 write_unlock(¤t->fs->lock);
944 int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
948 nd->last_type = LAST_ROOT; /* if there are only slashes... */
952 read_lock(¤t->fs->lock);
954 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
955 nd->mnt = mntget(current->fs->altrootmnt);
956 nd->dentry = dget(current->fs->altroot);
957 read_unlock(¤t->fs->lock);
958 if (__emul_lookup_dentry(name,nd))
960 read_lock(¤t->fs->lock);
962 nd->mnt = mntget(current->fs->rootmnt);
963 nd->dentry = dget(current->fs->root);
966 nd->mnt = mntget(current->fs->pwdmnt);
967 nd->dentry = dget(current->fs->pwd);
969 read_unlock(¤t->fs->lock);
970 current->total_link_count = 0;
971 retval = link_path_walk(name, nd);
972 if (unlikely(current->audit_context
973 && nd && nd->dentry && nd->dentry->d_inode))
975 nd->dentry->d_inode->i_ino,
976 nd->dentry->d_inode->i_rdev);
981 * Restricted form of lookup. Doesn't follow links, single-component only,
982 * needs parent already locked. Doesn't follow mounts.
985 static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
987 struct dentry * dentry;
991 inode = base->d_inode;
992 err = permission(inode, MAY_EXEC, nd);
993 dentry = ERR_PTR(err);
998 * See if the low-level filesystem might want
999 * to use its own hash..
1001 if (base->d_op && base->d_op->d_hash) {
1002 err = base->d_op->d_hash(base, name);
1003 dentry = ERR_PTR(err);
1008 dentry = cached_lookup(base, name, nd);
1010 struct dentry *new = d_alloc(base, name);
1011 dentry = ERR_PTR(-ENOMEM);
1014 dentry = inode->i_op->lookup(inode, new, nd);
1024 struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
1026 return __lookup_hash(name, base, NULL);
1030 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
1041 hash = init_name_hash();
1043 c = *(const unsigned char *)name++;
1044 if (c == '/' || c == '\0')
1046 hash = partial_name_hash(c, hash);
1048 this.hash = end_name_hash(hash);
1050 return lookup_hash(&this, base);
1052 return ERR_PTR(-EACCES);
1058 * is used by most simple commands to get the inode of a specified name.
1059 * Open, link etc use their own routines, but this is enough for things
1062 * namei exists in two versions: namei/lnamei. The only difference is
1063 * that namei follows links, while lnamei does not.
1066 int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1068 char *tmp = getname(name);
1069 int err = PTR_ERR(tmp);
1072 err = path_lookup(tmp, flags, nd);
1079 * It's inline, so penalty for filesystems that don't use sticky bit is
1082 static inline int check_sticky(struct inode *dir, struct inode *inode)
1084 if (!(dir->i_mode & S_ISVTX))
1086 if (inode->i_uid == current->fsuid)
1088 if (dir->i_uid == current->fsuid)
1090 return !capable(CAP_FOWNER);
1094 * Check whether we can remove a link victim from directory dir, check
1095 * whether the type of victim is right.
1096 * 1. We can't do it if dir is read-only (done in permission())
1097 * 2. We should have write and exec permissions on dir
1098 * 3. We can't remove anything from append-only dir
1099 * 4. We can't do anything with immutable dir (done in permission())
1100 * 5. If the sticky bit on dir is set we should either
1101 * a. be owner of dir, or
1102 * b. be owner of victim, or
1103 * c. have CAP_FOWNER capability
1104 * 6. If the victim is append-only or immutable we can't do antyhing with
1105 * links pointing to it.
1106 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1107 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1108 * 9. We can't remove a root or mountpoint.
1109 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1110 * nfs_async_unlink().
1112 static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1115 if (!victim->d_inode || victim->d_parent->d_inode != dir)
1117 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1122 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1123 IS_IXORUNLINK(victim->d_inode))
1126 if (!S_ISDIR(victim->d_inode->i_mode))
1128 if (IS_ROOT(victim))
1130 } else if (S_ISDIR(victim->d_inode->i_mode))
1132 if (IS_DEADDIR(dir))
1134 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1139 /* Check whether we can create an object with dentry child in directory
1141 * 1. We can't do it if child already exists (open has special treatment for
1142 * this case, but since we are inlined it's OK)
1143 * 2. We can't do it if dir is read-only (done in permission())
1144 * 3. We should have write and exec permissions on dir
1145 * 4. We can't do it if dir is immutable (done in permission())
1147 static inline int may_create(struct inode *dir, struct dentry *child,
1148 struct nameidata *nd)
1152 if (IS_DEADDIR(dir))
1154 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1158 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1161 * O_DIRECTORY translates into forcing a directory lookup.
1163 static inline int lookup_flags(unsigned int f)
1165 unsigned long retval = LOOKUP_FOLLOW;
1168 retval &= ~LOOKUP_FOLLOW;
1170 if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1171 retval &= ~LOOKUP_FOLLOW;
1173 if (f & O_DIRECTORY)
1174 retval |= LOOKUP_DIRECTORY;
1180 * p1 and p2 should be directories on the same fs.
1182 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1187 down(&p1->d_inode->i_sem);
1191 down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1193 for (p = p1; p->d_parent != p; p = p->d_parent) {
1194 if (p->d_parent == p2) {
1195 down(&p2->d_inode->i_sem);
1196 down(&p1->d_inode->i_sem);
1201 for (p = p2; p->d_parent != p; p = p->d_parent) {
1202 if (p->d_parent == p1) {
1203 down(&p1->d_inode->i_sem);
1204 down(&p2->d_inode->i_sem);
1209 down(&p1->d_inode->i_sem);
1210 down(&p2->d_inode->i_sem);
1214 void unlock_rename(struct dentry *p1, struct dentry *p2)
1216 up(&p1->d_inode->i_sem);
1218 up(&p2->d_inode->i_sem);
1219 up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1223 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1224 struct nameidata *nd)
1226 int error = may_create(dir, dentry, nd);
1231 if (!dir->i_op || !dir->i_op->create)
1232 return -EACCES; /* shouldn't it be ENOSYS? */
1235 error = security_inode_create(dir, dentry, mode);
1239 error = dir->i_op->create(dir, dentry, mode, nd);
1241 inode_dir_notify(dir, DN_CREATE);
1242 security_inode_post_create(dir, dentry, mode);
1247 int may_open(struct nameidata *nd, int acc_mode, int flag)
1249 struct dentry *dentry = nd->dentry;
1250 struct inode *inode = dentry->d_inode;
1256 if (S_ISLNK(inode->i_mode))
1259 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1262 error = permission(inode, acc_mode, nd);
1267 * FIFO's, sockets and device files are special: they don't
1268 * actually live on the filesystem itself, and as such you
1269 * can write to them even if the filesystem is read-only.
1271 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1273 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1274 if (nd->mnt->mnt_flags & MNT_NODEV)
1278 } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
1281 * An append-only file must be opened in append mode for writing.
1283 if (IS_APPEND(inode)) {
1284 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1290 /* O_NOATIME can only be set by the owner or superuser */
1291 if (flag & O_NOATIME)
1292 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1296 * Ensure there are no outstanding leases on the file.
1298 error = break_lease(inode, flag);
1302 if (flag & O_TRUNC) {
1303 error = get_write_access(inode);
1308 * Refuse to truncate files with mandatory locks held on them.
1310 error = locks_verify_locked(inode);
1314 error = do_truncate(dentry, 0);
1316 put_write_access(inode);
1320 if (flag & FMODE_WRITE)
1329 * namei for open - this is in fact almost the whole open-routine.
1331 * Note that the low bits of "flag" aren't the same as in the open
1332 * system call - they are 00 - no permissions needed
1333 * 01 - read permission needed
1334 * 10 - write permission needed
1335 * 11 - read/write permissions needed
1336 * which is a lot more logical, and also allows the "no perm" needed
1337 * for symlinks (where the permissions are checked later).
1340 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1342 int acc_mode, error = 0;
1343 struct dentry *dentry;
1347 acc_mode = ACC_MODE(flag);
1349 /* Allow the LSM permission hook to distinguish append
1350 access from general write access. */
1351 if (flag & O_APPEND)
1352 acc_mode |= MAY_APPEND;
1354 /* Fill in the open() intent data */
1355 nd->intent.open.flags = flag;
1356 nd->intent.open.create_mode = mode;
1359 * The simplest case - just a plain lookup.
1361 if (!(flag & O_CREAT)) {
1362 error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd);
1369 * Create - we need to know the parent.
1371 error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd);
1376 * We have the parent and last component. First of all, check
1377 * that we are not asked to creat(2) an obvious directory - that
1381 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1385 nd->flags &= ~LOOKUP_PARENT;
1386 down(&dir->d_inode->i_sem);
1387 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1390 error = PTR_ERR(dentry);
1391 if (IS_ERR(dentry)) {
1392 up(&dir->d_inode->i_sem);
1396 /* Negative dentry, just create the file */
1397 if (!dentry->d_inode) {
1398 if (!IS_POSIXACL(dir->d_inode))
1399 mode &= ~current->fs->umask;
1400 error = vfs_create(dir->d_inode, dentry, mode, nd);
1401 up(&dir->d_inode->i_sem);
1403 nd->dentry = dentry;
1406 /* Don't check for write permission, don't truncate */
1413 * It already exists.
1415 up(&dir->d_inode->i_sem);
1421 if (d_mountpoint(dentry)) {
1423 if (flag & O_NOFOLLOW)
1425 while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry));
1428 if (!dentry->d_inode)
1430 if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
1434 nd->dentry = dentry;
1436 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
1439 error = may_open(nd, acc_mode, flag);
1452 if (flag & O_NOFOLLOW)
1455 * This is subtle. Instead of calling do_follow_link() we do the
1456 * thing by hands. The reason is that this way we have zero link_count
1457 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1458 * After that we have the parent and last component, i.e.
1459 * we are in the same situation as after the first path_walk().
1460 * Well, almost - if the last component is normal we get its copy
1461 * stored in nd->last.name and we will have to putname() it when we
1462 * are done. Procfs-like symlinks just set LAST_BIND.
1464 nd->flags |= LOOKUP_PARENT;
1465 error = security_inode_follow_link(dentry, nd);
1468 touch_atime(nd->mnt, dentry);
1469 nd_set_link(nd, NULL);
1470 error = dentry->d_inode->i_op->follow_link(dentry, nd);
1472 char *s = nd_get_link(nd);
1474 error = __vfs_follow_link(nd, s);
1475 if (dentry->d_inode->i_op->put_link)
1476 dentry->d_inode->i_op->put_link(dentry, nd);
1481 nd->flags &= ~LOOKUP_PARENT;
1482 if (nd->last_type == LAST_BIND) {
1483 dentry = nd->dentry;
1487 if (nd->last_type != LAST_NORM)
1489 if (nd->last.name[nd->last.len]) {
1490 putname(nd->last.name);
1495 putname(nd->last.name);
1499 down(&dir->d_inode->i_sem);
1500 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1501 putname(nd->last.name);
1506 * lookup_create - lookup a dentry, creating it if it doesn't exist
1507 * @nd: nameidata info
1508 * @is_dir: directory flag
1510 * Simple function to lookup and return a dentry and create it
1511 * if it doesn't exist. Is SMP-safe.
1513 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1515 struct dentry *dentry;
1517 down(&nd->dentry->d_inode->i_sem);
1518 dentry = ERR_PTR(-EEXIST);
1519 if (nd->last_type != LAST_NORM)
1521 nd->flags &= ~LOOKUP_PARENT;
1522 dentry = lookup_hash(&nd->last, nd->dentry);
1525 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1530 dentry = ERR_PTR(-ENOENT);
1535 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1537 int error = may_create(dir, dentry, NULL);
1542 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1545 if (!dir->i_op || !dir->i_op->mknod)
1548 error = security_inode_mknod(dir, dentry, mode, dev);
1553 error = dir->i_op->mknod(dir, dentry, mode, dev);
1555 inode_dir_notify(dir, DN_CREATE);
1556 security_inode_post_mknod(dir, dentry, mode, dev);
1561 asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
1565 struct dentry * dentry;
1566 struct nameidata nd;
1570 tmp = getname(filename);
1572 return PTR_ERR(tmp);
1574 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1577 dentry = lookup_create(&nd, 0);
1578 error = PTR_ERR(dentry);
1580 if (!IS_POSIXACL(nd.dentry->d_inode))
1581 mode &= ~current->fs->umask;
1582 if (!IS_ERR(dentry)) {
1583 switch (mode & S_IFMT) {
1584 case 0: case S_IFREG:
1585 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1587 case S_IFCHR: case S_IFBLK:
1588 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1589 new_decode_dev(dev));
1591 case S_IFIFO: case S_IFSOCK:
1592 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1602 up(&nd.dentry->d_inode->i_sem);
1610 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1612 int error = may_create(dir, dentry, NULL);
1617 if (!dir->i_op || !dir->i_op->mkdir)
1620 mode &= (S_IRWXUGO|S_ISVTX);
1621 error = security_inode_mkdir(dir, dentry, mode);
1626 error = dir->i_op->mkdir(dir, dentry, mode);
1628 inode_dir_notify(dir, DN_CREATE);
1629 security_inode_post_mkdir(dir,dentry, mode);
1634 asmlinkage long sys_mkdir(const char __user * pathname, int mode)
1639 tmp = getname(pathname);
1640 error = PTR_ERR(tmp);
1642 struct dentry *dentry;
1643 struct nameidata nd;
1645 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1648 dentry = lookup_create(&nd, 1);
1649 error = PTR_ERR(dentry);
1650 if (!IS_ERR(dentry)) {
1651 if (!IS_POSIXACL(nd.dentry->d_inode))
1652 mode &= ~current->fs->umask;
1653 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1656 up(&nd.dentry->d_inode->i_sem);
1666 * We try to drop the dentry early: we should have
1667 * a usage count of 2 if we're the only user of this
1668 * dentry, and if that is true (possibly after pruning
1669 * the dcache), then we drop the dentry now.
1671 * A low-level filesystem can, if it choses, legally
1674 * if (!d_unhashed(dentry))
1677 * if it cannot handle the case of removing a directory
1678 * that is still in use by something else..
1680 static void d_unhash(struct dentry *dentry)
1683 spin_lock(&dcache_lock);
1684 switch (atomic_read(&dentry->d_count)) {
1686 spin_unlock(&dcache_lock);
1687 shrink_dcache_parent(dentry);
1688 spin_lock(&dcache_lock);
1689 if (atomic_read(&dentry->d_count) != 2)
1694 spin_unlock(&dcache_lock);
1697 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1699 int error = may_delete(dir, dentry, 1);
1704 if (!dir->i_op || !dir->i_op->rmdir)
1709 down(&dentry->d_inode->i_sem);
1711 if (d_mountpoint(dentry))
1714 error = security_inode_rmdir(dir, dentry);
1716 error = dir->i_op->rmdir(dir, dentry);
1718 dentry->d_inode->i_flags |= S_DEAD;
1721 up(&dentry->d_inode->i_sem);
1723 inode_dir_notify(dir, DN_DELETE);
1731 asmlinkage long sys_rmdir(const char __user * pathname)
1735 struct dentry *dentry;
1736 struct nameidata nd;
1738 name = getname(pathname);
1740 return PTR_ERR(name);
1742 error = path_lookup(name, LOOKUP_PARENT, &nd);
1746 switch(nd.last_type) {
1757 down(&nd.dentry->d_inode->i_sem);
1758 dentry = lookup_hash(&nd.last, nd.dentry);
1759 error = PTR_ERR(dentry);
1760 if (!IS_ERR(dentry)) {
1761 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1764 up(&nd.dentry->d_inode->i_sem);
1772 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1774 int error = may_delete(dir, dentry, 0);
1779 if (!dir->i_op || !dir->i_op->unlink)
1784 down(&dentry->d_inode->i_sem);
1785 if (d_mountpoint(dentry))
1788 error = security_inode_unlink(dir, dentry);
1790 error = dir->i_op->unlink(dir, dentry);
1792 up(&dentry->d_inode->i_sem);
1794 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1795 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1797 inode_dir_notify(dir, DN_DELETE);
1803 * Make sure that the actual truncation of the file will occur outside its
1804 * directory's i_sem. Truncate can take a long time if there is a lot of
1805 * writeout happening, and we don't want to prevent access to the directory
1806 * while waiting on the I/O.
1808 asmlinkage long sys_unlink(const char __user * pathname)
1812 struct dentry *dentry;
1813 struct nameidata nd;
1814 struct inode *inode = NULL;
1816 name = getname(pathname);
1818 return PTR_ERR(name);
1820 error = path_lookup(name, LOOKUP_PARENT, &nd);
1824 if (nd.last_type != LAST_NORM)
1826 down(&nd.dentry->d_inode->i_sem);
1827 dentry = lookup_hash(&nd.last, nd.dentry);
1828 error = PTR_ERR(dentry);
1829 if (!IS_ERR(dentry)) {
1830 /* Why not before? Because we want correct error value */
1831 if (nd.last.name[nd.last.len])
1833 inode = dentry->d_inode;
1835 atomic_inc(&inode->i_count);
1836 error = vfs_unlink(nd.dentry->d_inode, dentry);
1840 up(&nd.dentry->d_inode->i_sem);
1847 iput(inode); /* truncate the inode here */
1851 error = !dentry->d_inode ? -ENOENT :
1852 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1856 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
1858 int error = may_create(dir, dentry, NULL);
1863 if (!dir->i_op || !dir->i_op->symlink)
1866 error = security_inode_symlink(dir, dentry, oldname);
1871 error = dir->i_op->symlink(dir, dentry, oldname);
1873 inode_dir_notify(dir, DN_CREATE);
1874 security_inode_post_symlink(dir, dentry, oldname);
1879 asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
1885 from = getname(oldname);
1887 return PTR_ERR(from);
1888 to = getname(newname);
1889 error = PTR_ERR(to);
1891 struct dentry *dentry;
1892 struct nameidata nd;
1894 error = path_lookup(to, LOOKUP_PARENT, &nd);
1897 dentry = lookup_create(&nd, 0);
1898 error = PTR_ERR(dentry);
1899 if (!IS_ERR(dentry)) {
1900 error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
1903 up(&nd.dentry->d_inode->i_sem);
1912 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1914 struct inode *inode = old_dentry->d_inode;
1920 error = may_create(dir, new_dentry, NULL);
1924 if (dir->i_sb != inode->i_sb)
1928 * A link to an append-only or immutable file cannot be created.
1930 if (IS_APPEND(inode) || IS_IXORUNLINK(inode))
1932 if (!dir->i_op || !dir->i_op->link)
1934 if (S_ISDIR(old_dentry->d_inode->i_mode))
1937 error = security_inode_link(old_dentry, dir, new_dentry);
1941 down(&old_dentry->d_inode->i_sem);
1943 error = dir->i_op->link(old_dentry, dir, new_dentry);
1944 up(&old_dentry->d_inode->i_sem);
1946 inode_dir_notify(dir, DN_CREATE);
1947 security_inode_post_link(old_dentry, dir, new_dentry);
1953 * Hardlinks are often used in delicate situations. We avoid
1954 * security-related surprises by not following symlinks on the
1957 * We don't follow them on the oldname either to be compatible
1958 * with linux 2.0, and to avoid hard-linking to directories
1959 * and other special files. --ADM
1961 asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
1963 struct dentry *new_dentry;
1964 struct nameidata nd, old_nd;
1968 to = getname(newname);
1972 error = __user_walk(oldname, 0, &old_nd);
1975 error = path_lookup(to, LOOKUP_PARENT, &nd);
1979 if (old_nd.mnt != nd.mnt)
1981 new_dentry = lookup_create(&nd, 0);
1982 error = PTR_ERR(new_dentry);
1983 if (!IS_ERR(new_dentry)) {
1984 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
1987 up(&nd.dentry->d_inode->i_sem);
1991 path_release(&old_nd);
1999 * The worst of all namespace operations - renaming directory. "Perverted"
2000 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2002 * a) we can get into loop creation. Check is done in is_subdir().
2003 * b) race potential - two innocent renames can create a loop together.
2004 * That's where 4.4 screws up. Current fix: serialization on
2005 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
2007 * c) we have to lock _three_ objects - parents and victim (if it exists).
2008 * And that - after we got ->i_sem on parents (until then we don't know
2009 * whether the target exists). Solution: try to be smart with locking
2010 * order for inodes. We rely on the fact that tree topology may change
2011 * only under ->s_vfs_rename_sem _and_ that parent of the object we
2012 * move will be locked. Thus we can rank directories by the tree
2013 * (ancestors first) and rank all non-directories after them.
2014 * That works since everybody except rename does "lock parent, lookup,
2015 * lock child" and rename is under ->s_vfs_rename_sem.
2016 * HOWEVER, it relies on the assumption that any object with ->lookup()
2017 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2018 * we'd better make sure that there's no link(2) for them.
2019 * d) some filesystems don't support opened-but-unlinked directories,
2020 * either because of layout or because they are not ready to deal with
2021 * all cases correctly. The latter will be fixed (taking this sort of
2022 * stuff into VFS), but the former is not going away. Solution: the same
2023 * trick as in rmdir().
2024 * e) conversion from fhandle to dentry may come in the wrong moment - when
2025 * we are removing the target. Solution: we will have to grab ->i_sem
2026 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2027 * ->i_sem on parents, which works but leads to some truely excessive
2030 int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2031 struct inode *new_dir, struct dentry *new_dentry)
2034 struct inode *target;
2037 * If we are going to change the parent - check write permissions,
2038 * we'll need to flip '..'.
2040 if (new_dir != old_dir) {
2041 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2046 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2050 target = new_dentry->d_inode;
2052 down(&target->i_sem);
2053 d_unhash(new_dentry);
2055 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2058 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2061 target->i_flags |= S_DEAD;
2063 if (d_unhashed(new_dentry))
2064 d_rehash(new_dentry);
2068 d_move(old_dentry,new_dentry);
2069 security_inode_post_rename(old_dir, old_dentry,
2070 new_dir, new_dentry);
2075 int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2076 struct inode *new_dir, struct dentry *new_dentry)
2078 struct inode *target;
2081 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2086 target = new_dentry->d_inode;
2088 down(&target->i_sem);
2089 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2092 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2094 /* The following d_move() should become unconditional */
2095 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
2096 d_move(old_dentry, new_dentry);
2097 security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
2105 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2106 struct inode *new_dir, struct dentry *new_dentry)
2109 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2111 if (old_dentry->d_inode == new_dentry->d_inode)
2114 error = may_delete(old_dir, old_dentry, is_dir);
2118 if (!new_dentry->d_inode)
2119 error = may_create(new_dir, new_dentry, NULL);
2121 error = may_delete(new_dir, new_dentry, is_dir);
2125 if (!old_dir->i_op || !old_dir->i_op->rename)
2128 DQUOT_INIT(old_dir);
2129 DQUOT_INIT(new_dir);
2132 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2134 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2136 if (old_dir == new_dir)
2137 inode_dir_notify(old_dir, DN_RENAME);
2139 inode_dir_notify(old_dir, DN_DELETE);
2140 inode_dir_notify(new_dir, DN_CREATE);
2146 static inline int do_rename(const char * oldname, const char * newname)
2149 struct dentry * old_dir, * new_dir;
2150 struct dentry * old_dentry, *new_dentry;
2151 struct dentry * trap;
2152 struct nameidata oldnd, newnd;
2154 error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2158 error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2163 if (oldnd.mnt != newnd.mnt)
2166 old_dir = oldnd.dentry;
2168 if (oldnd.last_type != LAST_NORM)
2171 new_dir = newnd.dentry;
2172 if (newnd.last_type != LAST_NORM)
2175 trap = lock_rename(new_dir, old_dir);
2177 old_dentry = lookup_hash(&oldnd.last, old_dir);
2178 error = PTR_ERR(old_dentry);
2179 if (IS_ERR(old_dentry))
2181 /* source must exist */
2183 if (!old_dentry->d_inode)
2185 /* unless the source is a directory trailing slashes give -ENOTDIR */
2186 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2188 if (oldnd.last.name[oldnd.last.len])
2190 if (newnd.last.name[newnd.last.len])
2193 /* source should not be ancestor of target */
2195 if (old_dentry == trap)
2197 new_dentry = lookup_hash(&newnd.last, new_dir);
2198 error = PTR_ERR(new_dentry);
2199 if (IS_ERR(new_dentry))
2201 /* target should not be an ancestor of source */
2203 if (new_dentry == trap)
2206 error = vfs_rename(old_dir->d_inode, old_dentry,
2207 new_dir->d_inode, new_dentry);
2213 unlock_rename(new_dir, old_dir);
2215 path_release(&newnd);
2217 path_release(&oldnd);
2222 asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
2228 from = getname(oldname);
2230 return PTR_ERR(from);
2231 to = getname(newname);
2232 error = PTR_ERR(to);
2234 error = do_rename(from,to);
2241 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2245 len = PTR_ERR(link);
2250 if (len > (unsigned) buflen)
2252 if (copy_to_user(buffer, link, len))
2259 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2260 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2261 * using) it for any given inode is up to filesystem.
2263 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2265 struct nameidata nd;
2268 res = dentry->d_inode->i_op->follow_link(dentry, &nd);
2270 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2271 if (dentry->d_inode->i_op->put_link)
2272 dentry->d_inode->i_op->put_link(dentry, &nd);
2277 int vfs_follow_link(struct nameidata *nd, const char *link)
2279 return __vfs_follow_link(nd, link);
2282 /* get the link contents into pagecache */
2283 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2286 struct address_space *mapping = dentry->d_inode->i_mapping;
2287 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2291 wait_on_page_locked(page);
2292 if (!PageUptodate(page))
2298 page_cache_release(page);
2299 return ERR_PTR(-EIO);
2305 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2307 struct page *page = NULL;
2308 char *s = page_getlink(dentry, &page);
2309 int res = vfs_readlink(dentry,buffer,buflen,s);
2312 page_cache_release(page);
2317 int page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2320 char *s = page_getlink(dentry, &page);
2328 void page_put_link(struct dentry *dentry, struct nameidata *nd)
2330 if (!IS_ERR(nd_get_link(nd))) {
2332 page = find_get_page(dentry->d_inode->i_mapping, 0);
2336 page_cache_release(page);
2337 page_cache_release(page);
2341 int page_follow_link(struct dentry *dentry, struct nameidata *nd)
2343 struct page *page = NULL;
2344 char *s = page_getlink(dentry, &page);
2345 int res = __vfs_follow_link(nd, s);
2348 page_cache_release(page);
2353 int page_symlink(struct inode *inode, const char *symname, int len)
2355 struct address_space *mapping = inode->i_mapping;
2356 struct page *page = grab_cache_page(mapping, 0);
2362 err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2365 kaddr = kmap_atomic(page, KM_USER0);
2366 memcpy(kaddr, symname, len-1);
2367 kunmap_atomic(kaddr, KM_USER0);
2368 mapping->a_ops->commit_write(NULL, page, 0, len-1);
2370 * Notice that we are _not_ going to block here - end of page is
2371 * unmapped, so this will only try to map the rest of page, see
2372 * that it is unmapped (typically even will not look into inode -
2373 * ->i_size will be enough for everything) and zero it out.
2374 * OTOH it's obviously correct and should make the page up-to-date.
2376 if (!PageUptodate(page)) {
2377 err = mapping->a_ops->readpage(NULL, page);
2378 wait_on_page_locked(page);
2382 page_cache_release(page);
2385 mark_inode_dirty(inode);
2389 page_cache_release(page);
2394 struct inode_operations page_symlink_inode_operations = {
2395 .readlink = generic_readlink,
2396 .follow_link = page_follow_link_light,
2397 .put_link = page_put_link,
2400 EXPORT_SYMBOL(__user_walk);
2401 EXPORT_SYMBOL(follow_down);
2402 EXPORT_SYMBOL(follow_up);
2403 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2404 EXPORT_SYMBOL(getname);
2405 EXPORT_SYMBOL(lock_rename);
2406 EXPORT_SYMBOL(lookup_create);
2407 EXPORT_SYMBOL(lookup_hash);
2408 EXPORT_SYMBOL(lookup_one_len);
2409 EXPORT_SYMBOL(page_follow_link);
2410 EXPORT_SYMBOL(page_follow_link_light);
2411 EXPORT_SYMBOL(page_put_link);
2412 EXPORT_SYMBOL(page_readlink);
2413 EXPORT_SYMBOL(page_symlink);
2414 EXPORT_SYMBOL(page_symlink_inode_operations);
2415 EXPORT_SYMBOL(path_lookup);
2416 EXPORT_SYMBOL(path_release);
2417 EXPORT_SYMBOL(path_walk);
2418 EXPORT_SYMBOL(permission);
2419 EXPORT_SYMBOL(unlock_rename);
2420 EXPORT_SYMBOL(vfs_create);
2421 EXPORT_SYMBOL(vfs_follow_link);
2422 EXPORT_SYMBOL(vfs_link);
2423 EXPORT_SYMBOL(vfs_mkdir);
2424 EXPORT_SYMBOL(vfs_mknod);
2425 EXPORT_SYMBOL(vfs_permission);
2426 EXPORT_SYMBOL(vfs_readlink);
2427 EXPORT_SYMBOL(vfs_rename);
2428 EXPORT_SYMBOL(vfs_rmdir);
2429 EXPORT_SYMBOL(vfs_symlink);
2430 EXPORT_SYMBOL(vfs_unlink);
2431 EXPORT_SYMBOL(generic_readlink);