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/fsnotify.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/file.h>
32 #include <asm/namei.h>
33 #include <asm/uaccess.h>
35 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
37 /* [Feb-1997 T. Schoebel-Theuer]
38 * Fundamental changes in the pathname lookup mechanisms (namei)
39 * were necessary because of omirr. The reason is that omirr needs
40 * to know the _real_ pathname, not the user-supplied one, in case
41 * of symlinks (and also when transname replacements occur).
43 * The new code replaces the old recursive symlink resolution with
44 * an iterative one (in case of non-nested symlink chains). It does
45 * this with calls to <fs>_follow_link().
46 * As a side effect, dir_namei(), _namei() and follow_link() are now
47 * replaced with a single function lookup_dentry() that can handle all
48 * the special cases of the former code.
50 * With the new dcache, the pathname is stored at each inode, at least as
51 * long as the refcount of the inode is positive. As a side effect, the
52 * size of the dcache depends on the inode cache and thus is dynamic.
54 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
55 * resolution to correspond with current state of the code.
57 * Note that the symlink resolution is not *completely* iterative.
58 * There is still a significant amount of tail- and mid- recursion in
59 * the algorithm. Also, note that <fs>_readlink() is not used in
60 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
61 * may return different results than <fs>_follow_link(). Many virtual
62 * filesystems (including /proc) exhibit this behavior.
65 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
66 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
67 * and the name already exists in form of a symlink, try to create the new
68 * name indicated by the symlink. The old code always complained that the
69 * name already exists, due to not following the symlink even if its target
70 * is nonexistent. The new semantics affects also mknod() and link() when
71 * the name is a symlink pointing to a non-existant name.
73 * I don't know which semantics is the right one, since I have no access
74 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
75 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
76 * "old" one. Personally, I think the new semantics is much more logical.
77 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
78 * file does succeed in both HP-UX and SunOs, but not in Solaris
79 * and in the old Linux semantics.
82 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
83 * semantics. See the comments in "open_namei" and "do_link" below.
85 * [10-Sep-98 Alan Modra] Another symlink change.
88 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
89 * inside the path - always follow.
90 * in the last component in creation/removal/renaming - never follow.
91 * if LOOKUP_FOLLOW passed - follow.
92 * if the pathname has trailing slashes - follow.
93 * otherwise - don't follow.
94 * (applied in that order).
96 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
97 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
98 * During the 2.4 we need to fix the userland stuff depending on it -
99 * hopefully we will be able to get rid of that wart in 2.5. So far only
100 * XEmacs seems to be relying on it...
103 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
104 * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives
105 * any extra contention...
108 /* In order to reduce some races, while at the same time doing additional
109 * checking and hopefully speeding things up, we copy filenames to the
110 * kernel data space before using them..
112 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
113 * PATH_MAX includes the nul terminator --RR.
115 static inline int do_getname(const char __user *filename, char *page)
118 unsigned long len = PATH_MAX;
120 if (!segment_eq(get_fs(), KERNEL_DS)) {
121 if ((unsigned long) filename >= TASK_SIZE)
123 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
124 len = TASK_SIZE - (unsigned long) filename;
127 retval = strncpy_from_user(page, filename, len);
131 return -ENAMETOOLONG;
137 char * getname(const char __user * filename)
141 result = ERR_PTR(-ENOMEM);
144 int retval = do_getname(filename, tmp);
149 result = ERR_PTR(retval);
152 audit_getname(result);
156 #ifdef CONFIG_AUDITSYSCALL
157 void putname(const char *name)
159 if (unlikely(current->audit_context))
164 EXPORT_SYMBOL(putname);
169 * generic_permission - check for access rights on a Posix-like filesystem
170 * @inode: inode to check access rights for
171 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
172 * @check_acl: optional callback to check for Posix ACLs
174 * Used to check for read/write/execute permissions on a file.
175 * We use "fsuid" for this, letting us set arbitrary permissions
176 * for filesystem access without changing the "normal" uids which
177 * are used for other things..
179 int generic_permission(struct inode *inode, int mask,
180 int (*check_acl)(struct inode *inode, int mask))
182 umode_t mode = inode->i_mode;
184 if (current->fsuid == inode->i_uid)
187 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
188 int error = check_acl(inode, mask);
189 if (error == -EACCES)
190 goto check_capabilities;
191 else if (error != -EAGAIN)
195 if (in_group_p(inode->i_gid))
200 * If the DACs are ok we don't need any capability check.
202 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
207 * Read/write DACs are always overridable.
208 * Executable DACs are overridable if at least one exec bit is set.
210 if (!(mask & MAY_EXEC) ||
211 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
212 if (capable(CAP_DAC_OVERRIDE))
216 * Searching includes executable on directories, else just read.
218 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
219 if (capable(CAP_DAC_READ_SEARCH))
225 int permission(struct inode *inode, int mask, struct nameidata *nd)
229 if (mask & MAY_WRITE) {
230 umode_t mode = inode->i_mode;
233 * Nobody gets write access to a read-only fs.
235 if (IS_RDONLY(inode) &&
236 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
240 * Nobody gets write access to an immutable file.
242 if (IS_IMMUTABLE(inode))
247 /* Ordinary permission routines do not understand MAY_APPEND. */
248 submask = mask & ~MAY_APPEND;
249 if (inode->i_op && inode->i_op->permission)
250 retval = inode->i_op->permission(inode, submask, nd);
252 retval = generic_permission(inode, submask, NULL);
256 return security_inode_permission(inode, mask, nd);
260 * vfs_permission - check for access rights to a given path
261 * @nd: lookup result that describes the path
262 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
264 * Used to check for read/write/execute permissions on a path.
265 * We use "fsuid" for this, letting us set arbitrary permissions
266 * for filesystem access without changing the "normal" uids which
267 * are used for other things.
269 int vfs_permission(struct nameidata *nd, int mask)
271 return permission(nd->dentry->d_inode, mask, nd);
275 * file_permission - check for additional access rights to a given file
276 * @file: file to check access rights for
277 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
279 * Used to check for read/write/execute permissions on an already opened
283 * Do not use this function in new code. All access checks should
284 * be done using vfs_permission().
286 int file_permission(struct file *file, int mask)
288 return permission(file->f_dentry->d_inode, mask, NULL);
292 * get_write_access() gets write permission for a file.
293 * put_write_access() releases this write permission.
294 * This is used for regular files.
295 * We cannot support write (and maybe mmap read-write shared) accesses and
296 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
297 * can have the following values:
298 * 0: no writers, no VM_DENYWRITE mappings
299 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
300 * > 0: (i_writecount) users are writing to the file.
302 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
303 * except for the cases where we don't hold i_writecount yet. Then we need to
304 * use {get,deny}_write_access() - these functions check the sign and refuse
305 * to do the change if sign is wrong. Exclusion between them is provided by
306 * the inode->i_lock spinlock.
309 int get_write_access(struct inode * inode)
311 spin_lock(&inode->i_lock);
312 if (atomic_read(&inode->i_writecount) < 0) {
313 spin_unlock(&inode->i_lock);
316 atomic_inc(&inode->i_writecount);
317 spin_unlock(&inode->i_lock);
322 int deny_write_access(struct file * file)
324 struct inode *inode = file->f_dentry->d_inode;
326 spin_lock(&inode->i_lock);
327 if (atomic_read(&inode->i_writecount) > 0) {
328 spin_unlock(&inode->i_lock);
331 atomic_dec(&inode->i_writecount);
332 spin_unlock(&inode->i_lock);
337 void path_release(struct nameidata *nd)
344 * umount() mustn't call path_release()/mntput() as that would clear
347 void path_release_on_umount(struct nameidata *nd)
350 mntput_no_expire(nd->mnt);
354 * release_open_intent - free up open intent resources
355 * @nd: pointer to nameidata
357 void release_open_intent(struct nameidata *nd)
359 if (nd->intent.open.file->f_dentry == NULL)
360 put_filp(nd->intent.open.file);
362 fput(nd->intent.open.file);
366 * Internal lookup() using the new generic dcache.
369 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
371 struct dentry * dentry = __d_lookup(parent, name);
373 /* lockess __d_lookup may fail due to concurrent d_move()
374 * in some unrelated directory, so try with d_lookup
377 dentry = d_lookup(parent, name);
379 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
380 if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
389 * Short-cut version of permission(), for calling by
390 * path_walk(), when dcache lock is held. Combines parts
391 * of permission() and generic_permission(), and tests ONLY for
392 * MAY_EXEC permission.
394 * If appropriate, check DAC only. If not appropriate, or
395 * short-cut DAC fails, then call permission() to do more
396 * complete permission check.
398 static inline int exec_permission_lite(struct inode *inode,
399 struct nameidata *nd)
401 umode_t mode = inode->i_mode;
403 if (inode->i_op && inode->i_op->permission)
406 if (current->fsuid == inode->i_uid)
408 else if (in_group_p(inode->i_gid))
414 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
417 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
420 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
425 return security_inode_permission(inode, MAY_EXEC, nd);
429 * This is called when everything else fails, and we actually have
430 * to go to the low-level filesystem to find out what we should do..
432 * We get the directory semaphore, and after getting that we also
433 * make sure that nobody added the entry to the dcache in the meantime..
436 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
438 struct dentry * result;
439 struct inode *dir = parent->d_inode;
443 * First re-do the cached lookup just in case it was created
444 * while we waited for the directory semaphore..
446 * FIXME! This could use version numbering or similar to
447 * avoid unnecessary cache lookups.
449 * The "dcache_lock" is purely to protect the RCU list walker
450 * from concurrent renames at this point (we mustn't get false
451 * negatives from the RCU list walk here, unlike the optimistic
454 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
456 result = d_lookup(parent, name);
458 struct dentry * dentry = d_alloc(parent, name);
459 result = ERR_PTR(-ENOMEM);
461 result = dir->i_op->lookup(dir, dentry, nd);
472 * Uhhuh! Nasty case: the cache was re-populated while
473 * we waited on the semaphore. Need to revalidate.
476 if (result->d_op && result->d_op->d_revalidate) {
477 if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
479 result = ERR_PTR(-ENOENT);
485 static int __emul_lookup_dentry(const char *, struct nameidata *);
489 walk_init_root(const char *name, struct nameidata *nd)
491 read_lock(¤t->fs->lock);
492 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
493 nd->mnt = mntget(current->fs->altrootmnt);
494 nd->dentry = dget(current->fs->altroot);
495 read_unlock(¤t->fs->lock);
496 if (__emul_lookup_dentry(name,nd))
498 read_lock(¤t->fs->lock);
500 nd->mnt = mntget(current->fs->rootmnt);
501 nd->dentry = dget(current->fs->root);
502 read_unlock(¤t->fs->lock);
506 static inline int __vfs_follow_link(struct nameidata *nd, const char *link)
515 if (!walk_init_root(link, nd))
516 /* weird __emul_prefix() stuff did it */
519 res = link_path_walk(link, nd);
521 if (nd->depth || res || nd->last_type!=LAST_NORM)
524 * If it is an iterative symlinks resolution in open_namei() we
525 * have to copy the last component. And all that crap because of
526 * bloody create() on broken symlinks. Furrfu...
529 if (unlikely(!name)) {
533 strcpy(name, nd->last.name);
534 nd->last.name = name;
538 return PTR_ERR(link);
542 struct vfsmount *mnt;
543 struct dentry *dentry;
546 static inline int __do_follow_link(struct path *path, struct nameidata *nd)
550 struct dentry *dentry = path->dentry;
552 touch_atime(path->mnt, dentry);
553 nd_set_link(nd, NULL);
555 if (path->mnt == nd->mnt)
557 cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
558 error = PTR_ERR(cookie);
559 if (!IS_ERR(cookie)) {
560 char *s = nd_get_link(nd);
563 error = __vfs_follow_link(nd, s);
564 if (dentry->d_inode->i_op->put_link)
565 dentry->d_inode->i_op->put_link(dentry, nd, cookie);
573 static inline void dput_path(struct path *path, struct nameidata *nd)
576 if (path->mnt != nd->mnt)
580 static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
583 if (nd->mnt != path->mnt)
586 nd->dentry = path->dentry;
590 * This limits recursive symlink follows to 8, while
591 * limiting consecutive symlinks to 40.
593 * Without that kind of total limit, nasty chains of consecutive
594 * symlinks can cause almost arbitrarily long lookups.
596 static inline int do_follow_link(struct path *path, struct nameidata *nd)
599 if (current->link_count >= MAX_NESTED_LINKS)
601 if (current->total_link_count >= 40)
603 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
605 err = security_inode_follow_link(path->dentry, nd);
608 current->link_count++;
609 current->total_link_count++;
611 err = __do_follow_link(path, nd);
612 current->link_count--;
621 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
623 struct vfsmount *parent;
624 struct dentry *mountpoint;
625 spin_lock(&vfsmount_lock);
626 parent=(*mnt)->mnt_parent;
627 if (parent == *mnt) {
628 spin_unlock(&vfsmount_lock);
632 mountpoint=dget((*mnt)->mnt_mountpoint);
633 spin_unlock(&vfsmount_lock);
635 *dentry = mountpoint;
641 /* no need for dcache_lock, as serialization is taken care in
644 static int __follow_mount(struct path *path)
647 while (d_mountpoint(path->dentry)) {
648 struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
655 path->dentry = dget(mounted->mnt_root);
661 static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
663 while (d_mountpoint(*dentry)) {
664 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
670 *dentry = dget(mounted->mnt_root);
674 /* no need for dcache_lock, as serialization is taken care in
677 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
679 struct vfsmount *mounted;
681 mounted = lookup_mnt(*mnt, *dentry);
686 *dentry = dget(mounted->mnt_root);
692 static inline void follow_dotdot(struct nameidata *nd)
695 struct vfsmount *parent;
696 struct dentry *old = nd->dentry;
698 read_lock(¤t->fs->lock);
699 if (nd->dentry == current->fs->root &&
700 nd->mnt == current->fs->rootmnt) {
701 read_unlock(¤t->fs->lock);
704 read_unlock(¤t->fs->lock);
705 spin_lock(&dcache_lock);
706 if (nd->dentry != nd->mnt->mnt_root) {
707 nd->dentry = dget(nd->dentry->d_parent);
708 spin_unlock(&dcache_lock);
712 spin_unlock(&dcache_lock);
713 spin_lock(&vfsmount_lock);
714 parent = nd->mnt->mnt_parent;
715 if (parent == nd->mnt) {
716 spin_unlock(&vfsmount_lock);
720 nd->dentry = dget(nd->mnt->mnt_mountpoint);
721 spin_unlock(&vfsmount_lock);
726 follow_mount(&nd->mnt, &nd->dentry);
730 * It's more convoluted than I'd like it to be, but... it's still fairly
731 * small and for now I'd prefer to have fast path as straight as possible.
732 * It _is_ time-critical.
734 static int do_lookup(struct nameidata *nd, struct qstr *name,
737 struct vfsmount *mnt = nd->mnt;
738 struct dentry *dentry = __d_lookup(nd->dentry, name);
742 if (dentry->d_op && dentry->d_op->d_revalidate)
743 goto need_revalidate;
746 path->dentry = dentry;
747 __follow_mount(path);
751 dentry = real_lookup(nd->dentry, name, nd);
757 if (dentry->d_op->d_revalidate(dentry, nd))
759 if (d_invalidate(dentry))
765 return PTR_ERR(dentry);
770 * This is the basic name resolution function, turning a pathname into
771 * the final dentry. We expect 'base' to be positive and a directory.
773 * Returns 0 and nd will have valid dentry and mnt on success.
774 * Returns error and drops reference to input namei data on failure.
776 static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
781 unsigned int lookup_flags = nd->flags;
788 inode = nd->dentry->d_inode;
790 lookup_flags = LOOKUP_FOLLOW;
792 /* At this point we know we have a real path component. */
798 nd->flags |= LOOKUP_CONTINUE;
799 err = exec_permission_lite(inode, nd);
801 err = vfs_permission(nd, MAY_EXEC);
806 c = *(const unsigned char *)name;
808 hash = init_name_hash();
811 hash = partial_name_hash(c, hash);
812 c = *(const unsigned char *)name;
813 } while (c && (c != '/'));
814 this.len = name - (const char *) this.name;
815 this.hash = end_name_hash(hash);
817 /* remove trailing slashes? */
820 while (*++name == '/');
822 goto last_with_slashes;
825 * "." and ".." are special - ".." especially so because it has
826 * to be able to know about the current root directory and
827 * parent relationships.
829 if (this.name[0] == '.') switch (this.len) {
833 if (this.name[1] != '.')
836 inode = nd->dentry->d_inode;
842 * See if the low-level filesystem might want
843 * to use its own hash..
845 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
846 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
850 /* This does the actual lookups.. */
851 err = do_lookup(nd, &this, &next);
856 inode = next.dentry->d_inode;
863 if (inode->i_op->follow_link) {
864 err = do_follow_link(&next, nd);
868 inode = nd->dentry->d_inode;
875 path_to_nameidata(&next, nd);
877 if (!inode->i_op->lookup)
880 /* here ends the main loop */
883 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
885 nd->flags &= ~LOOKUP_CONTINUE;
886 if (lookup_flags & LOOKUP_PARENT)
888 if (this.name[0] == '.') switch (this.len) {
892 if (this.name[1] != '.')
895 inode = nd->dentry->d_inode;
900 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
901 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
905 err = do_lookup(nd, &this, &next);
908 inode = next.dentry->d_inode;
909 if ((lookup_flags & LOOKUP_FOLLOW)
910 && inode && inode->i_op && inode->i_op->follow_link) {
911 err = do_follow_link(&next, nd);
914 inode = nd->dentry->d_inode;
916 path_to_nameidata(&next, nd);
920 if (lookup_flags & LOOKUP_DIRECTORY) {
922 if (!inode->i_op || !inode->i_op->lookup)
928 nd->last_type = LAST_NORM;
929 if (this.name[0] != '.')
932 nd->last_type = LAST_DOT;
933 else if (this.len == 2 && this.name[1] == '.')
934 nd->last_type = LAST_DOTDOT;
939 * We bypassed the ordinary revalidation routines.
940 * We may need to check the cached dentry for staleness.
942 if (nd->dentry && nd->dentry->d_sb &&
943 (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
945 /* Note: we do not d_invalidate() */
946 if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
952 dput_path(&next, nd);
961 * Wrapper to retry pathname resolution whenever the underlying
962 * file system returns an ESTALE.
964 * Retry the whole path once, forcing real lookup requests
965 * instead of relying on the dcache.
967 int fastcall link_path_walk(const char *name, struct nameidata *nd)
969 struct nameidata save = *nd;
972 /* make sure the stuff we saved doesn't go away */
976 result = __link_path_walk(name, nd);
977 if (result == -ESTALE) {
981 nd->flags |= LOOKUP_REVAL;
982 result = __link_path_walk(name, nd);
991 int fastcall path_walk(const char * name, struct nameidata *nd)
993 current->total_link_count = 0;
994 return link_path_walk(name, nd);
998 * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
999 * everything is done. Returns 0 and drops input nd, if lookup failed;
1001 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
1003 if (path_walk(name, nd))
1004 return 0; /* something went wrong... */
1006 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
1007 struct dentry *old_dentry = nd->dentry;
1008 struct vfsmount *old_mnt = nd->mnt;
1009 struct qstr last = nd->last;
1010 int last_type = nd->last_type;
1012 * NAME was not found in alternate root or it's a directory. Try to find
1013 * it in the normal root:
1015 nd->last_type = LAST_ROOT;
1016 read_lock(¤t->fs->lock);
1017 nd->mnt = mntget(current->fs->rootmnt);
1018 nd->dentry = dget(current->fs->root);
1019 read_unlock(¤t->fs->lock);
1020 if (path_walk(name, nd) == 0) {
1021 if (nd->dentry->d_inode) {
1028 nd->dentry = old_dentry;
1031 nd->last_type = last_type;
1036 void set_fs_altroot(void)
1038 char *emul = __emul_prefix();
1039 struct nameidata nd;
1040 struct vfsmount *mnt = NULL, *oldmnt;
1041 struct dentry *dentry = NULL, *olddentry;
1046 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
1052 write_lock(¤t->fs->lock);
1053 oldmnt = current->fs->altrootmnt;
1054 olddentry = current->fs->altroot;
1055 current->fs->altrootmnt = mnt;
1056 current->fs->altroot = dentry;
1057 write_unlock(¤t->fs->lock);
1064 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1065 int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
1069 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1073 read_lock(¤t->fs->lock);
1075 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
1076 nd->mnt = mntget(current->fs->altrootmnt);
1077 nd->dentry = dget(current->fs->altroot);
1078 read_unlock(¤t->fs->lock);
1079 if (__emul_lookup_dentry(name,nd))
1080 goto out; /* found in altroot */
1081 read_lock(¤t->fs->lock);
1083 nd->mnt = mntget(current->fs->rootmnt);
1084 nd->dentry = dget(current->fs->root);
1086 nd->mnt = mntget(current->fs->pwdmnt);
1087 nd->dentry = dget(current->fs->pwd);
1089 read_unlock(¤t->fs->lock);
1090 current->total_link_count = 0;
1091 retval = link_path_walk(name, nd);
1093 if (unlikely(current->audit_context
1094 && nd && nd->dentry && nd->dentry->d_inode))
1095 audit_inode(name, nd->dentry->d_inode, flags);
1099 static int __path_lookup_intent_open(const char *name, unsigned int lookup_flags,
1100 struct nameidata *nd, int open_flags, int create_mode)
1102 struct file *filp = get_empty_filp();
1107 nd->intent.open.file = filp;
1108 nd->intent.open.flags = open_flags;
1109 nd->intent.open.create_mode = create_mode;
1110 err = path_lookup(name, lookup_flags|LOOKUP_OPEN, nd);
1111 if (IS_ERR(nd->intent.open.file)) {
1113 err = PTR_ERR(nd->intent.open.file);
1116 } else if (err != 0)
1117 release_open_intent(nd);
1122 * path_lookup_open - lookup a file path with open intent
1123 * @name: pointer to file name
1124 * @lookup_flags: lookup intent flags
1125 * @nd: pointer to nameidata
1126 * @open_flags: open intent flags
1128 int path_lookup_open(const char *name, unsigned int lookup_flags,
1129 struct nameidata *nd, int open_flags)
1131 return __path_lookup_intent_open(name, lookup_flags, nd,
1136 * path_lookup_create - lookup a file path with open + create intent
1137 * @name: pointer to file name
1138 * @lookup_flags: lookup intent flags
1139 * @nd: pointer to nameidata
1140 * @open_flags: open intent flags
1141 * @create_mode: create intent flags
1143 int path_lookup_create(const char *name, unsigned int lookup_flags,
1144 struct nameidata *nd, int open_flags, int create_mode)
1146 return __path_lookup_intent_open(name, lookup_flags|LOOKUP_CREATE, nd,
1147 open_flags, create_mode);
1150 int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags,
1151 struct nameidata *nd, int open_flags)
1153 char *tmp = getname(name);
1154 int err = PTR_ERR(tmp);
1157 err = __path_lookup_intent_open(tmp, lookup_flags, nd, open_flags, 0);
1164 * Restricted form of lookup. Doesn't follow links, single-component only,
1165 * needs parent already locked. Doesn't follow mounts.
1168 static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
1170 struct dentry * dentry;
1171 struct inode *inode;
1174 inode = base->d_inode;
1175 err = permission(inode, MAY_EXEC, nd);
1176 dentry = ERR_PTR(err);
1181 * See if the low-level filesystem might want
1182 * to use its own hash..
1184 if (base->d_op && base->d_op->d_hash) {
1185 err = base->d_op->d_hash(base, name);
1186 dentry = ERR_PTR(err);
1191 dentry = cached_lookup(base, name, nd);
1193 struct dentry *new = d_alloc(base, name);
1194 dentry = ERR_PTR(-ENOMEM);
1197 dentry = inode->i_op->lookup(inode, new, nd);
1207 struct dentry * lookup_hash(struct nameidata *nd)
1209 return __lookup_hash(&nd->last, nd->dentry, nd);
1213 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
1224 hash = init_name_hash();
1226 c = *(const unsigned char *)name++;
1227 if (c == '/' || c == '\0')
1229 hash = partial_name_hash(c, hash);
1231 this.hash = end_name_hash(hash);
1233 return __lookup_hash(&this, base, NULL);
1235 return ERR_PTR(-EACCES);
1241 * is used by most simple commands to get the inode of a specified name.
1242 * Open, link etc use their own routines, but this is enough for things
1245 * namei exists in two versions: namei/lnamei. The only difference is
1246 * that namei follows links, while lnamei does not.
1249 int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1251 char *tmp = getname(name);
1252 int err = PTR_ERR(tmp);
1255 err = path_lookup(tmp, flags, nd);
1262 * It's inline, so penalty for filesystems that don't use sticky bit is
1265 static inline int check_sticky(struct inode *dir, struct inode *inode)
1267 if (!(dir->i_mode & S_ISVTX))
1269 if (inode->i_uid == current->fsuid)
1271 if (dir->i_uid == current->fsuid)
1273 return !capable(CAP_FOWNER);
1277 * Check whether we can remove a link victim from directory dir, check
1278 * whether the type of victim is right.
1279 * 1. We can't do it if dir is read-only (done in permission())
1280 * 2. We should have write and exec permissions on dir
1281 * 3. We can't remove anything from append-only dir
1282 * 4. We can't do anything with immutable dir (done in permission())
1283 * 5. If the sticky bit on dir is set we should either
1284 * a. be owner of dir, or
1285 * b. be owner of victim, or
1286 * c. have CAP_FOWNER capability
1287 * 6. If the victim is append-only or immutable we can't do antyhing with
1288 * links pointing to it.
1289 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1290 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1291 * 9. We can't remove a root or mountpoint.
1292 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1293 * nfs_async_unlink().
1295 static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1299 if (!victim->d_inode)
1302 BUG_ON(victim->d_parent->d_inode != dir);
1304 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1309 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1310 IS_IMMUTABLE(victim->d_inode))
1313 if (!S_ISDIR(victim->d_inode->i_mode))
1315 if (IS_ROOT(victim))
1317 } else if (S_ISDIR(victim->d_inode->i_mode))
1319 if (IS_DEADDIR(dir))
1321 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1326 /* Check whether we can create an object with dentry child in directory
1328 * 1. We can't do it if child already exists (open has special treatment for
1329 * this case, but since we are inlined it's OK)
1330 * 2. We can't do it if dir is read-only (done in permission())
1331 * 3. We should have write and exec permissions on dir
1332 * 4. We can't do it if dir is immutable (done in permission())
1334 static inline int may_create(struct inode *dir, struct dentry *child,
1335 struct nameidata *nd)
1339 if (IS_DEADDIR(dir))
1341 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1345 * O_DIRECTORY translates into forcing a directory lookup.
1347 static inline int lookup_flags(unsigned int f)
1349 unsigned long retval = LOOKUP_FOLLOW;
1352 retval &= ~LOOKUP_FOLLOW;
1354 if (f & O_DIRECTORY)
1355 retval |= LOOKUP_DIRECTORY;
1361 * p1 and p2 should be directories on the same fs.
1363 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1368 down(&p1->d_inode->i_sem);
1372 down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1374 for (p = p1; p->d_parent != p; p = p->d_parent) {
1375 if (p->d_parent == p2) {
1376 down(&p2->d_inode->i_sem);
1377 down(&p1->d_inode->i_sem);
1382 for (p = p2; p->d_parent != p; p = p->d_parent) {
1383 if (p->d_parent == p1) {
1384 down(&p1->d_inode->i_sem);
1385 down(&p2->d_inode->i_sem);
1390 down(&p1->d_inode->i_sem);
1391 down(&p2->d_inode->i_sem);
1395 void unlock_rename(struct dentry *p1, struct dentry *p2)
1397 up(&p1->d_inode->i_sem);
1399 up(&p2->d_inode->i_sem);
1400 up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1404 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1405 struct nameidata *nd)
1407 int error = may_create(dir, dentry, nd);
1412 if (!dir->i_op || !dir->i_op->create)
1413 return -EACCES; /* shouldn't it be ENOSYS? */
1416 error = security_inode_create(dir, dentry, mode);
1420 error = dir->i_op->create(dir, dentry, mode, nd);
1422 fsnotify_create(dir, dentry->d_name.name);
1426 int may_open(struct nameidata *nd, int acc_mode, int flag)
1428 struct dentry *dentry = nd->dentry;
1429 struct inode *inode = dentry->d_inode;
1435 if (S_ISLNK(inode->i_mode))
1438 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1441 error = vfs_permission(nd, acc_mode);
1446 * FIFO's, sockets and device files are special: they don't
1447 * actually live on the filesystem itself, and as such you
1448 * can write to them even if the filesystem is read-only.
1450 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1452 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1453 if (nd->mnt->mnt_flags & MNT_NODEV)
1457 } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
1460 * An append-only file must be opened in append mode for writing.
1462 if (IS_APPEND(inode)) {
1463 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1469 /* O_NOATIME can only be set by the owner or superuser */
1470 if (flag & O_NOATIME)
1471 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1475 * Ensure there are no outstanding leases on the file.
1477 error = break_lease(inode, flag);
1481 if (flag & O_TRUNC) {
1482 error = get_write_access(inode);
1487 * Refuse to truncate files with mandatory locks held on them.
1489 error = locks_verify_locked(inode);
1493 error = do_truncate(dentry, 0, NULL);
1495 put_write_access(inode);
1499 if (flag & FMODE_WRITE)
1508 * namei for open - this is in fact almost the whole open-routine.
1510 * Note that the low bits of "flag" aren't the same as in the open
1511 * system call - they are 00 - no permissions needed
1512 * 01 - read permission needed
1513 * 10 - write permission needed
1514 * 11 - read/write permissions needed
1515 * which is a lot more logical, and also allows the "no perm" needed
1516 * for symlinks (where the permissions are checked later).
1519 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1521 int acc_mode, error;
1526 acc_mode = ACC_MODE(flag);
1528 /* O_TRUNC implies we need access checks for write permissions */
1530 acc_mode |= MAY_WRITE;
1532 /* Allow the LSM permission hook to distinguish append
1533 access from general write access. */
1534 if (flag & O_APPEND)
1535 acc_mode |= MAY_APPEND;
1538 * The simplest case - just a plain lookup.
1540 if (!(flag & O_CREAT)) {
1541 error = path_lookup_open(pathname, lookup_flags(flag), nd, flag);
1548 * Create - we need to know the parent.
1550 error = path_lookup_create(pathname, LOOKUP_PARENT, nd, flag, mode);
1555 * We have the parent and last component. First of all, check
1556 * that we are not asked to creat(2) an obvious directory - that
1560 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1564 nd->flags &= ~LOOKUP_PARENT;
1565 down(&dir->d_inode->i_sem);
1566 path.dentry = lookup_hash(nd);
1570 error = PTR_ERR(path.dentry);
1571 if (IS_ERR(path.dentry)) {
1572 up(&dir->d_inode->i_sem);
1576 /* Negative dentry, just create the file */
1577 if (!path.dentry->d_inode) {
1578 if (!IS_POSIXACL(dir->d_inode))
1579 mode &= ~current->fs->umask;
1580 error = vfs_create(dir->d_inode, path.dentry, mode, nd);
1581 up(&dir->d_inode->i_sem);
1583 nd->dentry = path.dentry;
1586 /* Don't check for write permission, don't truncate */
1593 * It already exists.
1595 up(&dir->d_inode->i_sem);
1601 if (__follow_mount(&path)) {
1603 if (flag & O_NOFOLLOW)
1607 if (!path.dentry->d_inode)
1609 if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
1612 path_to_nameidata(&path, nd);
1614 if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
1617 error = may_open(nd, acc_mode, flag);
1623 dput_path(&path, nd);
1625 if (!IS_ERR(nd->intent.open.file))
1626 release_open_intent(nd);
1632 if (flag & O_NOFOLLOW)
1635 * This is subtle. Instead of calling do_follow_link() we do the
1636 * thing by hands. The reason is that this way we have zero link_count
1637 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1638 * After that we have the parent and last component, i.e.
1639 * we are in the same situation as after the first path_walk().
1640 * Well, almost - if the last component is normal we get its copy
1641 * stored in nd->last.name and we will have to putname() it when we
1642 * are done. Procfs-like symlinks just set LAST_BIND.
1644 nd->flags |= LOOKUP_PARENT;
1645 error = security_inode_follow_link(path.dentry, nd);
1648 error = __do_follow_link(&path, nd);
1651 nd->flags &= ~LOOKUP_PARENT;
1652 if (nd->last_type == LAST_BIND)
1655 if (nd->last_type != LAST_NORM)
1657 if (nd->last.name[nd->last.len]) {
1658 __putname(nd->last.name);
1663 __putname(nd->last.name);
1667 down(&dir->d_inode->i_sem);
1668 path.dentry = lookup_hash(nd);
1670 __putname(nd->last.name);
1675 * lookup_create - lookup a dentry, creating it if it doesn't exist
1676 * @nd: nameidata info
1677 * @is_dir: directory flag
1679 * Simple function to lookup and return a dentry and create it
1680 * if it doesn't exist. Is SMP-safe.
1682 * Returns with nd->dentry->d_inode->i_sem locked.
1684 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1686 struct dentry *dentry = ERR_PTR(-EEXIST);
1688 down(&nd->dentry->d_inode->i_sem);
1690 * Yucky last component or no last component at all?
1691 * (foo/., foo/.., /////)
1693 if (nd->last_type != LAST_NORM)
1695 nd->flags &= ~LOOKUP_PARENT;
1698 * Do the final lookup.
1700 dentry = lookup_hash(nd);
1705 * Special case - lookup gave negative, but... we had foo/bar/
1706 * From the vfs_mknod() POV we just have a negative dentry -
1707 * all is fine. Let's be bastards - you had / on the end, you've
1708 * been asking for (non-existent) directory. -ENOENT for you.
1710 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1715 dentry = ERR_PTR(-ENOENT);
1719 EXPORT_SYMBOL_GPL(lookup_create);
1721 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1723 int error = may_create(dir, dentry, NULL);
1728 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1731 if (!dir->i_op || !dir->i_op->mknod)
1734 error = security_inode_mknod(dir, dentry, mode, dev);
1739 error = dir->i_op->mknod(dir, dentry, mode, dev);
1741 fsnotify_create(dir, dentry->d_name.name);
1745 asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
1749 struct dentry * dentry;
1750 struct nameidata nd;
1754 tmp = getname(filename);
1756 return PTR_ERR(tmp);
1758 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1761 dentry = lookup_create(&nd, 0);
1762 error = PTR_ERR(dentry);
1764 if (!IS_POSIXACL(nd.dentry->d_inode))
1765 mode &= ~current->fs->umask;
1766 if (!IS_ERR(dentry)) {
1767 switch (mode & S_IFMT) {
1768 case 0: case S_IFREG:
1769 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1771 case S_IFCHR: case S_IFBLK:
1772 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1773 new_decode_dev(dev));
1775 case S_IFIFO: case S_IFSOCK:
1776 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1786 up(&nd.dentry->d_inode->i_sem);
1794 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1796 int error = may_create(dir, dentry, NULL);
1801 if (!dir->i_op || !dir->i_op->mkdir)
1804 mode &= (S_IRWXUGO|S_ISVTX);
1805 error = security_inode_mkdir(dir, dentry, mode);
1810 error = dir->i_op->mkdir(dir, dentry, mode);
1812 fsnotify_mkdir(dir, dentry->d_name.name);
1816 asmlinkage long sys_mkdir(const char __user * pathname, int mode)
1821 tmp = getname(pathname);
1822 error = PTR_ERR(tmp);
1824 struct dentry *dentry;
1825 struct nameidata nd;
1827 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1830 dentry = lookup_create(&nd, 1);
1831 error = PTR_ERR(dentry);
1832 if (!IS_ERR(dentry)) {
1833 if (!IS_POSIXACL(nd.dentry->d_inode))
1834 mode &= ~current->fs->umask;
1835 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1838 up(&nd.dentry->d_inode->i_sem);
1848 * We try to drop the dentry early: we should have
1849 * a usage count of 2 if we're the only user of this
1850 * dentry, and if that is true (possibly after pruning
1851 * the dcache), then we drop the dentry now.
1853 * A low-level filesystem can, if it choses, legally
1856 * if (!d_unhashed(dentry))
1859 * if it cannot handle the case of removing a directory
1860 * that is still in use by something else..
1862 void dentry_unhash(struct dentry *dentry)
1865 if (atomic_read(&dentry->d_count))
1866 shrink_dcache_parent(dentry);
1867 spin_lock(&dcache_lock);
1868 spin_lock(&dentry->d_lock);
1869 if (atomic_read(&dentry->d_count) == 2)
1871 spin_unlock(&dentry->d_lock);
1872 spin_unlock(&dcache_lock);
1875 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1877 int error = may_delete(dir, dentry, 1);
1882 if (!dir->i_op || !dir->i_op->rmdir)
1887 down(&dentry->d_inode->i_sem);
1888 dentry_unhash(dentry);
1889 if (d_mountpoint(dentry))
1892 error = security_inode_rmdir(dir, dentry);
1894 error = dir->i_op->rmdir(dir, dentry);
1896 dentry->d_inode->i_flags |= S_DEAD;
1899 up(&dentry->d_inode->i_sem);
1908 asmlinkage long sys_rmdir(const char __user * pathname)
1912 struct dentry *dentry;
1913 struct nameidata nd;
1915 name = getname(pathname);
1917 return PTR_ERR(name);
1919 error = path_lookup(name, LOOKUP_PARENT, &nd);
1923 switch(nd.last_type) {
1934 down(&nd.dentry->d_inode->i_sem);
1935 dentry = lookup_hash(&nd);
1936 error = PTR_ERR(dentry);
1937 if (!IS_ERR(dentry)) {
1938 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1941 up(&nd.dentry->d_inode->i_sem);
1949 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1951 int error = may_delete(dir, dentry, 0);
1956 if (!dir->i_op || !dir->i_op->unlink)
1961 down(&dentry->d_inode->i_sem);
1962 if (d_mountpoint(dentry))
1965 error = security_inode_unlink(dir, dentry);
1967 error = dir->i_op->unlink(dir, dentry);
1969 up(&dentry->d_inode->i_sem);
1971 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1972 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1980 * Make sure that the actual truncation of the file will occur outside its
1981 * directory's i_sem. Truncate can take a long time if there is a lot of
1982 * writeout happening, and we don't want to prevent access to the directory
1983 * while waiting on the I/O.
1985 asmlinkage long sys_unlink(const char __user * pathname)
1989 struct dentry *dentry;
1990 struct nameidata nd;
1991 struct inode *inode = NULL;
1993 name = getname(pathname);
1995 return PTR_ERR(name);
1997 error = path_lookup(name, LOOKUP_PARENT, &nd);
2001 if (nd.last_type != LAST_NORM)
2003 down(&nd.dentry->d_inode->i_sem);
2004 dentry = lookup_hash(&nd);
2005 error = PTR_ERR(dentry);
2006 if (!IS_ERR(dentry)) {
2007 /* Why not before? Because we want correct error value */
2008 if (nd.last.name[nd.last.len])
2010 inode = dentry->d_inode;
2012 atomic_inc(&inode->i_count);
2013 error = vfs_unlink(nd.dentry->d_inode, dentry);
2017 up(&nd.dentry->d_inode->i_sem);
2019 iput(inode); /* truncate the inode here */
2027 error = !dentry->d_inode ? -ENOENT :
2028 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2032 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
2034 int error = may_create(dir, dentry, NULL);
2039 if (!dir->i_op || !dir->i_op->symlink)
2042 error = security_inode_symlink(dir, dentry, oldname);
2047 error = dir->i_op->symlink(dir, dentry, oldname);
2049 fsnotify_create(dir, dentry->d_name.name);
2053 asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
2059 from = getname(oldname);
2061 return PTR_ERR(from);
2062 to = getname(newname);
2063 error = PTR_ERR(to);
2065 struct dentry *dentry;
2066 struct nameidata nd;
2068 error = path_lookup(to, LOOKUP_PARENT, &nd);
2071 dentry = lookup_create(&nd, 0);
2072 error = PTR_ERR(dentry);
2073 if (!IS_ERR(dentry)) {
2074 error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
2077 up(&nd.dentry->d_inode->i_sem);
2086 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2088 struct inode *inode = old_dentry->d_inode;
2094 error = may_create(dir, new_dentry, NULL);
2098 if (dir->i_sb != inode->i_sb)
2102 * A link to an append-only or immutable file cannot be created.
2104 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2106 if (!dir->i_op || !dir->i_op->link)
2108 if (S_ISDIR(old_dentry->d_inode->i_mode))
2111 error = security_inode_link(old_dentry, dir, new_dentry);
2115 down(&old_dentry->d_inode->i_sem);
2117 error = dir->i_op->link(old_dentry, dir, new_dentry);
2118 up(&old_dentry->d_inode->i_sem);
2120 fsnotify_create(dir, new_dentry->d_name.name);
2125 * Hardlinks are often used in delicate situations. We avoid
2126 * security-related surprises by not following symlinks on the
2129 * We don't follow them on the oldname either to be compatible
2130 * with linux 2.0, and to avoid hard-linking to directories
2131 * and other special files. --ADM
2133 asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
2135 struct dentry *new_dentry;
2136 struct nameidata nd, old_nd;
2140 to = getname(newname);
2144 error = __user_walk(oldname, 0, &old_nd);
2147 error = path_lookup(to, LOOKUP_PARENT, &nd);
2151 if (old_nd.mnt != nd.mnt)
2153 new_dentry = lookup_create(&nd, 0);
2154 error = PTR_ERR(new_dentry);
2155 if (!IS_ERR(new_dentry)) {
2156 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
2159 up(&nd.dentry->d_inode->i_sem);
2163 path_release(&old_nd);
2171 * The worst of all namespace operations - renaming directory. "Perverted"
2172 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2174 * a) we can get into loop creation. Check is done in is_subdir().
2175 * b) race potential - two innocent renames can create a loop together.
2176 * That's where 4.4 screws up. Current fix: serialization on
2177 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
2179 * c) we have to lock _three_ objects - parents and victim (if it exists).
2180 * And that - after we got ->i_sem on parents (until then we don't know
2181 * whether the target exists). Solution: try to be smart with locking
2182 * order for inodes. We rely on the fact that tree topology may change
2183 * only under ->s_vfs_rename_sem _and_ that parent of the object we
2184 * move will be locked. Thus we can rank directories by the tree
2185 * (ancestors first) and rank all non-directories after them.
2186 * That works since everybody except rename does "lock parent, lookup,
2187 * lock child" and rename is under ->s_vfs_rename_sem.
2188 * HOWEVER, it relies on the assumption that any object with ->lookup()
2189 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2190 * we'd better make sure that there's no link(2) for them.
2191 * d) some filesystems don't support opened-but-unlinked directories,
2192 * either because of layout or because they are not ready to deal with
2193 * all cases correctly. The latter will be fixed (taking this sort of
2194 * stuff into VFS), but the former is not going away. Solution: the same
2195 * trick as in rmdir().
2196 * e) conversion from fhandle to dentry may come in the wrong moment - when
2197 * we are removing the target. Solution: we will have to grab ->i_sem
2198 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2199 * ->i_sem on parents, which works but leads to some truely excessive
2202 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2203 struct inode *new_dir, struct dentry *new_dentry)
2206 struct inode *target;
2209 * If we are going to change the parent - check write permissions,
2210 * we'll need to flip '..'.
2212 if (new_dir != old_dir) {
2213 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2218 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2222 target = new_dentry->d_inode;
2224 down(&target->i_sem);
2225 dentry_unhash(new_dentry);
2227 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2230 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2233 target->i_flags |= S_DEAD;
2235 if (d_unhashed(new_dentry))
2236 d_rehash(new_dentry);
2240 d_move(old_dentry,new_dentry);
2244 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2245 struct inode *new_dir, struct dentry *new_dentry)
2247 struct inode *target;
2250 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2255 target = new_dentry->d_inode;
2257 down(&target->i_sem);
2258 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2261 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2263 /* The following d_move() should become unconditional */
2264 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
2265 d_move(old_dentry, new_dentry);
2273 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2274 struct inode *new_dir, struct dentry *new_dentry)
2277 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2278 const char *old_name;
2280 if (old_dentry->d_inode == new_dentry->d_inode)
2283 error = may_delete(old_dir, old_dentry, is_dir);
2287 if (!new_dentry->d_inode)
2288 error = may_create(new_dir, new_dentry, NULL);
2290 error = may_delete(new_dir, new_dentry, is_dir);
2294 if (!old_dir->i_op || !old_dir->i_op->rename)
2297 DQUOT_INIT(old_dir);
2298 DQUOT_INIT(new_dir);
2300 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
2303 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2305 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2307 const char *new_name = old_dentry->d_name.name;
2308 fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
2309 new_dentry->d_inode, old_dentry->d_inode);
2311 fsnotify_oldname_free(old_name);
2316 static inline int do_rename(const char * oldname, const char * newname)
2319 struct dentry * old_dir, * new_dir;
2320 struct dentry * old_dentry, *new_dentry;
2321 struct dentry * trap;
2322 struct nameidata oldnd, newnd;
2324 error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2328 error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2333 if (oldnd.mnt != newnd.mnt)
2336 old_dir = oldnd.dentry;
2338 if (oldnd.last_type != LAST_NORM)
2341 new_dir = newnd.dentry;
2342 if (newnd.last_type != LAST_NORM)
2345 trap = lock_rename(new_dir, old_dir);
2347 old_dentry = lookup_hash(&oldnd);
2348 error = PTR_ERR(old_dentry);
2349 if (IS_ERR(old_dentry))
2351 /* source must exist */
2353 if (!old_dentry->d_inode)
2355 /* unless the source is a directory trailing slashes give -ENOTDIR */
2356 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2358 if (oldnd.last.name[oldnd.last.len])
2360 if (newnd.last.name[newnd.last.len])
2363 /* source should not be ancestor of target */
2365 if (old_dentry == trap)
2367 new_dentry = lookup_hash(&newnd);
2368 error = PTR_ERR(new_dentry);
2369 if (IS_ERR(new_dentry))
2371 /* target should not be an ancestor of source */
2373 if (new_dentry == trap)
2376 error = vfs_rename(old_dir->d_inode, old_dentry,
2377 new_dir->d_inode, new_dentry);
2383 unlock_rename(new_dir, old_dir);
2385 path_release(&newnd);
2387 path_release(&oldnd);
2392 asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
2398 from = getname(oldname);
2400 return PTR_ERR(from);
2401 to = getname(newname);
2402 error = PTR_ERR(to);
2404 error = do_rename(from,to);
2411 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2415 len = PTR_ERR(link);
2420 if (len > (unsigned) buflen)
2422 if (copy_to_user(buffer, link, len))
2429 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2430 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2431 * using) it for any given inode is up to filesystem.
2433 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2435 struct nameidata nd;
2439 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
2440 if (!IS_ERR(cookie)) {
2441 int res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2442 if (dentry->d_inode->i_op->put_link)
2443 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2444 cookie = ERR_PTR(res);
2446 return PTR_ERR(cookie);
2449 int vfs_follow_link(struct nameidata *nd, const char *link)
2451 return __vfs_follow_link(nd, link);
2454 /* get the link contents into pagecache */
2455 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2458 struct address_space *mapping = dentry->d_inode->i_mapping;
2459 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2463 wait_on_page_locked(page);
2464 if (!PageUptodate(page))
2470 page_cache_release(page);
2471 return ERR_PTR(-EIO);
2477 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2479 struct page *page = NULL;
2480 char *s = page_getlink(dentry, &page);
2481 int res = vfs_readlink(dentry,buffer,buflen,s);
2484 page_cache_release(page);
2489 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2491 struct page *page = NULL;
2492 nd_set_link(nd, page_getlink(dentry, &page));
2496 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
2498 struct page *page = cookie;
2502 page_cache_release(page);
2506 int page_symlink(struct inode *inode, const char *symname, int len)
2508 struct address_space *mapping = inode->i_mapping;
2509 struct page *page = grab_cache_page(mapping, 0);
2515 err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2518 kaddr = kmap_atomic(page, KM_USER0);
2519 memcpy(kaddr, symname, len-1);
2520 kunmap_atomic(kaddr, KM_USER0);
2521 mapping->a_ops->commit_write(NULL, page, 0, len-1);
2523 * Notice that we are _not_ going to block here - end of page is
2524 * unmapped, so this will only try to map the rest of page, see
2525 * that it is unmapped (typically even will not look into inode -
2526 * ->i_size will be enough for everything) and zero it out.
2527 * OTOH it's obviously correct and should make the page up-to-date.
2529 if (!PageUptodate(page)) {
2530 err = mapping->a_ops->readpage(NULL, page);
2531 wait_on_page_locked(page);
2535 page_cache_release(page);
2538 mark_inode_dirty(inode);
2542 page_cache_release(page);
2547 struct inode_operations page_symlink_inode_operations = {
2548 .readlink = generic_readlink,
2549 .follow_link = page_follow_link_light,
2550 .put_link = page_put_link,
2553 EXPORT_SYMBOL(__user_walk);
2554 EXPORT_SYMBOL(follow_down);
2555 EXPORT_SYMBOL(follow_up);
2556 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2557 EXPORT_SYMBOL(getname);
2558 EXPORT_SYMBOL(lock_rename);
2559 EXPORT_SYMBOL(lookup_hash);
2560 EXPORT_SYMBOL(lookup_one_len);
2561 EXPORT_SYMBOL(page_follow_link_light);
2562 EXPORT_SYMBOL(page_put_link);
2563 EXPORT_SYMBOL(page_readlink);
2564 EXPORT_SYMBOL(page_symlink);
2565 EXPORT_SYMBOL(page_symlink_inode_operations);
2566 EXPORT_SYMBOL(path_lookup);
2567 EXPORT_SYMBOL(path_release);
2568 EXPORT_SYMBOL(path_walk);
2569 EXPORT_SYMBOL(permission);
2570 EXPORT_SYMBOL(vfs_permission);
2571 EXPORT_SYMBOL(file_permission);
2572 EXPORT_SYMBOL(unlock_rename);
2573 EXPORT_SYMBOL(vfs_create);
2574 EXPORT_SYMBOL(vfs_follow_link);
2575 EXPORT_SYMBOL(vfs_link);
2576 EXPORT_SYMBOL(vfs_mkdir);
2577 EXPORT_SYMBOL(vfs_mknod);
2578 EXPORT_SYMBOL(generic_permission);
2579 EXPORT_SYMBOL(vfs_readlink);
2580 EXPORT_SYMBOL(vfs_rename);
2581 EXPORT_SYMBOL(vfs_rmdir);
2582 EXPORT_SYMBOL(vfs_symlink);
2583 EXPORT_SYMBOL(vfs_unlink);
2584 EXPORT_SYMBOL(dentry_unhash);
2585 EXPORT_SYMBOL(generic_readlink);