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/slab.h>
21 #include <linux/namei.h>
22 #include <linux/pagemap.h>
23 #include <linux/fsnotify.h>
24 #include <linux/personality.h>
25 #include <linux/security.h>
26 #include <linux/ima.h>
27 #include <linux/syscalls.h>
28 #include <linux/mount.h>
29 #include <linux/audit.h>
30 #include <linux/capability.h>
31 #include <linux/file.h>
32 #include <linux/fcntl.h>
33 #include <linux/device_cgroup.h>
34 #include <linux/fs_struct.h>
35 #include <linux/posix_acl.h>
36 #include <asm/uaccess.h>
41 /* [Feb-1997 T. Schoebel-Theuer]
42 * Fundamental changes in the pathname lookup mechanisms (namei)
43 * were necessary because of omirr. The reason is that omirr needs
44 * to know the _real_ pathname, not the user-supplied one, in case
45 * of symlinks (and also when transname replacements occur).
47 * The new code replaces the old recursive symlink resolution with
48 * an iterative one (in case of non-nested symlink chains). It does
49 * this with calls to <fs>_follow_link().
50 * As a side effect, dir_namei(), _namei() and follow_link() are now
51 * replaced with a single function lookup_dentry() that can handle all
52 * the special cases of the former code.
54 * With the new dcache, the pathname is stored at each inode, at least as
55 * long as the refcount of the inode is positive. As a side effect, the
56 * size of the dcache depends on the inode cache and thus is dynamic.
58 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
59 * resolution to correspond with current state of the code.
61 * Note that the symlink resolution is not *completely* iterative.
62 * There is still a significant amount of tail- and mid- recursion in
63 * the algorithm. Also, note that <fs>_readlink() is not used in
64 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
65 * may return different results than <fs>_follow_link(). Many virtual
66 * filesystems (including /proc) exhibit this behavior.
69 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
70 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
71 * and the name already exists in form of a symlink, try to create the new
72 * name indicated by the symlink. The old code always complained that the
73 * name already exists, due to not following the symlink even if its target
74 * is nonexistent. The new semantics affects also mknod() and link() when
75 * the name is a symlink pointing to a non-existent name.
77 * I don't know which semantics is the right one, since I have no access
78 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
79 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
80 * "old" one. Personally, I think the new semantics is much more logical.
81 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
82 * file does succeed in both HP-UX and SunOs, but not in Solaris
83 * and in the old Linux semantics.
86 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
87 * semantics. See the comments in "open_namei" and "do_link" below.
89 * [10-Sep-98 Alan Modra] Another symlink change.
92 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
93 * inside the path - always follow.
94 * in the last component in creation/removal/renaming - never follow.
95 * if LOOKUP_FOLLOW passed - follow.
96 * if the pathname has trailing slashes - follow.
97 * otherwise - don't follow.
98 * (applied in that order).
100 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
101 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
102 * During the 2.4 we need to fix the userland stuff depending on it -
103 * hopefully we will be able to get rid of that wart in 2.5. So far only
104 * XEmacs seems to be relying on it...
107 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
108 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
109 * any extra contention...
112 /* In order to reduce some races, while at the same time doing additional
113 * checking and hopefully speeding things up, we copy filenames to the
114 * kernel data space before using them..
116 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
117 * PATH_MAX includes the nul terminator --RR.
119 static char *getname_flags(const char __user *filename, int flags, int *empty)
121 char *result = __getname(), *err;
124 if (unlikely(!result))
125 return ERR_PTR(-ENOMEM);
127 len = strncpy_from_user(result, filename, PATH_MAX);
129 if (unlikely(len < 0))
132 /* The empty path is special. */
133 if (unlikely(!len)) {
136 err = ERR_PTR(-ENOENT);
137 if (!(flags & LOOKUP_EMPTY))
141 err = ERR_PTR(-ENAMETOOLONG);
142 if (likely(len < PATH_MAX)) {
143 audit_getname(result);
152 char *getname(const char __user * filename)
154 return getname_flags(filename, 0, NULL);
157 #ifdef CONFIG_AUDITSYSCALL
158 void putname(const char *name)
160 if (unlikely(!audit_dummy_context()))
165 EXPORT_SYMBOL(putname);
168 static int check_acl(struct inode *inode, int mask)
170 #ifdef CONFIG_FS_POSIX_ACL
171 struct posix_acl *acl;
173 if (mask & MAY_NOT_BLOCK) {
174 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
177 /* no ->get_acl() calls in RCU mode... */
178 if (acl == ACL_NOT_CACHED)
180 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
183 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
186 * A filesystem can force a ACL callback by just never filling the
187 * ACL cache. But normally you'd fill the cache either at inode
188 * instantiation time, or on the first ->get_acl call.
190 * If the filesystem doesn't have a get_acl() function at all, we'll
191 * just create the negative cache entry.
193 if (acl == ACL_NOT_CACHED) {
194 if (inode->i_op->get_acl) {
195 acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
199 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
205 int error = posix_acl_permission(inode, acl, mask);
206 posix_acl_release(acl);
215 * This does the basic permission checking
217 static int acl_permission_check(struct inode *inode, int mask)
219 unsigned int mode = inode->i_mode;
221 if (current_user_ns() != inode_userns(inode))
224 if (likely(current_fsuid() == inode->i_uid))
227 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
228 int error = check_acl(inode, mask);
229 if (error != -EAGAIN)
233 if (in_group_p(inode->i_gid))
239 * If the DACs are ok we don't need any capability check.
241 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
247 * generic_permission - check for access rights on a Posix-like filesystem
248 * @inode: inode to check access rights for
249 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
251 * Used to check for read/write/execute permissions on a file.
252 * We use "fsuid" for this, letting us set arbitrary permissions
253 * for filesystem access without changing the "normal" uids which
254 * are used for other things.
256 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
257 * request cannot be satisfied (eg. requires blocking or too much complexity).
258 * It would then be called again in ref-walk mode.
260 int generic_permission(struct inode *inode, int mask)
265 * Do the basic permission checks.
267 ret = acl_permission_check(inode, mask);
271 if (S_ISDIR(inode->i_mode)) {
272 /* DACs are overridable for directories */
273 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
275 if (!(mask & MAY_WRITE))
276 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
281 * Read/write DACs are always overridable.
282 * Executable DACs are overridable when there is
283 * at least one exec bit set.
285 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
286 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
290 * Searching includes executable on directories, else just read.
292 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
293 if (mask == MAY_READ)
294 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
301 * We _really_ want to just do "generic_permission()" without
302 * even looking at the inode->i_op values. So we keep a cache
303 * flag in inode->i_opflags, that says "this has not special
304 * permission function, use the fast case".
306 static inline int do_inode_permission(struct inode *inode, int mask)
308 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
309 if (likely(inode->i_op->permission))
310 return inode->i_op->permission(inode, mask);
312 /* This gets set once for the inode lifetime */
313 spin_lock(&inode->i_lock);
314 inode->i_opflags |= IOP_FASTPERM;
315 spin_unlock(&inode->i_lock);
317 return generic_permission(inode, mask);
321 * inode_permission - check for access rights to a given inode
322 * @inode: inode to check permission on
323 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
325 * Used to check for read/write/execute permissions on an inode.
326 * We use "fsuid" for this, letting us set arbitrary permissions
327 * for filesystem access without changing the "normal" uids which
328 * are used for other things.
330 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
332 int inode_permission(struct inode *inode, int mask)
336 if (unlikely(mask & MAY_WRITE)) {
337 umode_t mode = inode->i_mode;
340 * Nobody gets write access to a read-only fs.
342 if (IS_RDONLY(inode) &&
343 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
347 * Nobody gets write access to an immutable file.
349 if (IS_IMMUTABLE(inode))
353 retval = do_inode_permission(inode, mask);
357 retval = devcgroup_inode_permission(inode, mask);
361 return security_inode_permission(inode, mask);
365 * path_get - get a reference to a path
366 * @path: path to get the reference to
368 * Given a path increment the reference count to the dentry and the vfsmount.
370 void path_get(struct path *path)
375 EXPORT_SYMBOL(path_get);
378 * path_put - put a reference to a path
379 * @path: path to put the reference to
381 * Given a path decrement the reference count to the dentry and the vfsmount.
383 void path_put(struct path *path)
388 EXPORT_SYMBOL(path_put);
391 * Path walking has 2 modes, rcu-walk and ref-walk (see
392 * Documentation/filesystems/path-lookup.txt). In situations when we can't
393 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
394 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
395 * mode. Refcounts are grabbed at the last known good point before rcu-walk
396 * got stuck, so ref-walk may continue from there. If this is not successful
397 * (eg. a seqcount has changed), then failure is returned and it's up to caller
398 * to restart the path walk from the beginning in ref-walk mode.
402 * unlazy_walk - try to switch to ref-walk mode.
403 * @nd: nameidata pathwalk data
404 * @dentry: child of nd->path.dentry or NULL
405 * Returns: 0 on success, -ECHILD on failure
407 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
408 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
409 * @nd or NULL. Must be called from rcu-walk context.
411 static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
413 struct fs_struct *fs = current->fs;
414 struct dentry *parent = nd->path.dentry;
417 BUG_ON(!(nd->flags & LOOKUP_RCU));
418 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
420 spin_lock(&fs->lock);
421 if (nd->root.mnt != fs->root.mnt ||
422 nd->root.dentry != fs->root.dentry)
425 spin_lock(&parent->d_lock);
427 if (!__d_rcu_to_refcount(parent, nd->seq))
429 BUG_ON(nd->inode != parent->d_inode);
431 if (dentry->d_parent != parent)
433 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
434 if (!__d_rcu_to_refcount(dentry, nd->seq))
437 * If the sequence check on the child dentry passed, then
438 * the child has not been removed from its parent. This
439 * means the parent dentry must be valid and able to take
440 * a reference at this point.
442 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
443 BUG_ON(!parent->d_count);
445 spin_unlock(&dentry->d_lock);
447 spin_unlock(&parent->d_lock);
450 spin_unlock(&fs->lock);
452 mntget(nd->path.mnt);
455 br_read_unlock(vfsmount_lock);
456 nd->flags &= ~LOOKUP_RCU;
460 spin_unlock(&dentry->d_lock);
462 spin_unlock(&parent->d_lock);
465 spin_unlock(&fs->lock);
470 * release_open_intent - free up open intent resources
471 * @nd: pointer to nameidata
473 void release_open_intent(struct nameidata *nd)
475 struct file *file = nd->intent.open.file;
477 if (file && !IS_ERR(file)) {
478 if (file->f_path.dentry == NULL)
485 static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
487 return dentry->d_op->d_revalidate(dentry, nd);
491 * complete_walk - successful completion of path walk
492 * @nd: pointer nameidata
494 * If we had been in RCU mode, drop out of it and legitimize nd->path.
495 * Revalidate the final result, unless we'd already done that during
496 * the path walk or the filesystem doesn't ask for it. Return 0 on
497 * success, -error on failure. In case of failure caller does not
498 * need to drop nd->path.
500 static int complete_walk(struct nameidata *nd)
502 struct dentry *dentry = nd->path.dentry;
505 if (nd->flags & LOOKUP_RCU) {
506 nd->flags &= ~LOOKUP_RCU;
507 if (!(nd->flags & LOOKUP_ROOT))
509 spin_lock(&dentry->d_lock);
510 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
511 spin_unlock(&dentry->d_lock);
513 br_read_unlock(vfsmount_lock);
516 BUG_ON(nd->inode != dentry->d_inode);
517 spin_unlock(&dentry->d_lock);
518 mntget(nd->path.mnt);
520 br_read_unlock(vfsmount_lock);
523 if (likely(!(nd->flags & LOOKUP_JUMPED)))
526 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
529 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
532 /* Note: we do not d_invalidate() */
533 status = d_revalidate(dentry, nd);
544 static __always_inline void set_root(struct nameidata *nd)
547 get_fs_root(current->fs, &nd->root);
550 static int link_path_walk(const char *, struct nameidata *);
552 static __always_inline void set_root_rcu(struct nameidata *nd)
555 struct fs_struct *fs = current->fs;
559 seq = read_seqcount_begin(&fs->seq);
561 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
562 } while (read_seqcount_retry(&fs->seq, seq));
566 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
578 nd->flags |= LOOKUP_JUMPED;
580 nd->inode = nd->path.dentry->d_inode;
582 ret = link_path_walk(link, nd);
586 return PTR_ERR(link);
589 static void path_put_conditional(struct path *path, struct nameidata *nd)
592 if (path->mnt != nd->path.mnt)
596 static inline void path_to_nameidata(const struct path *path,
597 struct nameidata *nd)
599 if (!(nd->flags & LOOKUP_RCU)) {
600 dput(nd->path.dentry);
601 if (nd->path.mnt != path->mnt)
602 mntput(nd->path.mnt);
604 nd->path.mnt = path->mnt;
605 nd->path.dentry = path->dentry;
608 static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
610 struct inode *inode = link->dentry->d_inode;
611 if (!IS_ERR(cookie) && inode->i_op->put_link)
612 inode->i_op->put_link(link->dentry, nd, cookie);
616 static __always_inline int
617 follow_link(struct path *link, struct nameidata *nd, void **p)
620 struct dentry *dentry = link->dentry;
622 BUG_ON(nd->flags & LOOKUP_RCU);
624 if (link->mnt == nd->path.mnt)
627 if (unlikely(current->total_link_count >= 40)) {
628 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
633 current->total_link_count++;
636 nd_set_link(nd, NULL);
638 error = security_inode_follow_link(link->dentry, nd);
640 *p = ERR_PTR(error); /* no ->put_link(), please */
645 nd->last_type = LAST_BIND;
646 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
649 char *s = nd_get_link(nd);
652 error = __vfs_follow_link(nd, s);
653 else if (nd->last_type == LAST_BIND) {
654 nd->flags |= LOOKUP_JUMPED;
655 nd->inode = nd->path.dentry->d_inode;
656 if (nd->inode->i_op->follow_link) {
657 /* stepped on a _really_ weird one */
666 static int follow_up_rcu(struct path *path)
668 struct mount *mnt = real_mount(path->mnt);
669 struct mount *parent;
670 struct dentry *mountpoint;
672 parent = mnt->mnt_parent;
673 if (&parent->mnt == path->mnt)
675 mountpoint = mnt->mnt_mountpoint;
676 path->dentry = mountpoint;
677 path->mnt = &parent->mnt;
681 int follow_up(struct path *path)
683 struct mount *mnt = real_mount(path->mnt);
684 struct mount *parent;
685 struct dentry *mountpoint;
687 br_read_lock(vfsmount_lock);
688 parent = mnt->mnt_parent;
689 if (&parent->mnt == path->mnt) {
690 br_read_unlock(vfsmount_lock);
693 mntget(&parent->mnt);
694 mountpoint = dget(mnt->mnt_mountpoint);
695 br_read_unlock(vfsmount_lock);
697 path->dentry = mountpoint;
699 path->mnt = &parent->mnt;
704 * Perform an automount
705 * - return -EISDIR to tell follow_managed() to stop and return the path we
708 static int follow_automount(struct path *path, unsigned flags,
711 struct vfsmount *mnt;
714 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
717 /* We don't want to mount if someone's just doing a stat -
718 * unless they're stat'ing a directory and appended a '/' to
721 * We do, however, want to mount if someone wants to open or
722 * create a file of any type under the mountpoint, wants to
723 * traverse through the mountpoint or wants to open the
724 * mounted directory. Also, autofs may mark negative dentries
725 * as being automount points. These will need the attentions
726 * of the daemon to instantiate them before they can be used.
728 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
729 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
730 path->dentry->d_inode)
733 current->total_link_count++;
734 if (current->total_link_count >= 40)
737 mnt = path->dentry->d_op->d_automount(path);
740 * The filesystem is allowed to return -EISDIR here to indicate
741 * it doesn't want to automount. For instance, autofs would do
742 * this so that its userspace daemon can mount on this dentry.
744 * However, we can only permit this if it's a terminal point in
745 * the path being looked up; if it wasn't then the remainder of
746 * the path is inaccessible and we should say so.
748 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
753 if (!mnt) /* mount collision */
757 /* lock_mount() may release path->mnt on error */
761 err = finish_automount(mnt, path);
765 /* Someone else made a mount here whilst we were busy */
770 path->dentry = dget(mnt->mnt_root);
779 * Handle a dentry that is managed in some way.
780 * - Flagged for transit management (autofs)
781 * - Flagged as mountpoint
782 * - Flagged as automount point
784 * This may only be called in refwalk mode.
786 * Serialization is taken care of in namespace.c
788 static int follow_managed(struct path *path, unsigned flags)
790 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
792 bool need_mntput = false;
795 /* Given that we're not holding a lock here, we retain the value in a
796 * local variable for each dentry as we look at it so that we don't see
797 * the components of that value change under us */
798 while (managed = ACCESS_ONCE(path->dentry->d_flags),
799 managed &= DCACHE_MANAGED_DENTRY,
800 unlikely(managed != 0)) {
801 /* Allow the filesystem to manage the transit without i_mutex
803 if (managed & DCACHE_MANAGE_TRANSIT) {
804 BUG_ON(!path->dentry->d_op);
805 BUG_ON(!path->dentry->d_op->d_manage);
806 ret = path->dentry->d_op->d_manage(path->dentry, false);
811 /* Transit to a mounted filesystem. */
812 if (managed & DCACHE_MOUNTED) {
813 struct vfsmount *mounted = lookup_mnt(path);
819 path->dentry = dget(mounted->mnt_root);
824 /* Something is mounted on this dentry in another
825 * namespace and/or whatever was mounted there in this
826 * namespace got unmounted before we managed to get the
830 /* Handle an automount point */
831 if (managed & DCACHE_NEED_AUTOMOUNT) {
832 ret = follow_automount(path, flags, &need_mntput);
838 /* We didn't change the current path point */
842 if (need_mntput && path->mnt == mnt)
846 return ret < 0 ? ret : need_mntput;
849 int follow_down_one(struct path *path)
851 struct vfsmount *mounted;
853 mounted = lookup_mnt(path);
858 path->dentry = dget(mounted->mnt_root);
864 static inline bool managed_dentry_might_block(struct dentry *dentry)
866 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
867 dentry->d_op->d_manage(dentry, true) < 0);
871 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
872 * we meet a managed dentry that would need blocking.
874 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
875 struct inode **inode)
878 struct mount *mounted;
880 * Don't forget we might have a non-mountpoint managed dentry
881 * that wants to block transit.
883 if (unlikely(managed_dentry_might_block(path->dentry)))
886 if (!d_mountpoint(path->dentry))
889 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
892 path->mnt = &mounted->mnt;
893 path->dentry = mounted->mnt.mnt_root;
894 nd->flags |= LOOKUP_JUMPED;
895 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
897 * Update the inode too. We don't need to re-check the
898 * dentry sequence number here after this d_inode read,
899 * because a mount-point is always pinned.
901 *inode = path->dentry->d_inode;
906 static void follow_mount_rcu(struct nameidata *nd)
908 while (d_mountpoint(nd->path.dentry)) {
909 struct mount *mounted;
910 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
913 nd->path.mnt = &mounted->mnt;
914 nd->path.dentry = mounted->mnt.mnt_root;
915 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
919 static int follow_dotdot_rcu(struct nameidata *nd)
924 if (nd->path.dentry == nd->root.dentry &&
925 nd->path.mnt == nd->root.mnt) {
928 if (nd->path.dentry != nd->path.mnt->mnt_root) {
929 struct dentry *old = nd->path.dentry;
930 struct dentry *parent = old->d_parent;
933 seq = read_seqcount_begin(&parent->d_seq);
934 if (read_seqcount_retry(&old->d_seq, nd->seq))
936 nd->path.dentry = parent;
940 if (!follow_up_rcu(&nd->path))
942 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
944 follow_mount_rcu(nd);
945 nd->inode = nd->path.dentry->d_inode;
949 nd->flags &= ~LOOKUP_RCU;
950 if (!(nd->flags & LOOKUP_ROOT))
953 br_read_unlock(vfsmount_lock);
958 * Follow down to the covering mount currently visible to userspace. At each
959 * point, the filesystem owning that dentry may be queried as to whether the
960 * caller is permitted to proceed or not.
962 int follow_down(struct path *path)
967 while (managed = ACCESS_ONCE(path->dentry->d_flags),
968 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
969 /* Allow the filesystem to manage the transit without i_mutex
972 * We indicate to the filesystem if someone is trying to mount
973 * something here. This gives autofs the chance to deny anyone
974 * other than its daemon the right to mount on its
977 * The filesystem may sleep at this point.
979 if (managed & DCACHE_MANAGE_TRANSIT) {
980 BUG_ON(!path->dentry->d_op);
981 BUG_ON(!path->dentry->d_op->d_manage);
982 ret = path->dentry->d_op->d_manage(
983 path->dentry, false);
985 return ret == -EISDIR ? 0 : ret;
988 /* Transit to a mounted filesystem. */
989 if (managed & DCACHE_MOUNTED) {
990 struct vfsmount *mounted = lookup_mnt(path);
996 path->dentry = dget(mounted->mnt_root);
1000 /* Don't handle automount points here */
1007 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1009 static void follow_mount(struct path *path)
1011 while (d_mountpoint(path->dentry)) {
1012 struct vfsmount *mounted = lookup_mnt(path);
1017 path->mnt = mounted;
1018 path->dentry = dget(mounted->mnt_root);
1022 static void follow_dotdot(struct nameidata *nd)
1027 struct dentry *old = nd->path.dentry;
1029 if (nd->path.dentry == nd->root.dentry &&
1030 nd->path.mnt == nd->root.mnt) {
1033 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1034 /* rare case of legitimate dget_parent()... */
1035 nd->path.dentry = dget_parent(nd->path.dentry);
1039 if (!follow_up(&nd->path))
1042 follow_mount(&nd->path);
1043 nd->inode = nd->path.dentry->d_inode;
1047 * This looks up the name in dcache, possibly revalidates the old dentry and
1048 * allocates a new one if not found or not valid. In the need_lookup argument
1049 * returns whether i_op->lookup is necessary.
1051 * dir->d_inode->i_mutex must be held
1053 static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1054 struct nameidata *nd, bool *need_lookup)
1056 struct dentry *dentry;
1059 *need_lookup = false;
1060 dentry = d_lookup(dir, name);
1062 if (d_need_lookup(dentry)) {
1063 *need_lookup = true;
1064 } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1065 error = d_revalidate(dentry, nd);
1066 if (unlikely(error <= 0)) {
1069 return ERR_PTR(error);
1070 } else if (!d_invalidate(dentry)) {
1079 dentry = d_alloc(dir, name);
1080 if (unlikely(!dentry))
1081 return ERR_PTR(-ENOMEM);
1083 *need_lookup = true;
1089 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1090 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1092 * dir->d_inode->i_mutex must be held
1094 static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
1095 struct nameidata *nd)
1099 /* Don't create child dentry for a dead directory. */
1100 if (unlikely(IS_DEADDIR(dir))) {
1102 return ERR_PTR(-ENOENT);
1105 old = dir->i_op->lookup(dir, dentry, nd);
1106 if (unlikely(old)) {
1113 static struct dentry *__lookup_hash(struct qstr *name,
1114 struct dentry *base, struct nameidata *nd)
1117 struct dentry *dentry;
1119 dentry = lookup_dcache(name, base, nd, &need_lookup);
1123 return lookup_real(base->d_inode, dentry, nd);
1127 * It's more convoluted than I'd like it to be, but... it's still fairly
1128 * small and for now I'd prefer to have fast path as straight as possible.
1129 * It _is_ time-critical.
1131 static int do_lookup(struct nameidata *nd, struct qstr *name,
1132 struct path *path, struct inode **inode)
1134 struct vfsmount *mnt = nd->path.mnt;
1135 struct dentry *dentry, *parent = nd->path.dentry;
1141 * Rename seqlock is not required here because in the off chance
1142 * of a false negative due to a concurrent rename, we're going to
1143 * do the non-racy lookup, below.
1145 if (nd->flags & LOOKUP_RCU) {
1147 dentry = __d_lookup_rcu(parent, name, &seq, nd->inode);
1152 * This sequence count validates that the inode matches
1153 * the dentry name information from lookup.
1155 *inode = dentry->d_inode;
1156 if (read_seqcount_retry(&dentry->d_seq, seq))
1160 * This sequence count validates that the parent had no
1161 * changes while we did the lookup of the dentry above.
1163 * The memory barrier in read_seqcount_begin of child is
1164 * enough, we can use __read_seqcount_retry here.
1166 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1170 if (unlikely(d_need_lookup(dentry)))
1172 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1173 status = d_revalidate(dentry, nd);
1174 if (unlikely(status <= 0)) {
1175 if (status != -ECHILD)
1181 path->dentry = dentry;
1182 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1184 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1188 if (unlazy_walk(nd, dentry))
1191 dentry = __d_lookup(parent, name);
1194 if (unlikely(!dentry))
1197 if (unlikely(d_need_lookup(dentry))) {
1202 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1203 status = d_revalidate(dentry, nd);
1204 if (unlikely(status <= 0)) {
1209 if (!d_invalidate(dentry)) {
1216 path->dentry = dentry;
1217 err = follow_managed(path, nd->flags);
1218 if (unlikely(err < 0)) {
1219 path_put_conditional(path, nd);
1223 nd->flags |= LOOKUP_JUMPED;
1224 *inode = path->dentry->d_inode;
1228 BUG_ON(nd->inode != parent->d_inode);
1230 mutex_lock(&parent->d_inode->i_mutex);
1231 dentry = __lookup_hash(name, parent, nd);
1232 mutex_unlock(&parent->d_inode->i_mutex);
1234 return PTR_ERR(dentry);
1238 static inline int may_lookup(struct nameidata *nd)
1240 if (nd->flags & LOOKUP_RCU) {
1241 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
1244 if (unlazy_walk(nd, NULL))
1247 return inode_permission(nd->inode, MAY_EXEC);
1250 static inline int handle_dots(struct nameidata *nd, int type)
1252 if (type == LAST_DOTDOT) {
1253 if (nd->flags & LOOKUP_RCU) {
1254 if (follow_dotdot_rcu(nd))
1262 static void terminate_walk(struct nameidata *nd)
1264 if (!(nd->flags & LOOKUP_RCU)) {
1265 path_put(&nd->path);
1267 nd->flags &= ~LOOKUP_RCU;
1268 if (!(nd->flags & LOOKUP_ROOT))
1269 nd->root.mnt = NULL;
1271 br_read_unlock(vfsmount_lock);
1276 * Do we need to follow links? We _really_ want to be able
1277 * to do this check without having to look at inode->i_op,
1278 * so we keep a cache of "no, this doesn't need follow_link"
1279 * for the common case.
1281 static inline int should_follow_link(struct inode *inode, int follow)
1283 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1284 if (likely(inode->i_op->follow_link))
1287 /* This gets set once for the inode lifetime */
1288 spin_lock(&inode->i_lock);
1289 inode->i_opflags |= IOP_NOFOLLOW;
1290 spin_unlock(&inode->i_lock);
1295 static inline int walk_component(struct nameidata *nd, struct path *path,
1296 struct qstr *name, int type, int follow)
1298 struct inode *inode;
1301 * "." and ".." are special - ".." especially so because it has
1302 * to be able to know about the current root directory and
1303 * parent relationships.
1305 if (unlikely(type != LAST_NORM))
1306 return handle_dots(nd, type);
1307 err = do_lookup(nd, name, path, &inode);
1308 if (unlikely(err)) {
1313 path_to_nameidata(path, nd);
1317 if (should_follow_link(inode, follow)) {
1318 if (nd->flags & LOOKUP_RCU) {
1319 if (unlikely(unlazy_walk(nd, path->dentry))) {
1324 BUG_ON(inode != path->dentry->d_inode);
1327 path_to_nameidata(path, nd);
1333 * This limits recursive symlink follows to 8, while
1334 * limiting consecutive symlinks to 40.
1336 * Without that kind of total limit, nasty chains of consecutive
1337 * symlinks can cause almost arbitrarily long lookups.
1339 static inline int nested_symlink(struct path *path, struct nameidata *nd)
1343 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1344 path_put_conditional(path, nd);
1345 path_put(&nd->path);
1348 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1351 current->link_count++;
1354 struct path link = *path;
1357 res = follow_link(&link, nd, &cookie);
1359 res = walk_component(nd, path, &nd->last,
1360 nd->last_type, LOOKUP_FOLLOW);
1361 put_link(nd, &link, cookie);
1364 current->link_count--;
1370 * We really don't want to look at inode->i_op->lookup
1371 * when we don't have to. So we keep a cache bit in
1372 * the inode ->i_opflags field that says "yes, we can
1373 * do lookup on this inode".
1375 static inline int can_lookup(struct inode *inode)
1377 if (likely(inode->i_opflags & IOP_LOOKUP))
1379 if (likely(!inode->i_op->lookup))
1382 /* We do this once for the lifetime of the inode */
1383 spin_lock(&inode->i_lock);
1384 inode->i_opflags |= IOP_LOOKUP;
1385 spin_unlock(&inode->i_lock);
1390 * We can do the critical dentry name comparison and hashing
1391 * operations one word at a time, but we are limited to:
1393 * - Architectures with fast unaligned word accesses. We could
1394 * do a "get_unaligned()" if this helps and is sufficiently
1397 * - Little-endian machines (so that we can generate the mask
1398 * of low bytes efficiently). Again, we *could* do a byte
1399 * swapping load on big-endian architectures if that is not
1400 * expensive enough to make the optimization worthless.
1402 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1403 * do not trap on the (extremely unlikely) case of a page
1404 * crossing operation.
1406 * - Furthermore, we need an efficient 64-bit compile for the
1407 * 64-bit case in order to generate the "number of bytes in
1408 * the final mask". Again, that could be replaced with a
1409 * efficient population count instruction or similar.
1411 #ifdef CONFIG_DCACHE_WORD_ACCESS
1413 #include <asm/word-at-a-time.h>
1417 static inline unsigned int fold_hash(unsigned long hash)
1419 hash += hash >> (8*sizeof(int));
1423 #else /* 32-bit case */
1425 #define fold_hash(x) (x)
1429 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1431 unsigned long a, mask;
1432 unsigned long hash = 0;
1435 a = load_unaligned_zeropad(name);
1436 if (len < sizeof(unsigned long))
1440 name += sizeof(unsigned long);
1441 len -= sizeof(unsigned long);
1445 mask = ~(~0ul << len*8);
1448 return fold_hash(hash);
1450 EXPORT_SYMBOL(full_name_hash);
1453 * Calculate the length and hash of the path component, and
1454 * return the length of the component;
1456 static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1458 unsigned long a, mask, hash, len;
1461 len = -sizeof(unsigned long);
1463 hash = (hash + a) * 9;
1464 len += sizeof(unsigned long);
1465 a = load_unaligned_zeropad(name+len);
1466 /* Do we have any NUL or '/' bytes in this word? */
1467 mask = has_zero(a) | has_zero(a ^ REPEAT_BYTE('/'));
1470 /* The mask *below* the first high bit set */
1471 mask = (mask - 1) & ~mask;
1474 *hashp = fold_hash(hash);
1476 return len + count_masked_bytes(mask);
1481 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1483 unsigned long hash = init_name_hash();
1485 hash = partial_name_hash(*name++, hash);
1486 return end_name_hash(hash);
1488 EXPORT_SYMBOL(full_name_hash);
1491 * We know there's a real path component here of at least
1494 static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1496 unsigned long hash = init_name_hash();
1497 unsigned long len = 0, c;
1499 c = (unsigned char)*name;
1502 hash = partial_name_hash(c, hash);
1503 c = (unsigned char)name[len];
1504 } while (c && c != '/');
1505 *hashp = end_name_hash(hash);
1513 * This is the basic name resolution function, turning a pathname into
1514 * the final dentry. We expect 'base' to be positive and a directory.
1516 * Returns 0 and nd will have valid dentry and mnt on success.
1517 * Returns error and drops reference to input namei data on failure.
1519 static int link_path_walk(const char *name, struct nameidata *nd)
1529 /* At this point we know we have a real path component. */
1535 err = may_lookup(nd);
1539 len = hash_name(name, &this.hash);
1544 if (name[0] == '.') switch (len) {
1546 if (name[1] == '.') {
1548 nd->flags |= LOOKUP_JUMPED;
1554 if (likely(type == LAST_NORM)) {
1555 struct dentry *parent = nd->path.dentry;
1556 nd->flags &= ~LOOKUP_JUMPED;
1557 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1558 err = parent->d_op->d_hash(parent, nd->inode,
1566 goto last_component;
1568 * If it wasn't NUL, we know it was '/'. Skip that
1569 * slash, and continue until no more slashes.
1573 } while (unlikely(name[len] == '/'));
1575 goto last_component;
1578 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1583 err = nested_symlink(&next, nd);
1587 if (can_lookup(nd->inode))
1591 /* here ends the main loop */
1595 nd->last_type = type;
1602 static int path_init(int dfd, const char *name, unsigned int flags,
1603 struct nameidata *nd, struct file **fp)
1609 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1610 nd->flags = flags | LOOKUP_JUMPED;
1612 if (flags & LOOKUP_ROOT) {
1613 struct inode *inode = nd->root.dentry->d_inode;
1615 if (!inode->i_op->lookup)
1617 retval = inode_permission(inode, MAY_EXEC);
1621 nd->path = nd->root;
1623 if (flags & LOOKUP_RCU) {
1624 br_read_lock(vfsmount_lock);
1626 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1628 path_get(&nd->path);
1633 nd->root.mnt = NULL;
1636 if (flags & LOOKUP_RCU) {
1637 br_read_lock(vfsmount_lock);
1642 path_get(&nd->root);
1644 nd->path = nd->root;
1645 } else if (dfd == AT_FDCWD) {
1646 if (flags & LOOKUP_RCU) {
1647 struct fs_struct *fs = current->fs;
1650 br_read_lock(vfsmount_lock);
1654 seq = read_seqcount_begin(&fs->seq);
1656 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1657 } while (read_seqcount_retry(&fs->seq, seq));
1659 get_fs_pwd(current->fs, &nd->path);
1662 struct dentry *dentry;
1664 file = fget_raw_light(dfd, &fput_needed);
1669 dentry = file->f_path.dentry;
1673 if (!S_ISDIR(dentry->d_inode->i_mode))
1676 retval = inode_permission(dentry->d_inode, MAY_EXEC);
1681 nd->path = file->f_path;
1682 if (flags & LOOKUP_RCU) {
1685 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1686 br_read_lock(vfsmount_lock);
1689 path_get(&file->f_path);
1690 fput_light(file, fput_needed);
1694 nd->inode = nd->path.dentry->d_inode;
1698 fput_light(file, fput_needed);
1703 static inline int lookup_last(struct nameidata *nd, struct path *path)
1705 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1706 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1708 nd->flags &= ~LOOKUP_PARENT;
1709 return walk_component(nd, path, &nd->last, nd->last_type,
1710 nd->flags & LOOKUP_FOLLOW);
1713 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1714 static int path_lookupat(int dfd, const char *name,
1715 unsigned int flags, struct nameidata *nd)
1717 struct file *base = NULL;
1722 * Path walking is largely split up into 2 different synchronisation
1723 * schemes, rcu-walk and ref-walk (explained in
1724 * Documentation/filesystems/path-lookup.txt). These share much of the
1725 * path walk code, but some things particularly setup, cleanup, and
1726 * following mounts are sufficiently divergent that functions are
1727 * duplicated. Typically there is a function foo(), and its RCU
1728 * analogue, foo_rcu().
1730 * -ECHILD is the error number of choice (just to avoid clashes) that
1731 * is returned if some aspect of an rcu-walk fails. Such an error must
1732 * be handled by restarting a traditional ref-walk (which will always
1733 * be able to complete).
1735 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1740 current->total_link_count = 0;
1741 err = link_path_walk(name, nd);
1743 if (!err && !(flags & LOOKUP_PARENT)) {
1744 err = lookup_last(nd, &path);
1747 struct path link = path;
1748 nd->flags |= LOOKUP_PARENT;
1749 err = follow_link(&link, nd, &cookie);
1751 err = lookup_last(nd, &path);
1752 put_link(nd, &link, cookie);
1757 err = complete_walk(nd);
1759 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1760 if (!nd->inode->i_op->lookup) {
1761 path_put(&nd->path);
1769 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
1770 path_put(&nd->root);
1771 nd->root.mnt = NULL;
1776 static int do_path_lookup(int dfd, const char *name,
1777 unsigned int flags, struct nameidata *nd)
1779 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1780 if (unlikely(retval == -ECHILD))
1781 retval = path_lookupat(dfd, name, flags, nd);
1782 if (unlikely(retval == -ESTALE))
1783 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1785 if (likely(!retval)) {
1786 if (unlikely(!audit_dummy_context())) {
1787 if (nd->path.dentry && nd->inode)
1788 audit_inode(name, nd->path.dentry);
1794 int kern_path_parent(const char *name, struct nameidata *nd)
1796 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
1799 int kern_path(const char *name, unsigned int flags, struct path *path)
1801 struct nameidata nd;
1802 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1809 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1810 * @dentry: pointer to dentry of the base directory
1811 * @mnt: pointer to vfs mount of the base directory
1812 * @name: pointer to file name
1813 * @flags: lookup flags
1814 * @path: pointer to struct path to fill
1816 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1817 const char *name, unsigned int flags,
1820 struct nameidata nd;
1822 nd.root.dentry = dentry;
1824 BUG_ON(flags & LOOKUP_PARENT);
1825 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1826 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1833 * Restricted form of lookup. Doesn't follow links, single-component only,
1834 * needs parent already locked. Doesn't follow mounts.
1837 static struct dentry *lookup_hash(struct nameidata *nd)
1839 return __lookup_hash(&nd->last, nd->path.dentry, nd);
1843 * lookup_one_len - filesystem helper to lookup single pathname component
1844 * @name: pathname component to lookup
1845 * @base: base directory to lookup from
1846 * @len: maximum length @len should be interpreted to
1848 * Note that this routine is purely a helper for filesystem usage and should
1849 * not be called by generic code. Also note that by using this function the
1850 * nameidata argument is passed to the filesystem methods and a filesystem
1851 * using this helper needs to be prepared for that.
1853 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1859 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1863 this.hash = full_name_hash(name, len);
1865 return ERR_PTR(-EACCES);
1868 c = *(const unsigned char *)name++;
1869 if (c == '/' || c == '\0')
1870 return ERR_PTR(-EACCES);
1873 * See if the low-level filesystem might want
1874 * to use its own hash..
1876 if (base->d_flags & DCACHE_OP_HASH) {
1877 int err = base->d_op->d_hash(base, base->d_inode, &this);
1879 return ERR_PTR(err);
1882 err = inode_permission(base->d_inode, MAY_EXEC);
1884 return ERR_PTR(err);
1886 return __lookup_hash(&this, base, NULL);
1889 int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
1890 struct path *path, int *empty)
1892 struct nameidata nd;
1893 char *tmp = getname_flags(name, flags, empty);
1894 int err = PTR_ERR(tmp);
1897 BUG_ON(flags & LOOKUP_PARENT);
1899 err = do_path_lookup(dfd, tmp, flags, &nd);
1907 int user_path_at(int dfd, const char __user *name, unsigned flags,
1910 return user_path_at_empty(dfd, name, flags, path, NULL);
1913 static int user_path_parent(int dfd, const char __user *path,
1914 struct nameidata *nd, char **name)
1916 char *s = getname(path);
1922 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1932 * It's inline, so penalty for filesystems that don't use sticky bit is
1935 static inline int check_sticky(struct inode *dir, struct inode *inode)
1937 uid_t fsuid = current_fsuid();
1939 if (!(dir->i_mode & S_ISVTX))
1941 if (current_user_ns() != inode_userns(inode))
1943 if (inode->i_uid == fsuid)
1945 if (dir->i_uid == fsuid)
1949 return !ns_capable(inode_userns(inode), CAP_FOWNER);
1953 * Check whether we can remove a link victim from directory dir, check
1954 * whether the type of victim is right.
1955 * 1. We can't do it if dir is read-only (done in permission())
1956 * 2. We should have write and exec permissions on dir
1957 * 3. We can't remove anything from append-only dir
1958 * 4. We can't do anything with immutable dir (done in permission())
1959 * 5. If the sticky bit on dir is set we should either
1960 * a. be owner of dir, or
1961 * b. be owner of victim, or
1962 * c. have CAP_FOWNER capability
1963 * 6. If the victim is append-only or immutable we can't do antyhing with
1964 * links pointing to it.
1965 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1966 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1967 * 9. We can't remove a root or mountpoint.
1968 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1969 * nfs_async_unlink().
1971 static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1975 if (!victim->d_inode)
1978 BUG_ON(victim->d_parent->d_inode != dir);
1979 audit_inode_child(victim, dir);
1981 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1986 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1987 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
1990 if (!S_ISDIR(victim->d_inode->i_mode))
1992 if (IS_ROOT(victim))
1994 } else if (S_ISDIR(victim->d_inode->i_mode))
1996 if (IS_DEADDIR(dir))
1998 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2003 /* Check whether we can create an object with dentry child in directory
2005 * 1. We can't do it if child already exists (open has special treatment for
2006 * this case, but since we are inlined it's OK)
2007 * 2. We can't do it if dir is read-only (done in permission())
2008 * 3. We should have write and exec permissions on dir
2009 * 4. We can't do it if dir is immutable (done in permission())
2011 static inline int may_create(struct inode *dir, struct dentry *child)
2015 if (IS_DEADDIR(dir))
2017 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
2021 * p1 and p2 should be directories on the same fs.
2023 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2028 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2032 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2034 p = d_ancestor(p2, p1);
2036 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2037 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2041 p = d_ancestor(p1, p2);
2043 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2044 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2048 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2049 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2053 void unlock_rename(struct dentry *p1, struct dentry *p2)
2055 mutex_unlock(&p1->d_inode->i_mutex);
2057 mutex_unlock(&p2->d_inode->i_mutex);
2058 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2062 int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2063 struct nameidata *nd)
2065 int error = may_create(dir, dentry);
2070 if (!dir->i_op->create)
2071 return -EACCES; /* shouldn't it be ENOSYS? */
2074 error = security_inode_create(dir, dentry, mode);
2077 error = dir->i_op->create(dir, dentry, mode, nd);
2079 fsnotify_create(dir, dentry);
2083 static int may_open(struct path *path, int acc_mode, int flag)
2085 struct dentry *dentry = path->dentry;
2086 struct inode *inode = dentry->d_inode;
2096 switch (inode->i_mode & S_IFMT) {
2100 if (acc_mode & MAY_WRITE)
2105 if (path->mnt->mnt_flags & MNT_NODEV)
2114 error = inode_permission(inode, acc_mode);
2119 * An append-only file must be opened in append mode for writing.
2121 if (IS_APPEND(inode)) {
2122 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
2128 /* O_NOATIME can only be set by the owner or superuser */
2129 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
2135 static int handle_truncate(struct file *filp)
2137 struct path *path = &filp->f_path;
2138 struct inode *inode = path->dentry->d_inode;
2139 int error = get_write_access(inode);
2143 * Refuse to truncate files with mandatory locks held on them.
2145 error = locks_verify_locked(inode);
2147 error = security_path_truncate(path);
2149 error = do_truncate(path->dentry, 0,
2150 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2153 put_write_access(inode);
2157 static inline int open_to_namei_flags(int flag)
2159 if ((flag & O_ACCMODE) == 3)
2165 * Handle the last step of open()
2167 static struct file *do_last(struct nameidata *nd, struct path *path,
2168 const struct open_flags *op, const char *pathname)
2170 struct dentry *dir = nd->path.dentry;
2171 struct dentry *dentry;
2172 int open_flag = op->open_flag;
2173 int will_truncate = open_flag & O_TRUNC;
2175 int acc_mode = op->acc_mode;
2179 nd->flags &= ~LOOKUP_PARENT;
2180 nd->flags |= op->intent;
2182 switch (nd->last_type) {
2185 error = handle_dots(nd, nd->last_type);
2187 return ERR_PTR(error);
2190 error = complete_walk(nd);
2192 return ERR_PTR(error);
2193 audit_inode(pathname, nd->path.dentry);
2194 if (open_flag & O_CREAT) {
2200 error = complete_walk(nd);
2202 return ERR_PTR(error);
2203 audit_inode(pathname, dir);
2207 if (!(open_flag & O_CREAT)) {
2209 if (nd->last.name[nd->last.len])
2210 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2211 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2213 /* we _can_ be in RCU mode here */
2214 error = walk_component(nd, path, &nd->last, LAST_NORM,
2217 return ERR_PTR(error);
2218 if (error) /* symlink */
2221 error = complete_walk(nd);
2223 return ERR_PTR(error);
2226 if (nd->flags & LOOKUP_DIRECTORY) {
2227 if (!nd->inode->i_op->lookup)
2230 audit_inode(pathname, nd->path.dentry);
2234 /* create side of things */
2236 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED has been
2237 * cleared when we got to the last component we are about to look up
2239 error = complete_walk(nd);
2241 return ERR_PTR(error);
2243 audit_inode(pathname, dir);
2245 /* trailing slashes? */
2246 if (nd->last.name[nd->last.len])
2249 mutex_lock(&dir->d_inode->i_mutex);
2251 dentry = lookup_hash(nd);
2252 error = PTR_ERR(dentry);
2253 if (IS_ERR(dentry)) {
2254 mutex_unlock(&dir->d_inode->i_mutex);
2258 path->dentry = dentry;
2259 path->mnt = nd->path.mnt;
2261 /* Negative dentry, just create the file */
2262 if (!dentry->d_inode) {
2263 umode_t mode = op->mode;
2264 if (!IS_POSIXACL(dir->d_inode))
2265 mode &= ~current_umask();
2267 * This write is needed to ensure that a
2268 * rw->ro transition does not occur between
2269 * the time when the file is created and when
2270 * a permanent write count is taken through
2271 * the 'struct file' in nameidata_to_filp().
2273 error = mnt_want_write(nd->path.mnt);
2275 goto exit_mutex_unlock;
2277 /* Don't check for write permission, don't truncate */
2278 open_flag &= ~O_TRUNC;
2280 acc_mode = MAY_OPEN;
2281 error = security_path_mknod(&nd->path, dentry, mode, 0);
2283 goto exit_mutex_unlock;
2284 error = vfs_create(dir->d_inode, dentry, mode, nd);
2286 goto exit_mutex_unlock;
2287 mutex_unlock(&dir->d_inode->i_mutex);
2288 dput(nd->path.dentry);
2289 nd->path.dentry = dentry;
2294 * It already exists.
2296 mutex_unlock(&dir->d_inode->i_mutex);
2297 audit_inode(pathname, path->dentry);
2300 if (open_flag & O_EXCL)
2303 error = follow_managed(path, nd->flags);
2308 nd->flags |= LOOKUP_JUMPED;
2311 if (!path->dentry->d_inode)
2314 if (path->dentry->d_inode->i_op->follow_link)
2317 path_to_nameidata(path, nd);
2318 nd->inode = path->dentry->d_inode;
2319 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
2320 error = complete_walk(nd);
2322 return ERR_PTR(error);
2324 if (S_ISDIR(nd->inode->i_mode))
2327 if (!S_ISREG(nd->inode->i_mode))
2330 if (will_truncate) {
2331 error = mnt_want_write(nd->path.mnt);
2337 error = may_open(&nd->path, acc_mode, open_flag);
2340 filp = nameidata_to_filp(nd);
2341 if (!IS_ERR(filp)) {
2342 error = ima_file_check(filp, op->acc_mode);
2345 filp = ERR_PTR(error);
2348 if (!IS_ERR(filp)) {
2349 if (will_truncate) {
2350 error = handle_truncate(filp);
2353 filp = ERR_PTR(error);
2359 mnt_drop_write(nd->path.mnt);
2360 path_put(&nd->path);
2364 mutex_unlock(&dir->d_inode->i_mutex);
2366 path_put_conditional(path, nd);
2368 filp = ERR_PTR(error);
2372 static struct file *path_openat(int dfd, const char *pathname,
2373 struct nameidata *nd, const struct open_flags *op, int flags)
2375 struct file *base = NULL;
2380 filp = get_empty_filp();
2382 return ERR_PTR(-ENFILE);
2384 filp->f_flags = op->open_flag;
2385 nd->intent.open.file = filp;
2386 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2387 nd->intent.open.create_mode = op->mode;
2389 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
2390 if (unlikely(error))
2393 current->total_link_count = 0;
2394 error = link_path_walk(pathname, nd);
2395 if (unlikely(error))
2398 filp = do_last(nd, &path, op, pathname);
2399 while (unlikely(!filp)) { /* trailing symlink */
2400 struct path link = path;
2402 if (!(nd->flags & LOOKUP_FOLLOW)) {
2403 path_put_conditional(&path, nd);
2404 path_put(&nd->path);
2405 filp = ERR_PTR(-ELOOP);
2408 nd->flags |= LOOKUP_PARENT;
2409 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2410 error = follow_link(&link, nd, &cookie);
2411 if (unlikely(error))
2412 filp = ERR_PTR(error);
2414 filp = do_last(nd, &path, op, pathname);
2415 put_link(nd, &link, cookie);
2418 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2419 path_put(&nd->root);
2422 release_open_intent(nd);
2426 filp = ERR_PTR(error);
2430 struct file *do_filp_open(int dfd, const char *pathname,
2431 const struct open_flags *op, int flags)
2433 struct nameidata nd;
2436 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
2437 if (unlikely(filp == ERR_PTR(-ECHILD)))
2438 filp = path_openat(dfd, pathname, &nd, op, flags);
2439 if (unlikely(filp == ERR_PTR(-ESTALE)))
2440 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
2444 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2445 const char *name, const struct open_flags *op, int flags)
2447 struct nameidata nd;
2451 nd.root.dentry = dentry;
2453 flags |= LOOKUP_ROOT;
2455 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
2456 return ERR_PTR(-ELOOP);
2458 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2459 if (unlikely(file == ERR_PTR(-ECHILD)))
2460 file = path_openat(-1, name, &nd, op, flags);
2461 if (unlikely(file == ERR_PTR(-ESTALE)))
2462 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2466 struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
2468 struct dentry *dentry = ERR_PTR(-EEXIST);
2469 struct nameidata nd;
2470 int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2472 return ERR_PTR(error);
2475 * Yucky last component or no last component at all?
2476 * (foo/., foo/.., /////)
2478 if (nd.last_type != LAST_NORM)
2480 nd.flags &= ~LOOKUP_PARENT;
2481 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2482 nd.intent.open.flags = O_EXCL;
2485 * Do the final lookup.
2487 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2488 dentry = lookup_hash(&nd);
2492 if (dentry->d_inode)
2495 * Special case - lookup gave negative, but... we had foo/bar/
2496 * From the vfs_mknod() POV we just have a negative dentry -
2497 * all is fine. Let's be bastards - you had / on the end, you've
2498 * been asking for (non-existent) directory. -ENOENT for you.
2500 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
2502 dentry = ERR_PTR(-ENOENT);
2509 dentry = ERR_PTR(-EEXIST);
2511 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2516 EXPORT_SYMBOL(kern_path_create);
2518 struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2520 char *tmp = getname(pathname);
2523 return ERR_CAST(tmp);
2524 res = kern_path_create(dfd, tmp, path, is_dir);
2528 EXPORT_SYMBOL(user_path_create);
2530 int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
2532 int error = may_create(dir, dentry);
2537 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2538 !ns_capable(inode_userns(dir), CAP_MKNOD))
2541 if (!dir->i_op->mknod)
2544 error = devcgroup_inode_mknod(mode, dev);
2548 error = security_inode_mknod(dir, dentry, mode, dev);
2552 error = dir->i_op->mknod(dir, dentry, mode, dev);
2554 fsnotify_create(dir, dentry);
2558 static int may_mknod(umode_t mode)
2560 switch (mode & S_IFMT) {
2566 case 0: /* zero mode translates to S_IFREG */
2575 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
2578 struct dentry *dentry;
2585 dentry = user_path_create(dfd, filename, &path, 0);
2587 return PTR_ERR(dentry);
2589 if (!IS_POSIXACL(path.dentry->d_inode))
2590 mode &= ~current_umask();
2591 error = may_mknod(mode);
2594 error = mnt_want_write(path.mnt);
2597 error = security_path_mknod(&path, dentry, mode, dev);
2599 goto out_drop_write;
2600 switch (mode & S_IFMT) {
2601 case 0: case S_IFREG:
2602 error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
2604 case S_IFCHR: case S_IFBLK:
2605 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
2606 new_decode_dev(dev));
2608 case S_IFIFO: case S_IFSOCK:
2609 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
2613 mnt_drop_write(path.mnt);
2616 mutex_unlock(&path.dentry->d_inode->i_mutex);
2622 SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
2624 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2627 int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2629 int error = may_create(dir, dentry);
2630 unsigned max_links = dir->i_sb->s_max_links;
2635 if (!dir->i_op->mkdir)
2638 mode &= (S_IRWXUGO|S_ISVTX);
2639 error = security_inode_mkdir(dir, dentry, mode);
2643 if (max_links && dir->i_nlink >= max_links)
2646 error = dir->i_op->mkdir(dir, dentry, mode);
2648 fsnotify_mkdir(dir, dentry);
2652 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
2654 struct dentry *dentry;
2658 dentry = user_path_create(dfd, pathname, &path, 1);
2660 return PTR_ERR(dentry);
2662 if (!IS_POSIXACL(path.dentry->d_inode))
2663 mode &= ~current_umask();
2664 error = mnt_want_write(path.mnt);
2667 error = security_path_mkdir(&path, dentry, mode);
2669 goto out_drop_write;
2670 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
2672 mnt_drop_write(path.mnt);
2675 mutex_unlock(&path.dentry->d_inode->i_mutex);
2680 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
2682 return sys_mkdirat(AT_FDCWD, pathname, mode);
2686 * The dentry_unhash() helper will try to drop the dentry early: we
2687 * should have a usage count of 1 if we're the only user of this
2688 * dentry, and if that is true (possibly after pruning the dcache),
2689 * then we drop the dentry now.
2691 * A low-level filesystem can, if it choses, legally
2694 * if (!d_unhashed(dentry))
2697 * if it cannot handle the case of removing a directory
2698 * that is still in use by something else..
2700 void dentry_unhash(struct dentry *dentry)
2702 shrink_dcache_parent(dentry);
2703 spin_lock(&dentry->d_lock);
2704 if (dentry->d_count == 1)
2706 spin_unlock(&dentry->d_lock);
2709 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2711 int error = may_delete(dir, dentry, 1);
2716 if (!dir->i_op->rmdir)
2720 mutex_lock(&dentry->d_inode->i_mutex);
2723 if (d_mountpoint(dentry))
2726 error = security_inode_rmdir(dir, dentry);
2730 shrink_dcache_parent(dentry);
2731 error = dir->i_op->rmdir(dir, dentry);
2735 dentry->d_inode->i_flags |= S_DEAD;
2739 mutex_unlock(&dentry->d_inode->i_mutex);
2746 static long do_rmdir(int dfd, const char __user *pathname)
2750 struct dentry *dentry;
2751 struct nameidata nd;
2753 error = user_path_parent(dfd, pathname, &nd, &name);
2757 switch(nd.last_type) {
2769 nd.flags &= ~LOOKUP_PARENT;
2771 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2772 dentry = lookup_hash(&nd);
2773 error = PTR_ERR(dentry);
2776 if (!dentry->d_inode) {
2780 error = mnt_want_write(nd.path.mnt);
2783 error = security_path_rmdir(&nd.path, dentry);
2786 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2788 mnt_drop_write(nd.path.mnt);
2792 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2799 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
2801 return do_rmdir(AT_FDCWD, pathname);
2804 int vfs_unlink(struct inode *dir, struct dentry *dentry)
2806 int error = may_delete(dir, dentry, 0);
2811 if (!dir->i_op->unlink)
2814 mutex_lock(&dentry->d_inode->i_mutex);
2815 if (d_mountpoint(dentry))
2818 error = security_inode_unlink(dir, dentry);
2820 error = dir->i_op->unlink(dir, dentry);
2825 mutex_unlock(&dentry->d_inode->i_mutex);
2827 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2828 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2829 fsnotify_link_count(dentry->d_inode);
2837 * Make sure that the actual truncation of the file will occur outside its
2838 * directory's i_mutex. Truncate can take a long time if there is a lot of
2839 * writeout happening, and we don't want to prevent access to the directory
2840 * while waiting on the I/O.
2842 static long do_unlinkat(int dfd, const char __user *pathname)
2846 struct dentry *dentry;
2847 struct nameidata nd;
2848 struct inode *inode = NULL;
2850 error = user_path_parent(dfd, pathname, &nd, &name);
2855 if (nd.last_type != LAST_NORM)
2858 nd.flags &= ~LOOKUP_PARENT;
2860 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2861 dentry = lookup_hash(&nd);
2862 error = PTR_ERR(dentry);
2863 if (!IS_ERR(dentry)) {
2864 /* Why not before? Because we want correct error value */
2865 if (nd.last.name[nd.last.len])
2867 inode = dentry->d_inode;
2871 error = mnt_want_write(nd.path.mnt);
2874 error = security_path_unlink(&nd.path, dentry);
2877 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2879 mnt_drop_write(nd.path.mnt);
2883 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2885 iput(inode); /* truncate the inode here */
2892 error = !dentry->d_inode ? -ENOENT :
2893 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2897 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
2899 if ((flag & ~AT_REMOVEDIR) != 0)
2902 if (flag & AT_REMOVEDIR)
2903 return do_rmdir(dfd, pathname);
2905 return do_unlinkat(dfd, pathname);
2908 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
2910 return do_unlinkat(AT_FDCWD, pathname);
2913 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
2915 int error = may_create(dir, dentry);
2920 if (!dir->i_op->symlink)
2923 error = security_inode_symlink(dir, dentry, oldname);
2927 error = dir->i_op->symlink(dir, dentry, oldname);
2929 fsnotify_create(dir, dentry);
2933 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2934 int, newdfd, const char __user *, newname)
2938 struct dentry *dentry;
2941 from = getname(oldname);
2943 return PTR_ERR(from);
2945 dentry = user_path_create(newdfd, newname, &path, 0);
2946 error = PTR_ERR(dentry);
2950 error = mnt_want_write(path.mnt);
2953 error = security_path_symlink(&path, dentry, from);
2955 goto out_drop_write;
2956 error = vfs_symlink(path.dentry->d_inode, dentry, from);
2958 mnt_drop_write(path.mnt);
2961 mutex_unlock(&path.dentry->d_inode->i_mutex);
2968 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
2970 return sys_symlinkat(oldname, AT_FDCWD, newname);
2973 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2975 struct inode *inode = old_dentry->d_inode;
2976 unsigned max_links = dir->i_sb->s_max_links;
2982 error = may_create(dir, new_dentry);
2986 if (dir->i_sb != inode->i_sb)
2990 * A link to an append-only or immutable file cannot be created.
2992 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2994 if (!dir->i_op->link)
2996 if (S_ISDIR(inode->i_mode))
2999 error = security_inode_link(old_dentry, dir, new_dentry);
3003 mutex_lock(&inode->i_mutex);
3004 /* Make sure we don't allow creating hardlink to an unlinked file */
3005 if (inode->i_nlink == 0)
3007 else if (max_links && inode->i_nlink >= max_links)
3010 error = dir->i_op->link(old_dentry, dir, new_dentry);
3011 mutex_unlock(&inode->i_mutex);
3013 fsnotify_link(dir, inode, new_dentry);
3018 * Hardlinks are often used in delicate situations. We avoid
3019 * security-related surprises by not following symlinks on the
3022 * We don't follow them on the oldname either to be compatible
3023 * with linux 2.0, and to avoid hard-linking to directories
3024 * and other special files. --ADM
3026 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3027 int, newdfd, const char __user *, newname, int, flags)
3029 struct dentry *new_dentry;
3030 struct path old_path, new_path;
3034 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3037 * To use null names we require CAP_DAC_READ_SEARCH
3038 * This ensures that not everyone will be able to create
3039 * handlink using the passed filedescriptor.
3041 if (flags & AT_EMPTY_PATH) {
3042 if (!capable(CAP_DAC_READ_SEARCH))
3047 if (flags & AT_SYMLINK_FOLLOW)
3048 how |= LOOKUP_FOLLOW;
3050 error = user_path_at(olddfd, oldname, how, &old_path);
3054 new_dentry = user_path_create(newdfd, newname, &new_path, 0);
3055 error = PTR_ERR(new_dentry);
3056 if (IS_ERR(new_dentry))
3060 if (old_path.mnt != new_path.mnt)
3062 error = mnt_want_write(new_path.mnt);
3065 error = security_path_link(old_path.dentry, &new_path, new_dentry);
3067 goto out_drop_write;
3068 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
3070 mnt_drop_write(new_path.mnt);
3073 mutex_unlock(&new_path.dentry->d_inode->i_mutex);
3074 path_put(&new_path);
3076 path_put(&old_path);
3081 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
3083 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
3087 * The worst of all namespace operations - renaming directory. "Perverted"
3088 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3090 * a) we can get into loop creation. Check is done in is_subdir().
3091 * b) race potential - two innocent renames can create a loop together.
3092 * That's where 4.4 screws up. Current fix: serialization on
3093 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
3095 * c) we have to lock _three_ objects - parents and victim (if it exists).
3096 * And that - after we got ->i_mutex on parents (until then we don't know
3097 * whether the target exists). Solution: try to be smart with locking
3098 * order for inodes. We rely on the fact that tree topology may change
3099 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
3100 * move will be locked. Thus we can rank directories by the tree
3101 * (ancestors first) and rank all non-directories after them.
3102 * That works since everybody except rename does "lock parent, lookup,
3103 * lock child" and rename is under ->s_vfs_rename_mutex.
3104 * HOWEVER, it relies on the assumption that any object with ->lookup()
3105 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3106 * we'd better make sure that there's no link(2) for them.
3107 * d) conversion from fhandle to dentry may come in the wrong moment - when
3108 * we are removing the target. Solution: we will have to grab ->i_mutex
3109 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3110 * ->i_mutex on parents, which works but leads to some truly excessive
3113 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3114 struct inode *new_dir, struct dentry *new_dentry)
3117 struct inode *target = new_dentry->d_inode;
3118 unsigned max_links = new_dir->i_sb->s_max_links;
3121 * If we are going to change the parent - check write permissions,
3122 * we'll need to flip '..'.
3124 if (new_dir != old_dir) {
3125 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
3130 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3136 mutex_lock(&target->i_mutex);
3139 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3143 if (max_links && !target && new_dir != old_dir &&
3144 new_dir->i_nlink >= max_links)
3148 shrink_dcache_parent(new_dentry);
3149 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3154 target->i_flags |= S_DEAD;
3155 dont_mount(new_dentry);
3159 mutex_unlock(&target->i_mutex);
3162 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3163 d_move(old_dentry,new_dentry);
3167 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3168 struct inode *new_dir, struct dentry *new_dentry)
3170 struct inode *target = new_dentry->d_inode;
3173 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3179 mutex_lock(&target->i_mutex);
3182 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3185 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3190 dont_mount(new_dentry);
3191 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3192 d_move(old_dentry, new_dentry);
3195 mutex_unlock(&target->i_mutex);
3200 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3201 struct inode *new_dir, struct dentry *new_dentry)
3204 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
3205 const unsigned char *old_name;
3207 if (old_dentry->d_inode == new_dentry->d_inode)
3210 error = may_delete(old_dir, old_dentry, is_dir);
3214 if (!new_dentry->d_inode)
3215 error = may_create(new_dir, new_dentry);
3217 error = may_delete(new_dir, new_dentry, is_dir);
3221 if (!old_dir->i_op->rename)
3224 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3227 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3229 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3231 fsnotify_move(old_dir, new_dir, old_name, is_dir,
3232 new_dentry->d_inode, old_dentry);
3233 fsnotify_oldname_free(old_name);
3238 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3239 int, newdfd, const char __user *, newname)
3241 struct dentry *old_dir, *new_dir;
3242 struct dentry *old_dentry, *new_dentry;
3243 struct dentry *trap;
3244 struct nameidata oldnd, newnd;
3249 error = user_path_parent(olddfd, oldname, &oldnd, &from);
3253 error = user_path_parent(newdfd, newname, &newnd, &to);
3258 if (oldnd.path.mnt != newnd.path.mnt)
3261 old_dir = oldnd.path.dentry;
3263 if (oldnd.last_type != LAST_NORM)
3266 new_dir = newnd.path.dentry;
3267 if (newnd.last_type != LAST_NORM)
3270 oldnd.flags &= ~LOOKUP_PARENT;
3271 newnd.flags &= ~LOOKUP_PARENT;
3272 newnd.flags |= LOOKUP_RENAME_TARGET;
3274 trap = lock_rename(new_dir, old_dir);
3276 old_dentry = lookup_hash(&oldnd);
3277 error = PTR_ERR(old_dentry);
3278 if (IS_ERR(old_dentry))
3280 /* source must exist */
3282 if (!old_dentry->d_inode)
3284 /* unless the source is a directory trailing slashes give -ENOTDIR */
3285 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3287 if (oldnd.last.name[oldnd.last.len])
3289 if (newnd.last.name[newnd.last.len])
3292 /* source should not be ancestor of target */
3294 if (old_dentry == trap)
3296 new_dentry = lookup_hash(&newnd);
3297 error = PTR_ERR(new_dentry);
3298 if (IS_ERR(new_dentry))
3300 /* target should not be an ancestor of source */
3302 if (new_dentry == trap)
3305 error = mnt_want_write(oldnd.path.mnt);
3308 error = security_path_rename(&oldnd.path, old_dentry,
3309 &newnd.path, new_dentry);
3312 error = vfs_rename(old_dir->d_inode, old_dentry,
3313 new_dir->d_inode, new_dentry);
3315 mnt_drop_write(oldnd.path.mnt);
3321 unlock_rename(new_dir, old_dir);
3323 path_put(&newnd.path);
3326 path_put(&oldnd.path);
3332 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
3334 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3337 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3341 len = PTR_ERR(link);
3346 if (len > (unsigned) buflen)
3348 if (copy_to_user(buffer, link, len))
3355 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3356 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3357 * using) it for any given inode is up to filesystem.
3359 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3361 struct nameidata nd;
3366 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3368 return PTR_ERR(cookie);
3370 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3371 if (dentry->d_inode->i_op->put_link)
3372 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3376 int vfs_follow_link(struct nameidata *nd, const char *link)
3378 return __vfs_follow_link(nd, link);
3381 /* get the link contents into pagecache */
3382 static char *page_getlink(struct dentry * dentry, struct page **ppage)
3386 struct address_space *mapping = dentry->d_inode->i_mapping;
3387 page = read_mapping_page(mapping, 0, NULL);
3392 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3396 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3398 struct page *page = NULL;
3399 char *s = page_getlink(dentry, &page);
3400 int res = vfs_readlink(dentry,buffer,buflen,s);
3403 page_cache_release(page);
3408 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
3410 struct page *page = NULL;
3411 nd_set_link(nd, page_getlink(dentry, &page));
3415 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
3417 struct page *page = cookie;
3421 page_cache_release(page);
3426 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3428 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
3430 struct address_space *mapping = inode->i_mapping;
3435 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3437 flags |= AOP_FLAG_NOFS;
3440 err = pagecache_write_begin(NULL, mapping, 0, len-1,
3441 flags, &page, &fsdata);
3445 kaddr = kmap_atomic(page);
3446 memcpy(kaddr, symname, len-1);
3447 kunmap_atomic(kaddr);
3449 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3456 mark_inode_dirty(inode);
3462 int page_symlink(struct inode *inode, const char *symname, int len)
3464 return __page_symlink(inode, symname, len,
3465 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
3468 const struct inode_operations page_symlink_inode_operations = {
3469 .readlink = generic_readlink,
3470 .follow_link = page_follow_link_light,
3471 .put_link = page_put_link,
3474 EXPORT_SYMBOL(user_path_at);
3475 EXPORT_SYMBOL(follow_down_one);
3476 EXPORT_SYMBOL(follow_down);
3477 EXPORT_SYMBOL(follow_up);
3478 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3479 EXPORT_SYMBOL(getname);
3480 EXPORT_SYMBOL(lock_rename);
3481 EXPORT_SYMBOL(lookup_one_len);
3482 EXPORT_SYMBOL(page_follow_link_light);
3483 EXPORT_SYMBOL(page_put_link);
3484 EXPORT_SYMBOL(page_readlink);
3485 EXPORT_SYMBOL(__page_symlink);
3486 EXPORT_SYMBOL(page_symlink);
3487 EXPORT_SYMBOL(page_symlink_inode_operations);
3488 EXPORT_SYMBOL(kern_path);
3489 EXPORT_SYMBOL(vfs_path_lookup);
3490 EXPORT_SYMBOL(inode_permission);
3491 EXPORT_SYMBOL(unlock_rename);
3492 EXPORT_SYMBOL(vfs_create);
3493 EXPORT_SYMBOL(vfs_follow_link);
3494 EXPORT_SYMBOL(vfs_link);
3495 EXPORT_SYMBOL(vfs_mkdir);
3496 EXPORT_SYMBOL(vfs_mknod);
3497 EXPORT_SYMBOL(generic_permission);
3498 EXPORT_SYMBOL(vfs_readlink);
3499 EXPORT_SYMBOL(vfs_rename);
3500 EXPORT_SYMBOL(vfs_rmdir);
3501 EXPORT_SYMBOL(vfs_symlink);
3502 EXPORT_SYMBOL(vfs_unlink);
3503 EXPORT_SYMBOL(dentry_unhash);
3504 EXPORT_SYMBOL(generic_readlink);