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 umode_t 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 if at least one exec bit is set.
243 if (!(mask & MAY_EXEC) || execute_ok(inode))
244 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
248 * Searching includes executable on directories, else just read.
250 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
251 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
252 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
259 * inode_permission - check for access rights to a given inode
260 * @inode: inode to check permission on
261 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
263 * Used to check for read/write/execute permissions on an inode.
264 * We use "fsuid" for this, letting us set arbitrary permissions
265 * for filesystem access without changing the "normal" uids which
266 * are used for other things.
268 int inode_permission(struct inode *inode, int mask)
272 if (mask & MAY_WRITE) {
273 umode_t mode = inode->i_mode;
276 * Nobody gets write access to a read-only fs.
278 if (IS_RDONLY(inode) &&
279 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
283 * Nobody gets write access to an immutable file.
285 if (IS_IMMUTABLE(inode))
289 if (inode->i_op->permission)
290 retval = inode->i_op->permission(inode, mask, 0);
292 retval = generic_permission(inode, mask, 0,
293 inode->i_op->check_acl);
298 retval = devcgroup_inode_permission(inode, mask);
302 return security_inode_permission(inode, mask);
306 * file_permission - check for additional access rights to a given file
307 * @file: file to check access rights for
308 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
310 * Used to check for read/write/execute permissions on an already opened
314 * Do not use this function in new code. All access checks should
315 * be done using inode_permission().
317 int file_permission(struct file *file, int mask)
319 return inode_permission(file->f_path.dentry->d_inode, mask);
323 * get_write_access() gets write permission for a file.
324 * put_write_access() releases this write permission.
325 * This is used for regular files.
326 * We cannot support write (and maybe mmap read-write shared) accesses and
327 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
328 * can have the following values:
329 * 0: no writers, no VM_DENYWRITE mappings
330 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
331 * > 0: (i_writecount) users are writing to the file.
333 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
334 * except for the cases where we don't hold i_writecount yet. Then we need to
335 * use {get,deny}_write_access() - these functions check the sign and refuse
336 * to do the change if sign is wrong. Exclusion between them is provided by
337 * the inode->i_lock spinlock.
340 int get_write_access(struct inode * inode)
342 spin_lock(&inode->i_lock);
343 if (atomic_read(&inode->i_writecount) < 0) {
344 spin_unlock(&inode->i_lock);
347 atomic_inc(&inode->i_writecount);
348 spin_unlock(&inode->i_lock);
353 int deny_write_access(struct file * file)
355 struct inode *inode = file->f_path.dentry->d_inode;
357 spin_lock(&inode->i_lock);
358 if (atomic_read(&inode->i_writecount) > 0) {
359 spin_unlock(&inode->i_lock);
362 atomic_dec(&inode->i_writecount);
363 spin_unlock(&inode->i_lock);
369 * path_get - get a reference to a path
370 * @path: path to get the reference to
372 * Given a path increment the reference count to the dentry and the vfsmount.
374 void path_get(struct path *path)
379 EXPORT_SYMBOL(path_get);
382 * path_put - put a reference to a path
383 * @path: path to put the reference to
385 * Given a path decrement the reference count to the dentry and the vfsmount.
387 void path_put(struct path *path)
392 EXPORT_SYMBOL(path_put);
395 * Path walking has 2 modes, rcu-walk and ref-walk (see
396 * Documentation/filesystems/path-lookup.txt). In situations when we can't
397 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
398 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
399 * mode. Refcounts are grabbed at the last known good point before rcu-walk
400 * got stuck, so ref-walk may continue from there. If this is not successful
401 * (eg. a seqcount has changed), then failure is returned and it's up to caller
402 * to restart the path walk from the beginning in ref-walk mode.
406 * unlazy_walk - try to switch to ref-walk mode.
407 * @nd: nameidata pathwalk data
408 * @dentry: child of nd->path.dentry or NULL
409 * Returns: 0 on success, -ECHILD on failure
411 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
412 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
413 * @nd or NULL. Must be called from rcu-walk context.
415 static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
417 struct fs_struct *fs = current->fs;
418 struct dentry *parent = nd->path.dentry;
421 BUG_ON(!(nd->flags & LOOKUP_RCU));
422 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
424 spin_lock(&fs->lock);
425 if (nd->root.mnt != fs->root.mnt ||
426 nd->root.dentry != fs->root.dentry)
429 spin_lock(&parent->d_lock);
431 if (!__d_rcu_to_refcount(parent, nd->seq))
433 BUG_ON(nd->inode != parent->d_inode);
435 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
436 if (!__d_rcu_to_refcount(dentry, nd->seq))
439 * If the sequence check on the child dentry passed, then
440 * the child has not been removed from its parent. This
441 * means the parent dentry must be valid and able to take
442 * a reference at this point.
444 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
445 BUG_ON(!parent->d_count);
447 spin_unlock(&dentry->d_lock);
449 spin_unlock(&parent->d_lock);
452 spin_unlock(&fs->lock);
454 mntget(nd->path.mnt);
457 br_read_unlock(vfsmount_lock);
458 nd->flags &= ~LOOKUP_RCU;
462 spin_unlock(&dentry->d_lock);
464 spin_unlock(&parent->d_lock);
467 spin_unlock(&fs->lock);
472 * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk
473 * @nd: nameidata pathwalk data to drop
474 * Returns: 0 on success, -ECHILD on failure
476 * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk.
477 * nd->path should be the final element of the lookup, so nd->root is discarded.
478 * Must be called from rcu-walk context.
480 static int nameidata_drop_rcu_last(struct nameidata *nd)
482 struct dentry *dentry = nd->path.dentry;
484 BUG_ON(!(nd->flags & LOOKUP_RCU));
485 nd->flags &= ~LOOKUP_RCU;
486 if (!(nd->flags & LOOKUP_ROOT))
488 spin_lock(&dentry->d_lock);
489 if (!__d_rcu_to_refcount(dentry, nd->seq))
491 BUG_ON(nd->inode != dentry->d_inode);
492 spin_unlock(&dentry->d_lock);
494 mntget(nd->path.mnt);
497 br_read_unlock(vfsmount_lock);
502 spin_unlock(&dentry->d_lock);
504 br_read_unlock(vfsmount_lock);
509 * release_open_intent - free up open intent resources
510 * @nd: pointer to nameidata
512 void release_open_intent(struct nameidata *nd)
514 struct file *file = nd->intent.open.file;
516 if (file && !IS_ERR(file)) {
517 if (file->f_path.dentry == NULL)
524 static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
526 return dentry->d_op->d_revalidate(dentry, nd);
529 static struct dentry *
530 do_revalidate(struct dentry *dentry, struct nameidata *nd)
532 int status = d_revalidate(dentry, nd);
533 if (unlikely(status <= 0)) {
535 * The dentry failed validation.
536 * If d_revalidate returned 0 attempt to invalidate
537 * the dentry otherwise d_revalidate is asking us
538 * to return a fail status.
542 dentry = ERR_PTR(status);
543 } else if (!d_invalidate(dentry)) {
552 * handle_reval_path - force revalidation of a dentry
554 * In some situations the path walking code will trust dentries without
555 * revalidating them. This causes problems for filesystems that depend on
556 * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
557 * (which indicates that it's possible for the dentry to go stale), force
558 * a d_revalidate call before proceeding.
560 * Returns 0 if the revalidation was successful. If the revalidation fails,
561 * either return the error returned by d_revalidate or -ESTALE if the
562 * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
563 * invalidate the dentry. It's up to the caller to handle putting references
564 * to the path if necessary.
566 static inline int handle_reval_path(struct nameidata *nd)
568 struct dentry *dentry = nd->path.dentry;
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 * Short-cut version of permission(), for calling on directories
593 * during pathname resolution. Combines parts of permission()
594 * and generic_permission(), and tests ONLY for MAY_EXEC permission.
596 * If appropriate, check DAC only. If not appropriate, or
597 * short-cut DAC fails, then call ->permission() to do more
598 * complete permission check.
600 static inline int exec_permission(struct inode *inode, unsigned int flags)
603 struct user_namespace *ns = inode_userns(inode);
605 if (inode->i_op->permission) {
606 ret = inode->i_op->permission(inode, MAY_EXEC, flags);
608 ret = acl_permission_check(inode, MAY_EXEC, flags,
609 inode->i_op->check_acl);
616 if (ns_capable(ns, CAP_DAC_OVERRIDE) ||
617 ns_capable(ns, CAP_DAC_READ_SEARCH))
622 return security_inode_exec_permission(inode, flags);
625 static __always_inline void set_root(struct nameidata *nd)
628 get_fs_root(current->fs, &nd->root);
631 static int link_path_walk(const char *, struct nameidata *);
633 static __always_inline void set_root_rcu(struct nameidata *nd)
636 struct fs_struct *fs = current->fs;
640 seq = read_seqcount_begin(&fs->seq);
642 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
643 } while (read_seqcount_retry(&fs->seq, seq));
647 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
659 nd->flags |= LOOKUP_JUMPED;
661 nd->inode = nd->path.dentry->d_inode;
663 ret = link_path_walk(link, nd);
667 return PTR_ERR(link);
670 static void path_put_conditional(struct path *path, struct nameidata *nd)
673 if (path->mnt != nd->path.mnt)
677 static inline void path_to_nameidata(const struct path *path,
678 struct nameidata *nd)
680 if (!(nd->flags & LOOKUP_RCU)) {
681 dput(nd->path.dentry);
682 if (nd->path.mnt != path->mnt)
683 mntput(nd->path.mnt);
685 nd->path.mnt = path->mnt;
686 nd->path.dentry = path->dentry;
689 static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
691 struct inode *inode = link->dentry->d_inode;
692 if (!IS_ERR(cookie) && inode->i_op->put_link)
693 inode->i_op->put_link(link->dentry, nd, cookie);
697 static __always_inline int
698 follow_link(struct path *link, struct nameidata *nd, void **p)
701 struct dentry *dentry = link->dentry;
703 BUG_ON(nd->flags & LOOKUP_RCU);
705 if (link->mnt == nd->path.mnt)
708 if (unlikely(current->total_link_count >= 40)) {
709 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
714 current->total_link_count++;
716 touch_atime(link->mnt, dentry);
717 nd_set_link(nd, NULL);
719 error = security_inode_follow_link(link->dentry, nd);
721 *p = ERR_PTR(error); /* no ->put_link(), please */
726 nd->last_type = LAST_BIND;
727 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
730 char *s = nd_get_link(nd);
733 error = __vfs_follow_link(nd, s);
734 else if (nd->last_type == LAST_BIND) {
735 nd->flags |= LOOKUP_JUMPED;
736 nd->inode = nd->path.dentry->d_inode;
737 if (nd->inode->i_op->follow_link) {
738 /* stepped on a _really_ weird one */
747 static int follow_up_rcu(struct path *path)
749 struct vfsmount *parent;
750 struct dentry *mountpoint;
752 parent = path->mnt->mnt_parent;
753 if (parent == path->mnt)
755 mountpoint = path->mnt->mnt_mountpoint;
756 path->dentry = mountpoint;
761 int follow_up(struct path *path)
763 struct vfsmount *parent;
764 struct dentry *mountpoint;
766 br_read_lock(vfsmount_lock);
767 parent = path->mnt->mnt_parent;
768 if (parent == path->mnt) {
769 br_read_unlock(vfsmount_lock);
773 mountpoint = dget(path->mnt->mnt_mountpoint);
774 br_read_unlock(vfsmount_lock);
776 path->dentry = mountpoint;
783 * Perform an automount
784 * - return -EISDIR to tell follow_managed() to stop and return the path we
787 static int follow_automount(struct path *path, unsigned flags,
790 struct vfsmount *mnt;
793 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
796 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
797 * and this is the terminal part of the path.
799 if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
800 return -EISDIR; /* we actually want to stop here */
802 /* We want to mount if someone is trying to open/create a file of any
803 * type under the mountpoint, wants to traverse through the mountpoint
804 * or wants to open the mounted directory.
806 * We don't want to mount if someone's just doing a stat and they've
807 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
808 * appended a '/' to the name.
810 if (!(flags & LOOKUP_FOLLOW) &&
811 !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
812 LOOKUP_OPEN | LOOKUP_CREATE)))
815 current->total_link_count++;
816 if (current->total_link_count >= 40)
819 mnt = path->dentry->d_op->d_automount(path);
822 * The filesystem is allowed to return -EISDIR here to indicate
823 * it doesn't want to automount. For instance, autofs would do
824 * this so that its userspace daemon can mount on this dentry.
826 * However, we can only permit this if it's a terminal point in
827 * the path being looked up; if it wasn't then the remainder of
828 * the path is inaccessible and we should say so.
830 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
835 if (!mnt) /* mount collision */
838 err = finish_automount(mnt, path);
842 /* Someone else made a mount here whilst we were busy */
849 path->dentry = dget(mnt->mnt_root);
859 * Handle a dentry that is managed in some way.
860 * - Flagged for transit management (autofs)
861 * - Flagged as mountpoint
862 * - Flagged as automount point
864 * This may only be called in refwalk mode.
866 * Serialization is taken care of in namespace.c
868 static int follow_managed(struct path *path, unsigned flags)
871 bool need_mntput = false;
874 /* Given that we're not holding a lock here, we retain the value in a
875 * local variable for each dentry as we look at it so that we don't see
876 * the components of that value change under us */
877 while (managed = ACCESS_ONCE(path->dentry->d_flags),
878 managed &= DCACHE_MANAGED_DENTRY,
879 unlikely(managed != 0)) {
880 /* Allow the filesystem to manage the transit without i_mutex
882 if (managed & DCACHE_MANAGE_TRANSIT) {
883 BUG_ON(!path->dentry->d_op);
884 BUG_ON(!path->dentry->d_op->d_manage);
885 ret = path->dentry->d_op->d_manage(path->dentry, false);
887 return ret == -EISDIR ? 0 : ret;
890 /* Transit to a mounted filesystem. */
891 if (managed & DCACHE_MOUNTED) {
892 struct vfsmount *mounted = lookup_mnt(path);
898 path->dentry = dget(mounted->mnt_root);
903 /* Something is mounted on this dentry in another
904 * namespace and/or whatever was mounted there in this
905 * namespace got unmounted before we managed to get the
909 /* Handle an automount point */
910 if (managed & DCACHE_NEED_AUTOMOUNT) {
911 ret = follow_automount(path, flags, &need_mntput);
913 return ret == -EISDIR ? 0 : ret;
917 /* We didn't change the current path point */
923 int follow_down_one(struct path *path)
925 struct vfsmount *mounted;
927 mounted = lookup_mnt(path);
932 path->dentry = dget(mounted->mnt_root);
938 static inline bool managed_dentry_might_block(struct dentry *dentry)
940 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
941 dentry->d_op->d_manage(dentry, true) < 0);
945 * Skip to top of mountpoint pile in rcuwalk mode. We abort the rcu-walk if we
946 * meet a managed dentry and we're not walking to "..". True is returned to
947 * continue, false to abort.
949 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
950 struct inode **inode, bool reverse_transit)
953 struct vfsmount *mounted;
955 * Don't forget we might have a non-mountpoint managed dentry
956 * that wants to block transit.
958 *inode = path->dentry->d_inode;
959 if (!reverse_transit &&
960 unlikely(managed_dentry_might_block(path->dentry)))
963 if (!d_mountpoint(path->dentry))
966 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
970 path->dentry = mounted->mnt_root;
971 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
974 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
975 return reverse_transit;
979 static int follow_dotdot_rcu(struct nameidata *nd)
981 struct inode *inode = nd->inode;
986 if (nd->path.dentry == nd->root.dentry &&
987 nd->path.mnt == nd->root.mnt) {
990 if (nd->path.dentry != nd->path.mnt->mnt_root) {
991 struct dentry *old = nd->path.dentry;
992 struct dentry *parent = old->d_parent;
995 seq = read_seqcount_begin(&parent->d_seq);
996 if (read_seqcount_retry(&old->d_seq, nd->seq))
998 inode = parent->d_inode;
999 nd->path.dentry = parent;
1003 if (!follow_up_rcu(&nd->path))
1005 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1006 inode = nd->path.dentry->d_inode;
1008 __follow_mount_rcu(nd, &nd->path, &inode, true);
1013 nd->flags &= ~LOOKUP_RCU;
1014 if (!(nd->flags & LOOKUP_ROOT))
1015 nd->root.mnt = NULL;
1017 br_read_unlock(vfsmount_lock);
1022 * Follow down to the covering mount currently visible to userspace. At each
1023 * point, the filesystem owning that dentry may be queried as to whether the
1024 * caller is permitted to proceed or not.
1026 * Care must be taken as namespace_sem may be held (indicated by mounting_here
1029 int follow_down(struct path *path)
1034 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1035 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1036 /* Allow the filesystem to manage the transit without i_mutex
1039 * We indicate to the filesystem if someone is trying to mount
1040 * something here. This gives autofs the chance to deny anyone
1041 * other than its daemon the right to mount on its
1044 * The filesystem may sleep at this point.
1046 if (managed & DCACHE_MANAGE_TRANSIT) {
1047 BUG_ON(!path->dentry->d_op);
1048 BUG_ON(!path->dentry->d_op->d_manage);
1049 ret = path->dentry->d_op->d_manage(
1050 path->dentry, false);
1052 return ret == -EISDIR ? 0 : ret;
1055 /* Transit to a mounted filesystem. */
1056 if (managed & DCACHE_MOUNTED) {
1057 struct vfsmount *mounted = lookup_mnt(path);
1062 path->mnt = mounted;
1063 path->dentry = dget(mounted->mnt_root);
1067 /* Don't handle automount points here */
1074 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1076 static void follow_mount(struct path *path)
1078 while (d_mountpoint(path->dentry)) {
1079 struct vfsmount *mounted = lookup_mnt(path);
1084 path->mnt = mounted;
1085 path->dentry = dget(mounted->mnt_root);
1089 static void follow_dotdot(struct nameidata *nd)
1094 struct dentry *old = nd->path.dentry;
1096 if (nd->path.dentry == nd->root.dentry &&
1097 nd->path.mnt == nd->root.mnt) {
1100 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1101 /* rare case of legitimate dget_parent()... */
1102 nd->path.dentry = dget_parent(nd->path.dentry);
1106 if (!follow_up(&nd->path))
1109 follow_mount(&nd->path);
1110 nd->inode = nd->path.dentry->d_inode;
1114 * Allocate a dentry with name and parent, and perform a parent
1115 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1116 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1117 * have verified that no child exists while under i_mutex.
1119 static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1120 struct qstr *name, struct nameidata *nd)
1122 struct inode *inode = parent->d_inode;
1123 struct dentry *dentry;
1126 /* Don't create child dentry for a dead directory. */
1127 if (unlikely(IS_DEADDIR(inode)))
1128 return ERR_PTR(-ENOENT);
1130 dentry = d_alloc(parent, name);
1131 if (unlikely(!dentry))
1132 return ERR_PTR(-ENOMEM);
1134 old = inode->i_op->lookup(inode, dentry, nd);
1135 if (unlikely(old)) {
1143 * It's more convoluted than I'd like it to be, but... it's still fairly
1144 * small and for now I'd prefer to have fast path as straight as possible.
1145 * It _is_ time-critical.
1147 static int do_lookup(struct nameidata *nd, struct qstr *name,
1148 struct path *path, struct inode **inode)
1150 struct vfsmount *mnt = nd->path.mnt;
1151 struct dentry *dentry, *parent = nd->path.dentry;
1157 * Rename seqlock is not required here because in the off chance
1158 * of a false negative due to a concurrent rename, we're going to
1159 * do the non-racy lookup, below.
1161 if (nd->flags & LOOKUP_RCU) {
1164 dentry = __d_lookup_rcu(parent, name, &seq, inode);
1168 /* Memory barrier in read_seqcount_begin of child is enough */
1169 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1173 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1174 status = d_revalidate(dentry, nd);
1175 if (unlikely(status <= 0)) {
1176 if (status != -ECHILD)
1182 path->dentry = dentry;
1183 if (likely(__follow_mount_rcu(nd, path, inode, false)))
1186 if (unlazy_walk(nd, dentry))
1189 dentry = __d_lookup(parent, name);
1193 if (unlikely(!dentry)) {
1194 struct inode *dir = parent->d_inode;
1195 BUG_ON(nd->inode != dir);
1197 mutex_lock(&dir->i_mutex);
1198 dentry = d_lookup(parent, name);
1199 if (likely(!dentry)) {
1200 dentry = d_alloc_and_lookup(parent, name, nd);
1201 if (IS_ERR(dentry)) {
1202 mutex_unlock(&dir->i_mutex);
1203 return PTR_ERR(dentry);
1209 mutex_unlock(&dir->i_mutex);
1211 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1212 status = d_revalidate(dentry, nd);
1213 if (unlikely(status <= 0)) {
1218 if (!d_invalidate(dentry)) {
1227 path->dentry = dentry;
1228 err = follow_managed(path, nd->flags);
1229 if (unlikely(err < 0)) {
1230 path_put_conditional(path, nd);
1233 *inode = path->dentry->d_inode;
1237 static inline int may_lookup(struct nameidata *nd)
1239 if (nd->flags & LOOKUP_RCU) {
1240 int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
1243 if (unlazy_walk(nd, NULL))
1246 return exec_permission(nd->inode, 0);
1249 static inline int handle_dots(struct nameidata *nd, int type)
1251 if (type == LAST_DOTDOT) {
1252 if (nd->flags & LOOKUP_RCU) {
1253 if (follow_dotdot_rcu(nd))
1261 static void terminate_walk(struct nameidata *nd)
1263 if (!(nd->flags & LOOKUP_RCU)) {
1264 path_put(&nd->path);
1266 nd->flags &= ~LOOKUP_RCU;
1267 if (!(nd->flags & LOOKUP_ROOT))
1268 nd->root.mnt = NULL;
1270 br_read_unlock(vfsmount_lock);
1274 static inline int walk_component(struct nameidata *nd, struct path *path,
1275 struct qstr *name, int type, int follow)
1277 struct inode *inode;
1280 * "." and ".." are special - ".." especially so because it has
1281 * to be able to know about the current root directory and
1282 * parent relationships.
1284 if (unlikely(type != LAST_NORM))
1285 return handle_dots(nd, type);
1286 err = do_lookup(nd, name, path, &inode);
1287 if (unlikely(err)) {
1292 path_to_nameidata(path, nd);
1296 if (unlikely(inode->i_op->follow_link) && follow) {
1297 if (nd->flags & LOOKUP_RCU) {
1298 if (unlikely(unlazy_walk(nd, path->dentry))) {
1303 BUG_ON(inode != path->dentry->d_inode);
1306 path_to_nameidata(path, nd);
1312 * This limits recursive symlink follows to 8, while
1313 * limiting consecutive symlinks to 40.
1315 * Without that kind of total limit, nasty chains of consecutive
1316 * symlinks can cause almost arbitrarily long lookups.
1318 static inline int nested_symlink(struct path *path, struct nameidata *nd)
1322 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1323 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1324 path_put_conditional(path, nd);
1325 path_put(&nd->path);
1330 current->link_count++;
1333 struct path link = *path;
1336 res = follow_link(&link, nd, &cookie);
1338 res = walk_component(nd, path, &nd->last,
1339 nd->last_type, LOOKUP_FOLLOW);
1340 put_link(nd, &link, cookie);
1343 current->link_count--;
1350 * This is the basic name resolution function, turning a pathname into
1351 * the final dentry. We expect 'base' to be positive and a directory.
1353 * Returns 0 and nd will have valid dentry and mnt on success.
1354 * Returns error and drops reference to input namei data on failure.
1356 static int link_path_walk(const char *name, struct nameidata *nd)
1360 unsigned int lookup_flags = nd->flags;
1367 /* At this point we know we have a real path component. */
1374 nd->flags |= LOOKUP_CONTINUE;
1376 err = may_lookup(nd);
1381 c = *(const unsigned char *)name;
1383 hash = init_name_hash();
1386 hash = partial_name_hash(c, hash);
1387 c = *(const unsigned char *)name;
1388 } while (c && (c != '/'));
1389 this.len = name - (const char *) this.name;
1390 this.hash = end_name_hash(hash);
1393 if (this.name[0] == '.') switch (this.len) {
1395 if (this.name[1] == '.') {
1397 nd->flags |= LOOKUP_JUMPED;
1403 if (likely(type == LAST_NORM)) {
1404 struct dentry *parent = nd->path.dentry;
1405 nd->flags &= ~LOOKUP_JUMPED;
1406 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1407 err = parent->d_op->d_hash(parent, nd->inode,
1414 /* remove trailing slashes? */
1416 goto last_component;
1417 while (*++name == '/');
1419 goto last_component;
1421 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1426 err = nested_symlink(&next, nd);
1431 if (!nd->inode->i_op->lookup)
1434 /* here ends the main loop */
1437 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1438 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
1440 nd->last_type = type;
1447 static int path_init(int dfd, const char *name, unsigned int flags,
1448 struct nameidata *nd, struct file **fp)
1454 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1455 nd->flags = flags | LOOKUP_JUMPED;
1457 if (flags & LOOKUP_ROOT) {
1458 struct inode *inode = nd->root.dentry->d_inode;
1460 if (!inode->i_op->lookup)
1462 retval = inode_permission(inode, MAY_EXEC);
1466 nd->path = nd->root;
1468 if (flags & LOOKUP_RCU) {
1469 br_read_lock(vfsmount_lock);
1471 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1473 path_get(&nd->path);
1478 nd->root.mnt = NULL;
1481 if (flags & LOOKUP_RCU) {
1482 br_read_lock(vfsmount_lock);
1487 path_get(&nd->root);
1489 nd->path = nd->root;
1490 } else if (dfd == AT_FDCWD) {
1491 if (flags & LOOKUP_RCU) {
1492 struct fs_struct *fs = current->fs;
1495 br_read_lock(vfsmount_lock);
1499 seq = read_seqcount_begin(&fs->seq);
1501 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1502 } while (read_seqcount_retry(&fs->seq, seq));
1504 get_fs_pwd(current->fs, &nd->path);
1507 struct dentry *dentry;
1509 file = fget_raw_light(dfd, &fput_needed);
1514 dentry = file->f_path.dentry;
1518 if (!S_ISDIR(dentry->d_inode->i_mode))
1521 retval = file_permission(file, MAY_EXEC);
1526 nd->path = file->f_path;
1527 if (flags & LOOKUP_RCU) {
1530 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1531 br_read_lock(vfsmount_lock);
1534 path_get(&file->f_path);
1535 fput_light(file, fput_needed);
1539 nd->inode = nd->path.dentry->d_inode;
1543 fput_light(file, fput_needed);
1548 static inline int lookup_last(struct nameidata *nd, struct path *path)
1550 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1551 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1553 nd->flags &= ~LOOKUP_PARENT;
1554 return walk_component(nd, path, &nd->last, nd->last_type,
1555 nd->flags & LOOKUP_FOLLOW);
1558 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1559 static int path_lookupat(int dfd, const char *name,
1560 unsigned int flags, struct nameidata *nd)
1562 struct file *base = NULL;
1567 * Path walking is largely split up into 2 different synchronisation
1568 * schemes, rcu-walk and ref-walk (explained in
1569 * Documentation/filesystems/path-lookup.txt). These share much of the
1570 * path walk code, but some things particularly setup, cleanup, and
1571 * following mounts are sufficiently divergent that functions are
1572 * duplicated. Typically there is a function foo(), and its RCU
1573 * analogue, foo_rcu().
1575 * -ECHILD is the error number of choice (just to avoid clashes) that
1576 * is returned if some aspect of an rcu-walk fails. Such an error must
1577 * be handled by restarting a traditional ref-walk (which will always
1578 * be able to complete).
1580 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1585 current->total_link_count = 0;
1586 err = link_path_walk(name, nd);
1588 if (!err && !(flags & LOOKUP_PARENT)) {
1589 err = lookup_last(nd, &path);
1592 struct path link = path;
1593 nd->flags |= LOOKUP_PARENT;
1594 err = follow_link(&link, nd, &cookie);
1596 err = lookup_last(nd, &path);
1597 put_link(nd, &link, cookie);
1601 if (nd->flags & LOOKUP_RCU) {
1602 /* went all way through without dropping RCU */
1604 if (nameidata_drop_rcu_last(nd))
1609 err = handle_reval_path(nd);
1611 path_put(&nd->path);
1614 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1615 if (!nd->inode->i_op->lookup) {
1616 path_put(&nd->path);
1624 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
1625 path_put(&nd->root);
1626 nd->root.mnt = NULL;
1631 static int do_path_lookup(int dfd, const char *name,
1632 unsigned int flags, struct nameidata *nd)
1634 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1635 if (unlikely(retval == -ECHILD))
1636 retval = path_lookupat(dfd, name, flags, nd);
1637 if (unlikely(retval == -ESTALE))
1638 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1640 if (likely(!retval)) {
1641 if (unlikely(!audit_dummy_context())) {
1642 if (nd->path.dentry && nd->inode)
1643 audit_inode(name, nd->path.dentry);
1649 int kern_path_parent(const char *name, struct nameidata *nd)
1651 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
1654 int kern_path(const char *name, unsigned int flags, struct path *path)
1656 struct nameidata nd;
1657 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1664 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1665 * @dentry: pointer to dentry of the base directory
1666 * @mnt: pointer to vfs mount of the base directory
1667 * @name: pointer to file name
1668 * @flags: lookup flags
1669 * @nd: pointer to nameidata
1671 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1672 const char *name, unsigned int flags,
1673 struct nameidata *nd)
1675 nd->root.dentry = dentry;
1677 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1678 return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
1681 static struct dentry *__lookup_hash(struct qstr *name,
1682 struct dentry *base, struct nameidata *nd)
1684 struct inode *inode = base->d_inode;
1685 struct dentry *dentry;
1688 err = exec_permission(inode, 0);
1690 return ERR_PTR(err);
1693 * Don't bother with __d_lookup: callers are for creat as
1694 * well as unlink, so a lot of the time it would cost
1697 dentry = d_lookup(base, name);
1699 if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
1700 dentry = do_revalidate(dentry, nd);
1703 dentry = d_alloc_and_lookup(base, name, nd);
1709 * Restricted form of lookup. Doesn't follow links, single-component only,
1710 * needs parent already locked. Doesn't follow mounts.
1713 static struct dentry *lookup_hash(struct nameidata *nd)
1715 return __lookup_hash(&nd->last, nd->path.dentry, nd);
1719 * lookup_one_len - filesystem helper to lookup single pathname component
1720 * @name: pathname component to lookup
1721 * @base: base directory to lookup from
1722 * @len: maximum length @len should be interpreted to
1724 * Note that this routine is purely a helper for filesystem usage and should
1725 * not be called by generic code. Also note that by using this function the
1726 * nameidata argument is passed to the filesystem methods and a filesystem
1727 * using this helper needs to be prepared for that.
1729 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1735 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1740 return ERR_PTR(-EACCES);
1742 hash = init_name_hash();
1744 c = *(const unsigned char *)name++;
1745 if (c == '/' || c == '\0')
1746 return ERR_PTR(-EACCES);
1747 hash = partial_name_hash(c, hash);
1749 this.hash = end_name_hash(hash);
1751 * See if the low-level filesystem might want
1752 * to use its own hash..
1754 if (base->d_flags & DCACHE_OP_HASH) {
1755 int err = base->d_op->d_hash(base, base->d_inode, &this);
1757 return ERR_PTR(err);
1760 return __lookup_hash(&this, base, NULL);
1763 int user_path_at(int dfd, const char __user *name, unsigned flags,
1766 struct nameidata nd;
1767 char *tmp = getname_flags(name, flags);
1768 int err = PTR_ERR(tmp);
1771 BUG_ON(flags & LOOKUP_PARENT);
1773 err = do_path_lookup(dfd, tmp, flags, &nd);
1781 static int user_path_parent(int dfd, const char __user *path,
1782 struct nameidata *nd, char **name)
1784 char *s = getname(path);
1790 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1800 * It's inline, so penalty for filesystems that don't use sticky bit is
1803 static inline int check_sticky(struct inode *dir, struct inode *inode)
1805 uid_t fsuid = current_fsuid();
1807 if (!(dir->i_mode & S_ISVTX))
1809 if (current_user_ns() != inode_userns(inode))
1811 if (inode->i_uid == fsuid)
1813 if (dir->i_uid == fsuid)
1817 return !ns_capable(inode_userns(inode), CAP_FOWNER);
1821 * Check whether we can remove a link victim from directory dir, check
1822 * whether the type of victim is right.
1823 * 1. We can't do it if dir is read-only (done in permission())
1824 * 2. We should have write and exec permissions on dir
1825 * 3. We can't remove anything from append-only dir
1826 * 4. We can't do anything with immutable dir (done in permission())
1827 * 5. If the sticky bit on dir is set we should either
1828 * a. be owner of dir, or
1829 * b. be owner of victim, or
1830 * c. have CAP_FOWNER capability
1831 * 6. If the victim is append-only or immutable we can't do antyhing with
1832 * links pointing to it.
1833 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1834 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1835 * 9. We can't remove a root or mountpoint.
1836 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1837 * nfs_async_unlink().
1839 static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1843 if (!victim->d_inode)
1846 BUG_ON(victim->d_parent->d_inode != dir);
1847 audit_inode_child(victim, dir);
1849 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1854 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1855 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
1858 if (!S_ISDIR(victim->d_inode->i_mode))
1860 if (IS_ROOT(victim))
1862 } else if (S_ISDIR(victim->d_inode->i_mode))
1864 if (IS_DEADDIR(dir))
1866 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1871 /* Check whether we can create an object with dentry child in directory
1873 * 1. We can't do it if child already exists (open has special treatment for
1874 * this case, but since we are inlined it's OK)
1875 * 2. We can't do it if dir is read-only (done in permission())
1876 * 3. We should have write and exec permissions on dir
1877 * 4. We can't do it if dir is immutable (done in permission())
1879 static inline int may_create(struct inode *dir, struct dentry *child)
1883 if (IS_DEADDIR(dir))
1885 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
1889 * p1 and p2 should be directories on the same fs.
1891 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1896 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1900 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1902 p = d_ancestor(p2, p1);
1904 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1905 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1909 p = d_ancestor(p1, p2);
1911 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1912 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1916 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1917 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1921 void unlock_rename(struct dentry *p1, struct dentry *p2)
1923 mutex_unlock(&p1->d_inode->i_mutex);
1925 mutex_unlock(&p2->d_inode->i_mutex);
1926 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1930 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1931 struct nameidata *nd)
1933 int error = may_create(dir, dentry);
1938 if (!dir->i_op->create)
1939 return -EACCES; /* shouldn't it be ENOSYS? */
1942 error = security_inode_create(dir, dentry, mode);
1945 error = dir->i_op->create(dir, dentry, mode, nd);
1947 fsnotify_create(dir, dentry);
1951 static int may_open(struct path *path, int acc_mode, int flag)
1953 struct dentry *dentry = path->dentry;
1954 struct inode *inode = dentry->d_inode;
1964 switch (inode->i_mode & S_IFMT) {
1968 if (acc_mode & MAY_WRITE)
1973 if (path->mnt->mnt_flags & MNT_NODEV)
1982 error = inode_permission(inode, acc_mode);
1987 * An append-only file must be opened in append mode for writing.
1989 if (IS_APPEND(inode)) {
1990 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
1996 /* O_NOATIME can only be set by the owner or superuser */
1997 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
2001 * Ensure there are no outstanding leases on the file.
2003 return break_lease(inode, flag);
2006 static int handle_truncate(struct file *filp)
2008 struct path *path = &filp->f_path;
2009 struct inode *inode = path->dentry->d_inode;
2010 int error = get_write_access(inode);
2014 * Refuse to truncate files with mandatory locks held on them.
2016 error = locks_verify_locked(inode);
2018 error = security_path_truncate(path);
2020 error = do_truncate(path->dentry, 0,
2021 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2024 put_write_access(inode);
2029 * Note that while the flag value (low two bits) for sys_open means:
2034 * it is changed into
2035 * 00 - no permissions needed
2036 * 01 - read-permission
2037 * 10 - write-permission
2039 * for the internal routines (ie open_namei()/follow_link() etc)
2040 * This is more logical, and also allows the 00 "no perm needed"
2041 * to be used for symlinks (where the permissions are checked
2045 static inline int open_to_namei_flags(int flag)
2047 if ((flag+1) & O_ACCMODE)
2053 * Handle the last step of open()
2055 static struct file *do_last(struct nameidata *nd, struct path *path,
2056 const struct open_flags *op, const char *pathname)
2058 struct dentry *dir = nd->path.dentry;
2059 struct dentry *dentry;
2060 int open_flag = op->open_flag;
2061 int will_truncate = open_flag & O_TRUNC;
2063 int acc_mode = op->acc_mode;
2067 nd->flags &= ~LOOKUP_PARENT;
2068 nd->flags |= op->intent;
2070 switch (nd->last_type) {
2073 error = handle_dots(nd, nd->last_type);
2075 return ERR_PTR(error);
2078 if (nd->flags & LOOKUP_RCU) {
2079 if (nameidata_drop_rcu_last(nd))
2080 return ERR_PTR(-ECHILD);
2082 error = handle_reval_path(nd);
2085 audit_inode(pathname, nd->path.dentry);
2086 if (open_flag & O_CREAT) {
2092 /* can't be RCU mode here */
2093 error = handle_reval_path(nd);
2096 audit_inode(pathname, dir);
2100 if (!(open_flag & O_CREAT)) {
2102 if (nd->last.name[nd->last.len])
2103 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2104 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2106 /* we _can_ be in RCU mode here */
2107 error = walk_component(nd, path, &nd->last, LAST_NORM,
2110 return ERR_PTR(error);
2111 if (error) /* symlink */
2114 if (nd->flags & LOOKUP_RCU) {
2115 if (nameidata_drop_rcu_last(nd))
2116 return ERR_PTR(-ECHILD);
2120 if (nd->flags & LOOKUP_DIRECTORY) {
2121 if (!nd->inode->i_op->lookup)
2124 audit_inode(pathname, nd->path.dentry);
2128 /* create side of things */
2130 if (nd->flags & LOOKUP_RCU) {
2131 if (nameidata_drop_rcu_last(nd))
2132 return ERR_PTR(-ECHILD);
2135 audit_inode(pathname, dir);
2137 /* trailing slashes? */
2138 if (nd->last.name[nd->last.len])
2141 mutex_lock(&dir->d_inode->i_mutex);
2143 dentry = lookup_hash(nd);
2144 error = PTR_ERR(dentry);
2145 if (IS_ERR(dentry)) {
2146 mutex_unlock(&dir->d_inode->i_mutex);
2150 path->dentry = dentry;
2151 path->mnt = nd->path.mnt;
2153 /* Negative dentry, just create the file */
2154 if (!dentry->d_inode) {
2155 int mode = op->mode;
2156 if (!IS_POSIXACL(dir->d_inode))
2157 mode &= ~current_umask();
2159 * This write is needed to ensure that a
2160 * rw->ro transition does not occur between
2161 * the time when the file is created and when
2162 * a permanent write count is taken through
2163 * the 'struct file' in nameidata_to_filp().
2165 error = mnt_want_write(nd->path.mnt);
2167 goto exit_mutex_unlock;
2169 /* Don't check for write permission, don't truncate */
2170 open_flag &= ~O_TRUNC;
2172 acc_mode = MAY_OPEN;
2173 error = security_path_mknod(&nd->path, dentry, mode, 0);
2175 goto exit_mutex_unlock;
2176 error = vfs_create(dir->d_inode, dentry, mode, nd);
2178 goto exit_mutex_unlock;
2179 mutex_unlock(&dir->d_inode->i_mutex);
2180 dput(nd->path.dentry);
2181 nd->path.dentry = dentry;
2186 * It already exists.
2188 mutex_unlock(&dir->d_inode->i_mutex);
2189 audit_inode(pathname, path->dentry);
2192 if (open_flag & O_EXCL)
2195 error = follow_managed(path, nd->flags);
2200 if (!path->dentry->d_inode)
2203 if (path->dentry->d_inode->i_op->follow_link)
2206 path_to_nameidata(path, nd);
2207 nd->inode = path->dentry->d_inode;
2209 if (S_ISDIR(nd->inode->i_mode))
2212 if (!S_ISREG(nd->inode->i_mode))
2215 if (will_truncate) {
2216 error = mnt_want_write(nd->path.mnt);
2222 error = may_open(&nd->path, acc_mode, open_flag);
2225 filp = nameidata_to_filp(nd);
2226 if (!IS_ERR(filp)) {
2227 error = ima_file_check(filp, op->acc_mode);
2230 filp = ERR_PTR(error);
2233 if (!IS_ERR(filp)) {
2234 if (will_truncate) {
2235 error = handle_truncate(filp);
2238 filp = ERR_PTR(error);
2244 mnt_drop_write(nd->path.mnt);
2245 path_put(&nd->path);
2249 mutex_unlock(&dir->d_inode->i_mutex);
2251 path_put_conditional(path, nd);
2253 filp = ERR_PTR(error);
2257 static struct file *path_openat(int dfd, const char *pathname,
2258 struct nameidata *nd, const struct open_flags *op, int flags)
2260 struct file *base = NULL;
2265 filp = get_empty_filp();
2267 return ERR_PTR(-ENFILE);
2269 filp->f_flags = op->open_flag;
2270 nd->intent.open.file = filp;
2271 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2272 nd->intent.open.create_mode = op->mode;
2274 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
2275 if (unlikely(error))
2278 current->total_link_count = 0;
2279 error = link_path_walk(pathname, nd);
2280 if (unlikely(error))
2283 filp = do_last(nd, &path, op, pathname);
2284 while (unlikely(!filp)) { /* trailing symlink */
2285 struct path link = path;
2287 if (!(nd->flags & LOOKUP_FOLLOW)) {
2288 path_put_conditional(&path, nd);
2289 path_put(&nd->path);
2290 filp = ERR_PTR(-ELOOP);
2293 nd->flags |= LOOKUP_PARENT;
2294 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2295 error = follow_link(&link, nd, &cookie);
2296 if (unlikely(error))
2297 filp = ERR_PTR(error);
2299 filp = do_last(nd, &path, op, pathname);
2300 put_link(nd, &link, cookie);
2303 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2304 path_put(&nd->root);
2307 release_open_intent(nd);
2311 filp = ERR_PTR(error);
2315 struct file *do_filp_open(int dfd, const char *pathname,
2316 const struct open_flags *op, int flags)
2318 struct nameidata nd;
2321 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
2322 if (unlikely(filp == ERR_PTR(-ECHILD)))
2323 filp = path_openat(dfd, pathname, &nd, op, flags);
2324 if (unlikely(filp == ERR_PTR(-ESTALE)))
2325 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
2329 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2330 const char *name, const struct open_flags *op, int flags)
2332 struct nameidata nd;
2336 nd.root.dentry = dentry;
2338 flags |= LOOKUP_ROOT;
2340 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
2341 return ERR_PTR(-ELOOP);
2343 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2344 if (unlikely(file == ERR_PTR(-ECHILD)))
2345 file = path_openat(-1, name, &nd, op, flags);
2346 if (unlikely(file == ERR_PTR(-ESTALE)))
2347 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2352 * lookup_create - lookup a dentry, creating it if it doesn't exist
2353 * @nd: nameidata info
2354 * @is_dir: directory flag
2356 * Simple function to lookup and return a dentry and create it
2357 * if it doesn't exist. Is SMP-safe.
2359 * Returns with nd->path.dentry->d_inode->i_mutex locked.
2361 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
2363 struct dentry *dentry = ERR_PTR(-EEXIST);
2365 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2367 * Yucky last component or no last component at all?
2368 * (foo/., foo/.., /////)
2370 if (nd->last_type != LAST_NORM)
2372 nd->flags &= ~LOOKUP_PARENT;
2373 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2374 nd->intent.open.flags = O_EXCL;
2377 * Do the final lookup.
2379 dentry = lookup_hash(nd);
2383 if (dentry->d_inode)
2386 * Special case - lookup gave negative, but... we had foo/bar/
2387 * From the vfs_mknod() POV we just have a negative dentry -
2388 * all is fine. Let's be bastards - you had / on the end, you've
2389 * been asking for (non-existent) directory. -ENOENT for you.
2391 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
2393 dentry = ERR_PTR(-ENOENT);
2398 dentry = ERR_PTR(-EEXIST);
2402 EXPORT_SYMBOL_GPL(lookup_create);
2404 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2406 int error = may_create(dir, dentry);
2411 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2412 !ns_capable(inode_userns(dir), CAP_MKNOD))
2415 if (!dir->i_op->mknod)
2418 error = devcgroup_inode_mknod(mode, dev);
2422 error = security_inode_mknod(dir, dentry, mode, dev);
2426 error = dir->i_op->mknod(dir, dentry, mode, dev);
2428 fsnotify_create(dir, dentry);
2432 static int may_mknod(mode_t mode)
2434 switch (mode & S_IFMT) {
2440 case 0: /* zero mode translates to S_IFREG */
2449 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2454 struct dentry *dentry;
2455 struct nameidata nd;
2460 error = user_path_parent(dfd, filename, &nd, &tmp);
2464 dentry = lookup_create(&nd, 0);
2465 if (IS_ERR(dentry)) {
2466 error = PTR_ERR(dentry);
2469 if (!IS_POSIXACL(nd.path.dentry->d_inode))
2470 mode &= ~current_umask();
2471 error = may_mknod(mode);
2474 error = mnt_want_write(nd.path.mnt);
2477 error = security_path_mknod(&nd.path, dentry, mode, dev);
2479 goto out_drop_write;
2480 switch (mode & S_IFMT) {
2481 case 0: case S_IFREG:
2482 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
2484 case S_IFCHR: case S_IFBLK:
2485 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
2486 new_decode_dev(dev));
2488 case S_IFIFO: case S_IFSOCK:
2489 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
2493 mnt_drop_write(nd.path.mnt);
2497 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2504 SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
2506 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2509 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2511 int error = may_create(dir, dentry);
2516 if (!dir->i_op->mkdir)
2519 mode &= (S_IRWXUGO|S_ISVTX);
2520 error = security_inode_mkdir(dir, dentry, mode);
2524 error = dir->i_op->mkdir(dir, dentry, mode);
2526 fsnotify_mkdir(dir, dentry);
2530 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
2534 struct dentry *dentry;
2535 struct nameidata nd;
2537 error = user_path_parent(dfd, pathname, &nd, &tmp);
2541 dentry = lookup_create(&nd, 1);
2542 error = PTR_ERR(dentry);
2546 if (!IS_POSIXACL(nd.path.dentry->d_inode))
2547 mode &= ~current_umask();
2548 error = mnt_want_write(nd.path.mnt);
2551 error = security_path_mkdir(&nd.path, dentry, mode);
2553 goto out_drop_write;
2554 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2556 mnt_drop_write(nd.path.mnt);
2560 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2567 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
2569 return sys_mkdirat(AT_FDCWD, pathname, mode);
2573 * We try to drop the dentry early: we should have
2574 * a usage count of 2 if we're the only user of this
2575 * dentry, and if that is true (possibly after pruning
2576 * the dcache), then we drop the dentry now.
2578 * A low-level filesystem can, if it choses, legally
2581 * if (!d_unhashed(dentry))
2584 * if it cannot handle the case of removing a directory
2585 * that is still in use by something else..
2587 void dentry_unhash(struct dentry *dentry)
2590 shrink_dcache_parent(dentry);
2591 spin_lock(&dentry->d_lock);
2592 if (dentry->d_count == 2)
2594 spin_unlock(&dentry->d_lock);
2597 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2599 int error = may_delete(dir, dentry, 1);
2604 if (!dir->i_op->rmdir)
2607 mutex_lock(&dentry->d_inode->i_mutex);
2608 dentry_unhash(dentry);
2609 if (d_mountpoint(dentry))
2612 error = security_inode_rmdir(dir, dentry);
2614 error = dir->i_op->rmdir(dir, dentry);
2616 dentry->d_inode->i_flags |= S_DEAD;
2621 mutex_unlock(&dentry->d_inode->i_mutex);
2630 static long do_rmdir(int dfd, const char __user *pathname)
2634 struct dentry *dentry;
2635 struct nameidata nd;
2637 error = user_path_parent(dfd, pathname, &nd, &name);
2641 switch(nd.last_type) {
2653 nd.flags &= ~LOOKUP_PARENT;
2655 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2656 dentry = lookup_hash(&nd);
2657 error = PTR_ERR(dentry);
2660 error = mnt_want_write(nd.path.mnt);
2663 error = security_path_rmdir(&nd.path, dentry);
2666 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2668 mnt_drop_write(nd.path.mnt);
2672 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2679 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
2681 return do_rmdir(AT_FDCWD, pathname);
2684 int vfs_unlink(struct inode *dir, struct dentry *dentry)
2686 int error = may_delete(dir, dentry, 0);
2691 if (!dir->i_op->unlink)
2694 mutex_lock(&dentry->d_inode->i_mutex);
2695 if (d_mountpoint(dentry))
2698 error = security_inode_unlink(dir, dentry);
2700 error = dir->i_op->unlink(dir, dentry);
2705 mutex_unlock(&dentry->d_inode->i_mutex);
2707 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2708 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2709 fsnotify_link_count(dentry->d_inode);
2717 * Make sure that the actual truncation of the file will occur outside its
2718 * directory's i_mutex. Truncate can take a long time if there is a lot of
2719 * writeout happening, and we don't want to prevent access to the directory
2720 * while waiting on the I/O.
2722 static long do_unlinkat(int dfd, const char __user *pathname)
2726 struct dentry *dentry;
2727 struct nameidata nd;
2728 struct inode *inode = NULL;
2730 error = user_path_parent(dfd, pathname, &nd, &name);
2735 if (nd.last_type != LAST_NORM)
2738 nd.flags &= ~LOOKUP_PARENT;
2740 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2741 dentry = lookup_hash(&nd);
2742 error = PTR_ERR(dentry);
2743 if (!IS_ERR(dentry)) {
2744 /* Why not before? Because we want correct error value */
2745 if (nd.last.name[nd.last.len])
2747 inode = dentry->d_inode;
2750 error = mnt_want_write(nd.path.mnt);
2753 error = security_path_unlink(&nd.path, dentry);
2756 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2758 mnt_drop_write(nd.path.mnt);
2762 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2764 iput(inode); /* truncate the inode here */
2771 error = !dentry->d_inode ? -ENOENT :
2772 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2776 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
2778 if ((flag & ~AT_REMOVEDIR) != 0)
2781 if (flag & AT_REMOVEDIR)
2782 return do_rmdir(dfd, pathname);
2784 return do_unlinkat(dfd, pathname);
2787 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
2789 return do_unlinkat(AT_FDCWD, pathname);
2792 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
2794 int error = may_create(dir, dentry);
2799 if (!dir->i_op->symlink)
2802 error = security_inode_symlink(dir, dentry, oldname);
2806 error = dir->i_op->symlink(dir, dentry, oldname);
2808 fsnotify_create(dir, dentry);
2812 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2813 int, newdfd, const char __user *, newname)
2818 struct dentry *dentry;
2819 struct nameidata nd;
2821 from = getname(oldname);
2823 return PTR_ERR(from);
2825 error = user_path_parent(newdfd, newname, &nd, &to);
2829 dentry = lookup_create(&nd, 0);
2830 error = PTR_ERR(dentry);
2834 error = mnt_want_write(nd.path.mnt);
2837 error = security_path_symlink(&nd.path, dentry, from);
2839 goto out_drop_write;
2840 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2842 mnt_drop_write(nd.path.mnt);
2846 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2854 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
2856 return sys_symlinkat(oldname, AT_FDCWD, newname);
2859 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2861 struct inode *inode = old_dentry->d_inode;
2867 error = may_create(dir, new_dentry);
2871 if (dir->i_sb != inode->i_sb)
2875 * A link to an append-only or immutable file cannot be created.
2877 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2879 if (!dir->i_op->link)
2881 if (S_ISDIR(inode->i_mode))
2884 error = security_inode_link(old_dentry, dir, new_dentry);
2888 mutex_lock(&inode->i_mutex);
2889 /* Make sure we don't allow creating hardlink to an unlinked file */
2890 if (inode->i_nlink == 0)
2893 error = dir->i_op->link(old_dentry, dir, new_dentry);
2894 mutex_unlock(&inode->i_mutex);
2896 fsnotify_link(dir, inode, new_dentry);
2901 * Hardlinks are often used in delicate situations. We avoid
2902 * security-related surprises by not following symlinks on the
2905 * We don't follow them on the oldname either to be compatible
2906 * with linux 2.0, and to avoid hard-linking to directories
2907 * and other special files. --ADM
2909 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2910 int, newdfd, const char __user *, newname, int, flags)
2912 struct dentry *new_dentry;
2913 struct nameidata nd;
2914 struct path old_path;
2919 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
2922 * To use null names we require CAP_DAC_READ_SEARCH
2923 * This ensures that not everyone will be able to create
2924 * handlink using the passed filedescriptor.
2926 if (flags & AT_EMPTY_PATH) {
2927 if (!capable(CAP_DAC_READ_SEARCH))
2932 if (flags & AT_SYMLINK_FOLLOW)
2933 how |= LOOKUP_FOLLOW;
2935 error = user_path_at(olddfd, oldname, how, &old_path);
2939 error = user_path_parent(newdfd, newname, &nd, &to);
2943 if (old_path.mnt != nd.path.mnt)
2945 new_dentry = lookup_create(&nd, 0);
2946 error = PTR_ERR(new_dentry);
2947 if (IS_ERR(new_dentry))
2949 error = mnt_want_write(nd.path.mnt);
2952 error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2954 goto out_drop_write;
2955 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
2957 mnt_drop_write(nd.path.mnt);
2961 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2966 path_put(&old_path);
2971 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
2973 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
2977 * The worst of all namespace operations - renaming directory. "Perverted"
2978 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2980 * a) we can get into loop creation. Check is done in is_subdir().
2981 * b) race potential - two innocent renames can create a loop together.
2982 * That's where 4.4 screws up. Current fix: serialization on
2983 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
2985 * c) we have to lock _three_ objects - parents and victim (if it exists).
2986 * And that - after we got ->i_mutex on parents (until then we don't know
2987 * whether the target exists). Solution: try to be smart with locking
2988 * order for inodes. We rely on the fact that tree topology may change
2989 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
2990 * move will be locked. Thus we can rank directories by the tree
2991 * (ancestors first) and rank all non-directories after them.
2992 * That works since everybody except rename does "lock parent, lookup,
2993 * lock child" and rename is under ->s_vfs_rename_mutex.
2994 * HOWEVER, it relies on the assumption that any object with ->lookup()
2995 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2996 * we'd better make sure that there's no link(2) for them.
2997 * d) some filesystems don't support opened-but-unlinked directories,
2998 * either because of layout or because they are not ready to deal with
2999 * all cases correctly. The latter will be fixed (taking this sort of
3000 * stuff into VFS), but the former is not going away. Solution: the same
3001 * trick as in rmdir().
3002 * e) conversion from fhandle to dentry may come in the wrong moment - when
3003 * we are removing the target. Solution: we will have to grab ->i_mutex
3004 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3005 * ->i_mutex on parents, which works but leads to some truly excessive
3008 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3009 struct inode *new_dir, struct dentry *new_dentry)
3012 struct inode *target;
3015 * If we are going to change the parent - check write permissions,
3016 * we'll need to flip '..'.
3018 if (new_dir != old_dir) {
3019 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
3024 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3028 target = new_dentry->d_inode;
3030 mutex_lock(&target->i_mutex);
3031 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3035 dentry_unhash(new_dentry);
3036 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3040 target->i_flags |= S_DEAD;
3041 dont_mount(new_dentry);
3043 mutex_unlock(&target->i_mutex);
3044 if (d_unhashed(new_dentry))
3045 d_rehash(new_dentry);
3049 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3050 d_move(old_dentry,new_dentry);
3054 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3055 struct inode *new_dir, struct dentry *new_dentry)
3057 struct inode *target;
3060 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3065 target = new_dentry->d_inode;
3067 mutex_lock(&target->i_mutex);
3068 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3071 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3074 dont_mount(new_dentry);
3075 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3076 d_move(old_dentry, new_dentry);
3079 mutex_unlock(&target->i_mutex);
3084 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3085 struct inode *new_dir, struct dentry *new_dentry)
3088 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
3089 const unsigned char *old_name;
3091 if (old_dentry->d_inode == new_dentry->d_inode)
3094 error = may_delete(old_dir, old_dentry, is_dir);
3098 if (!new_dentry->d_inode)
3099 error = may_create(new_dir, new_dentry);
3101 error = may_delete(new_dir, new_dentry, is_dir);
3105 if (!old_dir->i_op->rename)
3108 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3111 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3113 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3115 fsnotify_move(old_dir, new_dir, old_name, is_dir,
3116 new_dentry->d_inode, old_dentry);
3117 fsnotify_oldname_free(old_name);
3122 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3123 int, newdfd, const char __user *, newname)
3125 struct dentry *old_dir, *new_dir;
3126 struct dentry *old_dentry, *new_dentry;
3127 struct dentry *trap;
3128 struct nameidata oldnd, newnd;
3133 error = user_path_parent(olddfd, oldname, &oldnd, &from);
3137 error = user_path_parent(newdfd, newname, &newnd, &to);
3142 if (oldnd.path.mnt != newnd.path.mnt)
3145 old_dir = oldnd.path.dentry;
3147 if (oldnd.last_type != LAST_NORM)
3150 new_dir = newnd.path.dentry;
3151 if (newnd.last_type != LAST_NORM)
3154 oldnd.flags &= ~LOOKUP_PARENT;
3155 newnd.flags &= ~LOOKUP_PARENT;
3156 newnd.flags |= LOOKUP_RENAME_TARGET;
3158 trap = lock_rename(new_dir, old_dir);
3160 old_dentry = lookup_hash(&oldnd);
3161 error = PTR_ERR(old_dentry);
3162 if (IS_ERR(old_dentry))
3164 /* source must exist */
3166 if (!old_dentry->d_inode)
3168 /* unless the source is a directory trailing slashes give -ENOTDIR */
3169 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3171 if (oldnd.last.name[oldnd.last.len])
3173 if (newnd.last.name[newnd.last.len])
3176 /* source should not be ancestor of target */
3178 if (old_dentry == trap)
3180 new_dentry = lookup_hash(&newnd);
3181 error = PTR_ERR(new_dentry);
3182 if (IS_ERR(new_dentry))
3184 /* target should not be an ancestor of source */
3186 if (new_dentry == trap)
3189 error = mnt_want_write(oldnd.path.mnt);
3192 error = security_path_rename(&oldnd.path, old_dentry,
3193 &newnd.path, new_dentry);
3196 error = vfs_rename(old_dir->d_inode, old_dentry,
3197 new_dir->d_inode, new_dentry);
3199 mnt_drop_write(oldnd.path.mnt);
3205 unlock_rename(new_dir, old_dir);
3207 path_put(&newnd.path);
3210 path_put(&oldnd.path);
3216 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
3218 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3221 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3225 len = PTR_ERR(link);
3230 if (len > (unsigned) buflen)
3232 if (copy_to_user(buffer, link, len))
3239 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3240 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3241 * using) it for any given inode is up to filesystem.
3243 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3245 struct nameidata nd;
3250 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3252 return PTR_ERR(cookie);
3254 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3255 if (dentry->d_inode->i_op->put_link)
3256 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3260 int vfs_follow_link(struct nameidata *nd, const char *link)
3262 return __vfs_follow_link(nd, link);
3265 /* get the link contents into pagecache */
3266 static char *page_getlink(struct dentry * dentry, struct page **ppage)
3270 struct address_space *mapping = dentry->d_inode->i_mapping;
3271 page = read_mapping_page(mapping, 0, NULL);
3276 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3280 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3282 struct page *page = NULL;
3283 char *s = page_getlink(dentry, &page);
3284 int res = vfs_readlink(dentry,buffer,buflen,s);
3287 page_cache_release(page);
3292 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
3294 struct page *page = NULL;
3295 nd_set_link(nd, page_getlink(dentry, &page));
3299 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
3301 struct page *page = cookie;
3305 page_cache_release(page);
3310 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3312 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
3314 struct address_space *mapping = inode->i_mapping;
3319 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3321 flags |= AOP_FLAG_NOFS;
3324 err = pagecache_write_begin(NULL, mapping, 0, len-1,
3325 flags, &page, &fsdata);
3329 kaddr = kmap_atomic(page, KM_USER0);
3330 memcpy(kaddr, symname, len-1);
3331 kunmap_atomic(kaddr, KM_USER0);
3333 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3340 mark_inode_dirty(inode);
3346 int page_symlink(struct inode *inode, const char *symname, int len)
3348 return __page_symlink(inode, symname, len,
3349 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
3352 const struct inode_operations page_symlink_inode_operations = {
3353 .readlink = generic_readlink,
3354 .follow_link = page_follow_link_light,
3355 .put_link = page_put_link,
3358 EXPORT_SYMBOL(user_path_at);
3359 EXPORT_SYMBOL(follow_down_one);
3360 EXPORT_SYMBOL(follow_down);
3361 EXPORT_SYMBOL(follow_up);
3362 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3363 EXPORT_SYMBOL(getname);
3364 EXPORT_SYMBOL(lock_rename);
3365 EXPORT_SYMBOL(lookup_one_len);
3366 EXPORT_SYMBOL(page_follow_link_light);
3367 EXPORT_SYMBOL(page_put_link);
3368 EXPORT_SYMBOL(page_readlink);
3369 EXPORT_SYMBOL(__page_symlink);
3370 EXPORT_SYMBOL(page_symlink);
3371 EXPORT_SYMBOL(page_symlink_inode_operations);
3372 EXPORT_SYMBOL(kern_path_parent);
3373 EXPORT_SYMBOL(kern_path);
3374 EXPORT_SYMBOL(vfs_path_lookup);
3375 EXPORT_SYMBOL(inode_permission);
3376 EXPORT_SYMBOL(file_permission);
3377 EXPORT_SYMBOL(unlock_rename);
3378 EXPORT_SYMBOL(vfs_create);
3379 EXPORT_SYMBOL(vfs_follow_link);
3380 EXPORT_SYMBOL(vfs_link);
3381 EXPORT_SYMBOL(vfs_mkdir);
3382 EXPORT_SYMBOL(vfs_mknod);
3383 EXPORT_SYMBOL(generic_permission);
3384 EXPORT_SYMBOL(vfs_readlink);
3385 EXPORT_SYMBOL(vfs_rename);
3386 EXPORT_SYMBOL(vfs_rmdir);
3387 EXPORT_SYMBOL(vfs_symlink);
3388 EXPORT_SYMBOL(vfs_unlink);
3389 EXPORT_SYMBOL(dentry_unhash);
3390 EXPORT_SYMBOL(generic_readlink);