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 (likely(uid_eq(current_fsuid(), inode->i_uid)))
224 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
225 int error = check_acl(inode, mask);
226 if (error != -EAGAIN)
230 if (in_group_p(inode->i_gid))
235 * If the DACs are ok we don't need any capability check.
237 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
243 * generic_permission - check for access rights on a Posix-like filesystem
244 * @inode: inode to check access rights for
245 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
247 * Used to check for read/write/execute permissions on a file.
248 * We use "fsuid" for this, letting us set arbitrary permissions
249 * for filesystem access without changing the "normal" uids which
250 * are used for other things.
252 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
253 * request cannot be satisfied (eg. requires blocking or too much complexity).
254 * It would then be called again in ref-walk mode.
256 int generic_permission(struct inode *inode, int mask)
261 * Do the basic permission checks.
263 ret = acl_permission_check(inode, mask);
267 if (S_ISDIR(inode->i_mode)) {
268 /* DACs are overridable for directories */
269 if (inode_capable(inode, CAP_DAC_OVERRIDE))
271 if (!(mask & MAY_WRITE))
272 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
277 * Read/write DACs are always overridable.
278 * Executable DACs are overridable when there is
279 * at least one exec bit set.
281 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
282 if (inode_capable(inode, CAP_DAC_OVERRIDE))
286 * Searching includes executable on directories, else just read.
288 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
289 if (mask == MAY_READ)
290 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
297 * We _really_ want to just do "generic_permission()" without
298 * even looking at the inode->i_op values. So we keep a cache
299 * flag in inode->i_opflags, that says "this has not special
300 * permission function, use the fast case".
302 static inline int do_inode_permission(struct inode *inode, int mask)
304 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
305 if (likely(inode->i_op->permission))
306 return inode->i_op->permission(inode, mask);
308 /* This gets set once for the inode lifetime */
309 spin_lock(&inode->i_lock);
310 inode->i_opflags |= IOP_FASTPERM;
311 spin_unlock(&inode->i_lock);
313 return generic_permission(inode, mask);
317 * inode_permission - check for access rights to a given inode
318 * @inode: inode to check permission on
319 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
321 * Used to check for read/write/execute permissions on an inode.
322 * We use "fsuid" for this, letting us set arbitrary permissions
323 * for filesystem access without changing the "normal" uids which
324 * are used for other things.
326 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
328 int inode_permission(struct inode *inode, int mask)
332 if (unlikely(mask & MAY_WRITE)) {
333 umode_t mode = inode->i_mode;
336 * Nobody gets write access to a read-only fs.
338 if (IS_RDONLY(inode) &&
339 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
343 * Nobody gets write access to an immutable file.
345 if (IS_IMMUTABLE(inode))
349 retval = do_inode_permission(inode, mask);
353 retval = devcgroup_inode_permission(inode, mask);
357 return security_inode_permission(inode, mask);
361 * path_get - get a reference to a path
362 * @path: path to get the reference to
364 * Given a path increment the reference count to the dentry and the vfsmount.
366 void path_get(struct path *path)
371 EXPORT_SYMBOL(path_get);
374 * path_put - put a reference to a path
375 * @path: path to put the reference to
377 * Given a path decrement the reference count to the dentry and the vfsmount.
379 void path_put(struct path *path)
384 EXPORT_SYMBOL(path_put);
387 * Path walking has 2 modes, rcu-walk and ref-walk (see
388 * Documentation/filesystems/path-lookup.txt). In situations when we can't
389 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
390 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
391 * mode. Refcounts are grabbed at the last known good point before rcu-walk
392 * got stuck, so ref-walk may continue from there. If this is not successful
393 * (eg. a seqcount has changed), then failure is returned and it's up to caller
394 * to restart the path walk from the beginning in ref-walk mode.
398 * unlazy_walk - try to switch to ref-walk mode.
399 * @nd: nameidata pathwalk data
400 * @dentry: child of nd->path.dentry or NULL
401 * Returns: 0 on success, -ECHILD on failure
403 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
404 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
405 * @nd or NULL. Must be called from rcu-walk context.
407 static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
409 struct fs_struct *fs = current->fs;
410 struct dentry *parent = nd->path.dentry;
413 BUG_ON(!(nd->flags & LOOKUP_RCU));
414 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
416 spin_lock(&fs->lock);
417 if (nd->root.mnt != fs->root.mnt ||
418 nd->root.dentry != fs->root.dentry)
421 spin_lock(&parent->d_lock);
423 if (!__d_rcu_to_refcount(parent, nd->seq))
425 BUG_ON(nd->inode != parent->d_inode);
427 if (dentry->d_parent != parent)
429 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
430 if (!__d_rcu_to_refcount(dentry, nd->seq))
433 * If the sequence check on the child dentry passed, then
434 * the child has not been removed from its parent. This
435 * means the parent dentry must be valid and able to take
436 * a reference at this point.
438 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
439 BUG_ON(!parent->d_count);
441 spin_unlock(&dentry->d_lock);
443 spin_unlock(&parent->d_lock);
446 spin_unlock(&fs->lock);
448 mntget(nd->path.mnt);
451 br_read_unlock(vfsmount_lock);
452 nd->flags &= ~LOOKUP_RCU;
456 spin_unlock(&dentry->d_lock);
458 spin_unlock(&parent->d_lock);
461 spin_unlock(&fs->lock);
466 * release_open_intent - free up open intent resources
467 * @nd: pointer to nameidata
469 void release_open_intent(struct nameidata *nd)
471 struct file *file = nd->intent.open.file;
473 if (file && !IS_ERR(file)) {
474 if (file->f_path.dentry == NULL)
481 static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
483 return dentry->d_op->d_revalidate(dentry, nd);
487 * complete_walk - successful completion of path walk
488 * @nd: pointer nameidata
490 * If we had been in RCU mode, drop out of it and legitimize nd->path.
491 * Revalidate the final result, unless we'd already done that during
492 * the path walk or the filesystem doesn't ask for it. Return 0 on
493 * success, -error on failure. In case of failure caller does not
494 * need to drop nd->path.
496 static int complete_walk(struct nameidata *nd)
498 struct dentry *dentry = nd->path.dentry;
501 if (nd->flags & LOOKUP_RCU) {
502 nd->flags &= ~LOOKUP_RCU;
503 if (!(nd->flags & LOOKUP_ROOT))
505 spin_lock(&dentry->d_lock);
506 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
507 spin_unlock(&dentry->d_lock);
509 br_read_unlock(vfsmount_lock);
512 BUG_ON(nd->inode != dentry->d_inode);
513 spin_unlock(&dentry->d_lock);
514 mntget(nd->path.mnt);
516 br_read_unlock(vfsmount_lock);
519 if (likely(!(nd->flags & LOOKUP_JUMPED)))
522 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
525 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
528 /* Note: we do not d_invalidate() */
529 status = d_revalidate(dentry, nd);
540 static __always_inline void set_root(struct nameidata *nd)
543 get_fs_root(current->fs, &nd->root);
546 static int link_path_walk(const char *, struct nameidata *);
548 static __always_inline void set_root_rcu(struct nameidata *nd)
551 struct fs_struct *fs = current->fs;
555 seq = read_seqcount_begin(&fs->seq);
557 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
558 } while (read_seqcount_retry(&fs->seq, seq));
562 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
574 nd->flags |= LOOKUP_JUMPED;
576 nd->inode = nd->path.dentry->d_inode;
578 ret = link_path_walk(link, nd);
582 return PTR_ERR(link);
585 static void path_put_conditional(struct path *path, struct nameidata *nd)
588 if (path->mnt != nd->path.mnt)
592 static inline void path_to_nameidata(const struct path *path,
593 struct nameidata *nd)
595 if (!(nd->flags & LOOKUP_RCU)) {
596 dput(nd->path.dentry);
597 if (nd->path.mnt != path->mnt)
598 mntput(nd->path.mnt);
600 nd->path.mnt = path->mnt;
601 nd->path.dentry = path->dentry;
604 static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
606 struct inode *inode = link->dentry->d_inode;
607 if (!IS_ERR(cookie) && inode->i_op->put_link)
608 inode->i_op->put_link(link->dentry, nd, cookie);
612 static __always_inline int
613 follow_link(struct path *link, struct nameidata *nd, void **p)
616 struct dentry *dentry = link->dentry;
618 BUG_ON(nd->flags & LOOKUP_RCU);
620 if (link->mnt == nd->path.mnt)
623 if (unlikely(current->total_link_count >= 40)) {
624 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
629 current->total_link_count++;
632 nd_set_link(nd, NULL);
634 error = security_inode_follow_link(link->dentry, nd);
636 *p = ERR_PTR(error); /* no ->put_link(), please */
641 nd->last_type = LAST_BIND;
642 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
645 char *s = nd_get_link(nd);
648 error = __vfs_follow_link(nd, s);
649 else if (nd->last_type == LAST_BIND) {
650 nd->flags |= LOOKUP_JUMPED;
651 nd->inode = nd->path.dentry->d_inode;
652 if (nd->inode->i_op->follow_link) {
653 /* stepped on a _really_ weird one */
662 static int follow_up_rcu(struct path *path)
664 struct mount *mnt = real_mount(path->mnt);
665 struct mount *parent;
666 struct dentry *mountpoint;
668 parent = mnt->mnt_parent;
669 if (&parent->mnt == path->mnt)
671 mountpoint = mnt->mnt_mountpoint;
672 path->dentry = mountpoint;
673 path->mnt = &parent->mnt;
677 int follow_up(struct path *path)
679 struct mount *mnt = real_mount(path->mnt);
680 struct mount *parent;
681 struct dentry *mountpoint;
683 br_read_lock(vfsmount_lock);
684 parent = mnt->mnt_parent;
685 if (&parent->mnt == path->mnt) {
686 br_read_unlock(vfsmount_lock);
689 mntget(&parent->mnt);
690 mountpoint = dget(mnt->mnt_mountpoint);
691 br_read_unlock(vfsmount_lock);
693 path->dentry = mountpoint;
695 path->mnt = &parent->mnt;
700 * Perform an automount
701 * - return -EISDIR to tell follow_managed() to stop and return the path we
704 static int follow_automount(struct path *path, unsigned flags,
707 struct vfsmount *mnt;
710 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
713 /* We don't want to mount if someone's just doing a stat -
714 * unless they're stat'ing a directory and appended a '/' to
717 * We do, however, want to mount if someone wants to open or
718 * create a file of any type under the mountpoint, wants to
719 * traverse through the mountpoint or wants to open the
720 * mounted directory. Also, autofs may mark negative dentries
721 * as being automount points. These will need the attentions
722 * of the daemon to instantiate them before they can be used.
724 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
725 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
726 path->dentry->d_inode)
729 current->total_link_count++;
730 if (current->total_link_count >= 40)
733 mnt = path->dentry->d_op->d_automount(path);
736 * The filesystem is allowed to return -EISDIR here to indicate
737 * it doesn't want to automount. For instance, autofs would do
738 * this so that its userspace daemon can mount on this dentry.
740 * However, we can only permit this if it's a terminal point in
741 * the path being looked up; if it wasn't then the remainder of
742 * the path is inaccessible and we should say so.
744 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
749 if (!mnt) /* mount collision */
753 /* lock_mount() may release path->mnt on error */
757 err = finish_automount(mnt, path);
761 /* Someone else made a mount here whilst we were busy */
766 path->dentry = dget(mnt->mnt_root);
775 * Handle a dentry that is managed in some way.
776 * - Flagged for transit management (autofs)
777 * - Flagged as mountpoint
778 * - Flagged as automount point
780 * This may only be called in refwalk mode.
782 * Serialization is taken care of in namespace.c
784 static int follow_managed(struct path *path, unsigned flags)
786 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
788 bool need_mntput = false;
791 /* Given that we're not holding a lock here, we retain the value in a
792 * local variable for each dentry as we look at it so that we don't see
793 * the components of that value change under us */
794 while (managed = ACCESS_ONCE(path->dentry->d_flags),
795 managed &= DCACHE_MANAGED_DENTRY,
796 unlikely(managed != 0)) {
797 /* Allow the filesystem to manage the transit without i_mutex
799 if (managed & DCACHE_MANAGE_TRANSIT) {
800 BUG_ON(!path->dentry->d_op);
801 BUG_ON(!path->dentry->d_op->d_manage);
802 ret = path->dentry->d_op->d_manage(path->dentry, false);
807 /* Transit to a mounted filesystem. */
808 if (managed & DCACHE_MOUNTED) {
809 struct vfsmount *mounted = lookup_mnt(path);
815 path->dentry = dget(mounted->mnt_root);
820 /* Something is mounted on this dentry in another
821 * namespace and/or whatever was mounted there in this
822 * namespace got unmounted before we managed to get the
826 /* Handle an automount point */
827 if (managed & DCACHE_NEED_AUTOMOUNT) {
828 ret = follow_automount(path, flags, &need_mntput);
834 /* We didn't change the current path point */
838 if (need_mntput && path->mnt == mnt)
842 return ret < 0 ? ret : need_mntput;
845 int follow_down_one(struct path *path)
847 struct vfsmount *mounted;
849 mounted = lookup_mnt(path);
854 path->dentry = dget(mounted->mnt_root);
860 static inline bool managed_dentry_might_block(struct dentry *dentry)
862 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
863 dentry->d_op->d_manage(dentry, true) < 0);
867 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
868 * we meet a managed dentry that would need blocking.
870 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
871 struct inode **inode)
874 struct mount *mounted;
876 * Don't forget we might have a non-mountpoint managed dentry
877 * that wants to block transit.
879 if (unlikely(managed_dentry_might_block(path->dentry)))
882 if (!d_mountpoint(path->dentry))
885 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
888 path->mnt = &mounted->mnt;
889 path->dentry = mounted->mnt.mnt_root;
890 nd->flags |= LOOKUP_JUMPED;
891 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
893 * Update the inode too. We don't need to re-check the
894 * dentry sequence number here after this d_inode read,
895 * because a mount-point is always pinned.
897 *inode = path->dentry->d_inode;
902 static void follow_mount_rcu(struct nameidata *nd)
904 while (d_mountpoint(nd->path.dentry)) {
905 struct mount *mounted;
906 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
909 nd->path.mnt = &mounted->mnt;
910 nd->path.dentry = mounted->mnt.mnt_root;
911 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
915 static int follow_dotdot_rcu(struct nameidata *nd)
920 if (nd->path.dentry == nd->root.dentry &&
921 nd->path.mnt == nd->root.mnt) {
924 if (nd->path.dentry != nd->path.mnt->mnt_root) {
925 struct dentry *old = nd->path.dentry;
926 struct dentry *parent = old->d_parent;
929 seq = read_seqcount_begin(&parent->d_seq);
930 if (read_seqcount_retry(&old->d_seq, nd->seq))
932 nd->path.dentry = parent;
936 if (!follow_up_rcu(&nd->path))
938 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
940 follow_mount_rcu(nd);
941 nd->inode = nd->path.dentry->d_inode;
945 nd->flags &= ~LOOKUP_RCU;
946 if (!(nd->flags & LOOKUP_ROOT))
949 br_read_unlock(vfsmount_lock);
954 * Follow down to the covering mount currently visible to userspace. At each
955 * point, the filesystem owning that dentry may be queried as to whether the
956 * caller is permitted to proceed or not.
958 int follow_down(struct path *path)
963 while (managed = ACCESS_ONCE(path->dentry->d_flags),
964 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
965 /* Allow the filesystem to manage the transit without i_mutex
968 * We indicate to the filesystem if someone is trying to mount
969 * something here. This gives autofs the chance to deny anyone
970 * other than its daemon the right to mount on its
973 * The filesystem may sleep at this point.
975 if (managed & DCACHE_MANAGE_TRANSIT) {
976 BUG_ON(!path->dentry->d_op);
977 BUG_ON(!path->dentry->d_op->d_manage);
978 ret = path->dentry->d_op->d_manage(
979 path->dentry, false);
981 return ret == -EISDIR ? 0 : ret;
984 /* Transit to a mounted filesystem. */
985 if (managed & DCACHE_MOUNTED) {
986 struct vfsmount *mounted = lookup_mnt(path);
992 path->dentry = dget(mounted->mnt_root);
996 /* Don't handle automount points here */
1003 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1005 static void follow_mount(struct path *path)
1007 while (d_mountpoint(path->dentry)) {
1008 struct vfsmount *mounted = lookup_mnt(path);
1013 path->mnt = mounted;
1014 path->dentry = dget(mounted->mnt_root);
1018 static void follow_dotdot(struct nameidata *nd)
1023 struct dentry *old = nd->path.dentry;
1025 if (nd->path.dentry == nd->root.dentry &&
1026 nd->path.mnt == nd->root.mnt) {
1029 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1030 /* rare case of legitimate dget_parent()... */
1031 nd->path.dentry = dget_parent(nd->path.dentry);
1035 if (!follow_up(&nd->path))
1038 follow_mount(&nd->path);
1039 nd->inode = nd->path.dentry->d_inode;
1043 * This looks up the name in dcache, possibly revalidates the old dentry and
1044 * allocates a new one if not found or not valid. In the need_lookup argument
1045 * returns whether i_op->lookup is necessary.
1047 * dir->d_inode->i_mutex must be held
1049 static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1050 struct nameidata *nd, bool *need_lookup)
1052 struct dentry *dentry;
1055 *need_lookup = false;
1056 dentry = d_lookup(dir, name);
1058 if (d_need_lookup(dentry)) {
1059 *need_lookup = true;
1060 } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1061 error = d_revalidate(dentry, nd);
1062 if (unlikely(error <= 0)) {
1065 return ERR_PTR(error);
1066 } else if (!d_invalidate(dentry)) {
1075 dentry = d_alloc(dir, name);
1076 if (unlikely(!dentry))
1077 return ERR_PTR(-ENOMEM);
1079 *need_lookup = true;
1085 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1086 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1088 * dir->d_inode->i_mutex must be held
1090 static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
1091 struct nameidata *nd)
1095 /* Don't create child dentry for a dead directory. */
1096 if (unlikely(IS_DEADDIR(dir))) {
1098 return ERR_PTR(-ENOENT);
1101 old = dir->i_op->lookup(dir, dentry, nd);
1102 if (unlikely(old)) {
1109 static struct dentry *__lookup_hash(struct qstr *name,
1110 struct dentry *base, struct nameidata *nd)
1113 struct dentry *dentry;
1115 dentry = lookup_dcache(name, base, nd, &need_lookup);
1119 return lookup_real(base->d_inode, dentry, nd);
1123 * It's more convoluted than I'd like it to be, but... it's still fairly
1124 * small and for now I'd prefer to have fast path as straight as possible.
1125 * It _is_ time-critical.
1127 static int do_lookup(struct nameidata *nd, struct qstr *name,
1128 struct path *path, struct inode **inode)
1130 struct vfsmount *mnt = nd->path.mnt;
1131 struct dentry *dentry, *parent = nd->path.dentry;
1137 * Rename seqlock is not required here because in the off chance
1138 * of a false negative due to a concurrent rename, we're going to
1139 * do the non-racy lookup, below.
1141 if (nd->flags & LOOKUP_RCU) {
1143 dentry = __d_lookup_rcu(parent, name, &seq, nd->inode);
1148 * This sequence count validates that the inode matches
1149 * the dentry name information from lookup.
1151 *inode = dentry->d_inode;
1152 if (read_seqcount_retry(&dentry->d_seq, seq))
1156 * This sequence count validates that the parent had no
1157 * changes while we did the lookup of the dentry above.
1159 * The memory barrier in read_seqcount_begin of child is
1160 * enough, we can use __read_seqcount_retry here.
1162 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1166 if (unlikely(d_need_lookup(dentry)))
1168 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1169 status = d_revalidate(dentry, nd);
1170 if (unlikely(status <= 0)) {
1171 if (status != -ECHILD)
1177 path->dentry = dentry;
1178 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1180 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1184 if (unlazy_walk(nd, dentry))
1187 dentry = __d_lookup(parent, name);
1190 if (unlikely(!dentry))
1193 if (unlikely(d_need_lookup(dentry))) {
1198 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1199 status = d_revalidate(dentry, nd);
1200 if (unlikely(status <= 0)) {
1205 if (!d_invalidate(dentry)) {
1212 path->dentry = dentry;
1213 err = follow_managed(path, nd->flags);
1214 if (unlikely(err < 0)) {
1215 path_put_conditional(path, nd);
1219 nd->flags |= LOOKUP_JUMPED;
1220 *inode = path->dentry->d_inode;
1224 BUG_ON(nd->inode != parent->d_inode);
1226 mutex_lock(&parent->d_inode->i_mutex);
1227 dentry = __lookup_hash(name, parent, nd);
1228 mutex_unlock(&parent->d_inode->i_mutex);
1230 return PTR_ERR(dentry);
1234 static inline int may_lookup(struct nameidata *nd)
1236 if (nd->flags & LOOKUP_RCU) {
1237 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
1240 if (unlazy_walk(nd, NULL))
1243 return inode_permission(nd->inode, MAY_EXEC);
1246 static inline int handle_dots(struct nameidata *nd, int type)
1248 if (type == LAST_DOTDOT) {
1249 if (nd->flags & LOOKUP_RCU) {
1250 if (follow_dotdot_rcu(nd))
1258 static void terminate_walk(struct nameidata *nd)
1260 if (!(nd->flags & LOOKUP_RCU)) {
1261 path_put(&nd->path);
1263 nd->flags &= ~LOOKUP_RCU;
1264 if (!(nd->flags & LOOKUP_ROOT))
1265 nd->root.mnt = NULL;
1267 br_read_unlock(vfsmount_lock);
1272 * Do we need to follow links? We _really_ want to be able
1273 * to do this check without having to look at inode->i_op,
1274 * so we keep a cache of "no, this doesn't need follow_link"
1275 * for the common case.
1277 static inline int should_follow_link(struct inode *inode, int follow)
1279 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1280 if (likely(inode->i_op->follow_link))
1283 /* This gets set once for the inode lifetime */
1284 spin_lock(&inode->i_lock);
1285 inode->i_opflags |= IOP_NOFOLLOW;
1286 spin_unlock(&inode->i_lock);
1291 static inline int walk_component(struct nameidata *nd, struct path *path,
1292 struct qstr *name, int type, int follow)
1294 struct inode *inode;
1297 * "." and ".." are special - ".." especially so because it has
1298 * to be able to know about the current root directory and
1299 * parent relationships.
1301 if (unlikely(type != LAST_NORM))
1302 return handle_dots(nd, type);
1303 err = do_lookup(nd, name, path, &inode);
1304 if (unlikely(err)) {
1309 path_to_nameidata(path, nd);
1313 if (should_follow_link(inode, follow)) {
1314 if (nd->flags & LOOKUP_RCU) {
1315 if (unlikely(unlazy_walk(nd, path->dentry))) {
1320 BUG_ON(inode != path->dentry->d_inode);
1323 path_to_nameidata(path, nd);
1329 * This limits recursive symlink follows to 8, while
1330 * limiting consecutive symlinks to 40.
1332 * Without that kind of total limit, nasty chains of consecutive
1333 * symlinks can cause almost arbitrarily long lookups.
1335 static inline int nested_symlink(struct path *path, struct nameidata *nd)
1339 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1340 path_put_conditional(path, nd);
1341 path_put(&nd->path);
1344 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1347 current->link_count++;
1350 struct path link = *path;
1353 res = follow_link(&link, nd, &cookie);
1355 res = walk_component(nd, path, &nd->last,
1356 nd->last_type, LOOKUP_FOLLOW);
1357 put_link(nd, &link, cookie);
1360 current->link_count--;
1366 * We really don't want to look at inode->i_op->lookup
1367 * when we don't have to. So we keep a cache bit in
1368 * the inode ->i_opflags field that says "yes, we can
1369 * do lookup on this inode".
1371 static inline int can_lookup(struct inode *inode)
1373 if (likely(inode->i_opflags & IOP_LOOKUP))
1375 if (likely(!inode->i_op->lookup))
1378 /* We do this once for the lifetime of the inode */
1379 spin_lock(&inode->i_lock);
1380 inode->i_opflags |= IOP_LOOKUP;
1381 spin_unlock(&inode->i_lock);
1386 * We can do the critical dentry name comparison and hashing
1387 * operations one word at a time, but we are limited to:
1389 * - Architectures with fast unaligned word accesses. We could
1390 * do a "get_unaligned()" if this helps and is sufficiently
1393 * - Little-endian machines (so that we can generate the mask
1394 * of low bytes efficiently). Again, we *could* do a byte
1395 * swapping load on big-endian architectures if that is not
1396 * expensive enough to make the optimization worthless.
1398 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1399 * do not trap on the (extremely unlikely) case of a page
1400 * crossing operation.
1402 * - Furthermore, we need an efficient 64-bit compile for the
1403 * 64-bit case in order to generate the "number of bytes in
1404 * the final mask". Again, that could be replaced with a
1405 * efficient population count instruction or similar.
1407 #ifdef CONFIG_DCACHE_WORD_ACCESS
1409 #include <asm/word-at-a-time.h>
1413 static inline unsigned int fold_hash(unsigned long hash)
1415 hash += hash >> (8*sizeof(int));
1419 #else /* 32-bit case */
1421 #define fold_hash(x) (x)
1425 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1427 unsigned long a, mask;
1428 unsigned long hash = 0;
1431 a = load_unaligned_zeropad(name);
1432 if (len < sizeof(unsigned long))
1436 name += sizeof(unsigned long);
1437 len -= sizeof(unsigned long);
1441 mask = ~(~0ul << len*8);
1444 return fold_hash(hash);
1446 EXPORT_SYMBOL(full_name_hash);
1449 * Calculate the length and hash of the path component, and
1450 * return the length of the component;
1452 static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1454 unsigned long a, mask, hash, len;
1457 len = -sizeof(unsigned long);
1459 hash = (hash + a) * 9;
1460 len += sizeof(unsigned long);
1461 a = load_unaligned_zeropad(name+len);
1462 /* Do we have any NUL or '/' bytes in this word? */
1463 mask = has_zero(a) | has_zero(a ^ REPEAT_BYTE('/'));
1466 /* The mask *below* the first high bit set */
1467 mask = (mask - 1) & ~mask;
1470 *hashp = fold_hash(hash);
1472 return len + count_masked_bytes(mask);
1477 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1479 unsigned long hash = init_name_hash();
1481 hash = partial_name_hash(*name++, hash);
1482 return end_name_hash(hash);
1484 EXPORT_SYMBOL(full_name_hash);
1487 * We know there's a real path component here of at least
1490 static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1492 unsigned long hash = init_name_hash();
1493 unsigned long len = 0, c;
1495 c = (unsigned char)*name;
1498 hash = partial_name_hash(c, hash);
1499 c = (unsigned char)name[len];
1500 } while (c && c != '/');
1501 *hashp = end_name_hash(hash);
1509 * This is the basic name resolution function, turning a pathname into
1510 * the final dentry. We expect 'base' to be positive and a directory.
1512 * Returns 0 and nd will have valid dentry and mnt on success.
1513 * Returns error and drops reference to input namei data on failure.
1515 static int link_path_walk(const char *name, struct nameidata *nd)
1525 /* At this point we know we have a real path component. */
1531 err = may_lookup(nd);
1535 len = hash_name(name, &this.hash);
1540 if (name[0] == '.') switch (len) {
1542 if (name[1] == '.') {
1544 nd->flags |= LOOKUP_JUMPED;
1550 if (likely(type == LAST_NORM)) {
1551 struct dentry *parent = nd->path.dentry;
1552 nd->flags &= ~LOOKUP_JUMPED;
1553 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1554 err = parent->d_op->d_hash(parent, nd->inode,
1562 goto last_component;
1564 * If it wasn't NUL, we know it was '/'. Skip that
1565 * slash, and continue until no more slashes.
1569 } while (unlikely(name[len] == '/'));
1571 goto last_component;
1574 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1579 err = nested_symlink(&next, nd);
1583 if (can_lookup(nd->inode))
1587 /* here ends the main loop */
1591 nd->last_type = type;
1598 static int path_init(int dfd, const char *name, unsigned int flags,
1599 struct nameidata *nd, struct file **fp)
1605 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1606 nd->flags = flags | LOOKUP_JUMPED;
1608 if (flags & LOOKUP_ROOT) {
1609 struct inode *inode = nd->root.dentry->d_inode;
1611 if (!inode->i_op->lookup)
1613 retval = inode_permission(inode, MAY_EXEC);
1617 nd->path = nd->root;
1619 if (flags & LOOKUP_RCU) {
1620 br_read_lock(vfsmount_lock);
1622 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1624 path_get(&nd->path);
1629 nd->root.mnt = NULL;
1632 if (flags & LOOKUP_RCU) {
1633 br_read_lock(vfsmount_lock);
1638 path_get(&nd->root);
1640 nd->path = nd->root;
1641 } else if (dfd == AT_FDCWD) {
1642 if (flags & LOOKUP_RCU) {
1643 struct fs_struct *fs = current->fs;
1646 br_read_lock(vfsmount_lock);
1650 seq = read_seqcount_begin(&fs->seq);
1652 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1653 } while (read_seqcount_retry(&fs->seq, seq));
1655 get_fs_pwd(current->fs, &nd->path);
1658 struct dentry *dentry;
1660 file = fget_raw_light(dfd, &fput_needed);
1665 dentry = file->f_path.dentry;
1669 if (!S_ISDIR(dentry->d_inode->i_mode))
1672 retval = inode_permission(dentry->d_inode, MAY_EXEC);
1677 nd->path = file->f_path;
1678 if (flags & LOOKUP_RCU) {
1681 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1682 br_read_lock(vfsmount_lock);
1685 path_get(&file->f_path);
1686 fput_light(file, fput_needed);
1690 nd->inode = nd->path.dentry->d_inode;
1694 fput_light(file, fput_needed);
1699 static inline int lookup_last(struct nameidata *nd, struct path *path)
1701 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1702 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1704 nd->flags &= ~LOOKUP_PARENT;
1705 return walk_component(nd, path, &nd->last, nd->last_type,
1706 nd->flags & LOOKUP_FOLLOW);
1709 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1710 static int path_lookupat(int dfd, const char *name,
1711 unsigned int flags, struct nameidata *nd)
1713 struct file *base = NULL;
1718 * Path walking is largely split up into 2 different synchronisation
1719 * schemes, rcu-walk and ref-walk (explained in
1720 * Documentation/filesystems/path-lookup.txt). These share much of the
1721 * path walk code, but some things particularly setup, cleanup, and
1722 * following mounts are sufficiently divergent that functions are
1723 * duplicated. Typically there is a function foo(), and its RCU
1724 * analogue, foo_rcu().
1726 * -ECHILD is the error number of choice (just to avoid clashes) that
1727 * is returned if some aspect of an rcu-walk fails. Such an error must
1728 * be handled by restarting a traditional ref-walk (which will always
1729 * be able to complete).
1731 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1736 current->total_link_count = 0;
1737 err = link_path_walk(name, nd);
1739 if (!err && !(flags & LOOKUP_PARENT)) {
1740 err = lookup_last(nd, &path);
1743 struct path link = path;
1744 nd->flags |= LOOKUP_PARENT;
1745 err = follow_link(&link, nd, &cookie);
1747 err = lookup_last(nd, &path);
1748 put_link(nd, &link, cookie);
1753 err = complete_walk(nd);
1755 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1756 if (!nd->inode->i_op->lookup) {
1757 path_put(&nd->path);
1765 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
1766 path_put(&nd->root);
1767 nd->root.mnt = NULL;
1772 static int do_path_lookup(int dfd, const char *name,
1773 unsigned int flags, struct nameidata *nd)
1775 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1776 if (unlikely(retval == -ECHILD))
1777 retval = path_lookupat(dfd, name, flags, nd);
1778 if (unlikely(retval == -ESTALE))
1779 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1781 if (likely(!retval)) {
1782 if (unlikely(!audit_dummy_context())) {
1783 if (nd->path.dentry && nd->inode)
1784 audit_inode(name, nd->path.dentry);
1790 int kern_path_parent(const char *name, struct nameidata *nd)
1792 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
1795 int kern_path(const char *name, unsigned int flags, struct path *path)
1797 struct nameidata nd;
1798 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1805 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1806 * @dentry: pointer to dentry of the base directory
1807 * @mnt: pointer to vfs mount of the base directory
1808 * @name: pointer to file name
1809 * @flags: lookup flags
1810 * @path: pointer to struct path to fill
1812 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1813 const char *name, unsigned int flags,
1816 struct nameidata nd;
1818 nd.root.dentry = dentry;
1820 BUG_ON(flags & LOOKUP_PARENT);
1821 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1822 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1829 * Restricted form of lookup. Doesn't follow links, single-component only,
1830 * needs parent already locked. Doesn't follow mounts.
1833 static struct dentry *lookup_hash(struct nameidata *nd)
1835 return __lookup_hash(&nd->last, nd->path.dentry, nd);
1839 * lookup_one_len - filesystem helper to lookup single pathname component
1840 * @name: pathname component to lookup
1841 * @base: base directory to lookup from
1842 * @len: maximum length @len should be interpreted to
1844 * Note that this routine is purely a helper for filesystem usage and should
1845 * not be called by generic code. Also note that by using this function the
1846 * nameidata argument is passed to the filesystem methods and a filesystem
1847 * using this helper needs to be prepared for that.
1849 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1855 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1859 this.hash = full_name_hash(name, len);
1861 return ERR_PTR(-EACCES);
1864 c = *(const unsigned char *)name++;
1865 if (c == '/' || c == '\0')
1866 return ERR_PTR(-EACCES);
1869 * See if the low-level filesystem might want
1870 * to use its own hash..
1872 if (base->d_flags & DCACHE_OP_HASH) {
1873 int err = base->d_op->d_hash(base, base->d_inode, &this);
1875 return ERR_PTR(err);
1878 err = inode_permission(base->d_inode, MAY_EXEC);
1880 return ERR_PTR(err);
1882 return __lookup_hash(&this, base, NULL);
1885 int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
1886 struct path *path, int *empty)
1888 struct nameidata nd;
1889 char *tmp = getname_flags(name, flags, empty);
1890 int err = PTR_ERR(tmp);
1893 BUG_ON(flags & LOOKUP_PARENT);
1895 err = do_path_lookup(dfd, tmp, flags, &nd);
1903 int user_path_at(int dfd, const char __user *name, unsigned flags,
1906 return user_path_at_empty(dfd, name, flags, path, NULL);
1909 static int user_path_parent(int dfd, const char __user *path,
1910 struct nameidata *nd, char **name)
1912 char *s = getname(path);
1918 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1928 * It's inline, so penalty for filesystems that don't use sticky bit is
1931 static inline int check_sticky(struct inode *dir, struct inode *inode)
1933 kuid_t fsuid = current_fsuid();
1935 if (!(dir->i_mode & S_ISVTX))
1937 if (uid_eq(inode->i_uid, fsuid))
1939 if (uid_eq(dir->i_uid, fsuid))
1941 return !inode_capable(inode, CAP_FOWNER);
1945 * Check whether we can remove a link victim from directory dir, check
1946 * whether the type of victim is right.
1947 * 1. We can't do it if dir is read-only (done in permission())
1948 * 2. We should have write and exec permissions on dir
1949 * 3. We can't remove anything from append-only dir
1950 * 4. We can't do anything with immutable dir (done in permission())
1951 * 5. If the sticky bit on dir is set we should either
1952 * a. be owner of dir, or
1953 * b. be owner of victim, or
1954 * c. have CAP_FOWNER capability
1955 * 6. If the victim is append-only or immutable we can't do antyhing with
1956 * links pointing to it.
1957 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1958 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1959 * 9. We can't remove a root or mountpoint.
1960 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1961 * nfs_async_unlink().
1963 static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1967 if (!victim->d_inode)
1970 BUG_ON(victim->d_parent->d_inode != dir);
1971 audit_inode_child(victim, dir);
1973 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1978 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1979 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
1982 if (!S_ISDIR(victim->d_inode->i_mode))
1984 if (IS_ROOT(victim))
1986 } else if (S_ISDIR(victim->d_inode->i_mode))
1988 if (IS_DEADDIR(dir))
1990 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1995 /* Check whether we can create an object with dentry child in directory
1997 * 1. We can't do it if child already exists (open has special treatment for
1998 * this case, but since we are inlined it's OK)
1999 * 2. We can't do it if dir is read-only (done in permission())
2000 * 3. We should have write and exec permissions on dir
2001 * 4. We can't do it if dir is immutable (done in permission())
2003 static inline int may_create(struct inode *dir, struct dentry *child)
2007 if (IS_DEADDIR(dir))
2009 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
2013 * p1 and p2 should be directories on the same fs.
2015 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2020 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2024 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2026 p = d_ancestor(p2, p1);
2028 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2029 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2033 p = d_ancestor(p1, p2);
2035 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2036 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2040 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2041 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2045 void unlock_rename(struct dentry *p1, struct dentry *p2)
2047 mutex_unlock(&p1->d_inode->i_mutex);
2049 mutex_unlock(&p2->d_inode->i_mutex);
2050 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2054 int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2055 struct nameidata *nd)
2057 int error = may_create(dir, dentry);
2062 if (!dir->i_op->create)
2063 return -EACCES; /* shouldn't it be ENOSYS? */
2066 error = security_inode_create(dir, dentry, mode);
2069 error = dir->i_op->create(dir, dentry, mode, nd);
2071 fsnotify_create(dir, dentry);
2075 static int may_open(struct path *path, int acc_mode, int flag)
2077 struct dentry *dentry = path->dentry;
2078 struct inode *inode = dentry->d_inode;
2088 switch (inode->i_mode & S_IFMT) {
2092 if (acc_mode & MAY_WRITE)
2097 if (path->mnt->mnt_flags & MNT_NODEV)
2106 error = inode_permission(inode, acc_mode);
2111 * An append-only file must be opened in append mode for writing.
2113 if (IS_APPEND(inode)) {
2114 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
2120 /* O_NOATIME can only be set by the owner or superuser */
2121 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
2127 static int handle_truncate(struct file *filp)
2129 struct path *path = &filp->f_path;
2130 struct inode *inode = path->dentry->d_inode;
2131 int error = get_write_access(inode);
2135 * Refuse to truncate files with mandatory locks held on them.
2137 error = locks_verify_locked(inode);
2139 error = security_path_truncate(path);
2141 error = do_truncate(path->dentry, 0,
2142 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2145 put_write_access(inode);
2149 static inline int open_to_namei_flags(int flag)
2151 if ((flag & O_ACCMODE) == 3)
2157 * Handle the last step of open()
2159 static struct file *do_last(struct nameidata *nd, struct path *path,
2160 const struct open_flags *op, const char *pathname)
2162 struct dentry *dir = nd->path.dentry;
2163 struct dentry *dentry;
2164 int open_flag = op->open_flag;
2165 int will_truncate = open_flag & O_TRUNC;
2167 int acc_mode = op->acc_mode;
2171 nd->flags &= ~LOOKUP_PARENT;
2172 nd->flags |= op->intent;
2174 switch (nd->last_type) {
2177 error = handle_dots(nd, nd->last_type);
2179 return ERR_PTR(error);
2182 error = complete_walk(nd);
2184 return ERR_PTR(error);
2185 audit_inode(pathname, nd->path.dentry);
2186 if (open_flag & O_CREAT) {
2192 error = complete_walk(nd);
2194 return ERR_PTR(error);
2195 audit_inode(pathname, dir);
2199 if (!(open_flag & O_CREAT)) {
2201 if (nd->last.name[nd->last.len])
2202 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2203 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2205 /* we _can_ be in RCU mode here */
2206 error = walk_component(nd, path, &nd->last, LAST_NORM,
2209 return ERR_PTR(error);
2210 if (error) /* symlink */
2213 error = complete_walk(nd);
2215 return ERR_PTR(error);
2218 if (nd->flags & LOOKUP_DIRECTORY) {
2219 if (!nd->inode->i_op->lookup)
2222 audit_inode(pathname, nd->path.dentry);
2226 /* create side of things */
2228 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED has been
2229 * cleared when we got to the last component we are about to look up
2231 error = complete_walk(nd);
2233 return ERR_PTR(error);
2235 audit_inode(pathname, dir);
2237 /* trailing slashes? */
2238 if (nd->last.name[nd->last.len])
2241 mutex_lock(&dir->d_inode->i_mutex);
2243 dentry = lookup_hash(nd);
2244 error = PTR_ERR(dentry);
2245 if (IS_ERR(dentry)) {
2246 mutex_unlock(&dir->d_inode->i_mutex);
2250 path->dentry = dentry;
2251 path->mnt = nd->path.mnt;
2253 /* Negative dentry, just create the file */
2254 if (!dentry->d_inode) {
2255 umode_t mode = op->mode;
2256 if (!IS_POSIXACL(dir->d_inode))
2257 mode &= ~current_umask();
2259 * This write is needed to ensure that a
2260 * rw->ro transition does not occur between
2261 * the time when the file is created and when
2262 * a permanent write count is taken through
2263 * the 'struct file' in nameidata_to_filp().
2265 error = mnt_want_write(nd->path.mnt);
2267 goto exit_mutex_unlock;
2269 /* Don't check for write permission, don't truncate */
2270 open_flag &= ~O_TRUNC;
2272 acc_mode = MAY_OPEN;
2273 error = security_path_mknod(&nd->path, dentry, mode, 0);
2275 goto exit_mutex_unlock;
2276 error = vfs_create(dir->d_inode, dentry, mode, nd);
2278 goto exit_mutex_unlock;
2279 mutex_unlock(&dir->d_inode->i_mutex);
2280 dput(nd->path.dentry);
2281 nd->path.dentry = dentry;
2286 * It already exists.
2288 mutex_unlock(&dir->d_inode->i_mutex);
2289 audit_inode(pathname, path->dentry);
2292 if (open_flag & O_EXCL)
2295 error = follow_managed(path, nd->flags);
2300 nd->flags |= LOOKUP_JUMPED;
2303 if (!path->dentry->d_inode)
2306 if (path->dentry->d_inode->i_op->follow_link)
2309 path_to_nameidata(path, nd);
2310 nd->inode = path->dentry->d_inode;
2311 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
2312 error = complete_walk(nd);
2314 return ERR_PTR(error);
2316 if (S_ISDIR(nd->inode->i_mode))
2319 if (!S_ISREG(nd->inode->i_mode))
2322 if (will_truncate) {
2323 error = mnt_want_write(nd->path.mnt);
2329 error = may_open(&nd->path, acc_mode, open_flag);
2332 filp = nameidata_to_filp(nd);
2333 if (!IS_ERR(filp)) {
2334 error = ima_file_check(filp, op->acc_mode);
2337 filp = ERR_PTR(error);
2340 if (!IS_ERR(filp)) {
2341 if (will_truncate) {
2342 error = handle_truncate(filp);
2345 filp = ERR_PTR(error);
2351 mnt_drop_write(nd->path.mnt);
2352 path_put(&nd->path);
2356 mutex_unlock(&dir->d_inode->i_mutex);
2358 path_put_conditional(path, nd);
2360 filp = ERR_PTR(error);
2364 static struct file *path_openat(int dfd, const char *pathname,
2365 struct nameidata *nd, const struct open_flags *op, int flags)
2367 struct file *base = NULL;
2372 filp = get_empty_filp();
2374 return ERR_PTR(-ENFILE);
2376 filp->f_flags = op->open_flag;
2377 nd->intent.open.file = filp;
2378 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2379 nd->intent.open.create_mode = op->mode;
2381 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
2382 if (unlikely(error))
2385 current->total_link_count = 0;
2386 error = link_path_walk(pathname, nd);
2387 if (unlikely(error))
2390 filp = do_last(nd, &path, op, pathname);
2391 while (unlikely(!filp)) { /* trailing symlink */
2392 struct path link = path;
2394 if (!(nd->flags & LOOKUP_FOLLOW)) {
2395 path_put_conditional(&path, nd);
2396 path_put(&nd->path);
2397 filp = ERR_PTR(-ELOOP);
2400 nd->flags |= LOOKUP_PARENT;
2401 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2402 error = follow_link(&link, nd, &cookie);
2403 if (unlikely(error))
2404 filp = ERR_PTR(error);
2406 filp = do_last(nd, &path, op, pathname);
2407 put_link(nd, &link, cookie);
2410 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2411 path_put(&nd->root);
2414 release_open_intent(nd);
2418 filp = ERR_PTR(error);
2422 struct file *do_filp_open(int dfd, const char *pathname,
2423 const struct open_flags *op, int flags)
2425 struct nameidata nd;
2428 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
2429 if (unlikely(filp == ERR_PTR(-ECHILD)))
2430 filp = path_openat(dfd, pathname, &nd, op, flags);
2431 if (unlikely(filp == ERR_PTR(-ESTALE)))
2432 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
2436 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2437 const char *name, const struct open_flags *op, int flags)
2439 struct nameidata nd;
2443 nd.root.dentry = dentry;
2445 flags |= LOOKUP_ROOT;
2447 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
2448 return ERR_PTR(-ELOOP);
2450 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2451 if (unlikely(file == ERR_PTR(-ECHILD)))
2452 file = path_openat(-1, name, &nd, op, flags);
2453 if (unlikely(file == ERR_PTR(-ESTALE)))
2454 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2458 struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
2460 struct dentry *dentry = ERR_PTR(-EEXIST);
2461 struct nameidata nd;
2462 int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2464 return ERR_PTR(error);
2467 * Yucky last component or no last component at all?
2468 * (foo/., foo/.., /////)
2470 if (nd.last_type != LAST_NORM)
2472 nd.flags &= ~LOOKUP_PARENT;
2473 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2474 nd.intent.open.flags = O_EXCL;
2477 * Do the final lookup.
2479 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2480 dentry = lookup_hash(&nd);
2484 if (dentry->d_inode)
2487 * Special case - lookup gave negative, but... we had foo/bar/
2488 * From the vfs_mknod() POV we just have a negative dentry -
2489 * all is fine. Let's be bastards - you had / on the end, you've
2490 * been asking for (non-existent) directory. -ENOENT for you.
2492 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
2494 dentry = ERR_PTR(-ENOENT);
2501 dentry = ERR_PTR(-EEXIST);
2503 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2508 EXPORT_SYMBOL(kern_path_create);
2510 struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2512 char *tmp = getname(pathname);
2515 return ERR_CAST(tmp);
2516 res = kern_path_create(dfd, tmp, path, is_dir);
2520 EXPORT_SYMBOL(user_path_create);
2522 int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
2524 int error = may_create(dir, dentry);
2529 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
2532 if (!dir->i_op->mknod)
2535 error = devcgroup_inode_mknod(mode, dev);
2539 error = security_inode_mknod(dir, dentry, mode, dev);
2543 error = dir->i_op->mknod(dir, dentry, mode, dev);
2545 fsnotify_create(dir, dentry);
2549 static int may_mknod(umode_t mode)
2551 switch (mode & S_IFMT) {
2557 case 0: /* zero mode translates to S_IFREG */
2566 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
2569 struct dentry *dentry;
2576 dentry = user_path_create(dfd, filename, &path, 0);
2578 return PTR_ERR(dentry);
2580 if (!IS_POSIXACL(path.dentry->d_inode))
2581 mode &= ~current_umask();
2582 error = may_mknod(mode);
2585 error = mnt_want_write(path.mnt);
2588 error = security_path_mknod(&path, dentry, mode, dev);
2590 goto out_drop_write;
2591 switch (mode & S_IFMT) {
2592 case 0: case S_IFREG:
2593 error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
2595 case S_IFCHR: case S_IFBLK:
2596 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
2597 new_decode_dev(dev));
2599 case S_IFIFO: case S_IFSOCK:
2600 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
2604 mnt_drop_write(path.mnt);
2607 mutex_unlock(&path.dentry->d_inode->i_mutex);
2613 SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
2615 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2618 int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2620 int error = may_create(dir, dentry);
2621 unsigned max_links = dir->i_sb->s_max_links;
2626 if (!dir->i_op->mkdir)
2629 mode &= (S_IRWXUGO|S_ISVTX);
2630 error = security_inode_mkdir(dir, dentry, mode);
2634 if (max_links && dir->i_nlink >= max_links)
2637 error = dir->i_op->mkdir(dir, dentry, mode);
2639 fsnotify_mkdir(dir, dentry);
2643 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
2645 struct dentry *dentry;
2649 dentry = user_path_create(dfd, pathname, &path, 1);
2651 return PTR_ERR(dentry);
2653 if (!IS_POSIXACL(path.dentry->d_inode))
2654 mode &= ~current_umask();
2655 error = mnt_want_write(path.mnt);
2658 error = security_path_mkdir(&path, dentry, mode);
2660 goto out_drop_write;
2661 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
2663 mnt_drop_write(path.mnt);
2666 mutex_unlock(&path.dentry->d_inode->i_mutex);
2671 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
2673 return sys_mkdirat(AT_FDCWD, pathname, mode);
2677 * The dentry_unhash() helper will try to drop the dentry early: we
2678 * should have a usage count of 1 if we're the only user of this
2679 * dentry, and if that is true (possibly after pruning the dcache),
2680 * then we drop the dentry now.
2682 * A low-level filesystem can, if it choses, legally
2685 * if (!d_unhashed(dentry))
2688 * if it cannot handle the case of removing a directory
2689 * that is still in use by something else..
2691 void dentry_unhash(struct dentry *dentry)
2693 shrink_dcache_parent(dentry);
2694 spin_lock(&dentry->d_lock);
2695 if (dentry->d_count == 1)
2697 spin_unlock(&dentry->d_lock);
2700 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2702 int error = may_delete(dir, dentry, 1);
2707 if (!dir->i_op->rmdir)
2711 mutex_lock(&dentry->d_inode->i_mutex);
2714 if (d_mountpoint(dentry))
2717 error = security_inode_rmdir(dir, dentry);
2721 shrink_dcache_parent(dentry);
2722 error = dir->i_op->rmdir(dir, dentry);
2726 dentry->d_inode->i_flags |= S_DEAD;
2730 mutex_unlock(&dentry->d_inode->i_mutex);
2737 static long do_rmdir(int dfd, const char __user *pathname)
2741 struct dentry *dentry;
2742 struct nameidata nd;
2744 error = user_path_parent(dfd, pathname, &nd, &name);
2748 switch(nd.last_type) {
2760 nd.flags &= ~LOOKUP_PARENT;
2762 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2763 dentry = lookup_hash(&nd);
2764 error = PTR_ERR(dentry);
2767 if (!dentry->d_inode) {
2771 error = mnt_want_write(nd.path.mnt);
2774 error = security_path_rmdir(&nd.path, dentry);
2777 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2779 mnt_drop_write(nd.path.mnt);
2783 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2790 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
2792 return do_rmdir(AT_FDCWD, pathname);
2795 int vfs_unlink(struct inode *dir, struct dentry *dentry)
2797 int error = may_delete(dir, dentry, 0);
2802 if (!dir->i_op->unlink)
2805 mutex_lock(&dentry->d_inode->i_mutex);
2806 if (d_mountpoint(dentry))
2809 error = security_inode_unlink(dir, dentry);
2811 error = dir->i_op->unlink(dir, dentry);
2816 mutex_unlock(&dentry->d_inode->i_mutex);
2818 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2819 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2820 fsnotify_link_count(dentry->d_inode);
2828 * Make sure that the actual truncation of the file will occur outside its
2829 * directory's i_mutex. Truncate can take a long time if there is a lot of
2830 * writeout happening, and we don't want to prevent access to the directory
2831 * while waiting on the I/O.
2833 static long do_unlinkat(int dfd, const char __user *pathname)
2837 struct dentry *dentry;
2838 struct nameidata nd;
2839 struct inode *inode = NULL;
2841 error = user_path_parent(dfd, pathname, &nd, &name);
2846 if (nd.last_type != LAST_NORM)
2849 nd.flags &= ~LOOKUP_PARENT;
2851 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2852 dentry = lookup_hash(&nd);
2853 error = PTR_ERR(dentry);
2854 if (!IS_ERR(dentry)) {
2855 /* Why not before? Because we want correct error value */
2856 if (nd.last.name[nd.last.len])
2858 inode = dentry->d_inode;
2862 error = mnt_want_write(nd.path.mnt);
2865 error = security_path_unlink(&nd.path, dentry);
2868 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2870 mnt_drop_write(nd.path.mnt);
2874 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2876 iput(inode); /* truncate the inode here */
2883 error = !dentry->d_inode ? -ENOENT :
2884 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2888 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
2890 if ((flag & ~AT_REMOVEDIR) != 0)
2893 if (flag & AT_REMOVEDIR)
2894 return do_rmdir(dfd, pathname);
2896 return do_unlinkat(dfd, pathname);
2899 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
2901 return do_unlinkat(AT_FDCWD, pathname);
2904 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
2906 int error = may_create(dir, dentry);
2911 if (!dir->i_op->symlink)
2914 error = security_inode_symlink(dir, dentry, oldname);
2918 error = dir->i_op->symlink(dir, dentry, oldname);
2920 fsnotify_create(dir, dentry);
2924 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2925 int, newdfd, const char __user *, newname)
2929 struct dentry *dentry;
2932 from = getname(oldname);
2934 return PTR_ERR(from);
2936 dentry = user_path_create(newdfd, newname, &path, 0);
2937 error = PTR_ERR(dentry);
2941 error = mnt_want_write(path.mnt);
2944 error = security_path_symlink(&path, dentry, from);
2946 goto out_drop_write;
2947 error = vfs_symlink(path.dentry->d_inode, dentry, from);
2949 mnt_drop_write(path.mnt);
2952 mutex_unlock(&path.dentry->d_inode->i_mutex);
2959 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
2961 return sys_symlinkat(oldname, AT_FDCWD, newname);
2964 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2966 struct inode *inode = old_dentry->d_inode;
2967 unsigned max_links = dir->i_sb->s_max_links;
2973 error = may_create(dir, new_dentry);
2977 if (dir->i_sb != inode->i_sb)
2981 * A link to an append-only or immutable file cannot be created.
2983 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2985 if (!dir->i_op->link)
2987 if (S_ISDIR(inode->i_mode))
2990 error = security_inode_link(old_dentry, dir, new_dentry);
2994 mutex_lock(&inode->i_mutex);
2995 /* Make sure we don't allow creating hardlink to an unlinked file */
2996 if (inode->i_nlink == 0)
2998 else if (max_links && inode->i_nlink >= max_links)
3001 error = dir->i_op->link(old_dentry, dir, new_dentry);
3002 mutex_unlock(&inode->i_mutex);
3004 fsnotify_link(dir, inode, new_dentry);
3009 * Hardlinks are often used in delicate situations. We avoid
3010 * security-related surprises by not following symlinks on the
3013 * We don't follow them on the oldname either to be compatible
3014 * with linux 2.0, and to avoid hard-linking to directories
3015 * and other special files. --ADM
3017 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3018 int, newdfd, const char __user *, newname, int, flags)
3020 struct dentry *new_dentry;
3021 struct path old_path, new_path;
3025 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3028 * To use null names we require CAP_DAC_READ_SEARCH
3029 * This ensures that not everyone will be able to create
3030 * handlink using the passed filedescriptor.
3032 if (flags & AT_EMPTY_PATH) {
3033 if (!capable(CAP_DAC_READ_SEARCH))
3038 if (flags & AT_SYMLINK_FOLLOW)
3039 how |= LOOKUP_FOLLOW;
3041 error = user_path_at(olddfd, oldname, how, &old_path);
3045 new_dentry = user_path_create(newdfd, newname, &new_path, 0);
3046 error = PTR_ERR(new_dentry);
3047 if (IS_ERR(new_dentry))
3051 if (old_path.mnt != new_path.mnt)
3053 error = mnt_want_write(new_path.mnt);
3056 error = security_path_link(old_path.dentry, &new_path, new_dentry);
3058 goto out_drop_write;
3059 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
3061 mnt_drop_write(new_path.mnt);
3064 mutex_unlock(&new_path.dentry->d_inode->i_mutex);
3065 path_put(&new_path);
3067 path_put(&old_path);
3072 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
3074 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
3078 * The worst of all namespace operations - renaming directory. "Perverted"
3079 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3081 * a) we can get into loop creation. Check is done in is_subdir().
3082 * b) race potential - two innocent renames can create a loop together.
3083 * That's where 4.4 screws up. Current fix: serialization on
3084 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
3086 * c) we have to lock _three_ objects - parents and victim (if it exists).
3087 * And that - after we got ->i_mutex on parents (until then we don't know
3088 * whether the target exists). Solution: try to be smart with locking
3089 * order for inodes. We rely on the fact that tree topology may change
3090 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
3091 * move will be locked. Thus we can rank directories by the tree
3092 * (ancestors first) and rank all non-directories after them.
3093 * That works since everybody except rename does "lock parent, lookup,
3094 * lock child" and rename is under ->s_vfs_rename_mutex.
3095 * HOWEVER, it relies on the assumption that any object with ->lookup()
3096 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3097 * we'd better make sure that there's no link(2) for them.
3098 * d) conversion from fhandle to dentry may come in the wrong moment - when
3099 * we are removing the target. Solution: we will have to grab ->i_mutex
3100 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3101 * ->i_mutex on parents, which works but leads to some truly excessive
3104 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3105 struct inode *new_dir, struct dentry *new_dentry)
3108 struct inode *target = new_dentry->d_inode;
3109 unsigned max_links = new_dir->i_sb->s_max_links;
3112 * If we are going to change the parent - check write permissions,
3113 * we'll need to flip '..'.
3115 if (new_dir != old_dir) {
3116 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
3121 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3127 mutex_lock(&target->i_mutex);
3130 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3134 if (max_links && !target && new_dir != old_dir &&
3135 new_dir->i_nlink >= max_links)
3139 shrink_dcache_parent(new_dentry);
3140 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3145 target->i_flags |= S_DEAD;
3146 dont_mount(new_dentry);
3150 mutex_unlock(&target->i_mutex);
3153 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3154 d_move(old_dentry,new_dentry);
3158 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3159 struct inode *new_dir, struct dentry *new_dentry)
3161 struct inode *target = new_dentry->d_inode;
3164 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3170 mutex_lock(&target->i_mutex);
3173 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3176 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3181 dont_mount(new_dentry);
3182 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3183 d_move(old_dentry, new_dentry);
3186 mutex_unlock(&target->i_mutex);
3191 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3192 struct inode *new_dir, struct dentry *new_dentry)
3195 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
3196 const unsigned char *old_name;
3198 if (old_dentry->d_inode == new_dentry->d_inode)
3201 error = may_delete(old_dir, old_dentry, is_dir);
3205 if (!new_dentry->d_inode)
3206 error = may_create(new_dir, new_dentry);
3208 error = may_delete(new_dir, new_dentry, is_dir);
3212 if (!old_dir->i_op->rename)
3215 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3218 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3220 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3222 fsnotify_move(old_dir, new_dir, old_name, is_dir,
3223 new_dentry->d_inode, old_dentry);
3224 fsnotify_oldname_free(old_name);
3229 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3230 int, newdfd, const char __user *, newname)
3232 struct dentry *old_dir, *new_dir;
3233 struct dentry *old_dentry, *new_dentry;
3234 struct dentry *trap;
3235 struct nameidata oldnd, newnd;
3240 error = user_path_parent(olddfd, oldname, &oldnd, &from);
3244 error = user_path_parent(newdfd, newname, &newnd, &to);
3249 if (oldnd.path.mnt != newnd.path.mnt)
3252 old_dir = oldnd.path.dentry;
3254 if (oldnd.last_type != LAST_NORM)
3257 new_dir = newnd.path.dentry;
3258 if (newnd.last_type != LAST_NORM)
3261 oldnd.flags &= ~LOOKUP_PARENT;
3262 newnd.flags &= ~LOOKUP_PARENT;
3263 newnd.flags |= LOOKUP_RENAME_TARGET;
3265 trap = lock_rename(new_dir, old_dir);
3267 old_dentry = lookup_hash(&oldnd);
3268 error = PTR_ERR(old_dentry);
3269 if (IS_ERR(old_dentry))
3271 /* source must exist */
3273 if (!old_dentry->d_inode)
3275 /* unless the source is a directory trailing slashes give -ENOTDIR */
3276 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3278 if (oldnd.last.name[oldnd.last.len])
3280 if (newnd.last.name[newnd.last.len])
3283 /* source should not be ancestor of target */
3285 if (old_dentry == trap)
3287 new_dentry = lookup_hash(&newnd);
3288 error = PTR_ERR(new_dentry);
3289 if (IS_ERR(new_dentry))
3291 /* target should not be an ancestor of source */
3293 if (new_dentry == trap)
3296 error = mnt_want_write(oldnd.path.mnt);
3299 error = security_path_rename(&oldnd.path, old_dentry,
3300 &newnd.path, new_dentry);
3303 error = vfs_rename(old_dir->d_inode, old_dentry,
3304 new_dir->d_inode, new_dentry);
3306 mnt_drop_write(oldnd.path.mnt);
3312 unlock_rename(new_dir, old_dir);
3314 path_put(&newnd.path);
3317 path_put(&oldnd.path);
3323 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
3325 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3328 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3332 len = PTR_ERR(link);
3337 if (len > (unsigned) buflen)
3339 if (copy_to_user(buffer, link, len))
3346 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3347 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3348 * using) it for any given inode is up to filesystem.
3350 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3352 struct nameidata nd;
3357 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3359 return PTR_ERR(cookie);
3361 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3362 if (dentry->d_inode->i_op->put_link)
3363 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3367 int vfs_follow_link(struct nameidata *nd, const char *link)
3369 return __vfs_follow_link(nd, link);
3372 /* get the link contents into pagecache */
3373 static char *page_getlink(struct dentry * dentry, struct page **ppage)
3377 struct address_space *mapping = dentry->d_inode->i_mapping;
3378 page = read_mapping_page(mapping, 0, NULL);
3383 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3387 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3389 struct page *page = NULL;
3390 char *s = page_getlink(dentry, &page);
3391 int res = vfs_readlink(dentry,buffer,buflen,s);
3394 page_cache_release(page);
3399 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
3401 struct page *page = NULL;
3402 nd_set_link(nd, page_getlink(dentry, &page));
3406 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
3408 struct page *page = cookie;
3412 page_cache_release(page);
3417 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3419 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
3421 struct address_space *mapping = inode->i_mapping;
3426 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3428 flags |= AOP_FLAG_NOFS;
3431 err = pagecache_write_begin(NULL, mapping, 0, len-1,
3432 flags, &page, &fsdata);
3436 kaddr = kmap_atomic(page);
3437 memcpy(kaddr, symname, len-1);
3438 kunmap_atomic(kaddr);
3440 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3447 mark_inode_dirty(inode);
3453 int page_symlink(struct inode *inode, const char *symname, int len)
3455 return __page_symlink(inode, symname, len,
3456 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
3459 const struct inode_operations page_symlink_inode_operations = {
3460 .readlink = generic_readlink,
3461 .follow_link = page_follow_link_light,
3462 .put_link = page_put_link,
3465 EXPORT_SYMBOL(user_path_at);
3466 EXPORT_SYMBOL(follow_down_one);
3467 EXPORT_SYMBOL(follow_down);
3468 EXPORT_SYMBOL(follow_up);
3469 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3470 EXPORT_SYMBOL(getname);
3471 EXPORT_SYMBOL(lock_rename);
3472 EXPORT_SYMBOL(lookup_one_len);
3473 EXPORT_SYMBOL(page_follow_link_light);
3474 EXPORT_SYMBOL(page_put_link);
3475 EXPORT_SYMBOL(page_readlink);
3476 EXPORT_SYMBOL(__page_symlink);
3477 EXPORT_SYMBOL(page_symlink);
3478 EXPORT_SYMBOL(page_symlink_inode_operations);
3479 EXPORT_SYMBOL(kern_path);
3480 EXPORT_SYMBOL(vfs_path_lookup);
3481 EXPORT_SYMBOL(inode_permission);
3482 EXPORT_SYMBOL(unlock_rename);
3483 EXPORT_SYMBOL(vfs_create);
3484 EXPORT_SYMBOL(vfs_follow_link);
3485 EXPORT_SYMBOL(vfs_link);
3486 EXPORT_SYMBOL(vfs_mkdir);
3487 EXPORT_SYMBOL(vfs_mknod);
3488 EXPORT_SYMBOL(generic_permission);
3489 EXPORT_SYMBOL(vfs_readlink);
3490 EXPORT_SYMBOL(vfs_rename);
3491 EXPORT_SYMBOL(vfs_rmdir);
3492 EXPORT_SYMBOL(vfs_symlink);
3493 EXPORT_SYMBOL(vfs_unlink);
3494 EXPORT_SYMBOL(dentry_unhash);
3495 EXPORT_SYMBOL(generic_readlink);