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/export.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
22 #include <linux/namei.h>
23 #include <linux/pagemap.h>
24 #include <linux/fsnotify.h>
25 #include <linux/personality.h>
26 #include <linux/security.h>
27 #include <linux/ima.h>
28 #include <linux/syscalls.h>
29 #include <linux/mount.h>
30 #include <linux/audit.h>
31 #include <linux/capability.h>
32 #include <linux/file.h>
33 #include <linux/fcntl.h>
34 #include <linux/device_cgroup.h>
35 #include <linux/fs_struct.h>
36 #include <linux/posix_acl.h>
37 #include <asm/uaccess.h>
42 /* [Feb-1997 T. Schoebel-Theuer]
43 * Fundamental changes in the pathname lookup mechanisms (namei)
44 * were necessary because of omirr. The reason is that omirr needs
45 * to know the _real_ pathname, not the user-supplied one, in case
46 * of symlinks (and also when transname replacements occur).
48 * The new code replaces the old recursive symlink resolution with
49 * an iterative one (in case of non-nested symlink chains). It does
50 * this with calls to <fs>_follow_link().
51 * As a side effect, dir_namei(), _namei() and follow_link() are now
52 * replaced with a single function lookup_dentry() that can handle all
53 * the special cases of the former code.
55 * With the new dcache, the pathname is stored at each inode, at least as
56 * long as the refcount of the inode is positive. As a side effect, the
57 * size of the dcache depends on the inode cache and thus is dynamic.
59 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
60 * resolution to correspond with current state of the code.
62 * Note that the symlink resolution is not *completely* iterative.
63 * There is still a significant amount of tail- and mid- recursion in
64 * the algorithm. Also, note that <fs>_readlink() is not used in
65 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
66 * may return different results than <fs>_follow_link(). Many virtual
67 * filesystems (including /proc) exhibit this behavior.
70 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
71 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
72 * and the name already exists in form of a symlink, try to create the new
73 * name indicated by the symlink. The old code always complained that the
74 * name already exists, due to not following the symlink even if its target
75 * is nonexistent. The new semantics affects also mknod() and link() when
76 * the name is a symlink pointing to a non-existent name.
78 * I don't know which semantics is the right one, since I have no access
79 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
80 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
81 * "old" one. Personally, I think the new semantics is much more logical.
82 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
83 * file does succeed in both HP-UX and SunOs, but not in Solaris
84 * and in the old Linux semantics.
87 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
88 * semantics. See the comments in "open_namei" and "do_link" below.
90 * [10-Sep-98 Alan Modra] Another symlink change.
93 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
94 * inside the path - always follow.
95 * in the last component in creation/removal/renaming - never follow.
96 * if LOOKUP_FOLLOW passed - follow.
97 * if the pathname has trailing slashes - follow.
98 * otherwise - don't follow.
99 * (applied in that order).
101 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
102 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
103 * During the 2.4 we need to fix the userland stuff depending on it -
104 * hopefully we will be able to get rid of that wart in 2.5. So far only
105 * XEmacs seems to be relying on it...
108 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
109 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
110 * any extra contention...
113 /* In order to reduce some races, while at the same time doing additional
114 * checking and hopefully speeding things up, we copy filenames to the
115 * kernel data space before using them..
117 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
118 * PATH_MAX includes the nul terminator --RR.
120 static char *getname_flags(const char __user *filename, int flags, int *empty)
122 char *result = __getname(), *err;
125 if (unlikely(!result))
126 return ERR_PTR(-ENOMEM);
128 len = strncpy_from_user(result, filename, PATH_MAX);
130 if (unlikely(len < 0))
133 /* The empty path is special. */
134 if (unlikely(!len)) {
137 err = ERR_PTR(-ENOENT);
138 if (!(flags & LOOKUP_EMPTY))
142 err = ERR_PTR(-ENAMETOOLONG);
143 if (likely(len < PATH_MAX)) {
144 audit_getname(result);
153 char *getname(const char __user * filename)
155 return getname_flags(filename, 0, NULL);
158 #ifdef CONFIG_AUDITSYSCALL
159 void putname(const char *name)
161 if (unlikely(!audit_dummy_context()))
166 EXPORT_SYMBOL(putname);
169 static int check_acl(struct inode *inode, int mask)
171 #ifdef CONFIG_FS_POSIX_ACL
172 struct posix_acl *acl;
174 if (mask & MAY_NOT_BLOCK) {
175 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
178 /* no ->get_acl() calls in RCU mode... */
179 if (acl == ACL_NOT_CACHED)
181 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
184 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
187 * A filesystem can force a ACL callback by just never filling the
188 * ACL cache. But normally you'd fill the cache either at inode
189 * instantiation time, or on the first ->get_acl call.
191 * If the filesystem doesn't have a get_acl() function at all, we'll
192 * just create the negative cache entry.
194 if (acl == ACL_NOT_CACHED) {
195 if (inode->i_op->get_acl) {
196 acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
200 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
206 int error = posix_acl_permission(inode, acl, mask);
207 posix_acl_release(acl);
216 * This does the basic permission checking
218 static int acl_permission_check(struct inode *inode, int mask)
220 unsigned int mode = inode->i_mode;
222 if (current_user_ns() != inode_userns(inode))
225 if (likely(current_fsuid() == inode->i_uid))
228 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
229 int error = check_acl(inode, mask);
230 if (error != -EAGAIN)
234 if (in_group_p(inode->i_gid))
240 * If the DACs are ok we don't need any capability check.
242 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
248 * generic_permission - check for access rights on a Posix-like filesystem
249 * @inode: inode to check access rights for
250 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
252 * Used to check for read/write/execute permissions on a file.
253 * We use "fsuid" for this, letting us set arbitrary permissions
254 * for filesystem access without changing the "normal" uids which
255 * are used for other things.
257 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
258 * request cannot be satisfied (eg. requires blocking or too much complexity).
259 * It would then be called again in ref-walk mode.
261 int generic_permission(struct inode *inode, int mask)
266 * Do the basic permission checks.
268 ret = acl_permission_check(inode, mask);
272 if (S_ISDIR(inode->i_mode)) {
273 /* DACs are overridable for directories */
274 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
276 if (!(mask & MAY_WRITE))
277 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
282 * Read/write DACs are always overridable.
283 * Executable DACs are overridable when there is
284 * at least one exec bit set.
286 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
287 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
291 * Searching includes executable on directories, else just read.
293 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
294 if (mask == MAY_READ)
295 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
302 * We _really_ want to just do "generic_permission()" without
303 * even looking at the inode->i_op values. So we keep a cache
304 * flag in inode->i_opflags, that says "this has not special
305 * permission function, use the fast case".
307 static inline int do_inode_permission(struct inode *inode, int mask)
309 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
310 if (likely(inode->i_op->permission))
311 return inode->i_op->permission(inode, mask);
313 /* This gets set once for the inode lifetime */
314 spin_lock(&inode->i_lock);
315 inode->i_opflags |= IOP_FASTPERM;
316 spin_unlock(&inode->i_lock);
318 return generic_permission(inode, mask);
322 * inode_permission - check for access rights to a given inode
323 * @inode: inode to check permission on
324 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
326 * Used to check for read/write/execute permissions on an inode.
327 * We use "fsuid" for this, letting us set arbitrary permissions
328 * for filesystem access without changing the "normal" uids which
329 * are used for other things.
331 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
333 int inode_permission(struct inode *inode, int mask)
337 if (unlikely(mask & MAY_WRITE)) {
338 umode_t mode = inode->i_mode;
341 * Nobody gets write access to a read-only fs.
343 if (IS_RDONLY(inode) &&
344 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
348 * Nobody gets write access to an immutable file.
350 if (IS_IMMUTABLE(inode))
354 retval = do_inode_permission(inode, mask);
358 retval = devcgroup_inode_permission(inode, mask);
362 return security_inode_permission(inode, mask);
366 * path_get - get a reference to a path
367 * @path: path to get the reference to
369 * Given a path increment the reference count to the dentry and the vfsmount.
371 void path_get(struct path *path)
376 EXPORT_SYMBOL(path_get);
379 * path_put - put a reference to a path
380 * @path: path to put the reference to
382 * Given a path decrement the reference count to the dentry and the vfsmount.
384 void path_put(struct path *path)
389 EXPORT_SYMBOL(path_put);
392 * Path walking has 2 modes, rcu-walk and ref-walk (see
393 * Documentation/filesystems/path-lookup.txt). In situations when we can't
394 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
395 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
396 * mode. Refcounts are grabbed at the last known good point before rcu-walk
397 * got stuck, so ref-walk may continue from there. If this is not successful
398 * (eg. a seqcount has changed), then failure is returned and it's up to caller
399 * to restart the path walk from the beginning in ref-walk mode.
403 * unlazy_walk - try to switch to ref-walk mode.
404 * @nd: nameidata pathwalk data
405 * @dentry: child of nd->path.dentry or NULL
406 * Returns: 0 on success, -ECHILD on failure
408 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
409 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
410 * @nd or NULL. Must be called from rcu-walk context.
412 static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
414 struct fs_struct *fs = current->fs;
415 struct dentry *parent = nd->path.dentry;
418 BUG_ON(!(nd->flags & LOOKUP_RCU));
419 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
421 spin_lock(&fs->lock);
422 if (nd->root.mnt != fs->root.mnt ||
423 nd->root.dentry != fs->root.dentry)
426 spin_lock(&parent->d_lock);
428 if (!__d_rcu_to_refcount(parent, nd->seq))
430 BUG_ON(nd->inode != parent->d_inode);
432 if (dentry->d_parent != parent)
434 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
435 if (!__d_rcu_to_refcount(dentry, nd->seq))
438 * If the sequence check on the child dentry passed, then
439 * the child has not been removed from its parent. This
440 * means the parent dentry must be valid and able to take
441 * a reference at this point.
443 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
444 BUG_ON(!parent->d_count);
446 spin_unlock(&dentry->d_lock);
448 spin_unlock(&parent->d_lock);
451 spin_unlock(&fs->lock);
453 mntget(nd->path.mnt);
456 br_read_unlock(vfsmount_lock);
457 nd->flags &= ~LOOKUP_RCU;
461 spin_unlock(&dentry->d_lock);
463 spin_unlock(&parent->d_lock);
466 spin_unlock(&fs->lock);
471 * release_open_intent - free up open intent resources
472 * @nd: pointer to nameidata
474 void release_open_intent(struct nameidata *nd)
476 struct file *file = nd->intent.open.file;
478 if (file && !IS_ERR(file)) {
479 if (file->f_path.dentry == NULL)
486 static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
488 return dentry->d_op->d_revalidate(dentry, nd);
492 * complete_walk - successful completion of path walk
493 * @nd: pointer nameidata
495 * If we had been in RCU mode, drop out of it and legitimize nd->path.
496 * Revalidate the final result, unless we'd already done that during
497 * the path walk or the filesystem doesn't ask for it. Return 0 on
498 * success, -error on failure. In case of failure caller does not
499 * need to drop nd->path.
501 static int complete_walk(struct nameidata *nd)
503 struct dentry *dentry = nd->path.dentry;
506 if (nd->flags & LOOKUP_RCU) {
507 nd->flags &= ~LOOKUP_RCU;
508 if (!(nd->flags & LOOKUP_ROOT))
510 spin_lock(&dentry->d_lock);
511 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
512 spin_unlock(&dentry->d_lock);
514 br_read_unlock(vfsmount_lock);
517 BUG_ON(nd->inode != dentry->d_inode);
518 spin_unlock(&dentry->d_lock);
519 mntget(nd->path.mnt);
521 br_read_unlock(vfsmount_lock);
524 if (likely(!(nd->flags & LOOKUP_JUMPED)))
527 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
530 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
533 /* Note: we do not d_invalidate() */
534 status = d_revalidate(dentry, nd);
545 static __always_inline void set_root(struct nameidata *nd)
548 get_fs_root(current->fs, &nd->root);
551 static int link_path_walk(const char *, struct nameidata *);
553 static __always_inline void set_root_rcu(struct nameidata *nd)
556 struct fs_struct *fs = current->fs;
560 seq = read_seqcount_begin(&fs->seq);
562 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
563 } while (read_seqcount_retry(&fs->seq, seq));
567 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
579 nd->flags |= LOOKUP_JUMPED;
581 nd->inode = nd->path.dentry->d_inode;
583 ret = link_path_walk(link, nd);
587 return PTR_ERR(link);
590 static void path_put_conditional(struct path *path, struct nameidata *nd)
593 if (path->mnt != nd->path.mnt)
597 static inline void path_to_nameidata(const struct path *path,
598 struct nameidata *nd)
600 if (!(nd->flags & LOOKUP_RCU)) {
601 dput(nd->path.dentry);
602 if (nd->path.mnt != path->mnt)
603 mntput(nd->path.mnt);
605 nd->path.mnt = path->mnt;
606 nd->path.dentry = path->dentry;
609 static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
611 struct inode *inode = link->dentry->d_inode;
612 if (!IS_ERR(cookie) && inode->i_op->put_link)
613 inode->i_op->put_link(link->dentry, nd, cookie);
617 static __always_inline int
618 follow_link(struct path *link, struct nameidata *nd, void **p)
621 struct dentry *dentry = link->dentry;
623 BUG_ON(nd->flags & LOOKUP_RCU);
625 if (link->mnt == nd->path.mnt)
628 if (unlikely(current->total_link_count >= 40)) {
629 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
634 current->total_link_count++;
637 nd_set_link(nd, NULL);
639 error = security_inode_follow_link(link->dentry, nd);
641 *p = ERR_PTR(error); /* no ->put_link(), please */
646 nd->last_type = LAST_BIND;
647 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
650 char *s = nd_get_link(nd);
653 error = __vfs_follow_link(nd, s);
654 else if (nd->last_type == LAST_BIND) {
655 nd->flags |= LOOKUP_JUMPED;
656 nd->inode = nd->path.dentry->d_inode;
657 if (nd->inode->i_op->follow_link) {
658 /* stepped on a _really_ weird one */
667 static int follow_up_rcu(struct path *path)
669 struct mount *mnt = real_mount(path->mnt);
670 struct mount *parent;
671 struct dentry *mountpoint;
673 parent = mnt->mnt_parent;
674 if (&parent->mnt == path->mnt)
676 mountpoint = mnt->mnt_mountpoint;
677 path->dentry = mountpoint;
678 path->mnt = &parent->mnt;
682 int follow_up(struct path *path)
684 struct mount *mnt = real_mount(path->mnt);
685 struct mount *parent;
686 struct dentry *mountpoint;
688 br_read_lock(vfsmount_lock);
689 parent = mnt->mnt_parent;
690 if (&parent->mnt == path->mnt) {
691 br_read_unlock(vfsmount_lock);
694 mntget(&parent->mnt);
695 mountpoint = dget(mnt->mnt_mountpoint);
696 br_read_unlock(vfsmount_lock);
698 path->dentry = mountpoint;
700 path->mnt = &parent->mnt;
705 * Perform an automount
706 * - return -EISDIR to tell follow_managed() to stop and return the path we
709 static int follow_automount(struct path *path, unsigned flags,
712 struct vfsmount *mnt;
715 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
718 /* We don't want to mount if someone's just doing a stat -
719 * unless they're stat'ing a directory and appended a '/' to
722 * We do, however, want to mount if someone wants to open or
723 * create a file of any type under the mountpoint, wants to
724 * traverse through the mountpoint or wants to open the
725 * mounted directory. Also, autofs may mark negative dentries
726 * as being automount points. These will need the attentions
727 * of the daemon to instantiate them before they can be used.
729 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
730 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
731 path->dentry->d_inode)
734 current->total_link_count++;
735 if (current->total_link_count >= 40)
738 mnt = path->dentry->d_op->d_automount(path);
741 * The filesystem is allowed to return -EISDIR here to indicate
742 * it doesn't want to automount. For instance, autofs would do
743 * this so that its userspace daemon can mount on this dentry.
745 * However, we can only permit this if it's a terminal point in
746 * the path being looked up; if it wasn't then the remainder of
747 * the path is inaccessible and we should say so.
749 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
754 if (!mnt) /* mount collision */
758 /* lock_mount() may release path->mnt on error */
762 err = finish_automount(mnt, path);
766 /* Someone else made a mount here whilst we were busy */
771 path->dentry = dget(mnt->mnt_root);
780 * Handle a dentry that is managed in some way.
781 * - Flagged for transit management (autofs)
782 * - Flagged as mountpoint
783 * - Flagged as automount point
785 * This may only be called in refwalk mode.
787 * Serialization is taken care of in namespace.c
789 static int follow_managed(struct path *path, unsigned flags)
791 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
793 bool need_mntput = false;
796 /* Given that we're not holding a lock here, we retain the value in a
797 * local variable for each dentry as we look at it so that we don't see
798 * the components of that value change under us */
799 while (managed = ACCESS_ONCE(path->dentry->d_flags),
800 managed &= DCACHE_MANAGED_DENTRY,
801 unlikely(managed != 0)) {
802 /* Allow the filesystem to manage the transit without i_mutex
804 if (managed & DCACHE_MANAGE_TRANSIT) {
805 BUG_ON(!path->dentry->d_op);
806 BUG_ON(!path->dentry->d_op->d_manage);
807 ret = path->dentry->d_op->d_manage(path->dentry, false);
812 /* Transit to a mounted filesystem. */
813 if (managed & DCACHE_MOUNTED) {
814 struct vfsmount *mounted = lookup_mnt(path);
820 path->dentry = dget(mounted->mnt_root);
825 /* Something is mounted on this dentry in another
826 * namespace and/or whatever was mounted there in this
827 * namespace got unmounted before we managed to get the
831 /* Handle an automount point */
832 if (managed & DCACHE_NEED_AUTOMOUNT) {
833 ret = follow_automount(path, flags, &need_mntput);
839 /* We didn't change the current path point */
843 if (need_mntput && path->mnt == mnt)
847 return ret < 0 ? ret : need_mntput;
850 int follow_down_one(struct path *path)
852 struct vfsmount *mounted;
854 mounted = lookup_mnt(path);
859 path->dentry = dget(mounted->mnt_root);
865 static inline bool managed_dentry_might_block(struct dentry *dentry)
867 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
868 dentry->d_op->d_manage(dentry, true) < 0);
872 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
873 * we meet a managed dentry that would need blocking.
875 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
876 struct inode **inode)
879 struct mount *mounted;
881 * Don't forget we might have a non-mountpoint managed dentry
882 * that wants to block transit.
884 if (unlikely(managed_dentry_might_block(path->dentry)))
887 if (!d_mountpoint(path->dentry))
890 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
893 path->mnt = &mounted->mnt;
894 path->dentry = mounted->mnt.mnt_root;
895 nd->flags |= LOOKUP_JUMPED;
896 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
898 * Update the inode too. We don't need to re-check the
899 * dentry sequence number here after this d_inode read,
900 * because a mount-point is always pinned.
902 *inode = path->dentry->d_inode;
907 static void follow_mount_rcu(struct nameidata *nd)
909 while (d_mountpoint(nd->path.dentry)) {
910 struct mount *mounted;
911 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
914 nd->path.mnt = &mounted->mnt;
915 nd->path.dentry = mounted->mnt.mnt_root;
916 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
920 static int follow_dotdot_rcu(struct nameidata *nd)
925 if (nd->path.dentry == nd->root.dentry &&
926 nd->path.mnt == nd->root.mnt) {
929 if (nd->path.dentry != nd->path.mnt->mnt_root) {
930 struct dentry *old = nd->path.dentry;
931 struct dentry *parent = old->d_parent;
934 seq = read_seqcount_begin(&parent->d_seq);
935 if (read_seqcount_retry(&old->d_seq, nd->seq))
937 nd->path.dentry = parent;
941 if (!follow_up_rcu(&nd->path))
943 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
945 follow_mount_rcu(nd);
946 nd->inode = nd->path.dentry->d_inode;
950 nd->flags &= ~LOOKUP_RCU;
951 if (!(nd->flags & LOOKUP_ROOT))
954 br_read_unlock(vfsmount_lock);
959 * Follow down to the covering mount currently visible to userspace. At each
960 * point, the filesystem owning that dentry may be queried as to whether the
961 * caller is permitted to proceed or not.
963 int follow_down(struct path *path)
968 while (managed = ACCESS_ONCE(path->dentry->d_flags),
969 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
970 /* Allow the filesystem to manage the transit without i_mutex
973 * We indicate to the filesystem if someone is trying to mount
974 * something here. This gives autofs the chance to deny anyone
975 * other than its daemon the right to mount on its
978 * The filesystem may sleep at this point.
980 if (managed & DCACHE_MANAGE_TRANSIT) {
981 BUG_ON(!path->dentry->d_op);
982 BUG_ON(!path->dentry->d_op->d_manage);
983 ret = path->dentry->d_op->d_manage(
984 path->dentry, false);
986 return ret == -EISDIR ? 0 : ret;
989 /* Transit to a mounted filesystem. */
990 if (managed & DCACHE_MOUNTED) {
991 struct vfsmount *mounted = lookup_mnt(path);
997 path->dentry = dget(mounted->mnt_root);
1001 /* Don't handle automount points here */
1008 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1010 static void follow_mount(struct path *path)
1012 while (d_mountpoint(path->dentry)) {
1013 struct vfsmount *mounted = lookup_mnt(path);
1018 path->mnt = mounted;
1019 path->dentry = dget(mounted->mnt_root);
1023 static void follow_dotdot(struct nameidata *nd)
1028 struct dentry *old = nd->path.dentry;
1030 if (nd->path.dentry == nd->root.dentry &&
1031 nd->path.mnt == nd->root.mnt) {
1034 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1035 /* rare case of legitimate dget_parent()... */
1036 nd->path.dentry = dget_parent(nd->path.dentry);
1040 if (!follow_up(&nd->path))
1043 follow_mount(&nd->path);
1044 nd->inode = nd->path.dentry->d_inode;
1048 * This looks up the name in dcache, possibly revalidates the old dentry and
1049 * allocates a new one if not found or not valid. In the need_lookup argument
1050 * returns whether i_op->lookup is necessary.
1052 * dir->d_inode->i_mutex must be held
1054 static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1055 struct nameidata *nd, bool *need_lookup)
1057 struct dentry *dentry;
1060 *need_lookup = false;
1061 dentry = d_lookup(dir, name);
1063 if (d_need_lookup(dentry)) {
1064 *need_lookup = true;
1065 } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1066 error = d_revalidate(dentry, nd);
1067 if (unlikely(error <= 0)) {
1070 return ERR_PTR(error);
1071 } else if (!d_invalidate(dentry)) {
1080 dentry = d_alloc(dir, name);
1081 if (unlikely(!dentry))
1082 return ERR_PTR(-ENOMEM);
1084 *need_lookup = true;
1090 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1091 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1093 * dir->d_inode->i_mutex must be held
1095 static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
1096 struct nameidata *nd)
1100 /* Don't create child dentry for a dead directory. */
1101 if (unlikely(IS_DEADDIR(dir))) {
1103 return ERR_PTR(-ENOENT);
1106 old = dir->i_op->lookup(dir, dentry, nd);
1107 if (unlikely(old)) {
1114 static struct dentry *__lookup_hash(struct qstr *name,
1115 struct dentry *base, struct nameidata *nd)
1118 struct dentry *dentry;
1120 dentry = lookup_dcache(name, base, nd, &need_lookup);
1124 return lookup_real(base->d_inode, dentry, nd);
1128 * It's more convoluted than I'd like it to be, but... it's still fairly
1129 * small and for now I'd prefer to have fast path as straight as possible.
1130 * It _is_ time-critical.
1132 static int do_lookup(struct nameidata *nd, struct qstr *name,
1133 struct path *path, struct inode **inode)
1135 struct vfsmount *mnt = nd->path.mnt;
1136 struct dentry *dentry, *parent = nd->path.dentry;
1142 * Rename seqlock is not required here because in the off chance
1143 * of a false negative due to a concurrent rename, we're going to
1144 * do the non-racy lookup, below.
1146 if (nd->flags & LOOKUP_RCU) {
1148 dentry = __d_lookup_rcu(parent, name, &seq, nd->inode);
1153 * This sequence count validates that the inode matches
1154 * the dentry name information from lookup.
1156 *inode = dentry->d_inode;
1157 if (read_seqcount_retry(&dentry->d_seq, seq))
1161 * This sequence count validates that the parent had no
1162 * changes while we did the lookup of the dentry above.
1164 * The memory barrier in read_seqcount_begin of child is
1165 * enough, we can use __read_seqcount_retry here.
1167 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1171 if (unlikely(d_need_lookup(dentry)))
1173 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1174 status = d_revalidate(dentry, nd);
1175 if (unlikely(status <= 0)) {
1176 if (status != -ECHILD)
1182 path->dentry = dentry;
1183 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1185 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1189 if (unlazy_walk(nd, dentry))
1192 dentry = __d_lookup(parent, name);
1195 if (unlikely(!dentry))
1198 if (unlikely(d_need_lookup(dentry))) {
1203 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1204 status = d_revalidate(dentry, nd);
1205 if (unlikely(status <= 0)) {
1210 if (!d_invalidate(dentry)) {
1217 path->dentry = dentry;
1218 err = follow_managed(path, nd->flags);
1219 if (unlikely(err < 0)) {
1220 path_put_conditional(path, nd);
1224 nd->flags |= LOOKUP_JUMPED;
1225 *inode = path->dentry->d_inode;
1229 BUG_ON(nd->inode != parent->d_inode);
1231 mutex_lock(&parent->d_inode->i_mutex);
1232 dentry = __lookup_hash(name, parent, nd);
1233 mutex_unlock(&parent->d_inode->i_mutex);
1235 return PTR_ERR(dentry);
1239 static inline int may_lookup(struct nameidata *nd)
1241 if (nd->flags & LOOKUP_RCU) {
1242 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
1245 if (unlazy_walk(nd, NULL))
1248 return inode_permission(nd->inode, MAY_EXEC);
1251 static inline int handle_dots(struct nameidata *nd, int type)
1253 if (type == LAST_DOTDOT) {
1254 if (nd->flags & LOOKUP_RCU) {
1255 if (follow_dotdot_rcu(nd))
1263 static void terminate_walk(struct nameidata *nd)
1265 if (!(nd->flags & LOOKUP_RCU)) {
1266 path_put(&nd->path);
1268 nd->flags &= ~LOOKUP_RCU;
1269 if (!(nd->flags & LOOKUP_ROOT))
1270 nd->root.mnt = NULL;
1272 br_read_unlock(vfsmount_lock);
1277 * Do we need to follow links? We _really_ want to be able
1278 * to do this check without having to look at inode->i_op,
1279 * so we keep a cache of "no, this doesn't need follow_link"
1280 * for the common case.
1282 static inline int should_follow_link(struct inode *inode, int follow)
1284 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1285 if (likely(inode->i_op->follow_link))
1288 /* This gets set once for the inode lifetime */
1289 spin_lock(&inode->i_lock);
1290 inode->i_opflags |= IOP_NOFOLLOW;
1291 spin_unlock(&inode->i_lock);
1296 static inline int walk_component(struct nameidata *nd, struct path *path,
1297 struct qstr *name, int type, int follow)
1299 struct inode *inode;
1302 * "." and ".." are special - ".." especially so because it has
1303 * to be able to know about the current root directory and
1304 * parent relationships.
1306 if (unlikely(type != LAST_NORM))
1307 return handle_dots(nd, type);
1308 err = do_lookup(nd, name, path, &inode);
1309 if (unlikely(err)) {
1314 path_to_nameidata(path, nd);
1318 if (should_follow_link(inode, follow)) {
1319 if (nd->flags & LOOKUP_RCU) {
1320 if (unlikely(unlazy_walk(nd, path->dentry))) {
1325 BUG_ON(inode != path->dentry->d_inode);
1328 path_to_nameidata(path, nd);
1334 * This limits recursive symlink follows to 8, while
1335 * limiting consecutive symlinks to 40.
1337 * Without that kind of total limit, nasty chains of consecutive
1338 * symlinks can cause almost arbitrarily long lookups.
1340 static inline int nested_symlink(struct path *path, struct nameidata *nd)
1344 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1345 path_put_conditional(path, nd);
1346 path_put(&nd->path);
1349 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1352 current->link_count++;
1355 struct path link = *path;
1358 res = follow_link(&link, nd, &cookie);
1360 res = walk_component(nd, path, &nd->last,
1361 nd->last_type, LOOKUP_FOLLOW);
1362 put_link(nd, &link, cookie);
1365 current->link_count--;
1371 * We really don't want to look at inode->i_op->lookup
1372 * when we don't have to. So we keep a cache bit in
1373 * the inode ->i_opflags field that says "yes, we can
1374 * do lookup on this inode".
1376 static inline int can_lookup(struct inode *inode)
1378 if (likely(inode->i_opflags & IOP_LOOKUP))
1380 if (likely(!inode->i_op->lookup))
1383 /* We do this once for the lifetime of the inode */
1384 spin_lock(&inode->i_lock);
1385 inode->i_opflags |= IOP_LOOKUP;
1386 spin_unlock(&inode->i_lock);
1391 * We can do the critical dentry name comparison and hashing
1392 * operations one word at a time, but we are limited to:
1394 * - Architectures with fast unaligned word accesses. We could
1395 * do a "get_unaligned()" if this helps and is sufficiently
1398 * - Little-endian machines (so that we can generate the mask
1399 * of low bytes efficiently). Again, we *could* do a byte
1400 * swapping load on big-endian architectures if that is not
1401 * expensive enough to make the optimization worthless.
1403 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1404 * do not trap on the (extremely unlikely) case of a page
1405 * crossing operation.
1407 * - Furthermore, we need an efficient 64-bit compile for the
1408 * 64-bit case in order to generate the "number of bytes in
1409 * the final mask". Again, that could be replaced with a
1410 * efficient population count instruction or similar.
1412 #ifdef CONFIG_DCACHE_WORD_ACCESS
1414 #include <asm/word-at-a-time.h>
1418 static inline unsigned int fold_hash(unsigned long hash)
1420 hash += hash >> (8*sizeof(int));
1424 #else /* 32-bit case */
1426 #define fold_hash(x) (x)
1430 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1432 unsigned long a, mask;
1433 unsigned long hash = 0;
1436 a = load_unaligned_zeropad(name);
1437 if (len < sizeof(unsigned long))
1441 name += sizeof(unsigned long);
1442 len -= sizeof(unsigned long);
1446 mask = ~(~0ul << len*8);
1449 return fold_hash(hash);
1451 EXPORT_SYMBOL(full_name_hash);
1454 * Calculate the length and hash of the path component, and
1455 * return the length of the component;
1457 static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1459 unsigned long a, mask, hash, len;
1462 len = -sizeof(unsigned long);
1464 hash = (hash + a) * 9;
1465 len += sizeof(unsigned long);
1466 a = load_unaligned_zeropad(name+len);
1467 /* Do we have any NUL or '/' bytes in this word? */
1468 mask = has_zero(a) | has_zero(a ^ REPEAT_BYTE('/'));
1471 /* The mask *below* the first high bit set */
1472 mask = (mask - 1) & ~mask;
1475 *hashp = fold_hash(hash);
1477 return len + count_masked_bytes(mask);
1482 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1484 unsigned long hash = init_name_hash();
1486 hash = partial_name_hash(*name++, hash);
1487 return end_name_hash(hash);
1489 EXPORT_SYMBOL(full_name_hash);
1492 * We know there's a real path component here of at least
1495 static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1497 unsigned long hash = init_name_hash();
1498 unsigned long len = 0, c;
1500 c = (unsigned char)*name;
1503 hash = partial_name_hash(c, hash);
1504 c = (unsigned char)name[len];
1505 } while (c && c != '/');
1506 *hashp = end_name_hash(hash);
1514 * This is the basic name resolution function, turning a pathname into
1515 * the final dentry. We expect 'base' to be positive and a directory.
1517 * Returns 0 and nd will have valid dentry and mnt on success.
1518 * Returns error and drops reference to input namei data on failure.
1520 static int link_path_walk(const char *name, struct nameidata *nd)
1530 /* At this point we know we have a real path component. */
1536 err = may_lookup(nd);
1540 len = hash_name(name, &this.hash);
1545 if (name[0] == '.') switch (len) {
1547 if (name[1] == '.') {
1549 nd->flags |= LOOKUP_JUMPED;
1555 if (likely(type == LAST_NORM)) {
1556 struct dentry *parent = nd->path.dentry;
1557 nd->flags &= ~LOOKUP_JUMPED;
1558 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1559 err = parent->d_op->d_hash(parent, nd->inode,
1567 goto last_component;
1569 * If it wasn't NUL, we know it was '/'. Skip that
1570 * slash, and continue until no more slashes.
1574 } while (unlikely(name[len] == '/'));
1576 goto last_component;
1579 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1584 err = nested_symlink(&next, nd);
1588 if (can_lookup(nd->inode))
1592 /* here ends the main loop */
1596 nd->last_type = type;
1603 static int path_init(int dfd, const char *name, unsigned int flags,
1604 struct nameidata *nd, struct file **fp)
1610 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1611 nd->flags = flags | LOOKUP_JUMPED;
1613 if (flags & LOOKUP_ROOT) {
1614 struct inode *inode = nd->root.dentry->d_inode;
1616 if (!inode->i_op->lookup)
1618 retval = inode_permission(inode, MAY_EXEC);
1622 nd->path = nd->root;
1624 if (flags & LOOKUP_RCU) {
1625 br_read_lock(vfsmount_lock);
1627 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1629 path_get(&nd->path);
1634 nd->root.mnt = NULL;
1637 if (flags & LOOKUP_RCU) {
1638 br_read_lock(vfsmount_lock);
1643 path_get(&nd->root);
1645 nd->path = nd->root;
1646 } else if (dfd == AT_FDCWD) {
1647 if (flags & LOOKUP_RCU) {
1648 struct fs_struct *fs = current->fs;
1651 br_read_lock(vfsmount_lock);
1655 seq = read_seqcount_begin(&fs->seq);
1657 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1658 } while (read_seqcount_retry(&fs->seq, seq));
1660 get_fs_pwd(current->fs, &nd->path);
1663 struct dentry *dentry;
1665 file = fget_raw_light(dfd, &fput_needed);
1670 dentry = file->f_path.dentry;
1674 if (!S_ISDIR(dentry->d_inode->i_mode))
1677 retval = inode_permission(dentry->d_inode, MAY_EXEC);
1682 nd->path = file->f_path;
1683 if (flags & LOOKUP_RCU) {
1686 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1687 br_read_lock(vfsmount_lock);
1690 path_get(&file->f_path);
1691 fput_light(file, fput_needed);
1695 nd->inode = nd->path.dentry->d_inode;
1699 fput_light(file, fput_needed);
1704 static inline int lookup_last(struct nameidata *nd, struct path *path)
1706 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1707 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1709 nd->flags &= ~LOOKUP_PARENT;
1710 return walk_component(nd, path, &nd->last, nd->last_type,
1711 nd->flags & LOOKUP_FOLLOW);
1714 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1715 static int path_lookupat(int dfd, const char *name,
1716 unsigned int flags, struct nameidata *nd)
1718 struct file *base = NULL;
1723 * Path walking is largely split up into 2 different synchronisation
1724 * schemes, rcu-walk and ref-walk (explained in
1725 * Documentation/filesystems/path-lookup.txt). These share much of the
1726 * path walk code, but some things particularly setup, cleanup, and
1727 * following mounts are sufficiently divergent that functions are
1728 * duplicated. Typically there is a function foo(), and its RCU
1729 * analogue, foo_rcu().
1731 * -ECHILD is the error number of choice (just to avoid clashes) that
1732 * is returned if some aspect of an rcu-walk fails. Such an error must
1733 * be handled by restarting a traditional ref-walk (which will always
1734 * be able to complete).
1736 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1741 current->total_link_count = 0;
1742 err = link_path_walk(name, nd);
1744 if (!err && !(flags & LOOKUP_PARENT)) {
1745 err = lookup_last(nd, &path);
1748 struct path link = path;
1749 nd->flags |= LOOKUP_PARENT;
1750 err = follow_link(&link, nd, &cookie);
1752 err = lookup_last(nd, &path);
1753 put_link(nd, &link, cookie);
1758 err = complete_walk(nd);
1760 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1761 if (!nd->inode->i_op->lookup) {
1762 path_put(&nd->path);
1770 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
1771 path_put(&nd->root);
1772 nd->root.mnt = NULL;
1777 static int do_path_lookup(int dfd, const char *name,
1778 unsigned int flags, struct nameidata *nd)
1780 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1781 if (unlikely(retval == -ECHILD))
1782 retval = path_lookupat(dfd, name, flags, nd);
1783 if (unlikely(retval == -ESTALE))
1784 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1786 if (likely(!retval)) {
1787 if (unlikely(!audit_dummy_context())) {
1788 if (nd->path.dentry && nd->inode)
1789 audit_inode(name, nd->path.dentry);
1795 int kern_path_parent(const char *name, struct nameidata *nd)
1797 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
1800 int kern_path(const char *name, unsigned int flags, struct path *path)
1802 struct nameidata nd;
1803 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1810 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1811 * @dentry: pointer to dentry of the base directory
1812 * @mnt: pointer to vfs mount of the base directory
1813 * @name: pointer to file name
1814 * @flags: lookup flags
1815 * @path: pointer to struct path to fill
1817 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1818 const char *name, unsigned int flags,
1821 struct nameidata nd;
1823 nd.root.dentry = dentry;
1825 BUG_ON(flags & LOOKUP_PARENT);
1826 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1827 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1834 * Restricted form of lookup. Doesn't follow links, single-component only,
1835 * needs parent already locked. Doesn't follow mounts.
1838 static struct dentry *lookup_hash(struct nameidata *nd)
1840 return __lookup_hash(&nd->last, nd->path.dentry, nd);
1844 * lookup_one_len - filesystem helper to lookup single pathname component
1845 * @name: pathname component to lookup
1846 * @base: base directory to lookup from
1847 * @len: maximum length @len should be interpreted to
1849 * Note that this routine is purely a helper for filesystem usage and should
1850 * not be called by generic code. Also note that by using this function the
1851 * nameidata argument is passed to the filesystem methods and a filesystem
1852 * using this helper needs to be prepared for that.
1854 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1860 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1864 this.hash = full_name_hash(name, len);
1866 return ERR_PTR(-EACCES);
1869 c = *(const unsigned char *)name++;
1870 if (c == '/' || c == '\0')
1871 return ERR_PTR(-EACCES);
1874 * See if the low-level filesystem might want
1875 * to use its own hash..
1877 if (base->d_flags & DCACHE_OP_HASH) {
1878 int err = base->d_op->d_hash(base, base->d_inode, &this);
1880 return ERR_PTR(err);
1883 err = inode_permission(base->d_inode, MAY_EXEC);
1885 return ERR_PTR(err);
1887 return __lookup_hash(&this, base, NULL);
1890 int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
1891 struct path *path, int *empty)
1893 struct nameidata nd;
1894 char *tmp = getname_flags(name, flags, empty);
1895 int err = PTR_ERR(tmp);
1898 BUG_ON(flags & LOOKUP_PARENT);
1900 err = do_path_lookup(dfd, tmp, flags, &nd);
1908 int user_path_at(int dfd, const char __user *name, unsigned flags,
1911 return user_path_at_empty(dfd, name, flags, path, NULL);
1914 static int user_path_parent(int dfd, const char __user *path,
1915 struct nameidata *nd, char **name)
1917 char *s = getname(path);
1923 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1933 * It's inline, so penalty for filesystems that don't use sticky bit is
1936 static inline int check_sticky(struct inode *dir, struct inode *inode)
1938 uid_t fsuid = current_fsuid();
1940 if (!(dir->i_mode & S_ISVTX))
1942 if (current_user_ns() != inode_userns(inode))
1944 if (inode->i_uid == fsuid)
1946 if (dir->i_uid == fsuid)
1950 return !ns_capable(inode_userns(inode), CAP_FOWNER);
1954 * Check whether we can remove a link victim from directory dir, check
1955 * whether the type of victim is right.
1956 * 1. We can't do it if dir is read-only (done in permission())
1957 * 2. We should have write and exec permissions on dir
1958 * 3. We can't remove anything from append-only dir
1959 * 4. We can't do anything with immutable dir (done in permission())
1960 * 5. If the sticky bit on dir is set we should either
1961 * a. be owner of dir, or
1962 * b. be owner of victim, or
1963 * c. have CAP_FOWNER capability
1964 * 6. If the victim is append-only or immutable we can't do antyhing with
1965 * links pointing to it.
1966 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1967 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1968 * 9. We can't remove a root or mountpoint.
1969 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1970 * nfs_async_unlink().
1972 static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1976 if (!victim->d_inode)
1979 BUG_ON(victim->d_parent->d_inode != dir);
1980 audit_inode_child(victim, dir);
1982 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1987 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1988 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
1991 if (!S_ISDIR(victim->d_inode->i_mode))
1993 if (IS_ROOT(victim))
1995 } else if (S_ISDIR(victim->d_inode->i_mode))
1997 if (IS_DEADDIR(dir))
1999 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2004 /* Check whether we can create an object with dentry child in directory
2006 * 1. We can't do it if child already exists (open has special treatment for
2007 * this case, but since we are inlined it's OK)
2008 * 2. We can't do it if dir is read-only (done in permission())
2009 * 3. We should have write and exec permissions on dir
2010 * 4. We can't do it if dir is immutable (done in permission())
2012 static inline int may_create(struct inode *dir, struct dentry *child)
2016 if (IS_DEADDIR(dir))
2018 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
2022 * p1 and p2 should be directories on the same fs.
2024 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2029 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2033 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2035 p = d_ancestor(p2, p1);
2037 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2038 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2042 p = d_ancestor(p1, p2);
2044 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2045 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2049 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2050 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2054 void unlock_rename(struct dentry *p1, struct dentry *p2)
2056 mutex_unlock(&p1->d_inode->i_mutex);
2058 mutex_unlock(&p2->d_inode->i_mutex);
2059 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2063 int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2064 struct nameidata *nd)
2066 int error = may_create(dir, dentry);
2071 if (!dir->i_op->create)
2072 return -EACCES; /* shouldn't it be ENOSYS? */
2075 error = security_inode_create(dir, dentry, mode);
2078 error = dir->i_op->create(dir, dentry, mode, nd);
2080 fsnotify_create(dir, dentry);
2084 static int may_open(struct path *path, int acc_mode, int flag)
2086 struct dentry *dentry = path->dentry;
2087 struct inode *inode = dentry->d_inode;
2097 switch (inode->i_mode & S_IFMT) {
2101 if (acc_mode & MAY_WRITE)
2106 if (path->mnt->mnt_flags & MNT_NODEV)
2115 error = inode_permission(inode, acc_mode);
2120 * An append-only file must be opened in append mode for writing.
2122 if (IS_APPEND(inode)) {
2123 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
2129 /* O_NOATIME can only be set by the owner or superuser */
2130 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
2136 static int handle_truncate(struct file *filp)
2138 struct path *path = &filp->f_path;
2139 struct inode *inode = path->dentry->d_inode;
2140 int error = get_write_access(inode);
2144 * Refuse to truncate files with mandatory locks held on them.
2146 error = locks_verify_locked(inode);
2148 error = security_path_truncate(path);
2150 error = do_truncate(path->dentry, 0,
2151 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2154 put_write_access(inode);
2158 static inline int open_to_namei_flags(int flag)
2160 if ((flag & O_ACCMODE) == 3)
2166 * Handle the last step of open()
2168 static struct file *do_last(struct nameidata *nd, struct path *path,
2169 const struct open_flags *op, const char *pathname)
2171 struct dentry *dir = nd->path.dentry;
2172 struct dentry *dentry;
2173 int open_flag = op->open_flag;
2174 int will_truncate = open_flag & O_TRUNC;
2176 int acc_mode = op->acc_mode;
2180 nd->flags &= ~LOOKUP_PARENT;
2181 nd->flags |= op->intent;
2183 switch (nd->last_type) {
2186 error = handle_dots(nd, nd->last_type);
2188 return ERR_PTR(error);
2191 error = complete_walk(nd);
2193 return ERR_PTR(error);
2194 audit_inode(pathname, nd->path.dentry);
2195 if (open_flag & O_CREAT) {
2201 error = complete_walk(nd);
2203 return ERR_PTR(error);
2204 audit_inode(pathname, dir);
2208 if (!(open_flag & O_CREAT)) {
2210 if (nd->last.name[nd->last.len])
2211 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2212 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2214 /* we _can_ be in RCU mode here */
2215 error = walk_component(nd, path, &nd->last, LAST_NORM,
2218 return ERR_PTR(error);
2219 if (error) /* symlink */
2222 error = complete_walk(nd);
2224 return ERR_PTR(error);
2227 if (nd->flags & LOOKUP_DIRECTORY) {
2228 if (!nd->inode->i_op->lookup)
2231 audit_inode(pathname, nd->path.dentry);
2235 /* create side of things */
2237 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED has been
2238 * cleared when we got to the last component we are about to look up
2240 error = complete_walk(nd);
2242 return ERR_PTR(error);
2244 audit_inode(pathname, dir);
2246 /* trailing slashes? */
2247 if (nd->last.name[nd->last.len])
2250 mutex_lock(&dir->d_inode->i_mutex);
2252 dentry = lookup_hash(nd);
2253 error = PTR_ERR(dentry);
2254 if (IS_ERR(dentry)) {
2255 mutex_unlock(&dir->d_inode->i_mutex);
2259 path->dentry = dentry;
2260 path->mnt = nd->path.mnt;
2262 /* Negative dentry, just create the file */
2263 if (!dentry->d_inode) {
2264 umode_t mode = op->mode;
2265 if (!IS_POSIXACL(dir->d_inode))
2266 mode &= ~current_umask();
2268 * This write is needed to ensure that a
2269 * rw->ro transition does not occur between
2270 * the time when the file is created and when
2271 * a permanent write count is taken through
2272 * the 'struct file' in nameidata_to_filp().
2274 error = mnt_want_write(nd->path.mnt);
2276 goto exit_mutex_unlock;
2278 /* Don't check for write permission, don't truncate */
2279 open_flag &= ~O_TRUNC;
2281 acc_mode = MAY_OPEN;
2282 error = security_path_mknod(&nd->path, dentry, mode, 0);
2284 goto exit_mutex_unlock;
2285 error = vfs_create(dir->d_inode, dentry, mode, nd);
2287 goto exit_mutex_unlock;
2288 mutex_unlock(&dir->d_inode->i_mutex);
2289 dput(nd->path.dentry);
2290 nd->path.dentry = dentry;
2295 * It already exists.
2297 mutex_unlock(&dir->d_inode->i_mutex);
2298 audit_inode(pathname, path->dentry);
2301 if (open_flag & O_EXCL)
2304 error = follow_managed(path, nd->flags);
2309 nd->flags |= LOOKUP_JUMPED;
2312 if (!path->dentry->d_inode)
2315 if (path->dentry->d_inode->i_op->follow_link)
2318 path_to_nameidata(path, nd);
2319 nd->inode = path->dentry->d_inode;
2320 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
2321 error = complete_walk(nd);
2323 return ERR_PTR(error);
2325 if (S_ISDIR(nd->inode->i_mode))
2328 if (!S_ISREG(nd->inode->i_mode))
2331 if (will_truncate) {
2332 error = mnt_want_write(nd->path.mnt);
2338 error = may_open(&nd->path, acc_mode, open_flag);
2341 filp = nameidata_to_filp(nd);
2342 if (!IS_ERR(filp)) {
2343 error = ima_file_check(filp, op->acc_mode);
2346 filp = ERR_PTR(error);
2349 if (!IS_ERR(filp)) {
2350 if (will_truncate) {
2351 error = handle_truncate(filp);
2354 filp = ERR_PTR(error);
2360 mnt_drop_write(nd->path.mnt);
2361 path_put(&nd->path);
2365 mutex_unlock(&dir->d_inode->i_mutex);
2367 path_put_conditional(path, nd);
2369 filp = ERR_PTR(error);
2373 static struct file *path_openat(int dfd, const char *pathname,
2374 struct nameidata *nd, const struct open_flags *op, int flags)
2376 struct file *base = NULL;
2381 filp = get_empty_filp();
2383 return ERR_PTR(-ENFILE);
2385 filp->f_flags = op->open_flag;
2386 nd->intent.open.file = filp;
2387 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2388 nd->intent.open.create_mode = op->mode;
2390 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
2391 if (unlikely(error))
2394 current->total_link_count = 0;
2395 error = link_path_walk(pathname, nd);
2396 if (unlikely(error))
2399 filp = do_last(nd, &path, op, pathname);
2400 while (unlikely(!filp)) { /* trailing symlink */
2401 struct path link = path;
2403 if (!(nd->flags & LOOKUP_FOLLOW)) {
2404 path_put_conditional(&path, nd);
2405 path_put(&nd->path);
2406 filp = ERR_PTR(-ELOOP);
2409 nd->flags |= LOOKUP_PARENT;
2410 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2411 error = follow_link(&link, nd, &cookie);
2412 if (unlikely(error))
2413 filp = ERR_PTR(error);
2415 filp = do_last(nd, &path, op, pathname);
2416 put_link(nd, &link, cookie);
2419 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2420 path_put(&nd->root);
2423 release_open_intent(nd);
2427 filp = ERR_PTR(error);
2431 struct file *do_filp_open(int dfd, const char *pathname,
2432 const struct open_flags *op, int flags)
2434 struct nameidata nd;
2437 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
2438 if (unlikely(filp == ERR_PTR(-ECHILD)))
2439 filp = path_openat(dfd, pathname, &nd, op, flags);
2440 if (unlikely(filp == ERR_PTR(-ESTALE)))
2441 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
2445 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2446 const char *name, const struct open_flags *op, int flags)
2448 struct nameidata nd;
2452 nd.root.dentry = dentry;
2454 flags |= LOOKUP_ROOT;
2456 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
2457 return ERR_PTR(-ELOOP);
2459 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2460 if (unlikely(file == ERR_PTR(-ECHILD)))
2461 file = path_openat(-1, name, &nd, op, flags);
2462 if (unlikely(file == ERR_PTR(-ESTALE)))
2463 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2467 struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
2469 struct dentry *dentry = ERR_PTR(-EEXIST);
2470 struct nameidata nd;
2471 int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2473 return ERR_PTR(error);
2476 * Yucky last component or no last component at all?
2477 * (foo/., foo/.., /////)
2479 if (nd.last_type != LAST_NORM)
2481 nd.flags &= ~LOOKUP_PARENT;
2482 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2483 nd.intent.open.flags = O_EXCL;
2486 * Do the final lookup.
2488 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2489 dentry = lookup_hash(&nd);
2493 if (dentry->d_inode)
2496 * Special case - lookup gave negative, but... we had foo/bar/
2497 * From the vfs_mknod() POV we just have a negative dentry -
2498 * all is fine. Let's be bastards - you had / on the end, you've
2499 * been asking for (non-existent) directory. -ENOENT for you.
2501 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
2503 dentry = ERR_PTR(-ENOENT);
2510 dentry = ERR_PTR(-EEXIST);
2512 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2517 EXPORT_SYMBOL(kern_path_create);
2519 struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2521 char *tmp = getname(pathname);
2524 return ERR_CAST(tmp);
2525 res = kern_path_create(dfd, tmp, path, is_dir);
2529 EXPORT_SYMBOL(user_path_create);
2531 int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
2533 int error = may_create(dir, dentry);
2538 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2539 !ns_capable(inode_userns(dir), CAP_MKNOD))
2542 if (!dir->i_op->mknod)
2545 error = devcgroup_inode_mknod(mode, dev);
2549 error = security_inode_mknod(dir, dentry, mode, dev);
2553 error = dir->i_op->mknod(dir, dentry, mode, dev);
2555 fsnotify_create(dir, dentry);
2559 static int may_mknod(umode_t mode)
2561 switch (mode & S_IFMT) {
2567 case 0: /* zero mode translates to S_IFREG */
2576 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
2579 struct dentry *dentry;
2586 dentry = user_path_create(dfd, filename, &path, 0);
2588 return PTR_ERR(dentry);
2590 if (!IS_POSIXACL(path.dentry->d_inode))
2591 mode &= ~current_umask();
2592 error = may_mknod(mode);
2595 error = mnt_want_write(path.mnt);
2598 error = security_path_mknod(&path, dentry, mode, dev);
2600 goto out_drop_write;
2601 switch (mode & S_IFMT) {
2602 case 0: case S_IFREG:
2603 error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
2605 case S_IFCHR: case S_IFBLK:
2606 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
2607 new_decode_dev(dev));
2609 case S_IFIFO: case S_IFSOCK:
2610 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
2614 mnt_drop_write(path.mnt);
2617 mutex_unlock(&path.dentry->d_inode->i_mutex);
2623 SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
2625 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2628 int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2630 int error = may_create(dir, dentry);
2631 unsigned max_links = dir->i_sb->s_max_links;
2636 if (!dir->i_op->mkdir)
2639 mode &= (S_IRWXUGO|S_ISVTX);
2640 error = security_inode_mkdir(dir, dentry, mode);
2644 if (max_links && dir->i_nlink >= max_links)
2647 error = dir->i_op->mkdir(dir, dentry, mode);
2649 fsnotify_mkdir(dir, dentry);
2653 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
2655 struct dentry *dentry;
2659 dentry = user_path_create(dfd, pathname, &path, 1);
2661 return PTR_ERR(dentry);
2663 if (!IS_POSIXACL(path.dentry->d_inode))
2664 mode &= ~current_umask();
2665 error = mnt_want_write(path.mnt);
2668 error = security_path_mkdir(&path, dentry, mode);
2670 goto out_drop_write;
2671 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
2673 mnt_drop_write(path.mnt);
2676 mutex_unlock(&path.dentry->d_inode->i_mutex);
2681 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
2683 return sys_mkdirat(AT_FDCWD, pathname, mode);
2687 * The dentry_unhash() helper will try to drop the dentry early: we
2688 * should have a usage count of 1 if we're the only user of this
2689 * dentry, and if that is true (possibly after pruning the dcache),
2690 * then we drop the dentry now.
2692 * A low-level filesystem can, if it choses, legally
2695 * if (!d_unhashed(dentry))
2698 * if it cannot handle the case of removing a directory
2699 * that is still in use by something else..
2701 void dentry_unhash(struct dentry *dentry)
2703 shrink_dcache_parent(dentry);
2704 spin_lock(&dentry->d_lock);
2705 if (dentry->d_count == 1)
2707 spin_unlock(&dentry->d_lock);
2710 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2712 int error = may_delete(dir, dentry, 1);
2717 if (!dir->i_op->rmdir)
2721 mutex_lock(&dentry->d_inode->i_mutex);
2724 if (d_mountpoint(dentry))
2727 error = security_inode_rmdir(dir, dentry);
2731 shrink_dcache_parent(dentry);
2732 error = dir->i_op->rmdir(dir, dentry);
2736 dentry->d_inode->i_flags |= S_DEAD;
2740 mutex_unlock(&dentry->d_inode->i_mutex);
2747 static long do_rmdir(int dfd, const char __user *pathname)
2751 struct dentry *dentry;
2752 struct nameidata nd;
2754 error = user_path_parent(dfd, pathname, &nd, &name);
2758 switch(nd.last_type) {
2770 nd.flags &= ~LOOKUP_PARENT;
2772 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2773 dentry = lookup_hash(&nd);
2774 error = PTR_ERR(dentry);
2777 if (!dentry->d_inode) {
2781 error = mnt_want_write(nd.path.mnt);
2784 error = security_path_rmdir(&nd.path, dentry);
2787 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2789 mnt_drop_write(nd.path.mnt);
2793 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2800 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
2802 return do_rmdir(AT_FDCWD, pathname);
2805 int vfs_unlink(struct inode *dir, struct dentry *dentry)
2807 int error = may_delete(dir, dentry, 0);
2812 if (!dir->i_op->unlink)
2815 mutex_lock(&dentry->d_inode->i_mutex);
2816 if (d_mountpoint(dentry))
2819 error = security_inode_unlink(dir, dentry);
2821 error = dir->i_op->unlink(dir, dentry);
2826 mutex_unlock(&dentry->d_inode->i_mutex);
2828 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2829 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2830 fsnotify_link_count(dentry->d_inode);
2838 * Make sure that the actual truncation of the file will occur outside its
2839 * directory's i_mutex. Truncate can take a long time if there is a lot of
2840 * writeout happening, and we don't want to prevent access to the directory
2841 * while waiting on the I/O.
2843 static long do_unlinkat(int dfd, const char __user *pathname)
2847 struct dentry *dentry;
2848 struct nameidata nd;
2849 struct inode *inode = NULL;
2851 error = user_path_parent(dfd, pathname, &nd, &name);
2856 if (nd.last_type != LAST_NORM)
2859 nd.flags &= ~LOOKUP_PARENT;
2861 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2862 dentry = lookup_hash(&nd);
2863 error = PTR_ERR(dentry);
2864 if (!IS_ERR(dentry)) {
2865 /* Why not before? Because we want correct error value */
2866 if (nd.last.name[nd.last.len])
2868 inode = dentry->d_inode;
2872 error = mnt_want_write(nd.path.mnt);
2875 error = security_path_unlink(&nd.path, dentry);
2878 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2880 mnt_drop_write(nd.path.mnt);
2884 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2886 iput(inode); /* truncate the inode here */
2893 error = !dentry->d_inode ? -ENOENT :
2894 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2898 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
2900 if ((flag & ~AT_REMOVEDIR) != 0)
2903 if (flag & AT_REMOVEDIR)
2904 return do_rmdir(dfd, pathname);
2906 return do_unlinkat(dfd, pathname);
2909 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
2911 return do_unlinkat(AT_FDCWD, pathname);
2914 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
2916 int error = may_create(dir, dentry);
2921 if (!dir->i_op->symlink)
2924 error = security_inode_symlink(dir, dentry, oldname);
2928 error = dir->i_op->symlink(dir, dentry, oldname);
2930 fsnotify_create(dir, dentry);
2934 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2935 int, newdfd, const char __user *, newname)
2939 struct dentry *dentry;
2942 from = getname(oldname);
2944 return PTR_ERR(from);
2946 dentry = user_path_create(newdfd, newname, &path, 0);
2947 error = PTR_ERR(dentry);
2951 error = mnt_want_write(path.mnt);
2954 error = security_path_symlink(&path, dentry, from);
2956 goto out_drop_write;
2957 error = vfs_symlink(path.dentry->d_inode, dentry, from);
2959 mnt_drop_write(path.mnt);
2962 mutex_unlock(&path.dentry->d_inode->i_mutex);
2969 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
2971 return sys_symlinkat(oldname, AT_FDCWD, newname);
2974 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2976 struct inode *inode = old_dentry->d_inode;
2977 unsigned max_links = dir->i_sb->s_max_links;
2983 error = may_create(dir, new_dentry);
2987 if (dir->i_sb != inode->i_sb)
2991 * A link to an append-only or immutable file cannot be created.
2993 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2995 if (!dir->i_op->link)
2997 if (S_ISDIR(inode->i_mode))
3000 error = security_inode_link(old_dentry, dir, new_dentry);
3004 mutex_lock(&inode->i_mutex);
3005 /* Make sure we don't allow creating hardlink to an unlinked file */
3006 if (inode->i_nlink == 0)
3008 else if (max_links && inode->i_nlink >= max_links)
3011 error = dir->i_op->link(old_dentry, dir, new_dentry);
3012 mutex_unlock(&inode->i_mutex);
3014 fsnotify_link(dir, inode, new_dentry);
3019 * Hardlinks are often used in delicate situations. We avoid
3020 * security-related surprises by not following symlinks on the
3023 * We don't follow them on the oldname either to be compatible
3024 * with linux 2.0, and to avoid hard-linking to directories
3025 * and other special files. --ADM
3027 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3028 int, newdfd, const char __user *, newname, int, flags)
3030 struct dentry *new_dentry;
3031 struct path old_path, new_path;
3035 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3038 * To use null names we require CAP_DAC_READ_SEARCH
3039 * This ensures that not everyone will be able to create
3040 * handlink using the passed filedescriptor.
3042 if (flags & AT_EMPTY_PATH) {
3043 if (!capable(CAP_DAC_READ_SEARCH))
3048 if (flags & AT_SYMLINK_FOLLOW)
3049 how |= LOOKUP_FOLLOW;
3051 error = user_path_at(olddfd, oldname, how, &old_path);
3055 new_dentry = user_path_create(newdfd, newname, &new_path, 0);
3056 error = PTR_ERR(new_dentry);
3057 if (IS_ERR(new_dentry))
3061 if (old_path.mnt != new_path.mnt)
3063 error = mnt_want_write(new_path.mnt);
3066 error = security_path_link(old_path.dentry, &new_path, new_dentry);
3068 goto out_drop_write;
3069 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
3071 mnt_drop_write(new_path.mnt);
3074 mutex_unlock(&new_path.dentry->d_inode->i_mutex);
3075 path_put(&new_path);
3077 path_put(&old_path);
3082 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
3084 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
3088 * The worst of all namespace operations - renaming directory. "Perverted"
3089 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3091 * a) we can get into loop creation. Check is done in is_subdir().
3092 * b) race potential - two innocent renames can create a loop together.
3093 * That's where 4.4 screws up. Current fix: serialization on
3094 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
3096 * c) we have to lock _three_ objects - parents and victim (if it exists).
3097 * And that - after we got ->i_mutex on parents (until then we don't know
3098 * whether the target exists). Solution: try to be smart with locking
3099 * order for inodes. We rely on the fact that tree topology may change
3100 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
3101 * move will be locked. Thus we can rank directories by the tree
3102 * (ancestors first) and rank all non-directories after them.
3103 * That works since everybody except rename does "lock parent, lookup,
3104 * lock child" and rename is under ->s_vfs_rename_mutex.
3105 * HOWEVER, it relies on the assumption that any object with ->lookup()
3106 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3107 * we'd better make sure that there's no link(2) for them.
3108 * d) conversion from fhandle to dentry may come in the wrong moment - when
3109 * we are removing the target. Solution: we will have to grab ->i_mutex
3110 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3111 * ->i_mutex on parents, which works but leads to some truly excessive
3114 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3115 struct inode *new_dir, struct dentry *new_dentry)
3118 struct inode *target = new_dentry->d_inode;
3119 unsigned max_links = new_dir->i_sb->s_max_links;
3122 * If we are going to change the parent - check write permissions,
3123 * we'll need to flip '..'.
3125 if (new_dir != old_dir) {
3126 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
3131 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3137 mutex_lock(&target->i_mutex);
3140 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3144 if (max_links && !target && new_dir != old_dir &&
3145 new_dir->i_nlink >= max_links)
3149 shrink_dcache_parent(new_dentry);
3150 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3155 target->i_flags |= S_DEAD;
3156 dont_mount(new_dentry);
3160 mutex_unlock(&target->i_mutex);
3163 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3164 d_move(old_dentry,new_dentry);
3168 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3169 struct inode *new_dir, struct dentry *new_dentry)
3171 struct inode *target = new_dentry->d_inode;
3174 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3180 mutex_lock(&target->i_mutex);
3183 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3186 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3191 dont_mount(new_dentry);
3192 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3193 d_move(old_dentry, new_dentry);
3196 mutex_unlock(&target->i_mutex);
3201 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3202 struct inode *new_dir, struct dentry *new_dentry)
3205 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
3206 const unsigned char *old_name;
3208 if (old_dentry->d_inode == new_dentry->d_inode)
3211 error = may_delete(old_dir, old_dentry, is_dir);
3215 if (!new_dentry->d_inode)
3216 error = may_create(new_dir, new_dentry);
3218 error = may_delete(new_dir, new_dentry, is_dir);
3222 if (!old_dir->i_op->rename)
3225 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3228 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3230 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3232 fsnotify_move(old_dir, new_dir, old_name, is_dir,
3233 new_dentry->d_inode, old_dentry);
3234 fsnotify_oldname_free(old_name);
3239 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3240 int, newdfd, const char __user *, newname)
3242 struct dentry *old_dir, *new_dir;
3243 struct dentry *old_dentry, *new_dentry;
3244 struct dentry *trap;
3245 struct nameidata oldnd, newnd;
3250 error = user_path_parent(olddfd, oldname, &oldnd, &from);
3254 error = user_path_parent(newdfd, newname, &newnd, &to);
3259 if (oldnd.path.mnt != newnd.path.mnt)
3262 old_dir = oldnd.path.dentry;
3264 if (oldnd.last_type != LAST_NORM)
3267 new_dir = newnd.path.dentry;
3268 if (newnd.last_type != LAST_NORM)
3271 oldnd.flags &= ~LOOKUP_PARENT;
3272 newnd.flags &= ~LOOKUP_PARENT;
3273 newnd.flags |= LOOKUP_RENAME_TARGET;
3275 trap = lock_rename(new_dir, old_dir);
3277 old_dentry = lookup_hash(&oldnd);
3278 error = PTR_ERR(old_dentry);
3279 if (IS_ERR(old_dentry))
3281 /* source must exist */
3283 if (!old_dentry->d_inode)
3285 /* unless the source is a directory trailing slashes give -ENOTDIR */
3286 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3288 if (oldnd.last.name[oldnd.last.len])
3290 if (newnd.last.name[newnd.last.len])
3293 /* source should not be ancestor of target */
3295 if (old_dentry == trap)
3297 new_dentry = lookup_hash(&newnd);
3298 error = PTR_ERR(new_dentry);
3299 if (IS_ERR(new_dentry))
3301 /* target should not be an ancestor of source */
3303 if (new_dentry == trap)
3306 error = mnt_want_write(oldnd.path.mnt);
3309 error = security_path_rename(&oldnd.path, old_dentry,
3310 &newnd.path, new_dentry);
3313 error = vfs_rename(old_dir->d_inode, old_dentry,
3314 new_dir->d_inode, new_dentry);
3316 mnt_drop_write(oldnd.path.mnt);
3322 unlock_rename(new_dir, old_dir);
3324 path_put(&newnd.path);
3327 path_put(&oldnd.path);
3333 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
3335 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3338 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3342 len = PTR_ERR(link);
3347 if (len > (unsigned) buflen)
3349 if (copy_to_user(buffer, link, len))
3356 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3357 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3358 * using) it for any given inode is up to filesystem.
3360 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3362 struct nameidata nd;
3367 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3369 return PTR_ERR(cookie);
3371 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3372 if (dentry->d_inode->i_op->put_link)
3373 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3377 int vfs_follow_link(struct nameidata *nd, const char *link)
3379 return __vfs_follow_link(nd, link);
3382 /* get the link contents into pagecache */
3383 static char *page_getlink(struct dentry * dentry, struct page **ppage)
3387 struct address_space *mapping = dentry->d_inode->i_mapping;
3388 page = read_mapping_page(mapping, 0, NULL);
3393 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3397 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3399 struct page *page = NULL;
3400 char *s = page_getlink(dentry, &page);
3401 int res = vfs_readlink(dentry,buffer,buflen,s);
3404 page_cache_release(page);
3409 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
3411 struct page *page = NULL;
3412 nd_set_link(nd, page_getlink(dentry, &page));
3416 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
3418 struct page *page = cookie;
3422 page_cache_release(page);
3427 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3429 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
3431 struct address_space *mapping = inode->i_mapping;
3436 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3438 flags |= AOP_FLAG_NOFS;
3441 err = pagecache_write_begin(NULL, mapping, 0, len-1,
3442 flags, &page, &fsdata);
3446 kaddr = kmap_atomic(page);
3447 memcpy(kaddr, symname, len-1);
3448 kunmap_atomic(kaddr);
3450 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3457 mark_inode_dirty(inode);
3463 int page_symlink(struct inode *inode, const char *symname, int len)
3465 return __page_symlink(inode, symname, len,
3466 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
3469 const struct inode_operations page_symlink_inode_operations = {
3470 .readlink = generic_readlink,
3471 .follow_link = page_follow_link_light,
3472 .put_link = page_put_link,
3475 EXPORT_SYMBOL(user_path_at);
3476 EXPORT_SYMBOL(follow_down_one);
3477 EXPORT_SYMBOL(follow_down);
3478 EXPORT_SYMBOL(follow_up);
3479 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3480 EXPORT_SYMBOL(getname);
3481 EXPORT_SYMBOL(lock_rename);
3482 EXPORT_SYMBOL(lookup_one_len);
3483 EXPORT_SYMBOL(page_follow_link_light);
3484 EXPORT_SYMBOL(page_put_link);
3485 EXPORT_SYMBOL(page_readlink);
3486 EXPORT_SYMBOL(__page_symlink);
3487 EXPORT_SYMBOL(page_symlink);
3488 EXPORT_SYMBOL(page_symlink_inode_operations);
3489 EXPORT_SYMBOL(kern_path);
3490 EXPORT_SYMBOL(vfs_path_lookup);
3491 EXPORT_SYMBOL(inode_permission);
3492 EXPORT_SYMBOL(unlock_rename);
3493 EXPORT_SYMBOL(vfs_create);
3494 EXPORT_SYMBOL(vfs_follow_link);
3495 EXPORT_SYMBOL(vfs_link);
3496 EXPORT_SYMBOL(vfs_mkdir);
3497 EXPORT_SYMBOL(vfs_mknod);
3498 EXPORT_SYMBOL(generic_permission);
3499 EXPORT_SYMBOL(vfs_readlink);
3500 EXPORT_SYMBOL(vfs_rename);
3501 EXPORT_SYMBOL(vfs_rmdir);
3502 EXPORT_SYMBOL(vfs_symlink);
3503 EXPORT_SYMBOL(vfs_unlink);
3504 EXPORT_SYMBOL(dentry_unhash);
3505 EXPORT_SYMBOL(generic_readlink);