1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
9 * Some corrections by tytso.
12 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
15 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
18 #include <linux/init.h>
19 #include <linux/export.h>
20 #include <linux/kernel.h>
21 #include <linux/slab.h>
23 #include <linux/namei.h>
24 #include <linux/pagemap.h>
25 #include <linux/fsnotify.h>
26 #include <linux/personality.h>
27 #include <linux/security.h>
28 #include <linux/ima.h>
29 #include <linux/syscalls.h>
30 #include <linux/mount.h>
31 #include <linux/audit.h>
32 #include <linux/capability.h>
33 #include <linux/file.h>
34 #include <linux/fcntl.h>
35 #include <linux/device_cgroup.h>
36 #include <linux/fs_struct.h>
37 #include <linux/posix_acl.h>
38 #include <linux/hash.h>
39 #include <linux/bitops.h>
40 #include <linux/init_task.h>
41 #include <linux/uaccess.h>
46 /* [Feb-1997 T. Schoebel-Theuer]
47 * Fundamental changes in the pathname lookup mechanisms (namei)
48 * were necessary because of omirr. The reason is that omirr needs
49 * to know the _real_ pathname, not the user-supplied one, in case
50 * of symlinks (and also when transname replacements occur).
52 * The new code replaces the old recursive symlink resolution with
53 * an iterative one (in case of non-nested symlink chains). It does
54 * this with calls to <fs>_follow_link().
55 * As a side effect, dir_namei(), _namei() and follow_link() are now
56 * replaced with a single function lookup_dentry() that can handle all
57 * the special cases of the former code.
59 * With the new dcache, the pathname is stored at each inode, at least as
60 * long as the refcount of the inode is positive. As a side effect, the
61 * size of the dcache depends on the inode cache and thus is dynamic.
63 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
64 * resolution to correspond with current state of the code.
66 * Note that the symlink resolution is not *completely* iterative.
67 * There is still a significant amount of tail- and mid- recursion in
68 * the algorithm. Also, note that <fs>_readlink() is not used in
69 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
70 * may return different results than <fs>_follow_link(). Many virtual
71 * filesystems (including /proc) exhibit this behavior.
74 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
75 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
76 * and the name already exists in form of a symlink, try to create the new
77 * name indicated by the symlink. The old code always complained that the
78 * name already exists, due to not following the symlink even if its target
79 * is nonexistent. The new semantics affects also mknod() and link() when
80 * the name is a symlink pointing to a non-existent name.
82 * I don't know which semantics is the right one, since I have no access
83 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
84 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
85 * "old" one. Personally, I think the new semantics is much more logical.
86 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
87 * file does succeed in both HP-UX and SunOs, but not in Solaris
88 * and in the old Linux semantics.
91 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
92 * semantics. See the comments in "open_namei" and "do_link" below.
94 * [10-Sep-98 Alan Modra] Another symlink change.
97 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
98 * inside the path - always follow.
99 * in the last component in creation/removal/renaming - never follow.
100 * if LOOKUP_FOLLOW passed - follow.
101 * if the pathname has trailing slashes - follow.
102 * otherwise - don't follow.
103 * (applied in that order).
105 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
106 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
107 * During the 2.4 we need to fix the userland stuff depending on it -
108 * hopefully we will be able to get rid of that wart in 2.5. So far only
109 * XEmacs seems to be relying on it...
112 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
113 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
114 * any extra contention...
117 /* In order to reduce some races, while at the same time doing additional
118 * checking and hopefully speeding things up, we copy filenames to the
119 * kernel data space before using them..
121 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
122 * PATH_MAX includes the nul terminator --RR.
125 #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
128 getname_flags(const char __user *filename, int flags, int *empty)
130 struct filename *result;
134 result = audit_reusename(filename);
138 result = __getname();
139 if (unlikely(!result))
140 return ERR_PTR(-ENOMEM);
143 * First, try to embed the struct filename inside the names_cache
146 kname = (char *)result->iname;
147 result->name = kname;
149 len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
150 if (unlikely(len < 0)) {
156 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
157 * separate struct filename so we can dedicate the entire
158 * names_cache allocation for the pathname, and re-do the copy from
161 if (unlikely(len == EMBEDDED_NAME_MAX)) {
162 const size_t size = offsetof(struct filename, iname[1]);
163 kname = (char *)result;
166 * size is chosen that way we to guarantee that
167 * result->iname[0] is within the same object and that
168 * kname can't be equal to result->iname, no matter what.
170 result = kzalloc(size, GFP_KERNEL);
171 if (unlikely(!result)) {
173 return ERR_PTR(-ENOMEM);
175 result->name = kname;
176 len = strncpy_from_user(kname, filename, PATH_MAX);
177 if (unlikely(len < 0)) {
182 if (unlikely(len == PATH_MAX)) {
185 return ERR_PTR(-ENAMETOOLONG);
190 /* The empty path is special. */
191 if (unlikely(!len)) {
194 if (!(flags & LOOKUP_EMPTY)) {
196 return ERR_PTR(-ENOENT);
200 result->uptr = filename;
201 result->aname = NULL;
202 audit_getname(result);
207 getname(const char __user * filename)
209 return getname_flags(filename, 0, NULL);
213 getname_kernel(const char * filename)
215 struct filename *result;
216 int len = strlen(filename) + 1;
218 result = __getname();
219 if (unlikely(!result))
220 return ERR_PTR(-ENOMEM);
222 if (len <= EMBEDDED_NAME_MAX) {
223 result->name = (char *)result->iname;
224 } else if (len <= PATH_MAX) {
225 const size_t size = offsetof(struct filename, iname[1]);
226 struct filename *tmp;
228 tmp = kmalloc(size, GFP_KERNEL);
229 if (unlikely(!tmp)) {
231 return ERR_PTR(-ENOMEM);
233 tmp->name = (char *)result;
237 return ERR_PTR(-ENAMETOOLONG);
239 memcpy((char *)result->name, filename, len);
241 result->aname = NULL;
243 audit_getname(result);
248 void putname(struct filename *name)
250 BUG_ON(name->refcnt <= 0);
252 if (--name->refcnt > 0)
255 if (name->name != name->iname) {
256 __putname(name->name);
262 static int check_acl(struct inode *inode, int mask)
264 #ifdef CONFIG_FS_POSIX_ACL
265 struct posix_acl *acl;
267 if (mask & MAY_NOT_BLOCK) {
268 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
271 /* no ->get_acl() calls in RCU mode... */
272 if (is_uncached_acl(acl))
274 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
277 acl = get_acl(inode, ACL_TYPE_ACCESS);
281 int error = posix_acl_permission(inode, acl, mask);
282 posix_acl_release(acl);
291 * This does the basic permission checking
293 static int acl_permission_check(struct inode *inode, int mask)
295 unsigned int mode = inode->i_mode;
297 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
300 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
301 int error = check_acl(inode, mask);
302 if (error != -EAGAIN)
306 if (in_group_p(inode->i_gid))
311 * If the DACs are ok we don't need any capability check.
313 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
319 * generic_permission - check for access rights on a Posix-like filesystem
320 * @inode: inode to check access rights for
321 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
323 * Used to check for read/write/execute permissions on a file.
324 * We use "fsuid" for this, letting us set arbitrary permissions
325 * for filesystem access without changing the "normal" uids which
326 * are used for other things.
328 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
329 * request cannot be satisfied (eg. requires blocking or too much complexity).
330 * It would then be called again in ref-walk mode.
332 int generic_permission(struct inode *inode, int mask)
337 * Do the basic permission checks.
339 ret = acl_permission_check(inode, mask);
343 if (S_ISDIR(inode->i_mode)) {
344 /* DACs are overridable for directories */
345 if (!(mask & MAY_WRITE))
346 if (capable_wrt_inode_uidgid(inode,
347 CAP_DAC_READ_SEARCH))
349 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
355 * Searching includes executable on directories, else just read.
357 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
358 if (mask == MAY_READ)
359 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
362 * Read/write DACs are always overridable.
363 * Executable DACs are overridable when there is
364 * at least one exec bit set.
366 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
367 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
372 EXPORT_SYMBOL(generic_permission);
375 * We _really_ want to just do "generic_permission()" without
376 * even looking at the inode->i_op values. So we keep a cache
377 * flag in inode->i_opflags, that says "this has not special
378 * permission function, use the fast case".
380 static inline int do_inode_permission(struct inode *inode, int mask)
382 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
383 if (likely(inode->i_op->permission))
384 return inode->i_op->permission(inode, mask);
386 /* This gets set once for the inode lifetime */
387 spin_lock(&inode->i_lock);
388 inode->i_opflags |= IOP_FASTPERM;
389 spin_unlock(&inode->i_lock);
391 return generic_permission(inode, mask);
395 * sb_permission - Check superblock-level permissions
396 * @sb: Superblock of inode to check permission on
397 * @inode: Inode to check permission on
398 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
400 * Separate out file-system wide checks from inode-specific permission checks.
402 static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
404 if (unlikely(mask & MAY_WRITE)) {
405 umode_t mode = inode->i_mode;
407 /* Nobody gets write access to a read-only fs. */
408 if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
415 * inode_permission - Check for access rights to a given inode
416 * @inode: Inode to check permission on
417 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
419 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
420 * this, letting us set arbitrary permissions for filesystem access without
421 * changing the "normal" UIDs which are used for other things.
423 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
425 int inode_permission(struct inode *inode, int mask)
429 retval = sb_permission(inode->i_sb, inode, mask);
433 if (unlikely(mask & MAY_WRITE)) {
435 * Nobody gets write access to an immutable file.
437 if (IS_IMMUTABLE(inode))
441 * Updating mtime will likely cause i_uid and i_gid to be
442 * written back improperly if their true value is unknown
445 if (HAS_UNMAPPED_ID(inode))
449 retval = do_inode_permission(inode, mask);
453 retval = devcgroup_inode_permission(inode, mask);
457 return security_inode_permission(inode, mask);
459 EXPORT_SYMBOL(inode_permission);
462 * path_get - get a reference to a path
463 * @path: path to get the reference to
465 * Given a path increment the reference count to the dentry and the vfsmount.
467 void path_get(const struct path *path)
472 EXPORT_SYMBOL(path_get);
475 * path_put - put a reference to a path
476 * @path: path to put the reference to
478 * Given a path decrement the reference count to the dentry and the vfsmount.
480 void path_put(const struct path *path)
485 EXPORT_SYMBOL(path_put);
487 #define EMBEDDED_LEVELS 2
492 struct inode *inode; /* path.dentry.d_inode */
497 int total_link_count;
500 struct delayed_call done;
503 } *stack, internal[EMBEDDED_LEVELS];
504 struct filename *name;
505 struct nameidata *saved;
506 struct inode *link_inode;
509 } __randomize_layout;
511 static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
513 struct nameidata *old = current->nameidata;
514 p->stack = p->internal;
517 p->total_link_count = old ? old->total_link_count : 0;
519 current->nameidata = p;
522 static void restore_nameidata(void)
524 struct nameidata *now = current->nameidata, *old = now->saved;
526 current->nameidata = old;
528 old->total_link_count = now->total_link_count;
529 if (now->stack != now->internal)
533 static int __nd_alloc_stack(struct nameidata *nd)
537 if (nd->flags & LOOKUP_RCU) {
538 p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved),
543 p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved),
548 memcpy(p, nd->internal, sizeof(nd->internal));
554 * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
555 * @path: nameidate to verify
557 * Rename can sometimes move a file or directory outside of a bind
558 * mount, path_connected allows those cases to be detected.
560 static bool path_connected(const struct path *path)
562 struct vfsmount *mnt = path->mnt;
563 struct super_block *sb = mnt->mnt_sb;
565 /* Bind mounts and multi-root filesystems can have disconnected paths */
566 if (!(sb->s_iflags & SB_I_MULTIROOT) && (mnt->mnt_root == sb->s_root))
569 return is_subdir(path->dentry, mnt->mnt_root);
572 static inline int nd_alloc_stack(struct nameidata *nd)
574 if (likely(nd->depth != EMBEDDED_LEVELS))
576 if (likely(nd->stack != nd->internal))
578 return __nd_alloc_stack(nd);
581 static void drop_links(struct nameidata *nd)
585 struct saved *last = nd->stack + i;
586 do_delayed_call(&last->done);
587 clear_delayed_call(&last->done);
591 static void terminate_walk(struct nameidata *nd)
594 if (!(nd->flags & LOOKUP_RCU)) {
597 for (i = 0; i < nd->depth; i++)
598 path_put(&nd->stack[i].link);
599 if (nd->flags & LOOKUP_ROOT_GRABBED) {
601 nd->flags &= ~LOOKUP_ROOT_GRABBED;
604 nd->flags &= ~LOOKUP_RCU;
610 /* path_put is needed afterwards regardless of success or failure */
611 static bool legitimize_path(struct nameidata *nd,
612 struct path *path, unsigned seq)
614 int res = __legitimize_mnt(path->mnt, nd->m_seq);
621 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
625 return !read_seqcount_retry(&path->dentry->d_seq, seq);
628 static bool legitimize_links(struct nameidata *nd)
631 for (i = 0; i < nd->depth; i++) {
632 struct saved *last = nd->stack + i;
633 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
642 static bool legitimize_root(struct nameidata *nd)
644 if (!nd->root.mnt || (nd->flags & LOOKUP_ROOT))
646 nd->flags |= LOOKUP_ROOT_GRABBED;
647 return legitimize_path(nd, &nd->root, nd->root_seq);
651 * Path walking has 2 modes, rcu-walk and ref-walk (see
652 * Documentation/filesystems/path-lookup.txt). In situations when we can't
653 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
654 * normal reference counts on dentries and vfsmounts to transition to ref-walk
655 * mode. Refcounts are grabbed at the last known good point before rcu-walk
656 * got stuck, so ref-walk may continue from there. If this is not successful
657 * (eg. a seqcount has changed), then failure is returned and it's up to caller
658 * to restart the path walk from the beginning in ref-walk mode.
662 * unlazy_walk - try to switch to ref-walk mode.
663 * @nd: nameidata pathwalk data
664 * Returns: 0 on success, -ECHILD on failure
666 * unlazy_walk attempts to legitimize the current nd->path and nd->root
668 * Must be called from rcu-walk context.
669 * Nothing should touch nameidata between unlazy_walk() failure and
672 static int unlazy_walk(struct nameidata *nd)
674 struct dentry *parent = nd->path.dentry;
676 BUG_ON(!(nd->flags & LOOKUP_RCU));
678 nd->flags &= ~LOOKUP_RCU;
679 if (unlikely(!legitimize_links(nd)))
681 if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
683 if (unlikely(!legitimize_root(nd)))
686 BUG_ON(nd->inode != parent->d_inode);
691 nd->path.dentry = NULL;
698 * unlazy_child - try to switch to ref-walk mode.
699 * @nd: nameidata pathwalk data
700 * @dentry: child of nd->path.dentry
701 * @seq: seq number to check dentry against
702 * Returns: 0 on success, -ECHILD on failure
704 * unlazy_child attempts to legitimize the current nd->path, nd->root and dentry
705 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
706 * @nd. Must be called from rcu-walk context.
707 * Nothing should touch nameidata between unlazy_child() failure and
710 static int unlazy_child(struct nameidata *nd, struct dentry *dentry, unsigned seq)
712 BUG_ON(!(nd->flags & LOOKUP_RCU));
714 nd->flags &= ~LOOKUP_RCU;
715 if (unlikely(!legitimize_links(nd)))
717 if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
719 if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref)))
723 * We need to move both the parent and the dentry from the RCU domain
724 * to be properly refcounted. And the sequence number in the dentry
725 * validates *both* dentry counters, since we checked the sequence
726 * number of the parent after we got the child sequence number. So we
727 * know the parent must still be valid if the child sequence number is
729 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref)))
731 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
734 * Sequence counts matched. Now make sure that the root is
735 * still valid and get it if required.
737 if (unlikely(!legitimize_root(nd)))
745 nd->path.dentry = NULL;
755 static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
757 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
758 return dentry->d_op->d_revalidate(dentry, flags);
764 * complete_walk - successful completion of path walk
765 * @nd: pointer nameidata
767 * If we had been in RCU mode, drop out of it and legitimize nd->path.
768 * Revalidate the final result, unless we'd already done that during
769 * the path walk or the filesystem doesn't ask for it. Return 0 on
770 * success, -error on failure. In case of failure caller does not
771 * need to drop nd->path.
773 static int complete_walk(struct nameidata *nd)
775 struct dentry *dentry = nd->path.dentry;
778 if (nd->flags & LOOKUP_RCU) {
779 if (!(nd->flags & LOOKUP_ROOT))
781 if (unlikely(unlazy_walk(nd)))
785 if (likely(!(nd->flags & LOOKUP_JUMPED)))
788 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
791 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
801 static void set_root(struct nameidata *nd)
803 struct fs_struct *fs = current->fs;
805 if (nd->flags & LOOKUP_RCU) {
809 seq = read_seqcount_begin(&fs->seq);
811 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
812 } while (read_seqcount_retry(&fs->seq, seq));
814 get_fs_root(fs, &nd->root);
815 nd->flags |= LOOKUP_ROOT_GRABBED;
819 static void path_put_conditional(struct path *path, struct nameidata *nd)
822 if (path->mnt != nd->path.mnt)
826 static inline void path_to_nameidata(const struct path *path,
827 struct nameidata *nd)
829 if (!(nd->flags & LOOKUP_RCU)) {
830 dput(nd->path.dentry);
831 if (nd->path.mnt != path->mnt)
832 mntput(nd->path.mnt);
834 nd->path.mnt = path->mnt;
835 nd->path.dentry = path->dentry;
838 static int nd_jump_root(struct nameidata *nd)
840 if (nd->flags & LOOKUP_RCU) {
844 nd->inode = d->d_inode;
845 nd->seq = nd->root_seq;
846 if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
852 nd->inode = nd->path.dentry->d_inode;
854 nd->flags |= LOOKUP_JUMPED;
859 * Helper to directly jump to a known parsed path from ->get_link,
860 * caller must have taken a reference to path beforehand.
862 void nd_jump_link(struct path *path)
864 struct nameidata *nd = current->nameidata;
868 nd->inode = nd->path.dentry->d_inode;
869 nd->flags |= LOOKUP_JUMPED;
872 static inline void put_link(struct nameidata *nd)
874 struct saved *last = nd->stack + --nd->depth;
875 do_delayed_call(&last->done);
876 if (!(nd->flags & LOOKUP_RCU))
877 path_put(&last->link);
880 int sysctl_protected_symlinks __read_mostly = 0;
881 int sysctl_protected_hardlinks __read_mostly = 0;
882 int sysctl_protected_fifos __read_mostly;
883 int sysctl_protected_regular __read_mostly;
886 * may_follow_link - Check symlink following for unsafe situations
887 * @nd: nameidata pathwalk data
889 * In the case of the sysctl_protected_symlinks sysctl being enabled,
890 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
891 * in a sticky world-writable directory. This is to protect privileged
892 * processes from failing races against path names that may change out
893 * from under them by way of other users creating malicious symlinks.
894 * It will permit symlinks to be followed only when outside a sticky
895 * world-writable directory, or when the uid of the symlink and follower
896 * match, or when the directory owner matches the symlink's owner.
898 * Returns 0 if following the symlink is allowed, -ve on error.
900 static inline int may_follow_link(struct nameidata *nd)
902 const struct inode *inode;
903 const struct inode *parent;
906 if (!sysctl_protected_symlinks)
909 /* Allowed if owner and follower match. */
910 inode = nd->link_inode;
911 if (uid_eq(current_cred()->fsuid, inode->i_uid))
914 /* Allowed if parent directory not sticky and world-writable. */
916 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
919 /* Allowed if parent directory and link owner match. */
920 puid = parent->i_uid;
921 if (uid_valid(puid) && uid_eq(puid, inode->i_uid))
924 if (nd->flags & LOOKUP_RCU)
927 audit_inode(nd->name, nd->stack[0].link.dentry, 0);
928 audit_log_link_denied("follow_link");
933 * safe_hardlink_source - Check for safe hardlink conditions
934 * @inode: the source inode to hardlink from
936 * Return false if at least one of the following conditions:
937 * - inode is not a regular file
939 * - inode is setgid and group-exec
940 * - access failure for read and write
942 * Otherwise returns true.
944 static bool safe_hardlink_source(struct inode *inode)
946 umode_t mode = inode->i_mode;
948 /* Special files should not get pinned to the filesystem. */
952 /* Setuid files should not get pinned to the filesystem. */
956 /* Executable setgid files should not get pinned to the filesystem. */
957 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
960 /* Hardlinking to unreadable or unwritable sources is dangerous. */
961 if (inode_permission(inode, MAY_READ | MAY_WRITE))
968 * may_linkat - Check permissions for creating a hardlink
969 * @link: the source to hardlink from
971 * Block hardlink when all of:
972 * - sysctl_protected_hardlinks enabled
973 * - fsuid does not match inode
974 * - hardlink source is unsafe (see safe_hardlink_source() above)
975 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
977 * Returns 0 if successful, -ve on error.
979 static int may_linkat(struct path *link)
981 struct inode *inode = link->dentry->d_inode;
983 /* Inode writeback is not safe when the uid or gid are invalid. */
984 if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
987 if (!sysctl_protected_hardlinks)
990 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
991 * otherwise, it must be a safe source.
993 if (safe_hardlink_source(inode) || inode_owner_or_capable(inode))
996 audit_log_link_denied("linkat");
1001 * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
1002 * should be allowed, or not, on files that already
1004 * @dir_mode: mode bits of directory
1005 * @dir_uid: owner of directory
1006 * @inode: the inode of the file to open
1008 * Block an O_CREAT open of a FIFO (or a regular file) when:
1009 * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
1010 * - the file already exists
1011 * - we are in a sticky directory
1012 * - we don't own the file
1013 * - the owner of the directory doesn't own the file
1014 * - the directory is world writable
1015 * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
1016 * the directory doesn't have to be world writable: being group writable will
1019 * Returns 0 if the open is allowed, -ve on error.
1021 static int may_create_in_sticky(umode_t dir_mode, kuid_t dir_uid,
1022 struct inode * const inode)
1024 if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) ||
1025 (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
1026 likely(!(dir_mode & S_ISVTX)) ||
1027 uid_eq(inode->i_uid, dir_uid) ||
1028 uid_eq(current_fsuid(), inode->i_uid))
1031 if (likely(dir_mode & 0002) ||
1033 ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) ||
1034 (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) {
1040 static __always_inline
1041 const char *get_link(struct nameidata *nd)
1043 struct saved *last = nd->stack + nd->depth - 1;
1044 struct dentry *dentry = last->link.dentry;
1045 struct inode *inode = nd->link_inode;
1049 if (!(nd->flags & LOOKUP_RCU)) {
1050 touch_atime(&last->link);
1052 } else if (atime_needs_update(&last->link, inode)) {
1053 if (unlikely(unlazy_walk(nd)))
1054 return ERR_PTR(-ECHILD);
1055 touch_atime(&last->link);
1058 error = security_inode_follow_link(dentry, inode,
1059 nd->flags & LOOKUP_RCU);
1060 if (unlikely(error))
1061 return ERR_PTR(error);
1063 nd->last_type = LAST_BIND;
1064 res = READ_ONCE(inode->i_link);
1066 const char * (*get)(struct dentry *, struct inode *,
1067 struct delayed_call *);
1068 get = inode->i_op->get_link;
1069 if (nd->flags & LOOKUP_RCU) {
1070 res = get(NULL, inode, &last->done);
1071 if (res == ERR_PTR(-ECHILD)) {
1072 if (unlikely(unlazy_walk(nd)))
1073 return ERR_PTR(-ECHILD);
1074 res = get(dentry, inode, &last->done);
1077 res = get(dentry, inode, &last->done);
1079 if (IS_ERR_OR_NULL(res))
1085 if (unlikely(nd_jump_root(nd)))
1086 return ERR_PTR(-ECHILD);
1087 while (unlikely(*++res == '/'))
1096 * follow_up - Find the mountpoint of path's vfsmount
1098 * Given a path, find the mountpoint of its source file system.
1099 * Replace @path with the path of the mountpoint in the parent mount.
1102 * Return 1 if we went up a level and 0 if we were already at the
1105 int follow_up(struct path *path)
1107 struct mount *mnt = real_mount(path->mnt);
1108 struct mount *parent;
1109 struct dentry *mountpoint;
1111 read_seqlock_excl(&mount_lock);
1112 parent = mnt->mnt_parent;
1113 if (parent == mnt) {
1114 read_sequnlock_excl(&mount_lock);
1117 mntget(&parent->mnt);
1118 mountpoint = dget(mnt->mnt_mountpoint);
1119 read_sequnlock_excl(&mount_lock);
1121 path->dentry = mountpoint;
1123 path->mnt = &parent->mnt;
1126 EXPORT_SYMBOL(follow_up);
1129 * Perform an automount
1130 * - return -EISDIR to tell follow_managed() to stop and return the path we
1133 static int follow_automount(struct path *path, struct nameidata *nd,
1136 struct vfsmount *mnt;
1139 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
1142 /* We don't want to mount if someone's just doing a stat -
1143 * unless they're stat'ing a directory and appended a '/' to
1146 * We do, however, want to mount if someone wants to open or
1147 * create a file of any type under the mountpoint, wants to
1148 * traverse through the mountpoint or wants to open the
1149 * mounted directory. Also, autofs may mark negative dentries
1150 * as being automount points. These will need the attentions
1151 * of the daemon to instantiate them before they can be used.
1153 if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
1154 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
1155 path->dentry->d_inode)
1158 nd->total_link_count++;
1159 if (nd->total_link_count >= 40)
1162 mnt = path->dentry->d_op->d_automount(path);
1165 * The filesystem is allowed to return -EISDIR here to indicate
1166 * it doesn't want to automount. For instance, autofs would do
1167 * this so that its userspace daemon can mount on this dentry.
1169 * However, we can only permit this if it's a terminal point in
1170 * the path being looked up; if it wasn't then the remainder of
1171 * the path is inaccessible and we should say so.
1173 if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT))
1175 return PTR_ERR(mnt);
1178 if (!mnt) /* mount collision */
1181 if (!*need_mntput) {
1182 /* lock_mount() may release path->mnt on error */
1184 *need_mntput = true;
1186 err = finish_automount(mnt, path);
1190 /* Someone else made a mount here whilst we were busy */
1195 path->dentry = dget(mnt->mnt_root);
1204 * Handle a dentry that is managed in some way.
1205 * - Flagged for transit management (autofs)
1206 * - Flagged as mountpoint
1207 * - Flagged as automount point
1209 * This may only be called in refwalk mode.
1211 * Serialization is taken care of in namespace.c
1213 static int follow_managed(struct path *path, struct nameidata *nd)
1215 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
1217 bool need_mntput = false;
1220 /* Given that we're not holding a lock here, we retain the value in a
1221 * local variable for each dentry as we look at it so that we don't see
1222 * the components of that value change under us */
1223 while (managed = READ_ONCE(path->dentry->d_flags),
1224 managed &= DCACHE_MANAGED_DENTRY,
1225 unlikely(managed != 0)) {
1226 /* Allow the filesystem to manage the transit without i_mutex
1228 if (managed & DCACHE_MANAGE_TRANSIT) {
1229 BUG_ON(!path->dentry->d_op);
1230 BUG_ON(!path->dentry->d_op->d_manage);
1231 ret = path->dentry->d_op->d_manage(path, false);
1236 /* Transit to a mounted filesystem. */
1237 if (managed & DCACHE_MOUNTED) {
1238 struct vfsmount *mounted = lookup_mnt(path);
1243 path->mnt = mounted;
1244 path->dentry = dget(mounted->mnt_root);
1249 /* Something is mounted on this dentry in another
1250 * namespace and/or whatever was mounted there in this
1251 * namespace got unmounted before lookup_mnt() could
1255 /* Handle an automount point */
1256 if (managed & DCACHE_NEED_AUTOMOUNT) {
1257 ret = follow_automount(path, nd, &need_mntput);
1263 /* We didn't change the current path point */
1267 if (need_mntput && path->mnt == mnt)
1269 if (ret == -EISDIR || !ret)
1272 nd->flags |= LOOKUP_JUMPED;
1273 if (unlikely(ret < 0))
1274 path_put_conditional(path, nd);
1278 int follow_down_one(struct path *path)
1280 struct vfsmount *mounted;
1282 mounted = lookup_mnt(path);
1286 path->mnt = mounted;
1287 path->dentry = dget(mounted->mnt_root);
1292 EXPORT_SYMBOL(follow_down_one);
1294 static inline int managed_dentry_rcu(const struct path *path)
1296 return (path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
1297 path->dentry->d_op->d_manage(path, true) : 0;
1301 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1302 * we meet a managed dentry that would need blocking.
1304 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1305 struct inode **inode, unsigned *seqp)
1308 struct mount *mounted;
1310 * Don't forget we might have a non-mountpoint managed dentry
1311 * that wants to block transit.
1313 switch (managed_dentry_rcu(path)) {
1323 if (!d_mountpoint(path->dentry))
1324 return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
1326 mounted = __lookup_mnt(path->mnt, path->dentry);
1329 path->mnt = &mounted->mnt;
1330 path->dentry = mounted->mnt.mnt_root;
1331 nd->flags |= LOOKUP_JUMPED;
1332 *seqp = read_seqcount_begin(&path->dentry->d_seq);
1334 * Update the inode too. We don't need to re-check the
1335 * dentry sequence number here after this d_inode read,
1336 * because a mount-point is always pinned.
1338 *inode = path->dentry->d_inode;
1340 return !read_seqretry(&mount_lock, nd->m_seq) &&
1341 !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
1344 static int follow_dotdot_rcu(struct nameidata *nd)
1346 struct inode *inode = nd->inode;
1349 if (path_equal(&nd->path, &nd->root))
1351 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1352 struct dentry *old = nd->path.dentry;
1353 struct dentry *parent = old->d_parent;
1356 inode = parent->d_inode;
1357 seq = read_seqcount_begin(&parent->d_seq);
1358 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1360 nd->path.dentry = parent;
1362 if (unlikely(!path_connected(&nd->path)))
1366 struct mount *mnt = real_mount(nd->path.mnt);
1367 struct mount *mparent = mnt->mnt_parent;
1368 struct dentry *mountpoint = mnt->mnt_mountpoint;
1369 struct inode *inode2 = mountpoint->d_inode;
1370 unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
1371 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1373 if (&mparent->mnt == nd->path.mnt)
1375 /* we know that mountpoint was pinned */
1376 nd->path.dentry = mountpoint;
1377 nd->path.mnt = &mparent->mnt;
1382 while (unlikely(d_mountpoint(nd->path.dentry))) {
1383 struct mount *mounted;
1384 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
1385 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1389 nd->path.mnt = &mounted->mnt;
1390 nd->path.dentry = mounted->mnt.mnt_root;
1391 inode = nd->path.dentry->d_inode;
1392 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1399 * Follow down to the covering mount currently visible to userspace. At each
1400 * point, the filesystem owning that dentry may be queried as to whether the
1401 * caller is permitted to proceed or not.
1403 int follow_down(struct path *path)
1408 while (managed = READ_ONCE(path->dentry->d_flags),
1409 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1410 /* Allow the filesystem to manage the transit without i_mutex
1413 * We indicate to the filesystem if someone is trying to mount
1414 * something here. This gives autofs the chance to deny anyone
1415 * other than its daemon the right to mount on its
1418 * The filesystem may sleep at this point.
1420 if (managed & DCACHE_MANAGE_TRANSIT) {
1421 BUG_ON(!path->dentry->d_op);
1422 BUG_ON(!path->dentry->d_op->d_manage);
1423 ret = path->dentry->d_op->d_manage(path, false);
1425 return ret == -EISDIR ? 0 : ret;
1428 /* Transit to a mounted filesystem. */
1429 if (managed & DCACHE_MOUNTED) {
1430 struct vfsmount *mounted = lookup_mnt(path);
1435 path->mnt = mounted;
1436 path->dentry = dget(mounted->mnt_root);
1440 /* Don't handle automount points here */
1445 EXPORT_SYMBOL(follow_down);
1448 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1450 static void follow_mount(struct path *path)
1452 while (d_mountpoint(path->dentry)) {
1453 struct vfsmount *mounted = lookup_mnt(path);
1458 path->mnt = mounted;
1459 path->dentry = dget(mounted->mnt_root);
1463 static int path_parent_directory(struct path *path)
1465 struct dentry *old = path->dentry;
1466 /* rare case of legitimate dget_parent()... */
1467 path->dentry = dget_parent(path->dentry);
1469 if (unlikely(!path_connected(path)))
1474 static int follow_dotdot(struct nameidata *nd)
1477 if (path_equal(&nd->path, &nd->root))
1479 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1480 int ret = path_parent_directory(&nd->path);
1485 if (!follow_up(&nd->path))
1488 follow_mount(&nd->path);
1489 nd->inode = nd->path.dentry->d_inode;
1494 * This looks up the name in dcache and possibly revalidates the found dentry.
1495 * NULL is returned if the dentry does not exist in the cache.
1497 static struct dentry *lookup_dcache(const struct qstr *name,
1501 struct dentry *dentry = d_lookup(dir, name);
1503 int error = d_revalidate(dentry, flags);
1504 if (unlikely(error <= 0)) {
1506 d_invalidate(dentry);
1508 return ERR_PTR(error);
1515 * Parent directory has inode locked exclusive. This is one
1516 * and only case when ->lookup() gets called on non in-lookup
1517 * dentries - as the matter of fact, this only gets called
1518 * when directory is guaranteed to have no in-lookup children
1521 static struct dentry *__lookup_hash(const struct qstr *name,
1522 struct dentry *base, unsigned int flags)
1524 struct dentry *dentry = lookup_dcache(name, base, flags);
1526 struct inode *dir = base->d_inode;
1531 /* Don't create child dentry for a dead directory. */
1532 if (unlikely(IS_DEADDIR(dir)))
1533 return ERR_PTR(-ENOENT);
1535 dentry = d_alloc(base, name);
1536 if (unlikely(!dentry))
1537 return ERR_PTR(-ENOMEM);
1539 old = dir->i_op->lookup(dir, dentry, flags);
1540 if (unlikely(old)) {
1547 static int lookup_fast(struct nameidata *nd,
1548 struct path *path, struct inode **inode,
1551 struct vfsmount *mnt = nd->path.mnt;
1552 struct dentry *dentry, *parent = nd->path.dentry;
1557 * Rename seqlock is not required here because in the off chance
1558 * of a false negative due to a concurrent rename, the caller is
1559 * going to fall back to non-racy lookup.
1561 if (nd->flags & LOOKUP_RCU) {
1564 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
1565 if (unlikely(!dentry)) {
1566 if (unlazy_walk(nd))
1572 * This sequence count validates that the inode matches
1573 * the dentry name information from lookup.
1575 *inode = d_backing_inode(dentry);
1576 negative = d_is_negative(dentry);
1577 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
1581 * This sequence count validates that the parent had no
1582 * changes while we did the lookup of the dentry above.
1584 * The memory barrier in read_seqcount_begin of child is
1585 * enough, we can use __read_seqcount_retry here.
1587 if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
1591 status = d_revalidate(dentry, nd->flags);
1592 if (likely(status > 0)) {
1594 * Note: do negative dentry check after revalidation in
1595 * case that drops it.
1597 if (unlikely(negative))
1600 path->dentry = dentry;
1601 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
1604 if (unlazy_child(nd, dentry, seq))
1606 if (unlikely(status == -ECHILD))
1607 /* we'd been told to redo it in non-rcu mode */
1608 status = d_revalidate(dentry, nd->flags);
1610 dentry = __d_lookup(parent, &nd->last);
1611 if (unlikely(!dentry))
1613 status = d_revalidate(dentry, nd->flags);
1615 if (unlikely(status <= 0)) {
1617 d_invalidate(dentry);
1621 if (unlikely(d_is_negative(dentry))) {
1627 path->dentry = dentry;
1628 err = follow_managed(path, nd);
1629 if (likely(err > 0))
1630 *inode = d_backing_inode(path->dentry);
1634 /* Fast lookup failed, do it the slow way */
1635 static struct dentry *__lookup_slow(const struct qstr *name,
1639 struct dentry *dentry, *old;
1640 struct inode *inode = dir->d_inode;
1641 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1643 /* Don't go there if it's already dead */
1644 if (unlikely(IS_DEADDIR(inode)))
1645 return ERR_PTR(-ENOENT);
1647 dentry = d_alloc_parallel(dir, name, &wq);
1650 if (unlikely(!d_in_lookup(dentry))) {
1651 if (!(flags & LOOKUP_NO_REVAL)) {
1652 int error = d_revalidate(dentry, flags);
1653 if (unlikely(error <= 0)) {
1655 d_invalidate(dentry);
1660 dentry = ERR_PTR(error);
1664 old = inode->i_op->lookup(inode, dentry, flags);
1665 d_lookup_done(dentry);
1666 if (unlikely(old)) {
1674 static struct dentry *lookup_slow(const struct qstr *name,
1678 struct inode *inode = dir->d_inode;
1680 inode_lock_shared(inode);
1681 res = __lookup_slow(name, dir, flags);
1682 inode_unlock_shared(inode);
1686 static inline int may_lookup(struct nameidata *nd)
1688 if (nd->flags & LOOKUP_RCU) {
1689 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
1692 if (unlazy_walk(nd))
1695 return inode_permission(nd->inode, MAY_EXEC);
1698 static inline int handle_dots(struct nameidata *nd, int type)
1700 if (type == LAST_DOTDOT) {
1703 if (nd->flags & LOOKUP_RCU) {
1704 return follow_dotdot_rcu(nd);
1706 return follow_dotdot(nd);
1711 static int pick_link(struct nameidata *nd, struct path *link,
1712 struct inode *inode, unsigned seq)
1716 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) {
1717 path_to_nameidata(link, nd);
1720 if (!(nd->flags & LOOKUP_RCU)) {
1721 if (link->mnt == nd->path.mnt)
1724 error = nd_alloc_stack(nd);
1725 if (unlikely(error)) {
1726 if (error == -ECHILD) {
1727 if (unlikely(!legitimize_path(nd, link, seq))) {
1730 nd->flags &= ~LOOKUP_RCU;
1731 nd->path.mnt = NULL;
1732 nd->path.dentry = NULL;
1734 } else if (likely(unlazy_walk(nd)) == 0)
1735 error = nd_alloc_stack(nd);
1743 last = nd->stack + nd->depth++;
1745 clear_delayed_call(&last->done);
1746 nd->link_inode = inode;
1751 enum {WALK_FOLLOW = 1, WALK_MORE = 2};
1754 * Do we need to follow links? We _really_ want to be able
1755 * to do this check without having to look at inode->i_op,
1756 * so we keep a cache of "no, this doesn't need follow_link"
1757 * for the common case.
1759 static inline int step_into(struct nameidata *nd, struct path *path,
1760 int flags, struct inode *inode, unsigned seq)
1762 if (!(flags & WALK_MORE) && nd->depth)
1764 if (likely(!d_is_symlink(path->dentry)) ||
1765 !(flags & WALK_FOLLOW || nd->flags & LOOKUP_FOLLOW)) {
1766 /* not a symlink or should not follow */
1767 path_to_nameidata(path, nd);
1772 /* make sure that d_is_symlink above matches inode */
1773 if (nd->flags & LOOKUP_RCU) {
1774 if (read_seqcount_retry(&path->dentry->d_seq, seq))
1777 return pick_link(nd, path, inode, seq);
1780 static int walk_component(struct nameidata *nd, int flags)
1783 struct inode *inode;
1787 * "." and ".." are special - ".." especially so because it has
1788 * to be able to know about the current root directory and
1789 * parent relationships.
1791 if (unlikely(nd->last_type != LAST_NORM)) {
1792 err = handle_dots(nd, nd->last_type);
1793 if (!(flags & WALK_MORE) && nd->depth)
1797 err = lookup_fast(nd, &path, &inode, &seq);
1798 if (unlikely(err <= 0)) {
1801 path.dentry = lookup_slow(&nd->last, nd->path.dentry,
1803 if (IS_ERR(path.dentry))
1804 return PTR_ERR(path.dentry);
1806 path.mnt = nd->path.mnt;
1807 err = follow_managed(&path, nd);
1808 if (unlikely(err < 0))
1811 if (unlikely(d_is_negative(path.dentry))) {
1812 path_to_nameidata(&path, nd);
1816 seq = 0; /* we are already out of RCU mode */
1817 inode = d_backing_inode(path.dentry);
1820 return step_into(nd, &path, flags, inode, seq);
1824 * We can do the critical dentry name comparison and hashing
1825 * operations one word at a time, but we are limited to:
1827 * - Architectures with fast unaligned word accesses. We could
1828 * do a "get_unaligned()" if this helps and is sufficiently
1831 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1832 * do not trap on the (extremely unlikely) case of a page
1833 * crossing operation.
1835 * - Furthermore, we need an efficient 64-bit compile for the
1836 * 64-bit case in order to generate the "number of bytes in
1837 * the final mask". Again, that could be replaced with a
1838 * efficient population count instruction or similar.
1840 #ifdef CONFIG_DCACHE_WORD_ACCESS
1842 #include <asm/word-at-a-time.h>
1846 /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
1848 #elif defined(CONFIG_64BIT)
1850 * Register pressure in the mixing function is an issue, particularly
1851 * on 32-bit x86, but almost any function requires one state value and
1852 * one temporary. Instead, use a function designed for two state values
1853 * and no temporaries.
1855 * This function cannot create a collision in only two iterations, so
1856 * we have two iterations to achieve avalanche. In those two iterations,
1857 * we have six layers of mixing, which is enough to spread one bit's
1858 * influence out to 2^6 = 64 state bits.
1860 * Rotate constants are scored by considering either 64 one-bit input
1861 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
1862 * probability of that delta causing a change to each of the 128 output
1863 * bits, using a sample of random initial states.
1865 * The Shannon entropy of the computed probabilities is then summed
1866 * to produce a score. Ideally, any input change has a 50% chance of
1867 * toggling any given output bit.
1869 * Mixing scores (in bits) for (12,45):
1870 * Input delta: 1-bit 2-bit
1871 * 1 round: 713.3 42542.6
1872 * 2 rounds: 2753.7 140389.8
1873 * 3 rounds: 5954.1 233458.2
1874 * 4 rounds: 7862.6 256672.2
1875 * Perfect: 8192 258048
1876 * (64*128) (64*63/2 * 128)
1878 #define HASH_MIX(x, y, a) \
1880 y ^= x, x = rol64(x,12),\
1881 x += y, y = rol64(y,45),\
1885 * Fold two longs into one 32-bit hash value. This must be fast, but
1886 * latency isn't quite as critical, as there is a fair bit of additional
1887 * work done before the hash value is used.
1889 static inline unsigned int fold_hash(unsigned long x, unsigned long y)
1891 y ^= x * GOLDEN_RATIO_64;
1892 y *= GOLDEN_RATIO_64;
1896 #else /* 32-bit case */
1899 * Mixing scores (in bits) for (7,20):
1900 * Input delta: 1-bit 2-bit
1901 * 1 round: 330.3 9201.6
1902 * 2 rounds: 1246.4 25475.4
1903 * 3 rounds: 1907.1 31295.1
1904 * 4 rounds: 2042.3 31718.6
1905 * Perfect: 2048 31744
1906 * (32*64) (32*31/2 * 64)
1908 #define HASH_MIX(x, y, a) \
1910 y ^= x, x = rol32(x, 7),\
1911 x += y, y = rol32(y,20),\
1914 static inline unsigned int fold_hash(unsigned long x, unsigned long y)
1916 /* Use arch-optimized multiply if one exists */
1917 return __hash_32(y ^ __hash_32(x));
1923 * Return the hash of a string of known length. This is carfully
1924 * designed to match hash_name(), which is the more critical function.
1925 * In particular, we must end by hashing a final word containing 0..7
1926 * payload bytes, to match the way that hash_name() iterates until it
1927 * finds the delimiter after the name.
1929 unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
1931 unsigned long a, x = 0, y = (unsigned long)salt;
1936 a = load_unaligned_zeropad(name);
1937 if (len < sizeof(unsigned long))
1940 name += sizeof(unsigned long);
1941 len -= sizeof(unsigned long);
1943 x ^= a & bytemask_from_count(len);
1945 return fold_hash(x, y);
1947 EXPORT_SYMBOL(full_name_hash);
1949 /* Return the "hash_len" (hash and length) of a null-terminated string */
1950 u64 hashlen_string(const void *salt, const char *name)
1952 unsigned long a = 0, x = 0, y = (unsigned long)salt;
1953 unsigned long adata, mask, len;
1954 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1961 len += sizeof(unsigned long);
1963 a = load_unaligned_zeropad(name+len);
1964 } while (!has_zero(a, &adata, &constants));
1966 adata = prep_zero_mask(a, adata, &constants);
1967 mask = create_zero_mask(adata);
1968 x ^= a & zero_bytemask(mask);
1970 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
1972 EXPORT_SYMBOL(hashlen_string);
1975 * Calculate the length and hash of the path component, and
1976 * return the "hash_len" as the result.
1978 static inline u64 hash_name(const void *salt, const char *name)
1980 unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
1981 unsigned long adata, bdata, mask, len;
1982 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1989 len += sizeof(unsigned long);
1991 a = load_unaligned_zeropad(name+len);
1992 b = a ^ REPEAT_BYTE('/');
1993 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1995 adata = prep_zero_mask(a, adata, &constants);
1996 bdata = prep_zero_mask(b, bdata, &constants);
1997 mask = create_zero_mask(adata | bdata);
1998 x ^= a & zero_bytemask(mask);
2000 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
2003 #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
2005 /* Return the hash of a string of known length */
2006 unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
2008 unsigned long hash = init_name_hash(salt);
2010 hash = partial_name_hash((unsigned char)*name++, hash);
2011 return end_name_hash(hash);
2013 EXPORT_SYMBOL(full_name_hash);
2015 /* Return the "hash_len" (hash and length) of a null-terminated string */
2016 u64 hashlen_string(const void *salt, const char *name)
2018 unsigned long hash = init_name_hash(salt);
2019 unsigned long len = 0, c;
2021 c = (unsigned char)*name;
2024 hash = partial_name_hash(c, hash);
2025 c = (unsigned char)name[len];
2027 return hashlen_create(end_name_hash(hash), len);
2029 EXPORT_SYMBOL(hashlen_string);
2032 * We know there's a real path component here of at least
2035 static inline u64 hash_name(const void *salt, const char *name)
2037 unsigned long hash = init_name_hash(salt);
2038 unsigned long len = 0, c;
2040 c = (unsigned char)*name;
2043 hash = partial_name_hash(c, hash);
2044 c = (unsigned char)name[len];
2045 } while (c && c != '/');
2046 return hashlen_create(end_name_hash(hash), len);
2053 * This is the basic name resolution function, turning a pathname into
2054 * the final dentry. We expect 'base' to be positive and a directory.
2056 * Returns 0 and nd will have valid dentry and mnt on success.
2057 * Returns error and drops reference to input namei data on failure.
2059 static int link_path_walk(const char *name, struct nameidata *nd)
2064 return PTR_ERR(name);
2070 /* At this point we know we have a real path component. */
2075 err = may_lookup(nd);
2079 hash_len = hash_name(nd->path.dentry, name);
2082 if (name[0] == '.') switch (hashlen_len(hash_len)) {
2084 if (name[1] == '.') {
2086 nd->flags |= LOOKUP_JUMPED;
2092 if (likely(type == LAST_NORM)) {
2093 struct dentry *parent = nd->path.dentry;
2094 nd->flags &= ~LOOKUP_JUMPED;
2095 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
2096 struct qstr this = { { .hash_len = hash_len }, .name = name };
2097 err = parent->d_op->d_hash(parent, &this);
2100 hash_len = this.hash_len;
2105 nd->last.hash_len = hash_len;
2106 nd->last.name = name;
2107 nd->last_type = type;
2109 name += hashlen_len(hash_len);
2113 * If it wasn't NUL, we know it was '/'. Skip that
2114 * slash, and continue until no more slashes.
2118 } while (unlikely(*name == '/'));
2119 if (unlikely(!*name)) {
2121 /* pathname body, done */
2124 name = nd->stack[nd->depth - 1].name;
2125 /* trailing symlink, done */
2128 /* last component of nested symlink */
2129 err = walk_component(nd, WALK_FOLLOW);
2131 /* not the last component */
2132 err = walk_component(nd, WALK_FOLLOW | WALK_MORE);
2138 const char *s = get_link(nd);
2147 nd->stack[nd->depth - 1].name = name;
2152 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2153 if (nd->flags & LOOKUP_RCU) {
2154 if (unlazy_walk(nd))
2162 /* must be paired with terminate_walk() */
2163 static const char *path_init(struct nameidata *nd, unsigned flags)
2165 const char *s = nd->name->name;
2168 flags &= ~LOOKUP_RCU;
2169 if (flags & LOOKUP_RCU)
2172 nd->last_type = LAST_ROOT; /* if there are only slashes... */
2173 nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
2175 if (flags & LOOKUP_ROOT) {
2176 struct dentry *root = nd->root.dentry;
2177 struct inode *inode = root->d_inode;
2178 if (*s && unlikely(!d_can_lookup(root)))
2179 return ERR_PTR(-ENOTDIR);
2180 nd->path = nd->root;
2182 if (flags & LOOKUP_RCU) {
2183 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2184 nd->root_seq = nd->seq;
2185 nd->m_seq = read_seqbegin(&mount_lock);
2187 path_get(&nd->path);
2192 nd->root.mnt = NULL;
2193 nd->path.mnt = NULL;
2194 nd->path.dentry = NULL;
2196 nd->m_seq = read_seqbegin(&mount_lock);
2199 if (likely(!nd_jump_root(nd)))
2201 return ERR_PTR(-ECHILD);
2202 } else if (nd->dfd == AT_FDCWD) {
2203 if (flags & LOOKUP_RCU) {
2204 struct fs_struct *fs = current->fs;
2208 seq = read_seqcount_begin(&fs->seq);
2210 nd->inode = nd->path.dentry->d_inode;
2211 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2212 } while (read_seqcount_retry(&fs->seq, seq));
2214 get_fs_pwd(current->fs, &nd->path);
2215 nd->inode = nd->path.dentry->d_inode;
2219 /* Caller must check execute permissions on the starting path component */
2220 struct fd f = fdget_raw(nd->dfd);
2221 struct dentry *dentry;
2224 return ERR_PTR(-EBADF);
2226 dentry = f.file->f_path.dentry;
2228 if (*s && unlikely(!d_can_lookup(dentry))) {
2230 return ERR_PTR(-ENOTDIR);
2233 nd->path = f.file->f_path;
2234 if (flags & LOOKUP_RCU) {
2235 nd->inode = nd->path.dentry->d_inode;
2236 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
2238 path_get(&nd->path);
2239 nd->inode = nd->path.dentry->d_inode;
2246 static const char *trailing_symlink(struct nameidata *nd)
2249 int error = may_follow_link(nd);
2250 if (unlikely(error))
2251 return ERR_PTR(error);
2252 nd->flags |= LOOKUP_PARENT;
2253 nd->stack[0].name = NULL;
2258 static inline int lookup_last(struct nameidata *nd)
2260 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2261 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2263 nd->flags &= ~LOOKUP_PARENT;
2264 return walk_component(nd, 0);
2267 static int handle_lookup_down(struct nameidata *nd)
2269 struct path path = nd->path;
2270 struct inode *inode = nd->inode;
2271 unsigned seq = nd->seq;
2274 if (nd->flags & LOOKUP_RCU) {
2276 * don't bother with unlazy_walk on failure - we are
2277 * at the very beginning of walk, so we lose nothing
2278 * if we simply redo everything in non-RCU mode
2280 if (unlikely(!__follow_mount_rcu(nd, &path, &inode, &seq)))
2284 err = follow_managed(&path, nd);
2285 if (unlikely(err < 0))
2287 inode = d_backing_inode(path.dentry);
2290 path_to_nameidata(&path, nd);
2296 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2297 static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
2299 const char *s = path_init(nd, flags);
2302 if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) {
2303 err = handle_lookup_down(nd);
2304 if (unlikely(err < 0))
2308 while (!(err = link_path_walk(s, nd))
2309 && ((err = lookup_last(nd)) > 0)) {
2310 s = trailing_symlink(nd);
2313 err = complete_walk(nd);
2315 if (!err && nd->flags & LOOKUP_DIRECTORY)
2316 if (!d_can_lookup(nd->path.dentry))
2320 nd->path.mnt = NULL;
2321 nd->path.dentry = NULL;
2327 int filename_lookup(int dfd, struct filename *name, unsigned flags,
2328 struct path *path, struct path *root)
2331 struct nameidata nd;
2333 return PTR_ERR(name);
2334 if (unlikely(root)) {
2336 flags |= LOOKUP_ROOT;
2338 set_nameidata(&nd, dfd, name);
2339 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
2340 if (unlikely(retval == -ECHILD))
2341 retval = path_lookupat(&nd, flags, path);
2342 if (unlikely(retval == -ESTALE))
2343 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
2345 if (likely(!retval))
2346 audit_inode(name, path->dentry, 0);
2347 restore_nameidata();
2352 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2353 static int path_parentat(struct nameidata *nd, unsigned flags,
2354 struct path *parent)
2356 const char *s = path_init(nd, flags);
2357 int err = link_path_walk(s, nd);
2359 err = complete_walk(nd);
2362 nd->path.mnt = NULL;
2363 nd->path.dentry = NULL;
2369 static struct filename *filename_parentat(int dfd, struct filename *name,
2370 unsigned int flags, struct path *parent,
2371 struct qstr *last, int *type)
2374 struct nameidata nd;
2378 set_nameidata(&nd, dfd, name);
2379 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
2380 if (unlikely(retval == -ECHILD))
2381 retval = path_parentat(&nd, flags, parent);
2382 if (unlikely(retval == -ESTALE))
2383 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
2384 if (likely(!retval)) {
2386 *type = nd.last_type;
2387 audit_inode(name, parent->dentry, AUDIT_INODE_PARENT);
2390 name = ERR_PTR(retval);
2392 restore_nameidata();
2396 /* does lookup, returns the object with parent locked */
2397 struct dentry *kern_path_locked(const char *name, struct path *path)
2399 struct filename *filename;
2404 filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
2406 if (IS_ERR(filename))
2407 return ERR_CAST(filename);
2408 if (unlikely(type != LAST_NORM)) {
2411 return ERR_PTR(-EINVAL);
2413 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
2414 d = __lookup_hash(&last, path->dentry, 0);
2416 inode_unlock(path->dentry->d_inode);
2423 int kern_path(const char *name, unsigned int flags, struct path *path)
2425 return filename_lookup(AT_FDCWD, getname_kernel(name),
2428 EXPORT_SYMBOL(kern_path);
2431 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2432 * @dentry: pointer to dentry of the base directory
2433 * @mnt: pointer to vfs mount of the base directory
2434 * @name: pointer to file name
2435 * @flags: lookup flags
2436 * @path: pointer to struct path to fill
2438 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2439 const char *name, unsigned int flags,
2442 struct path root = {.mnt = mnt, .dentry = dentry};
2443 /* the first argument of filename_lookup() is ignored with root */
2444 return filename_lookup(AT_FDCWD, getname_kernel(name),
2445 flags , path, &root);
2447 EXPORT_SYMBOL(vfs_path_lookup);
2449 static int lookup_one_len_common(const char *name, struct dentry *base,
2450 int len, struct qstr *this)
2454 this->hash = full_name_hash(base, name, len);
2458 if (unlikely(name[0] == '.')) {
2459 if (len < 2 || (len == 2 && name[1] == '.'))
2464 unsigned int c = *(const unsigned char *)name++;
2465 if (c == '/' || c == '\0')
2469 * See if the low-level filesystem might want
2470 * to use its own hash..
2472 if (base->d_flags & DCACHE_OP_HASH) {
2473 int err = base->d_op->d_hash(base, this);
2478 return inode_permission(base->d_inode, MAY_EXEC);
2482 * try_lookup_one_len - filesystem helper to lookup single pathname component
2483 * @name: pathname component to lookup
2484 * @base: base directory to lookup from
2485 * @len: maximum length @len should be interpreted to
2487 * Look up a dentry by name in the dcache, returning NULL if it does not
2488 * currently exist. The function does not try to create a dentry.
2490 * Note that this routine is purely a helper for filesystem usage and should
2491 * not be called by generic code.
2493 * The caller must hold base->i_mutex.
2495 struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len)
2500 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2502 err = lookup_one_len_common(name, base, len, &this);
2504 return ERR_PTR(err);
2506 return lookup_dcache(&this, base, 0);
2508 EXPORT_SYMBOL(try_lookup_one_len);
2511 * lookup_one_len - filesystem helper to lookup single pathname component
2512 * @name: pathname component to lookup
2513 * @base: base directory to lookup from
2514 * @len: maximum length @len should be interpreted to
2516 * Note that this routine is purely a helper for filesystem usage and should
2517 * not be called by generic code.
2519 * The caller must hold base->i_mutex.
2521 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2523 struct dentry *dentry;
2527 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2529 err = lookup_one_len_common(name, base, len, &this);
2531 return ERR_PTR(err);
2533 dentry = lookup_dcache(&this, base, 0);
2534 return dentry ? dentry : __lookup_slow(&this, base, 0);
2536 EXPORT_SYMBOL(lookup_one_len);
2539 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2540 * @name: pathname component to lookup
2541 * @base: base directory to lookup from
2542 * @len: maximum length @len should be interpreted to
2544 * Note that this routine is purely a helper for filesystem usage and should
2545 * not be called by generic code.
2547 * Unlike lookup_one_len, it should be called without the parent
2548 * i_mutex held, and will take the i_mutex itself if necessary.
2550 struct dentry *lookup_one_len_unlocked(const char *name,
2551 struct dentry *base, int len)
2557 err = lookup_one_len_common(name, base, len, &this);
2559 return ERR_PTR(err);
2561 ret = lookup_dcache(&this, base, 0);
2563 ret = lookup_slow(&this, base, 0);
2566 EXPORT_SYMBOL(lookup_one_len_unlocked);
2569 * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT)
2570 * on negatives. Returns known positive or ERR_PTR(); that's what
2571 * most of the users want. Note that pinned negative with unlocked parent
2572 * _can_ become positive at any time, so callers of lookup_one_len_unlocked()
2573 * need to be very careful; pinned positives have ->d_inode stable, so
2574 * this one avoids such problems.
2576 struct dentry *lookup_positive_unlocked(const char *name,
2577 struct dentry *base, int len)
2579 struct dentry *ret = lookup_one_len_unlocked(name, base, len);
2580 if (!IS_ERR(ret) && d_is_negative(ret)) {
2582 ret = ERR_PTR(-ENOENT);
2586 EXPORT_SYMBOL(lookup_positive_unlocked);
2588 #ifdef CONFIG_UNIX98_PTYS
2589 int path_pts(struct path *path)
2591 /* Find something mounted on "pts" in the same directory as
2594 struct dentry *child, *parent;
2598 ret = path_parent_directory(path);
2602 parent = path->dentry;
2605 child = d_hash_and_lookup(parent, &this);
2606 if (IS_ERR_OR_NULL(child))
2609 path->dentry = child;
2616 int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2617 struct path *path, int *empty)
2619 return filename_lookup(dfd, getname_flags(name, flags, empty),
2622 EXPORT_SYMBOL(user_path_at_empty);
2625 * mountpoint_last - look up last component for umount
2626 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2628 * This is a special lookup_last function just for umount. In this case, we
2629 * need to resolve the path without doing any revalidation.
2631 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2632 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2633 * in almost all cases, this lookup will be served out of the dcache. The only
2634 * cases where it won't are if nd->last refers to a symlink or the path is
2635 * bogus and it doesn't exist.
2638 * -error: if there was an error during lookup. This includes -ENOENT if the
2639 * lookup found a negative dentry.
2641 * 0: if we successfully resolved nd->last and found it to not to be a
2642 * symlink that needs to be followed.
2644 * 1: if we successfully resolved nd->last and found it to be a symlink
2645 * that needs to be followed.
2648 mountpoint_last(struct nameidata *nd)
2651 struct dentry *dir = nd->path.dentry;
2654 /* If we're in rcuwalk, drop out of it to handle last component */
2655 if (nd->flags & LOOKUP_RCU) {
2656 if (unlazy_walk(nd))
2660 nd->flags &= ~LOOKUP_PARENT;
2662 if (unlikely(nd->last_type != LAST_NORM)) {
2663 error = handle_dots(nd, nd->last_type);
2666 path.dentry = dget(nd->path.dentry);
2668 path.dentry = d_lookup(dir, &nd->last);
2671 * No cached dentry. Mounted dentries are pinned in the
2672 * cache, so that means that this dentry is probably
2673 * a symlink or the path doesn't actually point
2674 * to a mounted dentry.
2676 path.dentry = lookup_slow(&nd->last, dir,
2677 nd->flags | LOOKUP_NO_REVAL);
2678 if (IS_ERR(path.dentry))
2679 return PTR_ERR(path.dentry);
2682 if (d_is_negative(path.dentry)) {
2686 path.mnt = nd->path.mnt;
2687 return step_into(nd, &path, 0, d_backing_inode(path.dentry), 0);
2691 * path_mountpoint - look up a path to be umounted
2692 * @nd: lookup context
2693 * @flags: lookup flags
2694 * @path: pointer to container for result
2696 * Look up the given name, but don't attempt to revalidate the last component.
2697 * Returns 0 and "path" will be valid on success; Returns error otherwise.
2700 path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
2702 const char *s = path_init(nd, flags);
2705 while (!(err = link_path_walk(s, nd)) &&
2706 (err = mountpoint_last(nd)) > 0) {
2707 s = trailing_symlink(nd);
2711 nd->path.mnt = NULL;
2712 nd->path.dentry = NULL;
2720 filename_mountpoint(int dfd, struct filename *name, struct path *path,
2723 struct nameidata nd;
2726 return PTR_ERR(name);
2727 set_nameidata(&nd, dfd, name);
2728 error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
2729 if (unlikely(error == -ECHILD))
2730 error = path_mountpoint(&nd, flags, path);
2731 if (unlikely(error == -ESTALE))
2732 error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
2734 audit_inode(name, path->dentry, AUDIT_INODE_NOEVAL);
2735 restore_nameidata();
2741 * user_path_mountpoint_at - lookup a path from userland in order to umount it
2742 * @dfd: directory file descriptor
2743 * @name: pathname from userland
2744 * @flags: lookup flags
2745 * @path: pointer to container to hold result
2747 * A umount is a special case for path walking. We're not actually interested
2748 * in the inode in this situation, and ESTALE errors can be a problem. We
2749 * simply want track down the dentry and vfsmount attached at the mountpoint
2750 * and avoid revalidating the last component.
2752 * Returns 0 and populates "path" on success.
2755 user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
2758 return filename_mountpoint(dfd, getname(name), path, flags);
2762 kern_path_mountpoint(int dfd, const char *name, struct path *path,
2765 return filename_mountpoint(dfd, getname_kernel(name), path, flags);
2767 EXPORT_SYMBOL(kern_path_mountpoint);
2769 int __check_sticky(struct inode *dir, struct inode *inode)
2771 kuid_t fsuid = current_fsuid();
2773 if (uid_eq(inode->i_uid, fsuid))
2775 if (uid_eq(dir->i_uid, fsuid))
2777 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
2779 EXPORT_SYMBOL(__check_sticky);
2782 * Check whether we can remove a link victim from directory dir, check
2783 * whether the type of victim is right.
2784 * 1. We can't do it if dir is read-only (done in permission())
2785 * 2. We should have write and exec permissions on dir
2786 * 3. We can't remove anything from append-only dir
2787 * 4. We can't do anything with immutable dir (done in permission())
2788 * 5. If the sticky bit on dir is set we should either
2789 * a. be owner of dir, or
2790 * b. be owner of victim, or
2791 * c. have CAP_FOWNER capability
2792 * 6. If the victim is append-only or immutable we can't do antyhing with
2793 * links pointing to it.
2794 * 7. If the victim has an unknown uid or gid we can't change the inode.
2795 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2796 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2797 * 10. We can't remove a root or mountpoint.
2798 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
2799 * nfs_async_unlink().
2801 static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
2803 struct inode *inode = d_backing_inode(victim);
2806 if (d_is_negative(victim))
2810 BUG_ON(victim->d_parent->d_inode != dir);
2812 /* Inode writeback is not safe when the uid or gid are invalid. */
2813 if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
2816 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
2818 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
2824 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2825 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode))
2828 if (!d_is_dir(victim))
2830 if (IS_ROOT(victim))
2832 } else if (d_is_dir(victim))
2834 if (IS_DEADDIR(dir))
2836 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2841 /* Check whether we can create an object with dentry child in directory
2843 * 1. We can't do it if child already exists (open has special treatment for
2844 * this case, but since we are inlined it's OK)
2845 * 2. We can't do it if dir is read-only (done in permission())
2846 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2847 * 4. We should have write and exec permissions on dir
2848 * 5. We can't do it if dir is immutable (done in permission())
2850 static inline int may_create(struct inode *dir, struct dentry *child)
2852 struct user_namespace *s_user_ns;
2853 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
2856 if (IS_DEADDIR(dir))
2858 s_user_ns = dir->i_sb->s_user_ns;
2859 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2860 !kgid_has_mapping(s_user_ns, current_fsgid()))
2862 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
2866 * p1 and p2 should be directories on the same fs.
2868 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2873 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2877 mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
2879 p = d_ancestor(p2, p1);
2881 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
2882 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
2886 p = d_ancestor(p1, p2);
2888 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2889 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
2893 lock_two_inodes(p1->d_inode, p2->d_inode,
2894 I_MUTEX_PARENT, I_MUTEX_PARENT2);
2897 EXPORT_SYMBOL(lock_rename);
2899 void unlock_rename(struct dentry *p1, struct dentry *p2)
2901 inode_unlock(p1->d_inode);
2903 inode_unlock(p2->d_inode);
2904 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
2907 EXPORT_SYMBOL(unlock_rename);
2909 int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2912 int error = may_create(dir, dentry);
2916 if (!dir->i_op->create)
2917 return -EACCES; /* shouldn't it be ENOSYS? */
2920 error = security_inode_create(dir, dentry, mode);
2923 error = dir->i_op->create(dir, dentry, mode, want_excl);
2925 fsnotify_create(dir, dentry);
2928 EXPORT_SYMBOL(vfs_create);
2930 int vfs_mkobj(struct dentry *dentry, umode_t mode,
2931 int (*f)(struct dentry *, umode_t, void *),
2934 struct inode *dir = dentry->d_parent->d_inode;
2935 int error = may_create(dir, dentry);
2941 error = security_inode_create(dir, dentry, mode);
2944 error = f(dentry, mode, arg);
2946 fsnotify_create(dir, dentry);
2949 EXPORT_SYMBOL(vfs_mkobj);
2951 bool may_open_dev(const struct path *path)
2953 return !(path->mnt->mnt_flags & MNT_NODEV) &&
2954 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
2957 static int may_open(const struct path *path, int acc_mode, int flag)
2959 struct dentry *dentry = path->dentry;
2960 struct inode *inode = dentry->d_inode;
2966 switch (inode->i_mode & S_IFMT) {
2970 if (acc_mode & MAY_WRITE)
2975 if (!may_open_dev(path))
2984 error = inode_permission(inode, MAY_OPEN | acc_mode);
2989 * An append-only file must be opened in append mode for writing.
2991 if (IS_APPEND(inode)) {
2992 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
2998 /* O_NOATIME can only be set by the owner or superuser */
2999 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
3005 static int handle_truncate(struct file *filp)
3007 const struct path *path = &filp->f_path;
3008 struct inode *inode = path->dentry->d_inode;
3009 int error = get_write_access(inode);
3013 * Refuse to truncate files with mandatory locks held on them.
3015 error = locks_verify_locked(filp);
3017 error = security_path_truncate(path);
3019 error = do_truncate(path->dentry, 0,
3020 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
3023 put_write_access(inode);
3027 static inline int open_to_namei_flags(int flag)
3029 if ((flag & O_ACCMODE) == 3)
3034 static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode)
3036 struct user_namespace *s_user_ns;
3037 int error = security_path_mknod(dir, dentry, mode, 0);
3041 s_user_ns = dir->dentry->d_sb->s_user_ns;
3042 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
3043 !kgid_has_mapping(s_user_ns, current_fsgid()))
3046 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
3050 return security_inode_create(dir->dentry->d_inode, dentry, mode);
3054 * Attempt to atomically look up, create and open a file from a negative
3057 * Returns 0 if successful. The file will have been created and attached to
3058 * @file by the filesystem calling finish_open().
3060 * If the file was looked up only or didn't need creating, FMODE_OPENED won't
3061 * be set. The caller will need to perform the open themselves. @path will
3062 * have been updated to point to the new dentry. This may be negative.
3064 * Returns an error code otherwise.
3066 static int atomic_open(struct nameidata *nd, struct dentry *dentry,
3067 struct path *path, struct file *file,
3068 const struct open_flags *op,
3069 int open_flag, umode_t mode)
3071 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
3072 struct inode *dir = nd->path.dentry->d_inode;
3075 if (!(~open_flag & (O_EXCL | O_CREAT))) /* both O_EXCL and O_CREAT */
3076 open_flag &= ~O_TRUNC;
3078 if (nd->flags & LOOKUP_DIRECTORY)
3079 open_flag |= O_DIRECTORY;
3081 file->f_path.dentry = DENTRY_NOT_SET;
3082 file->f_path.mnt = nd->path.mnt;
3083 error = dir->i_op->atomic_open(dir, dentry, file,
3084 open_to_namei_flags(open_flag), mode);
3085 d_lookup_done(dentry);
3087 if (file->f_mode & FMODE_OPENED) {
3089 * We didn't have the inode before the open, so check open
3092 int acc_mode = op->acc_mode;
3093 if (file->f_mode & FMODE_CREATED) {
3094 WARN_ON(!(open_flag & O_CREAT));
3095 fsnotify_create(dir, dentry);
3098 error = may_open(&file->f_path, acc_mode, open_flag);
3099 if (WARN_ON(error > 0))
3101 } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
3104 if (file->f_path.dentry) {
3106 dentry = file->f_path.dentry;
3108 if (file->f_mode & FMODE_CREATED)
3109 fsnotify_create(dir, dentry);
3110 if (unlikely(d_is_negative(dentry))) {
3113 path->dentry = dentry;
3114 path->mnt = nd->path.mnt;
3124 * Look up and maybe create and open the last component.
3126 * Must be called with parent locked (exclusive in O_CREAT case).
3128 * Returns 0 on success, that is, if
3129 * the file was successfully atomically created (if necessary) and opened, or
3130 * the file was not completely opened at this time, though lookups and
3131 * creations were performed.
3132 * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
3133 * In the latter case dentry returned in @path might be negative if O_CREAT
3134 * hadn't been specified.
3136 * An error code is returned on failure.
3138 static int lookup_open(struct nameidata *nd, struct path *path,
3140 const struct open_flags *op,
3143 struct dentry *dir = nd->path.dentry;
3144 struct inode *dir_inode = dir->d_inode;
3145 int open_flag = op->open_flag;
3146 struct dentry *dentry;
3147 int error, create_error = 0;
3148 umode_t mode = op->mode;
3149 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
3151 if (unlikely(IS_DEADDIR(dir_inode)))
3154 file->f_mode &= ~FMODE_CREATED;
3155 dentry = d_lookup(dir, &nd->last);
3158 dentry = d_alloc_parallel(dir, &nd->last, &wq);
3160 return PTR_ERR(dentry);
3162 if (d_in_lookup(dentry))
3165 error = d_revalidate(dentry, nd->flags);
3166 if (likely(error > 0))
3170 d_invalidate(dentry);
3174 if (dentry->d_inode) {
3175 /* Cached positive dentry: will open in f_op->open */
3180 * Checking write permission is tricky, bacuse we don't know if we are
3181 * going to actually need it: O_CREAT opens should work as long as the
3182 * file exists. But checking existence breaks atomicity. The trick is
3183 * to check access and if not granted clear O_CREAT from the flags.
3185 * Another problem is returing the "right" error value (e.g. for an
3186 * O_EXCL open we want to return EEXIST not EROFS).
3188 if (open_flag & O_CREAT) {
3189 if (!IS_POSIXACL(dir->d_inode))
3190 mode &= ~current_umask();
3191 if (unlikely(!got_write)) {
3192 create_error = -EROFS;
3193 open_flag &= ~O_CREAT;
3194 if (open_flag & (O_EXCL | O_TRUNC))
3196 /* No side effects, safe to clear O_CREAT */
3198 create_error = may_o_create(&nd->path, dentry, mode);
3200 open_flag &= ~O_CREAT;
3201 if (open_flag & O_EXCL)
3205 } else if ((open_flag & (O_TRUNC|O_WRONLY|O_RDWR)) &&
3206 unlikely(!got_write)) {
3208 * No O_CREATE -> atomicity not a requirement -> fall
3209 * back to lookup + open
3214 if (dir_inode->i_op->atomic_open) {
3215 error = atomic_open(nd, dentry, path, file, op, open_flag,
3217 if (unlikely(error == -ENOENT) && create_error)
3218 error = create_error;
3223 if (d_in_lookup(dentry)) {
3224 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
3226 d_lookup_done(dentry);
3227 if (unlikely(res)) {
3229 error = PTR_ERR(res);
3237 /* Negative dentry, just create the file */
3238 if (!dentry->d_inode && (open_flag & O_CREAT)) {
3239 file->f_mode |= FMODE_CREATED;
3240 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
3241 if (!dir_inode->i_op->create) {
3245 error = dir_inode->i_op->create(dir_inode, dentry, mode,
3246 open_flag & O_EXCL);
3249 fsnotify_create(dir_inode, dentry);
3251 if (unlikely(create_error) && !dentry->d_inode) {
3252 error = create_error;
3256 path->dentry = dentry;
3257 path->mnt = nd->path.mnt;
3266 * Handle the last step of open()
3268 static int do_last(struct nameidata *nd,
3269 struct file *file, const struct open_flags *op)
3271 struct dentry *dir = nd->path.dentry;
3272 kuid_t dir_uid = nd->inode->i_uid;
3273 umode_t dir_mode = nd->inode->i_mode;
3274 int open_flag = op->open_flag;
3275 bool will_truncate = (open_flag & O_TRUNC) != 0;
3276 bool got_write = false;
3277 int acc_mode = op->acc_mode;
3279 struct inode *inode;
3283 nd->flags &= ~LOOKUP_PARENT;
3284 nd->flags |= op->intent;
3286 if (nd->last_type != LAST_NORM) {
3287 error = handle_dots(nd, nd->last_type);
3288 if (unlikely(error))
3293 if (!(open_flag & O_CREAT)) {
3294 if (nd->last.name[nd->last.len])
3295 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3296 /* we _can_ be in RCU mode here */
3297 error = lookup_fast(nd, &path, &inode, &seq);
3298 if (likely(error > 0))
3304 BUG_ON(nd->inode != dir->d_inode);
3305 BUG_ON(nd->flags & LOOKUP_RCU);
3307 /* create side of things */
3309 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
3310 * has been cleared when we got to the last component we are
3313 error = complete_walk(nd);
3317 audit_inode(nd->name, dir, AUDIT_INODE_PARENT);
3318 /* trailing slashes? */
3319 if (unlikely(nd->last.name[nd->last.len]))
3323 if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
3324 error = mnt_want_write(nd->path.mnt);
3328 * do _not_ fail yet - we might not need that or fail with
3329 * a different error; let lookup_open() decide; we'll be
3330 * dropping this one anyway.
3333 if (open_flag & O_CREAT)
3334 inode_lock(dir->d_inode);
3336 inode_lock_shared(dir->d_inode);
3337 error = lookup_open(nd, &path, file, op, got_write);
3338 if (open_flag & O_CREAT)
3339 inode_unlock(dir->d_inode);
3341 inode_unlock_shared(dir->d_inode);
3346 if (file->f_mode & FMODE_OPENED) {
3347 if ((file->f_mode & FMODE_CREATED) ||
3348 !S_ISREG(file_inode(file)->i_mode))
3349 will_truncate = false;
3351 audit_inode(nd->name, file->f_path.dentry, 0);
3355 if (file->f_mode & FMODE_CREATED) {
3356 /* Don't check for write permission, don't truncate */
3357 open_flag &= ~O_TRUNC;
3358 will_truncate = false;
3360 path_to_nameidata(&path, nd);
3361 goto finish_open_created;
3365 * If atomic_open() acquired write access it is dropped now due to
3366 * possible mount and symlink following (this might be optimized away if
3370 mnt_drop_write(nd->path.mnt);
3374 error = follow_managed(&path, nd);
3375 if (unlikely(error < 0))
3378 if (unlikely(d_is_negative(path.dentry))) {
3379 path_to_nameidata(&path, nd);
3384 * create/update audit record if it already exists.
3386 audit_inode(nd->name, path.dentry, 0);
3388 if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
3389 path_to_nameidata(&path, nd);
3393 seq = 0; /* out of RCU mode, so the value doesn't matter */
3394 inode = d_backing_inode(path.dentry);
3396 error = step_into(nd, &path, 0, inode, seq);
3397 if (unlikely(error))
3400 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
3401 error = complete_walk(nd);
3404 audit_inode(nd->name, nd->path.dentry, 0);
3405 if (open_flag & O_CREAT) {
3407 if (d_is_dir(nd->path.dentry))
3409 error = may_create_in_sticky(dir_mode, dir_uid,
3410 d_backing_inode(nd->path.dentry));
3411 if (unlikely(error))
3415 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
3417 if (!d_is_reg(nd->path.dentry))
3418 will_truncate = false;
3420 if (will_truncate) {
3421 error = mnt_want_write(nd->path.mnt);
3426 finish_open_created:
3427 error = may_open(&nd->path, acc_mode, open_flag);
3430 BUG_ON(file->f_mode & FMODE_OPENED); /* once it's opened, it's opened */
3431 error = vfs_open(&nd->path, file);
3435 error = ima_file_check(file, op->acc_mode);
3436 if (!error && will_truncate)
3437 error = handle_truncate(file);
3439 if (unlikely(error > 0)) {
3444 mnt_drop_write(nd->path.mnt);
3448 struct dentry *vfs_tmpfile(struct dentry *dentry, umode_t mode, int open_flag)
3450 struct dentry *child = NULL;
3451 struct inode *dir = dentry->d_inode;
3452 struct inode *inode;
3455 /* we want directory to be writable */
3456 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
3459 error = -EOPNOTSUPP;
3460 if (!dir->i_op->tmpfile)
3463 child = d_alloc(dentry, &slash_name);
3464 if (unlikely(!child))
3466 if (!IS_POSIXACL(dir))
3467 mode &= ~current_umask();
3468 error = dir->i_op->tmpfile(dir, child, mode);
3472 inode = child->d_inode;
3473 if (unlikely(!inode))
3475 if (!(open_flag & O_EXCL)) {
3476 spin_lock(&inode->i_lock);
3477 inode->i_state |= I_LINKABLE;
3478 spin_unlock(&inode->i_lock);
3480 ima_post_create_tmpfile(inode);
3485 return ERR_PTR(error);
3487 EXPORT_SYMBOL(vfs_tmpfile);
3489 static int do_tmpfile(struct nameidata *nd, unsigned flags,
3490 const struct open_flags *op,
3493 struct dentry *child;
3495 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
3496 if (unlikely(error))
3498 error = mnt_want_write(path.mnt);
3499 if (unlikely(error))
3501 child = vfs_tmpfile(path.dentry, op->mode, op->open_flag);
3502 error = PTR_ERR(child);
3506 path.dentry = child;
3507 audit_inode(nd->name, child, 0);
3508 /* Don't check for other permissions, the inode was just created */
3509 error = may_open(&path, 0, op->open_flag);
3512 file->f_path.mnt = path.mnt;
3513 error = finish_open(file, child, NULL);
3515 mnt_drop_write(path.mnt);
3521 static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
3524 int error = path_lookupat(nd, flags, &path);
3526 audit_inode(nd->name, path.dentry, 0);
3527 error = vfs_open(&path, file);
3533 static struct file *path_openat(struct nameidata *nd,
3534 const struct open_flags *op, unsigned flags)
3539 file = alloc_empty_file(op->open_flag, current_cred());
3543 if (unlikely(file->f_flags & __O_TMPFILE)) {
3544 error = do_tmpfile(nd, flags, op, file);
3545 } else if (unlikely(file->f_flags & O_PATH)) {
3546 error = do_o_path(nd, flags, file);
3548 const char *s = path_init(nd, flags);
3549 while (!(error = link_path_walk(s, nd)) &&
3550 (error = do_last(nd, file, op)) > 0) {
3551 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
3552 s = trailing_symlink(nd);
3556 if (likely(!error)) {
3557 if (likely(file->f_mode & FMODE_OPENED))
3563 if (error == -EOPENSTALE) {
3564 if (flags & LOOKUP_RCU)
3569 return ERR_PTR(error);
3572 struct file *do_filp_open(int dfd, struct filename *pathname,
3573 const struct open_flags *op)
3575 struct nameidata nd;
3576 int flags = op->lookup_flags;
3579 set_nameidata(&nd, dfd, pathname);
3580 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
3581 if (unlikely(filp == ERR_PTR(-ECHILD)))
3582 filp = path_openat(&nd, op, flags);
3583 if (unlikely(filp == ERR_PTR(-ESTALE)))
3584 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
3585 restore_nameidata();
3589 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3590 const char *name, const struct open_flags *op)
3592 struct nameidata nd;
3594 struct filename *filename;
3595 int flags = op->lookup_flags | LOOKUP_ROOT;
3598 nd.root.dentry = dentry;
3600 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
3601 return ERR_PTR(-ELOOP);
3603 filename = getname_kernel(name);
3604 if (IS_ERR(filename))
3605 return ERR_CAST(filename);
3607 set_nameidata(&nd, -1, filename);
3608 file = path_openat(&nd, op, flags | LOOKUP_RCU);
3609 if (unlikely(file == ERR_PTR(-ECHILD)))
3610 file = path_openat(&nd, op, flags);
3611 if (unlikely(file == ERR_PTR(-ESTALE)))
3612 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
3613 restore_nameidata();
3618 static struct dentry *filename_create(int dfd, struct filename *name,
3619 struct path *path, unsigned int lookup_flags)
3621 struct dentry *dentry = ERR_PTR(-EEXIST);
3626 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3629 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3630 * other flags passed in are ignored!
3632 lookup_flags &= LOOKUP_REVAL;
3634 name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
3636 return ERR_CAST(name);
3639 * Yucky last component or no last component at all?
3640 * (foo/., foo/.., /////)
3642 if (unlikely(type != LAST_NORM))
3645 /* don't fail immediately if it's r/o, at least try to report other errors */
3646 err2 = mnt_want_write(path->mnt);
3648 * Do the final lookup.
3650 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3651 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
3652 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
3657 if (d_is_positive(dentry))
3661 * Special case - lookup gave negative, but... we had foo/bar/
3662 * From the vfs_mknod() POV we just have a negative dentry -
3663 * all is fine. Let's be bastards - you had / on the end, you've
3664 * been asking for (non-existent) directory. -ENOENT for you.
3666 if (unlikely(!is_dir && last.name[last.len])) {
3670 if (unlikely(err2)) {
3678 dentry = ERR_PTR(error);
3680 inode_unlock(path->dentry->d_inode);
3682 mnt_drop_write(path->mnt);
3689 struct dentry *kern_path_create(int dfd, const char *pathname,
3690 struct path *path, unsigned int lookup_flags)
3692 return filename_create(dfd, getname_kernel(pathname),
3693 path, lookup_flags);
3695 EXPORT_SYMBOL(kern_path_create);
3697 void done_path_create(struct path *path, struct dentry *dentry)
3700 inode_unlock(path->dentry->d_inode);
3701 mnt_drop_write(path->mnt);
3704 EXPORT_SYMBOL(done_path_create);
3706 inline struct dentry *user_path_create(int dfd, const char __user *pathname,
3707 struct path *path, unsigned int lookup_flags)
3709 return filename_create(dfd, getname(pathname), path, lookup_flags);
3711 EXPORT_SYMBOL(user_path_create);
3713 int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3715 int error = may_create(dir, dentry);
3720 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
3723 if (!dir->i_op->mknod)
3726 error = devcgroup_inode_mknod(mode, dev);
3730 error = security_inode_mknod(dir, dentry, mode, dev);
3734 error = dir->i_op->mknod(dir, dentry, mode, dev);
3736 fsnotify_create(dir, dentry);
3739 EXPORT_SYMBOL(vfs_mknod);
3741 static int may_mknod(umode_t mode)
3743 switch (mode & S_IFMT) {
3749 case 0: /* zero mode translates to S_IFREG */
3758 long do_mknodat(int dfd, const char __user *filename, umode_t mode,
3761 struct dentry *dentry;
3764 unsigned int lookup_flags = 0;
3766 error = may_mknod(mode);
3770 dentry = user_path_create(dfd, filename, &path, lookup_flags);
3772 return PTR_ERR(dentry);
3774 if (!IS_POSIXACL(path.dentry->d_inode))
3775 mode &= ~current_umask();
3776 error = security_path_mknod(&path, dentry, mode, dev);
3779 switch (mode & S_IFMT) {
3780 case 0: case S_IFREG:
3781 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
3783 ima_post_path_mknod(dentry);
3785 case S_IFCHR: case S_IFBLK:
3786 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
3787 new_decode_dev(dev));
3789 case S_IFIFO: case S_IFSOCK:
3790 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
3794 done_path_create(&path, dentry);
3795 if (retry_estale(error, lookup_flags)) {
3796 lookup_flags |= LOOKUP_REVAL;
3802 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3805 return do_mknodat(dfd, filename, mode, dev);
3808 SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
3810 return do_mknodat(AT_FDCWD, filename, mode, dev);
3813 int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
3815 int error = may_create(dir, dentry);
3816 unsigned max_links = dir->i_sb->s_max_links;
3821 if (!dir->i_op->mkdir)
3824 mode &= (S_IRWXUGO|S_ISVTX);
3825 error = security_inode_mkdir(dir, dentry, mode);
3829 if (max_links && dir->i_nlink >= max_links)
3832 error = dir->i_op->mkdir(dir, dentry, mode);
3834 fsnotify_mkdir(dir, dentry);
3837 EXPORT_SYMBOL(vfs_mkdir);
3839 long do_mkdirat(int dfd, const char __user *pathname, umode_t mode)
3841 struct dentry *dentry;
3844 unsigned int lookup_flags = LOOKUP_DIRECTORY;
3847 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
3849 return PTR_ERR(dentry);
3851 if (!IS_POSIXACL(path.dentry->d_inode))
3852 mode &= ~current_umask();
3853 error = security_path_mkdir(&path, dentry, mode);
3855 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3856 done_path_create(&path, dentry);
3857 if (retry_estale(error, lookup_flags)) {
3858 lookup_flags |= LOOKUP_REVAL;
3864 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3866 return do_mkdirat(dfd, pathname, mode);
3869 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
3871 return do_mkdirat(AT_FDCWD, pathname, mode);
3874 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3876 int error = may_delete(dir, dentry, 1);
3881 if (!dir->i_op->rmdir)
3885 inode_lock(dentry->d_inode);
3888 if (is_local_mountpoint(dentry))
3891 error = security_inode_rmdir(dir, dentry);
3895 error = dir->i_op->rmdir(dir, dentry);
3899 shrink_dcache_parent(dentry);
3900 dentry->d_inode->i_flags |= S_DEAD;
3902 detach_mounts(dentry);
3905 inode_unlock(dentry->d_inode);
3908 d_delete_notify(dir, dentry);
3911 EXPORT_SYMBOL(vfs_rmdir);
3913 long do_rmdir(int dfd, const char __user *pathname)
3916 struct filename *name;
3917 struct dentry *dentry;
3921 unsigned int lookup_flags = 0;
3923 name = filename_parentat(dfd, getname(pathname), lookup_flags,
3924 &path, &last, &type);
3926 return PTR_ERR(name);
3940 error = mnt_want_write(path.mnt);
3944 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
3945 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
3946 error = PTR_ERR(dentry);
3949 if (!dentry->d_inode) {
3953 error = security_path_rmdir(&path, dentry);
3956 error = vfs_rmdir(path.dentry->d_inode, dentry);
3960 inode_unlock(path.dentry->d_inode);
3961 mnt_drop_write(path.mnt);
3965 if (retry_estale(error, lookup_flags)) {
3966 lookup_flags |= LOOKUP_REVAL;
3972 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
3974 return do_rmdir(AT_FDCWD, pathname);
3978 * vfs_unlink - unlink a filesystem object
3979 * @dir: parent directory
3981 * @delegated_inode: returns victim inode, if the inode is delegated.
3983 * The caller must hold dir->i_mutex.
3985 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3986 * return a reference to the inode in delegated_inode. The caller
3987 * should then break the delegation on that inode and retry. Because
3988 * breaking a delegation may take a long time, the caller should drop
3989 * dir->i_mutex before doing so.
3991 * Alternatively, a caller may pass NULL for delegated_inode. This may
3992 * be appropriate for callers that expect the underlying filesystem not
3993 * to be NFS exported.
3995 int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
3997 struct inode *target = dentry->d_inode;
3998 int error = may_delete(dir, dentry, 0);
4003 if (!dir->i_op->unlink)
4007 if (is_local_mountpoint(dentry))
4010 error = security_inode_unlink(dir, dentry);
4012 error = try_break_deleg(target, delegated_inode);
4015 error = dir->i_op->unlink(dir, dentry);
4018 detach_mounts(dentry);
4023 inode_unlock(target);
4025 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
4026 if (!error && dentry->d_flags & DCACHE_NFSFS_RENAMED) {
4027 fsnotify_unlink(dir, dentry);
4028 } else if (!error) {
4029 fsnotify_link_count(target);
4030 d_delete_notify(dir, dentry);
4035 EXPORT_SYMBOL(vfs_unlink);
4038 * Make sure that the actual truncation of the file will occur outside its
4039 * directory's i_mutex. Truncate can take a long time if there is a lot of
4040 * writeout happening, and we don't want to prevent access to the directory
4041 * while waiting on the I/O.
4043 long do_unlinkat(int dfd, struct filename *name)
4046 struct dentry *dentry;
4050 struct inode *inode = NULL;
4051 struct inode *delegated_inode = NULL;
4052 unsigned int lookup_flags = 0;
4054 name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
4056 return PTR_ERR(name);
4059 if (type != LAST_NORM)
4062 error = mnt_want_write(path.mnt);
4066 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
4067 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
4068 error = PTR_ERR(dentry);
4069 if (!IS_ERR(dentry)) {
4070 /* Why not before? Because we want correct error value */
4071 if (last.name[last.len])
4073 inode = dentry->d_inode;
4074 if (d_is_negative(dentry))
4077 error = security_path_unlink(&path, dentry);
4080 error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
4084 inode_unlock(path.dentry->d_inode);
4086 iput(inode); /* truncate the inode here */
4088 if (delegated_inode) {
4089 error = break_deleg_wait(&delegated_inode);
4093 mnt_drop_write(path.mnt);
4096 if (retry_estale(error, lookup_flags)) {
4097 lookup_flags |= LOOKUP_REVAL;
4105 if (d_is_negative(dentry))
4107 else if (d_is_dir(dentry))
4114 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
4116 if ((flag & ~AT_REMOVEDIR) != 0)
4119 if (flag & AT_REMOVEDIR)
4120 return do_rmdir(dfd, pathname);
4122 return do_unlinkat(dfd, getname(pathname));
4125 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
4127 return do_unlinkat(AT_FDCWD, getname(pathname));
4130 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
4132 int error = may_create(dir, dentry);
4137 if (!dir->i_op->symlink)
4140 error = security_inode_symlink(dir, dentry, oldname);
4144 error = dir->i_op->symlink(dir, dentry, oldname);
4146 fsnotify_create(dir, dentry);
4149 EXPORT_SYMBOL(vfs_symlink);
4151 long do_symlinkat(const char __user *oldname, int newdfd,
4152 const char __user *newname)
4155 struct filename *from;
4156 struct dentry *dentry;
4158 unsigned int lookup_flags = 0;
4160 from = getname(oldname);
4162 return PTR_ERR(from);
4164 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
4165 error = PTR_ERR(dentry);
4169 error = security_path_symlink(&path, dentry, from->name);
4171 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
4172 done_path_create(&path, dentry);
4173 if (retry_estale(error, lookup_flags)) {
4174 lookup_flags |= LOOKUP_REVAL;
4182 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4183 int, newdfd, const char __user *, newname)
4185 return do_symlinkat(oldname, newdfd, newname);
4188 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
4190 return do_symlinkat(oldname, AT_FDCWD, newname);
4194 * vfs_link - create a new link
4195 * @old_dentry: object to be linked
4197 * @new_dentry: where to create the new link
4198 * @delegated_inode: returns inode needing a delegation break
4200 * The caller must hold dir->i_mutex
4202 * If vfs_link discovers a delegation on the to-be-linked file in need
4203 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4204 * inode in delegated_inode. The caller should then break the delegation
4205 * and retry. Because breaking a delegation may take a long time, the
4206 * caller should drop the i_mutex before doing so.
4208 * Alternatively, a caller may pass NULL for delegated_inode. This may
4209 * be appropriate for callers that expect the underlying filesystem not
4210 * to be NFS exported.
4212 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
4214 struct inode *inode = old_dentry->d_inode;
4215 unsigned max_links = dir->i_sb->s_max_links;
4221 error = may_create(dir, new_dentry);
4225 if (dir->i_sb != inode->i_sb)
4229 * A link to an append-only or immutable file cannot be created.
4231 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4234 * Updating the link count will likely cause i_uid and i_gid to
4235 * be writen back improperly if their true value is unknown to
4238 if (HAS_UNMAPPED_ID(inode))
4240 if (!dir->i_op->link)
4242 if (S_ISDIR(inode->i_mode))
4245 error = security_inode_link(old_dentry, dir, new_dentry);
4250 /* Make sure we don't allow creating hardlink to an unlinked file */
4251 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
4253 else if (max_links && inode->i_nlink >= max_links)
4256 error = try_break_deleg(inode, delegated_inode);
4258 error = dir->i_op->link(old_dentry, dir, new_dentry);
4261 if (!error && (inode->i_state & I_LINKABLE)) {
4262 spin_lock(&inode->i_lock);
4263 inode->i_state &= ~I_LINKABLE;
4264 spin_unlock(&inode->i_lock);
4266 inode_unlock(inode);
4268 fsnotify_link(dir, inode, new_dentry);
4271 EXPORT_SYMBOL(vfs_link);
4274 * Hardlinks are often used in delicate situations. We avoid
4275 * security-related surprises by not following symlinks on the
4278 * We don't follow them on the oldname either to be compatible
4279 * with linux 2.0, and to avoid hard-linking to directories
4280 * and other special files. --ADM
4282 int do_linkat(int olddfd, const char __user *oldname, int newdfd,
4283 const char __user *newname, int flags)
4285 struct dentry *new_dentry;
4286 struct path old_path, new_path;
4287 struct inode *delegated_inode = NULL;
4291 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
4294 * To use null names we require CAP_DAC_READ_SEARCH
4295 * This ensures that not everyone will be able to create
4296 * handlink using the passed filedescriptor.
4298 if (flags & AT_EMPTY_PATH) {
4299 if (!capable(CAP_DAC_READ_SEARCH))
4304 if (flags & AT_SYMLINK_FOLLOW)
4305 how |= LOOKUP_FOLLOW;
4307 error = user_path_at(olddfd, oldname, how, &old_path);
4311 new_dentry = user_path_create(newdfd, newname, &new_path,
4312 (how & LOOKUP_REVAL));
4313 error = PTR_ERR(new_dentry);
4314 if (IS_ERR(new_dentry))
4318 if (old_path.mnt != new_path.mnt)
4320 error = may_linkat(&old_path);
4321 if (unlikely(error))
4323 error = security_path_link(old_path.dentry, &new_path, new_dentry);
4326 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
4328 done_path_create(&new_path, new_dentry);
4329 if (delegated_inode) {
4330 error = break_deleg_wait(&delegated_inode);
4332 path_put(&old_path);
4336 if (retry_estale(error, how)) {
4337 path_put(&old_path);
4338 how |= LOOKUP_REVAL;
4342 path_put(&old_path);
4347 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4348 int, newdfd, const char __user *, newname, int, flags)
4350 return do_linkat(olddfd, oldname, newdfd, newname, flags);
4353 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
4355 return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
4359 * vfs_rename - rename a filesystem object
4360 * @old_dir: parent of source
4361 * @old_dentry: source
4362 * @new_dir: parent of destination
4363 * @new_dentry: destination
4364 * @delegated_inode: returns an inode needing a delegation break
4365 * @flags: rename flags
4367 * The caller must hold multiple mutexes--see lock_rename()).
4369 * If vfs_rename discovers a delegation in need of breaking at either
4370 * the source or destination, it will return -EWOULDBLOCK and return a
4371 * reference to the inode in delegated_inode. The caller should then
4372 * break the delegation and retry. Because breaking a delegation may
4373 * take a long time, the caller should drop all locks before doing
4376 * Alternatively, a caller may pass NULL for delegated_inode. This may
4377 * be appropriate for callers that expect the underlying filesystem not
4378 * to be NFS exported.
4380 * The worst of all namespace operations - renaming directory. "Perverted"
4381 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4384 * a) we can get into loop creation.
4385 * b) race potential - two innocent renames can create a loop together.
4386 * That's where 4.4 screws up. Current fix: serialization on
4387 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
4389 * c) we have to lock _four_ objects - parents and victim (if it exists),
4391 * And that - after we got ->i_mutex on parents (until then we don't know
4392 * whether the target exists). Solution: try to be smart with locking
4393 * order for inodes. We rely on the fact that tree topology may change
4394 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
4395 * move will be locked. Thus we can rank directories by the tree
4396 * (ancestors first) and rank all non-directories after them.
4397 * That works since everybody except rename does "lock parent, lookup,
4398 * lock child" and rename is under ->s_vfs_rename_mutex.
4399 * HOWEVER, it relies on the assumption that any object with ->lookup()
4400 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4401 * we'd better make sure that there's no link(2) for them.
4402 * d) conversion from fhandle to dentry may come in the wrong moment - when
4403 * we are removing the target. Solution: we will have to grab ->i_mutex
4404 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
4405 * ->i_mutex on parents, which works but leads to some truly excessive
4408 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4409 struct inode *new_dir, struct dentry *new_dentry,
4410 struct inode **delegated_inode, unsigned int flags)
4413 bool is_dir = d_is_dir(old_dentry);
4414 struct inode *source = old_dentry->d_inode;
4415 struct inode *target = new_dentry->d_inode;
4416 bool new_is_dir = false;
4417 unsigned max_links = new_dir->i_sb->s_max_links;
4418 struct name_snapshot old_name;
4420 if (source == target)
4423 error = may_delete(old_dir, old_dentry, is_dir);
4428 error = may_create(new_dir, new_dentry);
4430 new_is_dir = d_is_dir(new_dentry);
4432 if (!(flags & RENAME_EXCHANGE))
4433 error = may_delete(new_dir, new_dentry, is_dir);
4435 error = may_delete(new_dir, new_dentry, new_is_dir);
4440 if (!old_dir->i_op->rename)
4444 * If we are going to change the parent - check write permissions,
4445 * we'll need to flip '..'.
4447 if (new_dir != old_dir) {
4449 error = inode_permission(source, MAY_WRITE);
4453 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4454 error = inode_permission(target, MAY_WRITE);
4460 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4465 take_dentry_name_snapshot(&old_name, old_dentry);
4468 * Lock all moved children. Moved directories may need to change parent
4469 * pointer so they need the lock to prevent against concurrent
4470 * directory changes moving parent pointer. For regular files we've
4471 * historically always done this. The lockdep locking subclasses are
4472 * somewhat arbitrary but RENAME_EXCHANGE in particular can swap
4473 * regular files and directories so it's difficult to tell which
4474 * subclasses to use.
4476 lock_two_inodes(source, target, I_MUTEX_NORMAL, I_MUTEX_NONDIR2);
4479 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
4482 if (max_links && new_dir != old_dir) {
4484 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
4486 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4487 old_dir->i_nlink >= max_links)
4491 error = try_break_deleg(source, delegated_inode);
4495 if (target && !new_is_dir) {
4496 error = try_break_deleg(target, delegated_inode);
4500 error = old_dir->i_op->rename(old_dir, old_dentry,
4501 new_dir, new_dentry, flags);
4505 if (!(flags & RENAME_EXCHANGE) && target) {
4507 shrink_dcache_parent(new_dentry);
4508 target->i_flags |= S_DEAD;
4510 dont_mount(new_dentry);
4511 detach_mounts(new_dentry);
4513 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4514 if (!(flags & RENAME_EXCHANGE))
4515 d_move(old_dentry, new_dentry);
4517 d_exchange(old_dentry, new_dentry);
4520 inode_unlock(source);
4522 inode_unlock(target);
4525 fsnotify_move(old_dir, new_dir, &old_name.name, is_dir,
4526 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4527 if (flags & RENAME_EXCHANGE) {
4528 fsnotify_move(new_dir, old_dir, &old_dentry->d_name,
4529 new_is_dir, NULL, new_dentry);
4532 release_dentry_name_snapshot(&old_name);
4536 EXPORT_SYMBOL(vfs_rename);
4538 static int do_renameat2(int olddfd, const char __user *oldname, int newdfd,
4539 const char __user *newname, unsigned int flags)
4541 struct dentry *old_dentry, *new_dentry;
4542 struct dentry *trap;
4543 struct path old_path, new_path;
4544 struct qstr old_last, new_last;
4545 int old_type, new_type;
4546 struct inode *delegated_inode = NULL;
4547 struct filename *from;
4548 struct filename *to;
4549 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
4550 bool should_retry = false;
4553 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
4556 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4557 (flags & RENAME_EXCHANGE))
4560 if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4563 if (flags & RENAME_EXCHANGE)
4567 from = filename_parentat(olddfd, getname(oldname), lookup_flags,
4568 &old_path, &old_last, &old_type);
4570 error = PTR_ERR(from);
4574 to = filename_parentat(newdfd, getname(newname), lookup_flags,
4575 &new_path, &new_last, &new_type);
4577 error = PTR_ERR(to);
4582 if (old_path.mnt != new_path.mnt)
4586 if (old_type != LAST_NORM)
4589 if (flags & RENAME_NOREPLACE)
4591 if (new_type != LAST_NORM)
4594 error = mnt_want_write(old_path.mnt);
4599 trap = lock_rename(new_path.dentry, old_path.dentry);
4601 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
4602 error = PTR_ERR(old_dentry);
4603 if (IS_ERR(old_dentry))
4605 /* source must exist */
4607 if (d_is_negative(old_dentry))
4609 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
4610 error = PTR_ERR(new_dentry);
4611 if (IS_ERR(new_dentry))
4614 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4616 if (flags & RENAME_EXCHANGE) {
4618 if (d_is_negative(new_dentry))
4621 if (!d_is_dir(new_dentry)) {
4623 if (new_last.name[new_last.len])
4627 /* unless the source is a directory trailing slashes give -ENOTDIR */
4628 if (!d_is_dir(old_dentry)) {
4630 if (old_last.name[old_last.len])
4632 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
4635 /* source should not be ancestor of target */
4637 if (old_dentry == trap)
4639 /* target should not be an ancestor of source */
4640 if (!(flags & RENAME_EXCHANGE))
4642 if (new_dentry == trap)
4645 error = security_path_rename(&old_path, old_dentry,
4646 &new_path, new_dentry, flags);
4649 error = vfs_rename(old_path.dentry->d_inode, old_dentry,
4650 new_path.dentry->d_inode, new_dentry,
4651 &delegated_inode, flags);
4657 unlock_rename(new_path.dentry, old_path.dentry);
4658 if (delegated_inode) {
4659 error = break_deleg_wait(&delegated_inode);
4663 mnt_drop_write(old_path.mnt);
4665 if (retry_estale(error, lookup_flags))
4666 should_retry = true;
4667 path_put(&new_path);
4670 path_put(&old_path);
4673 should_retry = false;
4674 lookup_flags |= LOOKUP_REVAL;
4681 SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4682 int, newdfd, const char __user *, newname, unsigned int, flags)
4684 return do_renameat2(olddfd, oldname, newdfd, newname, flags);
4687 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4688 int, newdfd, const char __user *, newname)
4690 return do_renameat2(olddfd, oldname, newdfd, newname, 0);
4693 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
4695 return do_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
4698 int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4700 int error = may_create(dir, dentry);
4704 if (!dir->i_op->mknod)
4707 return dir->i_op->mknod(dir, dentry,
4708 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4710 EXPORT_SYMBOL(vfs_whiteout);
4712 int readlink_copy(char __user *buffer, int buflen, const char *link)
4714 int len = PTR_ERR(link);
4719 if (len > (unsigned) buflen)
4721 if (copy_to_user(buffer, link, len))
4728 * vfs_readlink - copy symlink body into userspace buffer
4729 * @dentry: dentry on which to get symbolic link
4730 * @buffer: user memory pointer
4731 * @buflen: size of buffer
4733 * Does not touch atime. That's up to the caller if necessary
4735 * Does not call security hook.
4737 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4739 struct inode *inode = d_inode(dentry);
4740 DEFINE_DELAYED_CALL(done);
4744 if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
4745 if (unlikely(inode->i_op->readlink))
4746 return inode->i_op->readlink(dentry, buffer, buflen);
4748 if (!d_is_symlink(dentry))
4751 spin_lock(&inode->i_lock);
4752 inode->i_opflags |= IOP_DEFAULT_READLINK;
4753 spin_unlock(&inode->i_lock);
4756 link = READ_ONCE(inode->i_link);
4758 link = inode->i_op->get_link(dentry, inode, &done);
4760 return PTR_ERR(link);
4762 res = readlink_copy(buffer, buflen, link);
4763 do_delayed_call(&done);
4766 EXPORT_SYMBOL(vfs_readlink);
4769 * vfs_get_link - get symlink body
4770 * @dentry: dentry on which to get symbolic link
4771 * @done: caller needs to free returned data with this
4773 * Calls security hook and i_op->get_link() on the supplied inode.
4775 * It does not touch atime. That's up to the caller if necessary.
4777 * Does not work on "special" symlinks like /proc/$$/fd/N
4779 const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4781 const char *res = ERR_PTR(-EINVAL);
4782 struct inode *inode = d_inode(dentry);
4784 if (d_is_symlink(dentry)) {
4785 res = ERR_PTR(security_inode_readlink(dentry));
4787 res = inode->i_op->get_link(dentry, inode, done);
4791 EXPORT_SYMBOL(vfs_get_link);
4793 /* get the link contents into pagecache */
4794 const char *page_get_link(struct dentry *dentry, struct inode *inode,
4795 struct delayed_call *callback)
4799 struct address_space *mapping = inode->i_mapping;
4802 page = find_get_page(mapping, 0);
4804 return ERR_PTR(-ECHILD);
4805 if (!PageUptodate(page)) {
4807 return ERR_PTR(-ECHILD);
4810 page = read_mapping_page(mapping, 0, NULL);
4814 set_delayed_call(callback, page_put_link, page);
4815 BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
4816 kaddr = page_address(page);
4817 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
4821 EXPORT_SYMBOL(page_get_link);
4823 void page_put_link(void *arg)
4827 EXPORT_SYMBOL(page_put_link);
4829 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4831 DEFINE_DELAYED_CALL(done);
4832 int res = readlink_copy(buffer, buflen,
4833 page_get_link(dentry, d_inode(dentry),
4835 do_delayed_call(&done);
4838 EXPORT_SYMBOL(page_readlink);
4841 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4843 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
4845 struct address_space *mapping = inode->i_mapping;
4847 void *fsdata = NULL;
4849 unsigned int flags = 0;
4851 flags |= AOP_FLAG_NOFS;
4854 err = pagecache_write_begin(NULL, mapping, 0, len-1,
4855 flags, &page, &fsdata);
4859 memcpy(page_address(page), symname, len-1);
4861 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4868 mark_inode_dirty(inode);
4873 EXPORT_SYMBOL(__page_symlink);
4875 int page_symlink(struct inode *inode, const char *symname, int len)
4877 return __page_symlink(inode, symname, len,
4878 !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
4880 EXPORT_SYMBOL(page_symlink);
4882 const struct inode_operations page_symlink_inode_operations = {
4883 .get_link = page_get_link,
4885 EXPORT_SYMBOL(page_symlink_inode_operations);