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 <linux/posix_acl.h>
36 #include <asm/uaccess.h>
40 /* [Feb-1997 T. Schoebel-Theuer]
41 * Fundamental changes in the pathname lookup mechanisms (namei)
42 * were necessary because of omirr. The reason is that omirr needs
43 * to know the _real_ pathname, not the user-supplied one, in case
44 * of symlinks (and also when transname replacements occur).
46 * The new code replaces the old recursive symlink resolution with
47 * an iterative one (in case of non-nested symlink chains). It does
48 * this with calls to <fs>_follow_link().
49 * As a side effect, dir_namei(), _namei() and follow_link() are now
50 * replaced with a single function lookup_dentry() that can handle all
51 * the special cases of the former code.
53 * With the new dcache, the pathname is stored at each inode, at least as
54 * long as the refcount of the inode is positive. As a side effect, the
55 * size of the dcache depends on the inode cache and thus is dynamic.
57 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
58 * resolution to correspond with current state of the code.
60 * Note that the symlink resolution is not *completely* iterative.
61 * There is still a significant amount of tail- and mid- recursion in
62 * the algorithm. Also, note that <fs>_readlink() is not used in
63 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
64 * may return different results than <fs>_follow_link(). Many virtual
65 * filesystems (including /proc) exhibit this behavior.
68 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
69 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
70 * and the name already exists in form of a symlink, try to create the new
71 * name indicated by the symlink. The old code always complained that the
72 * name already exists, due to not following the symlink even if its target
73 * is nonexistent. The new semantics affects also mknod() and link() when
74 * the name is a symlink pointing to a non-existent name.
76 * I don't know which semantics is the right one, since I have no access
77 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
78 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
79 * "old" one. Personally, I think the new semantics is much more logical.
80 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
81 * file does succeed in both HP-UX and SunOs, but not in Solaris
82 * and in the old Linux semantics.
85 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
86 * semantics. See the comments in "open_namei" and "do_link" below.
88 * [10-Sep-98 Alan Modra] Another symlink change.
91 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
92 * inside the path - always follow.
93 * in the last component in creation/removal/renaming - never follow.
94 * if LOOKUP_FOLLOW passed - follow.
95 * if the pathname has trailing slashes - follow.
96 * otherwise - don't follow.
97 * (applied in that order).
99 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
100 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
101 * During the 2.4 we need to fix the userland stuff depending on it -
102 * hopefully we will be able to get rid of that wart in 2.5. So far only
103 * XEmacs seems to be relying on it...
106 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
107 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
108 * any extra contention...
111 /* In order to reduce some races, while at the same time doing additional
112 * checking and hopefully speeding things up, we copy filenames to the
113 * kernel data space before using them..
115 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
116 * PATH_MAX includes the nul terminator --RR.
118 static int do_getname(const char __user *filename, char *page)
121 unsigned long len = PATH_MAX;
123 if (!segment_eq(get_fs(), KERNEL_DS)) {
124 if ((unsigned long) filename >= TASK_SIZE)
126 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
127 len = TASK_SIZE - (unsigned long) filename;
130 retval = strncpy_from_user(page, filename, len);
134 return -ENAMETOOLONG;
140 static char *getname_flags(const char __user * filename, int flags)
144 result = ERR_PTR(-ENOMEM);
147 int retval = do_getname(filename, tmp);
151 if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
153 result = ERR_PTR(retval);
157 audit_getname(result);
161 char *getname(const char __user * filename)
163 return getname_flags(filename, 0);
166 #ifdef CONFIG_AUDITSYSCALL
167 void putname(const char *name)
169 if (unlikely(!audit_dummy_context()))
174 EXPORT_SYMBOL(putname);
177 static int check_acl(struct inode *inode, int mask)
179 struct posix_acl *acl;
182 * Under RCU walk, we cannot even do a "get_cached_acl()",
183 * because that involves locking and getting a refcount on
186 * So the only case we handle during RCU walking is the
187 * case of a cached "no ACL at all", which needs no locks
190 if (mask & MAY_NOT_BLOCK) {
191 if (negative_cached_acl(inode, ACL_TYPE_ACCESS))
196 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
199 * A filesystem can force a ACL callback by just never filling the
200 * ACL cache. But normally you'd fill the cache either at inode
201 * instantiation time, or on the first ->get_acl call.
203 * If the filesystem doesn't have a get_acl() function at all, we'll
204 * just create the negative cache entry.
206 if (acl == ACL_NOT_CACHED) {
207 if (inode->i_op->get_acl) {
208 acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
212 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
218 int error = posix_acl_permission(inode, acl, mask);
219 posix_acl_release(acl);
227 * This does basic POSIX ACL permission checking
229 static int acl_permission_check(struct inode *inode, int mask)
231 unsigned int mode = inode->i_mode;
233 mask &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
235 if (current_user_ns() != inode_userns(inode))
238 if (current_fsuid() == inode->i_uid)
241 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
242 int error = check_acl(inode, mask);
243 if (error != -EAGAIN)
247 if (in_group_p(inode->i_gid))
253 * If the DACs are ok we don't need any capability check.
255 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
261 * generic_permission - check for access rights on a Posix-like filesystem
262 * @inode: inode to check access rights for
263 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
265 * Used to check for read/write/execute permissions on a file.
266 * We use "fsuid" for this, letting us set arbitrary permissions
267 * for filesystem access without changing the "normal" uids which
268 * are used for other things.
270 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
271 * request cannot be satisfied (eg. requires blocking or too much complexity).
272 * It would then be called again in ref-walk mode.
274 int generic_permission(struct inode *inode, int mask)
279 * Do the basic POSIX ACL permission checks.
281 ret = acl_permission_check(inode, mask);
285 if (S_ISDIR(inode->i_mode)) {
286 /* DACs are overridable for directories */
287 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
289 if (!(mask & MAY_WRITE))
290 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
295 * Read/write DACs are always overridable.
296 * Executable DACs are overridable when there is
297 * at least one exec bit set.
299 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
300 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
304 * Searching includes executable on directories, else just read.
306 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
307 if (mask == MAY_READ)
308 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
315 * inode_permission - check for access rights to a given inode
316 * @inode: inode to check permission on
317 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
319 * Used to check for read/write/execute permissions on an inode.
320 * We use "fsuid" for this, letting us set arbitrary permissions
321 * for filesystem access without changing the "normal" uids which
322 * are used for other things.
324 int inode_permission(struct inode *inode, int mask)
328 if (mask & MAY_WRITE) {
329 umode_t mode = inode->i_mode;
332 * Nobody gets write access to a read-only fs.
334 if (IS_RDONLY(inode) &&
335 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
339 * Nobody gets write access to an immutable file.
341 if (IS_IMMUTABLE(inode))
345 if (inode->i_op->permission)
346 retval = inode->i_op->permission(inode, mask);
348 retval = generic_permission(inode, mask);
353 retval = devcgroup_inode_permission(inode, mask);
357 return security_inode_permission(inode, mask);
361 * path_get - get a reference to a path
362 * @path: path to get the reference to
364 * Given a path increment the reference count to the dentry and the vfsmount.
366 void path_get(struct path *path)
371 EXPORT_SYMBOL(path_get);
374 * path_put - put a reference to a path
375 * @path: path to put the reference to
377 * Given a path decrement the reference count to the dentry and the vfsmount.
379 void path_put(struct path *path)
384 EXPORT_SYMBOL(path_put);
387 * Path walking has 2 modes, rcu-walk and ref-walk (see
388 * Documentation/filesystems/path-lookup.txt). In situations when we can't
389 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
390 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
391 * mode. Refcounts are grabbed at the last known good point before rcu-walk
392 * got stuck, so ref-walk may continue from there. If this is not successful
393 * (eg. a seqcount has changed), then failure is returned and it's up to caller
394 * to restart the path walk from the beginning in ref-walk mode.
398 * unlazy_walk - try to switch to ref-walk mode.
399 * @nd: nameidata pathwalk data
400 * @dentry: child of nd->path.dentry or NULL
401 * Returns: 0 on success, -ECHILD on failure
403 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
404 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
405 * @nd or NULL. Must be called from rcu-walk context.
407 static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
409 struct fs_struct *fs = current->fs;
410 struct dentry *parent = nd->path.dentry;
413 BUG_ON(!(nd->flags & LOOKUP_RCU));
414 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
416 spin_lock(&fs->lock);
417 if (nd->root.mnt != fs->root.mnt ||
418 nd->root.dentry != fs->root.dentry)
421 spin_lock(&parent->d_lock);
423 if (!__d_rcu_to_refcount(parent, nd->seq))
425 BUG_ON(nd->inode != parent->d_inode);
427 if (dentry->d_parent != parent)
429 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
430 if (!__d_rcu_to_refcount(dentry, nd->seq))
433 * If the sequence check on the child dentry passed, then
434 * the child has not been removed from its parent. This
435 * means the parent dentry must be valid and able to take
436 * a reference at this point.
438 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
439 BUG_ON(!parent->d_count);
441 spin_unlock(&dentry->d_lock);
443 spin_unlock(&parent->d_lock);
446 spin_unlock(&fs->lock);
448 mntget(nd->path.mnt);
451 br_read_unlock(vfsmount_lock);
452 nd->flags &= ~LOOKUP_RCU;
456 spin_unlock(&dentry->d_lock);
458 spin_unlock(&parent->d_lock);
461 spin_unlock(&fs->lock);
466 * release_open_intent - free up open intent resources
467 * @nd: pointer to nameidata
469 void release_open_intent(struct nameidata *nd)
471 struct file *file = nd->intent.open.file;
473 if (file && !IS_ERR(file)) {
474 if (file->f_path.dentry == NULL)
481 static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
483 return dentry->d_op->d_revalidate(dentry, nd);
487 * complete_walk - successful completion of path walk
488 * @nd: pointer nameidata
490 * If we had been in RCU mode, drop out of it and legitimize nd->path.
491 * Revalidate the final result, unless we'd already done that during
492 * the path walk or the filesystem doesn't ask for it. Return 0 on
493 * success, -error on failure. In case of failure caller does not
494 * need to drop nd->path.
496 static int complete_walk(struct nameidata *nd)
498 struct dentry *dentry = nd->path.dentry;
501 if (nd->flags & LOOKUP_RCU) {
502 nd->flags &= ~LOOKUP_RCU;
503 if (!(nd->flags & LOOKUP_ROOT))
505 spin_lock(&dentry->d_lock);
506 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
507 spin_unlock(&dentry->d_lock);
509 br_read_unlock(vfsmount_lock);
512 BUG_ON(nd->inode != dentry->d_inode);
513 spin_unlock(&dentry->d_lock);
514 mntget(nd->path.mnt);
516 br_read_unlock(vfsmount_lock);
519 if (likely(!(nd->flags & LOOKUP_JUMPED)))
522 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
525 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
528 /* Note: we do not d_invalidate() */
529 status = d_revalidate(dentry, nd);
540 static __always_inline void set_root(struct nameidata *nd)
543 get_fs_root(current->fs, &nd->root);
546 static int link_path_walk(const char *, struct nameidata *);
548 static __always_inline void set_root_rcu(struct nameidata *nd)
551 struct fs_struct *fs = current->fs;
555 seq = read_seqcount_begin(&fs->seq);
557 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
558 } while (read_seqcount_retry(&fs->seq, seq));
562 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
574 nd->flags |= LOOKUP_JUMPED;
576 nd->inode = nd->path.dentry->d_inode;
578 ret = link_path_walk(link, nd);
582 return PTR_ERR(link);
585 static void path_put_conditional(struct path *path, struct nameidata *nd)
588 if (path->mnt != nd->path.mnt)
592 static inline void path_to_nameidata(const struct path *path,
593 struct nameidata *nd)
595 if (!(nd->flags & LOOKUP_RCU)) {
596 dput(nd->path.dentry);
597 if (nd->path.mnt != path->mnt)
598 mntput(nd->path.mnt);
600 nd->path.mnt = path->mnt;
601 nd->path.dentry = path->dentry;
604 static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
606 struct inode *inode = link->dentry->d_inode;
607 if (!IS_ERR(cookie) && inode->i_op->put_link)
608 inode->i_op->put_link(link->dentry, nd, cookie);
612 static __always_inline int
613 follow_link(struct path *link, struct nameidata *nd, void **p)
616 struct dentry *dentry = link->dentry;
618 BUG_ON(nd->flags & LOOKUP_RCU);
620 if (link->mnt == nd->path.mnt)
623 if (unlikely(current->total_link_count >= 40)) {
624 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
629 current->total_link_count++;
631 touch_atime(link->mnt, dentry);
632 nd_set_link(nd, NULL);
634 error = security_inode_follow_link(link->dentry, nd);
636 *p = ERR_PTR(error); /* no ->put_link(), please */
641 nd->last_type = LAST_BIND;
642 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
645 char *s = nd_get_link(nd);
648 error = __vfs_follow_link(nd, s);
649 else if (nd->last_type == LAST_BIND) {
650 nd->flags |= LOOKUP_JUMPED;
651 nd->inode = nd->path.dentry->d_inode;
652 if (nd->inode->i_op->follow_link) {
653 /* stepped on a _really_ weird one */
662 static int follow_up_rcu(struct path *path)
664 struct vfsmount *parent;
665 struct dentry *mountpoint;
667 parent = path->mnt->mnt_parent;
668 if (parent == path->mnt)
670 mountpoint = path->mnt->mnt_mountpoint;
671 path->dentry = mountpoint;
676 int follow_up(struct path *path)
678 struct vfsmount *parent;
679 struct dentry *mountpoint;
681 br_read_lock(vfsmount_lock);
682 parent = path->mnt->mnt_parent;
683 if (parent == path->mnt) {
684 br_read_unlock(vfsmount_lock);
688 mountpoint = dget(path->mnt->mnt_mountpoint);
689 br_read_unlock(vfsmount_lock);
691 path->dentry = mountpoint;
698 * Perform an automount
699 * - return -EISDIR to tell follow_managed() to stop and return the path we
702 static int follow_automount(struct path *path, unsigned flags,
705 struct vfsmount *mnt;
708 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
711 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
712 * and this is the terminal part of the path.
714 if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_PARENT))
715 return -EISDIR; /* we actually want to stop here */
717 /* We want to mount if someone is trying to open/create a file of any
718 * type under the mountpoint, wants to traverse through the mountpoint
719 * or wants to open the mounted directory.
721 * We don't want to mount if someone's just doing a stat and they've
722 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
723 * appended a '/' to the name.
725 if (!(flags & LOOKUP_FOLLOW) &&
726 !(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
727 LOOKUP_OPEN | LOOKUP_CREATE)))
730 current->total_link_count++;
731 if (current->total_link_count >= 40)
734 mnt = path->dentry->d_op->d_automount(path);
737 * The filesystem is allowed to return -EISDIR here to indicate
738 * it doesn't want to automount. For instance, autofs would do
739 * this so that its userspace daemon can mount on this dentry.
741 * However, we can only permit this if it's a terminal point in
742 * the path being looked up; if it wasn't then the remainder of
743 * the path is inaccessible and we should say so.
745 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
750 if (!mnt) /* mount collision */
754 /* lock_mount() may release path->mnt on error */
758 err = finish_automount(mnt, path);
762 /* Someone else made a mount here whilst we were busy */
767 path->dentry = dget(mnt->mnt_root);
776 * Handle a dentry that is managed in some way.
777 * - Flagged for transit management (autofs)
778 * - Flagged as mountpoint
779 * - Flagged as automount point
781 * This may only be called in refwalk mode.
783 * Serialization is taken care of in namespace.c
785 static int follow_managed(struct path *path, unsigned flags)
787 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
789 bool need_mntput = false;
792 /* Given that we're not holding a lock here, we retain the value in a
793 * local variable for each dentry as we look at it so that we don't see
794 * the components of that value change under us */
795 while (managed = ACCESS_ONCE(path->dentry->d_flags),
796 managed &= DCACHE_MANAGED_DENTRY,
797 unlikely(managed != 0)) {
798 /* Allow the filesystem to manage the transit without i_mutex
800 if (managed & DCACHE_MANAGE_TRANSIT) {
801 BUG_ON(!path->dentry->d_op);
802 BUG_ON(!path->dentry->d_op->d_manage);
803 ret = path->dentry->d_op->d_manage(path->dentry, false);
808 /* Transit to a mounted filesystem. */
809 if (managed & DCACHE_MOUNTED) {
810 struct vfsmount *mounted = lookup_mnt(path);
816 path->dentry = dget(mounted->mnt_root);
821 /* Something is mounted on this dentry in another
822 * namespace and/or whatever was mounted there in this
823 * namespace got unmounted before we managed to get the
827 /* Handle an automount point */
828 if (managed & DCACHE_NEED_AUTOMOUNT) {
829 ret = follow_automount(path, flags, &need_mntput);
835 /* We didn't change the current path point */
839 if (need_mntput && path->mnt == mnt)
846 int follow_down_one(struct path *path)
848 struct vfsmount *mounted;
850 mounted = lookup_mnt(path);
855 path->dentry = dget(mounted->mnt_root);
861 static inline bool managed_dentry_might_block(struct dentry *dentry)
863 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
864 dentry->d_op->d_manage(dentry, true) < 0);
868 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
869 * we meet a managed dentry that would need blocking.
871 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
872 struct inode **inode)
875 struct vfsmount *mounted;
877 * Don't forget we might have a non-mountpoint managed dentry
878 * that wants to block transit.
880 if (unlikely(managed_dentry_might_block(path->dentry)))
883 if (!d_mountpoint(path->dentry))
886 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
890 path->dentry = mounted->mnt_root;
891 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
893 * Update the inode too. We don't need to re-check the
894 * dentry sequence number here after this d_inode read,
895 * because a mount-point is always pinned.
897 *inode = path->dentry->d_inode;
902 static void follow_mount_rcu(struct nameidata *nd)
904 while (d_mountpoint(nd->path.dentry)) {
905 struct vfsmount *mounted;
906 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
909 nd->path.mnt = mounted;
910 nd->path.dentry = mounted->mnt_root;
911 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
915 static int follow_dotdot_rcu(struct nameidata *nd)
920 if (nd->path.dentry == nd->root.dentry &&
921 nd->path.mnt == nd->root.mnt) {
924 if (nd->path.dentry != nd->path.mnt->mnt_root) {
925 struct dentry *old = nd->path.dentry;
926 struct dentry *parent = old->d_parent;
929 seq = read_seqcount_begin(&parent->d_seq);
930 if (read_seqcount_retry(&old->d_seq, nd->seq))
932 nd->path.dentry = parent;
936 if (!follow_up_rcu(&nd->path))
938 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
940 follow_mount_rcu(nd);
941 nd->inode = nd->path.dentry->d_inode;
945 nd->flags &= ~LOOKUP_RCU;
946 if (!(nd->flags & LOOKUP_ROOT))
949 br_read_unlock(vfsmount_lock);
954 * Follow down to the covering mount currently visible to userspace. At each
955 * point, the filesystem owning that dentry may be queried as to whether the
956 * caller is permitted to proceed or not.
958 int follow_down(struct path *path)
963 while (managed = ACCESS_ONCE(path->dentry->d_flags),
964 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
965 /* Allow the filesystem to manage the transit without i_mutex
968 * We indicate to the filesystem if someone is trying to mount
969 * something here. This gives autofs the chance to deny anyone
970 * other than its daemon the right to mount on its
973 * The filesystem may sleep at this point.
975 if (managed & DCACHE_MANAGE_TRANSIT) {
976 BUG_ON(!path->dentry->d_op);
977 BUG_ON(!path->dentry->d_op->d_manage);
978 ret = path->dentry->d_op->d_manage(
979 path->dentry, false);
981 return ret == -EISDIR ? 0 : ret;
984 /* Transit to a mounted filesystem. */
985 if (managed & DCACHE_MOUNTED) {
986 struct vfsmount *mounted = lookup_mnt(path);
992 path->dentry = dget(mounted->mnt_root);
996 /* Don't handle automount points here */
1003 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1005 static void follow_mount(struct path *path)
1007 while (d_mountpoint(path->dentry)) {
1008 struct vfsmount *mounted = lookup_mnt(path);
1013 path->mnt = mounted;
1014 path->dentry = dget(mounted->mnt_root);
1018 static void follow_dotdot(struct nameidata *nd)
1023 struct dentry *old = nd->path.dentry;
1025 if (nd->path.dentry == nd->root.dentry &&
1026 nd->path.mnt == nd->root.mnt) {
1029 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1030 /* rare case of legitimate dget_parent()... */
1031 nd->path.dentry = dget_parent(nd->path.dentry);
1035 if (!follow_up(&nd->path))
1038 follow_mount(&nd->path);
1039 nd->inode = nd->path.dentry->d_inode;
1043 * Allocate a dentry with name and parent, and perform a parent
1044 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1045 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1046 * have verified that no child exists while under i_mutex.
1048 static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1049 struct qstr *name, struct nameidata *nd)
1051 struct inode *inode = parent->d_inode;
1052 struct dentry *dentry;
1055 /* Don't create child dentry for a dead directory. */
1056 if (unlikely(IS_DEADDIR(inode)))
1057 return ERR_PTR(-ENOENT);
1059 dentry = d_alloc(parent, name);
1060 if (unlikely(!dentry))
1061 return ERR_PTR(-ENOMEM);
1063 old = inode->i_op->lookup(inode, dentry, nd);
1064 if (unlikely(old)) {
1072 * We already have a dentry, but require a lookup to be performed on the parent
1073 * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
1074 * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
1075 * child exists while under i_mutex.
1077 static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
1078 struct nameidata *nd)
1080 struct inode *inode = parent->d_inode;
1083 /* Don't create child dentry for a dead directory. */
1084 if (unlikely(IS_DEADDIR(inode)))
1085 return ERR_PTR(-ENOENT);
1087 old = inode->i_op->lookup(inode, dentry, nd);
1088 if (unlikely(old)) {
1096 * It's more convoluted than I'd like it to be, but... it's still fairly
1097 * small and for now I'd prefer to have fast path as straight as possible.
1098 * It _is_ time-critical.
1100 static int do_lookup(struct nameidata *nd, struct qstr *name,
1101 struct path *path, struct inode **inode)
1103 struct vfsmount *mnt = nd->path.mnt;
1104 struct dentry *dentry, *parent = nd->path.dentry;
1110 * Rename seqlock is not required here because in the off chance
1111 * of a false negative due to a concurrent rename, we're going to
1112 * do the non-racy lookup, below.
1114 if (nd->flags & LOOKUP_RCU) {
1117 dentry = __d_lookup_rcu(parent, name, &seq, inode);
1121 /* Memory barrier in read_seqcount_begin of child is enough */
1122 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1126 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1127 status = d_revalidate(dentry, nd);
1128 if (unlikely(status <= 0)) {
1129 if (status != -ECHILD)
1134 if (unlikely(d_need_lookup(dentry)))
1137 path->dentry = dentry;
1138 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1140 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1144 if (unlazy_walk(nd, dentry))
1147 dentry = __d_lookup(parent, name);
1150 if (dentry && unlikely(d_need_lookup(dentry))) {
1155 if (unlikely(!dentry)) {
1156 struct inode *dir = parent->d_inode;
1157 BUG_ON(nd->inode != dir);
1159 mutex_lock(&dir->i_mutex);
1160 dentry = d_lookup(parent, name);
1161 if (likely(!dentry)) {
1162 dentry = d_alloc_and_lookup(parent, name, nd);
1163 if (IS_ERR(dentry)) {
1164 mutex_unlock(&dir->i_mutex);
1165 return PTR_ERR(dentry);
1170 } else if (unlikely(d_need_lookup(dentry))) {
1171 dentry = d_inode_lookup(parent, dentry, nd);
1172 if (IS_ERR(dentry)) {
1173 mutex_unlock(&dir->i_mutex);
1174 return PTR_ERR(dentry);
1180 mutex_unlock(&dir->i_mutex);
1182 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1183 status = d_revalidate(dentry, nd);
1184 if (unlikely(status <= 0)) {
1189 if (!d_invalidate(dentry)) {
1198 path->dentry = dentry;
1199 err = follow_managed(path, nd->flags);
1200 if (unlikely(err < 0)) {
1201 path_put_conditional(path, nd);
1204 *inode = path->dentry->d_inode;
1208 static inline int may_lookup(struct nameidata *nd)
1210 if (nd->flags & LOOKUP_RCU) {
1211 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
1214 if (unlazy_walk(nd, NULL))
1217 return inode_permission(nd->inode, MAY_EXEC);
1220 static inline int handle_dots(struct nameidata *nd, int type)
1222 if (type == LAST_DOTDOT) {
1223 if (nd->flags & LOOKUP_RCU) {
1224 if (follow_dotdot_rcu(nd))
1232 static void terminate_walk(struct nameidata *nd)
1234 if (!(nd->flags & LOOKUP_RCU)) {
1235 path_put(&nd->path);
1237 nd->flags &= ~LOOKUP_RCU;
1238 if (!(nd->flags & LOOKUP_ROOT))
1239 nd->root.mnt = NULL;
1241 br_read_unlock(vfsmount_lock);
1245 static inline int walk_component(struct nameidata *nd, struct path *path,
1246 struct qstr *name, int type, int follow)
1248 struct inode *inode;
1251 * "." and ".." are special - ".." especially so because it has
1252 * to be able to know about the current root directory and
1253 * parent relationships.
1255 if (unlikely(type != LAST_NORM))
1256 return handle_dots(nd, type);
1257 err = do_lookup(nd, name, path, &inode);
1258 if (unlikely(err)) {
1263 path_to_nameidata(path, nd);
1267 if (unlikely(inode->i_op->follow_link) && follow) {
1268 if (nd->flags & LOOKUP_RCU) {
1269 if (unlikely(unlazy_walk(nd, path->dentry))) {
1274 BUG_ON(inode != path->dentry->d_inode);
1277 path_to_nameidata(path, nd);
1283 * This limits recursive symlink follows to 8, while
1284 * limiting consecutive symlinks to 40.
1286 * Without that kind of total limit, nasty chains of consecutive
1287 * symlinks can cause almost arbitrarily long lookups.
1289 static inline int nested_symlink(struct path *path, struct nameidata *nd)
1293 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1294 path_put_conditional(path, nd);
1295 path_put(&nd->path);
1298 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1301 current->link_count++;
1304 struct path link = *path;
1307 res = follow_link(&link, nd, &cookie);
1309 res = walk_component(nd, path, &nd->last,
1310 nd->last_type, LOOKUP_FOLLOW);
1311 put_link(nd, &link, cookie);
1314 current->link_count--;
1321 * This is the basic name resolution function, turning a pathname into
1322 * the final dentry. We expect 'base' to be positive and a directory.
1324 * Returns 0 and nd will have valid dentry and mnt on success.
1325 * Returns error and drops reference to input namei data on failure.
1327 static int link_path_walk(const char *name, struct nameidata *nd)
1337 /* At this point we know we have a real path component. */
1344 err = may_lookup(nd);
1349 c = *(const unsigned char *)name;
1351 hash = init_name_hash();
1354 hash = partial_name_hash(c, hash);
1355 c = *(const unsigned char *)name;
1356 } while (c && (c != '/'));
1357 this.len = name - (const char *) this.name;
1358 this.hash = end_name_hash(hash);
1361 if (this.name[0] == '.') switch (this.len) {
1363 if (this.name[1] == '.') {
1365 nd->flags |= LOOKUP_JUMPED;
1371 if (likely(type == LAST_NORM)) {
1372 struct dentry *parent = nd->path.dentry;
1373 nd->flags &= ~LOOKUP_JUMPED;
1374 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1375 err = parent->d_op->d_hash(parent, nd->inode,
1382 /* remove trailing slashes? */
1384 goto last_component;
1385 while (*++name == '/');
1387 goto last_component;
1389 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1394 err = nested_symlink(&next, nd);
1399 if (!nd->inode->i_op->lookup)
1402 /* here ends the main loop */
1406 nd->last_type = type;
1413 static int path_init(int dfd, const char *name, unsigned int flags,
1414 struct nameidata *nd, struct file **fp)
1420 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1421 nd->flags = flags | LOOKUP_JUMPED;
1423 if (flags & LOOKUP_ROOT) {
1424 struct inode *inode = nd->root.dentry->d_inode;
1426 if (!inode->i_op->lookup)
1428 retval = inode_permission(inode, MAY_EXEC);
1432 nd->path = nd->root;
1434 if (flags & LOOKUP_RCU) {
1435 br_read_lock(vfsmount_lock);
1437 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1439 path_get(&nd->path);
1444 nd->root.mnt = NULL;
1447 if (flags & LOOKUP_RCU) {
1448 br_read_lock(vfsmount_lock);
1453 path_get(&nd->root);
1455 nd->path = nd->root;
1456 } else if (dfd == AT_FDCWD) {
1457 if (flags & LOOKUP_RCU) {
1458 struct fs_struct *fs = current->fs;
1461 br_read_lock(vfsmount_lock);
1465 seq = read_seqcount_begin(&fs->seq);
1467 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1468 } while (read_seqcount_retry(&fs->seq, seq));
1470 get_fs_pwd(current->fs, &nd->path);
1473 struct dentry *dentry;
1475 file = fget_raw_light(dfd, &fput_needed);
1480 dentry = file->f_path.dentry;
1484 if (!S_ISDIR(dentry->d_inode->i_mode))
1487 retval = inode_permission(dentry->d_inode, MAY_EXEC);
1492 nd->path = file->f_path;
1493 if (flags & LOOKUP_RCU) {
1496 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1497 br_read_lock(vfsmount_lock);
1500 path_get(&file->f_path);
1501 fput_light(file, fput_needed);
1505 nd->inode = nd->path.dentry->d_inode;
1509 fput_light(file, fput_needed);
1514 static inline int lookup_last(struct nameidata *nd, struct path *path)
1516 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1517 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1519 nd->flags &= ~LOOKUP_PARENT;
1520 return walk_component(nd, path, &nd->last, nd->last_type,
1521 nd->flags & LOOKUP_FOLLOW);
1524 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1525 static int path_lookupat(int dfd, const char *name,
1526 unsigned int flags, struct nameidata *nd)
1528 struct file *base = NULL;
1533 * Path walking is largely split up into 2 different synchronisation
1534 * schemes, rcu-walk and ref-walk (explained in
1535 * Documentation/filesystems/path-lookup.txt). These share much of the
1536 * path walk code, but some things particularly setup, cleanup, and
1537 * following mounts are sufficiently divergent that functions are
1538 * duplicated. Typically there is a function foo(), and its RCU
1539 * analogue, foo_rcu().
1541 * -ECHILD is the error number of choice (just to avoid clashes) that
1542 * is returned if some aspect of an rcu-walk fails. Such an error must
1543 * be handled by restarting a traditional ref-walk (which will always
1544 * be able to complete).
1546 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1551 current->total_link_count = 0;
1552 err = link_path_walk(name, nd);
1554 if (!err && !(flags & LOOKUP_PARENT)) {
1555 err = lookup_last(nd, &path);
1558 struct path link = path;
1559 nd->flags |= LOOKUP_PARENT;
1560 err = follow_link(&link, nd, &cookie);
1562 err = lookup_last(nd, &path);
1563 put_link(nd, &link, cookie);
1568 err = complete_walk(nd);
1570 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1571 if (!nd->inode->i_op->lookup) {
1572 path_put(&nd->path);
1580 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
1581 path_put(&nd->root);
1582 nd->root.mnt = NULL;
1587 static int do_path_lookup(int dfd, const char *name,
1588 unsigned int flags, struct nameidata *nd)
1590 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1591 if (unlikely(retval == -ECHILD))
1592 retval = path_lookupat(dfd, name, flags, nd);
1593 if (unlikely(retval == -ESTALE))
1594 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1596 if (likely(!retval)) {
1597 if (unlikely(!audit_dummy_context())) {
1598 if (nd->path.dentry && nd->inode)
1599 audit_inode(name, nd->path.dentry);
1605 int kern_path_parent(const char *name, struct nameidata *nd)
1607 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
1610 int kern_path(const char *name, unsigned int flags, struct path *path)
1612 struct nameidata nd;
1613 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1620 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1621 * @dentry: pointer to dentry of the base directory
1622 * @mnt: pointer to vfs mount of the base directory
1623 * @name: pointer to file name
1624 * @flags: lookup flags
1625 * @path: pointer to struct path to fill
1627 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1628 const char *name, unsigned int flags,
1631 struct nameidata nd;
1633 nd.root.dentry = dentry;
1635 BUG_ON(flags & LOOKUP_PARENT);
1636 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1637 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1643 static struct dentry *__lookup_hash(struct qstr *name,
1644 struct dentry *base, struct nameidata *nd)
1646 struct inode *inode = base->d_inode;
1647 struct dentry *dentry;
1650 err = inode_permission(inode, MAY_EXEC);
1652 return ERR_PTR(err);
1655 * Don't bother with __d_lookup: callers are for creat as
1656 * well as unlink, so a lot of the time it would cost
1659 dentry = d_lookup(base, name);
1661 if (dentry && d_need_lookup(dentry)) {
1663 * __lookup_hash is called with the parent dir's i_mutex already
1664 * held, so we are good to go here.
1666 dentry = d_inode_lookup(base, dentry, nd);
1671 if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1672 int status = d_revalidate(dentry, nd);
1673 if (unlikely(status <= 0)) {
1675 * The dentry failed validation.
1676 * If d_revalidate returned 0 attempt to invalidate
1677 * the dentry otherwise d_revalidate is asking us
1678 * to return a fail status.
1682 return ERR_PTR(status);
1683 } else if (!d_invalidate(dentry)) {
1691 dentry = d_alloc_and_lookup(base, name, nd);
1697 * Restricted form of lookup. Doesn't follow links, single-component only,
1698 * needs parent already locked. Doesn't follow mounts.
1701 static struct dentry *lookup_hash(struct nameidata *nd)
1703 return __lookup_hash(&nd->last, nd->path.dentry, nd);
1707 * lookup_one_len - filesystem helper to lookup single pathname component
1708 * @name: pathname component to lookup
1709 * @base: base directory to lookup from
1710 * @len: maximum length @len should be interpreted to
1712 * Note that this routine is purely a helper for filesystem usage and should
1713 * not be called by generic code. Also note that by using this function the
1714 * nameidata argument is passed to the filesystem methods and a filesystem
1715 * using this helper needs to be prepared for that.
1717 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1723 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1728 return ERR_PTR(-EACCES);
1730 hash = init_name_hash();
1732 c = *(const unsigned char *)name++;
1733 if (c == '/' || c == '\0')
1734 return ERR_PTR(-EACCES);
1735 hash = partial_name_hash(c, hash);
1737 this.hash = end_name_hash(hash);
1739 * See if the low-level filesystem might want
1740 * to use its own hash..
1742 if (base->d_flags & DCACHE_OP_HASH) {
1743 int err = base->d_op->d_hash(base, base->d_inode, &this);
1745 return ERR_PTR(err);
1748 return __lookup_hash(&this, base, NULL);
1751 int user_path_at(int dfd, const char __user *name, unsigned flags,
1754 struct nameidata nd;
1755 char *tmp = getname_flags(name, flags);
1756 int err = PTR_ERR(tmp);
1759 BUG_ON(flags & LOOKUP_PARENT);
1761 err = do_path_lookup(dfd, tmp, flags, &nd);
1769 static int user_path_parent(int dfd, const char __user *path,
1770 struct nameidata *nd, char **name)
1772 char *s = getname(path);
1778 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1788 * It's inline, so penalty for filesystems that don't use sticky bit is
1791 static inline int check_sticky(struct inode *dir, struct inode *inode)
1793 uid_t fsuid = current_fsuid();
1795 if (!(dir->i_mode & S_ISVTX))
1797 if (current_user_ns() != inode_userns(inode))
1799 if (inode->i_uid == fsuid)
1801 if (dir->i_uid == fsuid)
1805 return !ns_capable(inode_userns(inode), CAP_FOWNER);
1809 * Check whether we can remove a link victim from directory dir, check
1810 * whether the type of victim is right.
1811 * 1. We can't do it if dir is read-only (done in permission())
1812 * 2. We should have write and exec permissions on dir
1813 * 3. We can't remove anything from append-only dir
1814 * 4. We can't do anything with immutable dir (done in permission())
1815 * 5. If the sticky bit on dir is set we should either
1816 * a. be owner of dir, or
1817 * b. be owner of victim, or
1818 * c. have CAP_FOWNER capability
1819 * 6. If the victim is append-only or immutable we can't do antyhing with
1820 * links pointing to it.
1821 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1822 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1823 * 9. We can't remove a root or mountpoint.
1824 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1825 * nfs_async_unlink().
1827 static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1831 if (!victim->d_inode)
1834 BUG_ON(victim->d_parent->d_inode != dir);
1835 audit_inode_child(victim, dir);
1837 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1842 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1843 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
1846 if (!S_ISDIR(victim->d_inode->i_mode))
1848 if (IS_ROOT(victim))
1850 } else if (S_ISDIR(victim->d_inode->i_mode))
1852 if (IS_DEADDIR(dir))
1854 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1859 /* Check whether we can create an object with dentry child in directory
1861 * 1. We can't do it if child already exists (open has special treatment for
1862 * this case, but since we are inlined it's OK)
1863 * 2. We can't do it if dir is read-only (done in permission())
1864 * 3. We should have write and exec permissions on dir
1865 * 4. We can't do it if dir is immutable (done in permission())
1867 static inline int may_create(struct inode *dir, struct dentry *child)
1871 if (IS_DEADDIR(dir))
1873 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
1877 * p1 and p2 should be directories on the same fs.
1879 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1884 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1888 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1890 p = d_ancestor(p2, p1);
1892 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1893 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1897 p = d_ancestor(p1, p2);
1899 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1900 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1904 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1905 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1909 void unlock_rename(struct dentry *p1, struct dentry *p2)
1911 mutex_unlock(&p1->d_inode->i_mutex);
1913 mutex_unlock(&p2->d_inode->i_mutex);
1914 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1918 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1919 struct nameidata *nd)
1921 int error = may_create(dir, dentry);
1926 if (!dir->i_op->create)
1927 return -EACCES; /* shouldn't it be ENOSYS? */
1930 error = security_inode_create(dir, dentry, mode);
1933 error = dir->i_op->create(dir, dentry, mode, nd);
1935 fsnotify_create(dir, dentry);
1939 static int may_open(struct path *path, int acc_mode, int flag)
1941 struct dentry *dentry = path->dentry;
1942 struct inode *inode = dentry->d_inode;
1952 switch (inode->i_mode & S_IFMT) {
1956 if (acc_mode & MAY_WRITE)
1961 if (path->mnt->mnt_flags & MNT_NODEV)
1970 error = inode_permission(inode, acc_mode);
1975 * An append-only file must be opened in append mode for writing.
1977 if (IS_APPEND(inode)) {
1978 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
1984 /* O_NOATIME can only be set by the owner or superuser */
1985 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
1989 * Ensure there are no outstanding leases on the file.
1991 return break_lease(inode, flag);
1994 static int handle_truncate(struct file *filp)
1996 struct path *path = &filp->f_path;
1997 struct inode *inode = path->dentry->d_inode;
1998 int error = get_write_access(inode);
2002 * Refuse to truncate files with mandatory locks held on them.
2004 error = locks_verify_locked(inode);
2006 error = security_path_truncate(path);
2008 error = do_truncate(path->dentry, 0,
2009 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2012 put_write_access(inode);
2016 static inline int open_to_namei_flags(int flag)
2018 if ((flag & O_ACCMODE) == 3)
2024 * Handle the last step of open()
2026 static struct file *do_last(struct nameidata *nd, struct path *path,
2027 const struct open_flags *op, const char *pathname)
2029 struct dentry *dir = nd->path.dentry;
2030 struct dentry *dentry;
2031 int open_flag = op->open_flag;
2032 int will_truncate = open_flag & O_TRUNC;
2034 int acc_mode = op->acc_mode;
2038 nd->flags &= ~LOOKUP_PARENT;
2039 nd->flags |= op->intent;
2041 switch (nd->last_type) {
2044 error = handle_dots(nd, nd->last_type);
2046 return ERR_PTR(error);
2049 error = complete_walk(nd);
2051 return ERR_PTR(error);
2052 audit_inode(pathname, nd->path.dentry);
2053 if (open_flag & O_CREAT) {
2059 error = complete_walk(nd);
2061 return ERR_PTR(error);
2062 audit_inode(pathname, dir);
2066 if (!(open_flag & O_CREAT)) {
2068 if (nd->last.name[nd->last.len])
2069 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2070 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2072 /* we _can_ be in RCU mode here */
2073 error = walk_component(nd, path, &nd->last, LAST_NORM,
2076 return ERR_PTR(error);
2077 if (error) /* symlink */
2080 error = complete_walk(nd);
2082 return ERR_PTR(-ECHILD);
2085 if (nd->flags & LOOKUP_DIRECTORY) {
2086 if (!nd->inode->i_op->lookup)
2089 audit_inode(pathname, nd->path.dentry);
2093 /* create side of things */
2094 error = complete_walk(nd);
2096 return ERR_PTR(error);
2098 audit_inode(pathname, dir);
2100 /* trailing slashes? */
2101 if (nd->last.name[nd->last.len])
2104 mutex_lock(&dir->d_inode->i_mutex);
2106 dentry = lookup_hash(nd);
2107 error = PTR_ERR(dentry);
2108 if (IS_ERR(dentry)) {
2109 mutex_unlock(&dir->d_inode->i_mutex);
2113 path->dentry = dentry;
2114 path->mnt = nd->path.mnt;
2116 /* Negative dentry, just create the file */
2117 if (!dentry->d_inode) {
2118 int mode = op->mode;
2119 if (!IS_POSIXACL(dir->d_inode))
2120 mode &= ~current_umask();
2122 * This write is needed to ensure that a
2123 * rw->ro transition does not occur between
2124 * the time when the file is created and when
2125 * a permanent write count is taken through
2126 * the 'struct file' in nameidata_to_filp().
2128 error = mnt_want_write(nd->path.mnt);
2130 goto exit_mutex_unlock;
2132 /* Don't check for write permission, don't truncate */
2133 open_flag &= ~O_TRUNC;
2135 acc_mode = MAY_OPEN;
2136 error = security_path_mknod(&nd->path, dentry, mode, 0);
2138 goto exit_mutex_unlock;
2139 error = vfs_create(dir->d_inode, dentry, mode, nd);
2141 goto exit_mutex_unlock;
2142 mutex_unlock(&dir->d_inode->i_mutex);
2143 dput(nd->path.dentry);
2144 nd->path.dentry = dentry;
2149 * It already exists.
2151 mutex_unlock(&dir->d_inode->i_mutex);
2152 audit_inode(pathname, path->dentry);
2155 if (open_flag & O_EXCL)
2158 error = follow_managed(path, nd->flags);
2163 if (!path->dentry->d_inode)
2166 if (path->dentry->d_inode->i_op->follow_link)
2169 path_to_nameidata(path, nd);
2170 nd->inode = path->dentry->d_inode;
2172 if (S_ISDIR(nd->inode->i_mode))
2175 if (!S_ISREG(nd->inode->i_mode))
2178 if (will_truncate) {
2179 error = mnt_want_write(nd->path.mnt);
2185 error = may_open(&nd->path, acc_mode, open_flag);
2188 filp = nameidata_to_filp(nd);
2189 if (!IS_ERR(filp)) {
2190 error = ima_file_check(filp, op->acc_mode);
2193 filp = ERR_PTR(error);
2196 if (!IS_ERR(filp)) {
2197 if (will_truncate) {
2198 error = handle_truncate(filp);
2201 filp = ERR_PTR(error);
2207 mnt_drop_write(nd->path.mnt);
2208 path_put(&nd->path);
2212 mutex_unlock(&dir->d_inode->i_mutex);
2214 path_put_conditional(path, nd);
2216 filp = ERR_PTR(error);
2220 static struct file *path_openat(int dfd, const char *pathname,
2221 struct nameidata *nd, const struct open_flags *op, int flags)
2223 struct file *base = NULL;
2228 filp = get_empty_filp();
2230 return ERR_PTR(-ENFILE);
2232 filp->f_flags = op->open_flag;
2233 nd->intent.open.file = filp;
2234 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2235 nd->intent.open.create_mode = op->mode;
2237 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
2238 if (unlikely(error))
2241 current->total_link_count = 0;
2242 error = link_path_walk(pathname, nd);
2243 if (unlikely(error))
2246 filp = do_last(nd, &path, op, pathname);
2247 while (unlikely(!filp)) { /* trailing symlink */
2248 struct path link = path;
2250 if (!(nd->flags & LOOKUP_FOLLOW)) {
2251 path_put_conditional(&path, nd);
2252 path_put(&nd->path);
2253 filp = ERR_PTR(-ELOOP);
2256 nd->flags |= LOOKUP_PARENT;
2257 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2258 error = follow_link(&link, nd, &cookie);
2259 if (unlikely(error))
2260 filp = ERR_PTR(error);
2262 filp = do_last(nd, &path, op, pathname);
2263 put_link(nd, &link, cookie);
2266 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2267 path_put(&nd->root);
2270 release_open_intent(nd);
2274 filp = ERR_PTR(error);
2278 struct file *do_filp_open(int dfd, const char *pathname,
2279 const struct open_flags *op, int flags)
2281 struct nameidata nd;
2284 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
2285 if (unlikely(filp == ERR_PTR(-ECHILD)))
2286 filp = path_openat(dfd, pathname, &nd, op, flags);
2287 if (unlikely(filp == ERR_PTR(-ESTALE)))
2288 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
2292 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2293 const char *name, const struct open_flags *op, int flags)
2295 struct nameidata nd;
2299 nd.root.dentry = dentry;
2301 flags |= LOOKUP_ROOT;
2303 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
2304 return ERR_PTR(-ELOOP);
2306 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2307 if (unlikely(file == ERR_PTR(-ECHILD)))
2308 file = path_openat(-1, name, &nd, op, flags);
2309 if (unlikely(file == ERR_PTR(-ESTALE)))
2310 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2314 struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
2316 struct dentry *dentry = ERR_PTR(-EEXIST);
2317 struct nameidata nd;
2318 int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2320 return ERR_PTR(error);
2323 * Yucky last component or no last component at all?
2324 * (foo/., foo/.., /////)
2326 if (nd.last_type != LAST_NORM)
2328 nd.flags &= ~LOOKUP_PARENT;
2329 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2330 nd.intent.open.flags = O_EXCL;
2333 * Do the final lookup.
2335 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2336 dentry = lookup_hash(&nd);
2340 if (dentry->d_inode)
2343 * Special case - lookup gave negative, but... we had foo/bar/
2344 * From the vfs_mknod() POV we just have a negative dentry -
2345 * all is fine. Let's be bastards - you had / on the end, you've
2346 * been asking for (non-existent) directory. -ENOENT for you.
2348 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
2350 dentry = ERR_PTR(-ENOENT);
2357 dentry = ERR_PTR(-EEXIST);
2359 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2364 EXPORT_SYMBOL(kern_path_create);
2366 struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2368 char *tmp = getname(pathname);
2371 return ERR_CAST(tmp);
2372 res = kern_path_create(dfd, tmp, path, is_dir);
2376 EXPORT_SYMBOL(user_path_create);
2378 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2380 int error = may_create(dir, dentry);
2385 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2386 !ns_capable(inode_userns(dir), CAP_MKNOD))
2389 if (!dir->i_op->mknod)
2392 error = devcgroup_inode_mknod(mode, dev);
2396 error = security_inode_mknod(dir, dentry, mode, dev);
2400 error = dir->i_op->mknod(dir, dentry, mode, dev);
2402 fsnotify_create(dir, dentry);
2406 static int may_mknod(mode_t mode)
2408 switch (mode & S_IFMT) {
2414 case 0: /* zero mode translates to S_IFREG */
2423 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2426 struct dentry *dentry;
2433 dentry = user_path_create(dfd, filename, &path, 0);
2435 return PTR_ERR(dentry);
2437 if (!IS_POSIXACL(path.dentry->d_inode))
2438 mode &= ~current_umask();
2439 error = may_mknod(mode);
2442 error = mnt_want_write(path.mnt);
2445 error = security_path_mknod(&path, dentry, mode, dev);
2447 goto out_drop_write;
2448 switch (mode & S_IFMT) {
2449 case 0: case S_IFREG:
2450 error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
2452 case S_IFCHR: case S_IFBLK:
2453 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
2454 new_decode_dev(dev));
2456 case S_IFIFO: case S_IFSOCK:
2457 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
2461 mnt_drop_write(path.mnt);
2464 mutex_unlock(&path.dentry->d_inode->i_mutex);
2470 SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
2472 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2475 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2477 int error = may_create(dir, dentry);
2482 if (!dir->i_op->mkdir)
2485 mode &= (S_IRWXUGO|S_ISVTX);
2486 error = security_inode_mkdir(dir, dentry, mode);
2490 error = dir->i_op->mkdir(dir, dentry, mode);
2492 fsnotify_mkdir(dir, dentry);
2496 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
2498 struct dentry *dentry;
2502 dentry = user_path_create(dfd, pathname, &path, 1);
2504 return PTR_ERR(dentry);
2506 if (!IS_POSIXACL(path.dentry->d_inode))
2507 mode &= ~current_umask();
2508 error = mnt_want_write(path.mnt);
2511 error = security_path_mkdir(&path, dentry, mode);
2513 goto out_drop_write;
2514 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
2516 mnt_drop_write(path.mnt);
2519 mutex_unlock(&path.dentry->d_inode->i_mutex);
2524 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
2526 return sys_mkdirat(AT_FDCWD, pathname, mode);
2530 * The dentry_unhash() helper will try to drop the dentry early: we
2531 * should have a usage count of 2 if we're the only user of this
2532 * dentry, and if that is true (possibly after pruning the dcache),
2533 * then we drop the dentry now.
2535 * A low-level filesystem can, if it choses, legally
2538 * if (!d_unhashed(dentry))
2541 * if it cannot handle the case of removing a directory
2542 * that is still in use by something else..
2544 void dentry_unhash(struct dentry *dentry)
2546 shrink_dcache_parent(dentry);
2547 spin_lock(&dentry->d_lock);
2548 if (dentry->d_count == 1)
2550 spin_unlock(&dentry->d_lock);
2553 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2555 int error = may_delete(dir, dentry, 1);
2560 if (!dir->i_op->rmdir)
2563 mutex_lock(&dentry->d_inode->i_mutex);
2566 if (d_mountpoint(dentry))
2569 error = security_inode_rmdir(dir, dentry);
2573 shrink_dcache_parent(dentry);
2574 error = dir->i_op->rmdir(dir, dentry);
2578 dentry->d_inode->i_flags |= S_DEAD;
2582 mutex_unlock(&dentry->d_inode->i_mutex);
2588 static long do_rmdir(int dfd, const char __user *pathname)
2592 struct dentry *dentry;
2593 struct nameidata nd;
2595 error = user_path_parent(dfd, pathname, &nd, &name);
2599 switch(nd.last_type) {
2611 nd.flags &= ~LOOKUP_PARENT;
2613 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2614 dentry = lookup_hash(&nd);
2615 error = PTR_ERR(dentry);
2618 if (!dentry->d_inode) {
2622 error = mnt_want_write(nd.path.mnt);
2625 error = security_path_rmdir(&nd.path, dentry);
2628 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2630 mnt_drop_write(nd.path.mnt);
2634 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2641 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
2643 return do_rmdir(AT_FDCWD, pathname);
2646 int vfs_unlink(struct inode *dir, struct dentry *dentry)
2648 int error = may_delete(dir, dentry, 0);
2653 if (!dir->i_op->unlink)
2656 mutex_lock(&dentry->d_inode->i_mutex);
2657 if (d_mountpoint(dentry))
2660 error = security_inode_unlink(dir, dentry);
2662 error = dir->i_op->unlink(dir, dentry);
2667 mutex_unlock(&dentry->d_inode->i_mutex);
2669 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2670 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2671 fsnotify_link_count(dentry->d_inode);
2679 * Make sure that the actual truncation of the file will occur outside its
2680 * directory's i_mutex. Truncate can take a long time if there is a lot of
2681 * writeout happening, and we don't want to prevent access to the directory
2682 * while waiting on the I/O.
2684 static long do_unlinkat(int dfd, const char __user *pathname)
2688 struct dentry *dentry;
2689 struct nameidata nd;
2690 struct inode *inode = NULL;
2692 error = user_path_parent(dfd, pathname, &nd, &name);
2697 if (nd.last_type != LAST_NORM)
2700 nd.flags &= ~LOOKUP_PARENT;
2702 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2703 dentry = lookup_hash(&nd);
2704 error = PTR_ERR(dentry);
2705 if (!IS_ERR(dentry)) {
2706 /* Why not before? Because we want correct error value */
2707 if (nd.last.name[nd.last.len])
2709 inode = dentry->d_inode;
2713 error = mnt_want_write(nd.path.mnt);
2716 error = security_path_unlink(&nd.path, dentry);
2719 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2721 mnt_drop_write(nd.path.mnt);
2725 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2727 iput(inode); /* truncate the inode here */
2734 error = !dentry->d_inode ? -ENOENT :
2735 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2739 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
2741 if ((flag & ~AT_REMOVEDIR) != 0)
2744 if (flag & AT_REMOVEDIR)
2745 return do_rmdir(dfd, pathname);
2747 return do_unlinkat(dfd, pathname);
2750 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
2752 return do_unlinkat(AT_FDCWD, pathname);
2755 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
2757 int error = may_create(dir, dentry);
2762 if (!dir->i_op->symlink)
2765 error = security_inode_symlink(dir, dentry, oldname);
2769 error = dir->i_op->symlink(dir, dentry, oldname);
2771 fsnotify_create(dir, dentry);
2775 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2776 int, newdfd, const char __user *, newname)
2780 struct dentry *dentry;
2783 from = getname(oldname);
2785 return PTR_ERR(from);
2787 dentry = user_path_create(newdfd, newname, &path, 0);
2788 error = PTR_ERR(dentry);
2792 error = mnt_want_write(path.mnt);
2795 error = security_path_symlink(&path, dentry, from);
2797 goto out_drop_write;
2798 error = vfs_symlink(path.dentry->d_inode, dentry, from);
2800 mnt_drop_write(path.mnt);
2803 mutex_unlock(&path.dentry->d_inode->i_mutex);
2810 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
2812 return sys_symlinkat(oldname, AT_FDCWD, newname);
2815 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2817 struct inode *inode = old_dentry->d_inode;
2823 error = may_create(dir, new_dentry);
2827 if (dir->i_sb != inode->i_sb)
2831 * A link to an append-only or immutable file cannot be created.
2833 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2835 if (!dir->i_op->link)
2837 if (S_ISDIR(inode->i_mode))
2840 error = security_inode_link(old_dentry, dir, new_dentry);
2844 mutex_lock(&inode->i_mutex);
2845 /* Make sure we don't allow creating hardlink to an unlinked file */
2846 if (inode->i_nlink == 0)
2849 error = dir->i_op->link(old_dentry, dir, new_dentry);
2850 mutex_unlock(&inode->i_mutex);
2852 fsnotify_link(dir, inode, new_dentry);
2857 * Hardlinks are often used in delicate situations. We avoid
2858 * security-related surprises by not following symlinks on the
2861 * We don't follow them on the oldname either to be compatible
2862 * with linux 2.0, and to avoid hard-linking to directories
2863 * and other special files. --ADM
2865 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2866 int, newdfd, const char __user *, newname, int, flags)
2868 struct dentry *new_dentry;
2869 struct path old_path, new_path;
2873 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
2876 * To use null names we require CAP_DAC_READ_SEARCH
2877 * This ensures that not everyone will be able to create
2878 * handlink using the passed filedescriptor.
2880 if (flags & AT_EMPTY_PATH) {
2881 if (!capable(CAP_DAC_READ_SEARCH))
2886 if (flags & AT_SYMLINK_FOLLOW)
2887 how |= LOOKUP_FOLLOW;
2889 error = user_path_at(olddfd, oldname, how, &old_path);
2893 new_dentry = user_path_create(newdfd, newname, &new_path, 0);
2894 error = PTR_ERR(new_dentry);
2895 if (IS_ERR(new_dentry))
2899 if (old_path.mnt != new_path.mnt)
2901 error = mnt_want_write(new_path.mnt);
2904 error = security_path_link(old_path.dentry, &new_path, new_dentry);
2906 goto out_drop_write;
2907 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
2909 mnt_drop_write(new_path.mnt);
2912 mutex_unlock(&new_path.dentry->d_inode->i_mutex);
2913 path_put(&new_path);
2915 path_put(&old_path);
2920 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
2922 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
2926 * The worst of all namespace operations - renaming directory. "Perverted"
2927 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2929 * a) we can get into loop creation. Check is done in is_subdir().
2930 * b) race potential - two innocent renames can create a loop together.
2931 * That's where 4.4 screws up. Current fix: serialization on
2932 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
2934 * c) we have to lock _three_ objects - parents and victim (if it exists).
2935 * And that - after we got ->i_mutex on parents (until then we don't know
2936 * whether the target exists). Solution: try to be smart with locking
2937 * order for inodes. We rely on the fact that tree topology may change
2938 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
2939 * move will be locked. Thus we can rank directories by the tree
2940 * (ancestors first) and rank all non-directories after them.
2941 * That works since everybody except rename does "lock parent, lookup,
2942 * lock child" and rename is under ->s_vfs_rename_mutex.
2943 * HOWEVER, it relies on the assumption that any object with ->lookup()
2944 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2945 * we'd better make sure that there's no link(2) for them.
2946 * d) conversion from fhandle to dentry may come in the wrong moment - when
2947 * we are removing the target. Solution: we will have to grab ->i_mutex
2948 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2949 * ->i_mutex on parents, which works but leads to some truly excessive
2952 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2953 struct inode *new_dir, struct dentry *new_dentry)
2956 struct inode *target = new_dentry->d_inode;
2959 * If we are going to change the parent - check write permissions,
2960 * we'll need to flip '..'.
2962 if (new_dir != old_dir) {
2963 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
2968 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2973 mutex_lock(&target->i_mutex);
2976 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
2980 shrink_dcache_parent(new_dentry);
2981 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2986 target->i_flags |= S_DEAD;
2987 dont_mount(new_dentry);
2991 mutex_unlock(&target->i_mutex);
2993 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2994 d_move(old_dentry,new_dentry);
2998 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2999 struct inode *new_dir, struct dentry *new_dentry)
3001 struct inode *target = new_dentry->d_inode;
3004 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3010 mutex_lock(&target->i_mutex);
3013 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3016 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3021 dont_mount(new_dentry);
3022 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3023 d_move(old_dentry, new_dentry);
3026 mutex_unlock(&target->i_mutex);
3031 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3032 struct inode *new_dir, struct dentry *new_dentry)
3035 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
3036 const unsigned char *old_name;
3038 if (old_dentry->d_inode == new_dentry->d_inode)
3041 error = may_delete(old_dir, old_dentry, is_dir);
3045 if (!new_dentry->d_inode)
3046 error = may_create(new_dir, new_dentry);
3048 error = may_delete(new_dir, new_dentry, is_dir);
3052 if (!old_dir->i_op->rename)
3055 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3058 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3060 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3062 fsnotify_move(old_dir, new_dir, old_name, is_dir,
3063 new_dentry->d_inode, old_dentry);
3064 fsnotify_oldname_free(old_name);
3069 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3070 int, newdfd, const char __user *, newname)
3072 struct dentry *old_dir, *new_dir;
3073 struct dentry *old_dentry, *new_dentry;
3074 struct dentry *trap;
3075 struct nameidata oldnd, newnd;
3080 error = user_path_parent(olddfd, oldname, &oldnd, &from);
3084 error = user_path_parent(newdfd, newname, &newnd, &to);
3089 if (oldnd.path.mnt != newnd.path.mnt)
3092 old_dir = oldnd.path.dentry;
3094 if (oldnd.last_type != LAST_NORM)
3097 new_dir = newnd.path.dentry;
3098 if (newnd.last_type != LAST_NORM)
3101 oldnd.flags &= ~LOOKUP_PARENT;
3102 newnd.flags &= ~LOOKUP_PARENT;
3103 newnd.flags |= LOOKUP_RENAME_TARGET;
3105 trap = lock_rename(new_dir, old_dir);
3107 old_dentry = lookup_hash(&oldnd);
3108 error = PTR_ERR(old_dentry);
3109 if (IS_ERR(old_dentry))
3111 /* source must exist */
3113 if (!old_dentry->d_inode)
3115 /* unless the source is a directory trailing slashes give -ENOTDIR */
3116 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3118 if (oldnd.last.name[oldnd.last.len])
3120 if (newnd.last.name[newnd.last.len])
3123 /* source should not be ancestor of target */
3125 if (old_dentry == trap)
3127 new_dentry = lookup_hash(&newnd);
3128 error = PTR_ERR(new_dentry);
3129 if (IS_ERR(new_dentry))
3131 /* target should not be an ancestor of source */
3133 if (new_dentry == trap)
3136 error = mnt_want_write(oldnd.path.mnt);
3139 error = security_path_rename(&oldnd.path, old_dentry,
3140 &newnd.path, new_dentry);
3143 error = vfs_rename(old_dir->d_inode, old_dentry,
3144 new_dir->d_inode, new_dentry);
3146 mnt_drop_write(oldnd.path.mnt);
3152 unlock_rename(new_dir, old_dir);
3154 path_put(&newnd.path);
3157 path_put(&oldnd.path);
3163 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
3165 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3168 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3172 len = PTR_ERR(link);
3177 if (len > (unsigned) buflen)
3179 if (copy_to_user(buffer, link, len))
3186 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3187 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3188 * using) it for any given inode is up to filesystem.
3190 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3192 struct nameidata nd;
3197 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3199 return PTR_ERR(cookie);
3201 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3202 if (dentry->d_inode->i_op->put_link)
3203 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3207 int vfs_follow_link(struct nameidata *nd, const char *link)
3209 return __vfs_follow_link(nd, link);
3212 /* get the link contents into pagecache */
3213 static char *page_getlink(struct dentry * dentry, struct page **ppage)
3217 struct address_space *mapping = dentry->d_inode->i_mapping;
3218 page = read_mapping_page(mapping, 0, NULL);
3223 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3227 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3229 struct page *page = NULL;
3230 char *s = page_getlink(dentry, &page);
3231 int res = vfs_readlink(dentry,buffer,buflen,s);
3234 page_cache_release(page);
3239 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
3241 struct page *page = NULL;
3242 nd_set_link(nd, page_getlink(dentry, &page));
3246 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
3248 struct page *page = cookie;
3252 page_cache_release(page);
3257 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3259 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
3261 struct address_space *mapping = inode->i_mapping;
3266 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3268 flags |= AOP_FLAG_NOFS;
3271 err = pagecache_write_begin(NULL, mapping, 0, len-1,
3272 flags, &page, &fsdata);
3276 kaddr = kmap_atomic(page, KM_USER0);
3277 memcpy(kaddr, symname, len-1);
3278 kunmap_atomic(kaddr, KM_USER0);
3280 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3287 mark_inode_dirty(inode);
3293 int page_symlink(struct inode *inode, const char *symname, int len)
3295 return __page_symlink(inode, symname, len,
3296 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
3299 const struct inode_operations page_symlink_inode_operations = {
3300 .readlink = generic_readlink,
3301 .follow_link = page_follow_link_light,
3302 .put_link = page_put_link,
3305 EXPORT_SYMBOL(user_path_at);
3306 EXPORT_SYMBOL(follow_down_one);
3307 EXPORT_SYMBOL(follow_down);
3308 EXPORT_SYMBOL(follow_up);
3309 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3310 EXPORT_SYMBOL(getname);
3311 EXPORT_SYMBOL(lock_rename);
3312 EXPORT_SYMBOL(lookup_one_len);
3313 EXPORT_SYMBOL(page_follow_link_light);
3314 EXPORT_SYMBOL(page_put_link);
3315 EXPORT_SYMBOL(page_readlink);
3316 EXPORT_SYMBOL(__page_symlink);
3317 EXPORT_SYMBOL(page_symlink);
3318 EXPORT_SYMBOL(page_symlink_inode_operations);
3319 EXPORT_SYMBOL(kern_path);
3320 EXPORT_SYMBOL(vfs_path_lookup);
3321 EXPORT_SYMBOL(inode_permission);
3322 EXPORT_SYMBOL(unlock_rename);
3323 EXPORT_SYMBOL(vfs_create);
3324 EXPORT_SYMBOL(vfs_follow_link);
3325 EXPORT_SYMBOL(vfs_link);
3326 EXPORT_SYMBOL(vfs_mkdir);
3327 EXPORT_SYMBOL(vfs_mknod);
3328 EXPORT_SYMBOL(generic_permission);
3329 EXPORT_SYMBOL(vfs_readlink);
3330 EXPORT_SYMBOL(vfs_rename);
3331 EXPORT_SYMBOL(vfs_rmdir);
3332 EXPORT_SYMBOL(vfs_symlink);
3333 EXPORT_SYMBOL(vfs_unlink);
3334 EXPORT_SYMBOL(dentry_unhash);
3335 EXPORT_SYMBOL(generic_readlink);