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 #warning MEF: need to make CONFIG_VSERVER_FILESHARING a Kconfig option
232 #define CONFIG_VSERVER_FILESHARING 1
234 static inline int xid_permission(struct inode *inode, int mask, struct nameidata *nd)
236 if (inode->i_xid == 0)
239 #ifdef CONFIG_VSERVER_FILESHARING
240 /* MEF: PlanetLab FS module assumes that any file that can be
241 * named (e.g., via a cross mount) is not hidden from another
242 * context or the admin context.
244 if (vx_check(inode->i_xid,VX_STATIC|VX_DYNAMIC))
247 if (vx_check(inode->i_xid,VX_ADMIN|VX_WATCH|VX_IDENT))
250 vxwprintk(1, "xid=%d denied access to %p[#%d,%lu] »%s«.",
251 vx_current_xid(), inode, inode->i_xid, inode->i_ino,
252 vxd_path(nd->dentry, nd->mnt));
256 int permission(struct inode * inode,int mask, struct nameidata *nd)
260 umode_t mode = inode->i_mode;
262 /* Prevent vservers from escaping chroot() barriers */
263 if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN))
266 /* Ordinary permission routines do not understand MAY_APPEND. */
267 submask = mask & ~MAY_APPEND;
269 if (nd && (mask & MAY_WRITE) && MNT_IS_RDONLY(nd->mnt) &&
270 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
273 if ((retval = xid_permission(inode, mask, nd)))
276 if (inode->i_op && inode->i_op->permission)
277 retval = inode->i_op->permission(inode, submask, nd);
279 retval = generic_permission(inode, submask, NULL);
283 return security_inode_permission(inode, mask, nd);
287 * get_write_access() gets write permission for a file.
288 * put_write_access() releases this write permission.
289 * This is used for regular files.
290 * We cannot support write (and maybe mmap read-write shared) accesses and
291 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
292 * can have the following values:
293 * 0: no writers, no VM_DENYWRITE mappings
294 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
295 * > 0: (i_writecount) users are writing to the file.
297 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
298 * except for the cases where we don't hold i_writecount yet. Then we need to
299 * use {get,deny}_write_access() - these functions check the sign and refuse
300 * to do the change if sign is wrong. Exclusion between them is provided by
301 * the inode->i_lock spinlock.
304 int get_write_access(struct inode * inode)
306 spin_lock(&inode->i_lock);
307 if (atomic_read(&inode->i_writecount) < 0) {
308 spin_unlock(&inode->i_lock);
311 atomic_inc(&inode->i_writecount);
312 spin_unlock(&inode->i_lock);
317 int deny_write_access(struct file * file)
319 struct inode *inode = file->f_dentry->d_inode;
321 spin_lock(&inode->i_lock);
322 if (atomic_read(&inode->i_writecount) > 0) {
323 spin_unlock(&inode->i_lock);
326 atomic_dec(&inode->i_writecount);
327 spin_unlock(&inode->i_lock);
332 void path_release(struct nameidata *nd)
339 * umount() mustn't call path_release()/mntput() as that would clear
342 void path_release_on_umount(struct nameidata *nd)
349 * Internal lookup() using the new generic dcache.
352 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
354 struct dentry * dentry = __d_lookup(parent, name);
356 /* lockess __d_lookup may fail due to concurrent d_move()
357 * in some unrelated directory, so try with d_lookup
360 dentry = d_lookup(parent, name);
362 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
363 if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
372 * Short-cut version of permission(), for calling by
373 * path_walk(), when dcache lock is held. Combines parts
374 * of permission() and generic_permission(), and tests ONLY for
375 * MAY_EXEC permission.
377 * If appropriate, check DAC only. If not appropriate, or
378 * short-cut DAC fails, then call permission() to do more
379 * complete permission check.
381 static inline int exec_permission_lite(struct inode *inode,
382 struct nameidata *nd)
384 umode_t mode = inode->i_mode;
386 if (inode->i_op && inode->i_op->permission)
389 if (current->fsuid == inode->i_uid)
391 else if (in_group_p(inode->i_gid))
397 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
400 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
403 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
408 return security_inode_permission(inode, MAY_EXEC, nd);
412 * This is called when everything else fails, and we actually have
413 * to go to the low-level filesystem to find out what we should do..
415 * We get the directory semaphore, and after getting that we also
416 * make sure that nobody added the entry to the dcache in the meantime..
419 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
421 struct dentry * result;
422 struct inode *dir = parent->d_inode;
426 * First re-do the cached lookup just in case it was created
427 * while we waited for the directory semaphore..
429 * FIXME! This could use version numbering or similar to
430 * avoid unnecessary cache lookups.
432 * The "dcache_lock" is purely to protect the RCU list walker
433 * from concurrent renames at this point (we mustn't get false
434 * negatives from the RCU list walk here, unlike the optimistic
437 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
439 result = d_lookup(parent, name);
441 struct dentry * dentry = d_alloc(parent, name);
442 result = ERR_PTR(-ENOMEM);
444 result = dir->i_op->lookup(dir, dentry, nd);
455 * Uhhuh! Nasty case: the cache was re-populated while
456 * we waited on the semaphore. Need to revalidate.
459 if (result->d_op && result->d_op->d_revalidate) {
460 if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
462 result = ERR_PTR(-ENOENT);
468 static int __emul_lookup_dentry(const char *, struct nameidata *);
472 walk_init_root(const char *name, struct nameidata *nd)
474 read_lock(¤t->fs->lock);
475 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
476 nd->mnt = mntget(current->fs->altrootmnt);
477 nd->dentry = dget(current->fs->altroot);
478 read_unlock(¤t->fs->lock);
479 if (__emul_lookup_dentry(name,nd))
481 read_lock(¤t->fs->lock);
483 nd->mnt = mntget(current->fs->rootmnt);
484 nd->dentry = dget(current->fs->root);
485 read_unlock(¤t->fs->lock);
489 static inline int __vfs_follow_link(struct nameidata *nd, const char *link)
498 if (!walk_init_root(link, nd))
499 /* weird __emul_prefix() stuff did it */
502 res = link_path_walk(link, nd);
504 if (nd->depth || res || nd->last_type!=LAST_NORM)
507 * If it is an iterative symlinks resolution in open_namei() we
508 * have to copy the last component. And all that crap because of
509 * bloody create() on broken symlinks. Furrfu...
512 if (unlikely(!name)) {
516 strcpy(name, nd->last.name);
517 nd->last.name = name;
521 return PTR_ERR(link);
525 * This limits recursive symlink follows to 8, while
526 * limiting consecutive symlinks to 40.
528 * Without that kind of total limit, nasty chains of consecutive
529 * symlinks can cause almost arbitrarily long lookups.
531 static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd)
534 if (current->link_count >= MAX_NESTED_LINKS)
536 if (current->total_link_count >= 40)
538 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
540 err = security_inode_follow_link(dentry, nd);
543 current->link_count++;
544 current->total_link_count++;
546 touch_atime(nd->mnt, dentry);
547 nd_set_link(nd, NULL);
548 err = dentry->d_inode->i_op->follow_link(dentry, nd);
550 char *s = nd_get_link(nd);
552 err = __vfs_follow_link(nd, s);
553 if (dentry->d_inode->i_op->put_link)
554 dentry->d_inode->i_op->put_link(dentry, nd);
556 current->link_count--;
564 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
566 struct vfsmount *parent;
567 struct dentry *mountpoint;
568 spin_lock(&vfsmount_lock);
569 parent=(*mnt)->mnt_parent;
570 if (parent == *mnt) {
571 spin_unlock(&vfsmount_lock);
575 mountpoint=dget((*mnt)->mnt_mountpoint);
576 spin_unlock(&vfsmount_lock);
578 *dentry = mountpoint;
584 /* no need for dcache_lock, as serialization is taken care in
587 static int follow_mount(struct vfsmount **mnt, struct dentry **dentry)
590 while (d_mountpoint(*dentry)) {
591 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
597 *dentry = dget(mounted->mnt_root);
603 /* no need for dcache_lock, as serialization is taken care in
606 static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
608 struct vfsmount *mounted;
610 mounted = lookup_mnt(*mnt, *dentry);
615 *dentry = dget(mounted->mnt_root);
621 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
623 return __follow_down(mnt,dentry);
626 static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry)
629 struct vfsmount *parent;
630 struct dentry *old = *dentry;
632 read_lock(¤t->fs->lock);
633 if (*dentry == current->fs->root &&
634 *mnt == current->fs->rootmnt) {
635 read_unlock(¤t->fs->lock);
638 read_unlock(¤t->fs->lock);
639 spin_lock(&dcache_lock);
640 if (*dentry != (*mnt)->mnt_root) {
641 *dentry = dget((*dentry)->d_parent);
642 spin_unlock(&dcache_lock);
646 spin_unlock(&dcache_lock);
647 spin_lock(&vfsmount_lock);
648 parent = (*mnt)->mnt_parent;
649 if (parent == *mnt) {
650 spin_unlock(&vfsmount_lock);
654 *dentry = dget((*mnt)->mnt_mountpoint);
655 spin_unlock(&vfsmount_lock);
660 follow_mount(mnt, dentry);
664 struct vfsmount *mnt;
665 struct dentry *dentry;
669 * It's more convoluted than I'd like it to be, but... it's still fairly
670 * small and for now I'd prefer to have fast path as straight as possible.
671 * It _is_ time-critical.
673 static int do_lookup(struct nameidata *nd, struct qstr *name,
674 struct path *path, int atomic)
676 struct vfsmount *mnt = nd->mnt;
677 struct dentry *dentry = __d_lookup(nd->dentry, name);
682 if (dentry->d_op && dentry->d_op->d_revalidate)
683 goto need_revalidate;
684 inode = dentry->d_inode;
687 if (inode->i_sb->s_magic == PROC_SUPER_MAGIC) {
688 struct proc_dir_entry *de = PDE(inode);
690 if (de && !vx_hide_check(0, de->vx_flags))
693 #ifdef CONFIG_VSERVER_FILESHARING
694 /* MEF: PlanetLab FS module assumes that any file that can be
695 * named (e.g., via a cross mount) is not hidden from another
696 * context or the admin context.
698 if (vx_check(inode->i_xid,VX_STATIC|VX_DYNAMIC|VX_ADMIN)) {
701 else /* do the following check */
703 if (!vx_check(inode->i_xid,
710 path->dentry = dentry;
713 vxwprintk(1, "xid=%d did lookup hidden %p[#%d,%lu] »%s«.",
714 vx_current_xid(), inode, inode->i_xid, inode->i_ino,
715 vxd_path(dentry, mnt));
721 return -EWOULDBLOCKIO;
722 dentry = real_lookup(nd->dentry, name, nd);
729 return -EWOULDBLOCKIO;
730 if (dentry->d_op->d_revalidate(dentry, nd))
732 if (d_invalidate(dentry))
738 return PTR_ERR(dentry);
744 * This is the basic name resolution function, turning a pathname
745 * into the final dentry.
747 * We expect 'base' to be positive and a directory.
749 int fastcall link_path_walk(const char * name, struct nameidata *nd)
754 unsigned int lookup_flags = nd->flags;
756 atomic = (lookup_flags & LOOKUP_ATOMIC);
763 inode = nd->dentry->d_inode;
765 lookup_flags = LOOKUP_FOLLOW;
767 /* At this point we know we have a real path component. */
773 err = exec_permission_lite(inode, nd);
774 if (err == -EAGAIN) {
775 err = permission(inode, MAY_EXEC, nd);
781 c = *(const unsigned char *)name;
783 hash = init_name_hash();
786 hash = partial_name_hash(c, hash);
787 c = *(const unsigned char *)name;
788 } while (c && (c != '/'));
789 this.len = name - (const char *) this.name;
790 this.hash = end_name_hash(hash);
792 /* remove trailing slashes? */
795 while (*++name == '/');
797 goto last_with_slashes;
800 * "." and ".." are special - ".." especially so because it has
801 * to be able to know about the current root directory and
802 * parent relationships.
804 if (this.name[0] == '.') switch (this.len) {
808 if (this.name[1] != '.')
810 follow_dotdot(&nd->mnt, &nd->dentry);
811 inode = nd->dentry->d_inode;
817 * See if the low-level filesystem might want
818 * to use its own hash..
820 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
821 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
825 nd->flags |= LOOKUP_CONTINUE;
826 /* This does the actual lookups.. */
827 err = do_lookup(nd, &this, &next, atomic);
830 /* Check mountpoints.. */
831 follow_mount(&next.mnt, &next.dentry);
834 inode = next.dentry->d_inode;
841 if (inode->i_op->follow_link) {
843 err = do_follow_link(next.dentry, nd);
849 inode = nd->dentry->d_inode;
858 nd->dentry = next.dentry;
861 if (!inode->i_op->lookup)
864 /* here ends the main loop */
867 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
869 nd->flags &= ~LOOKUP_CONTINUE;
870 if (lookup_flags & LOOKUP_PARENT)
872 if (this.name[0] == '.') switch (this.len) {
876 if (this.name[1] != '.')
878 follow_dotdot(&nd->mnt, &nd->dentry);
879 inode = nd->dentry->d_inode;
884 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
885 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
889 err = do_lookup(nd, &this, &next, atomic);
892 follow_mount(&next.mnt, &next.dentry);
893 inode = next.dentry->d_inode;
894 if ((lookup_flags & LOOKUP_FOLLOW)
895 && inode && inode->i_op && inode->i_op->follow_link) {
897 err = do_follow_link(next.dentry, nd);
902 inode = nd->dentry->d_inode;
906 nd->dentry = next.dentry;
911 if (lookup_flags & LOOKUP_DIRECTORY) {
913 if (!inode->i_op || !inode->i_op->lookup)
919 nd->last_type = LAST_NORM;
920 if (this.name[0] != '.')
923 nd->last_type = LAST_DOT;
924 else if (this.len == 2 && this.name[1] == '.')
925 nd->last_type = LAST_DOTDOT;
930 * We bypassed the ordinary revalidation routines.
931 * We may need to check the cached dentry for staleness.
933 if (nd->dentry && nd->dentry->d_sb &&
934 (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
936 /* Note: we do not d_invalidate() */
937 if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
951 int fastcall path_walk(const char * name, struct nameidata *nd)
953 current->total_link_count = 0;
954 return link_path_walk(name, nd);
958 /* returns 1 if everything is done */
959 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
961 if (path_walk(name, nd))
962 return 0; /* something went wrong... */
964 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
965 struct dentry *old_dentry = nd->dentry;
966 struct vfsmount *old_mnt = nd->mnt;
967 struct qstr last = nd->last;
968 int last_type = nd->last_type;
970 * NAME was not found in alternate root or it's a directory. Try to find
971 * it in the normal root:
973 nd->last_type = LAST_ROOT;
974 read_lock(¤t->fs->lock);
975 nd->mnt = mntget(current->fs->rootmnt);
976 nd->dentry = dget(current->fs->root);
977 read_unlock(¤t->fs->lock);
978 if (path_walk(name, nd) == 0) {
979 if (nd->dentry->d_inode) {
986 nd->dentry = old_dentry;
989 nd->last_type = last_type;
994 void set_fs_altroot(void)
996 char *emul = __emul_prefix();
998 struct vfsmount *mnt = NULL, *oldmnt;
999 struct dentry *dentry = NULL, *olddentry;
1004 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
1010 write_lock(¤t->fs->lock);
1011 oldmnt = current->fs->altrootmnt;
1012 olddentry = current->fs->altroot;
1013 current->fs->altrootmnt = mnt;
1014 current->fs->altroot = dentry;
1015 write_unlock(¤t->fs->lock);
1022 int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
1026 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1030 read_lock(¤t->fs->lock);
1032 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
1033 nd->mnt = mntget(current->fs->altrootmnt);
1034 nd->dentry = dget(current->fs->altroot);
1035 read_unlock(¤t->fs->lock);
1036 if (__emul_lookup_dentry(name,nd))
1038 read_lock(¤t->fs->lock);
1040 nd->mnt = mntget(current->fs->rootmnt);
1041 nd->dentry = dget(current->fs->root);
1043 nd->mnt = mntget(current->fs->pwdmnt);
1044 nd->dentry = dget(current->fs->pwd);
1046 read_unlock(¤t->fs->lock);
1047 current->total_link_count = 0;
1048 retval = link_path_walk(name, nd);
1049 if (unlikely(current->audit_context
1050 && nd && nd->dentry && nd->dentry->d_inode))
1052 nd->dentry->d_inode->i_ino,
1053 nd->dentry->d_inode->i_rdev);
1058 * Restricted form of lookup. Doesn't follow links, single-component only,
1059 * needs parent already locked. Doesn't follow mounts.
1062 static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
1064 struct dentry * dentry;
1065 struct inode *inode;
1068 inode = base->d_inode;
1069 err = permission(inode, MAY_EXEC, nd);
1070 dentry = ERR_PTR(err);
1075 * See if the low-level filesystem might want
1076 * to use its own hash..
1078 if (base->d_op && base->d_op->d_hash) {
1079 err = base->d_op->d_hash(base, name);
1080 dentry = ERR_PTR(err);
1085 dentry = cached_lookup(base, name, nd);
1087 struct dentry *new = d_alloc(base, name);
1088 dentry = ERR_PTR(-ENOMEM);
1091 dentry = inode->i_op->lookup(inode, new, nd);
1101 struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
1103 return __lookup_hash(name, base, NULL);
1107 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
1118 hash = init_name_hash();
1120 c = *(const unsigned char *)name++;
1121 if (c == '/' || c == '\0')
1123 hash = partial_name_hash(c, hash);
1125 this.hash = end_name_hash(hash);
1127 return lookup_hash(&this, base);
1129 return ERR_PTR(-EACCES);
1135 * is used by most simple commands to get the inode of a specified name.
1136 * Open, link etc use their own routines, but this is enough for things
1139 * namei exists in two versions: namei/lnamei. The only difference is
1140 * that namei follows links, while lnamei does not.
1143 int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1145 char *tmp = getname(name);
1146 int err = PTR_ERR(tmp);
1149 err = path_lookup(tmp, flags, nd);
1156 * It's inline, so penalty for filesystems that don't use sticky bit is
1159 static inline int check_sticky(struct inode *dir, struct inode *inode)
1161 if (!(dir->i_mode & S_ISVTX))
1163 if (inode->i_uid == current->fsuid)
1165 if (dir->i_uid == current->fsuid)
1167 return !capable(CAP_FOWNER);
1171 * Check whether we can remove a link victim from directory dir, check
1172 * whether the type of victim is right.
1173 * 1. We can't do it if dir is read-only (done in permission())
1174 * 2. We should have write and exec permissions on dir
1175 * 3. We can't remove anything from append-only dir
1176 * 4. We can't do anything with immutable dir (done in permission())
1177 * 5. If the sticky bit on dir is set we should either
1178 * a. be owner of dir, or
1179 * b. be owner of victim, or
1180 * c. have CAP_FOWNER capability
1181 * 6. If the victim is append-only or immutable we can't do antyhing with
1182 * links pointing to it.
1183 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1184 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1185 * 9. We can't remove a root or mountpoint.
1186 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1187 * nfs_async_unlink().
1189 static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1193 if (!victim->d_inode)
1196 BUG_ON(victim->d_parent->d_inode != dir);
1198 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1203 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1204 IS_IXORUNLINK(victim->d_inode))
1207 if (!S_ISDIR(victim->d_inode->i_mode))
1209 if (IS_ROOT(victim))
1211 } else if (S_ISDIR(victim->d_inode->i_mode))
1213 if (IS_DEADDIR(dir))
1215 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1220 /* Check whether we can create an object with dentry child in directory
1222 * 1. We can't do it if child already exists (open has special treatment for
1223 * this case, but since we are inlined it's OK)
1224 * 2. We can't do it if dir is read-only (done in permission())
1225 * 3. We should have write and exec permissions on dir
1226 * 4. We can't do it if dir is immutable (done in permission())
1228 static inline int may_create(struct inode *dir, struct dentry *child,
1229 struct nameidata *nd)
1233 if (IS_DEADDIR(dir))
1235 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1238 static inline int mnt_may_create(struct vfsmount *mnt, struct inode *dir, struct dentry *child) {
1241 if (IS_DEADDIR(dir))
1243 if (mnt->mnt_flags & MNT_RDONLY)
1248 static inline int mnt_may_unlink(struct vfsmount *mnt, struct inode *dir, struct dentry *child) {
1249 if (!child->d_inode)
1251 if (mnt->mnt_flags & MNT_RDONLY)
1257 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1260 * O_DIRECTORY translates into forcing a directory lookup.
1262 static inline int lookup_flags(unsigned int f)
1264 unsigned long retval = LOOKUP_FOLLOW;
1267 retval &= ~LOOKUP_FOLLOW;
1269 if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1270 retval &= ~LOOKUP_FOLLOW;
1272 if (f & O_DIRECTORY)
1273 retval |= LOOKUP_DIRECTORY;
1274 if (f & O_ATOMICLOOKUP)
1275 retval |= LOOKUP_ATOMIC;
1281 * p1 and p2 should be directories on the same fs.
1283 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1288 down(&p1->d_inode->i_sem);
1292 down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1294 for (p = p1; p->d_parent != p; p = p->d_parent) {
1295 if (p->d_parent == p2) {
1296 down(&p2->d_inode->i_sem);
1297 down(&p1->d_inode->i_sem);
1302 for (p = p2; p->d_parent != p; p = p->d_parent) {
1303 if (p->d_parent == p1) {
1304 down(&p1->d_inode->i_sem);
1305 down(&p2->d_inode->i_sem);
1310 down(&p1->d_inode->i_sem);
1311 down(&p2->d_inode->i_sem);
1315 void unlock_rename(struct dentry *p1, struct dentry *p2)
1317 up(&p1->d_inode->i_sem);
1319 up(&p2->d_inode->i_sem);
1320 up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1324 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1325 struct nameidata *nd)
1327 int error = may_create(dir, dentry, nd);
1332 if (!dir->i_op || !dir->i_op->create)
1333 return -EACCES; /* shouldn't it be ENOSYS? */
1336 error = security_inode_create(dir, dentry, mode);
1340 error = dir->i_op->create(dir, dentry, mode, nd);
1342 inode_dir_notify(dir, DN_CREATE);
1343 security_inode_post_create(dir, dentry, mode);
1348 int may_open(struct nameidata *nd, int acc_mode, int flag)
1350 struct dentry *dentry = nd->dentry;
1351 struct inode *inode = dentry->d_inode;
1357 if (S_ISLNK(inode->i_mode))
1360 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1363 error = permission(inode, acc_mode, nd);
1368 * FIFO's, sockets and device files are special: they don't
1369 * actually live on the filesystem itself, and as such you
1370 * can write to them even if the filesystem is read-only.
1372 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1374 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1375 if (nd->mnt->mnt_flags & MNT_NODEV)
1379 } else if ((IS_RDONLY(inode) || (nd && MNT_IS_RDONLY(nd->mnt)))
1380 && (flag & FMODE_WRITE))
1383 * An append-only file must be opened in append mode for writing.
1385 if (IS_APPEND(inode)) {
1386 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1392 /* O_NOATIME can only be set by the owner or superuser */
1393 if (flag & O_NOATIME)
1394 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1398 * Ensure there are no outstanding leases on the file.
1400 error = break_lease(inode, flag);
1404 if (flag & O_TRUNC) {
1405 error = get_write_access(inode);
1410 * Refuse to truncate files with mandatory locks held on them.
1412 error = locks_verify_locked(inode);
1416 error = do_truncate(dentry, 0);
1418 put_write_access(inode);
1422 if (flag & FMODE_WRITE)
1431 * namei for open - this is in fact almost the whole open-routine.
1433 * Note that the low bits of "flag" aren't the same as in the open
1434 * system call - they are 00 - no permissions needed
1435 * 01 - read permission needed
1436 * 10 - write permission needed
1437 * 11 - read/write permissions needed
1438 * which is a lot more logical, and also allows the "no perm" needed
1439 * for symlinks (where the permissions are checked later).
1442 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1444 int acc_mode, error = 0;
1445 struct dentry *dentry;
1449 acc_mode = ACC_MODE(flag);
1451 /* Allow the LSM permission hook to distinguish append
1452 access from general write access. */
1453 if (flag & O_APPEND)
1454 acc_mode |= MAY_APPEND;
1456 /* Fill in the open() intent data */
1457 nd->intent.open.flags = flag;
1458 nd->intent.open.create_mode = mode;
1461 * The simplest case - just a plain lookup.
1463 if (!(flag & O_CREAT)) {
1464 error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd);
1471 * Create - we need to know the parent.
1473 error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd);
1478 * We have the parent and last component. First of all, check
1479 * that we are not asked to creat(2) an obvious directory - that
1483 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1487 nd->flags &= ~LOOKUP_PARENT;
1488 down(&dir->d_inode->i_sem);
1489 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1492 error = PTR_ERR(dentry);
1493 if (IS_ERR(dentry)) {
1494 up(&dir->d_inode->i_sem);
1498 /* Negative dentry, just create the file */
1499 if (!dentry->d_inode) {
1500 if (!IS_POSIXACL(dir->d_inode))
1501 mode &= ~current->fs->umask;
1502 error = vfs_create(dir->d_inode, dentry, mode, nd);
1503 up(&dir->d_inode->i_sem);
1505 nd->dentry = dentry;
1508 /* Don't check for write permission, don't truncate */
1515 * It already exists.
1517 up(&dir->d_inode->i_sem);
1523 if (d_mountpoint(dentry)) {
1525 if (flag & O_NOFOLLOW)
1527 while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry));
1530 if (!dentry->d_inode)
1532 if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
1536 nd->dentry = dentry;
1538 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
1541 error = may_open(nd, acc_mode, flag);
1554 if (flag & O_NOFOLLOW)
1557 * This is subtle. Instead of calling do_follow_link() we do the
1558 * thing by hands. The reason is that this way we have zero link_count
1559 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1560 * After that we have the parent and last component, i.e.
1561 * we are in the same situation as after the first path_walk().
1562 * Well, almost - if the last component is normal we get its copy
1563 * stored in nd->last.name and we will have to putname() it when we
1564 * are done. Procfs-like symlinks just set LAST_BIND.
1566 nd->flags |= LOOKUP_PARENT;
1567 error = security_inode_follow_link(dentry, nd);
1570 touch_atime(nd->mnt, dentry);
1571 nd_set_link(nd, NULL);
1572 error = dentry->d_inode->i_op->follow_link(dentry, nd);
1574 char *s = nd_get_link(nd);
1576 error = __vfs_follow_link(nd, s);
1577 if (dentry->d_inode->i_op->put_link)
1578 dentry->d_inode->i_op->put_link(dentry, nd);
1583 nd->flags &= ~LOOKUP_PARENT;
1584 if (nd->last_type == LAST_BIND) {
1585 dentry = nd->dentry;
1589 if (nd->last_type != LAST_NORM)
1591 if (nd->last.name[nd->last.len]) {
1592 putname(nd->last.name);
1597 putname(nd->last.name);
1601 down(&dir->d_inode->i_sem);
1602 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1603 putname(nd->last.name);
1608 * lookup_create - lookup a dentry, creating it if it doesn't exist
1609 * @nd: nameidata info
1610 * @is_dir: directory flag
1612 * Simple function to lookup and return a dentry and create it
1613 * if it doesn't exist. Is SMP-safe.
1615 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1617 struct dentry *dentry;
1620 down(&nd->dentry->d_inode->i_sem);
1622 if (nd->last_type != LAST_NORM)
1624 nd->flags &= ~LOOKUP_PARENT;
1625 dentry = lookup_hash(&nd->last, nd->dentry);
1628 error = mnt_may_create(nd->mnt, nd->dentry->d_inode, dentry);
1632 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1639 return ERR_PTR(error);
1642 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1644 int error = may_create(dir, dentry, NULL);
1649 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1652 if (!dir->i_op || !dir->i_op->mknod)
1655 error = security_inode_mknod(dir, dentry, mode, dev);
1660 error = dir->i_op->mknod(dir, dentry, mode, dev);
1662 inode_dir_notify(dir, DN_CREATE);
1663 security_inode_post_mknod(dir, dentry, mode, dev);
1668 asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
1672 struct dentry * dentry;
1673 struct nameidata nd;
1677 tmp = getname(filename);
1679 return PTR_ERR(tmp);
1681 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1684 dentry = lookup_create(&nd, 0);
1685 error = PTR_ERR(dentry);
1687 if (!IS_POSIXACL(nd.dentry->d_inode))
1688 mode &= ~current->fs->umask;
1689 if (!IS_ERR(dentry)) {
1690 switch (mode & S_IFMT) {
1691 case 0: case S_IFREG:
1692 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1694 case S_IFCHR: case S_IFBLK:
1695 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1696 new_decode_dev(dev));
1698 case S_IFIFO: case S_IFSOCK:
1699 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1709 up(&nd.dentry->d_inode->i_sem);
1717 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1719 int error = may_create(dir, dentry, NULL);
1724 if (!dir->i_op || !dir->i_op->mkdir)
1727 mode &= (S_IRWXUGO|S_ISVTX);
1728 error = security_inode_mkdir(dir, dentry, mode);
1733 error = dir->i_op->mkdir(dir, dentry, mode);
1735 inode_dir_notify(dir, DN_CREATE);
1736 security_inode_post_mkdir(dir,dentry, mode);
1741 asmlinkage long sys_mkdir(const char __user * pathname, int mode)
1746 tmp = getname(pathname);
1747 error = PTR_ERR(tmp);
1749 struct dentry *dentry;
1750 struct nameidata nd;
1752 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1755 dentry = lookup_create(&nd, 1);
1756 error = PTR_ERR(dentry);
1757 if (!IS_ERR(dentry)) {
1758 if (!IS_POSIXACL(nd.dentry->d_inode))
1759 mode &= ~current->fs->umask;
1760 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1763 up(&nd.dentry->d_inode->i_sem);
1773 * We try to drop the dentry early: we should have
1774 * a usage count of 2 if we're the only user of this
1775 * dentry, and if that is true (possibly after pruning
1776 * the dcache), then we drop the dentry now.
1778 * A low-level filesystem can, if it choses, legally
1781 * if (!d_unhashed(dentry))
1784 * if it cannot handle the case of removing a directory
1785 * that is still in use by something else..
1787 void dentry_unhash(struct dentry *dentry)
1790 spin_lock(&dcache_lock);
1791 switch (atomic_read(&dentry->d_count)) {
1793 spin_unlock(&dcache_lock);
1794 shrink_dcache_parent(dentry);
1795 spin_lock(&dcache_lock);
1796 if (atomic_read(&dentry->d_count) != 2)
1801 spin_unlock(&dcache_lock);
1804 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1806 int error = may_delete(dir, dentry, 1);
1811 if (!dir->i_op || !dir->i_op->rmdir)
1816 down(&dentry->d_inode->i_sem);
1817 dentry_unhash(dentry);
1818 if (d_mountpoint(dentry))
1821 error = security_inode_rmdir(dir, dentry);
1823 error = dir->i_op->rmdir(dir, dentry);
1825 dentry->d_inode->i_flags |= S_DEAD;
1828 up(&dentry->d_inode->i_sem);
1830 inode_dir_notify(dir, DN_DELETE);
1838 asmlinkage long sys_rmdir(const char __user * pathname)
1842 struct dentry *dentry;
1843 struct nameidata nd;
1845 name = getname(pathname);
1847 return PTR_ERR(name);
1849 error = path_lookup(name, LOOKUP_PARENT, &nd);
1853 switch(nd.last_type) {
1864 down(&nd.dentry->d_inode->i_sem);
1865 dentry = lookup_hash(&nd.last, nd.dentry);
1866 error = PTR_ERR(dentry);
1867 if (!IS_ERR(dentry)) {
1868 error = mnt_may_unlink(nd.mnt, nd.dentry->d_inode, dentry);
1871 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1875 up(&nd.dentry->d_inode->i_sem);
1883 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1885 int error = may_delete(dir, dentry, 0);
1890 if (!dir->i_op || !dir->i_op->unlink)
1895 down(&dentry->d_inode->i_sem);
1896 if (d_mountpoint(dentry))
1899 error = security_inode_unlink(dir, dentry);
1901 error = dir->i_op->unlink(dir, dentry);
1903 up(&dentry->d_inode->i_sem);
1905 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1906 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1908 inode_dir_notify(dir, DN_DELETE);
1914 * Make sure that the actual truncation of the file will occur outside its
1915 * directory's i_sem. Truncate can take a long time if there is a lot of
1916 * writeout happening, and we don't want to prevent access to the directory
1917 * while waiting on the I/O.
1919 asmlinkage long sys_unlink(const char __user * pathname)
1923 struct dentry *dentry;
1924 struct nameidata nd;
1925 struct inode *inode = NULL;
1927 name = getname(pathname);
1929 return PTR_ERR(name);
1931 error = path_lookup(name, LOOKUP_PARENT, &nd);
1935 if (nd.last_type != LAST_NORM)
1937 down(&nd.dentry->d_inode->i_sem);
1938 dentry = lookup_hash(&nd.last, nd.dentry);
1939 error = PTR_ERR(dentry);
1940 if (!IS_ERR(dentry)) {
1941 /* Why not before? Because we want correct error value */
1942 if (nd.last.name[nd.last.len])
1944 error = mnt_may_unlink(nd.mnt, nd.dentry->d_inode, dentry);
1947 inode = dentry->d_inode;
1949 atomic_inc(&inode->i_count);
1950 error = vfs_unlink(nd.dentry->d_inode, dentry);
1954 up(&nd.dentry->d_inode->i_sem);
1956 iput(inode); /* truncate the inode here */
1964 error = !dentry->d_inode ? -ENOENT :
1965 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1969 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
1971 int error = may_create(dir, dentry, NULL);
1976 if (!dir->i_op || !dir->i_op->symlink)
1979 error = security_inode_symlink(dir, dentry, oldname);
1984 error = dir->i_op->symlink(dir, dentry, oldname);
1986 inode_dir_notify(dir, DN_CREATE);
1987 security_inode_post_symlink(dir, dentry, oldname);
1992 asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
1998 from = getname(oldname);
2000 return PTR_ERR(from);
2001 to = getname(newname);
2002 error = PTR_ERR(to);
2004 struct dentry *dentry;
2005 struct nameidata nd;
2007 error = path_lookup(to, LOOKUP_PARENT, &nd);
2010 dentry = lookup_create(&nd, 0);
2011 error = PTR_ERR(dentry);
2012 if (!IS_ERR(dentry)) {
2013 error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
2016 up(&nd.dentry->d_inode->i_sem);
2025 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2027 struct inode *inode = old_dentry->d_inode;
2033 error = may_create(dir, new_dentry, NULL);
2037 if (dir->i_sb != inode->i_sb)
2041 * A link to an append-only or immutable file cannot be created.
2043 if (IS_APPEND(inode) || IS_IXORUNLINK(inode))
2045 if (!dir->i_op || !dir->i_op->link)
2047 if (S_ISDIR(old_dentry->d_inode->i_mode))
2050 error = security_inode_link(old_dentry, dir, new_dentry);
2054 down(&old_dentry->d_inode->i_sem);
2056 error = dir->i_op->link(old_dentry, dir, new_dentry);
2057 up(&old_dentry->d_inode->i_sem);
2059 inode_dir_notify(dir, DN_CREATE);
2060 security_inode_post_link(old_dentry, dir, new_dentry);
2066 * Hardlinks are often used in delicate situations. We avoid
2067 * security-related surprises by not following symlinks on the
2070 * We don't follow them on the oldname either to be compatible
2071 * with linux 2.0, and to avoid hard-linking to directories
2072 * and other special files. --ADM
2074 asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
2076 struct dentry *new_dentry;
2077 struct nameidata nd, old_nd;
2081 to = getname(newname);
2085 error = __user_walk(oldname, 0, &old_nd);
2088 error = path_lookup(to, LOOKUP_PARENT, &nd);
2092 * We allow hard-links to be created to a bind-mount as long
2093 * as the bind-mount is not read-only. Checking for cross-dev
2094 * links is subsumed by the superblock check in vfs_link().
2097 if (MNT_IS_RDONLY(old_nd.mnt))
2099 new_dentry = lookup_create(&nd, 0);
2100 error = PTR_ERR(new_dentry);
2101 if (!IS_ERR(new_dentry)) {
2102 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
2105 up(&nd.dentry->d_inode->i_sem);
2109 path_release(&old_nd);
2117 * The worst of all namespace operations - renaming directory. "Perverted"
2118 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2120 * a) we can get into loop creation. Check is done in is_subdir().
2121 * b) race potential - two innocent renames can create a loop together.
2122 * That's where 4.4 screws up. Current fix: serialization on
2123 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
2125 * c) we have to lock _three_ objects - parents and victim (if it exists).
2126 * And that - after we got ->i_sem on parents (until then we don't know
2127 * whether the target exists). Solution: try to be smart with locking
2128 * order for inodes. We rely on the fact that tree topology may change
2129 * only under ->s_vfs_rename_sem _and_ that parent of the object we
2130 * move will be locked. Thus we can rank directories by the tree
2131 * (ancestors first) and rank all non-directories after them.
2132 * That works since everybody except rename does "lock parent, lookup,
2133 * lock child" and rename is under ->s_vfs_rename_sem.
2134 * HOWEVER, it relies on the assumption that any object with ->lookup()
2135 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2136 * we'd better make sure that there's no link(2) for them.
2137 * d) some filesystems don't support opened-but-unlinked directories,
2138 * either because of layout or because they are not ready to deal with
2139 * all cases correctly. The latter will be fixed (taking this sort of
2140 * stuff into VFS), but the former is not going away. Solution: the same
2141 * trick as in rmdir().
2142 * e) conversion from fhandle to dentry may come in the wrong moment - when
2143 * we are removing the target. Solution: we will have to grab ->i_sem
2144 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2145 * ->i_sem on parents, which works but leads to some truely excessive
2148 int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2149 struct inode *new_dir, struct dentry *new_dentry)
2152 struct inode *target;
2155 * If we are going to change the parent - check write permissions,
2156 * we'll need to flip '..'.
2158 if (new_dir != old_dir) {
2159 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2164 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2168 target = new_dentry->d_inode;
2170 down(&target->i_sem);
2171 dentry_unhash(new_dentry);
2173 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2176 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2179 target->i_flags |= S_DEAD;
2181 if (d_unhashed(new_dentry))
2182 d_rehash(new_dentry);
2186 d_move(old_dentry,new_dentry);
2187 security_inode_post_rename(old_dir, old_dentry,
2188 new_dir, new_dentry);
2193 int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2194 struct inode *new_dir, struct dentry *new_dentry)
2196 struct inode *target;
2199 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2204 target = new_dentry->d_inode;
2206 down(&target->i_sem);
2207 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2210 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2212 /* The following d_move() should become unconditional */
2213 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
2214 d_move(old_dentry, new_dentry);
2215 security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
2223 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2224 struct inode *new_dir, struct dentry *new_dentry)
2227 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2229 if (old_dentry->d_inode == new_dentry->d_inode)
2232 error = may_delete(old_dir, old_dentry, is_dir);
2236 if (!new_dentry->d_inode)
2237 error = may_create(new_dir, new_dentry, NULL);
2239 error = may_delete(new_dir, new_dentry, is_dir);
2243 if (!old_dir->i_op || !old_dir->i_op->rename)
2246 DQUOT_INIT(old_dir);
2247 DQUOT_INIT(new_dir);
2250 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2252 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2254 if (old_dir == new_dir)
2255 inode_dir_notify(old_dir, DN_RENAME);
2257 inode_dir_notify(old_dir, DN_DELETE);
2258 inode_dir_notify(new_dir, DN_CREATE);
2264 static inline int do_rename(const char * oldname, const char * newname)
2267 struct dentry * old_dir, * new_dir;
2268 struct dentry * old_dentry, *new_dentry;
2269 struct dentry * trap;
2270 struct nameidata oldnd, newnd;
2272 error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2276 error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2281 if (oldnd.mnt != newnd.mnt)
2284 old_dir = oldnd.dentry;
2286 if (oldnd.last_type != LAST_NORM)
2289 new_dir = newnd.dentry;
2290 if (newnd.last_type != LAST_NORM)
2293 trap = lock_rename(new_dir, old_dir);
2295 old_dentry = lookup_hash(&oldnd.last, old_dir);
2296 error = PTR_ERR(old_dentry);
2297 if (IS_ERR(old_dentry))
2299 /* source must exist */
2301 if (!old_dentry->d_inode)
2303 /* unless the source is a directory trailing slashes give -ENOTDIR */
2304 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2306 if (oldnd.last.name[oldnd.last.len])
2308 if (newnd.last.name[newnd.last.len])
2311 /* source should not be ancestor of target */
2313 if (old_dentry == trap)
2316 if (MNT_IS_RDONLY(newnd.mnt))
2318 new_dentry = lookup_hash(&newnd.last, new_dir);
2319 error = PTR_ERR(new_dentry);
2320 if (IS_ERR(new_dentry))
2322 /* target should not be an ancestor of source */
2324 if (new_dentry == trap)
2327 error = vfs_rename(old_dir->d_inode, old_dentry,
2328 new_dir->d_inode, new_dentry);
2334 unlock_rename(new_dir, old_dir);
2336 path_release(&newnd);
2338 path_release(&oldnd);
2343 asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
2349 from = getname(oldname);
2351 return PTR_ERR(from);
2352 to = getname(newname);
2353 error = PTR_ERR(to);
2355 error = do_rename(from,to);
2362 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2366 len = PTR_ERR(link);
2371 if (len > (unsigned) buflen)
2373 if (copy_to_user(buffer, link, len))
2380 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2381 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2382 * using) it for any given inode is up to filesystem.
2384 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2386 struct nameidata nd;
2389 res = dentry->d_inode->i_op->follow_link(dentry, &nd);
2391 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2392 if (dentry->d_inode->i_op->put_link)
2393 dentry->d_inode->i_op->put_link(dentry, &nd);
2398 int vfs_follow_link(struct nameidata *nd, const char *link)
2400 return __vfs_follow_link(nd, link);
2403 /* get the link contents into pagecache */
2404 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2407 struct address_space *mapping = dentry->d_inode->i_mapping;
2408 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2412 wait_on_page_locked(page);
2413 if (!PageUptodate(page))
2419 page_cache_release(page);
2420 return ERR_PTR(-EIO);
2426 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2428 struct page *page = NULL;
2429 char *s = page_getlink(dentry, &page);
2430 int res = vfs_readlink(dentry,buffer,buflen,s);
2433 page_cache_release(page);
2438 int page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2441 nd_set_link(nd, page_getlink(dentry, &page));
2445 void page_put_link(struct dentry *dentry, struct nameidata *nd)
2447 if (!IS_ERR(nd_get_link(nd))) {
2449 page = find_get_page(dentry->d_inode->i_mapping, 0);
2453 page_cache_release(page);
2454 page_cache_release(page);
2458 int page_symlink(struct inode *inode, const char *symname, int len)
2460 struct address_space *mapping = inode->i_mapping;
2461 struct page *page = grab_cache_page(mapping, 0);
2467 err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2470 kaddr = kmap_atomic(page, KM_USER0);
2471 memcpy(kaddr, symname, len-1);
2472 kunmap_atomic(kaddr, KM_USER0);
2473 mapping->a_ops->commit_write(NULL, page, 0, len-1);
2475 * Notice that we are _not_ going to block here - end of page is
2476 * unmapped, so this will only try to map the rest of page, see
2477 * that it is unmapped (typically even will not look into inode -
2478 * ->i_size will be enough for everything) and zero it out.
2479 * OTOH it's obviously correct and should make the page up-to-date.
2481 if (!PageUptodate(page)) {
2482 err = mapping->a_ops->readpage(NULL, page);
2483 wait_on_page_locked(page);
2487 page_cache_release(page);
2490 mark_inode_dirty(inode);
2494 page_cache_release(page);
2499 struct inode_operations page_symlink_inode_operations = {
2500 .readlink = generic_readlink,
2501 .follow_link = page_follow_link_light,
2502 .put_link = page_put_link,
2505 EXPORT_SYMBOL(__user_walk);
2506 EXPORT_SYMBOL(follow_down);
2507 EXPORT_SYMBOL(follow_up);
2508 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2509 EXPORT_SYMBOL(getname);
2510 EXPORT_SYMBOL(lock_rename);
2511 EXPORT_SYMBOL(lookup_hash);
2512 EXPORT_SYMBOL(lookup_one_len);
2513 EXPORT_SYMBOL(page_follow_link_light);
2514 EXPORT_SYMBOL(page_put_link);
2515 EXPORT_SYMBOL(page_readlink);
2516 EXPORT_SYMBOL(page_symlink);
2517 EXPORT_SYMBOL(page_symlink_inode_operations);
2518 EXPORT_SYMBOL(path_lookup);
2519 EXPORT_SYMBOL(path_release);
2520 EXPORT_SYMBOL(path_walk);
2521 EXPORT_SYMBOL(permission);
2522 EXPORT_SYMBOL(unlock_rename);
2523 EXPORT_SYMBOL(vfs_create);
2524 EXPORT_SYMBOL(vfs_follow_link);
2525 EXPORT_SYMBOL(vfs_link);
2526 EXPORT_SYMBOL(vfs_mkdir);
2527 EXPORT_SYMBOL(vfs_mknod);
2528 EXPORT_SYMBOL(generic_permission);
2529 EXPORT_SYMBOL(vfs_readlink);
2530 EXPORT_SYMBOL(vfs_rename);
2531 EXPORT_SYMBOL(vfs_rmdir);
2532 EXPORT_SYMBOL(vfs_symlink);
2533 EXPORT_SYMBOL(vfs_unlink);
2534 EXPORT_SYMBOL(dentry_unhash);
2535 EXPORT_SYMBOL(generic_readlink);