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/syscalls.h>
29 #include <linux/mount.h>
30 #include <linux/audit.h>
31 #include <linux/proc_fs.h>
32 #include <linux/vserver/inode.h>
33 #include <linux/vserver/debug.h>
35 #include <asm/namei.h>
36 #include <asm/uaccess.h>
38 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
40 /* [Feb-1997 T. Schoebel-Theuer]
41 * Fundamental changes in the pathname lookup mechanisms (namei)
42 * were necessary because of omirr. The reason is that omirr needs
43 * to know the _real_ pathname, not the user-supplied one, in case
44 * of symlinks (and also when transname replacements occur).
46 * The new code replaces the old recursive symlink resolution with
47 * an iterative one (in case of non-nested symlink chains). It does
48 * this with calls to <fs>_follow_link().
49 * As a side effect, dir_namei(), _namei() and follow_link() are now
50 * replaced with a single function lookup_dentry() that can handle all
51 * the special cases of the former code.
53 * With the new dcache, the pathname is stored at each inode, at least as
54 * long as the refcount of the inode is positive. As a side effect, the
55 * size of the dcache depends on the inode cache and thus is dynamic.
57 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
58 * resolution to correspond with current state of the code.
60 * Note that the symlink resolution is not *completely* iterative.
61 * There is still a significant amount of tail- and mid- recursion in
62 * the algorithm. Also, note that <fs>_readlink() is not used in
63 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
64 * may return different results than <fs>_follow_link(). Many virtual
65 * filesystems (including /proc) exhibit this behavior.
68 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
69 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
70 * and the name already exists in form of a symlink, try to create the new
71 * name indicated by the symlink. The old code always complained that the
72 * name already exists, due to not following the symlink even if its target
73 * is nonexistent. The new semantics affects also mknod() and link() when
74 * the name is a symlink pointing to a non-existant name.
76 * I don't know which semantics is the right one, since I have no access
77 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
78 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
79 * "old" one. Personally, I think the new semantics is much more logical.
80 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
81 * file does succeed in both HP-UX and SunOs, but not in Solaris
82 * and in the old Linux semantics.
85 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
86 * semantics. See the comments in "open_namei" and "do_link" below.
88 * [10-Sep-98 Alan Modra] Another symlink change.
91 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
92 * inside the path - always follow.
93 * in the last component in creation/removal/renaming - never follow.
94 * if LOOKUP_FOLLOW passed - follow.
95 * if the pathname has trailing slashes - follow.
96 * otherwise - don't follow.
97 * (applied in that order).
99 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
100 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
101 * During the 2.4 we need to fix the userland stuff depending on it -
102 * hopefully we will be able to get rid of that wart in 2.5. So far only
103 * XEmacs seems to be relying on it...
106 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
107 * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives
108 * any extra contention...
111 /* In order to reduce some races, while at the same time doing additional
112 * checking and hopefully speeding things up, we copy filenames to the
113 * kernel data space before using them..
115 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
116 * PATH_MAX includes the nul terminator --RR.
118 static inline int do_getname(const char __user *filename, char *page)
121 unsigned long len = PATH_MAX;
123 if ((unsigned long) filename >= TASK_SIZE) {
124 if (!segment_eq(get_fs(), KERNEL_DS))
126 } else if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
127 len = TASK_SIZE - (unsigned long) filename;
129 retval = strncpy_from_user((char *)page, filename, len);
133 return -ENAMETOOLONG;
139 char * getname(const char __user * filename)
143 result = ERR_PTR(-ENOMEM);
146 int retval = do_getname(filename, tmp);
151 result = ERR_PTR(retval);
154 if (unlikely(current->audit_context) && !IS_ERR(result) && result)
155 audit_getname(result);
160 * generic_permission - check for access rights on a Posix-like filesystem
161 * @inode: inode to check access rights for
162 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
163 * @check_acl: optional callback to check for Posix ACLs
165 * Used to check for read/write/execute permissions on a file.
166 * We use "fsuid" for this, letting us set arbitrary permissions
167 * for filesystem access without changing the "normal" uids which
168 * are used for other things..
170 int generic_permission(struct inode *inode, int mask,
171 int (*check_acl)(struct inode *inode, int mask))
173 umode_t mode = inode->i_mode;
175 if (mask & MAY_WRITE) {
177 * Nobody gets write access to a read-only fs.
179 if (IS_RDONLY(inode) &&
180 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
184 * Nobody gets write access to an immutable file.
186 if (IS_IMMUTABLE(inode))
190 if (current->fsuid == inode->i_uid)
193 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
194 int error = check_acl(inode, mask);
195 if (error == -EACCES)
196 goto check_capabilities;
197 else if (error != -EAGAIN)
201 if (in_group_p(inode->i_gid))
206 * If the DACs are ok we don't need any capability check.
208 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
213 * Read/write DACs are always overridable.
214 * Executable DACs are overridable if at least one exec bit is set.
216 if (!(mask & MAY_EXEC) ||
217 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
218 if (capable(CAP_DAC_OVERRIDE))
222 * Searching includes executable on directories, else just read.
224 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
225 if (capable(CAP_DAC_READ_SEARCH))
231 static inline int xid_permission(struct inode *inode, int mask, struct nameidata *nd)
233 if (inode->i_xid == 0)
236 #ifdef CONFIG_VSERVER_FILESHARING
237 /* MEF: PlanetLab FS module assumes that any file that can be
238 * named (e.g., via a cross mount) is not hidden from another
239 * context or the admin context.
241 if (vx_check(inode->i_xid,VX_STATIC|VX_DYNAMIC))
244 if (vx_check(inode->i_xid,VX_ADMIN|VX_WATCH|VX_IDENT))
247 vxwprintk(1, "xid=%d denied access to %p[#%d,%lu] »%s«.",
248 vx_current_xid(), inode, inode->i_xid, inode->i_ino,
249 vxd_path(nd->dentry, nd->mnt));
253 int permission(struct inode * inode,int mask, struct nameidata *nd)
257 umode_t mode = inode->i_mode;
259 /* Prevent vservers from escaping chroot() barriers */
260 if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN))
263 /* Ordinary permission routines do not understand MAY_APPEND. */
264 submask = mask & ~MAY_APPEND;
266 if (nd && (mask & MAY_WRITE) && MNT_IS_RDONLY(nd->mnt) &&
267 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
270 if ((retval = xid_permission(inode, mask, nd)))
273 if (inode->i_op && inode->i_op->permission)
274 retval = inode->i_op->permission(inode, submask, nd);
276 retval = generic_permission(inode, submask, NULL);
280 return security_inode_permission(inode, mask, nd);
284 * get_write_access() gets write permission for a file.
285 * put_write_access() releases this write permission.
286 * This is used for regular files.
287 * We cannot support write (and maybe mmap read-write shared) accesses and
288 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
289 * can have the following values:
290 * 0: no writers, no VM_DENYWRITE mappings
291 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
292 * > 0: (i_writecount) users are writing to the file.
294 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
295 * except for the cases where we don't hold i_writecount yet. Then we need to
296 * use {get,deny}_write_access() - these functions check the sign and refuse
297 * to do the change if sign is wrong. Exclusion between them is provided by
298 * the inode->i_lock spinlock.
301 int get_write_access(struct inode * inode)
303 spin_lock(&inode->i_lock);
304 if (atomic_read(&inode->i_writecount) < 0) {
305 spin_unlock(&inode->i_lock);
308 atomic_inc(&inode->i_writecount);
309 spin_unlock(&inode->i_lock);
314 int deny_write_access(struct file * file)
316 struct inode *inode = file->f_dentry->d_inode;
318 spin_lock(&inode->i_lock);
319 if (atomic_read(&inode->i_writecount) > 0) {
320 spin_unlock(&inode->i_lock);
323 atomic_dec(&inode->i_writecount);
324 spin_unlock(&inode->i_lock);
329 void path_release(struct nameidata *nd)
336 * umount() mustn't call path_release()/mntput() as that would clear
339 void path_release_on_umount(struct nameidata *nd)
346 * Internal lookup() using the new generic dcache.
349 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
351 struct dentry * dentry = __d_lookup(parent, name);
353 /* lockess __d_lookup may fail due to concurrent d_move()
354 * in some unrelated directory, so try with d_lookup
357 dentry = d_lookup(parent, name);
359 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
360 if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
369 * Short-cut version of permission(), for calling by
370 * path_walk(), when dcache lock is held. Combines parts
371 * of permission() and generic_permission(), and tests ONLY for
372 * MAY_EXEC permission.
374 * If appropriate, check DAC only. If not appropriate, or
375 * short-cut DAC fails, then call permission() to do more
376 * complete permission check.
378 static inline int exec_permission_lite(struct inode *inode,
379 struct nameidata *nd)
381 umode_t mode = inode->i_mode;
383 if (inode->i_op && inode->i_op->permission)
386 if (current->fsuid == inode->i_uid)
388 else if (in_group_p(inode->i_gid))
394 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
397 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
400 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
405 return security_inode_permission(inode, MAY_EXEC, nd);
409 * This is called when everything else fails, and we actually have
410 * to go to the low-level filesystem to find out what we should do..
412 * We get the directory semaphore, and after getting that we also
413 * make sure that nobody added the entry to the dcache in the meantime..
416 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
418 struct dentry * result;
419 struct inode *dir = parent->d_inode;
423 * First re-do the cached lookup just in case it was created
424 * while we waited for the directory semaphore..
426 * FIXME! This could use version numbering or similar to
427 * avoid unnecessary cache lookups.
429 * The "dcache_lock" is purely to protect the RCU list walker
430 * from concurrent renames at this point (we mustn't get false
431 * negatives from the RCU list walk here, unlike the optimistic
434 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
436 result = d_lookup(parent, name);
438 struct dentry * dentry = d_alloc(parent, name);
439 result = ERR_PTR(-ENOMEM);
441 result = dir->i_op->lookup(dir, dentry, nd);
452 * Uhhuh! Nasty case: the cache was re-populated while
453 * we waited on the semaphore. Need to revalidate.
456 if (result->d_op && result->d_op->d_revalidate) {
457 if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
459 result = ERR_PTR(-ENOENT);
465 static int __emul_lookup_dentry(const char *, struct nameidata *);
469 walk_init_root(const char *name, struct nameidata *nd)
471 read_lock(¤t->fs->lock);
472 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
473 nd->mnt = mntget(current->fs->altrootmnt);
474 nd->dentry = dget(current->fs->altroot);
475 read_unlock(¤t->fs->lock);
476 if (__emul_lookup_dentry(name,nd))
478 read_lock(¤t->fs->lock);
480 nd->mnt = mntget(current->fs->rootmnt);
481 nd->dentry = dget(current->fs->root);
482 read_unlock(¤t->fs->lock);
486 static inline int __vfs_follow_link(struct nameidata *nd, const char *link)
495 if (!walk_init_root(link, nd))
496 /* weird __emul_prefix() stuff did it */
499 res = link_path_walk(link, nd);
501 if (nd->depth || res || nd->last_type!=LAST_NORM)
504 * If it is an iterative symlinks resolution in open_namei() we
505 * have to copy the last component. And all that crap because of
506 * bloody create() on broken symlinks. Furrfu...
509 if (unlikely(!name)) {
513 strcpy(name, nd->last.name);
514 nd->last.name = name;
518 return PTR_ERR(link);
522 * This limits recursive symlink follows to 8, while
523 * limiting consecutive symlinks to 40.
525 * Without that kind of total limit, nasty chains of consecutive
526 * symlinks can cause almost arbitrarily long lookups.
528 static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd)
531 if (current->link_count >= MAX_NESTED_LINKS)
533 if (current->total_link_count >= 40)
535 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
537 err = security_inode_follow_link(dentry, nd);
540 current->link_count++;
541 current->total_link_count++;
543 touch_atime(nd->mnt, dentry);
544 nd_set_link(nd, NULL);
545 err = dentry->d_inode->i_op->follow_link(dentry, nd);
547 char *s = nd_get_link(nd);
549 err = __vfs_follow_link(nd, s);
550 if (dentry->d_inode->i_op->put_link)
551 dentry->d_inode->i_op->put_link(dentry, nd);
553 current->link_count--;
561 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
563 struct vfsmount *parent;
564 struct dentry *mountpoint;
565 spin_lock(&vfsmount_lock);
566 parent=(*mnt)->mnt_parent;
567 if (parent == *mnt) {
568 spin_unlock(&vfsmount_lock);
572 mountpoint=dget((*mnt)->mnt_mountpoint);
573 spin_unlock(&vfsmount_lock);
575 *dentry = mountpoint;
581 /* no need for dcache_lock, as serialization is taken care in
584 static int follow_mount(struct vfsmount **mnt, struct dentry **dentry)
587 while (d_mountpoint(*dentry)) {
588 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
594 *dentry = dget(mounted->mnt_root);
600 /* no need for dcache_lock, as serialization is taken care in
603 static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
605 struct vfsmount *mounted;
607 mounted = lookup_mnt(*mnt, *dentry);
612 *dentry = dget(mounted->mnt_root);
618 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
620 return __follow_down(mnt,dentry);
623 static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry)
626 struct vfsmount *parent;
627 struct dentry *old = *dentry;
629 read_lock(¤t->fs->lock);
630 if (*dentry == current->fs->root &&
631 *mnt == current->fs->rootmnt) {
632 read_unlock(¤t->fs->lock);
635 read_unlock(¤t->fs->lock);
636 spin_lock(&dcache_lock);
637 if (*dentry != (*mnt)->mnt_root) {
638 *dentry = dget((*dentry)->d_parent);
639 spin_unlock(&dcache_lock);
643 spin_unlock(&dcache_lock);
644 spin_lock(&vfsmount_lock);
645 parent = (*mnt)->mnt_parent;
646 if (parent == *mnt) {
647 spin_unlock(&vfsmount_lock);
651 *dentry = dget((*mnt)->mnt_mountpoint);
652 spin_unlock(&vfsmount_lock);
657 follow_mount(mnt, dentry);
661 struct vfsmount *mnt;
662 struct dentry *dentry;
666 * It's more convoluted than I'd like it to be, but... it's still fairly
667 * small and for now I'd prefer to have fast path as straight as possible.
668 * It _is_ time-critical.
670 static int do_lookup(struct nameidata *nd, struct qstr *name,
671 struct path *path, int atomic)
673 struct vfsmount *mnt = nd->mnt;
674 struct dentry *dentry = __d_lookup(nd->dentry, name);
679 if (dentry->d_op && dentry->d_op->d_revalidate)
680 goto need_revalidate;
681 inode = dentry->d_inode;
684 if (inode->i_sb->s_magic == PROC_SUPER_MAGIC) {
685 struct proc_dir_entry *de = PDE(inode);
687 if (de && !vx_hide_check(0, de->vx_flags))
690 #ifdef CONFIG_VSERVER_FILESHARING
691 /* MEF: PlanetLab FS module assumes that any file that can be
692 * named (e.g., via a cross mount) is not hidden from another
693 * context or the admin context.
695 if (vx_check(inode->i_xid,VX_STATIC|VX_DYNAMIC|VX_ADMIN)) {
698 else /* do the following check */
700 if (!vx_check(inode->i_xid,
707 path->dentry = dentry;
710 vxwprintk(1, "xid=%d did lookup hidden %p[#%d,%lu] »%s«.",
711 vx_current_xid(), inode, inode->i_xid, inode->i_ino,
712 vxd_path(dentry, mnt));
718 return -EWOULDBLOCKIO;
719 dentry = real_lookup(nd->dentry, name, nd);
726 return -EWOULDBLOCKIO;
727 if (dentry->d_op->d_revalidate(dentry, nd))
729 if (d_invalidate(dentry))
735 return PTR_ERR(dentry);
741 * This is the basic name resolution function, turning a pathname
742 * into the final dentry.
744 * We expect 'base' to be positive and a directory.
746 int fastcall link_path_walk(const char * name, struct nameidata *nd)
751 unsigned int lookup_flags = nd->flags;
753 atomic = (lookup_flags & LOOKUP_ATOMIC);
760 inode = nd->dentry->d_inode;
762 lookup_flags = LOOKUP_FOLLOW;
764 /* At this point we know we have a real path component. */
770 err = exec_permission_lite(inode, nd);
771 if (err == -EAGAIN) {
772 err = permission(inode, MAY_EXEC, nd);
778 c = *(const unsigned char *)name;
780 hash = init_name_hash();
783 hash = partial_name_hash(c, hash);
784 c = *(const unsigned char *)name;
785 } while (c && (c != '/'));
786 this.len = name - (const char *) this.name;
787 this.hash = end_name_hash(hash);
789 /* remove trailing slashes? */
792 while (*++name == '/');
794 goto last_with_slashes;
797 * "." and ".." are special - ".." especially so because it has
798 * to be able to know about the current root directory and
799 * parent relationships.
801 if (this.name[0] == '.') switch (this.len) {
805 if (this.name[1] != '.')
807 follow_dotdot(&nd->mnt, &nd->dentry);
808 inode = nd->dentry->d_inode;
814 * See if the low-level filesystem might want
815 * to use its own hash..
817 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
818 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
822 nd->flags |= LOOKUP_CONTINUE;
823 /* This does the actual lookups.. */
824 err = do_lookup(nd, &this, &next, atomic);
827 /* Check mountpoints.. */
828 follow_mount(&next.mnt, &next.dentry);
831 inode = next.dentry->d_inode;
838 if (inode->i_op->follow_link) {
840 err = do_follow_link(next.dentry, nd);
846 inode = nd->dentry->d_inode;
855 nd->dentry = next.dentry;
858 if (!inode->i_op->lookup)
861 /* here ends the main loop */
864 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
866 nd->flags &= ~LOOKUP_CONTINUE;
867 if (lookup_flags & LOOKUP_PARENT)
869 if (this.name[0] == '.') switch (this.len) {
873 if (this.name[1] != '.')
875 follow_dotdot(&nd->mnt, &nd->dentry);
876 inode = nd->dentry->d_inode;
881 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
882 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
886 err = do_lookup(nd, &this, &next, atomic);
889 follow_mount(&next.mnt, &next.dentry);
890 inode = next.dentry->d_inode;
891 if ((lookup_flags & LOOKUP_FOLLOW)
892 && inode && inode->i_op && inode->i_op->follow_link) {
894 err = do_follow_link(next.dentry, nd);
899 inode = nd->dentry->d_inode;
903 nd->dentry = next.dentry;
908 if (lookup_flags & LOOKUP_DIRECTORY) {
910 if (!inode->i_op || !inode->i_op->lookup)
916 nd->last_type = LAST_NORM;
917 if (this.name[0] != '.')
920 nd->last_type = LAST_DOT;
921 else if (this.len == 2 && this.name[1] == '.')
922 nd->last_type = LAST_DOTDOT;
927 * We bypassed the ordinary revalidation routines.
928 * We may need to check the cached dentry for staleness.
930 if (nd->dentry && nd->dentry->d_sb &&
931 (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
933 /* Note: we do not d_invalidate() */
934 if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
948 int fastcall path_walk(const char * name, struct nameidata *nd)
950 current->total_link_count = 0;
951 return link_path_walk(name, nd);
955 /* returns 1 if everything is done */
956 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
958 if (path_walk(name, nd))
959 return 0; /* something went wrong... */
961 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
962 struct dentry *old_dentry = nd->dentry;
963 struct vfsmount *old_mnt = nd->mnt;
964 struct qstr last = nd->last;
965 int last_type = nd->last_type;
967 * NAME was not found in alternate root or it's a directory. Try to find
968 * it in the normal root:
970 nd->last_type = LAST_ROOT;
971 read_lock(¤t->fs->lock);
972 nd->mnt = mntget(current->fs->rootmnt);
973 nd->dentry = dget(current->fs->root);
974 read_unlock(¤t->fs->lock);
975 if (path_walk(name, nd) == 0) {
976 if (nd->dentry->d_inode) {
983 nd->dentry = old_dentry;
986 nd->last_type = last_type;
991 void set_fs_altroot(void)
993 char *emul = __emul_prefix();
995 struct vfsmount *mnt = NULL, *oldmnt;
996 struct dentry *dentry = NULL, *olddentry;
1001 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
1007 write_lock(¤t->fs->lock);
1008 oldmnt = current->fs->altrootmnt;
1009 olddentry = current->fs->altroot;
1010 current->fs->altrootmnt = mnt;
1011 current->fs->altroot = dentry;
1012 write_unlock(¤t->fs->lock);
1019 int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
1023 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1027 read_lock(¤t->fs->lock);
1029 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
1030 nd->mnt = mntget(current->fs->altrootmnt);
1031 nd->dentry = dget(current->fs->altroot);
1032 read_unlock(¤t->fs->lock);
1033 if (__emul_lookup_dentry(name,nd))
1035 read_lock(¤t->fs->lock);
1037 nd->mnt = mntget(current->fs->rootmnt);
1038 nd->dentry = dget(current->fs->root);
1040 nd->mnt = mntget(current->fs->pwdmnt);
1041 nd->dentry = dget(current->fs->pwd);
1043 read_unlock(¤t->fs->lock);
1044 current->total_link_count = 0;
1045 retval = link_path_walk(name, nd);
1046 if (unlikely(current->audit_context
1047 && nd && nd->dentry && nd->dentry->d_inode))
1049 nd->dentry->d_inode->i_ino,
1050 nd->dentry->d_inode->i_rdev);
1055 * Restricted form of lookup. Doesn't follow links, single-component only,
1056 * needs parent already locked. Doesn't follow mounts.
1059 static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
1061 struct dentry * dentry;
1062 struct inode *inode;
1065 inode = base->d_inode;
1066 err = permission(inode, MAY_EXEC, nd);
1067 dentry = ERR_PTR(err);
1072 * See if the low-level filesystem might want
1073 * to use its own hash..
1075 if (base->d_op && base->d_op->d_hash) {
1076 err = base->d_op->d_hash(base, name);
1077 dentry = ERR_PTR(err);
1082 dentry = cached_lookup(base, name, nd);
1084 struct dentry *new = d_alloc(base, name);
1085 dentry = ERR_PTR(-ENOMEM);
1088 dentry = inode->i_op->lookup(inode, new, nd);
1098 struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
1100 return __lookup_hash(name, base, NULL);
1104 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
1115 hash = init_name_hash();
1117 c = *(const unsigned char *)name++;
1118 if (c == '/' || c == '\0')
1120 hash = partial_name_hash(c, hash);
1122 this.hash = end_name_hash(hash);
1124 return lookup_hash(&this, base);
1126 return ERR_PTR(-EACCES);
1132 * is used by most simple commands to get the inode of a specified name.
1133 * Open, link etc use their own routines, but this is enough for things
1136 * namei exists in two versions: namei/lnamei. The only difference is
1137 * that namei follows links, while lnamei does not.
1140 int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1142 char *tmp = getname(name);
1143 int err = PTR_ERR(tmp);
1146 err = path_lookup(tmp, flags, nd);
1153 * It's inline, so penalty for filesystems that don't use sticky bit is
1156 static inline int check_sticky(struct inode *dir, struct inode *inode)
1158 if (!(dir->i_mode & S_ISVTX))
1160 if (inode->i_uid == current->fsuid)
1162 if (dir->i_uid == current->fsuid)
1164 return !capable(CAP_FOWNER);
1168 * Check whether we can remove a link victim from directory dir, check
1169 * whether the type of victim is right.
1170 * 1. We can't do it if dir is read-only (done in permission())
1171 * 2. We should have write and exec permissions on dir
1172 * 3. We can't remove anything from append-only dir
1173 * 4. We can't do anything with immutable dir (done in permission())
1174 * 5. If the sticky bit on dir is set we should either
1175 * a. be owner of dir, or
1176 * b. be owner of victim, or
1177 * c. have CAP_FOWNER capability
1178 * 6. If the victim is append-only or immutable we can't do antyhing with
1179 * links pointing to it.
1180 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1181 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1182 * 9. We can't remove a root or mountpoint.
1183 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1184 * nfs_async_unlink().
1186 static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1190 if (!victim->d_inode)
1193 BUG_ON(victim->d_parent->d_inode != dir);
1195 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1200 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1201 IS_IXORUNLINK(victim->d_inode))
1204 if (!S_ISDIR(victim->d_inode->i_mode))
1206 if (IS_ROOT(victim))
1208 } else if (S_ISDIR(victim->d_inode->i_mode))
1210 if (IS_DEADDIR(dir))
1212 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1217 /* Check whether we can create an object with dentry child in directory
1219 * 1. We can't do it if child already exists (open has special treatment for
1220 * this case, but since we are inlined it's OK)
1221 * 2. We can't do it if dir is read-only (done in permission())
1222 * 3. We should have write and exec permissions on dir
1223 * 4. We can't do it if dir is immutable (done in permission())
1225 static inline int may_create(struct inode *dir, struct dentry *child,
1226 struct nameidata *nd)
1230 if (IS_DEADDIR(dir))
1232 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1235 static inline int mnt_may_create(struct vfsmount *mnt, struct inode *dir, struct dentry *child) {
1238 if (IS_DEADDIR(dir))
1240 if (mnt->mnt_flags & MNT_RDONLY)
1245 static inline int mnt_may_unlink(struct vfsmount *mnt, struct inode *dir, struct dentry *child) {
1246 if (!child->d_inode)
1248 if (mnt->mnt_flags & MNT_RDONLY)
1254 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1257 * O_DIRECTORY translates into forcing a directory lookup.
1259 static inline int lookup_flags(unsigned int f)
1261 unsigned long retval = LOOKUP_FOLLOW;
1264 retval &= ~LOOKUP_FOLLOW;
1266 if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1267 retval &= ~LOOKUP_FOLLOW;
1269 if (f & O_DIRECTORY)
1270 retval |= LOOKUP_DIRECTORY;
1271 if (f & O_ATOMICLOOKUP)
1272 retval |= LOOKUP_ATOMIC;
1278 * p1 and p2 should be directories on the same fs.
1280 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1285 down(&p1->d_inode->i_sem);
1289 down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1291 for (p = p1; p->d_parent != p; p = p->d_parent) {
1292 if (p->d_parent == p2) {
1293 down(&p2->d_inode->i_sem);
1294 down(&p1->d_inode->i_sem);
1299 for (p = p2; p->d_parent != p; p = p->d_parent) {
1300 if (p->d_parent == p1) {
1301 down(&p1->d_inode->i_sem);
1302 down(&p2->d_inode->i_sem);
1307 down(&p1->d_inode->i_sem);
1308 down(&p2->d_inode->i_sem);
1312 void unlock_rename(struct dentry *p1, struct dentry *p2)
1314 up(&p1->d_inode->i_sem);
1316 up(&p2->d_inode->i_sem);
1317 up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1321 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1322 struct nameidata *nd)
1324 int error = may_create(dir, dentry, nd);
1329 if (!dir->i_op || !dir->i_op->create)
1330 return -EACCES; /* shouldn't it be ENOSYS? */
1333 error = security_inode_create(dir, dentry, mode);
1337 error = dir->i_op->create(dir, dentry, mode, nd);
1339 inode_dir_notify(dir, DN_CREATE);
1340 security_inode_post_create(dir, dentry, mode);
1345 int may_open(struct nameidata *nd, int acc_mode, int flag)
1347 struct dentry *dentry = nd->dentry;
1348 struct inode *inode = dentry->d_inode;
1354 if (S_ISLNK(inode->i_mode))
1357 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1360 error = permission(inode, acc_mode, nd);
1365 * FIFO's, sockets and device files are special: they don't
1366 * actually live on the filesystem itself, and as such you
1367 * can write to them even if the filesystem is read-only.
1369 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1371 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1372 if (nd->mnt->mnt_flags & MNT_NODEV)
1376 } else if ((IS_RDONLY(inode) || (nd && MNT_IS_RDONLY(nd->mnt)))
1377 && (flag & FMODE_WRITE))
1380 * An append-only file must be opened in append mode for writing.
1382 if (IS_APPEND(inode)) {
1383 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1389 /* O_NOATIME can only be set by the owner or superuser */
1390 if (flag & O_NOATIME)
1391 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1395 * Ensure there are no outstanding leases on the file.
1397 error = break_lease(inode, flag);
1401 if (flag & O_TRUNC) {
1402 error = get_write_access(inode);
1407 * Refuse to truncate files with mandatory locks held on them.
1409 error = locks_verify_locked(inode);
1413 error = do_truncate(dentry, 0);
1415 put_write_access(inode);
1419 if (flag & FMODE_WRITE)
1428 * namei for open - this is in fact almost the whole open-routine.
1430 * Note that the low bits of "flag" aren't the same as in the open
1431 * system call - they are 00 - no permissions needed
1432 * 01 - read permission needed
1433 * 10 - write permission needed
1434 * 11 - read/write permissions needed
1435 * which is a lot more logical, and also allows the "no perm" needed
1436 * for symlinks (where the permissions are checked later).
1439 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1441 int acc_mode, error = 0;
1442 struct dentry *dentry;
1446 acc_mode = ACC_MODE(flag);
1448 /* Allow the LSM permission hook to distinguish append
1449 access from general write access. */
1450 if (flag & O_APPEND)
1451 acc_mode |= MAY_APPEND;
1453 /* Fill in the open() intent data */
1454 nd->intent.open.flags = flag;
1455 nd->intent.open.create_mode = mode;
1458 * The simplest case - just a plain lookup.
1460 if (!(flag & O_CREAT)) {
1461 error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd);
1468 * Create - we need to know the parent.
1470 error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd);
1475 * We have the parent and last component. First of all, check
1476 * that we are not asked to creat(2) an obvious directory - that
1480 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1484 nd->flags &= ~LOOKUP_PARENT;
1485 down(&dir->d_inode->i_sem);
1486 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1489 error = PTR_ERR(dentry);
1490 if (IS_ERR(dentry)) {
1491 up(&dir->d_inode->i_sem);
1495 /* Negative dentry, just create the file */
1496 if (!dentry->d_inode) {
1497 if (!IS_POSIXACL(dir->d_inode))
1498 mode &= ~current->fs->umask;
1499 error = vfs_create(dir->d_inode, dentry, mode, nd);
1500 up(&dir->d_inode->i_sem);
1502 nd->dentry = dentry;
1505 /* Don't check for write permission, don't truncate */
1512 * It already exists.
1514 up(&dir->d_inode->i_sem);
1520 if (d_mountpoint(dentry)) {
1522 if (flag & O_NOFOLLOW)
1524 while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry));
1527 if (!dentry->d_inode)
1529 if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
1533 nd->dentry = dentry;
1535 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
1538 error = may_open(nd, acc_mode, flag);
1551 if (flag & O_NOFOLLOW)
1554 * This is subtle. Instead of calling do_follow_link() we do the
1555 * thing by hands. The reason is that this way we have zero link_count
1556 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1557 * After that we have the parent and last component, i.e.
1558 * we are in the same situation as after the first path_walk().
1559 * Well, almost - if the last component is normal we get its copy
1560 * stored in nd->last.name and we will have to putname() it when we
1561 * are done. Procfs-like symlinks just set LAST_BIND.
1563 nd->flags |= LOOKUP_PARENT;
1564 error = security_inode_follow_link(dentry, nd);
1567 touch_atime(nd->mnt, dentry);
1568 nd_set_link(nd, NULL);
1569 error = dentry->d_inode->i_op->follow_link(dentry, nd);
1571 char *s = nd_get_link(nd);
1573 error = __vfs_follow_link(nd, s);
1574 if (dentry->d_inode->i_op->put_link)
1575 dentry->d_inode->i_op->put_link(dentry, nd);
1580 nd->flags &= ~LOOKUP_PARENT;
1581 if (nd->last_type == LAST_BIND) {
1582 dentry = nd->dentry;
1586 if (nd->last_type != LAST_NORM)
1588 if (nd->last.name[nd->last.len]) {
1589 putname(nd->last.name);
1594 putname(nd->last.name);
1598 down(&dir->d_inode->i_sem);
1599 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1600 putname(nd->last.name);
1605 * lookup_create - lookup a dentry, creating it if it doesn't exist
1606 * @nd: nameidata info
1607 * @is_dir: directory flag
1609 * Simple function to lookup and return a dentry and create it
1610 * if it doesn't exist. Is SMP-safe.
1612 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1614 struct dentry *dentry;
1617 down(&nd->dentry->d_inode->i_sem);
1619 if (nd->last_type != LAST_NORM)
1621 nd->flags &= ~LOOKUP_PARENT;
1622 dentry = lookup_hash(&nd->last, nd->dentry);
1625 error = mnt_may_create(nd->mnt, nd->dentry->d_inode, dentry);
1629 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1636 return ERR_PTR(error);
1639 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1641 int error = may_create(dir, dentry, NULL);
1646 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1649 if (!dir->i_op || !dir->i_op->mknod)
1652 error = security_inode_mknod(dir, dentry, mode, dev);
1657 error = dir->i_op->mknod(dir, dentry, mode, dev);
1659 inode_dir_notify(dir, DN_CREATE);
1660 security_inode_post_mknod(dir, dentry, mode, dev);
1665 asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
1669 struct dentry * dentry;
1670 struct nameidata nd;
1674 tmp = getname(filename);
1676 return PTR_ERR(tmp);
1678 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1681 dentry = lookup_create(&nd, 0);
1682 error = PTR_ERR(dentry);
1684 if (!IS_POSIXACL(nd.dentry->d_inode))
1685 mode &= ~current->fs->umask;
1686 if (!IS_ERR(dentry)) {
1687 switch (mode & S_IFMT) {
1688 case 0: case S_IFREG:
1689 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1691 case S_IFCHR: case S_IFBLK:
1692 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1693 new_decode_dev(dev));
1695 case S_IFIFO: case S_IFSOCK:
1696 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1706 up(&nd.dentry->d_inode->i_sem);
1714 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1716 int error = may_create(dir, dentry, NULL);
1721 if (!dir->i_op || !dir->i_op->mkdir)
1724 mode &= (S_IRWXUGO|S_ISVTX);
1725 error = security_inode_mkdir(dir, dentry, mode);
1730 error = dir->i_op->mkdir(dir, dentry, mode);
1732 inode_dir_notify(dir, DN_CREATE);
1733 security_inode_post_mkdir(dir,dentry, mode);
1738 asmlinkage long sys_mkdir(const char __user * pathname, int mode)
1743 tmp = getname(pathname);
1744 error = PTR_ERR(tmp);
1746 struct dentry *dentry;
1747 struct nameidata nd;
1749 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1752 dentry = lookup_create(&nd, 1);
1753 error = PTR_ERR(dentry);
1754 if (!IS_ERR(dentry)) {
1755 if (!IS_POSIXACL(nd.dentry->d_inode))
1756 mode &= ~current->fs->umask;
1757 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1760 up(&nd.dentry->d_inode->i_sem);
1770 * We try to drop the dentry early: we should have
1771 * a usage count of 2 if we're the only user of this
1772 * dentry, and if that is true (possibly after pruning
1773 * the dcache), then we drop the dentry now.
1775 * A low-level filesystem can, if it choses, legally
1778 * if (!d_unhashed(dentry))
1781 * if it cannot handle the case of removing a directory
1782 * that is still in use by something else..
1784 void dentry_unhash(struct dentry *dentry)
1787 spin_lock(&dcache_lock);
1788 switch (atomic_read(&dentry->d_count)) {
1790 spin_unlock(&dcache_lock);
1791 shrink_dcache_parent(dentry);
1792 spin_lock(&dcache_lock);
1793 if (atomic_read(&dentry->d_count) != 2)
1798 spin_unlock(&dcache_lock);
1801 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1803 int error = may_delete(dir, dentry, 1);
1808 if (!dir->i_op || !dir->i_op->rmdir)
1813 down(&dentry->d_inode->i_sem);
1814 dentry_unhash(dentry);
1815 if (d_mountpoint(dentry))
1818 error = security_inode_rmdir(dir, dentry);
1820 error = dir->i_op->rmdir(dir, dentry);
1822 dentry->d_inode->i_flags |= S_DEAD;
1825 up(&dentry->d_inode->i_sem);
1827 inode_dir_notify(dir, DN_DELETE);
1835 asmlinkage long sys_rmdir(const char __user * pathname)
1839 struct dentry *dentry;
1840 struct nameidata nd;
1842 name = getname(pathname);
1844 return PTR_ERR(name);
1846 error = path_lookup(name, LOOKUP_PARENT, &nd);
1850 switch(nd.last_type) {
1861 down(&nd.dentry->d_inode->i_sem);
1862 dentry = lookup_hash(&nd.last, nd.dentry);
1863 error = PTR_ERR(dentry);
1864 if (!IS_ERR(dentry)) {
1865 error = mnt_may_unlink(nd.mnt, nd.dentry->d_inode, dentry);
1868 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1872 up(&nd.dentry->d_inode->i_sem);
1880 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1882 int error = may_delete(dir, dentry, 0);
1887 if (!dir->i_op || !dir->i_op->unlink)
1892 down(&dentry->d_inode->i_sem);
1893 if (d_mountpoint(dentry))
1896 error = security_inode_unlink(dir, dentry);
1898 error = dir->i_op->unlink(dir, dentry);
1900 up(&dentry->d_inode->i_sem);
1902 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1903 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1905 inode_dir_notify(dir, DN_DELETE);
1911 * Make sure that the actual truncation of the file will occur outside its
1912 * directory's i_sem. Truncate can take a long time if there is a lot of
1913 * writeout happening, and we don't want to prevent access to the directory
1914 * while waiting on the I/O.
1916 asmlinkage long sys_unlink(const char __user * pathname)
1920 struct dentry *dentry;
1921 struct nameidata nd;
1922 struct inode *inode = NULL;
1924 name = getname(pathname);
1926 return PTR_ERR(name);
1928 error = path_lookup(name, LOOKUP_PARENT, &nd);
1932 if (nd.last_type != LAST_NORM)
1934 down(&nd.dentry->d_inode->i_sem);
1935 dentry = lookup_hash(&nd.last, nd.dentry);
1936 error = PTR_ERR(dentry);
1937 if (!IS_ERR(dentry)) {
1938 /* Why not before? Because we want correct error value */
1939 if (nd.last.name[nd.last.len])
1941 error = mnt_may_unlink(nd.mnt, nd.dentry->d_inode, dentry);
1944 inode = dentry->d_inode;
1946 atomic_inc(&inode->i_count);
1947 error = vfs_unlink(nd.dentry->d_inode, dentry);
1951 up(&nd.dentry->d_inode->i_sem);
1953 iput(inode); /* truncate the inode here */
1961 error = !dentry->d_inode ? -ENOENT :
1962 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1966 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
1968 int error = may_create(dir, dentry, NULL);
1973 if (!dir->i_op || !dir->i_op->symlink)
1976 error = security_inode_symlink(dir, dentry, oldname);
1981 error = dir->i_op->symlink(dir, dentry, oldname);
1983 inode_dir_notify(dir, DN_CREATE);
1984 security_inode_post_symlink(dir, dentry, oldname);
1989 asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
1995 from = getname(oldname);
1997 return PTR_ERR(from);
1998 to = getname(newname);
1999 error = PTR_ERR(to);
2001 struct dentry *dentry;
2002 struct nameidata nd;
2004 error = path_lookup(to, LOOKUP_PARENT, &nd);
2007 dentry = lookup_create(&nd, 0);
2008 error = PTR_ERR(dentry);
2009 if (!IS_ERR(dentry)) {
2010 error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
2013 up(&nd.dentry->d_inode->i_sem);
2022 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2024 struct inode *inode = old_dentry->d_inode;
2030 error = may_create(dir, new_dentry, NULL);
2034 if (dir->i_sb != inode->i_sb)
2038 * A link to an append-only or immutable file cannot be created.
2040 if (IS_APPEND(inode) || IS_IXORUNLINK(inode))
2042 if (!dir->i_op || !dir->i_op->link)
2044 if (S_ISDIR(old_dentry->d_inode->i_mode))
2047 error = security_inode_link(old_dentry, dir, new_dentry);
2051 down(&old_dentry->d_inode->i_sem);
2053 error = dir->i_op->link(old_dentry, dir, new_dentry);
2054 up(&old_dentry->d_inode->i_sem);
2056 inode_dir_notify(dir, DN_CREATE);
2057 security_inode_post_link(old_dentry, dir, new_dentry);
2063 * Hardlinks are often used in delicate situations. We avoid
2064 * security-related surprises by not following symlinks on the
2067 * We don't follow them on the oldname either to be compatible
2068 * with linux 2.0, and to avoid hard-linking to directories
2069 * and other special files. --ADM
2071 asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
2073 struct dentry *new_dentry;
2074 struct nameidata nd, old_nd;
2078 to = getname(newname);
2082 error = __user_walk(oldname, 0, &old_nd);
2085 error = path_lookup(to, LOOKUP_PARENT, &nd);
2089 * We allow hard-links to be created to a bind-mount as long
2090 * as the bind-mount is not read-only. Checking for cross-dev
2091 * links is subsumed by the superblock check in vfs_link().
2094 if (MNT_IS_RDONLY(old_nd.mnt))
2096 new_dentry = lookup_create(&nd, 0);
2097 error = PTR_ERR(new_dentry);
2098 if (!IS_ERR(new_dentry)) {
2099 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
2102 up(&nd.dentry->d_inode->i_sem);
2106 path_release(&old_nd);
2114 * The worst of all namespace operations - renaming directory. "Perverted"
2115 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2117 * a) we can get into loop creation. Check is done in is_subdir().
2118 * b) race potential - two innocent renames can create a loop together.
2119 * That's where 4.4 screws up. Current fix: serialization on
2120 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
2122 * c) we have to lock _three_ objects - parents and victim (if it exists).
2123 * And that - after we got ->i_sem on parents (until then we don't know
2124 * whether the target exists). Solution: try to be smart with locking
2125 * order for inodes. We rely on the fact that tree topology may change
2126 * only under ->s_vfs_rename_sem _and_ that parent of the object we
2127 * move will be locked. Thus we can rank directories by the tree
2128 * (ancestors first) and rank all non-directories after them.
2129 * That works since everybody except rename does "lock parent, lookup,
2130 * lock child" and rename is under ->s_vfs_rename_sem.
2131 * HOWEVER, it relies on the assumption that any object with ->lookup()
2132 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2133 * we'd better make sure that there's no link(2) for them.
2134 * d) some filesystems don't support opened-but-unlinked directories,
2135 * either because of layout or because they are not ready to deal with
2136 * all cases correctly. The latter will be fixed (taking this sort of
2137 * stuff into VFS), but the former is not going away. Solution: the same
2138 * trick as in rmdir().
2139 * e) conversion from fhandle to dentry may come in the wrong moment - when
2140 * we are removing the target. Solution: we will have to grab ->i_sem
2141 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2142 * ->i_sem on parents, which works but leads to some truely excessive
2145 int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2146 struct inode *new_dir, struct dentry *new_dentry)
2149 struct inode *target;
2152 * If we are going to change the parent - check write permissions,
2153 * we'll need to flip '..'.
2155 if (new_dir != old_dir) {
2156 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2161 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2165 target = new_dentry->d_inode;
2167 down(&target->i_sem);
2168 dentry_unhash(new_dentry);
2170 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2173 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2176 target->i_flags |= S_DEAD;
2178 if (d_unhashed(new_dentry))
2179 d_rehash(new_dentry);
2183 d_move(old_dentry,new_dentry);
2184 security_inode_post_rename(old_dir, old_dentry,
2185 new_dir, new_dentry);
2190 int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2191 struct inode *new_dir, struct dentry *new_dentry)
2193 struct inode *target;
2196 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2201 target = new_dentry->d_inode;
2203 down(&target->i_sem);
2204 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2207 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2209 /* The following d_move() should become unconditional */
2210 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
2211 d_move(old_dentry, new_dentry);
2212 security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
2220 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2221 struct inode *new_dir, struct dentry *new_dentry)
2224 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2226 if (old_dentry->d_inode == new_dentry->d_inode)
2229 error = may_delete(old_dir, old_dentry, is_dir);
2233 if (!new_dentry->d_inode)
2234 error = may_create(new_dir, new_dentry, NULL);
2236 error = may_delete(new_dir, new_dentry, is_dir);
2240 if (!old_dir->i_op || !old_dir->i_op->rename)
2243 DQUOT_INIT(old_dir);
2244 DQUOT_INIT(new_dir);
2247 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2249 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2251 if (old_dir == new_dir)
2252 inode_dir_notify(old_dir, DN_RENAME);
2254 inode_dir_notify(old_dir, DN_DELETE);
2255 inode_dir_notify(new_dir, DN_CREATE);
2261 static inline int do_rename(const char * oldname, const char * newname)
2264 struct dentry * old_dir, * new_dir;
2265 struct dentry * old_dentry, *new_dentry;
2266 struct dentry * trap;
2267 struct nameidata oldnd, newnd;
2269 error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2273 error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2278 if (oldnd.mnt != newnd.mnt)
2281 old_dir = oldnd.dentry;
2283 if (oldnd.last_type != LAST_NORM)
2286 new_dir = newnd.dentry;
2287 if (newnd.last_type != LAST_NORM)
2290 trap = lock_rename(new_dir, old_dir);
2292 old_dentry = lookup_hash(&oldnd.last, old_dir);
2293 error = PTR_ERR(old_dentry);
2294 if (IS_ERR(old_dentry))
2296 /* source must exist */
2298 if (!old_dentry->d_inode)
2300 /* unless the source is a directory trailing slashes give -ENOTDIR */
2301 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2303 if (oldnd.last.name[oldnd.last.len])
2305 if (newnd.last.name[newnd.last.len])
2308 /* source should not be ancestor of target */
2310 if (old_dentry == trap)
2313 if (MNT_IS_RDONLY(newnd.mnt))
2315 new_dentry = lookup_hash(&newnd.last, new_dir);
2316 error = PTR_ERR(new_dentry);
2317 if (IS_ERR(new_dentry))
2319 /* target should not be an ancestor of source */
2321 if (new_dentry == trap)
2324 error = vfs_rename(old_dir->d_inode, old_dentry,
2325 new_dir->d_inode, new_dentry);
2331 unlock_rename(new_dir, old_dir);
2333 path_release(&newnd);
2335 path_release(&oldnd);
2340 asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
2346 from = getname(oldname);
2348 return PTR_ERR(from);
2349 to = getname(newname);
2350 error = PTR_ERR(to);
2352 error = do_rename(from,to);
2359 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2363 len = PTR_ERR(link);
2368 if (len > (unsigned) buflen)
2370 if (copy_to_user(buffer, link, len))
2377 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2378 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2379 * using) it for any given inode is up to filesystem.
2381 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2383 struct nameidata nd;
2386 res = dentry->d_inode->i_op->follow_link(dentry, &nd);
2388 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2389 if (dentry->d_inode->i_op->put_link)
2390 dentry->d_inode->i_op->put_link(dentry, &nd);
2395 int vfs_follow_link(struct nameidata *nd, const char *link)
2397 return __vfs_follow_link(nd, link);
2400 /* get the link contents into pagecache */
2401 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2404 struct address_space *mapping = dentry->d_inode->i_mapping;
2405 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2409 wait_on_page_locked(page);
2410 if (!PageUptodate(page))
2416 page_cache_release(page);
2417 return ERR_PTR(-EIO);
2423 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2425 struct page *page = NULL;
2426 char *s = page_getlink(dentry, &page);
2427 int res = vfs_readlink(dentry,buffer,buflen,s);
2430 page_cache_release(page);
2435 int page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2438 nd_set_link(nd, page_getlink(dentry, &page));
2442 void page_put_link(struct dentry *dentry, struct nameidata *nd)
2444 if (!IS_ERR(nd_get_link(nd))) {
2446 page = find_get_page(dentry->d_inode->i_mapping, 0);
2450 page_cache_release(page);
2451 page_cache_release(page);
2455 int page_symlink(struct inode *inode, const char *symname, int len)
2457 struct address_space *mapping = inode->i_mapping;
2458 struct page *page = grab_cache_page(mapping, 0);
2464 err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2467 kaddr = kmap_atomic(page, KM_USER0);
2468 memcpy(kaddr, symname, len-1);
2469 kunmap_atomic(kaddr, KM_USER0);
2470 mapping->a_ops->commit_write(NULL, page, 0, len-1);
2472 * Notice that we are _not_ going to block here - end of page is
2473 * unmapped, so this will only try to map the rest of page, see
2474 * that it is unmapped (typically even will not look into inode -
2475 * ->i_size will be enough for everything) and zero it out.
2476 * OTOH it's obviously correct and should make the page up-to-date.
2478 if (!PageUptodate(page)) {
2479 err = mapping->a_ops->readpage(NULL, page);
2480 wait_on_page_locked(page);
2484 page_cache_release(page);
2487 mark_inode_dirty(inode);
2491 page_cache_release(page);
2496 struct inode_operations page_symlink_inode_operations = {
2497 .readlink = generic_readlink,
2498 .follow_link = page_follow_link_light,
2499 .put_link = page_put_link,
2502 EXPORT_SYMBOL(__user_walk);
2503 EXPORT_SYMBOL(follow_down);
2504 EXPORT_SYMBOL(follow_up);
2505 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2506 EXPORT_SYMBOL(getname);
2507 EXPORT_SYMBOL(lock_rename);
2508 EXPORT_SYMBOL(lookup_hash);
2509 EXPORT_SYMBOL(lookup_one_len);
2510 EXPORT_SYMBOL(page_follow_link_light);
2511 EXPORT_SYMBOL(page_put_link);
2512 EXPORT_SYMBOL(page_readlink);
2513 EXPORT_SYMBOL(page_symlink);
2514 EXPORT_SYMBOL(page_symlink_inode_operations);
2515 EXPORT_SYMBOL(path_lookup);
2516 EXPORT_SYMBOL(path_release);
2517 EXPORT_SYMBOL(path_walk);
2518 EXPORT_SYMBOL(permission);
2519 EXPORT_SYMBOL(unlock_rename);
2520 EXPORT_SYMBOL(vfs_create);
2521 EXPORT_SYMBOL(vfs_follow_link);
2522 EXPORT_SYMBOL(vfs_link);
2523 EXPORT_SYMBOL(vfs_mkdir);
2524 EXPORT_SYMBOL(vfs_mknod);
2525 EXPORT_SYMBOL(generic_permission);
2526 EXPORT_SYMBOL(vfs_readlink);
2527 EXPORT_SYMBOL(vfs_rename);
2528 EXPORT_SYMBOL(vfs_rmdir);
2529 EXPORT_SYMBOL(vfs_symlink);
2530 EXPORT_SYMBOL(vfs_unlink);
2531 EXPORT_SYMBOL(dentry_unhash);
2532 EXPORT_SYMBOL(generic_readlink);