4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Some corrections by tytso.
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
21 #include <linux/namei.h>
22 #include <linux/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 <asm/uaccess.h>
39 /* [Feb-1997 T. Schoebel-Theuer]
40 * Fundamental changes in the pathname lookup mechanisms (namei)
41 * were necessary because of omirr. The reason is that omirr needs
42 * to know the _real_ pathname, not the user-supplied one, in case
43 * of symlinks (and also when transname replacements occur).
45 * The new code replaces the old recursive symlink resolution with
46 * an iterative one (in case of non-nested symlink chains). It does
47 * this with calls to <fs>_follow_link().
48 * As a side effect, dir_namei(), _namei() and follow_link() are now
49 * replaced with a single function lookup_dentry() that can handle all
50 * the special cases of the former code.
52 * With the new dcache, the pathname is stored at each inode, at least as
53 * long as the refcount of the inode is positive. As a side effect, the
54 * size of the dcache depends on the inode cache and thus is dynamic.
56 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
57 * resolution to correspond with current state of the code.
59 * Note that the symlink resolution is not *completely* iterative.
60 * There is still a significant amount of tail- and mid- recursion in
61 * the algorithm. Also, note that <fs>_readlink() is not used in
62 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
63 * may return different results than <fs>_follow_link(). Many virtual
64 * filesystems (including /proc) exhibit this behavior.
67 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
68 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
69 * and the name already exists in form of a symlink, try to create the new
70 * name indicated by the symlink. The old code always complained that the
71 * name already exists, due to not following the symlink even if its target
72 * is nonexistent. The new semantics affects also mknod() and link() when
73 * the name is a symlink pointing to a non-existent name.
75 * I don't know which semantics is the right one, since I have no access
76 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
77 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
78 * "old" one. Personally, I think the new semantics is much more logical.
79 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
80 * file does succeed in both HP-UX and SunOs, but not in Solaris
81 * and in the old Linux semantics.
84 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
85 * semantics. See the comments in "open_namei" and "do_link" below.
87 * [10-Sep-98 Alan Modra] Another symlink change.
90 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
91 * inside the path - always follow.
92 * in the last component in creation/removal/renaming - never follow.
93 * if LOOKUP_FOLLOW passed - follow.
94 * if the pathname has trailing slashes - follow.
95 * otherwise - don't follow.
96 * (applied in that order).
98 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
99 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
100 * During the 2.4 we need to fix the userland stuff depending on it -
101 * hopefully we will be able to get rid of that wart in 2.5. So far only
102 * XEmacs seems to be relying on it...
105 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
106 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
107 * any extra contention...
110 /* In order to reduce some races, while at the same time doing additional
111 * checking and hopefully speeding things up, we copy filenames to the
112 * kernel data space before using them..
114 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
115 * PATH_MAX includes the nul terminator --RR.
117 static int do_getname(const char __user *filename, char *page)
120 unsigned long len = PATH_MAX;
122 if (!segment_eq(get_fs(), KERNEL_DS)) {
123 if ((unsigned long) filename >= TASK_SIZE)
125 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
126 len = TASK_SIZE - (unsigned long) filename;
129 retval = strncpy_from_user(page, filename, len);
133 return -ENAMETOOLONG;
139 static char *getname_flags(const char __user * filename, int flags)
143 result = ERR_PTR(-ENOMEM);
146 int retval = do_getname(filename, tmp);
150 if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
152 result = ERR_PTR(retval);
156 audit_getname(result);
160 char *getname(const char __user * filename)
162 return getname_flags(filename, 0);
165 #ifdef CONFIG_AUDITSYSCALL
166 void putname(const char *name)
168 if (unlikely(!audit_dummy_context()))
173 EXPORT_SYMBOL(putname);
177 * This does basic POSIX ACL permission checking
179 static int acl_permission_check(struct inode *inode, int mask, unsigned int flags,
180 int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
182 unsigned int mode = inode->i_mode;
184 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
186 if (current_user_ns() != inode_userns(inode))
189 if (current_fsuid() == inode->i_uid)
192 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
193 int error = check_acl(inode, mask, flags);
194 if (error != -EAGAIN)
198 if (in_group_p(inode->i_gid))
204 * If the DACs are ok we don't need any capability check.
206 if ((mask & ~mode) == 0)
212 * generic_permission - check for access rights on a Posix-like filesystem
213 * @inode: inode to check access rights for
214 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
215 * @check_acl: optional callback to check for Posix ACLs
216 * @flags: IPERM_FLAG_ flags.
218 * Used to check for read/write/execute permissions on a file.
219 * We use "fsuid" for this, letting us set arbitrary permissions
220 * for filesystem access without changing the "normal" uids which
221 * are used for other things.
223 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
224 * request cannot be satisfied (eg. requires blocking or too much complexity).
225 * It would then be called again in ref-walk mode.
227 int generic_permission(struct inode *inode, int mask, unsigned int flags,
228 int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
233 * Do the basic POSIX ACL permission checks.
235 ret = acl_permission_check(inode, mask, flags, check_acl);
240 * Read/write DACs are always overridable.
241 * Executable DACs are overridable for all directories and
242 * for non-directories that have least one exec bit set.
244 if (!(mask & MAY_EXEC) || execute_ok(inode))
245 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
249 * Searching includes executable on directories, else just read.
251 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
252 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
253 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
260 * inode_permission - check for access rights to a given inode
261 * @inode: inode to check permission on
262 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
264 * Used to check for read/write/execute permissions on an inode.
265 * We use "fsuid" for this, letting us set arbitrary permissions
266 * for filesystem access without changing the "normal" uids which
267 * are used for other things.
269 int inode_permission(struct inode *inode, int mask)
273 if (mask & MAY_WRITE) {
274 umode_t mode = inode->i_mode;
277 * Nobody gets write access to a read-only fs.
279 if (IS_RDONLY(inode) &&
280 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
284 * Nobody gets write access to an immutable file.
286 if (IS_IMMUTABLE(inode))
290 if (inode->i_op->permission)
291 retval = inode->i_op->permission(inode, mask, 0);
293 retval = generic_permission(inode, mask, 0,
294 inode->i_op->check_acl);
299 retval = devcgroup_inode_permission(inode, mask);
303 return security_inode_permission(inode, mask);
307 * exec_permission - check for right to do lookups in a given directory
308 * @inode: inode to check permission on
309 * @flags: IPERM_FLAG_ flags.
311 * Short-cut version of inode_permission(), for calling on directories
312 * during pathname resolution. Combines parts of inode_permission()
313 * and generic_permission(), and tests ONLY for MAY_EXEC permission.
315 * If appropriate, check DAC only. If not appropriate, or
316 * short-cut DAC fails, then call ->permission() to do more
317 * complete permission check.
319 static inline int exec_permission(struct inode *inode, unsigned int flags)
322 struct user_namespace *ns = inode_userns(inode);
324 if (inode->i_op->permission) {
325 ret = inode->i_op->permission(inode, MAY_EXEC, flags);
329 ret = acl_permission_check(inode, MAY_EXEC, flags,
330 inode->i_op->check_acl);
335 if (ns_capable(ns, CAP_DAC_OVERRIDE) ||
336 ns_capable(ns, CAP_DAC_READ_SEARCH))
341 return security_inode_exec_permission(inode, flags);
345 * get_write_access() gets write permission for a file.
346 * put_write_access() releases this write permission.
347 * This is used for regular files.
348 * We cannot support write (and maybe mmap read-write shared) accesses and
349 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
350 * can have the following values:
351 * 0: no writers, no VM_DENYWRITE mappings
352 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
353 * > 0: (i_writecount) users are writing to the file.
355 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
356 * except for the cases where we don't hold i_writecount yet. Then we need to
357 * use {get,deny}_write_access() - these functions check the sign and refuse
358 * to do the change if sign is wrong. Exclusion between them is provided by
359 * the inode->i_lock spinlock.
362 int get_write_access(struct inode * inode)
364 spin_lock(&inode->i_lock);
365 if (atomic_read(&inode->i_writecount) < 0) {
366 spin_unlock(&inode->i_lock);
369 atomic_inc(&inode->i_writecount);
370 spin_unlock(&inode->i_lock);
375 int deny_write_access(struct file * file)
377 struct inode *inode = file->f_path.dentry->d_inode;
379 spin_lock(&inode->i_lock);
380 if (atomic_read(&inode->i_writecount) > 0) {
381 spin_unlock(&inode->i_lock);
384 atomic_dec(&inode->i_writecount);
385 spin_unlock(&inode->i_lock);
391 * path_get - get a reference to a path
392 * @path: path to get the reference to
394 * Given a path increment the reference count to the dentry and the vfsmount.
396 void path_get(struct path *path)
401 EXPORT_SYMBOL(path_get);
404 * path_put - put a reference to a path
405 * @path: path to put the reference to
407 * Given a path decrement the reference count to the dentry and the vfsmount.
409 void path_put(struct path *path)
414 EXPORT_SYMBOL(path_put);
417 * Path walking has 2 modes, rcu-walk and ref-walk (see
418 * Documentation/filesystems/path-lookup.txt). In situations when we can't
419 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
420 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
421 * mode. Refcounts are grabbed at the last known good point before rcu-walk
422 * got stuck, so ref-walk may continue from there. If this is not successful
423 * (eg. a seqcount has changed), then failure is returned and it's up to caller
424 * to restart the path walk from the beginning in ref-walk mode.
428 * unlazy_walk - try to switch to ref-walk mode.
429 * @nd: nameidata pathwalk data
430 * @dentry: child of nd->path.dentry or NULL
431 * Returns: 0 on success, -ECHILD on failure
433 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
434 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
435 * @nd or NULL. Must be called from rcu-walk context.
437 static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
439 struct fs_struct *fs = current->fs;
440 struct dentry *parent = nd->path.dentry;
443 BUG_ON(!(nd->flags & LOOKUP_RCU));
444 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
446 spin_lock(&fs->lock);
447 if (nd->root.mnt != fs->root.mnt ||
448 nd->root.dentry != fs->root.dentry)
451 spin_lock(&parent->d_lock);
453 if (!__d_rcu_to_refcount(parent, nd->seq))
455 BUG_ON(nd->inode != parent->d_inode);
457 if (dentry->d_parent != parent)
459 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
460 if (!__d_rcu_to_refcount(dentry, nd->seq))
463 * If the sequence check on the child dentry passed, then
464 * the child has not been removed from its parent. This
465 * means the parent dentry must be valid and able to take
466 * a reference at this point.
468 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
469 BUG_ON(!parent->d_count);
471 spin_unlock(&dentry->d_lock);
473 spin_unlock(&parent->d_lock);
476 spin_unlock(&fs->lock);
478 mntget(nd->path.mnt);
481 br_read_unlock(vfsmount_lock);
482 nd->flags &= ~LOOKUP_RCU;
486 spin_unlock(&dentry->d_lock);
488 spin_unlock(&parent->d_lock);
491 spin_unlock(&fs->lock);
496 * release_open_intent - free up open intent resources
497 * @nd: pointer to nameidata
499 void release_open_intent(struct nameidata *nd)
501 struct file *file = nd->intent.open.file;
503 if (file && !IS_ERR(file)) {
504 if (file->f_path.dentry == NULL)
511 static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
513 return dentry->d_op->d_revalidate(dentry, nd);
516 static struct dentry *
517 do_revalidate(struct dentry *dentry, struct nameidata *nd)
519 int status = d_revalidate(dentry, nd);
520 if (unlikely(status <= 0)) {
522 * The dentry failed validation.
523 * If d_revalidate returned 0 attempt to invalidate
524 * the dentry otherwise d_revalidate is asking us
525 * to return a fail status.
529 dentry = ERR_PTR(status);
530 } else if (!d_invalidate(dentry)) {
539 * complete_walk - successful completion of path walk
540 * @nd: pointer nameidata
542 * If we had been in RCU mode, drop out of it and legitimize nd->path.
543 * Revalidate the final result, unless we'd already done that during
544 * the path walk or the filesystem doesn't ask for it. Return 0 on
545 * success, -error on failure. In case of failure caller does not
546 * need to drop nd->path.
548 static int complete_walk(struct nameidata *nd)
550 struct dentry *dentry = nd->path.dentry;
553 if (nd->flags & LOOKUP_RCU) {
554 nd->flags &= ~LOOKUP_RCU;
555 if (!(nd->flags & LOOKUP_ROOT))
557 spin_lock(&dentry->d_lock);
558 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
559 spin_unlock(&dentry->d_lock);
561 br_read_unlock(vfsmount_lock);
564 BUG_ON(nd->inode != dentry->d_inode);
565 spin_unlock(&dentry->d_lock);
566 mntget(nd->path.mnt);
568 br_read_unlock(vfsmount_lock);
571 if (likely(!(nd->flags & LOOKUP_JUMPED)))
574 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
577 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
580 /* Note: we do not d_invalidate() */
581 status = d_revalidate(dentry, nd);
592 static __always_inline void set_root(struct nameidata *nd)
595 get_fs_root(current->fs, &nd->root);
598 static int link_path_walk(const char *, struct nameidata *);
600 static __always_inline void set_root_rcu(struct nameidata *nd)
603 struct fs_struct *fs = current->fs;
607 seq = read_seqcount_begin(&fs->seq);
609 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
610 } while (read_seqcount_retry(&fs->seq, seq));
614 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
626 nd->flags |= LOOKUP_JUMPED;
628 nd->inode = nd->path.dentry->d_inode;
630 ret = link_path_walk(link, nd);
634 return PTR_ERR(link);
637 static void path_put_conditional(struct path *path, struct nameidata *nd)
640 if (path->mnt != nd->path.mnt)
644 static inline void path_to_nameidata(const struct path *path,
645 struct nameidata *nd)
647 if (!(nd->flags & LOOKUP_RCU)) {
648 dput(nd->path.dentry);
649 if (nd->path.mnt != path->mnt)
650 mntput(nd->path.mnt);
652 nd->path.mnt = path->mnt;
653 nd->path.dentry = path->dentry;
656 static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
658 struct inode *inode = link->dentry->d_inode;
659 if (!IS_ERR(cookie) && inode->i_op->put_link)
660 inode->i_op->put_link(link->dentry, nd, cookie);
664 static __always_inline int
665 follow_link(struct path *link, struct nameidata *nd, void **p)
668 struct dentry *dentry = link->dentry;
670 BUG_ON(nd->flags & LOOKUP_RCU);
672 if (link->mnt == nd->path.mnt)
675 if (unlikely(current->total_link_count >= 40)) {
676 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
681 current->total_link_count++;
683 touch_atime(link->mnt, dentry);
684 nd_set_link(nd, NULL);
686 error = security_inode_follow_link(link->dentry, nd);
688 *p = ERR_PTR(error); /* no ->put_link(), please */
693 nd->last_type = LAST_BIND;
694 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
697 char *s = nd_get_link(nd);
700 error = __vfs_follow_link(nd, s);
701 else if (nd->last_type == LAST_BIND) {
702 nd->flags |= LOOKUP_JUMPED;
703 nd->inode = nd->path.dentry->d_inode;
704 if (nd->inode->i_op->follow_link) {
705 /* stepped on a _really_ weird one */
714 static int follow_up_rcu(struct path *path)
716 struct vfsmount *parent;
717 struct dentry *mountpoint;
719 parent = path->mnt->mnt_parent;
720 if (parent == path->mnt)
722 mountpoint = path->mnt->mnt_mountpoint;
723 path->dentry = mountpoint;
728 int follow_up(struct path *path)
730 struct vfsmount *parent;
731 struct dentry *mountpoint;
733 br_read_lock(vfsmount_lock);
734 parent = path->mnt->mnt_parent;
735 if (parent == path->mnt) {
736 br_read_unlock(vfsmount_lock);
740 mountpoint = dget(path->mnt->mnt_mountpoint);
741 br_read_unlock(vfsmount_lock);
743 path->dentry = mountpoint;
750 * Perform an automount
751 * - return -EISDIR to tell follow_managed() to stop and return the path we
754 static int follow_automount(struct path *path, unsigned flags,
757 struct vfsmount *mnt;
760 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
763 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
764 * and this is the terminal part of the path.
766 if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
767 return -EISDIR; /* we actually want to stop here */
769 /* We want to mount if someone is trying to open/create a file of any
770 * type under the mountpoint, wants to traverse through the mountpoint
771 * or wants to open the mounted directory.
773 * We don't want to mount if someone's just doing a stat and they've
774 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
775 * appended a '/' to the name.
777 if (!(flags & LOOKUP_FOLLOW) &&
778 !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
779 LOOKUP_OPEN | LOOKUP_CREATE)))
782 current->total_link_count++;
783 if (current->total_link_count >= 40)
786 mnt = path->dentry->d_op->d_automount(path);
789 * The filesystem is allowed to return -EISDIR here to indicate
790 * it doesn't want to automount. For instance, autofs would do
791 * this so that its userspace daemon can mount on this dentry.
793 * However, we can only permit this if it's a terminal point in
794 * the path being looked up; if it wasn't then the remainder of
795 * the path is inaccessible and we should say so.
797 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
802 if (!mnt) /* mount collision */
806 /* lock_mount() may release path->mnt on error */
810 err = finish_automount(mnt, path);
814 /* Someone else made a mount here whilst we were busy */
819 path->dentry = dget(mnt->mnt_root);
828 * Handle a dentry that is managed in some way.
829 * - Flagged for transit management (autofs)
830 * - Flagged as mountpoint
831 * - Flagged as automount point
833 * This may only be called in refwalk mode.
835 * Serialization is taken care of in namespace.c
837 static int follow_managed(struct path *path, unsigned flags)
839 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
841 bool need_mntput = false;
844 /* Given that we're not holding a lock here, we retain the value in a
845 * local variable for each dentry as we look at it so that we don't see
846 * the components of that value change under us */
847 while (managed = ACCESS_ONCE(path->dentry->d_flags),
848 managed &= DCACHE_MANAGED_DENTRY,
849 unlikely(managed != 0)) {
850 /* Allow the filesystem to manage the transit without i_mutex
852 if (managed & DCACHE_MANAGE_TRANSIT) {
853 BUG_ON(!path->dentry->d_op);
854 BUG_ON(!path->dentry->d_op->d_manage);
855 ret = path->dentry->d_op->d_manage(path->dentry, false);
860 /* Transit to a mounted filesystem. */
861 if (managed & DCACHE_MOUNTED) {
862 struct vfsmount *mounted = lookup_mnt(path);
868 path->dentry = dget(mounted->mnt_root);
873 /* Something is mounted on this dentry in another
874 * namespace and/or whatever was mounted there in this
875 * namespace got unmounted before we managed to get the
879 /* Handle an automount point */
880 if (managed & DCACHE_NEED_AUTOMOUNT) {
881 ret = follow_automount(path, flags, &need_mntput);
887 /* We didn't change the current path point */
891 if (need_mntput && path->mnt == mnt)
898 int follow_down_one(struct path *path)
900 struct vfsmount *mounted;
902 mounted = lookup_mnt(path);
907 path->dentry = dget(mounted->mnt_root);
913 static inline bool managed_dentry_might_block(struct dentry *dentry)
915 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
916 dentry->d_op->d_manage(dentry, true) < 0);
920 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
921 * we meet a managed dentry that would need blocking.
923 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
924 struct inode **inode)
927 struct vfsmount *mounted;
929 * Don't forget we might have a non-mountpoint managed dentry
930 * that wants to block transit.
932 if (unlikely(managed_dentry_might_block(path->dentry)))
935 if (!d_mountpoint(path->dentry))
938 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
942 path->dentry = mounted->mnt_root;
943 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
945 * Update the inode too. We don't need to re-check the
946 * dentry sequence number here after this d_inode read,
947 * because a mount-point is always pinned.
949 *inode = path->dentry->d_inode;
954 static void follow_mount_rcu(struct nameidata *nd)
956 while (d_mountpoint(nd->path.dentry)) {
957 struct vfsmount *mounted;
958 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
961 nd->path.mnt = mounted;
962 nd->path.dentry = mounted->mnt_root;
963 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
967 static int follow_dotdot_rcu(struct nameidata *nd)
972 if (nd->path.dentry == nd->root.dentry &&
973 nd->path.mnt == nd->root.mnt) {
976 if (nd->path.dentry != nd->path.mnt->mnt_root) {
977 struct dentry *old = nd->path.dentry;
978 struct dentry *parent = old->d_parent;
981 seq = read_seqcount_begin(&parent->d_seq);
982 if (read_seqcount_retry(&old->d_seq, nd->seq))
984 nd->path.dentry = parent;
988 if (!follow_up_rcu(&nd->path))
990 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
992 follow_mount_rcu(nd);
993 nd->inode = nd->path.dentry->d_inode;
997 nd->flags &= ~LOOKUP_RCU;
998 if (!(nd->flags & LOOKUP_ROOT))
1001 br_read_unlock(vfsmount_lock);
1006 * Follow down to the covering mount currently visible to userspace. At each
1007 * point, the filesystem owning that dentry may be queried as to whether the
1008 * caller is permitted to proceed or not.
1010 int follow_down(struct path *path)
1015 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1016 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1017 /* Allow the filesystem to manage the transit without i_mutex
1020 * We indicate to the filesystem if someone is trying to mount
1021 * something here. This gives autofs the chance to deny anyone
1022 * other than its daemon the right to mount on its
1025 * The filesystem may sleep at this point.
1027 if (managed & DCACHE_MANAGE_TRANSIT) {
1028 BUG_ON(!path->dentry->d_op);
1029 BUG_ON(!path->dentry->d_op->d_manage);
1030 ret = path->dentry->d_op->d_manage(
1031 path->dentry, false);
1033 return ret == -EISDIR ? 0 : ret;
1036 /* Transit to a mounted filesystem. */
1037 if (managed & DCACHE_MOUNTED) {
1038 struct vfsmount *mounted = lookup_mnt(path);
1043 path->mnt = mounted;
1044 path->dentry = dget(mounted->mnt_root);
1048 /* Don't handle automount points here */
1055 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1057 static void follow_mount(struct path *path)
1059 while (d_mountpoint(path->dentry)) {
1060 struct vfsmount *mounted = lookup_mnt(path);
1065 path->mnt = mounted;
1066 path->dentry = dget(mounted->mnt_root);
1070 static void follow_dotdot(struct nameidata *nd)
1075 struct dentry *old = nd->path.dentry;
1077 if (nd->path.dentry == nd->root.dentry &&
1078 nd->path.mnt == nd->root.mnt) {
1081 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1082 /* rare case of legitimate dget_parent()... */
1083 nd->path.dentry = dget_parent(nd->path.dentry);
1087 if (!follow_up(&nd->path))
1090 follow_mount(&nd->path);
1091 nd->inode = nd->path.dentry->d_inode;
1095 * Allocate a dentry with name and parent, and perform a parent
1096 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1097 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1098 * have verified that no child exists while under i_mutex.
1100 static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1101 struct qstr *name, struct nameidata *nd)
1103 struct inode *inode = parent->d_inode;
1104 struct dentry *dentry;
1107 /* Don't create child dentry for a dead directory. */
1108 if (unlikely(IS_DEADDIR(inode)))
1109 return ERR_PTR(-ENOENT);
1111 dentry = d_alloc(parent, name);
1112 if (unlikely(!dentry))
1113 return ERR_PTR(-ENOMEM);
1115 old = inode->i_op->lookup(inode, dentry, nd);
1116 if (unlikely(old)) {
1124 * We already have a dentry, but require a lookup to be performed on the parent
1125 * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
1126 * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
1127 * child exists while under i_mutex.
1129 static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
1130 struct nameidata *nd)
1132 struct inode *inode = parent->d_inode;
1135 /* Don't create child dentry for a dead directory. */
1136 if (unlikely(IS_DEADDIR(inode)))
1137 return ERR_PTR(-ENOENT);
1139 old = inode->i_op->lookup(inode, dentry, nd);
1140 if (unlikely(old)) {
1148 * It's more convoluted than I'd like it to be, but... it's still fairly
1149 * small and for now I'd prefer to have fast path as straight as possible.
1150 * It _is_ time-critical.
1152 static int do_lookup(struct nameidata *nd, struct qstr *name,
1153 struct path *path, struct inode **inode)
1155 struct vfsmount *mnt = nd->path.mnt;
1156 struct dentry *dentry, *parent = nd->path.dentry;
1162 * Rename seqlock is not required here because in the off chance
1163 * of a false negative due to a concurrent rename, we're going to
1164 * do the non-racy lookup, below.
1166 if (nd->flags & LOOKUP_RCU) {
1169 dentry = __d_lookup_rcu(parent, name, &seq, inode);
1173 /* Memory barrier in read_seqcount_begin of child is enough */
1174 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1178 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1179 status = d_revalidate(dentry, nd);
1180 if (unlikely(status <= 0)) {
1181 if (status != -ECHILD)
1186 if (unlikely(d_need_lookup(dentry)))
1189 path->dentry = dentry;
1190 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1192 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1196 if (unlazy_walk(nd, dentry))
1199 dentry = __d_lookup(parent, name);
1202 if (dentry && unlikely(d_need_lookup(dentry))) {
1207 if (unlikely(!dentry)) {
1208 struct inode *dir = parent->d_inode;
1209 BUG_ON(nd->inode != dir);
1211 mutex_lock(&dir->i_mutex);
1212 dentry = d_lookup(parent, name);
1213 if (likely(!dentry)) {
1214 dentry = d_alloc_and_lookup(parent, name, nd);
1215 if (IS_ERR(dentry)) {
1216 mutex_unlock(&dir->i_mutex);
1217 return PTR_ERR(dentry);
1222 } else if (unlikely(d_need_lookup(dentry))) {
1223 dentry = d_inode_lookup(parent, dentry, nd);
1224 if (IS_ERR(dentry)) {
1225 mutex_unlock(&dir->i_mutex);
1226 return PTR_ERR(dentry);
1232 mutex_unlock(&dir->i_mutex);
1234 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1235 status = d_revalidate(dentry, nd);
1236 if (unlikely(status <= 0)) {
1241 if (!d_invalidate(dentry)) {
1250 path->dentry = dentry;
1251 err = follow_managed(path, nd->flags);
1252 if (unlikely(err < 0)) {
1253 path_put_conditional(path, nd);
1256 *inode = path->dentry->d_inode;
1260 static inline int may_lookup(struct nameidata *nd)
1262 if (nd->flags & LOOKUP_RCU) {
1263 int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
1266 if (unlazy_walk(nd, NULL))
1269 return exec_permission(nd->inode, 0);
1272 static inline int handle_dots(struct nameidata *nd, int type)
1274 if (type == LAST_DOTDOT) {
1275 if (nd->flags & LOOKUP_RCU) {
1276 if (follow_dotdot_rcu(nd))
1284 static void terminate_walk(struct nameidata *nd)
1286 if (!(nd->flags & LOOKUP_RCU)) {
1287 path_put(&nd->path);
1289 nd->flags &= ~LOOKUP_RCU;
1290 if (!(nd->flags & LOOKUP_ROOT))
1291 nd->root.mnt = NULL;
1293 br_read_unlock(vfsmount_lock);
1297 static inline int walk_component(struct nameidata *nd, struct path *path,
1298 struct qstr *name, int type, int follow)
1300 struct inode *inode;
1303 * "." and ".." are special - ".." especially so because it has
1304 * to be able to know about the current root directory and
1305 * parent relationships.
1307 if (unlikely(type != LAST_NORM))
1308 return handle_dots(nd, type);
1309 err = do_lookup(nd, name, path, &inode);
1310 if (unlikely(err)) {
1315 path_to_nameidata(path, nd);
1319 if (unlikely(inode->i_op->follow_link) && follow) {
1320 if (nd->flags & LOOKUP_RCU) {
1321 if (unlikely(unlazy_walk(nd, path->dentry))) {
1326 BUG_ON(inode != path->dentry->d_inode);
1329 path_to_nameidata(path, nd);
1335 * This limits recursive symlink follows to 8, while
1336 * limiting consecutive symlinks to 40.
1338 * Without that kind of total limit, nasty chains of consecutive
1339 * symlinks can cause almost arbitrarily long lookups.
1341 static inline int nested_symlink(struct path *path, struct nameidata *nd)
1345 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1346 path_put_conditional(path, nd);
1347 path_put(&nd->path);
1350 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1353 current->link_count++;
1356 struct path link = *path;
1359 res = follow_link(&link, nd, &cookie);
1361 res = walk_component(nd, path, &nd->last,
1362 nd->last_type, LOOKUP_FOLLOW);
1363 put_link(nd, &link, cookie);
1366 current->link_count--;
1373 * This is the basic name resolution function, turning a pathname into
1374 * the final dentry. We expect 'base' to be positive and a directory.
1376 * Returns 0 and nd will have valid dentry and mnt on success.
1377 * Returns error and drops reference to input namei data on failure.
1379 static int link_path_walk(const char *name, struct nameidata *nd)
1383 unsigned int lookup_flags = nd->flags;
1390 /* At this point we know we have a real path component. */
1397 nd->flags |= LOOKUP_CONTINUE;
1399 err = may_lookup(nd);
1404 c = *(const unsigned char *)name;
1406 hash = init_name_hash();
1409 hash = partial_name_hash(c, hash);
1410 c = *(const unsigned char *)name;
1411 } while (c && (c != '/'));
1412 this.len = name - (const char *) this.name;
1413 this.hash = end_name_hash(hash);
1416 if (this.name[0] == '.') switch (this.len) {
1418 if (this.name[1] == '.') {
1420 nd->flags |= LOOKUP_JUMPED;
1426 if (likely(type == LAST_NORM)) {
1427 struct dentry *parent = nd->path.dentry;
1428 nd->flags &= ~LOOKUP_JUMPED;
1429 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1430 err = parent->d_op->d_hash(parent, nd->inode,
1437 /* remove trailing slashes? */
1439 goto last_component;
1440 while (*++name == '/');
1442 goto last_component;
1444 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1449 err = nested_symlink(&next, nd);
1454 if (!nd->inode->i_op->lookup)
1457 /* here ends the main loop */
1460 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1461 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
1463 nd->last_type = type;
1470 static int path_init(int dfd, const char *name, unsigned int flags,
1471 struct nameidata *nd, struct file **fp)
1477 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1478 nd->flags = flags | LOOKUP_JUMPED;
1480 if (flags & LOOKUP_ROOT) {
1481 struct inode *inode = nd->root.dentry->d_inode;
1483 if (!inode->i_op->lookup)
1485 retval = inode_permission(inode, MAY_EXEC);
1489 nd->path = nd->root;
1491 if (flags & LOOKUP_RCU) {
1492 br_read_lock(vfsmount_lock);
1494 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1496 path_get(&nd->path);
1501 nd->root.mnt = NULL;
1504 if (flags & LOOKUP_RCU) {
1505 br_read_lock(vfsmount_lock);
1510 path_get(&nd->root);
1512 nd->path = nd->root;
1513 } else if (dfd == AT_FDCWD) {
1514 if (flags & LOOKUP_RCU) {
1515 struct fs_struct *fs = current->fs;
1518 br_read_lock(vfsmount_lock);
1522 seq = read_seqcount_begin(&fs->seq);
1524 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1525 } while (read_seqcount_retry(&fs->seq, seq));
1527 get_fs_pwd(current->fs, &nd->path);
1530 struct dentry *dentry;
1532 file = fget_raw_light(dfd, &fput_needed);
1537 dentry = file->f_path.dentry;
1541 if (!S_ISDIR(dentry->d_inode->i_mode))
1544 retval = exec_permission(dentry->d_inode, 0);
1549 nd->path = file->f_path;
1550 if (flags & LOOKUP_RCU) {
1553 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1554 br_read_lock(vfsmount_lock);
1557 path_get(&file->f_path);
1558 fput_light(file, fput_needed);
1562 nd->inode = nd->path.dentry->d_inode;
1566 fput_light(file, fput_needed);
1571 static inline int lookup_last(struct nameidata *nd, struct path *path)
1573 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1574 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1576 nd->flags &= ~LOOKUP_PARENT;
1577 return walk_component(nd, path, &nd->last, nd->last_type,
1578 nd->flags & LOOKUP_FOLLOW);
1581 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1582 static int path_lookupat(int dfd, const char *name,
1583 unsigned int flags, struct nameidata *nd)
1585 struct file *base = NULL;
1590 * Path walking is largely split up into 2 different synchronisation
1591 * schemes, rcu-walk and ref-walk (explained in
1592 * Documentation/filesystems/path-lookup.txt). These share much of the
1593 * path walk code, but some things particularly setup, cleanup, and
1594 * following mounts are sufficiently divergent that functions are
1595 * duplicated. Typically there is a function foo(), and its RCU
1596 * analogue, foo_rcu().
1598 * -ECHILD is the error number of choice (just to avoid clashes) that
1599 * is returned if some aspect of an rcu-walk fails. Such an error must
1600 * be handled by restarting a traditional ref-walk (which will always
1601 * be able to complete).
1603 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1608 current->total_link_count = 0;
1609 err = link_path_walk(name, nd);
1611 if (!err && !(flags & LOOKUP_PARENT)) {
1612 err = lookup_last(nd, &path);
1615 struct path link = path;
1616 nd->flags |= LOOKUP_PARENT;
1617 err = follow_link(&link, nd, &cookie);
1619 err = lookup_last(nd, &path);
1620 put_link(nd, &link, cookie);
1625 err = complete_walk(nd);
1627 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1628 if (!nd->inode->i_op->lookup) {
1629 path_put(&nd->path);
1637 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
1638 path_put(&nd->root);
1639 nd->root.mnt = NULL;
1644 static int do_path_lookup(int dfd, const char *name,
1645 unsigned int flags, struct nameidata *nd)
1647 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1648 if (unlikely(retval == -ECHILD))
1649 retval = path_lookupat(dfd, name, flags, nd);
1650 if (unlikely(retval == -ESTALE))
1651 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1653 if (likely(!retval)) {
1654 if (unlikely(!audit_dummy_context())) {
1655 if (nd->path.dentry && nd->inode)
1656 audit_inode(name, nd->path.dentry);
1662 int kern_path_parent(const char *name, struct nameidata *nd)
1664 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
1667 int kern_path(const char *name, unsigned int flags, struct path *path)
1669 struct nameidata nd;
1670 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1677 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1678 * @dentry: pointer to dentry of the base directory
1679 * @mnt: pointer to vfs mount of the base directory
1680 * @name: pointer to file name
1681 * @flags: lookup flags
1682 * @nd: pointer to nameidata
1684 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1685 const char *name, unsigned int flags,
1686 struct nameidata *nd)
1688 nd->root.dentry = dentry;
1690 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1691 return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
1694 static struct dentry *__lookup_hash(struct qstr *name,
1695 struct dentry *base, struct nameidata *nd)
1697 struct inode *inode = base->d_inode;
1698 struct dentry *dentry;
1701 err = exec_permission(inode, 0);
1703 return ERR_PTR(err);
1706 * Don't bother with __d_lookup: callers are for creat as
1707 * well as unlink, so a lot of the time it would cost
1710 dentry = d_lookup(base, name);
1712 if (dentry && d_need_lookup(dentry)) {
1714 * __lookup_hash is called with the parent dir's i_mutex already
1715 * held, so we are good to go here.
1717 dentry = d_inode_lookup(base, dentry, nd);
1722 if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
1723 dentry = do_revalidate(dentry, nd);
1726 dentry = d_alloc_and_lookup(base, name, nd);
1732 * Restricted form of lookup. Doesn't follow links, single-component only,
1733 * needs parent already locked. Doesn't follow mounts.
1736 static struct dentry *lookup_hash(struct nameidata *nd)
1738 return __lookup_hash(&nd->last, nd->path.dentry, nd);
1742 * lookup_one_len - filesystem helper to lookup single pathname component
1743 * @name: pathname component to lookup
1744 * @base: base directory to lookup from
1745 * @len: maximum length @len should be interpreted to
1747 * Note that this routine is purely a helper for filesystem usage and should
1748 * not be called by generic code. Also note that by using this function the
1749 * nameidata argument is passed to the filesystem methods and a filesystem
1750 * using this helper needs to be prepared for that.
1752 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1758 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1763 return ERR_PTR(-EACCES);
1765 hash = init_name_hash();
1767 c = *(const unsigned char *)name++;
1768 if (c == '/' || c == '\0')
1769 return ERR_PTR(-EACCES);
1770 hash = partial_name_hash(c, hash);
1772 this.hash = end_name_hash(hash);
1774 * See if the low-level filesystem might want
1775 * to use its own hash..
1777 if (base->d_flags & DCACHE_OP_HASH) {
1778 int err = base->d_op->d_hash(base, base->d_inode, &this);
1780 return ERR_PTR(err);
1783 return __lookup_hash(&this, base, NULL);
1786 int user_path_at(int dfd, const char __user *name, unsigned flags,
1789 struct nameidata nd;
1790 char *tmp = getname_flags(name, flags);
1791 int err = PTR_ERR(tmp);
1794 BUG_ON(flags & LOOKUP_PARENT);
1796 err = do_path_lookup(dfd, tmp, flags, &nd);
1804 static int user_path_parent(int dfd, const char __user *path,
1805 struct nameidata *nd, char **name)
1807 char *s = getname(path);
1813 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1823 * It's inline, so penalty for filesystems that don't use sticky bit is
1826 static inline int check_sticky(struct inode *dir, struct inode *inode)
1828 uid_t fsuid = current_fsuid();
1830 if (!(dir->i_mode & S_ISVTX))
1832 if (current_user_ns() != inode_userns(inode))
1834 if (inode->i_uid == fsuid)
1836 if (dir->i_uid == fsuid)
1840 return !ns_capable(inode_userns(inode), CAP_FOWNER);
1844 * Check whether we can remove a link victim from directory dir, check
1845 * whether the type of victim is right.
1846 * 1. We can't do it if dir is read-only (done in permission())
1847 * 2. We should have write and exec permissions on dir
1848 * 3. We can't remove anything from append-only dir
1849 * 4. We can't do anything with immutable dir (done in permission())
1850 * 5. If the sticky bit on dir is set we should either
1851 * a. be owner of dir, or
1852 * b. be owner of victim, or
1853 * c. have CAP_FOWNER capability
1854 * 6. If the victim is append-only or immutable we can't do antyhing with
1855 * links pointing to it.
1856 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1857 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1858 * 9. We can't remove a root or mountpoint.
1859 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1860 * nfs_async_unlink().
1862 static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1866 if (!victim->d_inode)
1869 BUG_ON(victim->d_parent->d_inode != dir);
1870 audit_inode_child(victim, dir);
1872 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1877 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1878 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
1881 if (!S_ISDIR(victim->d_inode->i_mode))
1883 if (IS_ROOT(victim))
1885 } else if (S_ISDIR(victim->d_inode->i_mode))
1887 if (IS_DEADDIR(dir))
1889 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1894 /* Check whether we can create an object with dentry child in directory
1896 * 1. We can't do it if child already exists (open has special treatment for
1897 * this case, but since we are inlined it's OK)
1898 * 2. We can't do it if dir is read-only (done in permission())
1899 * 3. We should have write and exec permissions on dir
1900 * 4. We can't do it if dir is immutable (done in permission())
1902 static inline int may_create(struct inode *dir, struct dentry *child)
1906 if (IS_DEADDIR(dir))
1908 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
1912 * p1 and p2 should be directories on the same fs.
1914 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1919 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1923 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1925 p = d_ancestor(p2, p1);
1927 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1928 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1932 p = d_ancestor(p1, p2);
1934 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1935 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1939 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1940 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1944 void unlock_rename(struct dentry *p1, struct dentry *p2)
1946 mutex_unlock(&p1->d_inode->i_mutex);
1948 mutex_unlock(&p2->d_inode->i_mutex);
1949 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1953 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1954 struct nameidata *nd)
1956 int error = may_create(dir, dentry);
1961 if (!dir->i_op->create)
1962 return -EACCES; /* shouldn't it be ENOSYS? */
1965 error = security_inode_create(dir, dentry, mode);
1968 error = dir->i_op->create(dir, dentry, mode, nd);
1970 fsnotify_create(dir, dentry);
1974 static int may_open(struct path *path, int acc_mode, int flag)
1976 struct dentry *dentry = path->dentry;
1977 struct inode *inode = dentry->d_inode;
1987 switch (inode->i_mode & S_IFMT) {
1991 if (acc_mode & MAY_WRITE)
1996 if (path->mnt->mnt_flags & MNT_NODEV)
2005 error = inode_permission(inode, acc_mode);
2010 * An append-only file must be opened in append mode for writing.
2012 if (IS_APPEND(inode)) {
2013 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
2019 /* O_NOATIME can only be set by the owner or superuser */
2020 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
2024 * Ensure there are no outstanding leases on the file.
2026 return break_lease(inode, flag);
2029 static int handle_truncate(struct file *filp)
2031 struct path *path = &filp->f_path;
2032 struct inode *inode = path->dentry->d_inode;
2033 int error = get_write_access(inode);
2037 * Refuse to truncate files with mandatory locks held on them.
2039 error = locks_verify_locked(inode);
2041 error = security_path_truncate(path);
2043 error = do_truncate(path->dentry, 0,
2044 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2047 put_write_access(inode);
2052 * Note that while the flag value (low two bits) for sys_open means:
2057 * it is changed into
2058 * 00 - no permissions needed
2059 * 01 - read-permission
2060 * 10 - write-permission
2062 * for the internal routines (ie open_namei()/follow_link() etc)
2063 * This is more logical, and also allows the 00 "no perm needed"
2064 * to be used for symlinks (where the permissions are checked
2068 static inline int open_to_namei_flags(int flag)
2070 if ((flag+1) & O_ACCMODE)
2076 * Handle the last step of open()
2078 static struct file *do_last(struct nameidata *nd, struct path *path,
2079 const struct open_flags *op, const char *pathname)
2081 struct dentry *dir = nd->path.dentry;
2082 struct dentry *dentry;
2083 int open_flag = op->open_flag;
2084 int will_truncate = open_flag & O_TRUNC;
2086 int acc_mode = op->acc_mode;
2090 nd->flags &= ~LOOKUP_PARENT;
2091 nd->flags |= op->intent;
2093 switch (nd->last_type) {
2096 error = handle_dots(nd, nd->last_type);
2098 return ERR_PTR(error);
2101 error = complete_walk(nd);
2103 return ERR_PTR(error);
2104 audit_inode(pathname, nd->path.dentry);
2105 if (open_flag & O_CREAT) {
2111 error = complete_walk(nd);
2113 return ERR_PTR(error);
2114 audit_inode(pathname, dir);
2118 if (!(open_flag & O_CREAT)) {
2120 if (nd->last.name[nd->last.len])
2121 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2122 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2124 /* we _can_ be in RCU mode here */
2125 error = walk_component(nd, path, &nd->last, LAST_NORM,
2128 return ERR_PTR(error);
2129 if (error) /* symlink */
2132 error = complete_walk(nd);
2134 return ERR_PTR(-ECHILD);
2137 if (nd->flags & LOOKUP_DIRECTORY) {
2138 if (!nd->inode->i_op->lookup)
2141 audit_inode(pathname, nd->path.dentry);
2145 /* create side of things */
2146 error = complete_walk(nd);
2148 return ERR_PTR(error);
2150 audit_inode(pathname, dir);
2152 /* trailing slashes? */
2153 if (nd->last.name[nd->last.len])
2156 mutex_lock(&dir->d_inode->i_mutex);
2158 dentry = lookup_hash(nd);
2159 error = PTR_ERR(dentry);
2160 if (IS_ERR(dentry)) {
2161 mutex_unlock(&dir->d_inode->i_mutex);
2165 path->dentry = dentry;
2166 path->mnt = nd->path.mnt;
2168 /* Negative dentry, just create the file */
2169 if (!dentry->d_inode) {
2170 int mode = op->mode;
2171 if (!IS_POSIXACL(dir->d_inode))
2172 mode &= ~current_umask();
2174 * This write is needed to ensure that a
2175 * rw->ro transition does not occur between
2176 * the time when the file is created and when
2177 * a permanent write count is taken through
2178 * the 'struct file' in nameidata_to_filp().
2180 error = mnt_want_write(nd->path.mnt);
2182 goto exit_mutex_unlock;
2184 /* Don't check for write permission, don't truncate */
2185 open_flag &= ~O_TRUNC;
2187 acc_mode = MAY_OPEN;
2188 error = security_path_mknod(&nd->path, dentry, mode, 0);
2190 goto exit_mutex_unlock;
2191 error = vfs_create(dir->d_inode, dentry, mode, nd);
2193 goto exit_mutex_unlock;
2194 mutex_unlock(&dir->d_inode->i_mutex);
2195 dput(nd->path.dentry);
2196 nd->path.dentry = dentry;
2201 * It already exists.
2203 mutex_unlock(&dir->d_inode->i_mutex);
2204 audit_inode(pathname, path->dentry);
2207 if (open_flag & O_EXCL)
2210 error = follow_managed(path, nd->flags);
2215 if (!path->dentry->d_inode)
2218 if (path->dentry->d_inode->i_op->follow_link)
2221 path_to_nameidata(path, nd);
2222 nd->inode = path->dentry->d_inode;
2224 if (S_ISDIR(nd->inode->i_mode))
2227 if (!S_ISREG(nd->inode->i_mode))
2230 if (will_truncate) {
2231 error = mnt_want_write(nd->path.mnt);
2237 error = may_open(&nd->path, acc_mode, open_flag);
2240 filp = nameidata_to_filp(nd);
2241 if (!IS_ERR(filp)) {
2242 error = ima_file_check(filp, op->acc_mode);
2245 filp = ERR_PTR(error);
2248 if (!IS_ERR(filp)) {
2249 if (will_truncate) {
2250 error = handle_truncate(filp);
2253 filp = ERR_PTR(error);
2259 mnt_drop_write(nd->path.mnt);
2260 path_put(&nd->path);
2264 mutex_unlock(&dir->d_inode->i_mutex);
2266 path_put_conditional(path, nd);
2268 filp = ERR_PTR(error);
2272 static struct file *path_openat(int dfd, const char *pathname,
2273 struct nameidata *nd, const struct open_flags *op, int flags)
2275 struct file *base = NULL;
2280 filp = get_empty_filp();
2282 return ERR_PTR(-ENFILE);
2284 filp->f_flags = op->open_flag;
2285 nd->intent.open.file = filp;
2286 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2287 nd->intent.open.create_mode = op->mode;
2289 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
2290 if (unlikely(error))
2293 current->total_link_count = 0;
2294 error = link_path_walk(pathname, nd);
2295 if (unlikely(error))
2298 filp = do_last(nd, &path, op, pathname);
2299 while (unlikely(!filp)) { /* trailing symlink */
2300 struct path link = path;
2302 if (!(nd->flags & LOOKUP_FOLLOW)) {
2303 path_put_conditional(&path, nd);
2304 path_put(&nd->path);
2305 filp = ERR_PTR(-ELOOP);
2308 nd->flags |= LOOKUP_PARENT;
2309 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2310 error = follow_link(&link, nd, &cookie);
2311 if (unlikely(error))
2312 filp = ERR_PTR(error);
2314 filp = do_last(nd, &path, op, pathname);
2315 put_link(nd, &link, cookie);
2318 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2319 path_put(&nd->root);
2322 release_open_intent(nd);
2326 filp = ERR_PTR(error);
2330 struct file *do_filp_open(int dfd, const char *pathname,
2331 const struct open_flags *op, int flags)
2333 struct nameidata nd;
2336 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
2337 if (unlikely(filp == ERR_PTR(-ECHILD)))
2338 filp = path_openat(dfd, pathname, &nd, op, flags);
2339 if (unlikely(filp == ERR_PTR(-ESTALE)))
2340 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
2344 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2345 const char *name, const struct open_flags *op, int flags)
2347 struct nameidata nd;
2351 nd.root.dentry = dentry;
2353 flags |= LOOKUP_ROOT;
2355 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
2356 return ERR_PTR(-ELOOP);
2358 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2359 if (unlikely(file == ERR_PTR(-ECHILD)))
2360 file = path_openat(-1, name, &nd, op, flags);
2361 if (unlikely(file == ERR_PTR(-ESTALE)))
2362 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2367 * lookup_create - lookup a dentry, creating it if it doesn't exist
2368 * @nd: nameidata info
2369 * @is_dir: directory flag
2371 * Simple function to lookup and return a dentry and create it
2372 * if it doesn't exist. Is SMP-safe.
2374 * Returns with nd->path.dentry->d_inode->i_mutex locked.
2376 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
2378 struct dentry *dentry = ERR_PTR(-EEXIST);
2380 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2382 * Yucky last component or no last component at all?
2383 * (foo/., foo/.., /////)
2385 if (nd->last_type != LAST_NORM)
2387 nd->flags &= ~LOOKUP_PARENT;
2388 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2389 nd->intent.open.flags = O_EXCL;
2392 * Do the final lookup.
2394 dentry = lookup_hash(nd);
2398 if (dentry->d_inode)
2401 * Special case - lookup gave negative, but... we had foo/bar/
2402 * From the vfs_mknod() POV we just have a negative dentry -
2403 * all is fine. Let's be bastards - you had / on the end, you've
2404 * been asking for (non-existent) directory. -ENOENT for you.
2406 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
2408 dentry = ERR_PTR(-ENOENT);
2413 dentry = ERR_PTR(-EEXIST);
2417 EXPORT_SYMBOL_GPL(lookup_create);
2419 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2421 int error = may_create(dir, dentry);
2426 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2427 !ns_capable(inode_userns(dir), CAP_MKNOD))
2430 if (!dir->i_op->mknod)
2433 error = devcgroup_inode_mknod(mode, dev);
2437 error = security_inode_mknod(dir, dentry, mode, dev);
2441 error = dir->i_op->mknod(dir, dentry, mode, dev);
2443 fsnotify_create(dir, dentry);
2447 static int may_mknod(mode_t mode)
2449 switch (mode & S_IFMT) {
2455 case 0: /* zero mode translates to S_IFREG */
2464 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2469 struct dentry *dentry;
2470 struct nameidata nd;
2475 error = user_path_parent(dfd, filename, &nd, &tmp);
2479 dentry = lookup_create(&nd, 0);
2480 if (IS_ERR(dentry)) {
2481 error = PTR_ERR(dentry);
2484 if (!IS_POSIXACL(nd.path.dentry->d_inode))
2485 mode &= ~current_umask();
2486 error = may_mknod(mode);
2489 error = mnt_want_write(nd.path.mnt);
2492 error = security_path_mknod(&nd.path, dentry, mode, dev);
2494 goto out_drop_write;
2495 switch (mode & S_IFMT) {
2496 case 0: case S_IFREG:
2497 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
2499 case S_IFCHR: case S_IFBLK:
2500 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
2501 new_decode_dev(dev));
2503 case S_IFIFO: case S_IFSOCK:
2504 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
2508 mnt_drop_write(nd.path.mnt);
2512 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2519 SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
2521 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2524 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2526 int error = may_create(dir, dentry);
2531 if (!dir->i_op->mkdir)
2534 mode &= (S_IRWXUGO|S_ISVTX);
2535 error = security_inode_mkdir(dir, dentry, mode);
2539 error = dir->i_op->mkdir(dir, dentry, mode);
2541 fsnotify_mkdir(dir, dentry);
2545 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
2549 struct dentry *dentry;
2550 struct nameidata nd;
2552 error = user_path_parent(dfd, pathname, &nd, &tmp);
2556 dentry = lookup_create(&nd, 1);
2557 error = PTR_ERR(dentry);
2561 if (!IS_POSIXACL(nd.path.dentry->d_inode))
2562 mode &= ~current_umask();
2563 error = mnt_want_write(nd.path.mnt);
2566 error = security_path_mkdir(&nd.path, dentry, mode);
2568 goto out_drop_write;
2569 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2571 mnt_drop_write(nd.path.mnt);
2575 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2582 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
2584 return sys_mkdirat(AT_FDCWD, pathname, mode);
2588 * The dentry_unhash() helper will try to drop the dentry early: we
2589 * should have a usage count of 2 if we're the only user of this
2590 * dentry, and if that is true (possibly after pruning the dcache),
2591 * then we drop the dentry now.
2593 * A low-level filesystem can, if it choses, legally
2596 * if (!d_unhashed(dentry))
2599 * if it cannot handle the case of removing a directory
2600 * that is still in use by something else..
2602 void dentry_unhash(struct dentry *dentry)
2604 shrink_dcache_parent(dentry);
2605 spin_lock(&dentry->d_lock);
2606 if (dentry->d_count == 1)
2608 spin_unlock(&dentry->d_lock);
2611 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2613 int error = may_delete(dir, dentry, 1);
2618 if (!dir->i_op->rmdir)
2621 mutex_lock(&dentry->d_inode->i_mutex);
2624 if (d_mountpoint(dentry))
2627 error = security_inode_rmdir(dir, dentry);
2631 shrink_dcache_parent(dentry);
2632 error = dir->i_op->rmdir(dir, dentry);
2636 dentry->d_inode->i_flags |= S_DEAD;
2640 mutex_unlock(&dentry->d_inode->i_mutex);
2646 static long do_rmdir(int dfd, const char __user *pathname)
2650 struct dentry *dentry;
2651 struct nameidata nd;
2653 error = user_path_parent(dfd, pathname, &nd, &name);
2657 switch(nd.last_type) {
2669 nd.flags &= ~LOOKUP_PARENT;
2671 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2672 dentry = lookup_hash(&nd);
2673 error = PTR_ERR(dentry);
2676 if (!dentry->d_inode) {
2680 error = mnt_want_write(nd.path.mnt);
2683 error = security_path_rmdir(&nd.path, dentry);
2686 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2688 mnt_drop_write(nd.path.mnt);
2692 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2699 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
2701 return do_rmdir(AT_FDCWD, pathname);
2704 int vfs_unlink(struct inode *dir, struct dentry *dentry)
2706 int error = may_delete(dir, dentry, 0);
2711 if (!dir->i_op->unlink)
2714 mutex_lock(&dentry->d_inode->i_mutex);
2715 if (d_mountpoint(dentry))
2718 error = security_inode_unlink(dir, dentry);
2720 error = dir->i_op->unlink(dir, dentry);
2725 mutex_unlock(&dentry->d_inode->i_mutex);
2727 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2728 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2729 fsnotify_link_count(dentry->d_inode);
2737 * Make sure that the actual truncation of the file will occur outside its
2738 * directory's i_mutex. Truncate can take a long time if there is a lot of
2739 * writeout happening, and we don't want to prevent access to the directory
2740 * while waiting on the I/O.
2742 static long do_unlinkat(int dfd, const char __user *pathname)
2746 struct dentry *dentry;
2747 struct nameidata nd;
2748 struct inode *inode = NULL;
2750 error = user_path_parent(dfd, pathname, &nd, &name);
2755 if (nd.last_type != LAST_NORM)
2758 nd.flags &= ~LOOKUP_PARENT;
2760 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2761 dentry = lookup_hash(&nd);
2762 error = PTR_ERR(dentry);
2763 if (!IS_ERR(dentry)) {
2764 /* Why not before? Because we want correct error value */
2765 if (nd.last.name[nd.last.len])
2767 inode = dentry->d_inode;
2771 error = mnt_want_write(nd.path.mnt);
2774 error = security_path_unlink(&nd.path, dentry);
2777 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2779 mnt_drop_write(nd.path.mnt);
2783 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2785 iput(inode); /* truncate the inode here */
2792 error = !dentry->d_inode ? -ENOENT :
2793 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2797 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
2799 if ((flag & ~AT_REMOVEDIR) != 0)
2802 if (flag & AT_REMOVEDIR)
2803 return do_rmdir(dfd, pathname);
2805 return do_unlinkat(dfd, pathname);
2808 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
2810 return do_unlinkat(AT_FDCWD, pathname);
2813 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
2815 int error = may_create(dir, dentry);
2820 if (!dir->i_op->symlink)
2823 error = security_inode_symlink(dir, dentry, oldname);
2827 error = dir->i_op->symlink(dir, dentry, oldname);
2829 fsnotify_create(dir, dentry);
2833 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2834 int, newdfd, const char __user *, newname)
2839 struct dentry *dentry;
2840 struct nameidata nd;
2842 from = getname(oldname);
2844 return PTR_ERR(from);
2846 error = user_path_parent(newdfd, newname, &nd, &to);
2850 dentry = lookup_create(&nd, 0);
2851 error = PTR_ERR(dentry);
2855 error = mnt_want_write(nd.path.mnt);
2858 error = security_path_symlink(&nd.path, dentry, from);
2860 goto out_drop_write;
2861 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2863 mnt_drop_write(nd.path.mnt);
2867 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2875 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
2877 return sys_symlinkat(oldname, AT_FDCWD, newname);
2880 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2882 struct inode *inode = old_dentry->d_inode;
2888 error = may_create(dir, new_dentry);
2892 if (dir->i_sb != inode->i_sb)
2896 * A link to an append-only or immutable file cannot be created.
2898 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2900 if (!dir->i_op->link)
2902 if (S_ISDIR(inode->i_mode))
2905 error = security_inode_link(old_dentry, dir, new_dentry);
2909 mutex_lock(&inode->i_mutex);
2910 /* Make sure we don't allow creating hardlink to an unlinked file */
2911 if (inode->i_nlink == 0)
2914 error = dir->i_op->link(old_dentry, dir, new_dentry);
2915 mutex_unlock(&inode->i_mutex);
2917 fsnotify_link(dir, inode, new_dentry);
2922 * Hardlinks are often used in delicate situations. We avoid
2923 * security-related surprises by not following symlinks on the
2926 * We don't follow them on the oldname either to be compatible
2927 * with linux 2.0, and to avoid hard-linking to directories
2928 * and other special files. --ADM
2930 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2931 int, newdfd, const char __user *, newname, int, flags)
2933 struct dentry *new_dentry;
2934 struct nameidata nd;
2935 struct path old_path;
2940 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
2943 * To use null names we require CAP_DAC_READ_SEARCH
2944 * This ensures that not everyone will be able to create
2945 * handlink using the passed filedescriptor.
2947 if (flags & AT_EMPTY_PATH) {
2948 if (!capable(CAP_DAC_READ_SEARCH))
2953 if (flags & AT_SYMLINK_FOLLOW)
2954 how |= LOOKUP_FOLLOW;
2956 error = user_path_at(olddfd, oldname, how, &old_path);
2960 error = user_path_parent(newdfd, newname, &nd, &to);
2964 if (old_path.mnt != nd.path.mnt)
2966 new_dentry = lookup_create(&nd, 0);
2967 error = PTR_ERR(new_dentry);
2968 if (IS_ERR(new_dentry))
2970 error = mnt_want_write(nd.path.mnt);
2973 error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2975 goto out_drop_write;
2976 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
2978 mnt_drop_write(nd.path.mnt);
2982 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2987 path_put(&old_path);
2992 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
2994 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
2998 * The worst of all namespace operations - renaming directory. "Perverted"
2999 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3001 * a) we can get into loop creation. Check is done in is_subdir().
3002 * b) race potential - two innocent renames can create a loop together.
3003 * That's where 4.4 screws up. Current fix: serialization on
3004 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
3006 * c) we have to lock _three_ objects - parents and victim (if it exists).
3007 * And that - after we got ->i_mutex on parents (until then we don't know
3008 * whether the target exists). Solution: try to be smart with locking
3009 * order for inodes. We rely on the fact that tree topology may change
3010 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
3011 * move will be locked. Thus we can rank directories by the tree
3012 * (ancestors first) and rank all non-directories after them.
3013 * That works since everybody except rename does "lock parent, lookup,
3014 * lock child" and rename is under ->s_vfs_rename_mutex.
3015 * HOWEVER, it relies on the assumption that any object with ->lookup()
3016 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3017 * we'd better make sure that there's no link(2) for them.
3018 * d) conversion from fhandle to dentry may come in the wrong moment - when
3019 * we are removing the target. Solution: we will have to grab ->i_mutex
3020 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3021 * ->i_mutex on parents, which works but leads to some truly excessive
3024 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3025 struct inode *new_dir, struct dentry *new_dentry)
3028 struct inode *target = new_dentry->d_inode;
3031 * If we are going to change the parent - check write permissions,
3032 * we'll need to flip '..'.
3034 if (new_dir != old_dir) {
3035 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
3040 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3045 mutex_lock(&target->i_mutex);
3048 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3052 shrink_dcache_parent(new_dentry);
3053 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3058 target->i_flags |= S_DEAD;
3059 dont_mount(new_dentry);
3063 mutex_unlock(&target->i_mutex);
3065 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3066 d_move(old_dentry,new_dentry);
3070 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3071 struct inode *new_dir, struct dentry *new_dentry)
3073 struct inode *target = new_dentry->d_inode;
3076 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3082 mutex_lock(&target->i_mutex);
3085 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3088 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3093 dont_mount(new_dentry);
3094 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3095 d_move(old_dentry, new_dentry);
3098 mutex_unlock(&target->i_mutex);
3103 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3104 struct inode *new_dir, struct dentry *new_dentry)
3107 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
3108 const unsigned char *old_name;
3110 if (old_dentry->d_inode == new_dentry->d_inode)
3113 error = may_delete(old_dir, old_dentry, is_dir);
3117 if (!new_dentry->d_inode)
3118 error = may_create(new_dir, new_dentry);
3120 error = may_delete(new_dir, new_dentry, is_dir);
3124 if (!old_dir->i_op->rename)
3127 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3130 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3132 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3134 fsnotify_move(old_dir, new_dir, old_name, is_dir,
3135 new_dentry->d_inode, old_dentry);
3136 fsnotify_oldname_free(old_name);
3141 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3142 int, newdfd, const char __user *, newname)
3144 struct dentry *old_dir, *new_dir;
3145 struct dentry *old_dentry, *new_dentry;
3146 struct dentry *trap;
3147 struct nameidata oldnd, newnd;
3152 error = user_path_parent(olddfd, oldname, &oldnd, &from);
3156 error = user_path_parent(newdfd, newname, &newnd, &to);
3161 if (oldnd.path.mnt != newnd.path.mnt)
3164 old_dir = oldnd.path.dentry;
3166 if (oldnd.last_type != LAST_NORM)
3169 new_dir = newnd.path.dentry;
3170 if (newnd.last_type != LAST_NORM)
3173 oldnd.flags &= ~LOOKUP_PARENT;
3174 newnd.flags &= ~LOOKUP_PARENT;
3175 newnd.flags |= LOOKUP_RENAME_TARGET;
3177 trap = lock_rename(new_dir, old_dir);
3179 old_dentry = lookup_hash(&oldnd);
3180 error = PTR_ERR(old_dentry);
3181 if (IS_ERR(old_dentry))
3183 /* source must exist */
3185 if (!old_dentry->d_inode)
3187 /* unless the source is a directory trailing slashes give -ENOTDIR */
3188 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3190 if (oldnd.last.name[oldnd.last.len])
3192 if (newnd.last.name[newnd.last.len])
3195 /* source should not be ancestor of target */
3197 if (old_dentry == trap)
3199 new_dentry = lookup_hash(&newnd);
3200 error = PTR_ERR(new_dentry);
3201 if (IS_ERR(new_dentry))
3203 /* target should not be an ancestor of source */
3205 if (new_dentry == trap)
3208 error = mnt_want_write(oldnd.path.mnt);
3211 error = security_path_rename(&oldnd.path, old_dentry,
3212 &newnd.path, new_dentry);
3215 error = vfs_rename(old_dir->d_inode, old_dentry,
3216 new_dir->d_inode, new_dentry);
3218 mnt_drop_write(oldnd.path.mnt);
3224 unlock_rename(new_dir, old_dir);
3226 path_put(&newnd.path);
3229 path_put(&oldnd.path);
3235 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
3237 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3240 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3244 len = PTR_ERR(link);
3249 if (len > (unsigned) buflen)
3251 if (copy_to_user(buffer, link, len))
3258 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3259 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3260 * using) it for any given inode is up to filesystem.
3262 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3264 struct nameidata nd;
3269 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3271 return PTR_ERR(cookie);
3273 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3274 if (dentry->d_inode->i_op->put_link)
3275 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3279 int vfs_follow_link(struct nameidata *nd, const char *link)
3281 return __vfs_follow_link(nd, link);
3284 /* get the link contents into pagecache */
3285 static char *page_getlink(struct dentry * dentry, struct page **ppage)
3289 struct address_space *mapping = dentry->d_inode->i_mapping;
3290 page = read_mapping_page(mapping, 0, NULL);
3295 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3299 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3301 struct page *page = NULL;
3302 char *s = page_getlink(dentry, &page);
3303 int res = vfs_readlink(dentry,buffer,buflen,s);
3306 page_cache_release(page);
3311 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
3313 struct page *page = NULL;
3314 nd_set_link(nd, page_getlink(dentry, &page));
3318 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
3320 struct page *page = cookie;
3324 page_cache_release(page);
3329 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3331 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
3333 struct address_space *mapping = inode->i_mapping;
3338 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3340 flags |= AOP_FLAG_NOFS;
3343 err = pagecache_write_begin(NULL, mapping, 0, len-1,
3344 flags, &page, &fsdata);
3348 kaddr = kmap_atomic(page, KM_USER0);
3349 memcpy(kaddr, symname, len-1);
3350 kunmap_atomic(kaddr, KM_USER0);
3352 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3359 mark_inode_dirty(inode);
3365 int page_symlink(struct inode *inode, const char *symname, int len)
3367 return __page_symlink(inode, symname, len,
3368 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
3371 const struct inode_operations page_symlink_inode_operations = {
3372 .readlink = generic_readlink,
3373 .follow_link = page_follow_link_light,
3374 .put_link = page_put_link,
3377 EXPORT_SYMBOL(user_path_at);
3378 EXPORT_SYMBOL(follow_down_one);
3379 EXPORT_SYMBOL(follow_down);
3380 EXPORT_SYMBOL(follow_up);
3381 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3382 EXPORT_SYMBOL(getname);
3383 EXPORT_SYMBOL(lock_rename);
3384 EXPORT_SYMBOL(lookup_one_len);
3385 EXPORT_SYMBOL(page_follow_link_light);
3386 EXPORT_SYMBOL(page_put_link);
3387 EXPORT_SYMBOL(page_readlink);
3388 EXPORT_SYMBOL(__page_symlink);
3389 EXPORT_SYMBOL(page_symlink);
3390 EXPORT_SYMBOL(page_symlink_inode_operations);
3391 EXPORT_SYMBOL(kern_path_parent);
3392 EXPORT_SYMBOL(kern_path);
3393 EXPORT_SYMBOL(vfs_path_lookup);
3394 EXPORT_SYMBOL(inode_permission);
3395 EXPORT_SYMBOL(unlock_rename);
3396 EXPORT_SYMBOL(vfs_create);
3397 EXPORT_SYMBOL(vfs_follow_link);
3398 EXPORT_SYMBOL(vfs_link);
3399 EXPORT_SYMBOL(vfs_mkdir);
3400 EXPORT_SYMBOL(vfs_mknod);
3401 EXPORT_SYMBOL(generic_permission);
3402 EXPORT_SYMBOL(vfs_readlink);
3403 EXPORT_SYMBOL(vfs_rename);
3404 EXPORT_SYMBOL(vfs_rmdir);
3405 EXPORT_SYMBOL(vfs_symlink);
3406 EXPORT_SYMBOL(vfs_unlink);
3407 EXPORT_SYMBOL(dentry_unhash);
3408 EXPORT_SYMBOL(generic_readlink);