VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   32 Branches   54 Tags
b24413180f560 (Greg Kroah-Hartman      2017-11-01 15:07:57 +0100    1) // SPDX-License-Identifier: GPL-2.0
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700    2) /*
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700    3)  *  linux/fs/namei.c
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700    4)  *
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700    5)  *  Copyright (C) 1991, 1992  Linus Torvalds
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700    6)  */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700    7) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700    8) /*
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700    9)  * Some corrections by tytso.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   10)  */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   11) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   12) /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   13)  * lookup logic.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   14)  */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   15) /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   16)  */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   17) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   18) #include <linux/init.h>
630d9c47274aa (Paul Gortmaker          2011-11-16 23:57:37 -0500   19) #include <linux/export.h>
446969084d33a (David S. Miller         2012-05-23 20:12:50 -0700   20) #include <linux/kernel.h>
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   21) #include <linux/slab.h>
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   22) #include <linux/fs.h>
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   23) #include <linux/namei.h>
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   24) #include <linux/pagemap.h>
0eeca28300df1 (Robert Love             2005-07-12 17:06:03 -0400   25) #include <linux/fsnotify.h>
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   26) #include <linux/personality.h>
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   27) #include <linux/security.h>
6146f0d5e47ca (Mimi Zohar              2009-02-04 09:06:57 -0500   28) #include <linux/ima.h>
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   29) #include <linux/syscalls.h>
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   30) #include <linux/mount.h>
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   31) #include <linux/audit.h>
16f7e0fe2ecc3 (Randy Dunlap            2006-01-11 12:17:46 -0800   32) #include <linux/capability.h>
834f2a4a1554d (Trond Myklebust         2005-10-18 14:20:16 -0700   33) #include <linux/file.h>
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800   34) #include <linux/fcntl.h>
08ce5f16ee466 (Serge Hallyn            2008-04-29 01:00:10 -0700   35) #include <linux/device_cgroup.h>
5ad4e53bd5406 (Al Viro                 2009-03-29 19:50:06 -0400   36) #include <linux/fs_struct.h>
e77819e57f081 (Linus Torvalds          2011-07-22 19:30:19 -0700   37) #include <linux/posix_acl.h>
99d263d4c5b2f (Linus Torvalds          2014-09-13 11:30:10 -0700   38) #include <linux/hash.h>
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400   39) #include <linux/bitops.h>
aeaa4a79ff6a5 (Eric W. Biederman       2016-07-23 11:20:44 -0500   40) #include <linux/init_task.h>
7c0f6ba682b9c (Linus Torvalds          2016-12-24 11:46:01 -0800   41) #include <linux/uaccess.h>
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   42) 
e81e3f4dca6c5 (Eric Paris              2009-12-04 15:47:36 -0500   43) #include "internal.h"
c71053659e3bb (Al Viro                 2011-11-24 18:22:03 -0500   44) #include "mount.h"
e81e3f4dca6c5 (Eric Paris              2009-12-04 15:47:36 -0500   45) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   46) /* [Feb-1997 T. Schoebel-Theuer]
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   47)  * Fundamental changes in the pathname lookup mechanisms (namei)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   48)  * were necessary because of omirr.  The reason is that omirr needs
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   49)  * to know the _real_ pathname, not the user-supplied one, in case
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   50)  * of symlinks (and also when transname replacements occur).
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   51)  *
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   52)  * The new code replaces the old recursive symlink resolution with
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   53)  * an iterative one (in case of non-nested symlink chains).  It does
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   54)  * this with calls to <fs>_follow_link().
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   55)  * As a side effect, dir_namei(), _namei() and follow_link() are now 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   56)  * replaced with a single function lookup_dentry() that can handle all 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   57)  * the special cases of the former code.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   58)  *
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   59)  * With the new dcache, the pathname is stored at each inode, at least as
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   60)  * long as the refcount of the inode is positive.  As a side effect, the
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   61)  * size of the dcache depends on the inode cache and thus is dynamic.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   62)  *
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   63)  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   64)  * resolution to correspond with current state of the code.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   65)  *
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   66)  * Note that the symlink resolution is not *completely* iterative.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   67)  * There is still a significant amount of tail- and mid- recursion in
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   68)  * the algorithm.  Also, note that <fs>_readlink() is not used in
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   69)  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   70)  * may return different results than <fs>_follow_link().  Many virtual
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   71)  * filesystems (including /proc) exhibit this behavior.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   72)  */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   73) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   74) /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   75)  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   76)  * and the name already exists in form of a symlink, try to create the new
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   77)  * name indicated by the symlink. The old code always complained that the
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   78)  * name already exists, due to not following the symlink even if its target
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   79)  * is nonexistent.  The new semantics affects also mknod() and link() when
25985edcedea6 (Lucas De Marchi         2011-03-30 22:57:33 -0300   80)  * the name is a symlink pointing to a non-existent name.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   81)  *
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   82)  * I don't know which semantics is the right one, since I have no access
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   83)  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   84)  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   85)  * "old" one. Personally, I think the new semantics is much more logical.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   86)  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   87)  * file does succeed in both HP-UX and SunOs, but not in Solaris
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   88)  * and in the old Linux semantics.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   89)  */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   90) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   91) /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   92)  * semantics.  See the comments in "open_namei" and "do_link" below.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   93)  *
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   94)  * [10-Sep-98 Alan Modra] Another symlink change.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   95)  */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   96) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   97) /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   98)  *	inside the path - always follow.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   99)  *	in the last component in creation/removal/renaming - never follow.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  100)  *	if LOOKUP_FOLLOW passed - follow.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  101)  *	if the pathname has trailing slashes - follow.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  102)  *	otherwise - don't follow.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  103)  * (applied in that order).
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  104)  *
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  105)  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  106)  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  107)  * During the 2.4 we need to fix the userland stuff depending on it -
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  108)  * hopefully we will be able to get rid of that wart in 2.5. So far only
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  109)  * XEmacs seems to be relying on it...
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  110)  */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  111) /*
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  112)  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
a11f3a0574a57 (Arjan van de Ven        2006-03-23 03:00:33 -0800  113)  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  114)  * any extra contention...
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  115)  */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  116) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  117) /* In order to reduce some races, while at the same time doing additional
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  118)  * checking and hopefully speeding things up, we copy filenames to the
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  119)  * kernel data space before using them..
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  120)  *
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  121)  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  122)  * PATH_MAX includes the nul terminator --RR.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  123)  */
91a27b2a75678 (Jeff Layton             2012-10-10 15:25:28 -0400  124) 
fd2f7cb5bcac5 (Al Viro                 2015-02-22 20:07:13 -0500  125) #define EMBEDDED_NAME_MAX	(PATH_MAX - offsetof(struct filename, iname))
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  126) 
51f39a1f0cea1 (David Drysdale          2014-12-12 16:57:29 -0800  127) struct filename *
91a27b2a75678 (Jeff Layton             2012-10-10 15:25:28 -0400  128) getname_flags(const char __user *filename, int flags, int *empty)
91a27b2a75678 (Jeff Layton             2012-10-10 15:25:28 -0400  129) {
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  130) 	struct filename *result;
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  131) 	char *kname;
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  132) 	int len;
4043cde8ecf7f (Eric Paris              2012-01-03 14:23:08 -0500  133) 
7ac86265dc8f6 (Jeff Layton             2012-10-10 15:25:28 -0400  134) 	result = audit_reusename(filename);
7ac86265dc8f6 (Jeff Layton             2012-10-10 15:25:28 -0400  135) 	if (result)
7ac86265dc8f6 (Jeff Layton             2012-10-10 15:25:28 -0400  136) 		return result;
7ac86265dc8f6 (Jeff Layton             2012-10-10 15:25:28 -0400  137) 
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  138) 	result = __getname();
3f9f0aa687d45 (Linus Torvalds          2012-04-28 14:38:32 -0700  139) 	if (unlikely(!result))
4043cde8ecf7f (Eric Paris              2012-01-03 14:23:08 -0500  140) 		return ERR_PTR(-ENOMEM);
4043cde8ecf7f (Eric Paris              2012-01-03 14:23:08 -0500  141) 
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  142) 	/*
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  143) 	 * First, try to embed the struct filename inside the names_cache
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  144) 	 * allocation
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  145) 	 */
fd2f7cb5bcac5 (Al Viro                 2015-02-22 20:07:13 -0500  146) 	kname = (char *)result->iname;
91a27b2a75678 (Jeff Layton             2012-10-10 15:25:28 -0400  147) 	result->name = kname;
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  148) 
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  149) 	len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
91a27b2a75678 (Jeff Layton             2012-10-10 15:25:28 -0400  150) 	if (unlikely(len < 0)) {
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  151) 		__putname(result);
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  152) 		return ERR_PTR(len);
91a27b2a75678 (Jeff Layton             2012-10-10 15:25:28 -0400  153) 	}
3f9f0aa687d45 (Linus Torvalds          2012-04-28 14:38:32 -0700  154) 
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  155) 	/*
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  156) 	 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  157) 	 * separate struct filename so we can dedicate the entire
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  158) 	 * names_cache allocation for the pathname, and re-do the copy from
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  159) 	 * userland.
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  160) 	 */
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  161) 	if (unlikely(len == EMBEDDED_NAME_MAX)) {
fd2f7cb5bcac5 (Al Viro                 2015-02-22 20:07:13 -0500  162) 		const size_t size = offsetof(struct filename, iname[1]);
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  163) 		kname = (char *)result;
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  164) 
fd2f7cb5bcac5 (Al Viro                 2015-02-22 20:07:13 -0500  165) 		/*
fd2f7cb5bcac5 (Al Viro                 2015-02-22 20:07:13 -0500  166) 		 * size is chosen that way we to guarantee that
fd2f7cb5bcac5 (Al Viro                 2015-02-22 20:07:13 -0500  167) 		 * result->iname[0] is within the same object and that
fd2f7cb5bcac5 (Al Viro                 2015-02-22 20:07:13 -0500  168) 		 * kname can't be equal to result->iname, no matter what.
fd2f7cb5bcac5 (Al Viro                 2015-02-22 20:07:13 -0500  169) 		 */
fd2f7cb5bcac5 (Al Viro                 2015-02-22 20:07:13 -0500  170) 		result = kzalloc(size, GFP_KERNEL);
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  171) 		if (unlikely(!result)) {
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  172) 			__putname(kname);
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  173) 			return ERR_PTR(-ENOMEM);
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  174) 		}
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  175) 		result->name = kname;
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  176) 		len = strncpy_from_user(kname, filename, PATH_MAX);
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  177) 		if (unlikely(len < 0)) {
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  178) 			__putname(kname);
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  179) 			kfree(result);
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  180) 			return ERR_PTR(len);
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  181) 		}
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  182) 		if (unlikely(len == PATH_MAX)) {
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  183) 			__putname(kname);
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  184) 			kfree(result);
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  185) 			return ERR_PTR(-ENAMETOOLONG);
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  186) 		}
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  187) 	}
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  188) 
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  189) 	result->refcnt = 1;
3f9f0aa687d45 (Linus Torvalds          2012-04-28 14:38:32 -0700  190) 	/* The empty path is special. */
3f9f0aa687d45 (Linus Torvalds          2012-04-28 14:38:32 -0700  191) 	if (unlikely(!len)) {
3f9f0aa687d45 (Linus Torvalds          2012-04-28 14:38:32 -0700  192) 		if (empty)
4043cde8ecf7f (Eric Paris              2012-01-03 14:23:08 -0500  193) 			*empty = 1;
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  194) 		if (!(flags & LOOKUP_EMPTY)) {
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  195) 			putname(result);
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  196) 			return ERR_PTR(-ENOENT);
94b5d2621aef5 (Al Viro                 2015-02-22 19:38:03 -0500  197) 		}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  198) 	}
3f9f0aa687d45 (Linus Torvalds          2012-04-28 14:38:32 -0700  199) 
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  200) 	result->uptr = filename;
c4ad8f98bef77 (Linus Torvalds          2014-02-05 12:54:53 -0800  201) 	result->aname = NULL;
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  202) 	audit_getname(result);
7950e3852ab86 (Jeff Layton             2012-10-10 16:43:13 -0400  203) 	return result;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  204) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  205) 
91a27b2a75678 (Jeff Layton             2012-10-10 15:25:28 -0400  206) struct filename *
91a27b2a75678 (Jeff Layton             2012-10-10 15:25:28 -0400  207) getname(const char __user * filename)
f52e0c11305aa (Al Viro                 2011-03-14 18:56:51 -0400  208) {
f7493e5d9cc10 (Linus Torvalds          2012-03-22 16:10:40 -0700  209) 	return getname_flags(filename, 0, NULL);
f52e0c11305aa (Al Viro                 2011-03-14 18:56:51 -0400  210) }
f52e0c11305aa (Al Viro                 2011-03-14 18:56:51 -0400  211) 
c4ad8f98bef77 (Linus Torvalds          2014-02-05 12:54:53 -0800  212) struct filename *
c4ad8f98bef77 (Linus Torvalds          2014-02-05 12:54:53 -0800  213) getname_kernel(const char * filename)
c4ad8f98bef77 (Linus Torvalds          2014-02-05 12:54:53 -0800  214) {
c4ad8f98bef77 (Linus Torvalds          2014-02-05 12:54:53 -0800  215) 	struct filename *result;
08518549722f0 (Paul Moore              2015-01-21 23:59:56 -0500  216) 	int len = strlen(filename) + 1;
c4ad8f98bef77 (Linus Torvalds          2014-02-05 12:54:53 -0800  217) 
c4ad8f98bef77 (Linus Torvalds          2014-02-05 12:54:53 -0800  218) 	result = __getname();
c4ad8f98bef77 (Linus Torvalds          2014-02-05 12:54:53 -0800  219) 	if (unlikely(!result))
c4ad8f98bef77 (Linus Torvalds          2014-02-05 12:54:53 -0800  220) 		return ERR_PTR(-ENOMEM);
c4ad8f98bef77 (Linus Torvalds          2014-02-05 12:54:53 -0800  221) 
08518549722f0 (Paul Moore              2015-01-21 23:59:56 -0500  222) 	if (len <= EMBEDDED_NAME_MAX) {
fd2f7cb5bcac5 (Al Viro                 2015-02-22 20:07:13 -0500  223) 		result->name = (char *)result->iname;
08518549722f0 (Paul Moore              2015-01-21 23:59:56 -0500  224) 	} else if (len <= PATH_MAX) {
30ce4d1903e1d (Al Viro                 2018-04-08 11:57:10 -0400  225) 		const size_t size = offsetof(struct filename, iname[1]);
08518549722f0 (Paul Moore              2015-01-21 23:59:56 -0500  226) 		struct filename *tmp;
08518549722f0 (Paul Moore              2015-01-21 23:59:56 -0500  227) 
30ce4d1903e1d (Al Viro                 2018-04-08 11:57:10 -0400  228) 		tmp = kmalloc(size, GFP_KERNEL);
08518549722f0 (Paul Moore              2015-01-21 23:59:56 -0500  229) 		if (unlikely(!tmp)) {
08518549722f0 (Paul Moore              2015-01-21 23:59:56 -0500  230) 			__putname(result);
08518549722f0 (Paul Moore              2015-01-21 23:59:56 -0500  231) 			return ERR_PTR(-ENOMEM);
08518549722f0 (Paul Moore              2015-01-21 23:59:56 -0500  232) 		}
08518549722f0 (Paul Moore              2015-01-21 23:59:56 -0500  233) 		tmp->name = (char *)result;
08518549722f0 (Paul Moore              2015-01-21 23:59:56 -0500  234) 		result = tmp;
08518549722f0 (Paul Moore              2015-01-21 23:59:56 -0500  235) 	} else {
08518549722f0 (Paul Moore              2015-01-21 23:59:56 -0500  236) 		__putname(result);
08518549722f0 (Paul Moore              2015-01-21 23:59:56 -0500  237) 		return ERR_PTR(-ENAMETOOLONG);
08518549722f0 (Paul Moore              2015-01-21 23:59:56 -0500  238) 	}
08518549722f0 (Paul Moore              2015-01-21 23:59:56 -0500  239) 	memcpy((char *)result->name, filename, len);
c4ad8f98bef77 (Linus Torvalds          2014-02-05 12:54:53 -0800  240) 	result->uptr = NULL;
c4ad8f98bef77 (Linus Torvalds          2014-02-05 12:54:53 -0800  241) 	result->aname = NULL;
55422d0bd292f (Paul Moore              2015-01-22 00:00:23 -0500  242) 	result->refcnt = 1;
fd3522fdc8402 (Paul Moore              2015-01-22 00:00:10 -0500  243) 	audit_getname(result);
c4ad8f98bef77 (Linus Torvalds          2014-02-05 12:54:53 -0800  244) 
c4ad8f98bef77 (Linus Torvalds          2014-02-05 12:54:53 -0800  245) 	return result;
c4ad8f98bef77 (Linus Torvalds          2014-02-05 12:54:53 -0800  246) }
c4ad8f98bef77 (Linus Torvalds          2014-02-05 12:54:53 -0800  247) 
91a27b2a75678 (Jeff Layton             2012-10-10 15:25:28 -0400  248) void putname(struct filename *name)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  249) {
55422d0bd292f (Paul Moore              2015-01-22 00:00:23 -0500  250) 	BUG_ON(name->refcnt <= 0);
55422d0bd292f (Paul Moore              2015-01-22 00:00:23 -0500  251) 
55422d0bd292f (Paul Moore              2015-01-22 00:00:23 -0500  252) 	if (--name->refcnt > 0)
55422d0bd292f (Paul Moore              2015-01-22 00:00:23 -0500  253) 		return;
55422d0bd292f (Paul Moore              2015-01-22 00:00:23 -0500  254) 
fd2f7cb5bcac5 (Al Viro                 2015-02-22 20:07:13 -0500  255) 	if (name->name != name->iname) {
55422d0bd292f (Paul Moore              2015-01-22 00:00:23 -0500  256) 		__putname(name->name);
55422d0bd292f (Paul Moore              2015-01-22 00:00:23 -0500  257) 		kfree(name);
55422d0bd292f (Paul Moore              2015-01-22 00:00:23 -0500  258) 	} else
55422d0bd292f (Paul Moore              2015-01-22 00:00:23 -0500  259) 		__putname(name);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  260) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  261) 
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  262) /**
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  263)  * check_acl - perform ACL permission checking
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  264)  * @mnt_userns:	user namespace of the mount the inode was found from
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  265)  * @inode:	inode to check permissions on
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  266)  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  267)  *
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  268)  * This function performs the ACL permission checking. Since this function
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  269)  * retrieve POSIX acls it needs to know whether it is called from a blocking or
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  270)  * non-blocking context and thus cares about the MAY_NOT_BLOCK bit.
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  271)  *
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  272)  * If the inode has been found through an idmapped mount the user namespace of
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  273)  * the vfsmount must be passed through @mnt_userns. This function will then take
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  274)  * care to map the inode according to @mnt_userns before checking permissions.
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  275)  * On non-idmapped mounts or if permission checking is to be performed on the
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  276)  * raw inode simply passs init_user_ns.
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  277)  */
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  278) static int check_acl(struct user_namespace *mnt_userns,
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  279) 		     struct inode *inode, int mask)
e77819e57f081 (Linus Torvalds          2011-07-22 19:30:19 -0700  280) {
84635d68be4b8 (Linus Torvalds          2011-07-25 22:47:03 -0700  281) #ifdef CONFIG_FS_POSIX_ACL
e77819e57f081 (Linus Torvalds          2011-07-22 19:30:19 -0700  282) 	struct posix_acl *acl;
e77819e57f081 (Linus Torvalds          2011-07-22 19:30:19 -0700  283) 
e77819e57f081 (Linus Torvalds          2011-07-22 19:30:19 -0700  284) 	if (mask & MAY_NOT_BLOCK) {
3567866bf2619 (Al Viro                 2011-08-02 21:32:13 -0400  285) 		acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
3567866bf2619 (Al Viro                 2011-08-02 21:32:13 -0400  286) 	        if (!acl)
e77819e57f081 (Linus Torvalds          2011-07-22 19:30:19 -0700  287) 	                return -EAGAIN;
3567866bf2619 (Al Viro                 2011-08-02 21:32:13 -0400  288) 		/* no ->get_acl() calls in RCU mode... */
b8a7a3a667472 (Andreas Gruenbacher     2016-03-24 14:38:37 +0100  289) 		if (is_uncached_acl(acl))
3567866bf2619 (Al Viro                 2011-08-02 21:32:13 -0400  290) 			return -ECHILD;
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  291) 	        return posix_acl_permission(mnt_userns, inode, acl, mask);
e77819e57f081 (Linus Torvalds          2011-07-22 19:30:19 -0700  292) 	}
e77819e57f081 (Linus Torvalds          2011-07-22 19:30:19 -0700  293) 
2982baa2ae31e (Christoph Hellwig       2013-12-20 05:16:38 -0800  294) 	acl = get_acl(inode, ACL_TYPE_ACCESS);
2982baa2ae31e (Christoph Hellwig       2013-12-20 05:16:38 -0800  295) 	if (IS_ERR(acl))
2982baa2ae31e (Christoph Hellwig       2013-12-20 05:16:38 -0800  296) 		return PTR_ERR(acl);
e77819e57f081 (Linus Torvalds          2011-07-22 19:30:19 -0700  297) 	if (acl) {
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  298) 	        int error = posix_acl_permission(mnt_userns, inode, acl, mask);
e77819e57f081 (Linus Torvalds          2011-07-22 19:30:19 -0700  299) 	        posix_acl_release(acl);
e77819e57f081 (Linus Torvalds          2011-07-22 19:30:19 -0700  300) 	        return error;
e77819e57f081 (Linus Torvalds          2011-07-22 19:30:19 -0700  301) 	}
84635d68be4b8 (Linus Torvalds          2011-07-25 22:47:03 -0700  302) #endif
e77819e57f081 (Linus Torvalds          2011-07-22 19:30:19 -0700  303) 
e77819e57f081 (Linus Torvalds          2011-07-22 19:30:19 -0700  304) 	return -EAGAIN;
e77819e57f081 (Linus Torvalds          2011-07-22 19:30:19 -0700  305) }
e77819e57f081 (Linus Torvalds          2011-07-22 19:30:19 -0700  306) 
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  307) /**
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  308)  * acl_permission_check - perform basic UNIX permission checking
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  309)  * @mnt_userns:	user namespace of the mount the inode was found from
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  310)  * @inode:	inode to check permissions on
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  311)  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  312)  *
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  313)  * This function performs the basic UNIX permission checking. Since this
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  314)  * function may retrieve POSIX acls it needs to know whether it is called from a
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  315)  * blocking or non-blocking context and thus cares about the MAY_NOT_BLOCK bit.
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  316)  *
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  317)  * If the inode has been found through an idmapped mount the user namespace of
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  318)  * the vfsmount must be passed through @mnt_userns. This function will then take
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  319)  * care to map the inode according to @mnt_userns before checking permissions.
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  320)  * On non-idmapped mounts or if permission checking is to be performed on the
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  321)  * raw inode simply passs init_user_ns.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  322)  */
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  323) static int acl_permission_check(struct user_namespace *mnt_userns,
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  324) 				struct inode *inode, int mask)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  325) {
26cf46be954a2 (Linus Torvalds          2011-05-13 11:51:01 -0700  326) 	unsigned int mode = inode->i_mode;
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  327) 	kuid_t i_uid;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  328) 
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  329) 	/* Are we the owner? If so, ACL's don't matter */
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  330) 	i_uid = i_uid_into_mnt(mnt_userns, inode);
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  331) 	if (likely(uid_eq(current_fsuid(), i_uid))) {
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  332) 		mask &= 7;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  333) 		mode >>= 6;
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  334) 		return (mask & ~mode) ? -EACCES : 0;
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  335) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  336) 
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  337) 	/* Do we have ACL's? */
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  338) 	if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  339) 		int error = check_acl(mnt_userns, inode, mask);
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  340) 		if (error != -EAGAIN)
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  341) 			return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  342) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  343) 
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  344) 	/* Only RWX matters for group/other mode bits */
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  345) 	mask &= 7;
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  346) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  347) 	/*
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  348) 	 * Are the group permissions different from
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  349) 	 * the other permissions in the bits we care
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  350) 	 * about? Need to check group ownership if so.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  351) 	 */
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  352) 	if (mask & (mode ^ (mode >> 3))) {
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  353) 		kgid_t kgid = i_gid_into_mnt(mnt_userns, inode);
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  354) 		if (in_group_p(kgid))
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  355) 			mode >>= 3;
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  356) 	}
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  357) 
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  358) 	/* Bits in 'mode' clear that we require? */
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  359) 	return (mask & ~mode) ? -EACCES : 0;
5909ccaa300a4 (Linus Torvalds          2009-08-28 11:51:25 -0700  360) }
5909ccaa300a4 (Linus Torvalds          2009-08-28 11:51:25 -0700  361) 
5909ccaa300a4 (Linus Torvalds          2009-08-28 11:51:25 -0700  362) /**
b74c79e99389c (Nicholas Piggin         2011-01-07 17:49:58 +1100  363)  * generic_permission -  check for access rights on a Posix-like filesystem
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  364)  * @mnt_userns:	user namespace of the mount the inode was found from
5909ccaa300a4 (Linus Torvalds          2009-08-28 11:51:25 -0700  365)  * @inode:	inode to check access rights for
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  366)  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC,
5fc475b749c72 (Linus Torvalds          2020-06-05 13:40:45 -0700  367)  *		%MAY_NOT_BLOCK ...)
5909ccaa300a4 (Linus Torvalds          2009-08-28 11:51:25 -0700  368)  *
5909ccaa300a4 (Linus Torvalds          2009-08-28 11:51:25 -0700  369)  * Used to check for read/write/execute permissions on a file.
5909ccaa300a4 (Linus Torvalds          2009-08-28 11:51:25 -0700  370)  * We use "fsuid" for this, letting us set arbitrary permissions
5909ccaa300a4 (Linus Torvalds          2009-08-28 11:51:25 -0700  371)  * for filesystem access without changing the "normal" uids which
b74c79e99389c (Nicholas Piggin         2011-01-07 17:49:58 +1100  372)  * are used for other things.
b74c79e99389c (Nicholas Piggin         2011-01-07 17:49:58 +1100  373)  *
b74c79e99389c (Nicholas Piggin         2011-01-07 17:49:58 +1100  374)  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
b74c79e99389c (Nicholas Piggin         2011-01-07 17:49:58 +1100  375)  * request cannot be satisfied (eg. requires blocking or too much complexity).
b74c79e99389c (Nicholas Piggin         2011-01-07 17:49:58 +1100  376)  * It would then be called again in ref-walk mode.
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  377)  *
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  378)  * If the inode has been found through an idmapped mount the user namespace of
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  379)  * the vfsmount must be passed through @mnt_userns. This function will then take
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  380)  * care to map the inode according to @mnt_userns before checking permissions.
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  381)  * On non-idmapped mounts or if permission checking is to be performed on the
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  382)  * raw inode simply passs init_user_ns.
5909ccaa300a4 (Linus Torvalds          2009-08-28 11:51:25 -0700  383)  */
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  384) int generic_permission(struct user_namespace *mnt_userns, struct inode *inode,
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  385) 		       int mask)
5909ccaa300a4 (Linus Torvalds          2009-08-28 11:51:25 -0700  386) {
5909ccaa300a4 (Linus Torvalds          2009-08-28 11:51:25 -0700  387) 	int ret;
5909ccaa300a4 (Linus Torvalds          2009-08-28 11:51:25 -0700  388) 
5909ccaa300a4 (Linus Torvalds          2009-08-28 11:51:25 -0700  389) 	/*
948409c74d217 (Andreas Gruenbacher     2011-10-23 23:13:33 +0530  390) 	 * Do the basic permission checks.
5909ccaa300a4 (Linus Torvalds          2009-08-28 11:51:25 -0700  391) 	 */
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  392) 	ret = acl_permission_check(mnt_userns, inode, mask);
5909ccaa300a4 (Linus Torvalds          2009-08-28 11:51:25 -0700  393) 	if (ret != -EACCES)
5909ccaa300a4 (Linus Torvalds          2009-08-28 11:51:25 -0700  394) 		return ret;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  395) 
d594e7ec4da6c (Al Viro                 2011-06-20 19:55:42 -0400  396) 	if (S_ISDIR(inode->i_mode)) {
d594e7ec4da6c (Al Viro                 2011-06-20 19:55:42 -0400  397) 		/* DACs are overridable for directories */
d594e7ec4da6c (Al Viro                 2011-06-20 19:55:42 -0400  398) 		if (!(mask & MAY_WRITE))
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  399) 			if (capable_wrt_inode_uidgid(mnt_userns, inode,
23adbe12ef7d3 (Andy Lutomirski         2014-06-10 12:45:42 -0700  400) 						     CAP_DAC_READ_SEARCH))
d594e7ec4da6c (Al Viro                 2011-06-20 19:55:42 -0400  401) 				return 0;
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  402) 		if (capable_wrt_inode_uidgid(mnt_userns, inode,
0558c1bf5a081 (Christian Brauner       2021-01-21 14:19:23 +0100  403) 					     CAP_DAC_OVERRIDE))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  404) 			return 0;
2a4c22426955d (Stephen Smalley         2017-03-10 12:14:18 -0500  405) 		return -EACCES;
2a4c22426955d (Stephen Smalley         2017-03-10 12:14:18 -0500  406) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  407) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  408) 	/*
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  409) 	 * Searching includes executable on directories, else just read.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  410) 	 */
7ea6600148c26 (Serge Hallyn            2009-12-29 14:50:19 -0600  411) 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
d594e7ec4da6c (Al Viro                 2011-06-20 19:55:42 -0400  412) 	if (mask == MAY_READ)
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  413) 		if (capable_wrt_inode_uidgid(mnt_userns, inode,
0558c1bf5a081 (Christian Brauner       2021-01-21 14:19:23 +0100  414) 					     CAP_DAC_READ_SEARCH))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  415) 			return 0;
2a4c22426955d (Stephen Smalley         2017-03-10 12:14:18 -0500  416) 	/*
2a4c22426955d (Stephen Smalley         2017-03-10 12:14:18 -0500  417) 	 * Read/write DACs are always overridable.
2a4c22426955d (Stephen Smalley         2017-03-10 12:14:18 -0500  418) 	 * Executable DACs are overridable when there is
2a4c22426955d (Stephen Smalley         2017-03-10 12:14:18 -0500  419) 	 * at least one exec bit set.
2a4c22426955d (Stephen Smalley         2017-03-10 12:14:18 -0500  420) 	 */
2a4c22426955d (Stephen Smalley         2017-03-10 12:14:18 -0500  421) 	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  422) 		if (capable_wrt_inode_uidgid(mnt_userns, inode,
0558c1bf5a081 (Christian Brauner       2021-01-21 14:19:23 +0100  423) 					     CAP_DAC_OVERRIDE))
2a4c22426955d (Stephen Smalley         2017-03-10 12:14:18 -0500  424) 			return 0;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  425) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  426) 	return -EACCES;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  427) }
4d359507346a1 (Al Viro                 2014-03-14 12:20:17 -0400  428) EXPORT_SYMBOL(generic_permission);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  429) 
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  430) /**
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  431)  * do_inode_permission - UNIX permission checking
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  432)  * @mnt_userns:	user namespace of the mount the inode was found from
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  433)  * @inode:	inode to check permissions on
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  434)  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  435)  *
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700  436)  * We _really_ want to just do "generic_permission()" without
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700  437)  * even looking at the inode->i_op values. So we keep a cache
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700  438)  * flag in inode->i_opflags, that says "this has not special
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700  439)  * permission function, use the fast case".
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700  440)  */
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  441) static inline int do_inode_permission(struct user_namespace *mnt_userns,
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  442) 				      struct inode *inode, int mask)
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700  443) {
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700  444) 	if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700  445) 		if (likely(inode->i_op->permission))
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100  446) 			return inode->i_op->permission(mnt_userns, inode, mask);
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700  447) 
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700  448) 		/* This gets set once for the inode lifetime */
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700  449) 		spin_lock(&inode->i_lock);
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700  450) 		inode->i_opflags |= IOP_FASTPERM;
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700  451) 		spin_unlock(&inode->i_lock);
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700  452) 	}
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  453) 	return generic_permission(mnt_userns, inode, mask);
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700  454) }
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700  455) 
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  456) /**
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  457)  * sb_permission - Check superblock-level permissions
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  458)  * @sb: Superblock of inode to check permission on
55852635a8e28 (Randy Dunlap            2012-08-18 17:39:25 -0700  459)  * @inode: Inode to check permission on
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  460)  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  461)  *
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  462)  * Separate out file-system wide checks from inode-specific permission checks.
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  463)  */
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  464) static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  465) {
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  466) 	if (unlikely(mask & MAY_WRITE)) {
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  467) 		umode_t mode = inode->i_mode;
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  468) 
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  469) 		/* Nobody gets write access to a read-only fs. */
bc98a42c1f7d0 (David Howells           2017-07-17 08:45:34 +0100  470) 		if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  471) 			return -EROFS;
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  472) 	}
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  473) 	return 0;
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  474) }
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  475) 
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  476) /**
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  477)  * inode_permission - Check for access rights to a given inode
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  478)  * @mnt_userns:	User namespace of the mount the inode was found from
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  479)  * @inode:	Inode to check permission on
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  480)  * @mask:	Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  481)  *
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  482)  * Check for read/write/execute permissions on an inode.  We use fs[ug]id for
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  483)  * this, letting us set arbitrary permissions for filesystem access without
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  484)  * changing the "normal" UIDs which are used for other things.
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  485)  *
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  486)  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  487)  */
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  488) int inode_permission(struct user_namespace *mnt_userns,
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  489) 		     struct inode *inode, int mask)
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  490) {
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  491) 	int retval;
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  492) 
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  493) 	retval = sb_permission(inode->i_sb, inode, mask);
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  494) 	if (retval)
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  495) 		return retval;
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  496) 
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  497) 	if (unlikely(mask & MAY_WRITE)) {
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  498) 		/*
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  499) 		 * Nobody gets write access to an immutable file.
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  500) 		 */
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  501) 		if (IS_IMMUTABLE(inode))
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  502) 			return -EPERM;
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  503) 
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  504) 		/*
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  505) 		 * Updating mtime will likely cause i_uid and i_gid to be
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  506) 		 * written back improperly if their true value is unknown
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  507) 		 * to the vfs.
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  508) 		 */
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100  509) 		if (HAS_UNMAPPED_ID(mnt_userns, inode))
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  510) 			return -EACCES;
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  511) 	}
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  512) 
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100  513) 	retval = do_inode_permission(mnt_userns, inode, mask);
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  514) 	if (retval)
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  515) 		return retval;
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  516) 
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  517) 	retval = devcgroup_inode_permission(inode, mask);
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  518) 	if (retval)
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  519) 		return retval;
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  520) 
4bfd054ae11ea (Eric Biggers            2018-01-16 21:44:24 -0800  521) 	return security_inode_permission(inode, mask);
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  522) }
4d359507346a1 (Al Viro                 2014-03-14 12:20:17 -0400  523) EXPORT_SYMBOL(inode_permission);
0bdaea9017b9d (David Howells           2012-06-25 12:55:46 +0100  524) 
5dd784d04924b (Jan Blunck              2008-02-14 19:34:38 -0800  525) /**
5dd784d04924b (Jan Blunck              2008-02-14 19:34:38 -0800  526)  * path_get - get a reference to a path
5dd784d04924b (Jan Blunck              2008-02-14 19:34:38 -0800  527)  * @path: path to get the reference to
5dd784d04924b (Jan Blunck              2008-02-14 19:34:38 -0800  528)  *
5dd784d04924b (Jan Blunck              2008-02-14 19:34:38 -0800  529)  * Given a path increment the reference count to the dentry and the vfsmount.
5dd784d04924b (Jan Blunck              2008-02-14 19:34:38 -0800  530)  */
dcf787f39162c (Al Viro                 2013-03-01 23:51:07 -0500  531) void path_get(const struct path *path)
5dd784d04924b (Jan Blunck              2008-02-14 19:34:38 -0800  532) {
5dd784d04924b (Jan Blunck              2008-02-14 19:34:38 -0800  533) 	mntget(path->mnt);
5dd784d04924b (Jan Blunck              2008-02-14 19:34:38 -0800  534) 	dget(path->dentry);
5dd784d04924b (Jan Blunck              2008-02-14 19:34:38 -0800  535) }
5dd784d04924b (Jan Blunck              2008-02-14 19:34:38 -0800  536) EXPORT_SYMBOL(path_get);
5dd784d04924b (Jan Blunck              2008-02-14 19:34:38 -0800  537) 
1d957f9bf87da (Jan Blunck              2008-02-14 19:34:35 -0800  538) /**
1d957f9bf87da (Jan Blunck              2008-02-14 19:34:35 -0800  539)  * path_put - put a reference to a path
1d957f9bf87da (Jan Blunck              2008-02-14 19:34:35 -0800  540)  * @path: path to put the reference to
1d957f9bf87da (Jan Blunck              2008-02-14 19:34:35 -0800  541)  *
1d957f9bf87da (Jan Blunck              2008-02-14 19:34:35 -0800  542)  * Given a path decrement the reference count to the dentry and the vfsmount.
1d957f9bf87da (Jan Blunck              2008-02-14 19:34:35 -0800  543)  */
dcf787f39162c (Al Viro                 2013-03-01 23:51:07 -0500  544) void path_put(const struct path *path)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  545) {
1d957f9bf87da (Jan Blunck              2008-02-14 19:34:35 -0800  546) 	dput(path->dentry);
1d957f9bf87da (Jan Blunck              2008-02-14 19:34:35 -0800  547) 	mntput(path->mnt);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  548) }
1d957f9bf87da (Jan Blunck              2008-02-14 19:34:35 -0800  549) EXPORT_SYMBOL(path_put);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  550) 
894bc8c4662ba (Al Viro                 2015-05-02 07:16:16 -0400  551) #define EMBEDDED_LEVELS 2
1f55a6ec940fb (Al Viro                 2014-11-01 19:30:41 -0400  552) struct nameidata {
1f55a6ec940fb (Al Viro                 2014-11-01 19:30:41 -0400  553) 	struct path	path;
1cf2665b5bdfc (Al Viro                 2015-05-06 16:01:56 -0400  554) 	struct qstr	last;
1f55a6ec940fb (Al Viro                 2014-11-01 19:30:41 -0400  555) 	struct path	root;
1f55a6ec940fb (Al Viro                 2014-11-01 19:30:41 -0400  556) 	struct inode	*inode; /* path.dentry.d_inode */
1f55a6ec940fb (Al Viro                 2014-11-01 19:30:41 -0400  557) 	unsigned int	flags;
ab87f9a56c8ee (Aleksa Sarai            2019-12-07 01:13:35 +1100  558) 	unsigned	seq, m_seq, r_seq;
1f55a6ec940fb (Al Viro                 2014-11-01 19:30:41 -0400  559) 	int		last_type;
1f55a6ec940fb (Al Viro                 2014-11-01 19:30:41 -0400  560) 	unsigned	depth;
756daf263ea53 (NeilBrown               2015-03-23 13:37:38 +1100  561) 	int		total_link_count;
697fc6ca666cd (Al Viro                 2015-05-02 19:38:35 -0400  562) 	struct saved {
697fc6ca666cd (Al Viro                 2015-05-02 19:38:35 -0400  563) 		struct path link;
fceef393a5381 (Al Viro                 2015-12-29 15:58:39 -0500  564) 		struct delayed_call done;
697fc6ca666cd (Al Viro                 2015-05-02 19:38:35 -0400  565) 		const char *name;
0450b2d120ed9 (Al Viro                 2015-05-08 13:23:53 -0400  566) 		unsigned seq;
894bc8c4662ba (Al Viro                 2015-05-02 07:16:16 -0400  567) 	} *stack, internal[EMBEDDED_LEVELS];
9883d1855ecfa (Al Viro                 2015-05-13 07:28:08 -0400  568) 	struct filename	*name;
9883d1855ecfa (Al Viro                 2015-05-13 07:28:08 -0400  569) 	struct nameidata *saved;
9883d1855ecfa (Al Viro                 2015-05-13 07:28:08 -0400  570) 	unsigned	root_seq;
9883d1855ecfa (Al Viro                 2015-05-13 07:28:08 -0400  571) 	int		dfd;
0f705953012a7 (Al Viro                 2020-03-05 11:34:48 -0500  572) 	kuid_t		dir_uid;
0f705953012a7 (Al Viro                 2020-03-05 11:34:48 -0500  573) 	umode_t		dir_mode;
3859a271a003a (Kees Cook               2016-10-28 01:22:25 -0700  574) } __randomize_layout;
1f55a6ec940fb (Al Viro                 2014-11-01 19:30:41 -0400  575) 
9883d1855ecfa (Al Viro                 2015-05-13 07:28:08 -0400  576) static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
894bc8c4662ba (Al Viro                 2015-05-02 07:16:16 -0400  577) {
756daf263ea53 (NeilBrown               2015-03-23 13:37:38 +1100  578) 	struct nameidata *old = current->nameidata;
756daf263ea53 (NeilBrown               2015-03-23 13:37:38 +1100  579) 	p->stack = p->internal;
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400  580) 	p->dfd = dfd;
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400  581) 	p->name = name;
7d01ef7585c07 (Al Viro                 2021-04-06 12:33:07 -0400  582) 	p->path.mnt = NULL;
7d01ef7585c07 (Al Viro                 2021-04-06 12:33:07 -0400  583) 	p->path.dentry = NULL;
756daf263ea53 (NeilBrown               2015-03-23 13:37:38 +1100  584) 	p->total_link_count = old ? old->total_link_count : 0;
9883d1855ecfa (Al Viro                 2015-05-13 07:28:08 -0400  585) 	p->saved = old;
756daf263ea53 (NeilBrown               2015-03-23 13:37:38 +1100  586) 	current->nameidata = p;
894bc8c4662ba (Al Viro                 2015-05-02 07:16:16 -0400  587) }
894bc8c4662ba (Al Viro                 2015-05-02 07:16:16 -0400  588) 
9883d1855ecfa (Al Viro                 2015-05-13 07:28:08 -0400  589) static void restore_nameidata(void)
894bc8c4662ba (Al Viro                 2015-05-02 07:16:16 -0400  590) {
9883d1855ecfa (Al Viro                 2015-05-13 07:28:08 -0400  591) 	struct nameidata *now = current->nameidata, *old = now->saved;
756daf263ea53 (NeilBrown               2015-03-23 13:37:38 +1100  592) 
756daf263ea53 (NeilBrown               2015-03-23 13:37:38 +1100  593) 	current->nameidata = old;
756daf263ea53 (NeilBrown               2015-03-23 13:37:38 +1100  594) 	if (old)
756daf263ea53 (NeilBrown               2015-03-23 13:37:38 +1100  595) 		old->total_link_count = now->total_link_count;
e1a63bbc40c00 (Al Viro                 2015-12-05 21:06:33 -0500  596) 	if (now->stack != now->internal)
756daf263ea53 (NeilBrown               2015-03-23 13:37:38 +1100  597) 		kfree(now->stack);
894bc8c4662ba (Al Viro                 2015-05-02 07:16:16 -0400  598) }
894bc8c4662ba (Al Viro                 2015-05-02 07:16:16 -0400  599) 
60ef60c7d7291 (Al Viro                 2020-03-03 11:43:55 -0500  600) static bool nd_alloc_stack(struct nameidata *nd)
894bc8c4662ba (Al Viro                 2015-05-02 07:16:16 -0400  601) {
bc40aee053be1 (Al Viro                 2015-05-09 13:04:24 -0400  602) 	struct saved *p;
bc40aee053be1 (Al Viro                 2015-05-09 13:04:24 -0400  603) 
60ef60c7d7291 (Al Viro                 2020-03-03 11:43:55 -0500  604) 	p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved),
60ef60c7d7291 (Al Viro                 2020-03-03 11:43:55 -0500  605) 			 nd->flags & LOOKUP_RCU ? GFP_ATOMIC : GFP_KERNEL);
60ef60c7d7291 (Al Viro                 2020-03-03 11:43:55 -0500  606) 	if (unlikely(!p))
60ef60c7d7291 (Al Viro                 2020-03-03 11:43:55 -0500  607) 		return false;
894bc8c4662ba (Al Viro                 2015-05-02 07:16:16 -0400  608) 	memcpy(p, nd->internal, sizeof(nd->internal));
894bc8c4662ba (Al Viro                 2015-05-02 07:16:16 -0400  609) 	nd->stack = p;
60ef60c7d7291 (Al Viro                 2020-03-03 11:43:55 -0500  610) 	return true;
894bc8c4662ba (Al Viro                 2015-05-02 07:16:16 -0400  611) }
894bc8c4662ba (Al Viro                 2015-05-02 07:16:16 -0400  612) 
397d425dc26da (Eric W. Biederman       2015-08-15 20:27:13 -0500  613) /**
6b03f7edf43e3 (Al Viro                 2020-02-24 15:53:19 -0500  614)  * path_connected - Verify that a dentry is below mnt.mnt_root
397d425dc26da (Eric W. Biederman       2015-08-15 20:27:13 -0500  615)  *
397d425dc26da (Eric W. Biederman       2015-08-15 20:27:13 -0500  616)  * Rename can sometimes move a file or directory outside of a bind
397d425dc26da (Eric W. Biederman       2015-08-15 20:27:13 -0500  617)  * mount, path_connected allows those cases to be detected.
397d425dc26da (Eric W. Biederman       2015-08-15 20:27:13 -0500  618)  */
6b03f7edf43e3 (Al Viro                 2020-02-24 15:53:19 -0500  619) static bool path_connected(struct vfsmount *mnt, struct dentry *dentry)
397d425dc26da (Eric W. Biederman       2015-08-15 20:27:13 -0500  620) {
95dd77580ccd6 (Eric W. Biederman       2018-03-14 18:20:29 -0500  621) 	struct super_block *sb = mnt->mnt_sb;
397d425dc26da (Eric W. Biederman       2015-08-15 20:27:13 -0500  622) 
402dd2cf46b17 (Christoph Hellwig       2020-09-24 08:51:28 +0200  623) 	/* Bind mounts can have disconnected paths */
402dd2cf46b17 (Christoph Hellwig       2020-09-24 08:51:28 +0200  624) 	if (mnt->mnt_root == sb->s_root)
397d425dc26da (Eric W. Biederman       2015-08-15 20:27:13 -0500  625) 		return true;
397d425dc26da (Eric W. Biederman       2015-08-15 20:27:13 -0500  626) 
6b03f7edf43e3 (Al Viro                 2020-02-24 15:53:19 -0500  627) 	return is_subdir(dentry, mnt->mnt_root);
397d425dc26da (Eric W. Biederman       2015-08-15 20:27:13 -0500  628) }
397d425dc26da (Eric W. Biederman       2015-08-15 20:27:13 -0500  629) 
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  630) static void drop_links(struct nameidata *nd)
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  631) {
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  632) 	int i = nd->depth;
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  633) 	while (i--) {
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  634) 		struct saved *last = nd->stack + i;
fceef393a5381 (Al Viro                 2015-12-29 15:58:39 -0500  635) 		do_delayed_call(&last->done);
fceef393a5381 (Al Viro                 2015-12-29 15:58:39 -0500  636) 		clear_delayed_call(&last->done);
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  637) 	}
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  638) }
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  639) 
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  640) static void terminate_walk(struct nameidata *nd)
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  641) {
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  642) 	drop_links(nd);
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  643) 	if (!(nd->flags & LOOKUP_RCU)) {
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  644) 		int i;
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  645) 		path_put(&nd->path);
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  646) 		for (i = 0; i < nd->depth; i++)
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  647) 			path_put(&nd->stack[i].link);
84a2bd39405ff (Al Viro                 2019-07-16 21:20:17 -0400  648) 		if (nd->flags & LOOKUP_ROOT_GRABBED) {
102b8af266fbc (Al Viro                 2015-05-12 17:35:52 -0400  649) 			path_put(&nd->root);
84a2bd39405ff (Al Viro                 2019-07-16 21:20:17 -0400  650) 			nd->flags &= ~LOOKUP_ROOT_GRABBED;
102b8af266fbc (Al Viro                 2015-05-12 17:35:52 -0400  651) 		}
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  652) 	} else {
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  653) 		nd->flags &= ~LOOKUP_RCU;
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  654) 		rcu_read_unlock();
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  655) 	}
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  656) 	nd->depth = 0;
7d01ef7585c07 (Al Viro                 2021-04-06 12:33:07 -0400  657) 	nd->path.mnt = NULL;
7d01ef7585c07 (Al Viro                 2021-04-06 12:33:07 -0400  658) 	nd->path.dentry = NULL;
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  659) }
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  660) 
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  661) /* path_put is needed afterwards regardless of success or failure */
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500  662) static bool __legitimize_path(struct path *path, unsigned seq, unsigned mseq)
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  663) {
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500  664) 	int res = __legitimize_mnt(path->mnt, mseq);
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  665) 	if (unlikely(res)) {
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  666) 		if (res > 0)
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  667) 			path->mnt = NULL;
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  668) 		path->dentry = NULL;
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  669) 		return false;
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  670) 	}
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  671) 	if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  672) 		path->dentry = NULL;
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  673) 		return false;
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  674) 	}
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  675) 	return !read_seqcount_retry(&path->dentry->d_seq, seq);
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  676) }
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  677) 
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500  678) static inline bool legitimize_path(struct nameidata *nd,
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500  679) 			    struct path *path, unsigned seq)
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500  680) {
5bd73286d50fc (Al Viro                 2020-04-05 21:59:55 -0400  681) 	return __legitimize_path(path, seq, nd->m_seq);
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500  682) }
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500  683) 
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  684) static bool legitimize_links(struct nameidata *nd)
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  685) {
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  686) 	int i;
eacd9aa8cedeb (Al Viro                 2021-02-15 12:03:23 -0500  687) 	if (unlikely(nd->flags & LOOKUP_CACHED)) {
eacd9aa8cedeb (Al Viro                 2021-02-15 12:03:23 -0500  688) 		drop_links(nd);
eacd9aa8cedeb (Al Viro                 2021-02-15 12:03:23 -0500  689) 		nd->depth = 0;
eacd9aa8cedeb (Al Viro                 2021-02-15 12:03:23 -0500  690) 		return false;
eacd9aa8cedeb (Al Viro                 2021-02-15 12:03:23 -0500  691) 	}
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  692) 	for (i = 0; i < nd->depth; i++) {
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  693) 		struct saved *last = nd->stack + i;
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  694) 		if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  695) 			drop_links(nd);
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  696) 			nd->depth = i + 1;
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  697) 			return false;
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  698) 		}
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  699) 	}
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  700) 	return true;
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  701) }
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  702) 
ee594bfff389a (Al Viro                 2019-07-16 21:05:36 -0400  703) static bool legitimize_root(struct nameidata *nd)
ee594bfff389a (Al Viro                 2019-07-16 21:05:36 -0400  704) {
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  705) 	/*
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  706) 	 * For scoped-lookups (where nd->root has been zeroed), we need to
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  707) 	 * restart the whole lookup from scratch -- because set_root() is wrong
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  708) 	 * for these lookups (nd->dfd is the root, not the filesystem root).
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  709) 	 */
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  710) 	if (!nd->root.mnt && (nd->flags & LOOKUP_IS_SCOPED))
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  711) 		return false;
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  712) 	/* Nothing to do if nd->root is zero or is managed by the VFS user. */
ee594bfff389a (Al Viro                 2019-07-16 21:05:36 -0400  713) 	if (!nd->root.mnt || (nd->flags & LOOKUP_ROOT))
ee594bfff389a (Al Viro                 2019-07-16 21:05:36 -0400  714) 		return true;
84a2bd39405ff (Al Viro                 2019-07-16 21:20:17 -0400  715) 	nd->flags |= LOOKUP_ROOT_GRABBED;
ee594bfff389a (Al Viro                 2019-07-16 21:05:36 -0400  716) 	return legitimize_path(nd, &nd->root, nd->root_seq);
ee594bfff389a (Al Viro                 2019-07-16 21:05:36 -0400  717) }
ee594bfff389a (Al Viro                 2019-07-16 21:05:36 -0400  718) 
19660af736ba0 (Al Viro                 2011-03-25 10:32:48 -0400  719) /*
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100  720)  * Path walking has 2 modes, rcu-walk and ref-walk (see
19660af736ba0 (Al Viro                 2011-03-25 10:32:48 -0400  721)  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
19660af736ba0 (Al Viro                 2011-03-25 10:32:48 -0400  722)  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
57e3715cfa3fb (Mike Marshall           2015-11-30 11:11:59 -0500  723)  * normal reference counts on dentries and vfsmounts to transition to ref-walk
19660af736ba0 (Al Viro                 2011-03-25 10:32:48 -0400  724)  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
19660af736ba0 (Al Viro                 2011-03-25 10:32:48 -0400  725)  * got stuck, so ref-walk may continue from there. If this is not successful
19660af736ba0 (Al Viro                 2011-03-25 10:32:48 -0400  726)  * (eg. a seqcount has changed), then failure is returned and it's up to caller
19660af736ba0 (Al Viro                 2011-03-25 10:32:48 -0400  727)  * to restart the path walk from the beginning in ref-walk mode.
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100  728)  */
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100  729) 
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100  730) /**
e36cffed20a32 (Jens Axboe              2020-12-17 09:19:08 -0700  731)  * try_to_unlazy - try to switch to ref-walk mode.
19660af736ba0 (Al Viro                 2011-03-25 10:32:48 -0400  732)  * @nd: nameidata pathwalk data
e36cffed20a32 (Jens Axboe              2020-12-17 09:19:08 -0700  733)  * Returns: true on success, false on failure
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100  734)  *
e36cffed20a32 (Jens Axboe              2020-12-17 09:19:08 -0700  735)  * try_to_unlazy attempts to legitimize the current nd->path and nd->root
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  736)  * for ref-walk mode.
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  737)  * Must be called from rcu-walk context.
e36cffed20a32 (Jens Axboe              2020-12-17 09:19:08 -0700  738)  * Nothing should touch nameidata between try_to_unlazy() failure and
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  739)  * terminate_walk().
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100  740)  */
e36cffed20a32 (Jens Axboe              2020-12-17 09:19:08 -0700  741) static bool try_to_unlazy(struct nameidata *nd)
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100  742) {
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100  743) 	struct dentry *parent = nd->path.dentry;
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100  744) 
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100  745) 	BUG_ON(!(nd->flags & LOOKUP_RCU));
e5c832d555882 (Linus Torvalds          2013-09-08 18:13:49 -0700  746) 
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  747) 	nd->flags &= ~LOOKUP_RCU;
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  748) 	if (unlikely(!legitimize_links(nd)))
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  749) 		goto out1;
84a2bd39405ff (Al Viro                 2019-07-16 21:20:17 -0400  750) 	if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
84a2bd39405ff (Al Viro                 2019-07-16 21:20:17 -0400  751) 		goto out;
ee594bfff389a (Al Viro                 2019-07-16 21:05:36 -0400  752) 	if (unlikely(!legitimize_root(nd)))
ee594bfff389a (Al Viro                 2019-07-16 21:05:36 -0400  753) 		goto out;
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  754) 	rcu_read_unlock();
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  755) 	BUG_ON(nd->inode != parent->d_inode);
e36cffed20a32 (Jens Axboe              2020-12-17 09:19:08 -0700  756) 	return true;
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  757) 
84a2bd39405ff (Al Viro                 2019-07-16 21:20:17 -0400  758) out1:
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  759) 	nd->path.mnt = NULL;
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  760) 	nd->path.dentry = NULL;
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  761) out:
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  762) 	rcu_read_unlock();
e36cffed20a32 (Jens Axboe              2020-12-17 09:19:08 -0700  763) 	return false;
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  764) }
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  765) 
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  766) /**
ae66db45fd309 (Al Viro                 2021-01-04 00:08:41 -0500  767)  * try_to_unlazy_next - try to switch to ref-walk mode.
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  768)  * @nd: nameidata pathwalk data
ae66db45fd309 (Al Viro                 2021-01-04 00:08:41 -0500  769)  * @dentry: next dentry to step into
ae66db45fd309 (Al Viro                 2021-01-04 00:08:41 -0500  770)  * @seq: seq number to check @dentry against
ae66db45fd309 (Al Viro                 2021-01-04 00:08:41 -0500  771)  * Returns: true on success, false on failure
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  772)  *
ae66db45fd309 (Al Viro                 2021-01-04 00:08:41 -0500  773)  * Similar to to try_to_unlazy(), but here we have the next dentry already
ae66db45fd309 (Al Viro                 2021-01-04 00:08:41 -0500  774)  * picked by rcu-walk and want to legitimize that in addition to the current
ae66db45fd309 (Al Viro                 2021-01-04 00:08:41 -0500  775)  * nd->path and nd->root for ref-walk mode.  Must be called from rcu-walk context.
ae66db45fd309 (Al Viro                 2021-01-04 00:08:41 -0500  776)  * Nothing should touch nameidata between try_to_unlazy_next() failure and
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  777)  * terminate_walk().
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  778)  */
ae66db45fd309 (Al Viro                 2021-01-04 00:08:41 -0500  779) static bool try_to_unlazy_next(struct nameidata *nd, struct dentry *dentry, unsigned seq)
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  780) {
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  781) 	BUG_ON(!(nd->flags & LOOKUP_RCU));
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  782) 
e5c832d555882 (Linus Torvalds          2013-09-08 18:13:49 -0700  783) 	nd->flags &= ~LOOKUP_RCU;
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  784) 	if (unlikely(!legitimize_links(nd)))
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  785) 		goto out2;
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  786) 	if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  787) 		goto out2;
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  788) 	if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref)))
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  789) 		goto out1;
48a066e72d970 (Al Viro                 2013-09-29 22:06:07 -0400  790) 
15570086b590a (Linus Torvalds          2013-09-02 11:38:06 -0700  791) 	/*
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  792) 	 * We need to move both the parent and the dentry from the RCU domain
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  793) 	 * to be properly refcounted. And the sequence number in the dentry
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  794) 	 * validates *both* dentry counters, since we checked the sequence
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  795) 	 * number of the parent after we got the child sequence number. So we
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  796) 	 * know the parent must still be valid if the child sequence number is
15570086b590a (Linus Torvalds          2013-09-02 11:38:06 -0700  797) 	 */
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  798) 	if (unlikely(!lockref_get_not_dead(&dentry->d_lockref)))
4675ac39b5dd5 (Al Viro                 2017-01-09 22:29:15 -0500  799) 		goto out;
84a2bd39405ff (Al Viro                 2019-07-16 21:20:17 -0400  800) 	if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
84a2bd39405ff (Al Viro                 2019-07-16 21:20:17 -0400  801) 		goto out_dput;
e5c832d555882 (Linus Torvalds          2013-09-08 18:13:49 -0700  802) 	/*
e5c832d555882 (Linus Torvalds          2013-09-08 18:13:49 -0700  803) 	 * Sequence counts matched. Now make sure that the root is
e5c832d555882 (Linus Torvalds          2013-09-08 18:13:49 -0700  804) 	 * still valid and get it if required.
e5c832d555882 (Linus Torvalds          2013-09-08 18:13:49 -0700  805) 	 */
84a2bd39405ff (Al Viro                 2019-07-16 21:20:17 -0400  806) 	if (unlikely(!legitimize_root(nd)))
84a2bd39405ff (Al Viro                 2019-07-16 21:20:17 -0400  807) 		goto out_dput;
8b61e74ffc631 (Al Viro                 2013-11-08 12:45:01 -0500  808) 	rcu_read_unlock();
ae66db45fd309 (Al Viro                 2021-01-04 00:08:41 -0500  809) 	return true;
19660af736ba0 (Al Viro                 2011-03-25 10:32:48 -0400  810) 
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  811) out2:
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  812) 	nd->path.mnt = NULL;
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  813) out1:
7973387a2f629 (Al Viro                 2015-05-09 12:55:43 -0400  814) 	nd->path.dentry = NULL;
e5c832d555882 (Linus Torvalds          2013-09-08 18:13:49 -0700  815) out:
8b61e74ffc631 (Al Viro                 2013-11-08 12:45:01 -0500  816) 	rcu_read_unlock();
ae66db45fd309 (Al Viro                 2021-01-04 00:08:41 -0500  817) 	return false;
84a2bd39405ff (Al Viro                 2019-07-16 21:20:17 -0400  818) out_dput:
84a2bd39405ff (Al Viro                 2019-07-16 21:20:17 -0400  819) 	rcu_read_unlock();
84a2bd39405ff (Al Viro                 2019-07-16 21:20:17 -0400  820) 	dput(dentry);
ae66db45fd309 (Al Viro                 2021-01-04 00:08:41 -0500  821) 	return false;
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100  822) }
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100  823) 
4ce16ef3fed92 (Al Viro                 2012-06-10 16:10:59 -0400  824) static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
34286d6662308 (Nicholas Piggin         2011-01-07 17:49:57 +1100  825) {
a89f833737e6c (Al Viro                 2017-01-09 22:25:28 -0500  826) 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
a89f833737e6c (Al Viro                 2017-01-09 22:25:28 -0500  827) 		return dentry->d_op->d_revalidate(dentry, flags);
a89f833737e6c (Al Viro                 2017-01-09 22:25:28 -0500  828) 	else
a89f833737e6c (Al Viro                 2017-01-09 22:25:28 -0500  829) 		return 1;
34286d6662308 (Nicholas Piggin         2011-01-07 17:49:57 +1100  830) }
34286d6662308 (Nicholas Piggin         2011-01-07 17:49:57 +1100  831) 
9f1fafee9e42b (Al Viro                 2011-03-25 11:00:12 -0400  832) /**
9f1fafee9e42b (Al Viro                 2011-03-25 11:00:12 -0400  833)  * complete_walk - successful completion of path walk
9f1fafee9e42b (Al Viro                 2011-03-25 11:00:12 -0400  834)  * @nd:  pointer nameidata
39159de2a091a (Jeff Layton             2009-12-07 12:01:50 -0500  835)  *
9f1fafee9e42b (Al Viro                 2011-03-25 11:00:12 -0400  836)  * If we had been in RCU mode, drop out of it and legitimize nd->path.
9f1fafee9e42b (Al Viro                 2011-03-25 11:00:12 -0400  837)  * Revalidate the final result, unless we'd already done that during
9f1fafee9e42b (Al Viro                 2011-03-25 11:00:12 -0400  838)  * the path walk or the filesystem doesn't ask for it.  Return 0 on
9f1fafee9e42b (Al Viro                 2011-03-25 11:00:12 -0400  839)  * success, -error on failure.  In case of failure caller does not
9f1fafee9e42b (Al Viro                 2011-03-25 11:00:12 -0400  840)  * need to drop nd->path.
39159de2a091a (Jeff Layton             2009-12-07 12:01:50 -0500  841)  */
9f1fafee9e42b (Al Viro                 2011-03-25 11:00:12 -0400  842) static int complete_walk(struct nameidata *nd)
39159de2a091a (Jeff Layton             2009-12-07 12:01:50 -0500  843) {
16c2cd7179881 (Al Viro                 2011-02-22 15:50:10 -0500  844) 	struct dentry *dentry = nd->path.dentry;
39159de2a091a (Jeff Layton             2009-12-07 12:01:50 -0500  845) 	int status;
39159de2a091a (Jeff Layton             2009-12-07 12:01:50 -0500  846) 
9f1fafee9e42b (Al Viro                 2011-03-25 11:00:12 -0400  847) 	if (nd->flags & LOOKUP_RCU) {
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  848) 		/*
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  849) 		 * We don't want to zero nd->root for scoped-lookups or
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  850) 		 * externally-managed nd->root.
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  851) 		 */
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  852) 		if (!(nd->flags & (LOOKUP_ROOT | LOOKUP_IS_SCOPED)))
9f1fafee9e42b (Al Viro                 2011-03-25 11:00:12 -0400  853) 			nd->root.mnt = NULL;
6c6ec2b0a3e03 (Jens Axboe              2020-12-17 09:19:09 -0700  854) 		nd->flags &= ~LOOKUP_CACHED;
e36cffed20a32 (Jens Axboe              2020-12-17 09:19:08 -0700  855) 		if (!try_to_unlazy(nd))
9f1fafee9e42b (Al Viro                 2011-03-25 11:00:12 -0400  856) 			return -ECHILD;
9f1fafee9e42b (Al Viro                 2011-03-25 11:00:12 -0400  857) 	}
9f1fafee9e42b (Al Viro                 2011-03-25 11:00:12 -0400  858) 
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  859) 	if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  860) 		/*
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  861) 		 * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  862) 		 * ever step outside the root during lookup" and should already
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  863) 		 * be guaranteed by the rest of namei, we want to avoid a namei
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  864) 		 * BUG resulting in userspace being given a path that was not
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  865) 		 * scoped within the root at some point during the lookup.
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  866) 		 *
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  867) 		 * So, do a final sanity-check to make sure that in the
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  868) 		 * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED)
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  869) 		 * we won't silently return an fd completely outside of the
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  870) 		 * requested root to userspace.
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  871) 		 *
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  872) 		 * Userspace could move the path outside the root after this
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  873) 		 * check, but as discussed elsewhere this is not a concern (the
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  874) 		 * resolved file was inside the root at some point).
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  875) 		 */
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  876) 		if (!path_is_under(&nd->path, &nd->root))
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  877) 			return -EXDEV;
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  878) 	}
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  879) 
16c2cd7179881 (Al Viro                 2011-02-22 15:50:10 -0500  880) 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
16c2cd7179881 (Al Viro                 2011-02-22 15:50:10 -0500  881) 		return 0;
16c2cd7179881 (Al Viro                 2011-02-22 15:50:10 -0500  882) 
ecf3d1f1aa74d (Jeff Layton             2013-02-20 11:19:05 -0500  883) 	if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
39159de2a091a (Jeff Layton             2009-12-07 12:01:50 -0500  884) 		return 0;
39159de2a091a (Jeff Layton             2009-12-07 12:01:50 -0500  885) 
ecf3d1f1aa74d (Jeff Layton             2013-02-20 11:19:05 -0500  886) 	status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
39159de2a091a (Jeff Layton             2009-12-07 12:01:50 -0500  887) 	if (status > 0)
39159de2a091a (Jeff Layton             2009-12-07 12:01:50 -0500  888) 		return 0;
39159de2a091a (Jeff Layton             2009-12-07 12:01:50 -0500  889) 
16c2cd7179881 (Al Viro                 2011-02-22 15:50:10 -0500  890) 	if (!status)
39159de2a091a (Jeff Layton             2009-12-07 12:01:50 -0500  891) 		status = -ESTALE;
16c2cd7179881 (Al Viro                 2011-02-22 15:50:10 -0500  892) 
39159de2a091a (Jeff Layton             2009-12-07 12:01:50 -0500  893) 	return status;
39159de2a091a (Jeff Layton             2009-12-07 12:01:50 -0500  894) }
39159de2a091a (Jeff Layton             2009-12-07 12:01:50 -0500  895) 
740a16782750a (Aleksa Sarai            2019-12-07 01:13:29 +1100  896) static int set_root(struct nameidata *nd)
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100  897) {
7bd88377d482e (Al Viro                 2014-09-13 21:55:46 -0400  898) 	struct fs_struct *fs = current->fs;
c28cc36469554 (Nicholas Piggin         2011-01-07 17:49:53 +1100  899) 
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  900) 	/*
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  901) 	 * Jumping to the real root in a scoped-lookup is a BUG in namei, but we
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  902) 	 * still have to ensure it doesn't happen because it will cause a breakout
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  903) 	 * from the dirfd.
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  904) 	 */
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  905) 	if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED))
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  906) 		return -ENOTRECOVERABLE;
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  907) 
9e6697e26f988 (Al Viro                 2015-12-05 20:07:21 -0500  908) 	if (nd->flags & LOOKUP_RCU) {
9e6697e26f988 (Al Viro                 2015-12-05 20:07:21 -0500  909) 		unsigned seq;
9e6697e26f988 (Al Viro                 2015-12-05 20:07:21 -0500  910) 
9e6697e26f988 (Al Viro                 2015-12-05 20:07:21 -0500  911) 		do {
9e6697e26f988 (Al Viro                 2015-12-05 20:07:21 -0500  912) 			seq = read_seqcount_begin(&fs->seq);
9e6697e26f988 (Al Viro                 2015-12-05 20:07:21 -0500  913) 			nd->root = fs->root;
9e6697e26f988 (Al Viro                 2015-12-05 20:07:21 -0500  914) 			nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
9e6697e26f988 (Al Viro                 2015-12-05 20:07:21 -0500  915) 		} while (read_seqcount_retry(&fs->seq, seq));
9e6697e26f988 (Al Viro                 2015-12-05 20:07:21 -0500  916) 	} else {
9e6697e26f988 (Al Viro                 2015-12-05 20:07:21 -0500  917) 		get_fs_root(fs, &nd->root);
84a2bd39405ff (Al Viro                 2019-07-16 21:20:17 -0400  918) 		nd->flags |= LOOKUP_ROOT_GRABBED;
9e6697e26f988 (Al Viro                 2015-12-05 20:07:21 -0500  919) 	}
740a16782750a (Aleksa Sarai            2019-12-07 01:13:29 +1100  920) 	return 0;
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100  921) }
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100  922) 
248fb5b9557aa (Al Viro                 2015-12-05 20:51:58 -0500  923) static int nd_jump_root(struct nameidata *nd)
248fb5b9557aa (Al Viro                 2015-12-05 20:51:58 -0500  924) {
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  925) 	if (unlikely(nd->flags & LOOKUP_BENEATH))
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  926) 		return -EXDEV;
72ba29297e143 (Aleksa Sarai            2019-12-07 01:13:32 +1100  927) 	if (unlikely(nd->flags & LOOKUP_NO_XDEV)) {
72ba29297e143 (Aleksa Sarai            2019-12-07 01:13:32 +1100  928) 		/* Absolute path arguments to path_init() are allowed. */
72ba29297e143 (Aleksa Sarai            2019-12-07 01:13:32 +1100  929) 		if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt)
72ba29297e143 (Aleksa Sarai            2019-12-07 01:13:32 +1100  930) 			return -EXDEV;
72ba29297e143 (Aleksa Sarai            2019-12-07 01:13:32 +1100  931) 	}
740a16782750a (Aleksa Sarai            2019-12-07 01:13:29 +1100  932) 	if (!nd->root.mnt) {
740a16782750a (Aleksa Sarai            2019-12-07 01:13:29 +1100  933) 		int error = set_root(nd);
740a16782750a (Aleksa Sarai            2019-12-07 01:13:29 +1100  934) 		if (error)
740a16782750a (Aleksa Sarai            2019-12-07 01:13:29 +1100  935) 			return error;
740a16782750a (Aleksa Sarai            2019-12-07 01:13:29 +1100  936) 	}
248fb5b9557aa (Al Viro                 2015-12-05 20:51:58 -0500  937) 	if (nd->flags & LOOKUP_RCU) {
248fb5b9557aa (Al Viro                 2015-12-05 20:51:58 -0500  938) 		struct dentry *d;
248fb5b9557aa (Al Viro                 2015-12-05 20:51:58 -0500  939) 		nd->path = nd->root;
248fb5b9557aa (Al Viro                 2015-12-05 20:51:58 -0500  940) 		d = nd->path.dentry;
248fb5b9557aa (Al Viro                 2015-12-05 20:51:58 -0500  941) 		nd->inode = d->d_inode;
248fb5b9557aa (Al Viro                 2015-12-05 20:51:58 -0500  942) 		nd->seq = nd->root_seq;
248fb5b9557aa (Al Viro                 2015-12-05 20:51:58 -0500  943) 		if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
248fb5b9557aa (Al Viro                 2015-12-05 20:51:58 -0500  944) 			return -ECHILD;
248fb5b9557aa (Al Viro                 2015-12-05 20:51:58 -0500  945) 	} else {
248fb5b9557aa (Al Viro                 2015-12-05 20:51:58 -0500  946) 		path_put(&nd->path);
248fb5b9557aa (Al Viro                 2015-12-05 20:51:58 -0500  947) 		nd->path = nd->root;
248fb5b9557aa (Al Viro                 2015-12-05 20:51:58 -0500  948) 		path_get(&nd->path);
248fb5b9557aa (Al Viro                 2015-12-05 20:51:58 -0500  949) 		nd->inode = nd->path.dentry->d_inode;
248fb5b9557aa (Al Viro                 2015-12-05 20:51:58 -0500  950) 	}
248fb5b9557aa (Al Viro                 2015-12-05 20:51:58 -0500  951) 	nd->flags |= LOOKUP_JUMPED;
248fb5b9557aa (Al Viro                 2015-12-05 20:51:58 -0500  952) 	return 0;
248fb5b9557aa (Al Viro                 2015-12-05 20:51:58 -0500  953) }
248fb5b9557aa (Al Viro                 2015-12-05 20:51:58 -0500  954) 
b5fb63c18315c (Christoph Hellwig       2012-06-18 10:47:04 -0400  955) /*
6b2553918d8b4 (Al Viro                 2015-11-17 10:20:54 -0500  956)  * Helper to directly jump to a known parsed path from ->get_link,
b5fb63c18315c (Christoph Hellwig       2012-06-18 10:47:04 -0400  957)  * caller must have taken a reference to path beforehand.
b5fb63c18315c (Christoph Hellwig       2012-06-18 10:47:04 -0400  958)  */
1bc82070fa276 (Aleksa Sarai            2019-12-07 01:13:28 +1100  959) int nd_jump_link(struct path *path)
b5fb63c18315c (Christoph Hellwig       2012-06-18 10:47:04 -0400  960) {
4b99d4996979d (Aleksa Sarai            2019-12-07 01:13:31 +1100  961) 	int error = -ELOOP;
6e77137b363b8 (Al Viro                 2015-05-02 13:37:52 -0400  962) 	struct nameidata *nd = current->nameidata;
b5fb63c18315c (Christoph Hellwig       2012-06-18 10:47:04 -0400  963) 
4b99d4996979d (Aleksa Sarai            2019-12-07 01:13:31 +1100  964) 	if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS))
4b99d4996979d (Aleksa Sarai            2019-12-07 01:13:31 +1100  965) 		goto err;
4b99d4996979d (Aleksa Sarai            2019-12-07 01:13:31 +1100  966) 
72ba29297e143 (Aleksa Sarai            2019-12-07 01:13:32 +1100  967) 	error = -EXDEV;
72ba29297e143 (Aleksa Sarai            2019-12-07 01:13:32 +1100  968) 	if (unlikely(nd->flags & LOOKUP_NO_XDEV)) {
72ba29297e143 (Aleksa Sarai            2019-12-07 01:13:32 +1100  969) 		if (nd->path.mnt != path->mnt)
72ba29297e143 (Aleksa Sarai            2019-12-07 01:13:32 +1100  970) 			goto err;
72ba29297e143 (Aleksa Sarai            2019-12-07 01:13:32 +1100  971) 	}
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  972) 	/* Not currently safe for scoped-lookups. */
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  973) 	if (unlikely(nd->flags & LOOKUP_IS_SCOPED))
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100  974) 		goto err;
72ba29297e143 (Aleksa Sarai            2019-12-07 01:13:32 +1100  975) 
4b99d4996979d (Aleksa Sarai            2019-12-07 01:13:31 +1100  976) 	path_put(&nd->path);
b5fb63c18315c (Christoph Hellwig       2012-06-18 10:47:04 -0400  977) 	nd->path = *path;
b5fb63c18315c (Christoph Hellwig       2012-06-18 10:47:04 -0400  978) 	nd->inode = nd->path.dentry->d_inode;
b5fb63c18315c (Christoph Hellwig       2012-06-18 10:47:04 -0400  979) 	nd->flags |= LOOKUP_JUMPED;
1bc82070fa276 (Aleksa Sarai            2019-12-07 01:13:28 +1100  980) 	return 0;
4b99d4996979d (Aleksa Sarai            2019-12-07 01:13:31 +1100  981) 
4b99d4996979d (Aleksa Sarai            2019-12-07 01:13:31 +1100  982) err:
4b99d4996979d (Aleksa Sarai            2019-12-07 01:13:31 +1100  983) 	path_put(path);
4b99d4996979d (Aleksa Sarai            2019-12-07 01:13:31 +1100  984) 	return error;
b5fb63c18315c (Christoph Hellwig       2012-06-18 10:47:04 -0400  985) }
b5fb63c18315c (Christoph Hellwig       2012-06-18 10:47:04 -0400  986) 
b9ff44293c64b (Al Viro                 2015-05-02 20:19:23 -0400  987) static inline void put_link(struct nameidata *nd)
574197e0de46a (Al Viro                 2011-03-14 22:20:34 -0400  988) {
21c3003d36a8b (Al Viro                 2015-05-03 21:06:24 -0400  989) 	struct saved *last = nd->stack + --nd->depth;
fceef393a5381 (Al Viro                 2015-12-29 15:58:39 -0500  990) 	do_delayed_call(&last->done);
6548fae2eca6b (Al Viro                 2015-05-07 20:32:22 -0400  991) 	if (!(nd->flags & LOOKUP_RCU))
6548fae2eca6b (Al Viro                 2015-05-07 20:32:22 -0400  992) 		path_put(&last->link);
574197e0de46a (Al Viro                 2011-03-14 22:20:34 -0400  993) }
574197e0de46a (Al Viro                 2011-03-14 22:20:34 -0400  994) 
561ec64ae67ef (Linus Torvalds          2012-10-26 10:05:07 -0700  995) int sysctl_protected_symlinks __read_mostly = 0;
561ec64ae67ef (Linus Torvalds          2012-10-26 10:05:07 -0700  996) int sysctl_protected_hardlinks __read_mostly = 0;
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700  997) int sysctl_protected_fifos __read_mostly;
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700  998) int sysctl_protected_regular __read_mostly;
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700  999) 
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1000) /**
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1001)  * may_follow_link - Check symlink following for unsafe situations
55852635a8e28 (Randy Dunlap            2012-08-18 17:39:25 -0700 1002)  * @nd: nameidata pathwalk data
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1003)  *
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1004)  * In the case of the sysctl_protected_symlinks sysctl being enabled,
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1005)  * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1006)  * in a sticky world-writable directory. This is to protect privileged
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1007)  * processes from failing races against path names that may change out
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1008)  * from under them by way of other users creating malicious symlinks.
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1009)  * It will permit symlinks to be followed only when outside a sticky
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1010)  * world-writable directory, or when the uid of the symlink and follower
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1011)  * match, or when the directory owner matches the symlink's owner.
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1012)  *
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1013)  * Returns 0 if following the symlink is allowed, -ve on error.
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1014)  */
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1015) static inline int may_follow_link(struct nameidata *nd, const struct inode *inode)
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1016) {
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1017) 	struct user_namespace *mnt_userns;
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1018) 	kuid_t i_uid;
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1019) 
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1020) 	if (!sysctl_protected_symlinks)
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1021) 		return 0;
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1022) 
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1023) 	mnt_userns = mnt_user_ns(nd->path.mnt);
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1024) 	i_uid = i_uid_into_mnt(mnt_userns, inode);
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1025) 	/* Allowed if owner and follower match. */
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1026) 	if (uid_eq(current_cred()->fsuid, i_uid))
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1027) 		return 0;
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1028) 
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1029) 	/* Allowed if parent directory not sticky and world-writable. */
0f705953012a7 (Al Viro                 2020-03-05 11:34:48 -0500 1030) 	if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1031) 		return 0;
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1032) 
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1033) 	/* Allowed if parent directory and link owner match. */
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1034) 	if (uid_valid(nd->dir_uid) && uid_eq(nd->dir_uid, i_uid))
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1035) 		return 0;
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1036) 
31956502dd2c9 (Al Viro                 2015-05-07 20:37:40 -0400 1037) 	if (nd->flags & LOOKUP_RCU)
31956502dd2c9 (Al Viro                 2015-05-07 20:37:40 -0400 1038) 		return -ECHILD;
31956502dd2c9 (Al Viro                 2015-05-07 20:37:40 -0400 1039) 
ea841bafda3f7 (Richard Guy Briggs      2018-03-21 04:42:21 -0400 1040) 	audit_inode(nd->name, nd->stack[0].link.dentry, 0);
245d73698ed7a (Kees Cook               2019-10-02 16:41:58 -0700 1041) 	audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link");
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1042) 	return -EACCES;
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1043) }
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1044) 
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1045) /**
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1046)  * safe_hardlink_source - Check for safe hardlink conditions
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1047)  * @mnt_userns:	user namespace of the mount the inode was found from
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1048)  * @inode: the source inode to hardlink from
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1049)  *
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1050)  * Return false if at least one of the following conditions:
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1051)  *    - inode is not a regular file
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1052)  *    - inode is setuid
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1053)  *    - inode is setgid and group-exec
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1054)  *    - access failure for read and write
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1055)  *
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1056)  * Otherwise returns true.
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1057)  */
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1058) static bool safe_hardlink_source(struct user_namespace *mnt_userns,
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1059) 				 struct inode *inode)
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1060) {
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1061) 	umode_t mode = inode->i_mode;
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1062) 
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1063) 	/* Special files should not get pinned to the filesystem. */
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1064) 	if (!S_ISREG(mode))
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1065) 		return false;
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1066) 
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1067) 	/* Setuid files should not get pinned to the filesystem. */
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1068) 	if (mode & S_ISUID)
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1069) 		return false;
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1070) 
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1071) 	/* Executable setgid files should not get pinned to the filesystem. */
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1072) 	if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1073) 		return false;
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1074) 
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1075) 	/* Hardlinking to unreadable or unwritable sources is dangerous. */
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1076) 	if (inode_permission(mnt_userns, inode, MAY_READ | MAY_WRITE))
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1077) 		return false;
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1078) 
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1079) 	return true;
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1080) }
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1081) 
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1082) /**
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1083)  * may_linkat - Check permissions for creating a hardlink
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1084)  * @mnt_userns:	user namespace of the mount the inode was found from
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1085)  * @link: the source to hardlink from
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1086)  *
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1087)  * Block hardlink when all of:
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1088)  *  - sysctl_protected_hardlinks enabled
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1089)  *  - fsuid does not match inode
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1090)  *  - hardlink source is unsafe (see safe_hardlink_source() above)
f2ca379642d7a (Dirk Steinmetz          2015-10-20 16:09:19 +0200 1091)  *  - not CAP_FOWNER in a namespace with the inode owner uid mapped
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1092)  *
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1093)  * If the inode has been found through an idmapped mount the user namespace of
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1094)  * the vfsmount must be passed through @mnt_userns. This function will then take
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1095)  * care to map the inode according to @mnt_userns before checking permissions.
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1096)  * On non-idmapped mounts or if permission checking is to be performed on the
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1097)  * raw inode simply passs init_user_ns.
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1098)  *
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1099)  * Returns 0 if successful, -ve on error.
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1100)  */
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1101) int may_linkat(struct user_namespace *mnt_userns, struct path *link)
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1102) {
593d1ce854dff (Eric W. Biederman       2017-09-14 12:07:32 -0500 1103) 	struct inode *inode = link->dentry->d_inode;
593d1ce854dff (Eric W. Biederman       2017-09-14 12:07:32 -0500 1104) 
593d1ce854dff (Eric W. Biederman       2017-09-14 12:07:32 -0500 1105) 	/* Inode writeback is not safe when the uid or gid are invalid. */
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1106) 	if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) ||
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1107) 	    !gid_valid(i_gid_into_mnt(mnt_userns, inode)))
593d1ce854dff (Eric W. Biederman       2017-09-14 12:07:32 -0500 1108) 		return -EOVERFLOW;
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1109) 
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1110) 	if (!sysctl_protected_hardlinks)
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1111) 		return 0;
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1112) 
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1113) 	/* Source inode owner (or CAP_FOWNER) can hardlink all they like,
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1114) 	 * otherwise, it must be a safe source.
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1115) 	 */
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1116) 	if (safe_hardlink_source(mnt_userns, inode) ||
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1117) 	    inode_owner_or_capable(mnt_userns, inode))
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1118) 		return 0;
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1119) 
245d73698ed7a (Kees Cook               2019-10-02 16:41:58 -0700 1120) 	audit_log_path_denied(AUDIT_ANOM_LINK, "linkat");
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1121) 	return -EPERM;
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1122) }
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 1123) 
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1124) /**
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1125)  * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1126)  *			  should be allowed, or not, on files that already
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1127)  *			  exist.
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1128)  * @mnt_userns:	user namespace of the mount the inode was found from
2111c3c0124f7 (Randy Dunlap            2021-02-15 20:29:28 -0800 1129)  * @nd: nameidata pathwalk data
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1130)  * @inode: the inode of the file to open
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1131)  *
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1132)  * Block an O_CREAT open of a FIFO (or a regular file) when:
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1133)  *   - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1134)  *   - the file already exists
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1135)  *   - we are in a sticky directory
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1136)  *   - we don't own the file
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1137)  *   - the owner of the directory doesn't own the file
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1138)  *   - the directory is world writable
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1139)  * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1140)  * the directory doesn't have to be world writable: being group writable will
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1141)  * be enough.
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1142)  *
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1143)  * If the inode has been found through an idmapped mount the user namespace of
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1144)  * the vfsmount must be passed through @mnt_userns. This function will then take
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1145)  * care to map the inode according to @mnt_userns before checking permissions.
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1146)  * On non-idmapped mounts or if permission checking is to be performed on the
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1147)  * raw inode simply passs init_user_ns.
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1148)  *
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1149)  * Returns 0 if the open is allowed, -ve on error.
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1150)  */
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1151) static int may_create_in_sticky(struct user_namespace *mnt_userns,
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1152) 				struct nameidata *nd, struct inode *const inode)
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1153) {
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1154) 	umode_t dir_mode = nd->dir_mode;
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1155) 	kuid_t dir_uid = nd->dir_uid;
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1156) 
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1157) 	if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) ||
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1158) 	    (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
d0cb50185ae94 (Al Viro                 2020-01-26 09:29:34 -0500 1159) 	    likely(!(dir_mode & S_ISVTX)) ||
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1160) 	    uid_eq(i_uid_into_mnt(mnt_userns, inode), dir_uid) ||
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1161) 	    uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)))
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1162) 		return 0;
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1163) 
d0cb50185ae94 (Al Viro                 2020-01-26 09:29:34 -0500 1164) 	if (likely(dir_mode & 0002) ||
d0cb50185ae94 (Al Viro                 2020-01-26 09:29:34 -0500 1165) 	    (dir_mode & 0020 &&
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1166) 	     ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) ||
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1167) 	      (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) {
245d73698ed7a (Kees Cook               2019-10-02 16:41:58 -0700 1168) 		const char *operation = S_ISFIFO(inode->i_mode) ?
245d73698ed7a (Kees Cook               2019-10-02 16:41:58 -0700 1169) 					"sticky_create_fifo" :
245d73698ed7a (Kees Cook               2019-10-02 16:41:58 -0700 1170) 					"sticky_create_regular";
245d73698ed7a (Kees Cook               2019-10-02 16:41:58 -0700 1171) 		audit_log_path_denied(AUDIT_ANOM_CREAT, operation);
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1172) 		return -EACCES;
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1173) 	}
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1174) 	return 0;
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1175) }
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 1176) 
f015f1267b23d (David Howells           2012-06-25 12:55:28 +0100 1177) /*
f015f1267b23d (David Howells           2012-06-25 12:55:28 +0100 1178)  * follow_up - Find the mountpoint of path's vfsmount
f015f1267b23d (David Howells           2012-06-25 12:55:28 +0100 1179)  *
f015f1267b23d (David Howells           2012-06-25 12:55:28 +0100 1180)  * Given a path, find the mountpoint of its source file system.
f015f1267b23d (David Howells           2012-06-25 12:55:28 +0100 1181)  * Replace @path with the path of the mountpoint in the parent mount.
f015f1267b23d (David Howells           2012-06-25 12:55:28 +0100 1182)  * Up is towards /.
f015f1267b23d (David Howells           2012-06-25 12:55:28 +0100 1183)  *
f015f1267b23d (David Howells           2012-06-25 12:55:28 +0100 1184)  * Return 1 if we went up a level and 0 if we were already at the
f015f1267b23d (David Howells           2012-06-25 12:55:28 +0100 1185)  * root.
f015f1267b23d (David Howells           2012-06-25 12:55:28 +0100 1186)  */
bab77ebf51e39 (Al Viro                 2009-04-18 03:26:48 -0400 1187) int follow_up(struct path *path)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1188) {
0714a533805a0 (Al Viro                 2011-11-24 22:19:58 -0500 1189) 	struct mount *mnt = real_mount(path->mnt);
0714a533805a0 (Al Viro                 2011-11-24 22:19:58 -0500 1190) 	struct mount *parent;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1191) 	struct dentry *mountpoint;
99b7db7b8ffd6 (Nicholas Piggin         2010-08-18 04:37:39 +1000 1192) 
48a066e72d970 (Al Viro                 2013-09-29 22:06:07 -0400 1193) 	read_seqlock_excl(&mount_lock);
0714a533805a0 (Al Viro                 2011-11-24 22:19:58 -0500 1194) 	parent = mnt->mnt_parent;
3c0a6163688b8 (Al Viro                 2012-07-18 17:32:50 +0400 1195) 	if (parent == mnt) {
48a066e72d970 (Al Viro                 2013-09-29 22:06:07 -0400 1196) 		read_sequnlock_excl(&mount_lock);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1197) 		return 0;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1198) 	}
0714a533805a0 (Al Viro                 2011-11-24 22:19:58 -0500 1199) 	mntget(&parent->mnt);
a73324da7af40 (Al Viro                 2011-11-24 22:25:07 -0500 1200) 	mountpoint = dget(mnt->mnt_mountpoint);
48a066e72d970 (Al Viro                 2013-09-29 22:06:07 -0400 1201) 	read_sequnlock_excl(&mount_lock);
bab77ebf51e39 (Al Viro                 2009-04-18 03:26:48 -0400 1202) 	dput(path->dentry);
bab77ebf51e39 (Al Viro                 2009-04-18 03:26:48 -0400 1203) 	path->dentry = mountpoint;
bab77ebf51e39 (Al Viro                 2009-04-18 03:26:48 -0400 1204) 	mntput(path->mnt);
0714a533805a0 (Al Viro                 2011-11-24 22:19:58 -0500 1205) 	path->mnt = &parent->mnt;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1206) 	return 1;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1207) }
4d359507346a1 (Al Viro                 2014-03-14 12:20:17 -0400 1208) EXPORT_SYMBOL(follow_up);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1209) 
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1210) static bool choose_mountpoint_rcu(struct mount *m, const struct path *root,
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1211) 				  struct path *path, unsigned *seqp)
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1212) {
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1213) 	while (mnt_has_parent(m)) {
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1214) 		struct dentry *mountpoint = m->mnt_mountpoint;
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1215) 
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1216) 		m = m->mnt_parent;
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1217) 		if (unlikely(root->dentry == mountpoint &&
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1218) 			     root->mnt == &m->mnt))
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1219) 			break;
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1220) 		if (mountpoint != m->mnt.mnt_root) {
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1221) 			path->mnt = &m->mnt;
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1222) 			path->dentry = mountpoint;
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1223) 			*seqp = read_seqcount_begin(&mountpoint->d_seq);
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1224) 			return true;
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1225) 		}
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1226) 	}
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1227) 	return false;
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1228) }
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1229) 
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1230) static bool choose_mountpoint(struct mount *m, const struct path *root,
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1231) 			      struct path *path)
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1232) {
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1233) 	bool found;
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1234) 
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1235) 	rcu_read_lock();
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1236) 	while (1) {
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1237) 		unsigned seq, mseq = read_seqbegin(&mount_lock);
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1238) 
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1239) 		found = choose_mountpoint_rcu(m, root, path, &seq);
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1240) 		if (unlikely(!found)) {
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1241) 			if (!read_seqretry(&mount_lock, mseq))
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1242) 				break;
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1243) 		} else {
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1244) 			if (likely(__legitimize_path(path, seq, mseq)))
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1245) 				break;
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1246) 			rcu_read_unlock();
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1247) 			path_put(path);
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1248) 			rcu_read_lock();
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1249) 		}
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1250) 	}
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1251) 	rcu_read_unlock();
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1252) 	return found;
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1253) }
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1254) 
b5c84bf6f6fa3 (Nicholas Piggin         2011-01-07 17:49:38 +1100 1255) /*
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1256)  * Perform an automount
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1257)  * - return -EISDIR to tell follow_managed() to stop and return the path we
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1258)  *   were called with.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1259)  */
1c9f5e06a613c (Al Viro                 2020-01-16 22:05:18 -0500 1260) static int follow_automount(struct path *path, int *count, unsigned lookup_flags)
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 1261) {
25e195aa1e607 (Al Viro                 2020-01-11 11:27:46 -0500 1262) 	struct dentry *dentry = path->dentry;
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1263) 
0ec26fd069828 (Miklos Szeredi          2011-09-05 18:06:26 +0200 1264) 	/* We don't want to mount if someone's just doing a stat -
0ec26fd069828 (Miklos Szeredi          2011-09-05 18:06:26 +0200 1265) 	 * unless they're stat'ing a directory and appended a '/' to
0ec26fd069828 (Miklos Szeredi          2011-09-05 18:06:26 +0200 1266) 	 * the name.
0ec26fd069828 (Miklos Szeredi          2011-09-05 18:06:26 +0200 1267) 	 *
0ec26fd069828 (Miklos Szeredi          2011-09-05 18:06:26 +0200 1268) 	 * We do, however, want to mount if someone wants to open or
0ec26fd069828 (Miklos Szeredi          2011-09-05 18:06:26 +0200 1269) 	 * create a file of any type under the mountpoint, wants to
0ec26fd069828 (Miklos Szeredi          2011-09-05 18:06:26 +0200 1270) 	 * traverse through the mountpoint or wants to open the
0ec26fd069828 (Miklos Szeredi          2011-09-05 18:06:26 +0200 1271) 	 * mounted directory.  Also, autofs may mark negative dentries
0ec26fd069828 (Miklos Szeredi          2011-09-05 18:06:26 +0200 1272) 	 * as being automount points.  These will need the attentions
0ec26fd069828 (Miklos Szeredi          2011-09-05 18:06:26 +0200 1273) 	 * of the daemon to instantiate them before they can be used.
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1274) 	 */
1c9f5e06a613c (Al Viro                 2020-01-16 22:05:18 -0500 1275) 	if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
5d38f049cee1e (Ian Kent                2017-11-29 16:11:26 -0800 1276) 			   LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
25e195aa1e607 (Al Viro                 2020-01-11 11:27:46 -0500 1277) 	    dentry->d_inode)
5d38f049cee1e (Ian Kent                2017-11-29 16:11:26 -0800 1278) 		return -EISDIR;
0ec26fd069828 (Miklos Szeredi          2011-09-05 18:06:26 +0200 1279) 
1c9f5e06a613c (Al Viro                 2020-01-16 22:05:18 -0500 1280) 	if (count && (*count)++ >= MAXSYMLINKS)
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1281) 		return -ELOOP;
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1282) 
25e195aa1e607 (Al Viro                 2020-01-11 11:27:46 -0500 1283) 	return finish_automount(dentry->d_op->d_automount(path), path);
463ffb2e9d39c (Al Viro                 2005-06-06 13:36:05 -0700 1284) }
463ffb2e9d39c (Al Viro                 2005-06-06 13:36:05 -0700 1285) 
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1286) /*
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1287)  * mount traversal - out-of-line part.  One note on ->d_flags accesses -
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1288)  * dentries are pinned but not locked here, so negative dentry can go
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1289)  * positive right under us.  Use of smp_load_acquire() provides a barrier
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1290)  * sufficient for ->d_inode and ->d_flags consistency.
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1291)  */
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1292) static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped,
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1293) 			     int *count, unsigned lookup_flags)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1294) {
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1295) 	struct vfsmount *mnt = path->mnt;
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1296) 	bool need_mntput = false;
8aef18845266f (Al Viro                 2011-06-16 15:10:06 +0100 1297) 	int ret = 0;
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1298) 
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1299) 	while (flags & DCACHE_MANAGED_DENTRY) {
cc53ce53c8692 (David Howells           2011-01-14 18:45:26 +0000 1300) 		/* Allow the filesystem to manage the transit without i_mutex
cc53ce53c8692 (David Howells           2011-01-14 18:45:26 +0000 1301) 		 * being held. */
d41efb522e902 (Al Viro                 2019-11-04 22:30:52 -0500 1302) 		if (flags & DCACHE_MANAGE_TRANSIT) {
fb5f51c7425eb (Ian Kent                2016-11-24 08:03:41 +1100 1303) 			ret = path->dentry->d_op->d_manage(path, false);
508c8772760d4 (Al Viro                 2020-01-14 22:09:57 -0500 1304) 			flags = smp_load_acquire(&path->dentry->d_flags);
cc53ce53c8692 (David Howells           2011-01-14 18:45:26 +0000 1305) 			if (ret < 0)
8aef18845266f (Al Viro                 2011-06-16 15:10:06 +0100 1306) 				break;
cc53ce53c8692 (David Howells           2011-01-14 18:45:26 +0000 1307) 		}
cc53ce53c8692 (David Howells           2011-01-14 18:45:26 +0000 1308) 
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1309) 		if (flags & DCACHE_MOUNTED) {	// something's mounted on it..
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1310) 			struct vfsmount *mounted = lookup_mnt(path);
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1311) 			if (mounted) {		// ... in our namespace
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1312) 				dput(path->dentry);
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1313) 				if (need_mntput)
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1314) 					mntput(path->mnt);
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1315) 				path->mnt = mounted;
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1316) 				path->dentry = dget(mounted->mnt_root);
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1317) 				// here we know it's positive
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1318) 				flags = path->dentry->d_flags;
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1319) 				need_mntput = true;
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1320) 				continue;
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1321) 			}
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1322) 		}
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1323) 
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1324) 		if (!(flags & DCACHE_NEED_AUTOMOUNT))
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1325) 			break;
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1326) 
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1327) 		// uncovered automount point
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1328) 		ret = follow_automount(path, count, lookup_flags);
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1329) 		flags = smp_load_acquire(&path->dentry->d_flags);
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1330) 		if (ret < 0)
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1331) 			break;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1332) 	}
8aef18845266f (Al Viro                 2011-06-16 15:10:06 +0100 1333) 
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1334) 	if (ret == -EISDIR)
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1335) 		ret = 0;
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1336) 	// possible if you race with several mount --move
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1337) 	if (need_mntput && path->mnt == mnt)
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1338) 		mntput(path->mnt);
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1339) 	if (!ret && unlikely(d_flags_negative(flags)))
d41efb522e902 (Al Viro                 2019-11-04 22:30:52 -0500 1340) 		ret = -ENOENT;
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1341) 	*jumped = need_mntput;
8402752ecf829 (Al Viro                 2015-04-22 10:30:08 -0400 1342) 	return ret;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1343) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1344) 
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1345) static inline int traverse_mounts(struct path *path, bool *jumped,
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1346) 				  int *count, unsigned lookup_flags)
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1347) {
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1348) 	unsigned flags = smp_load_acquire(&path->dentry->d_flags);
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1349) 
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1350) 	/* fastpath */
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1351) 	if (likely(!(flags & DCACHE_MANAGED_DENTRY))) {
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1352) 		*jumped = false;
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1353) 		if (unlikely(d_flags_negative(flags)))
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1354) 			return -ENOENT;
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1355) 		return 0;
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1356) 	}
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1357) 	return __traverse_mounts(path, flags, jumped, count, lookup_flags);
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1358) }
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1359) 
cc53ce53c8692 (David Howells           2011-01-14 18:45:26 +0000 1360) int follow_down_one(struct path *path)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1361) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1362) 	struct vfsmount *mounted;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1363) 
1c755af4df759 (Al Viro                 2009-04-18 14:06:57 -0400 1364) 	mounted = lookup_mnt(path);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1365) 	if (mounted) {
9393bd07cf218 (Al Viro                 2009-04-18 13:58:15 -0400 1366) 		dput(path->dentry);
9393bd07cf218 (Al Viro                 2009-04-18 13:58:15 -0400 1367) 		mntput(path->mnt);
9393bd07cf218 (Al Viro                 2009-04-18 13:58:15 -0400 1368) 		path->mnt = mounted;
9393bd07cf218 (Al Viro                 2009-04-18 13:58:15 -0400 1369) 		path->dentry = dget(mounted->mnt_root);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1370) 		return 1;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1371) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1372) 	return 0;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1373) }
4d359507346a1 (Al Viro                 2014-03-14 12:20:17 -0400 1374) EXPORT_SYMBOL(follow_down_one);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1375) 
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1376) /*
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1377)  * Follow down to the covering mount currently visible to userspace.  At each
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1378)  * point, the filesystem owning that dentry may be queried as to whether the
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1379)  * caller is permitted to proceed or not.
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1380)  */
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1381) int follow_down(struct path *path)
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1382) {
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1383) 	struct vfsmount *mnt = path->mnt;
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1384) 	bool jumped;
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1385) 	int ret = traverse_mounts(path, &jumped, NULL, 0);
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1386) 
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1387) 	if (path->mnt != mnt)
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1388) 		mntput(mnt);
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1389) 	return ret;
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1390) }
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1391) EXPORT_SYMBOL(follow_down);
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1392) 
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1393) /*
287548e46aa75 (Al Viro                 2011-05-27 06:50:06 -0400 1394)  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
287548e46aa75 (Al Viro                 2011-05-27 06:50:06 -0400 1395)  * we meet a managed dentry that would need blocking.
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1396)  */
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1397) static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
254cf582127b2 (Al Viro                 2015-05-05 09:40:46 -0400 1398) 			       struct inode **inode, unsigned *seqp)
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1399) {
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1400) 	struct dentry *dentry = path->dentry;
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1401) 	unsigned int flags = dentry->d_flags;
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1402) 
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1403) 	if (likely(!(flags & DCACHE_MANAGED_DENTRY)))
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1404) 		return true;
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1405) 
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1406) 	if (unlikely(nd->flags & LOOKUP_NO_XDEV))
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1407) 		return false;
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1408) 
62a7375e5d77d (Ian Kent                2011-03-25 01:51:02 +0800 1409) 	for (;;) {
62a7375e5d77d (Ian Kent                2011-03-25 01:51:02 +0800 1410) 		/*
62a7375e5d77d (Ian Kent                2011-03-25 01:51:02 +0800 1411) 		 * Don't forget we might have a non-mountpoint managed dentry
62a7375e5d77d (Ian Kent                2011-03-25 01:51:02 +0800 1412) 		 * that wants to block transit.
62a7375e5d77d (Ian Kent                2011-03-25 01:51:02 +0800 1413) 		 */
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1414) 		if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) {
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1415) 			int res = dentry->d_op->d_manage(path, true);
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1416) 			if (res)
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1417) 				return res == -EISDIR;
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1418) 			flags = dentry->d_flags;
b8faf035ea9d0 (NeilBrown               2014-08-04 17:06:29 +1000 1419) 		}
62a7375e5d77d (Ian Kent                2011-03-25 01:51:02 +0800 1420) 
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1421) 		if (flags & DCACHE_MOUNTED) {
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1422) 			struct mount *mounted = __lookup_mnt(path->mnt, dentry);
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1423) 			if (mounted) {
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1424) 				path->mnt = &mounted->mnt;
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1425) 				dentry = path->dentry = mounted->mnt.mnt_root;
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1426) 				nd->flags |= LOOKUP_JUMPED;
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1427) 				*seqp = read_seqcount_begin(&dentry->d_seq);
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1428) 				*inode = dentry->d_inode;
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1429) 				/*
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1430) 				 * We don't need to re-check ->d_seq after this
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1431) 				 * ->d_inode read - there will be an RCU delay
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1432) 				 * between mount hash removal and ->mnt_root
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1433) 				 * becoming unpinned.
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1434) 				 */
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1435) 				flags = dentry->d_flags;
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1436) 				continue;
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1437) 			}
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1438) 			if (read_seqretry(&mount_lock, nd->m_seq))
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1439) 				return false;
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1440) 		}
ea936aeb3ead0 (Al Viro                 2020-01-16 09:52:04 -0500 1441) 		return !(flags & DCACHE_NEED_AUTOMOUNT);
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1442) 	}
287548e46aa75 (Al Viro                 2011-05-27 06:50:06 -0400 1443) }
287548e46aa75 (Al Viro                 2011-05-27 06:50:06 -0400 1444) 
db3c9ade50b1f (Al Viro                 2020-01-09 14:41:00 -0500 1445) static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry,
db3c9ade50b1f (Al Viro                 2020-01-09 14:41:00 -0500 1446) 			  struct path *path, struct inode **inode,
db3c9ade50b1f (Al Viro                 2020-01-09 14:41:00 -0500 1447) 			  unsigned int *seqp)
bd7c4b5083446 (Al Viro                 2020-01-08 20:37:23 -0500 1448) {
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1449) 	bool jumped;
db3c9ade50b1f (Al Viro                 2020-01-09 14:41:00 -0500 1450) 	int ret;
bd7c4b5083446 (Al Viro                 2020-01-08 20:37:23 -0500 1451) 
db3c9ade50b1f (Al Viro                 2020-01-09 14:41:00 -0500 1452) 	path->mnt = nd->path.mnt;
db3c9ade50b1f (Al Viro                 2020-01-09 14:41:00 -0500 1453) 	path->dentry = dentry;
c153007b7b7a6 (Al Viro                 2020-01-09 14:50:18 -0500 1454) 	if (nd->flags & LOOKUP_RCU) {
c153007b7b7a6 (Al Viro                 2020-01-09 14:50:18 -0500 1455) 		unsigned int seq = *seqp;
c153007b7b7a6 (Al Viro                 2020-01-09 14:50:18 -0500 1456) 		if (unlikely(!*inode))
c153007b7b7a6 (Al Viro                 2020-01-09 14:50:18 -0500 1457) 			return -ENOENT;
c153007b7b7a6 (Al Viro                 2020-01-09 14:50:18 -0500 1458) 		if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1459) 			return 0;
ae66db45fd309 (Al Viro                 2021-01-04 00:08:41 -0500 1460) 		if (!try_to_unlazy_next(nd, dentry, seq))
c153007b7b7a6 (Al Viro                 2020-01-09 14:50:18 -0500 1461) 			return -ECHILD;
c153007b7b7a6 (Al Viro                 2020-01-09 14:50:18 -0500 1462) 		// *path might've been clobbered by __follow_mount_rcu()
c153007b7b7a6 (Al Viro                 2020-01-09 14:50:18 -0500 1463) 		path->mnt = nd->path.mnt;
c153007b7b7a6 (Al Viro                 2020-01-09 14:50:18 -0500 1464) 		path->dentry = dentry;
c153007b7b7a6 (Al Viro                 2020-01-09 14:50:18 -0500 1465) 	}
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1466) 	ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags);
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1467) 	if (jumped) {
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1468) 		if (unlikely(nd->flags & LOOKUP_NO_XDEV))
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1469) 			ret = -EXDEV;
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1470) 		else
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1471) 			nd->flags |= LOOKUP_JUMPED;
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1472) 	}
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1473) 	if (unlikely(ret)) {
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1474) 		dput(path->dentry);
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1475) 		if (path->mnt != nd->path.mnt)
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1476) 			mntput(path->mnt);
9deed3ebca244 (Al Viro                 2020-01-17 08:45:08 -0500 1477) 	} else {
bd7c4b5083446 (Al Viro                 2020-01-08 20:37:23 -0500 1478) 		*inode = d_backing_inode(path->dentry);
bd7c4b5083446 (Al Viro                 2020-01-08 20:37:23 -0500 1479) 		*seqp = 0; /* out of RCU mode, so the value doesn't matter */
bd7c4b5083446 (Al Viro                 2020-01-08 20:37:23 -0500 1480) 	}
bd7c4b5083446 (Al Viro                 2020-01-08 20:37:23 -0500 1481) 	return ret;
bd7c4b5083446 (Al Viro                 2020-01-08 20:37:23 -0500 1482) }
bd7c4b5083446 (Al Viro                 2020-01-08 20:37:23 -0500 1483) 
baa0389073eb7 (Nicholas Piggin         2010-08-18 04:37:31 +1000 1484) /*
f4fdace94722c (Oleg Drokin             2016-07-07 22:04:04 -0400 1485)  * This looks up the name in dcache and possibly revalidates the found dentry.
f4fdace94722c (Oleg Drokin             2016-07-07 22:04:04 -0400 1486)  * NULL is returned if the dentry does not exist in the cache.
baa0389073eb7 (Nicholas Piggin         2010-08-18 04:37:31 +1000 1487)  */
e3c13928086f1 (Al Viro                 2016-03-06 14:03:27 -0500 1488) static struct dentry *lookup_dcache(const struct qstr *name,
e3c13928086f1 (Al Viro                 2016-03-06 14:03:27 -0500 1489) 				    struct dentry *dir,
6c51e513a3aa4 (Al Viro                 2016-03-05 20:09:32 -0500 1490) 				    unsigned int flags)
baa0389073eb7 (Nicholas Piggin         2010-08-18 04:37:31 +1000 1491) {
a89f833737e6c (Al Viro                 2017-01-09 22:25:28 -0500 1492) 	struct dentry *dentry = d_lookup(dir, name);
bad61189780ec (Miklos Szeredi          2012-03-26 12:54:24 +0200 1493) 	if (dentry) {
a89f833737e6c (Al Viro                 2017-01-09 22:25:28 -0500 1494) 		int error = d_revalidate(dentry, flags);
a89f833737e6c (Al Viro                 2017-01-09 22:25:28 -0500 1495) 		if (unlikely(error <= 0)) {
a89f833737e6c (Al Viro                 2017-01-09 22:25:28 -0500 1496) 			if (!error)
a89f833737e6c (Al Viro                 2017-01-09 22:25:28 -0500 1497) 				d_invalidate(dentry);
a89f833737e6c (Al Viro                 2017-01-09 22:25:28 -0500 1498) 			dput(dentry);
a89f833737e6c (Al Viro                 2017-01-09 22:25:28 -0500 1499) 			return ERR_PTR(error);
bad61189780ec (Miklos Szeredi          2012-03-26 12:54:24 +0200 1500) 		}
bad61189780ec (Miklos Szeredi          2012-03-26 12:54:24 +0200 1501) 	}
baa0389073eb7 (Nicholas Piggin         2010-08-18 04:37:31 +1000 1502) 	return dentry;
baa0389073eb7 (Nicholas Piggin         2010-08-18 04:37:31 +1000 1503) }
baa0389073eb7 (Nicholas Piggin         2010-08-18 04:37:31 +1000 1504) 
44396f4b5cb85 (Josef Bacik             2011-05-31 11:58:49 -0400 1505) /*
a03ece5ff2bd7 (Al Viro                 2018-03-08 11:00:45 -0500 1506)  * Parent directory has inode locked exclusive.  This is one
a03ece5ff2bd7 (Al Viro                 2018-03-08 11:00:45 -0500 1507)  * and only case when ->lookup() gets called on non in-lookup
a03ece5ff2bd7 (Al Viro                 2018-03-08 11:00:45 -0500 1508)  * dentries - as the matter of fact, this only gets called
a03ece5ff2bd7 (Al Viro                 2018-03-08 11:00:45 -0500 1509)  * when directory is guaranteed to have no in-lookup children
a03ece5ff2bd7 (Al Viro                 2018-03-08 11:00:45 -0500 1510)  * at all.
44396f4b5cb85 (Josef Bacik             2011-05-31 11:58:49 -0400 1511)  */
e3c13928086f1 (Al Viro                 2016-03-06 14:03:27 -0500 1512) static struct dentry *__lookup_hash(const struct qstr *name,
72bd866a01fc6 (Al Viro                 2012-06-10 17:17:17 -0400 1513) 		struct dentry *base, unsigned int flags)
a32555466caee (Al Viro                 2012-03-30 14:41:51 -0400 1514) {
6c51e513a3aa4 (Al Viro                 2016-03-05 20:09:32 -0500 1515) 	struct dentry *dentry = lookup_dcache(name, base, flags);
a03ece5ff2bd7 (Al Viro                 2018-03-08 11:00:45 -0500 1516) 	struct dentry *old;
a03ece5ff2bd7 (Al Viro                 2018-03-08 11:00:45 -0500 1517) 	struct inode *dir = base->d_inode;
a32555466caee (Al Viro                 2012-03-30 14:41:51 -0400 1518) 
6c51e513a3aa4 (Al Viro                 2016-03-05 20:09:32 -0500 1519) 	if (dentry)
bad61189780ec (Miklos Szeredi          2012-03-26 12:54:24 +0200 1520) 		return dentry;
a32555466caee (Al Viro                 2012-03-30 14:41:51 -0400 1521) 
a03ece5ff2bd7 (Al Viro                 2018-03-08 11:00:45 -0500 1522) 	/* Don't create child dentry for a dead directory. */
a03ece5ff2bd7 (Al Viro                 2018-03-08 11:00:45 -0500 1523) 	if (unlikely(IS_DEADDIR(dir)))
a03ece5ff2bd7 (Al Viro                 2018-03-08 11:00:45 -0500 1524) 		return ERR_PTR(-ENOENT);
a03ece5ff2bd7 (Al Viro                 2018-03-08 11:00:45 -0500 1525) 
6c51e513a3aa4 (Al Viro                 2016-03-05 20:09:32 -0500 1526) 	dentry = d_alloc(base, name);
6c51e513a3aa4 (Al Viro                 2016-03-05 20:09:32 -0500 1527) 	if (unlikely(!dentry))
6c51e513a3aa4 (Al Viro                 2016-03-05 20:09:32 -0500 1528) 		return ERR_PTR(-ENOMEM);
6c51e513a3aa4 (Al Viro                 2016-03-05 20:09:32 -0500 1529) 
a03ece5ff2bd7 (Al Viro                 2018-03-08 11:00:45 -0500 1530) 	old = dir->i_op->lookup(dir, dentry, flags);
a03ece5ff2bd7 (Al Viro                 2018-03-08 11:00:45 -0500 1531) 	if (unlikely(old)) {
a03ece5ff2bd7 (Al Viro                 2018-03-08 11:00:45 -0500 1532) 		dput(dentry);
a03ece5ff2bd7 (Al Viro                 2018-03-08 11:00:45 -0500 1533) 		dentry = old;
a03ece5ff2bd7 (Al Viro                 2018-03-08 11:00:45 -0500 1534) 	}
a03ece5ff2bd7 (Al Viro                 2018-03-08 11:00:45 -0500 1535) 	return dentry;
a32555466caee (Al Viro                 2012-03-30 14:41:51 -0400 1536) }
a32555466caee (Al Viro                 2012-03-30 14:41:51 -0400 1537) 
20e343571cefe (Al Viro                 2020-01-09 14:58:31 -0500 1538) static struct dentry *lookup_fast(struct nameidata *nd,
20e343571cefe (Al Viro                 2020-01-09 14:58:31 -0500 1539) 				  struct inode **inode,
20e343571cefe (Al Viro                 2020-01-09 14:58:31 -0500 1540) 			          unsigned *seqp)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1541) {
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 1542) 	struct dentry *dentry, *parent = nd->path.dentry;
5a18fff2090c3 (Al Viro                 2011-03-11 04:44:53 -0500 1543) 	int status = 1;
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1544) 
b04f784e5d19e (Nicholas Piggin         2010-08-18 04:37:34 +1000 1545) 	/*
b04f784e5d19e (Nicholas Piggin         2010-08-18 04:37:34 +1000 1546) 	 * Rename seqlock is not required here because in the off chance
5d0f49c136b52 (Al Viro                 2016-03-05 21:32:53 -0500 1547) 	 * of a false negative due to a concurrent rename, the caller is
5d0f49c136b52 (Al Viro                 2016-03-05 21:32:53 -0500 1548) 	 * going to fall back to non-racy lookup.
b04f784e5d19e (Nicholas Piggin         2010-08-18 04:37:34 +1000 1549) 	 */
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 1550) 	if (nd->flags & LOOKUP_RCU) {
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 1551) 		unsigned seq;
da53be12bbb4f (Linus Torvalds          2013-05-21 15:22:44 -0700 1552) 		dentry = __d_lookup_rcu(parent, &nd->last, &seq);
5d0f49c136b52 (Al Viro                 2016-03-05 21:32:53 -0500 1553) 		if (unlikely(!dentry)) {
e36cffed20a32 (Jens Axboe              2020-12-17 09:19:08 -0700 1554) 			if (!try_to_unlazy(nd))
20e343571cefe (Al Viro                 2020-01-09 14:58:31 -0500 1555) 				return ERR_PTR(-ECHILD);
20e343571cefe (Al Viro                 2020-01-09 14:58:31 -0500 1556) 			return NULL;
5d0f49c136b52 (Al Viro                 2016-03-05 21:32:53 -0500 1557) 		}
5a18fff2090c3 (Al Viro                 2011-03-11 04:44:53 -0500 1558) 
12f8ad4b0533d (Linus Torvalds          2012-05-04 14:59:14 -0700 1559) 		/*
12f8ad4b0533d (Linus Torvalds          2012-05-04 14:59:14 -0700 1560) 		 * This sequence count validates that the inode matches
12f8ad4b0533d (Linus Torvalds          2012-05-04 14:59:14 -0700 1561) 		 * the dentry name information from lookup.
12f8ad4b0533d (Linus Torvalds          2012-05-04 14:59:14 -0700 1562) 		 */
63afdfc781e10 (David Howells           2015-05-06 15:59:00 +0100 1563) 		*inode = d_backing_inode(dentry);
5d0f49c136b52 (Al Viro                 2016-03-05 21:32:53 -0500 1564) 		if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
20e343571cefe (Al Viro                 2020-01-09 14:58:31 -0500 1565) 			return ERR_PTR(-ECHILD);
12f8ad4b0533d (Linus Torvalds          2012-05-04 14:59:14 -0700 1566) 
12f8ad4b0533d (Linus Torvalds          2012-05-04 14:59:14 -0700 1567) 		/*
12f8ad4b0533d (Linus Torvalds          2012-05-04 14:59:14 -0700 1568) 		 * This sequence count validates that the parent had no
12f8ad4b0533d (Linus Torvalds          2012-05-04 14:59:14 -0700 1569) 		 * changes while we did the lookup of the dentry above.
12f8ad4b0533d (Linus Torvalds          2012-05-04 14:59:14 -0700 1570) 		 *
12f8ad4b0533d (Linus Torvalds          2012-05-04 14:59:14 -0700 1571) 		 * The memory barrier in read_seqcount_begin of child is
12f8ad4b0533d (Linus Torvalds          2012-05-04 14:59:14 -0700 1572) 		 *  enough, we can use __read_seqcount_retry here.
12f8ad4b0533d (Linus Torvalds          2012-05-04 14:59:14 -0700 1573) 		 */
5d0f49c136b52 (Al Viro                 2016-03-05 21:32:53 -0500 1574) 		if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
20e343571cefe (Al Viro                 2020-01-09 14:58:31 -0500 1575) 			return ERR_PTR(-ECHILD);
5a18fff2090c3 (Al Viro                 2011-03-11 04:44:53 -0500 1576) 
254cf582127b2 (Al Viro                 2015-05-05 09:40:46 -0400 1577) 		*seqp = seq;
a89f833737e6c (Al Viro                 2017-01-09 22:25:28 -0500 1578) 		status = d_revalidate(dentry, nd->flags);
c153007b7b7a6 (Al Viro                 2020-01-09 14:50:18 -0500 1579) 		if (likely(status > 0))
20e343571cefe (Al Viro                 2020-01-09 14:58:31 -0500 1580) 			return dentry;
ae66db45fd309 (Al Viro                 2021-01-04 00:08:41 -0500 1581) 		if (!try_to_unlazy_next(nd, dentry, seq))
20e343571cefe (Al Viro                 2020-01-09 14:58:31 -0500 1582) 			return ERR_PTR(-ECHILD);
26ddb45e13a3e (Steven Rostedt (VMware) 2020-12-09 17:09:28 -0500 1583) 		if (status == -ECHILD)
209a7fb2104f2 (Al Viro                 2017-01-09 01:35:39 -0500 1584) 			/* we'd been told to redo it in non-rcu mode */
209a7fb2104f2 (Al Viro                 2017-01-09 01:35:39 -0500 1585) 			status = d_revalidate(dentry, nd->flags);
5a18fff2090c3 (Al Viro                 2011-03-11 04:44:53 -0500 1586) 	} else {
e97cdc87be580 (Al Viro                 2013-01-24 18:16:00 -0500 1587) 		dentry = __d_lookup(parent, &nd->last);
5d0f49c136b52 (Al Viro                 2016-03-05 21:32:53 -0500 1588) 		if (unlikely(!dentry))
20e343571cefe (Al Viro                 2020-01-09 14:58:31 -0500 1589) 			return NULL;
a89f833737e6c (Al Viro                 2017-01-09 22:25:28 -0500 1590) 		status = d_revalidate(dentry, nd->flags);
9875cf806403f (David Howells           2011-01-14 18:45:21 +0000 1591) 	}
5a18fff2090c3 (Al Viro                 2011-03-11 04:44:53 -0500 1592) 	if (unlikely(status <= 0)) {
e9742b5332061 (Al Viro                 2016-03-05 22:04:59 -0500 1593) 		if (!status)
5d0f49c136b52 (Al Viro                 2016-03-05 21:32:53 -0500 1594) 			d_invalidate(dentry);
5542aa2fa7f6c (Eric W. Biederman       2014-02-13 09:46:25 -0800 1595) 		dput(dentry);
20e343571cefe (Al Viro                 2020-01-09 14:58:31 -0500 1596) 		return ERR_PTR(status);
24643087e748b (Al Viro                 2011-02-15 01:26:22 -0500 1597) 	}
20e343571cefe (Al Viro                 2020-01-09 14:58:31 -0500 1598) 	return dentry;
697f514df10b0 (Miklos Szeredi          2012-05-21 17:30:05 +0200 1599) }
697f514df10b0 (Miklos Szeredi          2012-05-21 17:30:05 +0200 1600) 
697f514df10b0 (Miklos Szeredi          2012-05-21 17:30:05 +0200 1601) /* Fast lookup failed, do it the slow way */
88d8331afb0f7 (Al Viro                 2018-04-06 16:43:47 -0400 1602) static struct dentry *__lookup_slow(const struct qstr *name,
88d8331afb0f7 (Al Viro                 2018-04-06 16:43:47 -0400 1603) 				    struct dentry *dir,
88d8331afb0f7 (Al Viro                 2018-04-06 16:43:47 -0400 1604) 				    unsigned int flags)
697f514df10b0 (Miklos Szeredi          2012-05-21 17:30:05 +0200 1605) {
88d8331afb0f7 (Al Viro                 2018-04-06 16:43:47 -0400 1606) 	struct dentry *dentry, *old;
1936386ea9659 (Al Viro                 2016-04-14 19:33:34 -0400 1607) 	struct inode *inode = dir->d_inode;
d9171b9345261 (Al Viro                 2016-04-15 03:33:13 -0400 1608) 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1936386ea9659 (Al Viro                 2016-04-14 19:33:34 -0400 1609) 
1936386ea9659 (Al Viro                 2016-04-14 19:33:34 -0400 1610) 	/* Don't go there if it's already dead */
94bdd655caba2 (Al Viro                 2016-04-15 02:42:04 -0400 1611) 	if (unlikely(IS_DEADDIR(inode)))
88d8331afb0f7 (Al Viro                 2018-04-06 16:43:47 -0400 1612) 		return ERR_PTR(-ENOENT);
94bdd655caba2 (Al Viro                 2016-04-15 02:42:04 -0400 1613) again:
d9171b9345261 (Al Viro                 2016-04-15 03:33:13 -0400 1614) 	dentry = d_alloc_parallel(dir, name, &wq);
94bdd655caba2 (Al Viro                 2016-04-15 02:42:04 -0400 1615) 	if (IS_ERR(dentry))
88d8331afb0f7 (Al Viro                 2018-04-06 16:43:47 -0400 1616) 		return dentry;
94bdd655caba2 (Al Viro                 2016-04-15 02:42:04 -0400 1617) 	if (unlikely(!d_in_lookup(dentry))) {
c64cd6e34ea34 (Al Viro                 2020-01-10 17:17:19 -0500 1618) 		int error = d_revalidate(dentry, flags);
c64cd6e34ea34 (Al Viro                 2020-01-10 17:17:19 -0500 1619) 		if (unlikely(error <= 0)) {
c64cd6e34ea34 (Al Viro                 2020-01-10 17:17:19 -0500 1620) 			if (!error) {
c64cd6e34ea34 (Al Viro                 2020-01-10 17:17:19 -0500 1621) 				d_invalidate(dentry);
949a852e46dda (Al Viro                 2016-03-06 14:20:52 -0500 1622) 				dput(dentry);
c64cd6e34ea34 (Al Viro                 2020-01-10 17:17:19 -0500 1623) 				goto again;
949a852e46dda (Al Viro                 2016-03-06 14:20:52 -0500 1624) 			}
c64cd6e34ea34 (Al Viro                 2020-01-10 17:17:19 -0500 1625) 			dput(dentry);
c64cd6e34ea34 (Al Viro                 2020-01-10 17:17:19 -0500 1626) 			dentry = ERR_PTR(error);
949a852e46dda (Al Viro                 2016-03-06 14:20:52 -0500 1627) 		}
94bdd655caba2 (Al Viro                 2016-04-15 02:42:04 -0400 1628) 	} else {
94bdd655caba2 (Al Viro                 2016-04-15 02:42:04 -0400 1629) 		old = inode->i_op->lookup(inode, dentry, flags);
94bdd655caba2 (Al Viro                 2016-04-15 02:42:04 -0400 1630) 		d_lookup_done(dentry);
94bdd655caba2 (Al Viro                 2016-04-15 02:42:04 -0400 1631) 		if (unlikely(old)) {
94bdd655caba2 (Al Viro                 2016-04-15 02:42:04 -0400 1632) 			dput(dentry);
94bdd655caba2 (Al Viro                 2016-04-15 02:42:04 -0400 1633) 			dentry = old;
949a852e46dda (Al Viro                 2016-03-06 14:20:52 -0500 1634) 		}
949a852e46dda (Al Viro                 2016-03-06 14:20:52 -0500 1635) 	}
e3c13928086f1 (Al Viro                 2016-03-06 14:03:27 -0500 1636) 	return dentry;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1637) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1638) 
88d8331afb0f7 (Al Viro                 2018-04-06 16:43:47 -0400 1639) static struct dentry *lookup_slow(const struct qstr *name,
88d8331afb0f7 (Al Viro                 2018-04-06 16:43:47 -0400 1640) 				  struct dentry *dir,
88d8331afb0f7 (Al Viro                 2018-04-06 16:43:47 -0400 1641) 				  unsigned int flags)
88d8331afb0f7 (Al Viro                 2018-04-06 16:43:47 -0400 1642) {
88d8331afb0f7 (Al Viro                 2018-04-06 16:43:47 -0400 1643) 	struct inode *inode = dir->d_inode;
88d8331afb0f7 (Al Viro                 2018-04-06 16:43:47 -0400 1644) 	struct dentry *res;
88d8331afb0f7 (Al Viro                 2018-04-06 16:43:47 -0400 1645) 	inode_lock_shared(inode);
88d8331afb0f7 (Al Viro                 2018-04-06 16:43:47 -0400 1646) 	res = __lookup_slow(name, dir, flags);
88d8331afb0f7 (Al Viro                 2018-04-06 16:43:47 -0400 1647) 	inode_unlock_shared(inode);
88d8331afb0f7 (Al Viro                 2018-04-06 16:43:47 -0400 1648) 	return res;
88d8331afb0f7 (Al Viro                 2018-04-06 16:43:47 -0400 1649) }
88d8331afb0f7 (Al Viro                 2018-04-06 16:43:47 -0400 1650) 
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1651) static inline int may_lookup(struct user_namespace *mnt_userns,
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1652) 			     struct nameidata *nd)
52094c8a0610c (Al Viro                 2011-02-21 21:34:47 -0500 1653) {
52094c8a0610c (Al Viro                 2011-02-21 21:34:47 -0500 1654) 	if (nd->flags & LOOKUP_RCU) {
7d6beb71da3cc (Linus Torvalds          2021-02-23 13:39:45 -0800 1655) 		int err = inode_permission(mnt_userns, nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
e36cffed20a32 (Jens Axboe              2020-12-17 09:19:08 -0700 1656) 		if (err != -ECHILD || !try_to_unlazy(nd))
52094c8a0610c (Al Viro                 2011-02-21 21:34:47 -0500 1657) 			return err;
52094c8a0610c (Al Viro                 2011-02-21 21:34:47 -0500 1658) 	}
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1659) 	return inode_permission(mnt_userns, nd->inode, MAY_EXEC);
52094c8a0610c (Al Viro                 2011-02-21 21:34:47 -0500 1660) }
52094c8a0610c (Al Viro                 2011-02-21 21:34:47 -0500 1661) 
49055906af51b (Al Viro                 2020-03-03 11:22:34 -0500 1662) static int reserve_stack(struct nameidata *nd, struct path *link, unsigned seq)
49055906af51b (Al Viro                 2020-03-03 11:22:34 -0500 1663) {
49055906af51b (Al Viro                 2020-03-03 11:22:34 -0500 1664) 	if (unlikely(nd->total_link_count++ >= MAXSYMLINKS))
49055906af51b (Al Viro                 2020-03-03 11:22:34 -0500 1665) 		return -ELOOP;
4542576b79a68 (Al Viro                 2020-03-03 11:25:31 -0500 1666) 
4542576b79a68 (Al Viro                 2020-03-03 11:25:31 -0500 1667) 	if (likely(nd->depth != EMBEDDED_LEVELS))
4542576b79a68 (Al Viro                 2020-03-03 11:25:31 -0500 1668) 		return 0;
4542576b79a68 (Al Viro                 2020-03-03 11:25:31 -0500 1669) 	if (likely(nd->stack != nd->internal))
4542576b79a68 (Al Viro                 2020-03-03 11:25:31 -0500 1670) 		return 0;
60ef60c7d7291 (Al Viro                 2020-03-03 11:43:55 -0500 1671) 	if (likely(nd_alloc_stack(nd)))
49055906af51b (Al Viro                 2020-03-03 11:22:34 -0500 1672) 		return 0;
60ef60c7d7291 (Al Viro                 2020-03-03 11:43:55 -0500 1673) 
60ef60c7d7291 (Al Viro                 2020-03-03 11:43:55 -0500 1674) 	if (nd->flags & LOOKUP_RCU) {
60ef60c7d7291 (Al Viro                 2020-03-03 11:43:55 -0500 1675) 		// we need to grab link before we do unlazy.  And we can't skip
60ef60c7d7291 (Al Viro                 2020-03-03 11:43:55 -0500 1676) 		// unlazy even if we fail to grab the link - cleanup needs it
49055906af51b (Al Viro                 2020-03-03 11:22:34 -0500 1677) 		bool grabbed_link = legitimize_path(nd, link, seq);
60ef60c7d7291 (Al Viro                 2020-03-03 11:43:55 -0500 1678) 
e36cffed20a32 (Jens Axboe              2020-12-17 09:19:08 -0700 1679) 		if (!try_to_unlazy(nd) != 0 || !grabbed_link)
60ef60c7d7291 (Al Viro                 2020-03-03 11:43:55 -0500 1680) 			return -ECHILD;
60ef60c7d7291 (Al Viro                 2020-03-03 11:43:55 -0500 1681) 
60ef60c7d7291 (Al Viro                 2020-03-03 11:43:55 -0500 1682) 		if (nd_alloc_stack(nd))
60ef60c7d7291 (Al Viro                 2020-03-03 11:43:55 -0500 1683) 			return 0;
49055906af51b (Al Viro                 2020-03-03 11:22:34 -0500 1684) 	}
60ef60c7d7291 (Al Viro                 2020-03-03 11:43:55 -0500 1685) 	return -ENOMEM;
49055906af51b (Al Viro                 2020-03-03 11:22:34 -0500 1686) }
49055906af51b (Al Viro                 2020-03-03 11:22:34 -0500 1687) 
b1a819724074f (Al Viro                 2020-01-19 12:48:44 -0500 1688) enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4};
b1a819724074f (Al Viro                 2020-01-19 12:48:44 -0500 1689) 
06708adb99e88 (Al Viro                 2020-01-14 14:26:57 -0500 1690) static const char *pick_link(struct nameidata *nd, struct path *link,
b1a819724074f (Al Viro                 2020-01-19 12:48:44 -0500 1691) 		     struct inode *inode, unsigned seq, int flags)
d63ff28f0f693 (Al Viro                 2015-05-04 18:13:23 -0400 1692) {
1cf2665b5bdfc (Al Viro                 2015-05-06 16:01:56 -0400 1693) 	struct saved *last;
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1694) 	const char *res;
49055906af51b (Al Viro                 2020-03-03 11:22:34 -0500 1695) 	int error = reserve_stack(nd, link, seq);
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1696) 
626de99676e1e (Al Viro                 2015-05-04 18:26:59 -0400 1697) 	if (unlikely(error)) {
49055906af51b (Al Viro                 2020-03-03 11:22:34 -0500 1698) 		if (!(nd->flags & LOOKUP_RCU))
bc40aee053be1 (Al Viro                 2015-05-09 13:04:24 -0400 1699) 			path_put(link);
49055906af51b (Al Viro                 2020-03-03 11:22:34 -0500 1700) 		return ERR_PTR(error);
626de99676e1e (Al Viro                 2015-05-04 18:26:59 -0400 1701) 	}
ab10492345d1d (Al Viro                 2015-05-10 11:50:01 -0400 1702) 	last = nd->stack + nd->depth++;
1cf2665b5bdfc (Al Viro                 2015-05-06 16:01:56 -0400 1703) 	last->link = *link;
fceef393a5381 (Al Viro                 2015-12-29 15:58:39 -0500 1704) 	clear_delayed_call(&last->done);
0450b2d120ed9 (Al Viro                 2015-05-08 13:23:53 -0400 1705) 	last->seq = seq;
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1706) 
b1a819724074f (Al Viro                 2020-01-19 12:48:44 -0500 1707) 	if (flags & WALK_TRAILING) {
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1708) 		error = may_follow_link(nd, inode);
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1709) 		if (unlikely(error))
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1710) 			return ERR_PTR(error);
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1711) 	}
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1712) 
dab741e0e02bd (Mattias Nissler         2020-08-27 11:09:46 -0600 1713) 	if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) ||
dab741e0e02bd (Mattias Nissler         2020-08-27 11:09:46 -0600 1714) 			unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW))
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1715) 		return ERR_PTR(-ELOOP);
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1716) 
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1717) 	if (!(nd->flags & LOOKUP_RCU)) {
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1718) 		touch_atime(&last->link);
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1719) 		cond_resched();
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1720) 	} else if (atime_needs_update(&last->link, inode)) {
e36cffed20a32 (Jens Axboe              2020-12-17 09:19:08 -0700 1721) 		if (!try_to_unlazy(nd))
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1722) 			return ERR_PTR(-ECHILD);
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1723) 		touch_atime(&last->link);
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1724) 	}
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1725) 
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1726) 	error = security_inode_follow_link(link->dentry, inode,
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1727) 					   nd->flags & LOOKUP_RCU);
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1728) 	if (unlikely(error))
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1729) 		return ERR_PTR(error);
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1730) 
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1731) 	res = READ_ONCE(inode->i_link);
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1732) 	if (!res) {
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1733) 		const char * (*get)(struct dentry *, struct inode *,
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1734) 				struct delayed_call *);
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1735) 		get = inode->i_op->get_link;
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1736) 		if (nd->flags & LOOKUP_RCU) {
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1737) 			res = get(NULL, inode, &last->done);
e36cffed20a32 (Jens Axboe              2020-12-17 09:19:08 -0700 1738) 			if (res == ERR_PTR(-ECHILD) && try_to_unlazy(nd))
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1739) 				res = get(link->dentry, inode, &last->done);
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1740) 		} else {
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1741) 			res = get(link->dentry, inode, &last->done);
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1742) 		}
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1743) 		if (!res)
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1744) 			goto all_done;
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1745) 		if (IS_ERR(res))
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1746) 			return res;
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1747) 	}
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1748) 	if (*res == '/') {
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1749) 		error = nd_jump_root(nd);
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1750) 		if (unlikely(error))
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1751) 			return ERR_PTR(error);
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1752) 		while (unlikely(*++res == '/'))
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1753) 			;
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1754) 	}
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1755) 	if (*res)
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1756) 		return res;
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1757) all_done: // pure jump
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1758) 	put_link(nd);
ad6cc4c338f4e (Al Viro                 2020-01-14 14:41:39 -0500 1759) 	return NULL;
d63ff28f0f693 (Al Viro                 2015-05-04 18:13:23 -0400 1760) }
d63ff28f0f693 (Al Viro                 2015-05-04 18:13:23 -0400 1761) 
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700 1762) /*
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700 1763)  * Do we need to follow links? We _really_ want to be able
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700 1764)  * to do this check without having to look at inode->i_op,
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700 1765)  * so we keep a cache of "no, this doesn't need follow_link"
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700 1766)  * for the common case.
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700 1767)  */
b0417d2c72989 (Al Viro                 2020-01-14 13:34:20 -0500 1768) static const char *step_into(struct nameidata *nd, int flags,
cbae4d12eeee6 (Al Viro                 2020-01-12 13:40:02 -0500 1769) 		     struct dentry *dentry, struct inode *inode, unsigned seq)
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700 1770) {
cbae4d12eeee6 (Al Viro                 2020-01-12 13:40:02 -0500 1771) 	struct path path;
cbae4d12eeee6 (Al Viro                 2020-01-12 13:40:02 -0500 1772) 	int err = handle_mounts(nd, dentry, &path, &inode, &seq);
cbae4d12eeee6 (Al Viro                 2020-01-12 13:40:02 -0500 1773) 
cbae4d12eeee6 (Al Viro                 2020-01-12 13:40:02 -0500 1774) 	if (err < 0)
b0417d2c72989 (Al Viro                 2020-01-14 13:34:20 -0500 1775) 		return ERR_PTR(err);
cbae4d12eeee6 (Al Viro                 2020-01-12 13:40:02 -0500 1776) 	if (likely(!d_is_symlink(path.dentry)) ||
8c4efe22e7c4d (Al Viro                 2020-01-19 12:44:18 -0500 1777) 	   ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) ||
aca2903eefd0f (Al Viro                 2020-01-09 15:17:57 -0500 1778) 	   (flags & WALK_NOFOLLOW)) {
8f64fb1ccef33 (Al Viro                 2016-11-14 01:50:26 -0500 1779) 		/* not a symlink or should not follow */
c99687a03a787 (Al Viro                 2020-03-03 10:56:17 -0500 1780) 		if (!(nd->flags & LOOKUP_RCU)) {
c99687a03a787 (Al Viro                 2020-03-03 10:56:17 -0500 1781) 			dput(nd->path.dentry);
c99687a03a787 (Al Viro                 2020-03-03 10:56:17 -0500 1782) 			if (nd->path.mnt != path.mnt)
c99687a03a787 (Al Viro                 2020-03-03 10:56:17 -0500 1783) 				mntput(nd->path.mnt);
c99687a03a787 (Al Viro                 2020-03-03 10:56:17 -0500 1784) 		}
c99687a03a787 (Al Viro                 2020-03-03 10:56:17 -0500 1785) 		nd->path = path;
8f64fb1ccef33 (Al Viro                 2016-11-14 01:50:26 -0500 1786) 		nd->inode = inode;
8f64fb1ccef33 (Al Viro                 2016-11-14 01:50:26 -0500 1787) 		nd->seq = seq;
b0417d2c72989 (Al Viro                 2020-01-14 13:34:20 -0500 1788) 		return NULL;
8f64fb1ccef33 (Al Viro                 2016-11-14 01:50:26 -0500 1789) 	}
a7f775428b8f5 (Al Viro                 2016-02-27 19:31:01 -0500 1790) 	if (nd->flags & LOOKUP_RCU) {
84f0cd9e83daa (Al Viro                 2020-03-03 10:14:30 -0500 1791) 		/* make sure that d_is_symlink above matches inode */
cbae4d12eeee6 (Al Viro                 2020-01-12 13:40:02 -0500 1792) 		if (read_seqcount_retry(&path.dentry->d_seq, seq))
b0417d2c72989 (Al Viro                 2020-01-14 13:34:20 -0500 1793) 			return ERR_PTR(-ECHILD);
84f0cd9e83daa (Al Viro                 2020-03-03 10:14:30 -0500 1794) 	} else {
84f0cd9e83daa (Al Viro                 2020-03-03 10:14:30 -0500 1795) 		if (path.mnt == nd->path.mnt)
84f0cd9e83daa (Al Viro                 2020-03-03 10:14:30 -0500 1796) 			mntget(path.mnt);
a7f775428b8f5 (Al Viro                 2016-02-27 19:31:01 -0500 1797) 	}
b1a819724074f (Al Viro                 2020-01-19 12:48:44 -0500 1798) 	return pick_link(nd, &path, inode, seq, flags);
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700 1799) }
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700 1800) 
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1801) static struct dentry *follow_dotdot_rcu(struct nameidata *nd,
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1802) 					struct inode **inodep,
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1803) 					unsigned *seqp)
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1804) {
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1805) 	struct dentry *parent, *old;
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1806) 
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1807) 	if (path_equal(&nd->path, &nd->root))
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1808) 		goto in_root;
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1809) 	if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1810) 		struct path path;
efe772d6283b3 (Al Viro                 2020-02-28 10:06:37 -0500 1811) 		unsigned seq;
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1812) 		if (!choose_mountpoint_rcu(real_mount(nd->path.mnt),
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1813) 					   &nd->root, &path, &seq))
7ef482fa65513 (Al Viro                 2020-02-26 17:50:13 -0500 1814) 			goto in_root;
efe772d6283b3 (Al Viro                 2020-02-28 10:06:37 -0500 1815) 		if (unlikely(nd->flags & LOOKUP_NO_XDEV))
efe772d6283b3 (Al Viro                 2020-02-28 10:06:37 -0500 1816) 			return ERR_PTR(-ECHILD);
efe772d6283b3 (Al Viro                 2020-02-28 10:06:37 -0500 1817) 		nd->path = path;
efe772d6283b3 (Al Viro                 2020-02-28 10:06:37 -0500 1818) 		nd->inode = path.dentry->d_inode;
efe772d6283b3 (Al Viro                 2020-02-28 10:06:37 -0500 1819) 		nd->seq = seq;
efe772d6283b3 (Al Viro                 2020-02-28 10:06:37 -0500 1820) 		if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
efe772d6283b3 (Al Viro                 2020-02-28 10:06:37 -0500 1821) 			return ERR_PTR(-ECHILD);
efe772d6283b3 (Al Viro                 2020-02-28 10:06:37 -0500 1822) 		/* we know that mountpoint was pinned */
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1823) 	}
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1824) 	old = nd->path.dentry;
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1825) 	parent = old->d_parent;
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1826) 	*inodep = parent->d_inode;
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1827) 	*seqp = read_seqcount_begin(&parent->d_seq);
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1828) 	if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1829) 		return ERR_PTR(-ECHILD);
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1830) 	if (unlikely(!path_connected(nd->path.mnt, parent)))
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1831) 		return ERR_PTR(-ECHILD);
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1832) 	return parent;
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1833) in_root:
efe772d6283b3 (Al Viro                 2020-02-28 10:06:37 -0500 1834) 	if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
efe772d6283b3 (Al Viro                 2020-02-28 10:06:37 -0500 1835) 		return ERR_PTR(-ECHILD);
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1836) 	if (unlikely(nd->flags & LOOKUP_BENEATH))
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1837) 		return ERR_PTR(-ECHILD);
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1838) 	return NULL;
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1839) }
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1840) 
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1841) static struct dentry *follow_dotdot(struct nameidata *nd,
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1842) 				 struct inode **inodep,
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1843) 				 unsigned *seqp)
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1844) {
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1845) 	struct dentry *parent;
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1846) 
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1847) 	if (path_equal(&nd->path, &nd->root))
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1848) 		goto in_root;
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1849) 	if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1850) 		struct path path;
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1851) 
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1852) 		if (!choose_mountpoint(real_mount(nd->path.mnt),
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1853) 				       &nd->root, &path))
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1854) 			goto in_root;
165200d6cb88a (Al Viro                 2020-02-28 10:17:52 -0500 1855) 		path_put(&nd->path);
165200d6cb88a (Al Viro                 2020-02-28 10:17:52 -0500 1856) 		nd->path = path;
2aa38470853a6 (Al Viro                 2020-02-26 19:19:05 -0500 1857) 		nd->inode = path.dentry->d_inode;
165200d6cb88a (Al Viro                 2020-02-28 10:17:52 -0500 1858) 		if (unlikely(nd->flags & LOOKUP_NO_XDEV))
165200d6cb88a (Al Viro                 2020-02-28 10:17:52 -0500 1859) 			return ERR_PTR(-EXDEV);
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1860) 	}
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1861) 	/* rare case of legitimate dget_parent()... */
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1862) 	parent = dget_parent(nd->path.dentry);
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1863) 	if (unlikely(!path_connected(nd->path.mnt, parent))) {
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1864) 		dput(parent);
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1865) 		return ERR_PTR(-ENOENT);
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1866) 	}
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1867) 	*seqp = 0;
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1868) 	*inodep = parent->d_inode;
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1869) 	return parent;
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1870) 
12487f3067d8d (Al Viro                 2020-02-26 14:59:56 -0500 1871) in_root:
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1872) 	if (unlikely(nd->flags & LOOKUP_BENEATH))
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1873) 		return ERR_PTR(-EXDEV);
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1874) 	dget(nd->path.dentry);
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1875) 	return NULL;
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1876) }
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1877) 
7521f22b3ce2f (Al Viro                 2020-02-26 12:22:58 -0500 1878) static const char *handle_dots(struct nameidata *nd, int type)
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1879) {
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1880) 	if (type == LAST_DOTDOT) {
7521f22b3ce2f (Al Viro                 2020-02-26 12:22:58 -0500 1881) 		const char *error = NULL;
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1882) 		struct dentry *parent;
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1883) 		struct inode *inode;
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1884) 		unsigned seq;
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1885) 
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1886) 		if (!nd->root.mnt) {
7521f22b3ce2f (Al Viro                 2020-02-26 12:22:58 -0500 1887) 			error = ERR_PTR(set_root(nd));
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1888) 			if (error)
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1889) 				return error;
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1890) 		}
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1891) 		if (nd->flags & LOOKUP_RCU)
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1892) 			parent = follow_dotdot_rcu(nd, &inode, &seq);
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1893) 		else
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1894) 			parent = follow_dotdot(nd, &inode, &seq);
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1895) 		if (IS_ERR(parent))
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1896) 			return ERR_CAST(parent);
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1897) 		if (unlikely(!parent))
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1898) 			error = step_into(nd, WALK_NOFOLLOW,
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1899) 					 nd->path.dentry, nd->inode, nd->seq);
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1900) 		else
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1901) 			error = step_into(nd, WALK_NOFOLLOW,
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1902) 					 parent, inode, seq);
c2df196876609 (Al Viro                 2020-02-26 14:33:30 -0500 1903) 		if (unlikely(error))
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1904) 			return error;
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1905) 
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1906) 		if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1907) 			/*
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1908) 			 * If there was a racing rename or mount along our
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1909) 			 * path, then we can't be sure that ".." hasn't jumped
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1910) 			 * above nd->root (and so userspace should retry or use
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1911) 			 * some fallback).
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1912) 			 */
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1913) 			smp_rmb();
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1914) 			if (unlikely(__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq)))
7521f22b3ce2f (Al Viro                 2020-02-26 12:22:58 -0500 1915) 				return ERR_PTR(-EAGAIN);
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1916) 			if (unlikely(__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq)))
7521f22b3ce2f (Al Viro                 2020-02-26 12:22:58 -0500 1917) 				return ERR_PTR(-EAGAIN);
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1918) 		}
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1919) 	}
7521f22b3ce2f (Al Viro                 2020-02-26 12:22:58 -0500 1920) 	return NULL;
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1921) }
957dd41d88427 (Al Viro                 2020-02-26 01:40:04 -0500 1922) 
92d270165cffb (Al Viro                 2020-01-14 13:24:17 -0500 1923) static const char *walk_component(struct nameidata *nd, int flags)
ce57dfc179122 (Al Viro                 2011-03-13 19:58:58 -0400 1924) {
db3c9ade50b1f (Al Viro                 2020-01-09 14:41:00 -0500 1925) 	struct dentry *dentry;
ce57dfc179122 (Al Viro                 2011-03-13 19:58:58 -0400 1926) 	struct inode *inode;
254cf582127b2 (Al Viro                 2015-05-05 09:40:46 -0400 1927) 	unsigned seq;
ce57dfc179122 (Al Viro                 2011-03-13 19:58:58 -0400 1928) 	/*
ce57dfc179122 (Al Viro                 2011-03-13 19:58:58 -0400 1929) 	 * "." and ".." are special - ".." especially so because it has
ce57dfc179122 (Al Viro                 2011-03-13 19:58:58 -0400 1930) 	 * to be able to know about the current root directory and
ce57dfc179122 (Al Viro                 2011-03-13 19:58:58 -0400 1931) 	 * parent relationships.
ce57dfc179122 (Al Viro                 2011-03-13 19:58:58 -0400 1932) 	 */
4693a547cded4 (Al Viro                 2015-05-04 17:47:11 -0400 1933) 	if (unlikely(nd->last_type != LAST_NORM)) {
1c4ff1a87e46a (Al Viro                 2016-11-14 01:39:36 -0500 1934) 		if (!(flags & WALK_MORE) && nd->depth)
4693a547cded4 (Al Viro                 2015-05-04 17:47:11 -0400 1935) 			put_link(nd);
7521f22b3ce2f (Al Viro                 2020-02-26 12:22:58 -0500 1936) 		return handle_dots(nd, nd->last_type);
4693a547cded4 (Al Viro                 2015-05-04 17:47:11 -0400 1937) 	}
20e343571cefe (Al Viro                 2020-01-09 14:58:31 -0500 1938) 	dentry = lookup_fast(nd, &inode, &seq);
20e343571cefe (Al Viro                 2020-01-09 14:58:31 -0500 1939) 	if (IS_ERR(dentry))
92d270165cffb (Al Viro                 2020-01-14 13:24:17 -0500 1940) 		return ERR_CAST(dentry);
20e343571cefe (Al Viro                 2020-01-09 14:58:31 -0500 1941) 	if (unlikely(!dentry)) {
db3c9ade50b1f (Al Viro                 2020-01-09 14:41:00 -0500 1942) 		dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags);
db3c9ade50b1f (Al Viro                 2020-01-09 14:41:00 -0500 1943) 		if (IS_ERR(dentry))
92d270165cffb (Al Viro                 2020-01-14 13:24:17 -0500 1944) 			return ERR_CAST(dentry);
ce57dfc179122 (Al Viro                 2011-03-13 19:58:58 -0400 1945) 	}
56676ec390194 (Al Viro                 2020-03-10 21:54:54 -0400 1946) 	if (!(flags & WALK_MORE) && nd->depth)
56676ec390194 (Al Viro                 2020-03-10 21:54:54 -0400 1947) 		put_link(nd);
b0417d2c72989 (Al Viro                 2020-01-14 13:34:20 -0500 1948) 	return step_into(nd, flags, dentry, inode, seq);
ce57dfc179122 (Al Viro                 2011-03-13 19:58:58 -0400 1949) }
ce57dfc179122 (Al Viro                 2011-03-13 19:58:58 -0400 1950) 
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 1951) /*
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 1952)  * We can do the critical dentry name comparison and hashing
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 1953)  * operations one word at a time, but we are limited to:
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 1954)  *
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 1955)  * - Architectures with fast unaligned word accesses. We could
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 1956)  *   do a "get_unaligned()" if this helps and is sufficiently
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 1957)  *   fast.
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 1958)  *
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 1959)  * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 1960)  *   do not trap on the (extremely unlikely) case of a page
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 1961)  *   crossing operation.
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 1962)  *
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 1963)  * - Furthermore, we need an efficient 64-bit compile for the
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 1964)  *   64-bit case in order to generate the "number of bytes in
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 1965)  *   the final mask". Again, that could be replaced with a
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 1966)  *   efficient population count instruction or similar.
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 1967)  */
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 1968) #ifdef CONFIG_DCACHE_WORD_ACCESS
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 1969) 
f68e556e23d1a (Linus Torvalds          2012-04-06 13:54:56 -0700 1970) #include <asm/word-at-a-time.h>
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 1971) 
468a9428521e7 (George Spelvin          2016-05-26 22:11:51 -0400 1972) #ifdef HASH_MIX
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 1973) 
468a9428521e7 (George Spelvin          2016-05-26 22:11:51 -0400 1974) /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 1975) 
468a9428521e7 (George Spelvin          2016-05-26 22:11:51 -0400 1976) #elif defined(CONFIG_64BIT)
0fed3ac866eab (George Spelvin          2016-05-02 06:31:01 -0400 1977) /*
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 1978)  * Register pressure in the mixing function is an issue, particularly
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 1979)  * on 32-bit x86, but almost any function requires one state value and
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 1980)  * one temporary.  Instead, use a function designed for two state values
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 1981)  * and no temporaries.
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 1982)  *
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 1983)  * This function cannot create a collision in only two iterations, so
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 1984)  * we have two iterations to achieve avalanche.  In those two iterations,
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 1985)  * we have six layers of mixing, which is enough to spread one bit's
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 1986)  * influence out to 2^6 = 64 state bits.
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 1987)  *
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 1988)  * Rotate constants are scored by considering either 64 one-bit input
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 1989)  * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 1990)  * probability of that delta causing a change to each of the 128 output
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 1991)  * bits, using a sample of random initial states.
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 1992)  *
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 1993)  * The Shannon entropy of the computed probabilities is then summed
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 1994)  * to produce a score.  Ideally, any input change has a 50% chance of
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 1995)  * toggling any given output bit.
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 1996)  *
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 1997)  * Mixing scores (in bits) for (12,45):
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 1998)  * Input delta: 1-bit      2-bit
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 1999)  * 1 round:     713.3    42542.6
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2000)  * 2 rounds:   2753.7   140389.8
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2001)  * 3 rounds:   5954.1   233458.2
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2002)  * 4 rounds:   7862.6   256672.2
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2003)  * Perfect:    8192     258048
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2004)  *            (64*128) (64*63/2 * 128)
0fed3ac866eab (George Spelvin          2016-05-02 06:31:01 -0400 2005)  */
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2006) #define HASH_MIX(x, y, a)	\
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2007) 	(	x ^= (a),	\
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2008) 	y ^= x,	x = rol64(x,12),\
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2009) 	x += y,	y = rol64(y,45),\
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2010) 	y *= 9			)
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2011) 
0fed3ac866eab (George Spelvin          2016-05-02 06:31:01 -0400 2012) /*
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2013)  * Fold two longs into one 32-bit hash value.  This must be fast, but
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2014)  * latency isn't quite as critical, as there is a fair bit of additional
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2015)  * work done before the hash value is used.
0fed3ac866eab (George Spelvin          2016-05-02 06:31:01 -0400 2016)  */
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2017) static inline unsigned int fold_hash(unsigned long x, unsigned long y)
0fed3ac866eab (George Spelvin          2016-05-02 06:31:01 -0400 2018) {
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2019) 	y ^= x * GOLDEN_RATIO_64;
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2020) 	y *= GOLDEN_RATIO_64;
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2021) 	return y >> 32;
0fed3ac866eab (George Spelvin          2016-05-02 06:31:01 -0400 2022) }
0fed3ac866eab (George Spelvin          2016-05-02 06:31:01 -0400 2023) 
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2024) #else	/* 32-bit case */
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2025) 
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2026) /*
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2027)  * Mixing scores (in bits) for (7,20):
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2028)  * Input delta: 1-bit      2-bit
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2029)  * 1 round:     330.3     9201.6
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2030)  * 2 rounds:   1246.4    25475.4
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2031)  * 3 rounds:   1907.1    31295.1
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2032)  * 4 rounds:   2042.3    31718.6
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2033)  * Perfect:    2048      31744
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2034)  *            (32*64)   (32*31/2 * 64)
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2035)  */
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2036) #define HASH_MIX(x, y, a)	\
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2037) 	(	x ^= (a),	\
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2038) 	y ^= x,	x = rol32(x, 7),\
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2039) 	x += y,	y = rol32(y,20),\
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2040) 	y *= 9			)
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2041) 
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2042) static inline unsigned int fold_hash(unsigned long x, unsigned long y)
0fed3ac866eab (George Spelvin          2016-05-02 06:31:01 -0400 2043) {
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2044) 	/* Use arch-optimized multiply if one exists */
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2045) 	return __hash_32(y ^ __hash_32(x));
0fed3ac866eab (George Spelvin          2016-05-02 06:31:01 -0400 2046) }
0fed3ac866eab (George Spelvin          2016-05-02 06:31:01 -0400 2047) 
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2048) #endif
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2049) 
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2050) /*
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2051)  * Return the hash of a string of known length.  This is carfully
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2052)  * designed to match hash_name(), which is the more critical function.
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2053)  * In particular, we must end by hashing a final word containing 0..7
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2054)  * payload bytes, to match the way that hash_name() iterates until it
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2055)  * finds the delimiter after the name.
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2056)  */
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2057) unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2058) {
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2059) 	unsigned long a, x = 0, y = (unsigned long)salt;
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2060) 
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2061) 	for (;;) {
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2062) 		if (!len)
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2063) 			goto done;
e419b4cc58568 (Linus Torvalds          2012-05-03 10:16:43 -0700 2064) 		a = load_unaligned_zeropad(name);
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2065) 		if (len < sizeof(unsigned long))
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2066) 			break;
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2067) 		HASH_MIX(x, y, a);
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2068) 		name += sizeof(unsigned long);
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2069) 		len -= sizeof(unsigned long);
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2070) 	}
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2071) 	x ^= a & bytemask_from_count(len);
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2072) done:
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2073) 	return fold_hash(x, y);
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2074) }
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2075) EXPORT_SYMBOL(full_name_hash);
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2076) 
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2077) /* Return the "hash_len" (hash and length) of a null-terminated string */
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2078) u64 hashlen_string(const void *salt, const char *name)
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2079) {
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2080) 	unsigned long a = 0, x = 0, y = (unsigned long)salt;
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2081) 	unsigned long adata, mask, len;
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2082) 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2083) 
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2084) 	len = 0;
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2085) 	goto inside;
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2086) 
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2087) 	do {
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2088) 		HASH_MIX(x, y, a);
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2089) 		len += sizeof(unsigned long);
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2090) inside:
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2091) 		a = load_unaligned_zeropad(name+len);
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2092) 	} while (!has_zero(a, &adata, &constants));
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2093) 
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2094) 	adata = prep_zero_mask(a, adata, &constants);
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2095) 	mask = create_zero_mask(adata);
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2096) 	x ^= a & zero_bytemask(mask);
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2097) 
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2098) 	return hashlen_create(fold_hash(x, y), len + find_zero(mask));
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2099) }
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2100) EXPORT_SYMBOL(hashlen_string);
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2101) 
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2102) /*
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2103)  * Calculate the length and hash of the path component, and
d6bb3e9075bbc (Linus Torvalds          2014-09-15 10:51:07 -0700 2104)  * return the "hash_len" as the result.
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2105)  */
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2106) static inline u64 hash_name(const void *salt, const char *name)
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2107) {
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2108) 	unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2109) 	unsigned long adata, bdata, mask, len;
36126f8f2ed81 (Linus Torvalds          2012-05-26 10:43:17 -0700 2110) 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2111) 
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2112) 	len = 0;
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2113) 	goto inside;
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2114) 
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2115) 	do {
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2116) 		HASH_MIX(x, y, a);
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2117) 		len += sizeof(unsigned long);
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2118) inside:
e419b4cc58568 (Linus Torvalds          2012-05-03 10:16:43 -0700 2119) 		a = load_unaligned_zeropad(name+len);
36126f8f2ed81 (Linus Torvalds          2012-05-26 10:43:17 -0700 2120) 		b = a ^ REPEAT_BYTE('/');
36126f8f2ed81 (Linus Torvalds          2012-05-26 10:43:17 -0700 2121) 	} while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
36126f8f2ed81 (Linus Torvalds          2012-05-26 10:43:17 -0700 2122) 
36126f8f2ed81 (Linus Torvalds          2012-05-26 10:43:17 -0700 2123) 	adata = prep_zero_mask(a, adata, &constants);
36126f8f2ed81 (Linus Torvalds          2012-05-26 10:43:17 -0700 2124) 	bdata = prep_zero_mask(b, bdata, &constants);
36126f8f2ed81 (Linus Torvalds          2012-05-26 10:43:17 -0700 2125) 	mask = create_zero_mask(adata | bdata);
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2126) 	x ^= a & zero_bytemask(mask);
36126f8f2ed81 (Linus Torvalds          2012-05-26 10:43:17 -0700 2127) 
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2128) 	return hashlen_create(fold_hash(x, y), len + find_zero(mask));
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2129) }
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2130) 
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2131) #else	/* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2132) 
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2133) /* Return the hash of a string of known length */
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2134) unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
0145acc202ca6 (Linus Torvalds          2012-03-02 14:32:59 -0800 2135) {
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2136) 	unsigned long hash = init_name_hash(salt);
0145acc202ca6 (Linus Torvalds          2012-03-02 14:32:59 -0800 2137) 	while (len--)
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2138) 		hash = partial_name_hash((unsigned char)*name++, hash);
0145acc202ca6 (Linus Torvalds          2012-03-02 14:32:59 -0800 2139) 	return end_name_hash(hash);
0145acc202ca6 (Linus Torvalds          2012-03-02 14:32:59 -0800 2140) }
ae942ae71934f (Linus Torvalds          2012-03-02 19:40:57 -0800 2141) EXPORT_SYMBOL(full_name_hash);
0145acc202ca6 (Linus Torvalds          2012-03-02 14:32:59 -0800 2142) 
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2143) /* Return the "hash_len" (hash and length) of a null-terminated string */
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2144) u64 hashlen_string(const void *salt, const char *name)
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2145) {
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2146) 	unsigned long hash = init_name_hash(salt);
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2147) 	unsigned long len = 0, c;
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2148) 
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2149) 	c = (unsigned char)*name;
e0ab7af9bd1c2 (George Spelvin          2016-05-29 08:05:56 -0400 2150) 	while (c) {
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2151) 		len++;
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2152) 		hash = partial_name_hash(c, hash);
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2153) 		c = (unsigned char)name[len];
e0ab7af9bd1c2 (George Spelvin          2016-05-29 08:05:56 -0400 2154) 	}
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2155) 	return hashlen_create(end_name_hash(hash), len);
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2156) }
f2a031b66ec6d (George Spelvin          2016-05-29 01:26:41 -0400 2157) EXPORT_SYMBOL(hashlen_string);
fcfd2fbf22d25 (George Spelvin          2016-05-20 08:41:37 -0400 2158) 
200e9ef7ab51f (Linus Torvalds          2012-03-02 14:49:24 -0800 2159) /*
200e9ef7ab51f (Linus Torvalds          2012-03-02 14:49:24 -0800 2160)  * We know there's a real path component here of at least
200e9ef7ab51f (Linus Torvalds          2012-03-02 14:49:24 -0800 2161)  * one character.
200e9ef7ab51f (Linus Torvalds          2012-03-02 14:49:24 -0800 2162)  */
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2163) static inline u64 hash_name(const void *salt, const char *name)
200e9ef7ab51f (Linus Torvalds          2012-03-02 14:49:24 -0800 2164) {
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2165) 	unsigned long hash = init_name_hash(salt);
200e9ef7ab51f (Linus Torvalds          2012-03-02 14:49:24 -0800 2166) 	unsigned long len = 0, c;
200e9ef7ab51f (Linus Torvalds          2012-03-02 14:49:24 -0800 2167) 
200e9ef7ab51f (Linus Torvalds          2012-03-02 14:49:24 -0800 2168) 	c = (unsigned char)*name;
200e9ef7ab51f (Linus Torvalds          2012-03-02 14:49:24 -0800 2169) 	do {
200e9ef7ab51f (Linus Torvalds          2012-03-02 14:49:24 -0800 2170) 		len++;
200e9ef7ab51f (Linus Torvalds          2012-03-02 14:49:24 -0800 2171) 		hash = partial_name_hash(c, hash);
200e9ef7ab51f (Linus Torvalds          2012-03-02 14:49:24 -0800 2172) 		c = (unsigned char)name[len];
200e9ef7ab51f (Linus Torvalds          2012-03-02 14:49:24 -0800 2173) 	} while (c && c != '/');
d6bb3e9075bbc (Linus Torvalds          2014-09-15 10:51:07 -0700 2174) 	return hashlen_create(end_name_hash(hash), len);
200e9ef7ab51f (Linus Torvalds          2012-03-02 14:49:24 -0800 2175) }
200e9ef7ab51f (Linus Torvalds          2012-03-02 14:49:24 -0800 2176) 
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2177) #endif
bfcfaa77bdf0f (Linus Torvalds          2012-03-06 11:16:17 -0800 2178) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2179) /*
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2180)  * Name resolution.
ea3834d9fb348 (Prasanna Meda           2005-04-29 16:00:17 +0100 2181)  * This is the basic name resolution function, turning a pathname into
ea3834d9fb348 (Prasanna Meda           2005-04-29 16:00:17 +0100 2182)  * the final dentry. We expect 'base' to be positive and a directory.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2183)  *
ea3834d9fb348 (Prasanna Meda           2005-04-29 16:00:17 +0100 2184)  * Returns 0 and nd will have valid dentry and mnt on success.
ea3834d9fb348 (Prasanna Meda           2005-04-29 16:00:17 +0100 2185)  * Returns error and drops reference to input namei data on failure.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2186)  */
6de88d72927dc (Al Viro                 2009-08-09 01:41:57 +0400 2187) static int link_path_walk(const char *name, struct nameidata *nd)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2188) {
d8d4611a4f2dd (Al Viro                 2020-02-23 22:04:15 -0500 2189) 	int depth = 0; // depth <= nd->depth
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2190) 	int err;
32cd74685c75f (Al Viro                 2015-04-18 20:30:49 -0400 2191) 
b4c0353693d22 (Al Viro                 2020-01-19 11:44:51 -0500 2192) 	nd->last_type = LAST_ROOT;
c108837e06b65 (Al Viro                 2020-03-05 15:48:44 -0500 2193) 	nd->flags |= LOOKUP_PARENT;
9b5858e99ae1c (Al Viro                 2018-07-09 16:33:23 -0400 2194) 	if (IS_ERR(name))
9b5858e99ae1c (Al Viro                 2018-07-09 16:33:23 -0400 2195) 		return PTR_ERR(name);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2196) 	while (*name=='/')
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2197) 		name++;
1a97d899ecbc4 (Al Viro                 2020-09-19 17:55:58 +0100 2198) 	if (!*name) {
1a97d899ecbc4 (Al Viro                 2020-09-19 17:55:58 +0100 2199) 		nd->dir_mode = 0; // short-circuit the 'hardening' idiocy
9e18f10a30e0c (Al Viro                 2015-04-18 20:44:34 -0400 2200) 		return 0;
1a97d899ecbc4 (Al Viro                 2020-09-19 17:55:58 +0100 2201) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2202) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2203) 	/* At this point we know we have a real path component. */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2204) 	for(;;) {
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 2205) 		struct user_namespace *mnt_userns;
92d270165cffb (Al Viro                 2020-01-14 13:24:17 -0500 2206) 		const char *link;
d6bb3e9075bbc (Linus Torvalds          2014-09-15 10:51:07 -0700 2207) 		u64 hash_len;
fe479a580dc9c (Al Viro                 2011-02-22 15:10:03 -0500 2208) 		int type;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2209) 
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 2210) 		mnt_userns = mnt_user_ns(nd->path.mnt);
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 2211) 		err = may_lookup(mnt_userns, nd);
2a18da7a9c788 (George Spelvin          2016-05-23 07:43:58 -0400 2212) 		if (err)
3595e2346cd91 (Al Viro                 2015-05-09 16:54:45 -0400 2213) 			return err;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2214) 
8387ff2577eb9 (Linus Torvalds          2016-06-10 07:51:30 -0700 2215) 		hash_len = hash_name(nd->path.dentry, name);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2216) 
fe479a580dc9c (Al Viro                 2011-02-22 15:10:03 -0500 2217) 		type = LAST_NORM;
d6bb3e9075bbc (Linus Torvalds          2014-09-15 10:51:07 -0700 2218) 		if (name[0] == '.') switch (hashlen_len(hash_len)) {
fe479a580dc9c (Al Viro                 2011-02-22 15:10:03 -0500 2219) 			case 2:
200e9ef7ab51f (Linus Torvalds          2012-03-02 14:49:24 -0800 2220) 				if (name[1] == '.') {
fe479a580dc9c (Al Viro                 2011-02-22 15:10:03 -0500 2221) 					type = LAST_DOTDOT;
16c2cd7179881 (Al Viro                 2011-02-22 15:50:10 -0500 2222) 					nd->flags |= LOOKUP_JUMPED;
16c2cd7179881 (Al Viro                 2011-02-22 15:50:10 -0500 2223) 				}
fe479a580dc9c (Al Viro                 2011-02-22 15:10:03 -0500 2224) 				break;
fe479a580dc9c (Al Viro                 2011-02-22 15:10:03 -0500 2225) 			case 1:
fe479a580dc9c (Al Viro                 2011-02-22 15:10:03 -0500 2226) 				type = LAST_DOT;
fe479a580dc9c (Al Viro                 2011-02-22 15:10:03 -0500 2227) 		}
5a202bcd75bbd (Al Viro                 2011-03-08 14:17:44 -0500 2228) 		if (likely(type == LAST_NORM)) {
5a202bcd75bbd (Al Viro                 2011-03-08 14:17:44 -0500 2229) 			struct dentry *parent = nd->path.dentry;
16c2cd7179881 (Al Viro                 2011-02-22 15:50:10 -0500 2230) 			nd->flags &= ~LOOKUP_JUMPED;
5a202bcd75bbd (Al Viro                 2011-03-08 14:17:44 -0500 2231) 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
a060dc5010ffa (James Hogan             2014-09-16 13:07:35 +0100 2232) 				struct qstr this = { { .hash_len = hash_len }, .name = name };
da53be12bbb4f (Linus Torvalds          2013-05-21 15:22:44 -0700 2233) 				err = parent->d_op->d_hash(parent, &this);
5a202bcd75bbd (Al Viro                 2011-03-08 14:17:44 -0500 2234) 				if (err < 0)
3595e2346cd91 (Al Viro                 2015-05-09 16:54:45 -0400 2235) 					return err;
d6bb3e9075bbc (Linus Torvalds          2014-09-15 10:51:07 -0700 2236) 				hash_len = this.hash_len;
d6bb3e9075bbc (Linus Torvalds          2014-09-15 10:51:07 -0700 2237) 				name = this.name;
5a202bcd75bbd (Al Viro                 2011-03-08 14:17:44 -0500 2238) 			}
5a202bcd75bbd (Al Viro                 2011-03-08 14:17:44 -0500 2239) 		}
fe479a580dc9c (Al Viro                 2011-02-22 15:10:03 -0500 2240) 
d6bb3e9075bbc (Linus Torvalds          2014-09-15 10:51:07 -0700 2241) 		nd->last.hash_len = hash_len;
d6bb3e9075bbc (Linus Torvalds          2014-09-15 10:51:07 -0700 2242) 		nd->last.name = name;
5f4a6a695075b (Al Viro                 2013-01-24 18:04:22 -0500 2243) 		nd->last_type = type;
5f4a6a695075b (Al Viro                 2013-01-24 18:04:22 -0500 2244) 
d6bb3e9075bbc (Linus Torvalds          2014-09-15 10:51:07 -0700 2245) 		name += hashlen_len(hash_len);
d6bb3e9075bbc (Linus Torvalds          2014-09-15 10:51:07 -0700 2246) 		if (!*name)
bdf6cbf17923c (Al Viro                 2015-04-18 20:21:40 -0400 2247) 			goto OK;
200e9ef7ab51f (Linus Torvalds          2012-03-02 14:49:24 -0800 2248) 		/*
200e9ef7ab51f (Linus Torvalds          2012-03-02 14:49:24 -0800 2249) 		 * If it wasn't NUL, we know it was '/'. Skip that
200e9ef7ab51f (Linus Torvalds          2012-03-02 14:49:24 -0800 2250) 		 * slash, and continue until no more slashes.
200e9ef7ab51f (Linus Torvalds          2012-03-02 14:49:24 -0800 2251) 		 */
200e9ef7ab51f (Linus Torvalds          2012-03-02 14:49:24 -0800 2252) 		do {
d6bb3e9075bbc (Linus Torvalds          2014-09-15 10:51:07 -0700 2253) 			name++;
d6bb3e9075bbc (Linus Torvalds          2014-09-15 10:51:07 -0700 2254) 		} while (unlikely(*name == '/'));
8620c238edbf3 (Al Viro                 2015-05-04 08:58:35 -0400 2255) 		if (unlikely(!*name)) {
8620c238edbf3 (Al Viro                 2015-05-04 08:58:35 -0400 2256) OK:
d8d4611a4f2dd (Al Viro                 2020-02-23 22:04:15 -0500 2257) 			/* pathname or trailing symlink, done */
c108837e06b65 (Al Viro                 2020-03-05 15:48:44 -0500 2258) 			if (!depth) {
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 2259) 				nd->dir_uid = i_uid_into_mnt(mnt_userns, nd->inode);
0f705953012a7 (Al Viro                 2020-03-05 11:34:48 -0500 2260) 				nd->dir_mode = nd->inode->i_mode;
c108837e06b65 (Al Viro                 2020-03-05 15:48:44 -0500 2261) 				nd->flags &= ~LOOKUP_PARENT;
8620c238edbf3 (Al Viro                 2015-05-04 08:58:35 -0400 2262) 				return 0;
c108837e06b65 (Al Viro                 2020-03-05 15:48:44 -0500 2263) 			}
8620c238edbf3 (Al Viro                 2015-05-04 08:58:35 -0400 2264) 			/* last component of nested symlink */
d8d4611a4f2dd (Al Viro                 2020-02-23 22:04:15 -0500 2265) 			name = nd->stack[--depth].name;
8c4efe22e7c4d (Al Viro                 2020-01-19 12:44:18 -0500 2266) 			link = walk_component(nd, 0);
1c4ff1a87e46a (Al Viro                 2016-11-14 01:39:36 -0500 2267) 		} else {
1c4ff1a87e46a (Al Viro                 2016-11-14 01:39:36 -0500 2268) 			/* not the last component */
8c4efe22e7c4d (Al Viro                 2020-01-19 12:44:18 -0500 2269) 			link = walk_component(nd, WALK_MORE);
8620c238edbf3 (Al Viro                 2015-05-04 08:58:35 -0400 2270) 		}
92d270165cffb (Al Viro                 2020-01-14 13:24:17 -0500 2271) 		if (unlikely(link)) {
92d270165cffb (Al Viro                 2020-01-14 13:24:17 -0500 2272) 			if (IS_ERR(link))
92d270165cffb (Al Viro                 2020-01-14 13:24:17 -0500 2273) 				return PTR_ERR(link);
92d270165cffb (Al Viro                 2020-01-14 13:24:17 -0500 2274) 			/* a symlink to follow */
d8d4611a4f2dd (Al Viro                 2020-02-23 22:04:15 -0500 2275) 			nd->stack[depth++].name = name;
92d270165cffb (Al Viro                 2020-01-14 13:24:17 -0500 2276) 			name = link;
92d270165cffb (Al Viro                 2020-01-14 13:24:17 -0500 2277) 			continue;
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 2278) 		}
97242f99a0139 (Al Viro                 2015-08-01 19:59:28 -0400 2279) 		if (unlikely(!d_can_lookup(nd->path.dentry))) {
97242f99a0139 (Al Viro                 2015-08-01 19:59:28 -0400 2280) 			if (nd->flags & LOOKUP_RCU) {
e36cffed20a32 (Jens Axboe              2020-12-17 09:19:08 -0700 2281) 				if (!try_to_unlazy(nd))
97242f99a0139 (Al Viro                 2015-08-01 19:59:28 -0400 2282) 					return -ECHILD;
97242f99a0139 (Al Viro                 2015-08-01 19:59:28 -0400 2283) 			}
3595e2346cd91 (Al Viro                 2015-05-09 16:54:45 -0400 2284) 			return -ENOTDIR;
97242f99a0139 (Al Viro                 2015-08-01 19:59:28 -0400 2285) 		}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2286) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2287) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2288) 
edc2b1da77988 (Al Viro                 2018-07-09 16:27:23 -0400 2289) /* must be paired with terminate_walk() */
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 2290) static const char *path_init(struct nameidata *nd, unsigned flags)
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 2291) {
740a16782750a (Aleksa Sarai            2019-12-07 01:13:29 +1100 2292) 	int error;
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 2293) 	const char *s = nd->name->name;
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 2294) 
6c6ec2b0a3e03 (Jens Axboe              2020-12-17 09:19:09 -0700 2295) 	/* LOOKUP_CACHED requires RCU, ask caller to retry */
6c6ec2b0a3e03 (Jens Axboe              2020-12-17 09:19:09 -0700 2296) 	if ((flags & (LOOKUP_RCU | LOOKUP_CACHED)) == LOOKUP_CACHED)
6c6ec2b0a3e03 (Jens Axboe              2020-12-17 09:19:09 -0700 2297) 		return ERR_PTR(-EAGAIN);
6c6ec2b0a3e03 (Jens Axboe              2020-12-17 09:19:09 -0700 2298) 
c0eb027e5aef7 (Linus Torvalds          2017-04-02 17:10:08 -0700 2299) 	if (!*s)
c0eb027e5aef7 (Linus Torvalds          2017-04-02 17:10:08 -0700 2300) 		flags &= ~LOOKUP_RCU;
edc2b1da77988 (Al Viro                 2018-07-09 16:27:23 -0400 2301) 	if (flags & LOOKUP_RCU)
edc2b1da77988 (Al Viro                 2018-07-09 16:27:23 -0400 2302) 		rcu_read_lock();
c0eb027e5aef7 (Linus Torvalds          2017-04-02 17:10:08 -0700 2303) 
c108837e06b65 (Al Viro                 2020-03-05 15:48:44 -0500 2304) 	nd->flags = flags | LOOKUP_JUMPED;
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 2305) 	nd->depth = 0;
ab87f9a56c8ee (Aleksa Sarai            2019-12-07 01:13:35 +1100 2306) 
ab87f9a56c8ee (Aleksa Sarai            2019-12-07 01:13:35 +1100 2307) 	nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount);
ab87f9a56c8ee (Aleksa Sarai            2019-12-07 01:13:35 +1100 2308) 	nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount);
ab87f9a56c8ee (Aleksa Sarai            2019-12-07 01:13:35 +1100 2309) 	smp_rmb();
ab87f9a56c8ee (Aleksa Sarai            2019-12-07 01:13:35 +1100 2310) 
5b6ca027d85b7 (Al Viro                 2011-03-09 23:04:47 -0500 2311) 	if (flags & LOOKUP_ROOT) {
b18825a7c8e37 (David Howells           2013-09-12 19:22:53 +0100 2312) 		struct dentry *root = nd->root.dentry;
b18825a7c8e37 (David Howells           2013-09-12 19:22:53 +0100 2313) 		struct inode *inode = root->d_inode;
93893862fb7ba (Al Viro                 2017-04-15 17:29:14 -0400 2314) 		if (*s && unlikely(!d_can_lookup(root)))
93893862fb7ba (Al Viro                 2017-04-15 17:29:14 -0400 2315) 			return ERR_PTR(-ENOTDIR);
5b6ca027d85b7 (Al Viro                 2011-03-09 23:04:47 -0500 2316) 		nd->path = nd->root;
5b6ca027d85b7 (Al Viro                 2011-03-09 23:04:47 -0500 2317) 		nd->inode = inode;
5b6ca027d85b7 (Al Viro                 2011-03-09 23:04:47 -0500 2318) 		if (flags & LOOKUP_RCU) {
ab87f9a56c8ee (Aleksa Sarai            2019-12-07 01:13:35 +1100 2319) 			nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
8f47a0167c567 (Al Viro                 2015-05-09 19:02:01 -0400 2320) 			nd->root_seq = nd->seq;
5b6ca027d85b7 (Al Viro                 2011-03-09 23:04:47 -0500 2321) 		} else {
5b6ca027d85b7 (Al Viro                 2011-03-09 23:04:47 -0500 2322) 			path_get(&nd->path);
5b6ca027d85b7 (Al Viro                 2011-03-09 23:04:47 -0500 2323) 		}
368ee9ba565d6 (Al Viro                 2015-05-08 17:19:59 -0400 2324) 		return s;
5b6ca027d85b7 (Al Viro                 2011-03-09 23:04:47 -0500 2325) 	}
5b6ca027d85b7 (Al Viro                 2011-03-09 23:04:47 -0500 2326) 
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 2327) 	nd->root.mnt = NULL;
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 2328) 
8db52c7e7ee1b (Aleksa Sarai            2019-12-07 01:13:34 +1100 2329) 	/* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */
8db52c7e7ee1b (Aleksa Sarai            2019-12-07 01:13:34 +1100 2330) 	if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) {
740a16782750a (Aleksa Sarai            2019-12-07 01:13:29 +1100 2331) 		error = nd_jump_root(nd);
740a16782750a (Aleksa Sarai            2019-12-07 01:13:29 +1100 2332) 		if (unlikely(error))
740a16782750a (Aleksa Sarai            2019-12-07 01:13:29 +1100 2333) 			return ERR_PTR(error);
740a16782750a (Aleksa Sarai            2019-12-07 01:13:29 +1100 2334) 		return s;
8db52c7e7ee1b (Aleksa Sarai            2019-12-07 01:13:34 +1100 2335) 	}
8db52c7e7ee1b (Aleksa Sarai            2019-12-07 01:13:34 +1100 2336) 
8db52c7e7ee1b (Aleksa Sarai            2019-12-07 01:13:34 +1100 2337) 	/* Relative pathname -- get the starting-point it is relative to. */
8db52c7e7ee1b (Aleksa Sarai            2019-12-07 01:13:34 +1100 2338) 	if (nd->dfd == AT_FDCWD) {
e41f7d4ee5bdb (Al Viro                 2011-02-22 14:02:58 -0500 2339) 		if (flags & LOOKUP_RCU) {
e41f7d4ee5bdb (Al Viro                 2011-02-22 14:02:58 -0500 2340) 			struct fs_struct *fs = current->fs;
e41f7d4ee5bdb (Al Viro                 2011-02-22 14:02:58 -0500 2341) 			unsigned seq;
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 2342) 
e41f7d4ee5bdb (Al Viro                 2011-02-22 14:02:58 -0500 2343) 			do {
e41f7d4ee5bdb (Al Viro                 2011-02-22 14:02:58 -0500 2344) 				seq = read_seqcount_begin(&fs->seq);
e41f7d4ee5bdb (Al Viro                 2011-02-22 14:02:58 -0500 2345) 				nd->path = fs->pwd;
ef55d91700d54 (Al Viro                 2015-12-05 20:25:06 -0500 2346) 				nd->inode = nd->path.dentry->d_inode;
e41f7d4ee5bdb (Al Viro                 2011-02-22 14:02:58 -0500 2347) 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
e41f7d4ee5bdb (Al Viro                 2011-02-22 14:02:58 -0500 2348) 			} while (read_seqcount_retry(&fs->seq, seq));
e41f7d4ee5bdb (Al Viro                 2011-02-22 14:02:58 -0500 2349) 		} else {
e41f7d4ee5bdb (Al Viro                 2011-02-22 14:02:58 -0500 2350) 			get_fs_pwd(current->fs, &nd->path);
ef55d91700d54 (Al Viro                 2015-12-05 20:25:06 -0500 2351) 			nd->inode = nd->path.dentry->d_inode;
e41f7d4ee5bdb (Al Viro                 2011-02-22 14:02:58 -0500 2352) 		}
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 2353) 	} else {
582aa64a04a57 (Jeff Layton             2012-12-11 08:56:16 -0500 2354) 		/* Caller must check execute permissions on the starting path component */
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 2355) 		struct fd f = fdget_raw(nd->dfd);
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 2356) 		struct dentry *dentry;
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 2357) 
2903ff019b346 (Al Viro                 2012-08-28 12:52:22 -0400 2358) 		if (!f.file)
368ee9ba565d6 (Al Viro                 2015-05-08 17:19:59 -0400 2359) 			return ERR_PTR(-EBADF);
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 2360) 
2903ff019b346 (Al Viro                 2012-08-28 12:52:22 -0400 2361) 		dentry = f.file->f_path.dentry;
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 2362) 
edc2b1da77988 (Al Viro                 2018-07-09 16:27:23 -0400 2363) 		if (*s && unlikely(!d_can_lookup(dentry))) {
edc2b1da77988 (Al Viro                 2018-07-09 16:27:23 -0400 2364) 			fdput(f);
edc2b1da77988 (Al Viro                 2018-07-09 16:27:23 -0400 2365) 			return ERR_PTR(-ENOTDIR);
f52e0c11305aa (Al Viro                 2011-03-14 18:56:51 -0400 2366) 		}
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 2367) 
2903ff019b346 (Al Viro                 2012-08-28 12:52:22 -0400 2368) 		nd->path = f.file->f_path;
e41f7d4ee5bdb (Al Viro                 2011-02-22 14:02:58 -0500 2369) 		if (flags & LOOKUP_RCU) {
34a26b99b7814 (Al Viro                 2015-05-11 08:05:05 -0400 2370) 			nd->inode = nd->path.dentry->d_inode;
34a26b99b7814 (Al Viro                 2015-05-11 08:05:05 -0400 2371) 			nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
e41f7d4ee5bdb (Al Viro                 2011-02-22 14:02:58 -0500 2372) 		} else {
2903ff019b346 (Al Viro                 2012-08-28 12:52:22 -0400 2373) 			path_get(&nd->path);
34a26b99b7814 (Al Viro                 2015-05-11 08:05:05 -0400 2374) 			nd->inode = nd->path.dentry->d_inode;
e41f7d4ee5bdb (Al Viro                 2011-02-22 14:02:58 -0500 2375) 		}
34a26b99b7814 (Al Viro                 2015-05-11 08:05:05 -0400 2376) 		fdput(f);
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 2377) 	}
8db52c7e7ee1b (Aleksa Sarai            2019-12-07 01:13:34 +1100 2378) 
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100 2379) 	/* For scoped-lookups we need to set the root to the dirfd as well. */
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100 2380) 	if (flags & LOOKUP_IS_SCOPED) {
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100 2381) 		nd->root = nd->path;
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100 2382) 		if (flags & LOOKUP_RCU) {
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100 2383) 			nd->root_seq = nd->seq;
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100 2384) 		} else {
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100 2385) 			path_get(&nd->root);
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100 2386) 			nd->flags |= LOOKUP_ROOT_GRABBED;
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100 2387) 		}
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100 2388) 	}
adb21d2b526f7 (Aleksa Sarai            2019-12-07 01:13:33 +1100 2389) 	return s;
9b4a9b14a793b (Al Viro                 2009-04-07 11:44:16 -0400 2390) }
9b4a9b14a793b (Al Viro                 2009-04-07 11:44:16 -0400 2391) 
1ccac622f9da5 (Al Viro                 2020-01-14 10:13:40 -0500 2392) static inline const char *lookup_last(struct nameidata *nd)
bd92d7fed877e (Al Viro                 2011-03-14 19:54:59 -0400 2393) {
bd92d7fed877e (Al Viro                 2011-03-14 19:54:59 -0400 2394) 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
bd92d7fed877e (Al Viro                 2011-03-14 19:54:59 -0400 2395) 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
bd92d7fed877e (Al Viro                 2011-03-14 19:54:59 -0400 2396) 
c108837e06b65 (Al Viro                 2020-03-05 15:48:44 -0500 2397) 	return walk_component(nd, WALK_TRAILING);
bd92d7fed877e (Al Viro                 2011-03-14 19:54:59 -0400 2398) }
bd92d7fed877e (Al Viro                 2011-03-14 19:54:59 -0400 2399) 
4f757f3cbf54e (Al Viro                 2017-04-15 17:31:22 -0400 2400) static int handle_lookup_down(struct nameidata *nd)
4f757f3cbf54e (Al Viro                 2017-04-15 17:31:22 -0400 2401) {
c153007b7b7a6 (Al Viro                 2020-01-09 14:50:18 -0500 2402) 	if (!(nd->flags & LOOKUP_RCU))
db3c9ade50b1f (Al Viro                 2020-01-09 14:41:00 -0500 2403) 		dget(nd->path.dentry);
b0417d2c72989 (Al Viro                 2020-01-14 13:34:20 -0500 2404) 	return PTR_ERR(step_into(nd, WALK_NOFOLLOW,
b0417d2c72989 (Al Viro                 2020-01-14 13:34:20 -0500 2405) 			nd->path.dentry, nd->inode, nd->seq));
4f757f3cbf54e (Al Viro                 2017-04-15 17:31:22 -0400 2406) }
4f757f3cbf54e (Al Viro                 2017-04-15 17:31:22 -0400 2407) 
9b4a9b14a793b (Al Viro                 2009-04-07 11:44:16 -0400 2408) /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 2409) static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
9b4a9b14a793b (Al Viro                 2009-04-07 11:44:16 -0400 2410) {
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 2411) 	const char *s = path_init(nd, flags);
bd92d7fed877e (Al Viro                 2011-03-14 19:54:59 -0400 2412) 	int err;
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 2413) 
9b5858e99ae1c (Al Viro                 2018-07-09 16:33:23 -0400 2414) 	if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) {
4f757f3cbf54e (Al Viro                 2017-04-15 17:31:22 -0400 2415) 		err = handle_lookup_down(nd);
5f336e722cc96 (Al Viro                 2018-07-09 16:38:06 -0400 2416) 		if (unlikely(err < 0))
5f336e722cc96 (Al Viro                 2018-07-09 16:38:06 -0400 2417) 			s = ERR_PTR(err);
4f757f3cbf54e (Al Viro                 2017-04-15 17:31:22 -0400 2418) 	}
4f757f3cbf54e (Al Viro                 2017-04-15 17:31:22 -0400 2419) 
1ccac622f9da5 (Al Viro                 2020-01-14 10:13:40 -0500 2420) 	while (!(err = link_path_walk(s, nd)) &&
1ccac622f9da5 (Al Viro                 2020-01-14 10:13:40 -0500 2421) 	       (s = lookup_last(nd)) != NULL)
1ccac622f9da5 (Al Viro                 2020-01-14 10:13:40 -0500 2422) 		;
4f0ed93fb92d3 (Al Viro                 2021-04-06 19:46:51 -0400 2423) 	if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) {
4f0ed93fb92d3 (Al Viro                 2021-04-06 19:46:51 -0400 2424) 		err = handle_lookup_down(nd);
4f0ed93fb92d3 (Al Viro                 2021-04-06 19:46:51 -0400 2425) 		nd->flags &= ~LOOKUP_JUMPED; // no d_weak_revalidate(), please...
4f0ed93fb92d3 (Al Viro                 2021-04-06 19:46:51 -0400 2426) 	}
9f1fafee9e42b (Al Viro                 2011-03-25 11:00:12 -0400 2427) 	if (!err)
9f1fafee9e42b (Al Viro                 2011-03-25 11:00:12 -0400 2428) 		err = complete_walk(nd);
bd92d7fed877e (Al Viro                 2011-03-14 19:54:59 -0400 2429) 
deb106c632d73 (Al Viro                 2015-05-08 18:05:21 -0400 2430) 	if (!err && nd->flags & LOOKUP_DIRECTORY)
deb106c632d73 (Al Viro                 2015-05-08 18:05:21 -0400 2431) 		if (!d_can_lookup(nd->path.dentry))
bd23a539d0733 (Al Viro                 2011-03-23 09:56:30 -0400 2432) 			err = -ENOTDIR;
625b6d105446e (Al Viro                 2015-05-12 16:36:12 -0400 2433) 	if (!err) {
625b6d105446e (Al Viro                 2015-05-12 16:36:12 -0400 2434) 		*path = nd->path;
625b6d105446e (Al Viro                 2015-05-12 16:36:12 -0400 2435) 		nd->path.mnt = NULL;
625b6d105446e (Al Viro                 2015-05-12 16:36:12 -0400 2436) 		nd->path.dentry = NULL;
625b6d105446e (Al Viro                 2015-05-12 16:36:12 -0400 2437) 	}
625b6d105446e (Al Viro                 2015-05-12 16:36:12 -0400 2438) 	terminate_walk(nd);
bd92d7fed877e (Al Viro                 2011-03-14 19:54:59 -0400 2439) 	return err;
ee0827cd6b42b (Al Viro                 2011-02-21 23:38:09 -0500 2440) }
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 2441) 
31d921c7fb969 (David Howells           2018-11-01 23:07:24 +0000 2442) int filename_lookup(int dfd, struct filename *name, unsigned flags,
31d921c7fb969 (David Howells           2018-11-01 23:07:24 +0000 2443) 		    struct path *path, struct path *root)
ee0827cd6b42b (Al Viro                 2011-02-21 23:38:09 -0500 2444) {
894bc8c4662ba (Al Viro                 2015-05-02 07:16:16 -0400 2445) 	int retval;
9883d1855ecfa (Al Viro                 2015-05-13 07:28:08 -0400 2446) 	struct nameidata nd;
abc9f5beb1ca9 (Al Viro                 2015-05-12 16:53:42 -0400 2447) 	if (IS_ERR(name))
abc9f5beb1ca9 (Al Viro                 2015-05-12 16:53:42 -0400 2448) 		return PTR_ERR(name);
9ad1aaa61522f (Al Viro                 2015-05-12 16:44:39 -0400 2449) 	if (unlikely(root)) {
9ad1aaa61522f (Al Viro                 2015-05-12 16:44:39 -0400 2450) 		nd.root = *root;
9ad1aaa61522f (Al Viro                 2015-05-12 16:44:39 -0400 2451) 		flags |= LOOKUP_ROOT;
9ad1aaa61522f (Al Viro                 2015-05-12 16:44:39 -0400 2452) 	}
9883d1855ecfa (Al Viro                 2015-05-13 07:28:08 -0400 2453) 	set_nameidata(&nd, dfd, name);
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 2454) 	retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
ee0827cd6b42b (Al Viro                 2011-02-21 23:38:09 -0500 2455) 	if (unlikely(retval == -ECHILD))
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 2456) 		retval = path_lookupat(&nd, flags, path);
ee0827cd6b42b (Al Viro                 2011-02-21 23:38:09 -0500 2457) 	if (unlikely(retval == -ESTALE))
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 2458) 		retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 2459) 
f78570dd6ad95 (Jeff Layton             2012-10-10 15:25:20 -0400 2460) 	if (likely(!retval))
161aff1d93abf (Al Viro                 2020-01-11 22:52:26 -0500 2461) 		audit_inode(name, path->dentry,
161aff1d93abf (Al Viro                 2020-01-11 22:52:26 -0500 2462) 			    flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0);
9883d1855ecfa (Al Viro                 2015-05-13 07:28:08 -0400 2463) 	restore_nameidata();
e4bd1c1a95409 (Al Viro                 2015-05-12 16:40:39 -0400 2464) 	putname(name);
170aa3d02614a (Ulrich Drepper          2006-02-04 23:28:02 -0800 2465) 	return retval;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2466) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2467) 
8bcb77fabd7cb (Al Viro                 2015-05-08 16:59:20 -0400 2468) /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 2469) static int path_parentat(struct nameidata *nd, unsigned flags,
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 2470) 				struct path *parent)
8bcb77fabd7cb (Al Viro                 2015-05-08 16:59:20 -0400 2471) {
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 2472) 	const char *s = path_init(nd, flags);
9b5858e99ae1c (Al Viro                 2018-07-09 16:33:23 -0400 2473) 	int err = link_path_walk(s, nd);
8bcb77fabd7cb (Al Viro                 2015-05-08 16:59:20 -0400 2474) 	if (!err)
8bcb77fabd7cb (Al Viro                 2015-05-08 16:59:20 -0400 2475) 		err = complete_walk(nd);
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 2476) 	if (!err) {
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 2477) 		*parent = nd->path;
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 2478) 		nd->path.mnt = NULL;
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 2479) 		nd->path.dentry = NULL;
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 2480) 	}
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 2481) 	terminate_walk(nd);
8bcb77fabd7cb (Al Viro                 2015-05-08 16:59:20 -0400 2482) 	return err;
8bcb77fabd7cb (Al Viro                 2015-05-08 16:59:20 -0400 2483) }
8bcb77fabd7cb (Al Viro                 2015-05-08 16:59:20 -0400 2484) 
5c31b6cedb675 (Al Viro                 2015-05-12 17:32:54 -0400 2485) static struct filename *filename_parentat(int dfd, struct filename *name,
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 2486) 				unsigned int flags, struct path *parent,
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 2487) 				struct qstr *last, int *type)
8bcb77fabd7cb (Al Viro                 2015-05-08 16:59:20 -0400 2488) {
8bcb77fabd7cb (Al Viro                 2015-05-08 16:59:20 -0400 2489) 	int retval;
9883d1855ecfa (Al Viro                 2015-05-13 07:28:08 -0400 2490) 	struct nameidata nd;
8bcb77fabd7cb (Al Viro                 2015-05-08 16:59:20 -0400 2491) 
5c31b6cedb675 (Al Viro                 2015-05-12 17:32:54 -0400 2492) 	if (IS_ERR(name))
5c31b6cedb675 (Al Viro                 2015-05-12 17:32:54 -0400 2493) 		return name;
9883d1855ecfa (Al Viro                 2015-05-13 07:28:08 -0400 2494) 	set_nameidata(&nd, dfd, name);
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 2495) 	retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
8bcb77fabd7cb (Al Viro                 2015-05-08 16:59:20 -0400 2496) 	if (unlikely(retval == -ECHILD))
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 2497) 		retval = path_parentat(&nd, flags, parent);
8bcb77fabd7cb (Al Viro                 2015-05-08 16:59:20 -0400 2498) 	if (unlikely(retval == -ESTALE))
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 2499) 		retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 2500) 	if (likely(!retval)) {
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 2501) 		*last = nd.last;
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 2502) 		*type = nd.last_type;
c9b07eab0c876 (Al Viro                 2019-07-14 13:22:27 -0400 2503) 		audit_inode(name, parent->dentry, AUDIT_INODE_PARENT);
5c31b6cedb675 (Al Viro                 2015-05-12 17:32:54 -0400 2504) 	} else {
5c31b6cedb675 (Al Viro                 2015-05-12 17:32:54 -0400 2505) 		putname(name);
5c31b6cedb675 (Al Viro                 2015-05-12 17:32:54 -0400 2506) 		name = ERR_PTR(retval);
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 2507) 	}
9883d1855ecfa (Al Viro                 2015-05-13 07:28:08 -0400 2508) 	restore_nameidata();
5c31b6cedb675 (Al Viro                 2015-05-12 17:32:54 -0400 2509) 	return name;
8bcb77fabd7cb (Al Viro                 2015-05-08 16:59:20 -0400 2510) }
8bcb77fabd7cb (Al Viro                 2015-05-08 16:59:20 -0400 2511) 
79714f72d3b96 (Al Viro                 2012-06-15 03:01:42 +0400 2512) /* does lookup, returns the object with parent locked */
79714f72d3b96 (Al Viro                 2012-06-15 03:01:42 +0400 2513) struct dentry *kern_path_locked(const char *name, struct path *path)
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 2514) {
5c31b6cedb675 (Al Viro                 2015-05-12 17:32:54 -0400 2515) 	struct filename *filename;
5c31b6cedb675 (Al Viro                 2015-05-12 17:32:54 -0400 2516) 	struct dentry *d;
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 2517) 	struct qstr last;
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 2518) 	int type;
5168910413830 (Paul Moore              2015-01-22 00:00:03 -0500 2519) 
5c31b6cedb675 (Al Viro                 2015-05-12 17:32:54 -0400 2520) 	filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
5c31b6cedb675 (Al Viro                 2015-05-12 17:32:54 -0400 2521) 				    &last, &type);
5168910413830 (Paul Moore              2015-01-22 00:00:03 -0500 2522) 	if (IS_ERR(filename))
5168910413830 (Paul Moore              2015-01-22 00:00:03 -0500 2523) 		return ERR_CAST(filename);
5c31b6cedb675 (Al Viro                 2015-05-12 17:32:54 -0400 2524) 	if (unlikely(type != LAST_NORM)) {
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 2525) 		path_put(path);
5c31b6cedb675 (Al Viro                 2015-05-12 17:32:54 -0400 2526) 		putname(filename);
5c31b6cedb675 (Al Viro                 2015-05-12 17:32:54 -0400 2527) 		return ERR_PTR(-EINVAL);
79714f72d3b96 (Al Viro                 2012-06-15 03:01:42 +0400 2528) 	}
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 2529) 	inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 2530) 	d = __lookup_hash(&last, path->dentry, 0);
79714f72d3b96 (Al Viro                 2012-06-15 03:01:42 +0400 2531) 	if (IS_ERR(d)) {
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 2532) 		inode_unlock(path->dentry->d_inode);
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 2533) 		path_put(path);
79714f72d3b96 (Al Viro                 2012-06-15 03:01:42 +0400 2534) 	}
5168910413830 (Paul Moore              2015-01-22 00:00:03 -0500 2535) 	putname(filename);
79714f72d3b96 (Al Viro                 2012-06-15 03:01:42 +0400 2536) 	return d;
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 2537) }
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 2538) 
d181146572c4f (Al Viro                 2008-08-02 00:49:18 -0400 2539) int kern_path(const char *name, unsigned int flags, struct path *path)
d181146572c4f (Al Viro                 2008-08-02 00:49:18 -0400 2540) {
abc9f5beb1ca9 (Al Viro                 2015-05-12 16:53:42 -0400 2541) 	return filename_lookup(AT_FDCWD, getname_kernel(name),
abc9f5beb1ca9 (Al Viro                 2015-05-12 16:53:42 -0400 2542) 			       flags, path, NULL);
d181146572c4f (Al Viro                 2008-08-02 00:49:18 -0400 2543) }
4d359507346a1 (Al Viro                 2014-03-14 12:20:17 -0400 2544) EXPORT_SYMBOL(kern_path);
d181146572c4f (Al Viro                 2008-08-02 00:49:18 -0400 2545) 
16f1820028d66 (Josef 'Jeff' Sipek      2007-07-19 01:48:18 -0700 2546) /**
16f1820028d66 (Josef 'Jeff' Sipek      2007-07-19 01:48:18 -0700 2547)  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
16f1820028d66 (Josef 'Jeff' Sipek      2007-07-19 01:48:18 -0700 2548)  * @dentry:  pointer to dentry of the base directory
16f1820028d66 (Josef 'Jeff' Sipek      2007-07-19 01:48:18 -0700 2549)  * @mnt: pointer to vfs mount of the base directory
16f1820028d66 (Josef 'Jeff' Sipek      2007-07-19 01:48:18 -0700 2550)  * @name: pointer to file name
16f1820028d66 (Josef 'Jeff' Sipek      2007-07-19 01:48:18 -0700 2551)  * @flags: lookup flags
e0a0124936171 (Al Viro                 2011-06-27 17:00:37 -0400 2552)  * @path: pointer to struct path to fill
16f1820028d66 (Josef 'Jeff' Sipek      2007-07-19 01:48:18 -0700 2553)  */
16f1820028d66 (Josef 'Jeff' Sipek      2007-07-19 01:48:18 -0700 2554) int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
16f1820028d66 (Josef 'Jeff' Sipek      2007-07-19 01:48:18 -0700 2555) 		    const char *name, unsigned int flags,
e0a0124936171 (Al Viro                 2011-06-27 17:00:37 -0400 2556) 		    struct path *path)
16f1820028d66 (Josef 'Jeff' Sipek      2007-07-19 01:48:18 -0700 2557) {
9ad1aaa61522f (Al Viro                 2015-05-12 16:44:39 -0400 2558) 	struct path root = {.mnt = mnt, .dentry = dentry};
9ad1aaa61522f (Al Viro                 2015-05-12 16:44:39 -0400 2559) 	/* the first argument of filename_lookup() is ignored with root */
abc9f5beb1ca9 (Al Viro                 2015-05-12 16:53:42 -0400 2560) 	return filename_lookup(AT_FDCWD, getname_kernel(name),
abc9f5beb1ca9 (Al Viro                 2015-05-12 16:53:42 -0400 2561) 			       flags , path, &root);
16f1820028d66 (Josef 'Jeff' Sipek      2007-07-19 01:48:18 -0700 2562) }
4d359507346a1 (Al Viro                 2014-03-14 12:20:17 -0400 2563) EXPORT_SYMBOL(vfs_path_lookup);
16f1820028d66 (Josef 'Jeff' Sipek      2007-07-19 01:48:18 -0700 2564) 
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2565) static int lookup_one_len_common(const char *name, struct dentry *base,
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2566) 				 int len, struct qstr *this)
057f6c019fff9 (James Morris            2007-04-26 00:12:05 -0700 2567) {
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2568) 	this->name = name;
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2569) 	this->len = len;
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2570) 	this->hash = full_name_hash(base, name, len);
6a96ba54418be (Al Viro                 2011-03-07 23:49:20 -0500 2571) 	if (!len)
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2572) 		return -EACCES;
6a96ba54418be (Al Viro                 2011-03-07 23:49:20 -0500 2573) 
21d8a15ac333b (Al Viro                 2012-11-29 22:17:21 -0500 2574) 	if (unlikely(name[0] == '.')) {
21d8a15ac333b (Al Viro                 2012-11-29 22:17:21 -0500 2575) 		if (len < 2 || (len == 2 && name[1] == '.'))
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2576) 			return -EACCES;
21d8a15ac333b (Al Viro                 2012-11-29 22:17:21 -0500 2577) 	}
21d8a15ac333b (Al Viro                 2012-11-29 22:17:21 -0500 2578) 
6a96ba54418be (Al Viro                 2011-03-07 23:49:20 -0500 2579) 	while (len--) {
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2580) 		unsigned int c = *(const unsigned char *)name++;
6a96ba54418be (Al Viro                 2011-03-07 23:49:20 -0500 2581) 		if (c == '/' || c == '\0')
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2582) 			return -EACCES;
6a96ba54418be (Al Viro                 2011-03-07 23:49:20 -0500 2583) 	}
5a202bcd75bbd (Al Viro                 2011-03-08 14:17:44 -0500 2584) 	/*
5a202bcd75bbd (Al Viro                 2011-03-08 14:17:44 -0500 2585) 	 * See if the low-level filesystem might want
5a202bcd75bbd (Al Viro                 2011-03-08 14:17:44 -0500 2586) 	 * to use its own hash..
5a202bcd75bbd (Al Viro                 2011-03-08 14:17:44 -0500 2587) 	 */
5a202bcd75bbd (Al Viro                 2011-03-08 14:17:44 -0500 2588) 	if (base->d_flags & DCACHE_OP_HASH) {
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2589) 		int err = base->d_op->d_hash(base, this);
5a202bcd75bbd (Al Viro                 2011-03-08 14:17:44 -0500 2590) 		if (err < 0)
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2591) 			return err;
5a202bcd75bbd (Al Viro                 2011-03-08 14:17:44 -0500 2592) 	}
eead19115329c (Christoph Hellwig       2007-10-16 23:25:38 -0700 2593) 
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100 2594) 	return inode_permission(&init_user_ns, base->d_inode, MAY_EXEC);
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2595) }
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2596) 
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2597) /**
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2598)  * try_lookup_one_len - filesystem helper to lookup single pathname component
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2599)  * @name:	pathname component to lookup
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2600)  * @base:	base directory to lookup from
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2601)  * @len:	maximum length @len should be interpreted to
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2602)  *
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2603)  * Look up a dentry by name in the dcache, returning NULL if it does not
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2604)  * currently exist.  The function does not try to create a dentry.
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2605)  *
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2606)  * Note that this routine is purely a helper for filesystem usage and should
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2607)  * not be called by generic code.
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2608)  *
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2609)  * The caller must hold base->i_mutex.
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2610)  */
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2611) struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len)
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2612) {
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2613) 	struct qstr this;
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2614) 	int err;
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2615) 
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2616) 	WARN_ON_ONCE(!inode_is_locked(base->d_inode));
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2617) 
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2618) 	err = lookup_one_len_common(name, base, len, &this);
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2619) 	if (err)
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2620) 		return ERR_PTR(err);
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2621) 
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2622) 	return lookup_dcache(&this, base, 0);
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2623) }
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2624) EXPORT_SYMBOL(try_lookup_one_len);
0da0b7fd73e4f (David Howells           2018-06-15 15:19:22 +0100 2625) 
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2626) /**
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2627)  * lookup_one_len - filesystem helper to lookup single pathname component
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2628)  * @name:	pathname component to lookup
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2629)  * @base:	base directory to lookup from
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2630)  * @len:	maximum length @len should be interpreted to
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2631)  *
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2632)  * Note that this routine is purely a helper for filesystem usage and should
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2633)  * not be called by generic code.
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2634)  *
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2635)  * The caller must hold base->i_mutex.
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2636)  */
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2637) struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2638) {
8613a209ffa6d (Al Viro                 2018-04-06 16:45:33 -0400 2639) 	struct dentry *dentry;
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2640) 	struct qstr this;
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2641) 	int err;
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2642) 
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2643) 	WARN_ON_ONCE(!inode_is_locked(base->d_inode));
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2644) 
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2645) 	err = lookup_one_len_common(name, base, len, &this);
cda309de253f3 (Miklos Szeredi          2012-03-26 12:54:21 +0200 2646) 	if (err)
cda309de253f3 (Miklos Szeredi          2012-03-26 12:54:21 +0200 2647) 		return ERR_PTR(err);
cda309de253f3 (Miklos Szeredi          2012-03-26 12:54:21 +0200 2648) 
8613a209ffa6d (Al Viro                 2018-04-06 16:45:33 -0400 2649) 	dentry = lookup_dcache(&this, base, 0);
8613a209ffa6d (Al Viro                 2018-04-06 16:45:33 -0400 2650) 	return dentry ? dentry : __lookup_slow(&this, base, 0);
057f6c019fff9 (James Morris            2007-04-26 00:12:05 -0700 2651) }
4d359507346a1 (Al Viro                 2014-03-14 12:20:17 -0400 2652) EXPORT_SYMBOL(lookup_one_len);
057f6c019fff9 (James Morris            2007-04-26 00:12:05 -0700 2653) 
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2654) /**
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2655)  * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2656)  * @name:	pathname component to lookup
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2657)  * @base:	base directory to lookup from
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2658)  * @len:	maximum length @len should be interpreted to
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2659)  *
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2660)  * Note that this routine is purely a helper for filesystem usage and should
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2661)  * not be called by generic code.
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2662)  *
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2663)  * Unlike lookup_one_len, it should be called without the parent
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2664)  * i_mutex held, and will take the i_mutex itself if necessary.
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2665)  */
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2666) struct dentry *lookup_one_len_unlocked(const char *name,
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2667) 				       struct dentry *base, int len)
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2668) {
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2669) 	struct qstr this;
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2670) 	int err;
20d00ee829428 (Linus Torvalds          2016-07-29 12:17:52 -0700 2671) 	struct dentry *ret;
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2672) 
3c95f0dce8218 (Al Viro                 2018-04-06 16:32:38 -0400 2673) 	err = lookup_one_len_common(name, base, len, &this);
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2674) 	if (err)
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2675) 		return ERR_PTR(err);
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2676) 
20d00ee829428 (Linus Torvalds          2016-07-29 12:17:52 -0700 2677) 	ret = lookup_dcache(&this, base, 0);
20d00ee829428 (Linus Torvalds          2016-07-29 12:17:52 -0700 2678) 	if (!ret)
20d00ee829428 (Linus Torvalds          2016-07-29 12:17:52 -0700 2679) 		ret = lookup_slow(&this, base, 0);
20d00ee829428 (Linus Torvalds          2016-07-29 12:17:52 -0700 2680) 	return ret;
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2681) }
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2682) EXPORT_SYMBOL(lookup_one_len_unlocked);
bbddca8e8fac0 (NeilBrown               2016-01-07 16:08:20 -0500 2683) 
6c2d4798a8d16 (Al Viro                 2019-10-31 01:21:58 -0400 2684) /*
6c2d4798a8d16 (Al Viro                 2019-10-31 01:21:58 -0400 2685)  * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT)
6c2d4798a8d16 (Al Viro                 2019-10-31 01:21:58 -0400 2686)  * on negatives.  Returns known positive or ERR_PTR(); that's what
6c2d4798a8d16 (Al Viro                 2019-10-31 01:21:58 -0400 2687)  * most of the users want.  Note that pinned negative with unlocked parent
6c2d4798a8d16 (Al Viro                 2019-10-31 01:21:58 -0400 2688)  * _can_ become positive at any time, so callers of lookup_one_len_unlocked()
6c2d4798a8d16 (Al Viro                 2019-10-31 01:21:58 -0400 2689)  * need to be very careful; pinned positives have ->d_inode stable, so
6c2d4798a8d16 (Al Viro                 2019-10-31 01:21:58 -0400 2690)  * this one avoids such problems.
6c2d4798a8d16 (Al Viro                 2019-10-31 01:21:58 -0400 2691)  */
6c2d4798a8d16 (Al Viro                 2019-10-31 01:21:58 -0400 2692) struct dentry *lookup_positive_unlocked(const char *name,
6c2d4798a8d16 (Al Viro                 2019-10-31 01:21:58 -0400 2693) 				       struct dentry *base, int len)
6c2d4798a8d16 (Al Viro                 2019-10-31 01:21:58 -0400 2694) {
6c2d4798a8d16 (Al Viro                 2019-10-31 01:21:58 -0400 2695) 	struct dentry *ret = lookup_one_len_unlocked(name, base, len);
2fa6b1e01a9b1 (Al Viro                 2019-11-12 16:13:06 -0500 2696) 	if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
6c2d4798a8d16 (Al Viro                 2019-10-31 01:21:58 -0400 2697) 		dput(ret);
6c2d4798a8d16 (Al Viro                 2019-10-31 01:21:58 -0400 2698) 		ret = ERR_PTR(-ENOENT);
6c2d4798a8d16 (Al Viro                 2019-10-31 01:21:58 -0400 2699) 	}
6c2d4798a8d16 (Al Viro                 2019-10-31 01:21:58 -0400 2700) 	return ret;
6c2d4798a8d16 (Al Viro                 2019-10-31 01:21:58 -0400 2701) }
6c2d4798a8d16 (Al Viro                 2019-10-31 01:21:58 -0400 2702) EXPORT_SYMBOL(lookup_positive_unlocked);
6c2d4798a8d16 (Al Viro                 2019-10-31 01:21:58 -0400 2703) 
eedf265aa003b (Eric W. Biederman       2016-06-02 10:29:47 -0500 2704) #ifdef CONFIG_UNIX98_PTYS
eedf265aa003b (Eric W. Biederman       2016-06-02 10:29:47 -0500 2705) int path_pts(struct path *path)
eedf265aa003b (Eric W. Biederman       2016-06-02 10:29:47 -0500 2706) {
eedf265aa003b (Eric W. Biederman       2016-06-02 10:29:47 -0500 2707) 	/* Find something mounted on "pts" in the same directory as
eedf265aa003b (Eric W. Biederman       2016-06-02 10:29:47 -0500 2708) 	 * the input path.
eedf265aa003b (Eric W. Biederman       2016-06-02 10:29:47 -0500 2709) 	 */
a6a7eb7628cfa (Al Viro                 2020-03-11 13:05:03 -0400 2710) 	struct dentry *parent = dget_parent(path->dentry);
a6a7eb7628cfa (Al Viro                 2020-03-11 13:05:03 -0400 2711) 	struct dentry *child;
19f6028a01469 (Al Viro                 2020-02-26 20:09:37 -0500 2712) 	struct qstr this = QSTR_INIT("pts", 3);
eedf265aa003b (Eric W. Biederman       2016-06-02 10:29:47 -0500 2713) 
a6a7eb7628cfa (Al Viro                 2020-03-11 13:05:03 -0400 2714) 	if (unlikely(!path_connected(path->mnt, parent))) {
a6a7eb7628cfa (Al Viro                 2020-03-11 13:05:03 -0400 2715) 		dput(parent);
63b27720a476e (Al Viro                 2020-02-24 16:01:19 -0500 2716) 		return -ENOENT;
a6a7eb7628cfa (Al Viro                 2020-03-11 13:05:03 -0400 2717) 	}
63b27720a476e (Al Viro                 2020-02-24 16:01:19 -0500 2718) 	dput(path->dentry);
63b27720a476e (Al Viro                 2020-02-24 16:01:19 -0500 2719) 	path->dentry = parent;
eedf265aa003b (Eric W. Biederman       2016-06-02 10:29:47 -0500 2720) 	child = d_hash_and_lookup(parent, &this);
eedf265aa003b (Eric W. Biederman       2016-06-02 10:29:47 -0500 2721) 	if (!child)
eedf265aa003b (Eric W. Biederman       2016-06-02 10:29:47 -0500 2722) 		return -ENOENT;
eedf265aa003b (Eric W. Biederman       2016-06-02 10:29:47 -0500 2723) 
eedf265aa003b (Eric W. Biederman       2016-06-02 10:29:47 -0500 2724) 	path->dentry = child;
eedf265aa003b (Eric W. Biederman       2016-06-02 10:29:47 -0500 2725) 	dput(parent);
19f6028a01469 (Al Viro                 2020-02-26 20:09:37 -0500 2726) 	follow_down(path);
eedf265aa003b (Eric W. Biederman       2016-06-02 10:29:47 -0500 2727) 	return 0;
eedf265aa003b (Eric W. Biederman       2016-06-02 10:29:47 -0500 2728) }
eedf265aa003b (Eric W. Biederman       2016-06-02 10:29:47 -0500 2729) #endif
eedf265aa003b (Eric W. Biederman       2016-06-02 10:29:47 -0500 2730) 
1fa1e7f615f4d (Andy Whitcroft          2011-11-02 09:44:39 +0100 2731) int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
1fa1e7f615f4d (Andy Whitcroft          2011-11-02 09:44:39 +0100 2732) 		 struct path *path, int *empty)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2733) {
abc9f5beb1ca9 (Al Viro                 2015-05-12 16:53:42 -0400 2734) 	return filename_lookup(dfd, getname_flags(name, flags, empty),
abc9f5beb1ca9 (Al Viro                 2015-05-12 16:53:42 -0400 2735) 			       flags, path, NULL);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2736) }
b853a16176cf3 (Al Viro                 2015-05-13 09:12:02 -0400 2737) EXPORT_SYMBOL(user_path_at_empty);
1fa1e7f615f4d (Andy Whitcroft          2011-11-02 09:44:39 +0100 2738) 
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 2739) int __check_sticky(struct user_namespace *mnt_userns, struct inode *dir,
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 2740) 		   struct inode *inode)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2741) {
8e96e3b7b8407 (Eric W. Biederman       2012-03-03 21:17:15 -0800 2742) 	kuid_t fsuid = current_fsuid();
da9592edebcee (David Howells           2008-11-14 10:39:05 +1100 2743) 
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 2744) 	if (uid_eq(i_uid_into_mnt(mnt_userns, inode), fsuid))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2745) 		return 0;
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 2746) 	if (uid_eq(i_uid_into_mnt(mnt_userns, dir), fsuid))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2747) 		return 0;
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 2748) 	return !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FOWNER);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2749) }
cbdf35bcb833b (Miklos Szeredi          2014-10-24 00:14:36 +0200 2750) EXPORT_SYMBOL(__check_sticky);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2751) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2752) /*
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2753)  *	Check whether we can remove a link victim from directory dir, check
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2754)  *  whether the type of victim is right.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2755)  *  1. We can't do it if dir is read-only (done in permission())
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2756)  *  2. We should have write and exec permissions on dir
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2757)  *  3. We can't remove anything from append-only dir
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2758)  *  4. We can't do anything with immutable dir (done in permission())
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2759)  *  5. If the sticky bit on dir is set we should either
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2760)  *	a. be owner of dir, or
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2761)  *	b. be owner of victim, or
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2762)  *	c. have CAP_FOWNER capability
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2763)  *  6. If the victim is append-only or immutable we can't do antyhing with
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2764)  *     links pointing to it.
0bd23d09b874e (Eric W. Biederman       2016-06-29 14:54:46 -0500 2765)  *  7. If the victim has an unknown uid or gid we can't change the inode.
0bd23d09b874e (Eric W. Biederman       2016-06-29 14:54:46 -0500 2766)  *  8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
0bd23d09b874e (Eric W. Biederman       2016-06-29 14:54:46 -0500 2767)  *  9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
0bd23d09b874e (Eric W. Biederman       2016-06-29 14:54:46 -0500 2768)  * 10. We can't remove a root or mountpoint.
0bd23d09b874e (Eric W. Biederman       2016-06-29 14:54:46 -0500 2769)  * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2770)  *     nfs_async_unlink().
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2771)  */
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 2772) static int may_delete(struct user_namespace *mnt_userns, struct inode *dir,
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 2773) 		      struct dentry *victim, bool isdir)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2774) {
63afdfc781e10 (David Howells           2015-05-06 15:59:00 +0100 2775) 	struct inode *inode = d_backing_inode(victim);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2776) 	int error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2777) 
b18825a7c8e37 (David Howells           2013-09-12 19:22:53 +0100 2778) 	if (d_is_negative(victim))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2779) 		return -ENOENT;
b18825a7c8e37 (David Howells           2013-09-12 19:22:53 +0100 2780) 	BUG_ON(!inode);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2781) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2782) 	BUG_ON(victim->d_parent->d_inode != dir);
593d1ce854dff (Eric W. Biederman       2017-09-14 12:07:32 -0500 2783) 
593d1ce854dff (Eric W. Biederman       2017-09-14 12:07:32 -0500 2784) 	/* Inode writeback is not safe when the uid or gid are invalid. */
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 2785) 	if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) ||
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 2786) 	    !gid_valid(i_gid_into_mnt(mnt_userns, inode)))
593d1ce854dff (Eric W. Biederman       2017-09-14 12:07:32 -0500 2787) 		return -EOVERFLOW;
593d1ce854dff (Eric W. Biederman       2017-09-14 12:07:32 -0500 2788) 
4fa6b5ecbf092 (Jeff Layton             2012-10-10 15:25:25 -0400 2789) 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2790) 
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 2791) 	error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2792) 	if (error)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2793) 		return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2794) 	if (IS_APPEND(dir))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2795) 		return -EPERM;
b18825a7c8e37 (David Howells           2013-09-12 19:22:53 +0100 2796) 
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 2797) 	if (check_sticky(mnt_userns, dir, inode) || IS_APPEND(inode) ||
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 2798) 	    IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) ||
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 2799) 	    HAS_UNMAPPED_ID(mnt_userns, inode))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2800) 		return -EPERM;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2801) 	if (isdir) {
44b1d53043c48 (Miklos Szeredi          2014-04-01 17:08:41 +0200 2802) 		if (!d_is_dir(victim))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2803) 			return -ENOTDIR;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2804) 		if (IS_ROOT(victim))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2805) 			return -EBUSY;
44b1d53043c48 (Miklos Szeredi          2014-04-01 17:08:41 +0200 2806) 	} else if (d_is_dir(victim))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2807) 		return -EISDIR;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2808) 	if (IS_DEADDIR(dir))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2809) 		return -ENOENT;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2810) 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2811) 		return -EBUSY;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2812) 	return 0;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2813) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2814) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2815) /*	Check whether we can create an object with dentry child in directory
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2816)  *  dir.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2817)  *  1. We can't do it if child already exists (open has special treatment for
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2818)  *     this case, but since we are inlined it's OK)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2819)  *  2. We can't do it if dir is read-only (done in permission())
036d523641c66 (Eric W. Biederman       2016-07-01 12:52:06 -0500 2820)  *  3. We can't do it if the fs can't represent the fsuid or fsgid.
036d523641c66 (Eric W. Biederman       2016-07-01 12:52:06 -0500 2821)  *  4. We should have write and exec permissions on dir
036d523641c66 (Eric W. Biederman       2016-07-01 12:52:06 -0500 2822)  *  5. We can't do it if dir is immutable (done in permission())
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2823)  */
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 2824) static inline int may_create(struct user_namespace *mnt_userns,
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 2825) 			     struct inode *dir, struct dentry *child)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2826) {
14e972b451712 (Jeff Layton             2013-05-08 10:25:58 -0400 2827) 	audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2828) 	if (child->d_inode)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2829) 		return -EEXIST;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2830) 	if (IS_DEADDIR(dir))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2831) 		return -ENOENT;
8e5389132ab42 (Christian Brauner       2021-03-20 13:26:23 +0100 2832) 	if (!fsuidgid_has_mapping(dir->i_sb, mnt_userns))
036d523641c66 (Eric W. Biederman       2016-07-01 12:52:06 -0500 2833) 		return -EOVERFLOW;
8e5389132ab42 (Christian Brauner       2021-03-20 13:26:23 +0100 2834) 
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 2835) 	return inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2836) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2837) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2838) /*
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2839)  * p1 and p2 should be directories on the same fs.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2840)  */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2841) struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2842) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2843) 	struct dentry *p;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2844) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2845) 	if (p1 == p2) {
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 2846) 		inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2847) 		return NULL;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2848) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2849) 
fc64005c93090 (Al Viro                 2016-04-10 01:33:30 -0400 2850) 	mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2851) 
e2761a1167633 (OGAWA Hirofumi          2008-10-16 07:50:28 +0900 2852) 	p = d_ancestor(p2, p1);
e2761a1167633 (OGAWA Hirofumi          2008-10-16 07:50:28 +0900 2853) 	if (p) {
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 2854) 		inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 2855) 		inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
e2761a1167633 (OGAWA Hirofumi          2008-10-16 07:50:28 +0900 2856) 		return p;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2857) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2858) 
e2761a1167633 (OGAWA Hirofumi          2008-10-16 07:50:28 +0900 2859) 	p = d_ancestor(p1, p2);
e2761a1167633 (OGAWA Hirofumi          2008-10-16 07:50:28 +0900 2860) 	if (p) {
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 2861) 		inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 2862) 		inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
e2761a1167633 (OGAWA Hirofumi          2008-10-16 07:50:28 +0900 2863) 		return p;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2864) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2865) 
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 2866) 	inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 2867) 	inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2868) 	return NULL;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2869) }
4d359507346a1 (Al Viro                 2014-03-14 12:20:17 -0400 2870) EXPORT_SYMBOL(lock_rename);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2871) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2872) void unlock_rename(struct dentry *p1, struct dentry *p2)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2873) {
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 2874) 	inode_unlock(p1->d_inode);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2875) 	if (p1 != p2) {
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 2876) 		inode_unlock(p2->d_inode);
fc64005c93090 (Al Viro                 2016-04-10 01:33:30 -0400 2877) 		mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2878) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2879) }
4d359507346a1 (Al Viro                 2014-03-14 12:20:17 -0400 2880) EXPORT_SYMBOL(unlock_rename);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2881) 
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 2882) /**
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 2883)  * vfs_create - create new file
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 2884)  * @mnt_userns:	user namespace of the mount the inode was found from
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 2885)  * @dir:	inode of @dentry
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 2886)  * @dentry:	pointer to dentry of the base directory
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 2887)  * @mode:	mode of the new file
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 2888)  * @want_excl:	whether the file must not yet exist
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 2889)  *
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 2890)  * Create a new file.
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 2891)  *
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 2892)  * If the inode has been found through an idmapped mount the user namespace of
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 2893)  * the vfsmount must be passed through @mnt_userns. This function will then take
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 2894)  * care to map the inode according to @mnt_userns before checking permissions.
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 2895)  * On non-idmapped mounts or if permission checking is to be performed on the
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 2896)  * raw inode simply passs init_user_ns.
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 2897)  */
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 2898) int vfs_create(struct user_namespace *mnt_userns, struct inode *dir,
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 2899) 	       struct dentry *dentry, umode_t mode, bool want_excl)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2900) {
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 2901) 	int error = may_create(mnt_userns, dir, dentry);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2902) 	if (error)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2903) 		return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2904) 
acfa4380efe77 (Al Viro                 2008-12-04 10:06:33 -0500 2905) 	if (!dir->i_op->create)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2906) 		return -EACCES;	/* shouldn't it be ENOSYS? */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2907) 	mode &= S_IALLUGO;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2908) 	mode |= S_IFREG;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2909) 	error = security_inode_create(dir, dentry, mode);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2910) 	if (error)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2911) 		return error;
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 2912) 	error = dir->i_op->create(mnt_userns, dir, dentry, mode, want_excl);
a74574aafea3a (Stephen Smalley         2005-09-09 13:01:44 -0700 2913) 	if (!error)
f38aa94224c55 (Amy Griffis             2005-11-03 15:57:06 +0000 2914) 		fsnotify_create(dir, dentry);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2915) 	return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2916) }
4d359507346a1 (Al Viro                 2014-03-14 12:20:17 -0400 2917) EXPORT_SYMBOL(vfs_create);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2918) 
8e6c848eceaa3 (Al Viro                 2017-12-01 17:12:45 -0500 2919) int vfs_mkobj(struct dentry *dentry, umode_t mode,
8e6c848eceaa3 (Al Viro                 2017-12-01 17:12:45 -0500 2920) 		int (*f)(struct dentry *, umode_t, void *),
8e6c848eceaa3 (Al Viro                 2017-12-01 17:12:45 -0500 2921) 		void *arg)
8e6c848eceaa3 (Al Viro                 2017-12-01 17:12:45 -0500 2922) {
8e6c848eceaa3 (Al Viro                 2017-12-01 17:12:45 -0500 2923) 	struct inode *dir = dentry->d_parent->d_inode;
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 2924) 	int error = may_create(&init_user_ns, dir, dentry);
8e6c848eceaa3 (Al Viro                 2017-12-01 17:12:45 -0500 2925) 	if (error)
8e6c848eceaa3 (Al Viro                 2017-12-01 17:12:45 -0500 2926) 		return error;
8e6c848eceaa3 (Al Viro                 2017-12-01 17:12:45 -0500 2927) 
8e6c848eceaa3 (Al Viro                 2017-12-01 17:12:45 -0500 2928) 	mode &= S_IALLUGO;
8e6c848eceaa3 (Al Viro                 2017-12-01 17:12:45 -0500 2929) 	mode |= S_IFREG;
8e6c848eceaa3 (Al Viro                 2017-12-01 17:12:45 -0500 2930) 	error = security_inode_create(dir, dentry, mode);
8e6c848eceaa3 (Al Viro                 2017-12-01 17:12:45 -0500 2931) 	if (error)
8e6c848eceaa3 (Al Viro                 2017-12-01 17:12:45 -0500 2932) 		return error;
8e6c848eceaa3 (Al Viro                 2017-12-01 17:12:45 -0500 2933) 	error = f(dentry, mode, arg);
8e6c848eceaa3 (Al Viro                 2017-12-01 17:12:45 -0500 2934) 	if (!error)
8e6c848eceaa3 (Al Viro                 2017-12-01 17:12:45 -0500 2935) 		fsnotify_create(dir, dentry);
8e6c848eceaa3 (Al Viro                 2017-12-01 17:12:45 -0500 2936) 	return error;
8e6c848eceaa3 (Al Viro                 2017-12-01 17:12:45 -0500 2937) }
8e6c848eceaa3 (Al Viro                 2017-12-01 17:12:45 -0500 2938) EXPORT_SYMBOL(vfs_mkobj);
8e6c848eceaa3 (Al Viro                 2017-12-01 17:12:45 -0500 2939) 
a2982cc922c30 (Eric W. Biederman       2016-06-09 15:34:02 -0500 2940) bool may_open_dev(const struct path *path)
a2982cc922c30 (Eric W. Biederman       2016-06-09 15:34:02 -0500 2941) {
a2982cc922c30 (Eric W. Biederman       2016-06-09 15:34:02 -0500 2942) 	return !(path->mnt->mnt_flags & MNT_NODEV) &&
a2982cc922c30 (Eric W. Biederman       2016-06-09 15:34:02 -0500 2943) 		!(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
a2982cc922c30 (Eric W. Biederman       2016-06-09 15:34:02 -0500 2944) }
a2982cc922c30 (Eric W. Biederman       2016-06-09 15:34:02 -0500 2945) 
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 2946) static int may_open(struct user_namespace *mnt_userns, const struct path *path,
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 2947) 		    int acc_mode, int flag)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2948) {
3fb64190aa3c2 (Christoph Hellwig       2008-10-24 09:58:10 +0200 2949) 	struct dentry *dentry = path->dentry;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2950) 	struct inode *inode = dentry->d_inode;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2951) 	int error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2952) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2953) 	if (!inode)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2954) 		return -ENOENT;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2955) 
c8fe8f30c7fe6 (Christoph Hellwig       2009-01-05 19:27:23 +0100 2956) 	switch (inode->i_mode & S_IFMT) {
c8fe8f30c7fe6 (Christoph Hellwig       2009-01-05 19:27:23 +0100 2957) 	case S_IFLNK:
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2958) 		return -ELOOP;
c8fe8f30c7fe6 (Christoph Hellwig       2009-01-05 19:27:23 +0100 2959) 	case S_IFDIR:
fc4177be963dc (Kees Cook               2020-08-14 17:30:14 -0700 2960) 		if (acc_mode & MAY_WRITE)
c8fe8f30c7fe6 (Christoph Hellwig       2009-01-05 19:27:23 +0100 2961) 			return -EISDIR;
fc4177be963dc (Kees Cook               2020-08-14 17:30:14 -0700 2962) 		if (acc_mode & MAY_EXEC)
fc4177be963dc (Kees Cook               2020-08-14 17:30:14 -0700 2963) 			return -EACCES;
c8fe8f30c7fe6 (Christoph Hellwig       2009-01-05 19:27:23 +0100 2964) 		break;
c8fe8f30c7fe6 (Christoph Hellwig       2009-01-05 19:27:23 +0100 2965) 	case S_IFBLK:
c8fe8f30c7fe6 (Christoph Hellwig       2009-01-05 19:27:23 +0100 2966) 	case S_IFCHR:
a2982cc922c30 (Eric W. Biederman       2016-06-09 15:34:02 -0500 2967) 		if (!may_open_dev(path))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2968) 			return -EACCES;
633fb6ac39801 (Kees Cook               2020-08-11 18:36:26 -0700 2969) 		fallthrough;
c8fe8f30c7fe6 (Christoph Hellwig       2009-01-05 19:27:23 +0100 2970) 	case S_IFIFO:
c8fe8f30c7fe6 (Christoph Hellwig       2009-01-05 19:27:23 +0100 2971) 	case S_IFSOCK:
633fb6ac39801 (Kees Cook               2020-08-11 18:36:26 -0700 2972) 		if (acc_mode & MAY_EXEC)
633fb6ac39801 (Kees Cook               2020-08-11 18:36:26 -0700 2973) 			return -EACCES;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2974) 		flag &= ~O_TRUNC;
c8fe8f30c7fe6 (Christoph Hellwig       2009-01-05 19:27:23 +0100 2975) 		break;
0fd338b2d2cdf (Kees Cook               2020-08-11 18:36:30 -0700 2976) 	case S_IFREG:
0fd338b2d2cdf (Kees Cook               2020-08-11 18:36:30 -0700 2977) 		if ((acc_mode & MAY_EXEC) && path_noexec(path))
0fd338b2d2cdf (Kees Cook               2020-08-11 18:36:30 -0700 2978) 			return -EACCES;
0fd338b2d2cdf (Kees Cook               2020-08-11 18:36:30 -0700 2979) 		break;
4a3fd211ccfc0 (Dave Hansen             2008-02-15 14:37:48 -0800 2980) 	}
b41572e929221 (Dave Hansen             2007-10-16 23:31:14 -0700 2981) 
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 2982) 	error = inode_permission(mnt_userns, inode, MAY_OPEN | acc_mode);
b41572e929221 (Dave Hansen             2007-10-16 23:31:14 -0700 2983) 	if (error)
b41572e929221 (Dave Hansen             2007-10-16 23:31:14 -0700 2984) 		return error;
6146f0d5e47ca (Mimi Zohar              2009-02-04 09:06:57 -0500 2985) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2986) 	/*
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2987) 	 * An append-only file must be opened in append mode for writing.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2988) 	 */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2989) 	if (IS_APPEND(inode)) {
8737c9305bd56 (Al Viro                 2009-12-24 06:47:55 -0500 2990) 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
7715b521222b6 (Al Viro                 2009-12-16 03:54:00 -0500 2991) 			return -EPERM;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2992) 		if (flag & O_TRUNC)
7715b521222b6 (Al Viro                 2009-12-16 03:54:00 -0500 2993) 			return -EPERM;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2994) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2995) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2996) 	/* O_NOATIME can only be set by the owner or superuser */
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 2997) 	if (flag & O_NOATIME && !inode_owner_or_capable(mnt_userns, inode))
7715b521222b6 (Al Viro                 2009-12-16 03:54:00 -0500 2998) 		return -EPERM;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2999) 
f3c7691e8d30d (J. Bruce Fields         2011-09-21 10:58:13 -0400 3000) 	return 0;
7715b521222b6 (Al Viro                 2009-12-16 03:54:00 -0500 3001) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3002) 
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 3003) static int handle_truncate(struct user_namespace *mnt_userns, struct file *filp)
7715b521222b6 (Al Viro                 2009-12-16 03:54:00 -0500 3004) {
f0bb5aaf2c512 (Al Viro                 2016-11-20 20:27:12 -0500 3005) 	const struct path *path = &filp->f_path;
7715b521222b6 (Al Viro                 2009-12-16 03:54:00 -0500 3006) 	struct inode *inode = path->dentry->d_inode;
7715b521222b6 (Al Viro                 2009-12-16 03:54:00 -0500 3007) 	int error = get_write_access(inode);
7715b521222b6 (Al Viro                 2009-12-16 03:54:00 -0500 3008) 	if (error)
7715b521222b6 (Al Viro                 2009-12-16 03:54:00 -0500 3009) 		return error;
7715b521222b6 (Al Viro                 2009-12-16 03:54:00 -0500 3010) 	/*
7715b521222b6 (Al Viro                 2009-12-16 03:54:00 -0500 3011) 	 * Refuse to truncate files with mandatory locks held on them.
7715b521222b6 (Al Viro                 2009-12-16 03:54:00 -0500 3012) 	 */
d7a06983a01a3 (Jeff Layton             2014-03-10 09:54:15 -0400 3013) 	error = locks_verify_locked(filp);
7715b521222b6 (Al Viro                 2009-12-16 03:54:00 -0500 3014) 	if (!error)
ea0d3ab239fba (Tetsuo Handa            2010-06-02 13:24:43 +0900 3015) 		error = security_path_truncate(path);
7715b521222b6 (Al Viro                 2009-12-16 03:54:00 -0500 3016) 	if (!error) {
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 3017) 		error = do_truncate(mnt_userns, path->dentry, 0,
7715b521222b6 (Al Viro                 2009-12-16 03:54:00 -0500 3018) 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
e1181ee6575d7 (Jeff Layton             2010-12-07 16:19:50 -0500 3019) 				    filp);
7715b521222b6 (Al Viro                 2009-12-16 03:54:00 -0500 3020) 	}
7715b521222b6 (Al Viro                 2009-12-16 03:54:00 -0500 3021) 	put_write_access(inode);
acd0c93517864 (Mimi Zohar              2009-09-04 13:08:46 -0400 3022) 	return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3023) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3024) 
d57999e1527f0 (Dave Hansen             2008-02-15 14:37:27 -0800 3025) static inline int open_to_namei_flags(int flag)
d57999e1527f0 (Dave Hansen             2008-02-15 14:37:27 -0800 3026) {
8a5e929dd2e05 (Al Viro                 2011-06-25 19:15:54 -0400 3027) 	if ((flag & O_ACCMODE) == 3)
8a5e929dd2e05 (Al Viro                 2011-06-25 19:15:54 -0400 3028) 		flag--;
d57999e1527f0 (Dave Hansen             2008-02-15 14:37:27 -0800 3029) 	return flag;
d57999e1527f0 (Dave Hansen             2008-02-15 14:37:27 -0800 3030) }
d57999e1527f0 (Dave Hansen             2008-02-15 14:37:27 -0800 3031) 
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 3032) static int may_o_create(struct user_namespace *mnt_userns,
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 3033) 			const struct path *dir, struct dentry *dentry,
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 3034) 			umode_t mode)
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3035) {
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3036) 	int error = security_path_mknod(dir, dentry, mode, 0);
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3037) 	if (error)
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3038) 		return error;
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3039) 
8e5389132ab42 (Christian Brauner       2021-03-20 13:26:23 +0100 3040) 	if (!fsuidgid_has_mapping(dir->dentry->d_sb, mnt_userns))
1328c727004d4 (Seth Forshee            2017-01-26 14:33:46 -0600 3041) 		return -EOVERFLOW;
1328c727004d4 (Seth Forshee            2017-01-26 14:33:46 -0600 3042) 
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 3043) 	error = inode_permission(mnt_userns, dir->dentry->d_inode,
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100 3044) 				 MAY_WRITE | MAY_EXEC);
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3045) 	if (error)
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3046) 		return error;
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3047) 
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3048) 	return security_inode_create(dir->dentry->d_inode, dentry, mode);
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3049) }
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3050) 
1acf0af9b9810 (David Howells           2012-06-14 16:13:46 +0100 3051) /*
1acf0af9b9810 (David Howells           2012-06-14 16:13:46 +0100 3052)  * Attempt to atomically look up, create and open a file from a negative
1acf0af9b9810 (David Howells           2012-06-14 16:13:46 +0100 3053)  * dentry.
1acf0af9b9810 (David Howells           2012-06-14 16:13:46 +0100 3054)  *
1acf0af9b9810 (David Howells           2012-06-14 16:13:46 +0100 3055)  * Returns 0 if successful.  The file will have been created and attached to
1acf0af9b9810 (David Howells           2012-06-14 16:13:46 +0100 3056)  * @file by the filesystem calling finish_open().
1acf0af9b9810 (David Howells           2012-06-14 16:13:46 +0100 3057)  *
00a07c1591a3f (Al Viro                 2018-07-09 19:30:20 -0400 3058)  * If the file was looked up only or didn't need creating, FMODE_OPENED won't
00a07c1591a3f (Al Viro                 2018-07-09 19:30:20 -0400 3059)  * be set.  The caller will need to perform the open themselves.  @path will
00a07c1591a3f (Al Viro                 2018-07-09 19:30:20 -0400 3060)  * have been updated to point to the new dentry.  This may be negative.
1acf0af9b9810 (David Howells           2012-06-14 16:13:46 +0100 3061)  *
1acf0af9b9810 (David Howells           2012-06-14 16:13:46 +0100 3062)  * Returns an error code otherwise.
1acf0af9b9810 (David Howells           2012-06-14 16:13:46 +0100 3063)  */
239eb983383b1 (Al Viro                 2020-01-09 14:12:40 -0500 3064) static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry,
239eb983383b1 (Al Viro                 2020-01-09 14:12:40 -0500 3065) 				  struct file *file,
239eb983383b1 (Al Viro                 2020-01-09 14:12:40 -0500 3066) 				  int open_flag, umode_t mode)
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3067) {
384f26e28fe4d (Al Viro                 2016-04-28 02:03:55 -0400 3068) 	struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3069) 	struct inode *dir =  nd->path.dentry->d_inode;
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3070) 	int error;
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3071) 
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3072) 	if (nd->flags & LOOKUP_DIRECTORY)
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3073) 		open_flag |= O_DIRECTORY;
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3074) 
30d904947459c (Al Viro                 2012-06-22 12:40:19 +0400 3075) 	file->f_path.dentry = DENTRY_NOT_SET;
30d904947459c (Al Viro                 2012-06-22 12:40:19 +0400 3076) 	file->f_path.mnt = nd->path.mnt;
0fb1ea0933153 (Al Viro                 2016-04-27 14:13:10 -0400 3077) 	error = dir->i_op->atomic_open(dir, dentry, file,
44907d7900246 (Al Viro                 2018-06-08 13:32:02 -0400 3078) 				       open_to_namei_flags(open_flag), mode);
6fbd07146d992 (Al Viro                 2016-04-28 11:50:59 -0400 3079) 	d_lookup_done(dentry);
384f26e28fe4d (Al Viro                 2016-04-28 02:03:55 -0400 3080) 	if (!error) {
64e1ac4d46f9f (Al Viro                 2018-07-09 19:17:52 -0400 3081) 		if (file->f_mode & FMODE_OPENED) {
6fb968cdf9d0b (Al Viro                 2020-01-26 09:53:19 -0500 3082) 			if (unlikely(dentry != file->f_path.dentry)) {
6fb968cdf9d0b (Al Viro                 2020-01-26 09:53:19 -0500 3083) 				dput(dentry);
6fb968cdf9d0b (Al Viro                 2020-01-26 09:53:19 -0500 3084) 				dentry = dget(file->f_path.dentry);
6fb968cdf9d0b (Al Viro                 2020-01-26 09:53:19 -0500 3085) 			}
64e1ac4d46f9f (Al Viro                 2018-07-09 19:17:52 -0400 3086) 		} else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
2675a4eb6a9f1 (Al Viro                 2012-06-22 12:41:10 +0400 3087) 			error = -EIO;
03da633aa7b08 (Al Viro                 2013-09-16 19:22:33 -0400 3088) 		} else {
384f26e28fe4d (Al Viro                 2016-04-28 02:03:55 -0400 3089) 			if (file->f_path.dentry) {
384f26e28fe4d (Al Viro                 2016-04-28 02:03:55 -0400 3090) 				dput(dentry);
384f26e28fe4d (Al Viro                 2016-04-28 02:03:55 -0400 3091) 				dentry = file->f_path.dentry;
03da633aa7b08 (Al Viro                 2013-09-16 19:22:33 -0400 3092) 			}
239eb983383b1 (Al Viro                 2020-01-09 14:12:40 -0500 3093) 			if (unlikely(d_is_negative(dentry)))
a01e718f7241c (Al Viro                 2016-06-07 21:53:51 -0400 3094) 				error = -ENOENT;
62b2ce964bb90 (Sage Weil               2012-08-15 13:30:12 -0700 3095) 		}
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3096) 	}
239eb983383b1 (Al Viro                 2020-01-09 14:12:40 -0500 3097) 	if (error) {
239eb983383b1 (Al Viro                 2020-01-09 14:12:40 -0500 3098) 		dput(dentry);
239eb983383b1 (Al Viro                 2020-01-09 14:12:40 -0500 3099) 		dentry = ERR_PTR(error);
239eb983383b1 (Al Viro                 2020-01-09 14:12:40 -0500 3100) 	}
239eb983383b1 (Al Viro                 2020-01-09 14:12:40 -0500 3101) 	return dentry;
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3102) }
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3103) 
d58ffd35c1e59 (Miklos Szeredi          2012-06-05 15:10:15 +0200 3104) /*
1acf0af9b9810 (David Howells           2012-06-14 16:13:46 +0100 3105)  * Look up and maybe create and open the last component.
d58ffd35c1e59 (Miklos Szeredi          2012-06-05 15:10:15 +0200 3106)  *
00a07c1591a3f (Al Viro                 2018-07-09 19:30:20 -0400 3107)  * Must be called with parent locked (exclusive in O_CREAT case).
1acf0af9b9810 (David Howells           2012-06-14 16:13:46 +0100 3108)  *
00a07c1591a3f (Al Viro                 2018-07-09 19:30:20 -0400 3109)  * Returns 0 on success, that is, if
00a07c1591a3f (Al Viro                 2018-07-09 19:30:20 -0400 3110)  *  the file was successfully atomically created (if necessary) and opened, or
00a07c1591a3f (Al Viro                 2018-07-09 19:30:20 -0400 3111)  *  the file was not completely opened at this time, though lookups and
00a07c1591a3f (Al Viro                 2018-07-09 19:30:20 -0400 3112)  *  creations were performed.
00a07c1591a3f (Al Viro                 2018-07-09 19:30:20 -0400 3113)  * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
00a07c1591a3f (Al Viro                 2018-07-09 19:30:20 -0400 3114)  * In the latter case dentry returned in @path might be negative if O_CREAT
00a07c1591a3f (Al Viro                 2018-07-09 19:30:20 -0400 3115)  * hadn't been specified.
1acf0af9b9810 (David Howells           2012-06-14 16:13:46 +0100 3116)  *
00a07c1591a3f (Al Viro                 2018-07-09 19:30:20 -0400 3117)  * An error code is returned on failure.
d58ffd35c1e59 (Miklos Szeredi          2012-06-05 15:10:15 +0200 3118)  */
da5ebf5aa676c (Al Viro                 2020-01-09 14:25:14 -0500 3119) static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
da5ebf5aa676c (Al Viro                 2020-01-09 14:25:14 -0500 3120) 				  const struct open_flags *op,
da5ebf5aa676c (Al Viro                 2020-01-09 14:25:14 -0500 3121) 				  bool got_write)
d58ffd35c1e59 (Miklos Szeredi          2012-06-05 15:10:15 +0200 3122) {
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 3123) 	struct user_namespace *mnt_userns;
d58ffd35c1e59 (Miklos Szeredi          2012-06-05 15:10:15 +0200 3124) 	struct dentry *dir = nd->path.dentry;
54ef487241e86 (Miklos Szeredi          2012-06-05 15:10:16 +0200 3125) 	struct inode *dir_inode = dir->d_inode;
1643b43fbd052 (Al Viro                 2016-04-27 19:14:10 -0400 3126) 	int open_flag = op->open_flag;
d58ffd35c1e59 (Miklos Szeredi          2012-06-05 15:10:15 +0200 3127) 	struct dentry *dentry;
1643b43fbd052 (Al Viro                 2016-04-27 19:14:10 -0400 3128) 	int error, create_error = 0;
1643b43fbd052 (Al Viro                 2016-04-27 19:14:10 -0400 3129) 	umode_t mode = op->mode;
6fbd07146d992 (Al Viro                 2016-04-28 11:50:59 -0400 3130) 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
d58ffd35c1e59 (Miklos Szeredi          2012-06-05 15:10:15 +0200 3131) 
ce8644fcadc52 (Al Viro                 2016-04-26 14:17:56 -0400 3132) 	if (unlikely(IS_DEADDIR(dir_inode)))
da5ebf5aa676c (Al Viro                 2020-01-09 14:25:14 -0500 3133) 		return ERR_PTR(-ENOENT);
d58ffd35c1e59 (Miklos Szeredi          2012-06-05 15:10:15 +0200 3134) 
73a09dd94377e (Al Viro                 2018-06-08 13:22:02 -0400 3135) 	file->f_mode &= ~FMODE_CREATED;
6fbd07146d992 (Al Viro                 2016-04-28 11:50:59 -0400 3136) 	dentry = d_lookup(dir, &nd->last);
6fbd07146d992 (Al Viro                 2016-04-28 11:50:59 -0400 3137) 	for (;;) {
6fbd07146d992 (Al Viro                 2016-04-28 11:50:59 -0400 3138) 		if (!dentry) {
6fbd07146d992 (Al Viro                 2016-04-28 11:50:59 -0400 3139) 			dentry = d_alloc_parallel(dir, &nd->last, &wq);
6fbd07146d992 (Al Viro                 2016-04-28 11:50:59 -0400 3140) 			if (IS_ERR(dentry))
da5ebf5aa676c (Al Viro                 2020-01-09 14:25:14 -0500 3141) 				return dentry;
6fbd07146d992 (Al Viro                 2016-04-28 11:50:59 -0400 3142) 		}
6fbd07146d992 (Al Viro                 2016-04-28 11:50:59 -0400 3143) 		if (d_in_lookup(dentry))
6fbd07146d992 (Al Viro                 2016-04-28 11:50:59 -0400 3144) 			break;
d58ffd35c1e59 (Miklos Szeredi          2012-06-05 15:10:15 +0200 3145) 
6fbd07146d992 (Al Viro                 2016-04-28 11:50:59 -0400 3146) 		error = d_revalidate(dentry, nd->flags);
6fbd07146d992 (Al Viro                 2016-04-28 11:50:59 -0400 3147) 		if (likely(error > 0))
6fbd07146d992 (Al Viro                 2016-04-28 11:50:59 -0400 3148) 			break;
6fbd07146d992 (Al Viro                 2016-04-28 11:50:59 -0400 3149) 		if (error)
6fbd07146d992 (Al Viro                 2016-04-28 11:50:59 -0400 3150) 			goto out_dput;
6fbd07146d992 (Al Viro                 2016-04-28 11:50:59 -0400 3151) 		d_invalidate(dentry);
6fbd07146d992 (Al Viro                 2016-04-28 11:50:59 -0400 3152) 		dput(dentry);
6fbd07146d992 (Al Viro                 2016-04-28 11:50:59 -0400 3153) 		dentry = NULL;
6fbd07146d992 (Al Viro                 2016-04-28 11:50:59 -0400 3154) 	}
6fbd07146d992 (Al Viro                 2016-04-28 11:50:59 -0400 3155) 	if (dentry->d_inode) {
6c51e513a3aa4 (Al Viro                 2016-03-05 20:09:32 -0500 3156) 		/* Cached positive dentry: will open in f_op->open */
da5ebf5aa676c (Al Viro                 2020-01-09 14:25:14 -0500 3157) 		return dentry;
6c51e513a3aa4 (Al Viro                 2016-03-05 20:09:32 -0500 3158) 	}
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3159) 
1643b43fbd052 (Al Viro                 2016-04-27 19:14:10 -0400 3160) 	/*
1643b43fbd052 (Al Viro                 2016-04-27 19:14:10 -0400 3161) 	 * Checking write permission is tricky, bacuse we don't know if we are
1643b43fbd052 (Al Viro                 2016-04-27 19:14:10 -0400 3162) 	 * going to actually need it: O_CREAT opens should work as long as the
1643b43fbd052 (Al Viro                 2016-04-27 19:14:10 -0400 3163) 	 * file exists.  But checking existence breaks atomicity.  The trick is
1643b43fbd052 (Al Viro                 2016-04-27 19:14:10 -0400 3164) 	 * to check access and if not granted clear O_CREAT from the flags.
1643b43fbd052 (Al Viro                 2016-04-27 19:14:10 -0400 3165) 	 *
1643b43fbd052 (Al Viro                 2016-04-27 19:14:10 -0400 3166) 	 * Another problem is returing the "right" error value (e.g. for an
1643b43fbd052 (Al Viro                 2016-04-27 19:14:10 -0400 3167) 	 * O_EXCL open we want to return EEXIST not EROFS).
1643b43fbd052 (Al Viro                 2016-04-27 19:14:10 -0400 3168) 	 */
99a4a90c8e933 (Al Viro                 2020-03-12 14:07:27 -0400 3169) 	if (unlikely(!got_write))
99a4a90c8e933 (Al Viro                 2020-03-12 14:07:27 -0400 3170) 		open_flag &= ~O_TRUNC;
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 3171) 	mnt_userns = mnt_user_ns(nd->path.mnt);
1643b43fbd052 (Al Viro                 2016-04-27 19:14:10 -0400 3172) 	if (open_flag & O_CREAT) {
99a4a90c8e933 (Al Viro                 2020-03-12 14:07:27 -0400 3173) 		if (open_flag & O_EXCL)
99a4a90c8e933 (Al Viro                 2020-03-12 14:07:27 -0400 3174) 			open_flag &= ~O_TRUNC;
1643b43fbd052 (Al Viro                 2016-04-27 19:14:10 -0400 3175) 		if (!IS_POSIXACL(dir->d_inode))
1643b43fbd052 (Al Viro                 2016-04-27 19:14:10 -0400 3176) 			mode &= ~current_umask();
99a4a90c8e933 (Al Viro                 2020-03-12 14:07:27 -0400 3177) 		if (likely(got_write))
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 3178) 			create_error = may_o_create(mnt_userns, &nd->path,
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 3179) 						    dentry, mode);
99a4a90c8e933 (Al Viro                 2020-03-12 14:07:27 -0400 3180) 		else
99a4a90c8e933 (Al Viro                 2020-03-12 14:07:27 -0400 3181) 			create_error = -EROFS;
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3182) 	}
99a4a90c8e933 (Al Viro                 2020-03-12 14:07:27 -0400 3183) 	if (create_error)
99a4a90c8e933 (Al Viro                 2020-03-12 14:07:27 -0400 3184) 		open_flag &= ~O_CREAT;
6ac087099edf0 (Al Viro                 2016-04-26 00:02:50 -0400 3185) 	if (dir_inode->i_op->atomic_open) {
d489cf9a3e88a (Al Viro                 2020-03-11 08:07:53 -0400 3186) 		dentry = atomic_open(nd, dentry, file, open_flag, mode);
da5ebf5aa676c (Al Viro                 2020-01-09 14:25:14 -0500 3187) 		if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT))
da5ebf5aa676c (Al Viro                 2020-01-09 14:25:14 -0500 3188) 			dentry = ERR_PTR(create_error);
da5ebf5aa676c (Al Viro                 2020-01-09 14:25:14 -0500 3189) 		return dentry;
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3190) 	}
54ef487241e86 (Miklos Szeredi          2012-06-05 15:10:16 +0200 3191) 
6fbd07146d992 (Al Viro                 2016-04-28 11:50:59 -0400 3192) 	if (d_in_lookup(dentry)) {
12fa5e2404192 (Al Viro                 2016-04-28 11:19:43 -0400 3193) 		struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
12fa5e2404192 (Al Viro                 2016-04-28 11:19:43 -0400 3194) 							     nd->flags);
6fbd07146d992 (Al Viro                 2016-04-28 11:50:59 -0400 3195) 		d_lookup_done(dentry);
12fa5e2404192 (Al Viro                 2016-04-28 11:19:43 -0400 3196) 		if (unlikely(res)) {
12fa5e2404192 (Al Viro                 2016-04-28 11:19:43 -0400 3197) 			if (IS_ERR(res)) {
12fa5e2404192 (Al Viro                 2016-04-28 11:19:43 -0400 3198) 				error = PTR_ERR(res);
12fa5e2404192 (Al Viro                 2016-04-28 11:19:43 -0400 3199) 				goto out_dput;
12fa5e2404192 (Al Viro                 2016-04-28 11:19:43 -0400 3200) 			}
12fa5e2404192 (Al Viro                 2016-04-28 11:19:43 -0400 3201) 			dput(dentry);
12fa5e2404192 (Al Viro                 2016-04-28 11:19:43 -0400 3202) 			dentry = res;
12fa5e2404192 (Al Viro                 2016-04-28 11:19:43 -0400 3203) 		}
54ef487241e86 (Miklos Szeredi          2012-06-05 15:10:16 +0200 3204) 	}
54ef487241e86 (Miklos Szeredi          2012-06-05 15:10:16 +0200 3205) 
d58ffd35c1e59 (Miklos Szeredi          2012-06-05 15:10:15 +0200 3206) 	/* Negative dentry, just create the file */
1643b43fbd052 (Al Viro                 2016-04-27 19:14:10 -0400 3207) 	if (!dentry->d_inode && (open_flag & O_CREAT)) {
73a09dd94377e (Al Viro                 2018-06-08 13:22:02 -0400 3208) 		file->f_mode |= FMODE_CREATED;
ce8644fcadc52 (Al Viro                 2016-04-26 14:17:56 -0400 3209) 		audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
ce8644fcadc52 (Al Viro                 2016-04-26 14:17:56 -0400 3210) 		if (!dir_inode->i_op->create) {
ce8644fcadc52 (Al Viro                 2016-04-26 14:17:56 -0400 3211) 			error = -EACCES;
d58ffd35c1e59 (Miklos Szeredi          2012-06-05 15:10:15 +0200 3212) 			goto out_dput;
ce8644fcadc52 (Al Viro                 2016-04-26 14:17:56 -0400 3213) 		}
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 3214) 
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 3215) 		error = dir_inode->i_op->create(mnt_userns, dir_inode, dentry,
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 3216) 						mode, open_flag & O_EXCL);
d58ffd35c1e59 (Miklos Szeredi          2012-06-05 15:10:15 +0200 3217) 		if (error)
d58ffd35c1e59 (Miklos Szeredi          2012-06-05 15:10:15 +0200 3218) 			goto out_dput;
d58ffd35c1e59 (Miklos Szeredi          2012-06-05 15:10:15 +0200 3219) 	}
1643b43fbd052 (Al Viro                 2016-04-27 19:14:10 -0400 3220) 	if (unlikely(create_error) && !dentry->d_inode) {
1643b43fbd052 (Al Viro                 2016-04-27 19:14:10 -0400 3221) 		error = create_error;
1643b43fbd052 (Al Viro                 2016-04-27 19:14:10 -0400 3222) 		goto out_dput;
d58ffd35c1e59 (Miklos Szeredi          2012-06-05 15:10:15 +0200 3223) 	}
da5ebf5aa676c (Al Viro                 2020-01-09 14:25:14 -0500 3224) 	return dentry;
d58ffd35c1e59 (Miklos Szeredi          2012-06-05 15:10:15 +0200 3225) 
d58ffd35c1e59 (Miklos Szeredi          2012-06-05 15:10:15 +0200 3226) out_dput:
d58ffd35c1e59 (Miklos Szeredi          2012-06-05 15:10:15 +0200 3227) 	dput(dentry);
da5ebf5aa676c (Al Viro                 2020-01-09 14:25:14 -0500 3228) 	return ERR_PTR(error);
d58ffd35c1e59 (Miklos Szeredi          2012-06-05 15:10:15 +0200 3229) }
d58ffd35c1e59 (Miklos Szeredi          2012-06-05 15:10:15 +0200 3230) 
c981a4828125e (Al Viro                 2020-01-26 11:06:21 -0500 3231) static const char *open_last_lookups(struct nameidata *nd,
3ec2eef116e90 (Al Viro                 2018-06-08 13:43:47 -0400 3232) 		   struct file *file, const struct open_flags *op)
fb1cc555d5338 (Al Viro                 2009-12-24 01:58:28 -0500 3233) {
a1e28038df98e (Al Viro                 2009-12-24 02:12:06 -0500 3234) 	struct dentry *dir = nd->path.dentry;
ca344a894b41a (Al Viro                 2011-03-09 00:36:45 -0500 3235) 	int open_flag = op->open_flag;
64894cf843278 (Al Viro                 2012-07-31 00:53:35 +0400 3236) 	bool got_write = false;
254cf582127b2 (Al Viro                 2015-05-05 09:40:46 -0400 3237) 	unsigned seq;
a1eb331530905 (Miklos Szeredi          2012-05-21 17:30:07 +0200 3238) 	struct inode *inode;
da5ebf5aa676c (Al Viro                 2020-01-09 14:25:14 -0500 3239) 	struct dentry *dentry;
b0417d2c72989 (Al Viro                 2020-01-14 13:34:20 -0500 3240) 	const char *res;
1f36f774b22a0 (Al Viro                 2009-12-26 10:56:19 -0500 3241) 
c3e380b0b3cfa (Al Viro                 2011-02-23 13:39:45 -0500 3242) 	nd->flags |= op->intent;
c3e380b0b3cfa (Al Viro                 2011-02-23 13:39:45 -0500 3243) 
bc77daa783afc (Al Viro                 2013-06-06 09:12:33 -0400 3244) 	if (nd->last_type != LAST_NORM) {
56676ec390194 (Al Viro                 2020-03-10 21:54:54 -0400 3245) 		if (nd->depth)
56676ec390194 (Al Viro                 2020-03-10 21:54:54 -0400 3246) 			put_link(nd);
ff326a3299660 (Al Viro                 2020-03-10 10:19:24 -0400 3247) 		return handle_dots(nd, nd->last_type);
1f36f774b22a0 (Al Viro                 2009-12-26 10:56:19 -0500 3248) 	}
67ee3ad21d0d0 (Al Viro                 2009-12-26 07:01:01 -0500 3249) 
ca344a894b41a (Al Viro                 2011-03-09 00:36:45 -0500 3250) 	if (!(open_flag & O_CREAT)) {
fe2d35ff0d18a (Al Viro                 2011-03-05 22:58:25 -0500 3251) 		if (nd->last.name[nd->last.len])
fe2d35ff0d18a (Al Viro                 2011-03-05 22:58:25 -0500 3252) 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
fe2d35ff0d18a (Al Viro                 2011-03-05 22:58:25 -0500 3253) 		/* we _can_ be in RCU mode here */
20e343571cefe (Al Viro                 2020-01-09 14:58:31 -0500 3254) 		dentry = lookup_fast(nd, &inode, &seq);
20e343571cefe (Al Viro                 2020-01-09 14:58:31 -0500 3255) 		if (IS_ERR(dentry))
1ccac622f9da5 (Al Viro                 2020-01-14 10:13:40 -0500 3256) 			return ERR_CAST(dentry);
20e343571cefe (Al Viro                 2020-01-09 14:58:31 -0500 3257) 		if (likely(dentry))
7157486541bff (Miklos Szeredi          2012-06-05 15:10:14 +0200 3258) 			goto finish_lookup;
7157486541bff (Miklos Szeredi          2012-06-05 15:10:14 +0200 3259) 
6583fe22d1bce (Al Viro                 2016-03-05 18:14:03 -0500 3260) 		BUG_ON(nd->flags & LOOKUP_RCU);
b6183df7b2949 (Miklos Szeredi          2012-06-05 15:10:13 +0200 3261) 	} else {
b6183df7b2949 (Miklos Szeredi          2012-06-05 15:10:13 +0200 3262) 		/* create side of things */
72287417abd16 (Al Viro                 2020-03-10 10:09:26 -0400 3263) 		if (nd->flags & LOOKUP_RCU) {
e36cffed20a32 (Jens Axboe              2020-12-17 09:19:08 -0700 3264) 			if (!try_to_unlazy(nd))
e36cffed20a32 (Jens Axboe              2020-12-17 09:19:08 -0700 3265) 				return ERR_PTR(-ECHILD);
72287417abd16 (Al Viro                 2020-03-10 10:09:26 -0400 3266) 		}
c9b07eab0c876 (Al Viro                 2019-07-14 13:22:27 -0400 3267) 		audit_inode(nd->name, dir, AUDIT_INODE_PARENT);
b6183df7b2949 (Miklos Szeredi          2012-06-05 15:10:13 +0200 3268) 		/* trailing slashes? */
deb106c632d73 (Al Viro                 2015-05-08 18:05:21 -0400 3269) 		if (unlikely(nd->last.name[nd->last.len]))
1ccac622f9da5 (Al Viro                 2020-01-14 10:13:40 -0500 3270) 			return ERR_PTR(-EISDIR);
b6183df7b2949 (Miklos Szeredi          2012-06-05 15:10:13 +0200 3271) 	}
a2c36b450ee68 (Al Viro                 2009-12-24 03:39:50 -0500 3272) 
9cf843e3f47c4 (Al Viro                 2016-04-28 19:35:16 -0400 3273) 	if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
e36cffed20a32 (Jens Axboe              2020-12-17 09:19:08 -0700 3274) 		got_write = !mnt_want_write(nd->path.mnt);
64894cf843278 (Al Viro                 2012-07-31 00:53:35 +0400 3275) 		/*
64894cf843278 (Al Viro                 2012-07-31 00:53:35 +0400 3276) 		 * do _not_ fail yet - we might not need that or fail with
64894cf843278 (Al Viro                 2012-07-31 00:53:35 +0400 3277) 		 * a different error; let lookup_open() decide; we'll be
64894cf843278 (Al Viro                 2012-07-31 00:53:35 +0400 3278) 		 * dropping this one anyway.
64894cf843278 (Al Viro                 2012-07-31 00:53:35 +0400 3279) 		 */
64894cf843278 (Al Viro                 2012-07-31 00:53:35 +0400 3280) 	}
9cf843e3f47c4 (Al Viro                 2016-04-28 19:35:16 -0400 3281) 	if (open_flag & O_CREAT)
9cf843e3f47c4 (Al Viro                 2016-04-28 19:35:16 -0400 3282) 		inode_lock(dir->d_inode);
9cf843e3f47c4 (Al Viro                 2016-04-28 19:35:16 -0400 3283) 	else
9cf843e3f47c4 (Al Viro                 2016-04-28 19:35:16 -0400 3284) 		inode_lock_shared(dir->d_inode);
da5ebf5aa676c (Al Viro                 2020-01-09 14:25:14 -0500 3285) 	dentry = lookup_open(nd, file, op, got_write);
f7bb959d9623d (Al Viro                 2020-03-05 13:25:20 -0500 3286) 	if (!IS_ERR(dentry) && (file->f_mode & FMODE_CREATED))
f7bb959d9623d (Al Viro                 2020-03-05 13:25:20 -0500 3287) 		fsnotify_create(dir->d_inode, dentry);
9cf843e3f47c4 (Al Viro                 2016-04-28 19:35:16 -0400 3288) 	if (open_flag & O_CREAT)
9cf843e3f47c4 (Al Viro                 2016-04-28 19:35:16 -0400 3289) 		inode_unlock(dir->d_inode);
9cf843e3f47c4 (Al Viro                 2016-04-28 19:35:16 -0400 3290) 	else
9cf843e3f47c4 (Al Viro                 2016-04-28 19:35:16 -0400 3291) 		inode_unlock_shared(dir->d_inode);
a1e28038df98e (Al Viro                 2009-12-24 02:12:06 -0500 3292) 
c981a4828125e (Al Viro                 2020-01-26 11:06:21 -0500 3293) 	if (got_write)
59e96e65833e0 (Al Viro                 2020-01-26 10:22:24 -0500 3294) 		mnt_drop_write(nd->path.mnt);
d18e9008c377d (Miklos Szeredi          2012-06-05 15:10:17 +0200 3295) 
59e96e65833e0 (Al Viro                 2020-01-26 10:22:24 -0500 3296) 	if (IS_ERR(dentry))
59e96e65833e0 (Al Viro                 2020-01-26 10:22:24 -0500 3297) 		return ERR_CAST(dentry);
59e96e65833e0 (Al Viro                 2020-01-26 10:22:24 -0500 3298) 
973d4b73fbaf7 (Al Viro                 2020-01-26 10:48:16 -0500 3299) 	if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) {
e73cabff59170 (Al Viro                 2020-01-09 14:30:08 -0500 3300) 		dput(nd->path.dentry);
e73cabff59170 (Al Viro                 2020-01-09 14:30:08 -0500 3301) 		nd->path.dentry = dentry;
c981a4828125e (Al Viro                 2020-01-26 11:06:21 -0500 3302) 		return NULL;
fb1cc555d5338 (Al Viro                 2009-12-24 01:58:28 -0500 3303) 	}
fb1cc555d5338 (Al Viro                 2009-12-24 01:58:28 -0500 3304) 
20e343571cefe (Al Viro                 2020-01-09 14:58:31 -0500 3305) finish_lookup:
56676ec390194 (Al Viro                 2020-03-10 21:54:54 -0400 3306) 	if (nd->depth)
56676ec390194 (Al Viro                 2020-03-10 21:54:54 -0400 3307) 		put_link(nd);
8c4efe22e7c4d (Al Viro                 2020-01-19 12:44:18 -0500 3308) 	res = step_into(nd, WALK_TRAILING, dentry, inode, seq);
ff326a3299660 (Al Viro                 2020-03-10 10:19:24 -0400 3309) 	if (unlikely(res))
b0417d2c72989 (Al Viro                 2020-01-14 13:34:20 -0500 3310) 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
ff326a3299660 (Al Viro                 2020-03-10 10:19:24 -0400 3311) 	return res;
c981a4828125e (Al Viro                 2020-01-26 11:06:21 -0500 3312) }
c981a4828125e (Al Viro                 2020-01-26 11:06:21 -0500 3313) 
c981a4828125e (Al Viro                 2020-01-26 11:06:21 -0500 3314) /*
c981a4828125e (Al Viro                 2020-01-26 11:06:21 -0500 3315)  * Handle the last step of open()
c981a4828125e (Al Viro                 2020-01-26 11:06:21 -0500 3316)  */
c5971b8c6354a (Al Viro                 2020-03-05 11:41:29 -0500 3317) static int do_open(struct nameidata *nd,
c981a4828125e (Al Viro                 2020-01-26 11:06:21 -0500 3318) 		   struct file *file, const struct open_flags *op)
c981a4828125e (Al Viro                 2020-01-26 11:06:21 -0500 3319) {
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 3320) 	struct user_namespace *mnt_userns;
c981a4828125e (Al Viro                 2020-01-26 11:06:21 -0500 3321) 	int open_flag = op->open_flag;
c981a4828125e (Al Viro                 2020-01-26 11:06:21 -0500 3322) 	bool do_truncate;
c981a4828125e (Al Viro                 2020-01-26 11:06:21 -0500 3323) 	int acc_mode;
c981a4828125e (Al Viro                 2020-01-26 11:06:21 -0500 3324) 	int error;
c981a4828125e (Al Viro                 2020-01-26 11:06:21 -0500 3325) 
ff326a3299660 (Al Viro                 2020-03-10 10:19:24 -0400 3326) 	if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) {
ff326a3299660 (Al Viro                 2020-03-10 10:19:24 -0400 3327) 		error = complete_walk(nd);
ff326a3299660 (Al Viro                 2020-03-10 10:19:24 -0400 3328) 		if (error)
ff326a3299660 (Al Viro                 2020-03-10 10:19:24 -0400 3329) 			return error;
ff326a3299660 (Al Viro                 2020-03-10 10:19:24 -0400 3330) 	}
973d4b73fbaf7 (Al Viro                 2020-01-26 10:48:16 -0500 3331) 	if (!(file->f_mode & FMODE_CREATED))
973d4b73fbaf7 (Al Viro                 2020-01-26 10:48:16 -0500 3332) 		audit_inode(nd->name, nd->path.dentry, 0);
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 3333) 	mnt_userns = mnt_user_ns(nd->path.mnt);
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 3334) 	if (open_flag & O_CREAT) {
b94e0b32c8999 (Al Viro                 2020-03-10 10:13:53 -0400 3335) 		if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED))
b94e0b32c8999 (Al Viro                 2020-03-10 10:13:53 -0400 3336) 			return -EEXIST;
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 3337) 		if (d_is_dir(nd->path.dentry))
c5971b8c6354a (Al Viro                 2020-03-05 11:41:29 -0500 3338) 			return -EISDIR;
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 3339) 		error = may_create_in_sticky(mnt_userns, nd,
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 3340) 					     d_backing_inode(nd->path.dentry));
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 3341) 		if (unlikely(error))
c5971b8c6354a (Al Viro                 2020-03-05 11:41:29 -0500 3342) 			return error;
30aba6656f61e (Salvatore Mesoraca      2018-08-23 17:00:35 -0700 3343) 	}
44b1d53043c48 (Miklos Szeredi          2014-04-01 17:08:41 +0200 3344) 	if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
c5971b8c6354a (Al Viro                 2020-03-05 11:41:29 -0500 3345) 		return -ENOTDIR;
6c0d46c493217 (Al Viro                 2011-03-09 00:59:59 -0500 3346) 
8795e7d482881 (Al Viro                 2020-01-26 10:38:17 -0500 3347) 	do_truncate = false;
8795e7d482881 (Al Viro                 2020-01-26 10:38:17 -0500 3348) 	acc_mode = op->acc_mode;
5a2d3edd8dad8 (Al Viro                 2020-01-26 10:32:22 -0500 3349) 	if (file->f_mode & FMODE_CREATED) {
5a2d3edd8dad8 (Al Viro                 2020-01-26 10:32:22 -0500 3350) 		/* Don't check for write permission, don't truncate */
5a2d3edd8dad8 (Al Viro                 2020-01-26 10:32:22 -0500 3351) 		open_flag &= ~O_TRUNC;
5a2d3edd8dad8 (Al Viro                 2020-01-26 10:32:22 -0500 3352) 		acc_mode = 0;
8795e7d482881 (Al Viro                 2020-01-26 10:38:17 -0500 3353) 	} else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) {
0f9d1a10c3410 (Al Viro                 2011-03-09 00:13:14 -0500 3354) 		error = mnt_want_write(nd->path.mnt);
0f9d1a10c3410 (Al Viro                 2011-03-09 00:13:14 -0500 3355) 		if (error)
c5971b8c6354a (Al Viro                 2020-03-05 11:41:29 -0500 3356) 			return error;
8795e7d482881 (Al Viro                 2020-01-26 10:38:17 -0500 3357) 		do_truncate = true;
0f9d1a10c3410 (Al Viro                 2011-03-09 00:13:14 -0500 3358) 	}
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 3359) 	error = may_open(mnt_userns, &nd->path, acc_mode, open_flag);
8795e7d482881 (Al Viro                 2020-01-26 10:38:17 -0500 3360) 	if (!error && !(file->f_mode & FMODE_OPENED))
3ad5615a071f7 (Al Viro                 2020-01-26 10:06:13 -0500 3361) 		error = vfs_open(&nd->path, file);
8795e7d482881 (Al Viro                 2020-01-26 10:38:17 -0500 3362) 	if (!error)
8795e7d482881 (Al Viro                 2020-01-26 10:38:17 -0500 3363) 		error = ima_file_check(file, op->acc_mode);
8795e7d482881 (Al Viro                 2020-01-26 10:38:17 -0500 3364) 	if (!error && do_truncate)
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 3365) 		error = handle_truncate(mnt_userns, file);
c80567c82ae48 (Al Viro                 2016-02-27 19:17:33 -0500 3366) 	if (unlikely(error > 0)) {
c80567c82ae48 (Al Viro                 2016-02-27 19:17:33 -0500 3367) 		WARN_ON(1);
c80567c82ae48 (Al Viro                 2016-02-27 19:17:33 -0500 3368) 		error = -EINVAL;
c80567c82ae48 (Al Viro                 2016-02-27 19:17:33 -0500 3369) 	}
8795e7d482881 (Al Viro                 2020-01-26 10:38:17 -0500 3370) 	if (do_truncate)
0f9d1a10c3410 (Al Viro                 2011-03-09 00:13:14 -0500 3371) 		mnt_drop_write(nd->path.mnt);
c5971b8c6354a (Al Viro                 2020-03-05 11:41:29 -0500 3372) 	return error;
fb1cc555d5338 (Al Viro                 2009-12-24 01:58:28 -0500 3373) }
fb1cc555d5338 (Al Viro                 2009-12-24 01:58:28 -0500 3374) 
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3375) /**
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3376)  * vfs_tmpfile - create tmpfile
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3377)  * @mnt_userns:	user namespace of the mount the inode was found from
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3378)  * @dentry:	pointer to dentry of the base directory
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3379)  * @mode:	mode of the new tmpfile
2111c3c0124f7 (Randy Dunlap            2021-02-15 20:29:28 -0800 3380)  * @open_flag:	flags
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3381)  *
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3382)  * Create a temporary file.
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3383)  *
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3384)  * If the inode has been found through an idmapped mount the user namespace of
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3385)  * the vfsmount must be passed through @mnt_userns. This function will then take
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3386)  * care to map the inode according to @mnt_userns before checking permissions.
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3387)  * On non-idmapped mounts or if permission checking is to be performed on the
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3388)  * raw inode simply passs init_user_ns.
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3389)  */
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3390) struct dentry *vfs_tmpfile(struct user_namespace *mnt_userns,
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3391) 			   struct dentry *dentry, umode_t mode, int open_flag)
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3392) {
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3393) 	struct dentry *child = NULL;
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3394) 	struct inode *dir = dentry->d_inode;
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3395) 	struct inode *inode;
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3396) 	int error;
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3397) 
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3398) 	/* we want directory to be writable */
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3399) 	error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3400) 	if (error)
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3401) 		goto out_err;
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3402) 	error = -EOPNOTSUPP;
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3403) 	if (!dir->i_op->tmpfile)
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3404) 		goto out_err;
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3405) 	error = -ENOMEM;
cdf01226b26e9 (David Howells           2017-07-04 17:25:22 +0100 3406) 	child = d_alloc(dentry, &slash_name);
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3407) 	if (unlikely(!child))
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3408) 		goto out_err;
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 3409) 	error = dir->i_op->tmpfile(mnt_userns, dir, child, mode);
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3410) 	if (error)
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3411) 		goto out_err;
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3412) 	error = -ENOENT;
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3413) 	inode = child->d_inode;
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3414) 	if (unlikely(!inode))
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3415) 		goto out_err;
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3416) 	if (!(open_flag & O_EXCL)) {
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3417) 		spin_lock(&inode->i_lock);
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3418) 		inode->i_state |= I_LINKABLE;
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3419) 		spin_unlock(&inode->i_lock);
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3420) 	}
a2d2329e30e22 (Christian Brauner       2021-01-21 14:19:45 +0100 3421) 	ima_post_create_tmpfile(mnt_userns, inode);
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3422) 	return child;
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3423) 
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3424) out_err:
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3425) 	dput(child);
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3426) 	return ERR_PTR(error);
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3427) }
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3428) EXPORT_SYMBOL(vfs_tmpfile);
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3429) 
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 3430) static int do_tmpfile(struct nameidata *nd, unsigned flags,
60545d0d4610b (Al Viro                 2013-06-07 01:20:27 -0400 3431) 		const struct open_flags *op,
3ec2eef116e90 (Al Viro                 2018-06-08 13:43:47 -0400 3432) 		struct file *file)
60545d0d4610b (Al Viro                 2013-06-07 01:20:27 -0400 3433) {
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3434) 	struct user_namespace *mnt_userns;
625b6d105446e (Al Viro                 2015-05-12 16:36:12 -0400 3435) 	struct dentry *child;
625b6d105446e (Al Viro                 2015-05-12 16:36:12 -0400 3436) 	struct path path;
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 3437) 	int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
60545d0d4610b (Al Viro                 2013-06-07 01:20:27 -0400 3438) 	if (unlikely(error))
60545d0d4610b (Al Viro                 2013-06-07 01:20:27 -0400 3439) 		return error;
625b6d105446e (Al Viro                 2015-05-12 16:36:12 -0400 3440) 	error = mnt_want_write(path.mnt);
60545d0d4610b (Al Viro                 2013-06-07 01:20:27 -0400 3441) 	if (unlikely(error))
60545d0d4610b (Al Viro                 2013-06-07 01:20:27 -0400 3442) 		goto out;
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3443) 	mnt_userns = mnt_user_ns(path.mnt);
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3444) 	child = vfs_tmpfile(mnt_userns, path.dentry, op->mode, op->open_flag);
af7bd4dc13093 (Amir Goldstein          2017-01-17 06:34:52 +0200 3445) 	error = PTR_ERR(child);
684e73bee4b5e (Hirofumi Nakagawa       2017-09-26 03:21:26 +0900 3446) 	if (IS_ERR(child))
60545d0d4610b (Al Viro                 2013-06-07 01:20:27 -0400 3447) 		goto out2;
625b6d105446e (Al Viro                 2015-05-12 16:36:12 -0400 3448) 	dput(path.dentry);
625b6d105446e (Al Viro                 2015-05-12 16:36:12 -0400 3449) 	path.dentry = child;
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 3450) 	audit_inode(nd->name, child, 0);
69a91c237ab0e (Eric Rannaud            2014-10-30 01:51:01 -0700 3451) 	/* Don't check for other permissions, the inode was just created */
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 3452) 	error = may_open(mnt_userns, &path, 0, op->open_flag);
1e8f44f159b31 (Al Viro                 2020-03-11 17:22:19 -0400 3453) 	if (!error)
1e8f44f159b31 (Al Viro                 2020-03-11 17:22:19 -0400 3454) 		error = vfs_open(&path, file);
60545d0d4610b (Al Viro                 2013-06-07 01:20:27 -0400 3455) out2:
625b6d105446e (Al Viro                 2015-05-12 16:36:12 -0400 3456) 	mnt_drop_write(path.mnt);
60545d0d4610b (Al Viro                 2013-06-07 01:20:27 -0400 3457) out:
625b6d105446e (Al Viro                 2015-05-12 16:36:12 -0400 3458) 	path_put(&path);
60545d0d4610b (Al Viro                 2013-06-07 01:20:27 -0400 3459) 	return error;
60545d0d4610b (Al Viro                 2013-06-07 01:20:27 -0400 3460) }
60545d0d4610b (Al Viro                 2013-06-07 01:20:27 -0400 3461) 
6ac087099edf0 (Al Viro                 2016-04-26 00:02:50 -0400 3462) static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
6ac087099edf0 (Al Viro                 2016-04-26 00:02:50 -0400 3463) {
6ac087099edf0 (Al Viro                 2016-04-26 00:02:50 -0400 3464) 	struct path path;
6ac087099edf0 (Al Viro                 2016-04-26 00:02:50 -0400 3465) 	int error = path_lookupat(nd, flags, &path);
6ac087099edf0 (Al Viro                 2016-04-26 00:02:50 -0400 3466) 	if (!error) {
6ac087099edf0 (Al Viro                 2016-04-26 00:02:50 -0400 3467) 		audit_inode(nd->name, path.dentry, 0);
ae2bb293a3e8a (Al Viro                 2018-07-10 13:22:28 -0400 3468) 		error = vfs_open(&path, file);
6ac087099edf0 (Al Viro                 2016-04-26 00:02:50 -0400 3469) 		path_put(&path);
6ac087099edf0 (Al Viro                 2016-04-26 00:02:50 -0400 3470) 	}
6ac087099edf0 (Al Viro                 2016-04-26 00:02:50 -0400 3471) 	return error;
6ac087099edf0 (Al Viro                 2016-04-26 00:02:50 -0400 3472) }
6ac087099edf0 (Al Viro                 2016-04-26 00:02:50 -0400 3473) 
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 3474) static struct file *path_openat(struct nameidata *nd,
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 3475) 			const struct open_flags *op, unsigned flags)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3476) {
30d904947459c (Al Viro                 2012-06-22 12:40:19 +0400 3477) 	struct file *file;
13aab428a73d3 (Al Viro                 2011-02-23 17:54:08 -0500 3478) 	int error;
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 3479) 
ea73ea7279884 (Al Viro                 2018-07-11 15:00:04 -0400 3480) 	file = alloc_empty_file(op->open_flag, current_cred());
1afc99beaf0fc (Al Viro                 2013-02-14 20:41:04 -0500 3481) 	if (IS_ERR(file))
1afc99beaf0fc (Al Viro                 2013-02-14 20:41:04 -0500 3482) 		return file;
31e6b01f4183f (Nicholas Piggin         2011-01-07 17:49:52 +1100 3483) 
bb458c644a59d (Al Viro                 2013-07-13 13:26:37 +0400 3484) 	if (unlikely(file->f_flags & __O_TMPFILE)) {
3ec2eef116e90 (Al Viro                 2018-06-08 13:43:47 -0400 3485) 		error = do_tmpfile(nd, flags, op, file);
5f336e722cc96 (Al Viro                 2018-07-09 16:38:06 -0400 3486) 	} else if (unlikely(file->f_flags & O_PATH)) {
6ac087099edf0 (Al Viro                 2016-04-26 00:02:50 -0400 3487) 		error = do_o_path(nd, flags, file);
5f336e722cc96 (Al Viro                 2018-07-09 16:38:06 -0400 3488) 	} else {
5f336e722cc96 (Al Viro                 2018-07-09 16:38:06 -0400 3489) 		const char *s = path_init(nd, flags);
5f336e722cc96 (Al Viro                 2018-07-09 16:38:06 -0400 3490) 		while (!(error = link_path_walk(s, nd)) &&
c5971b8c6354a (Al Viro                 2020-03-05 11:41:29 -0500 3491) 		       (s = open_last_lookups(nd, file, op)) != NULL)
1ccac622f9da5 (Al Viro                 2020-01-14 10:13:40 -0500 3492) 			;
c5971b8c6354a (Al Viro                 2020-03-05 11:41:29 -0500 3493) 		if (!error)
c5971b8c6354a (Al Viro                 2020-03-05 11:41:29 -0500 3494) 			error = do_open(nd, file, op);
5f336e722cc96 (Al Viro                 2018-07-09 16:38:06 -0400 3495) 		terminate_walk(nd);
806b681cbe588 (Al Viro                 2009-12-26 07:16:40 -0500 3496) 	}
7c1c01ec20d61 (Al Viro                 2018-06-08 12:56:55 -0400 3497) 	if (likely(!error)) {
aad888f828fec (Al Viro                 2018-06-08 12:58:04 -0400 3498) 		if (likely(file->f_mode & FMODE_OPENED))
7c1c01ec20d61 (Al Viro                 2018-06-08 12:56:55 -0400 3499) 			return file;
7c1c01ec20d61 (Al Viro                 2018-06-08 12:56:55 -0400 3500) 		WARN_ON(1);
7c1c01ec20d61 (Al Viro                 2018-06-08 12:56:55 -0400 3501) 		error = -EINVAL;
16b1c1cd71176 (Miklos Szeredi          2012-05-21 17:30:19 +0200 3502) 	}
7c1c01ec20d61 (Al Viro                 2018-06-08 12:56:55 -0400 3503) 	fput(file);
7c1c01ec20d61 (Al Viro                 2018-06-08 12:56:55 -0400 3504) 	if (error == -EOPENSTALE) {
7c1c01ec20d61 (Al Viro                 2018-06-08 12:56:55 -0400 3505) 		if (flags & LOOKUP_RCU)
7c1c01ec20d61 (Al Viro                 2018-06-08 12:56:55 -0400 3506) 			error = -ECHILD;
7c1c01ec20d61 (Al Viro                 2018-06-08 12:56:55 -0400 3507) 		else
7c1c01ec20d61 (Al Viro                 2018-06-08 12:56:55 -0400 3508) 			error = -ESTALE;
2675a4eb6a9f1 (Al Viro                 2012-06-22 12:41:10 +0400 3509) 	}
7c1c01ec20d61 (Al Viro                 2018-06-08 12:56:55 -0400 3510) 	return ERR_PTR(error);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3511) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3512) 
669abf4e5539c (Jeff Layton             2012-10-10 16:43:10 -0400 3513) struct file *do_filp_open(int dfd, struct filename *pathname,
f9652e10c12b4 (Al Viro                 2013-06-11 08:23:01 +0400 3514) 		const struct open_flags *op)
13aab428a73d3 (Al Viro                 2011-02-23 17:54:08 -0500 3515) {
9883d1855ecfa (Al Viro                 2015-05-13 07:28:08 -0400 3516) 	struct nameidata nd;
f9652e10c12b4 (Al Viro                 2013-06-11 08:23:01 +0400 3517) 	int flags = op->lookup_flags;
13aab428a73d3 (Al Viro                 2011-02-23 17:54:08 -0500 3518) 	struct file *filp;
13aab428a73d3 (Al Viro                 2011-02-23 17:54:08 -0500 3519) 
9883d1855ecfa (Al Viro                 2015-05-13 07:28:08 -0400 3520) 	set_nameidata(&nd, dfd, pathname);
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 3521) 	filp = path_openat(&nd, op, flags | LOOKUP_RCU);
13aab428a73d3 (Al Viro                 2011-02-23 17:54:08 -0500 3522) 	if (unlikely(filp == ERR_PTR(-ECHILD)))
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 3523) 		filp = path_openat(&nd, op, flags);
13aab428a73d3 (Al Viro                 2011-02-23 17:54:08 -0500 3524) 	if (unlikely(filp == ERR_PTR(-ESTALE)))
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 3525) 		filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
9883d1855ecfa (Al Viro                 2015-05-13 07:28:08 -0400 3526) 	restore_nameidata();
13aab428a73d3 (Al Viro                 2011-02-23 17:54:08 -0500 3527) 	return filp;
13aab428a73d3 (Al Viro                 2011-02-23 17:54:08 -0500 3528) }
13aab428a73d3 (Al Viro                 2011-02-23 17:54:08 -0500 3529) 
73d049a40fc62 (Al Viro                 2011-03-11 12:08:24 -0500 3530) struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
f9652e10c12b4 (Al Viro                 2013-06-11 08:23:01 +0400 3531) 		const char *name, const struct open_flags *op)
73d049a40fc62 (Al Viro                 2011-03-11 12:08:24 -0500 3532) {
9883d1855ecfa (Al Viro                 2015-05-13 07:28:08 -0400 3533) 	struct nameidata nd;
73d049a40fc62 (Al Viro                 2011-03-11 12:08:24 -0500 3534) 	struct file *file;
5168910413830 (Paul Moore              2015-01-22 00:00:03 -0500 3535) 	struct filename *filename;
f9652e10c12b4 (Al Viro                 2013-06-11 08:23:01 +0400 3536) 	int flags = op->lookup_flags | LOOKUP_ROOT;
73d049a40fc62 (Al Viro                 2011-03-11 12:08:24 -0500 3537) 
73d049a40fc62 (Al Viro                 2011-03-11 12:08:24 -0500 3538) 	nd.root.mnt = mnt;
73d049a40fc62 (Al Viro                 2011-03-11 12:08:24 -0500 3539) 	nd.root.dentry = dentry;
73d049a40fc62 (Al Viro                 2011-03-11 12:08:24 -0500 3540) 
b18825a7c8e37 (David Howells           2013-09-12 19:22:53 +0100 3541) 	if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
73d049a40fc62 (Al Viro                 2011-03-11 12:08:24 -0500 3542) 		return ERR_PTR(-ELOOP);
73d049a40fc62 (Al Viro                 2011-03-11 12:08:24 -0500 3543) 
5168910413830 (Paul Moore              2015-01-22 00:00:03 -0500 3544) 	filename = getname_kernel(name);
a1c83681d5270 (Viresh Kumar            2015-08-12 15:59:44 +0530 3545) 	if (IS_ERR(filename))
5168910413830 (Paul Moore              2015-01-22 00:00:03 -0500 3546) 		return ERR_CAST(filename);
5168910413830 (Paul Moore              2015-01-22 00:00:03 -0500 3547) 
9883d1855ecfa (Al Viro                 2015-05-13 07:28:08 -0400 3548) 	set_nameidata(&nd, -1, filename);
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 3549) 	file = path_openat(&nd, op, flags | LOOKUP_RCU);
73d049a40fc62 (Al Viro                 2011-03-11 12:08:24 -0500 3550) 	if (unlikely(file == ERR_PTR(-ECHILD)))
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 3551) 		file = path_openat(&nd, op, flags);
73d049a40fc62 (Al Viro                 2011-03-11 12:08:24 -0500 3552) 	if (unlikely(file == ERR_PTR(-ESTALE)))
c8a53ee5ee4fc (Al Viro                 2015-05-12 18:43:07 -0400 3553) 		file = path_openat(&nd, op, flags | LOOKUP_REVAL);
9883d1855ecfa (Al Viro                 2015-05-13 07:28:08 -0400 3554) 	restore_nameidata();
5168910413830 (Paul Moore              2015-01-22 00:00:03 -0500 3555) 	putname(filename);
73d049a40fc62 (Al Viro                 2011-03-11 12:08:24 -0500 3556) 	return file;
73d049a40fc62 (Al Viro                 2011-03-11 12:08:24 -0500 3557) }
73d049a40fc62 (Al Viro                 2011-03-11 12:08:24 -0500 3558) 
fa14a0b8d2bca (Al Viro                 2015-01-22 02:16:49 -0500 3559) static struct dentry *filename_create(int dfd, struct filename *name,
1ac12b4b6d707 (Jeff Layton             2012-12-11 12:10:06 -0500 3560) 				struct path *path, unsigned int lookup_flags)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3561) {
c663e5d80ebec (Christoph Hellwig       2005-06-23 00:09:49 -0700 3562) 	struct dentry *dentry = ERR_PTR(-EEXIST);
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 3563) 	struct qstr last;
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 3564) 	int type;
c30dabfe5d10c (Jan Kara                2012-06-12 16:20:30 +0200 3565) 	int err2;
1ac12b4b6d707 (Jeff Layton             2012-12-11 12:10:06 -0500 3566) 	int error;
1ac12b4b6d707 (Jeff Layton             2012-12-11 12:10:06 -0500 3567) 	bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
1ac12b4b6d707 (Jeff Layton             2012-12-11 12:10:06 -0500 3568) 
1ac12b4b6d707 (Jeff Layton             2012-12-11 12:10:06 -0500 3569) 	/*
1ac12b4b6d707 (Jeff Layton             2012-12-11 12:10:06 -0500 3570) 	 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
1ac12b4b6d707 (Jeff Layton             2012-12-11 12:10:06 -0500 3571) 	 * other flags passed in are ignored!
1ac12b4b6d707 (Jeff Layton             2012-12-11 12:10:06 -0500 3572) 	 */
1ac12b4b6d707 (Jeff Layton             2012-12-11 12:10:06 -0500 3573) 	lookup_flags &= LOOKUP_REVAL;
1ac12b4b6d707 (Jeff Layton             2012-12-11 12:10:06 -0500 3574) 
5c31b6cedb675 (Al Viro                 2015-05-12 17:32:54 -0400 3575) 	name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
5c31b6cedb675 (Al Viro                 2015-05-12 17:32:54 -0400 3576) 	if (IS_ERR(name))
5c31b6cedb675 (Al Viro                 2015-05-12 17:32:54 -0400 3577) 		return ERR_CAST(name);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3578) 
c663e5d80ebec (Christoph Hellwig       2005-06-23 00:09:49 -0700 3579) 	/*
c663e5d80ebec (Christoph Hellwig       2005-06-23 00:09:49 -0700 3580) 	 * Yucky last component or no last component at all?
c663e5d80ebec (Christoph Hellwig       2005-06-23 00:09:49 -0700 3581) 	 * (foo/., foo/.., /////)
c663e5d80ebec (Christoph Hellwig       2005-06-23 00:09:49 -0700 3582) 	 */
5c31b6cedb675 (Al Viro                 2015-05-12 17:32:54 -0400 3583) 	if (unlikely(type != LAST_NORM))
ed75e95de574c (Al Viro                 2011-06-27 16:53:43 -0400 3584) 		goto out;
c663e5d80ebec (Christoph Hellwig       2005-06-23 00:09:49 -0700 3585) 
c30dabfe5d10c (Jan Kara                2012-06-12 16:20:30 +0200 3586) 	/* don't fail immediately if it's r/o, at least try to report other errors */
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 3587) 	err2 = mnt_want_write(path->mnt);
c663e5d80ebec (Christoph Hellwig       2005-06-23 00:09:49 -0700 3588) 	/*
c663e5d80ebec (Christoph Hellwig       2005-06-23 00:09:49 -0700 3589) 	 * Do the final lookup.
c663e5d80ebec (Christoph Hellwig       2005-06-23 00:09:49 -0700 3590) 	 */
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 3591) 	lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 3592) 	inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 3593) 	dentry = __lookup_hash(&last, path->dentry, lookup_flags);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3594) 	if (IS_ERR(dentry))
a8104a9fcdeb8 (Al Viro                 2012-07-20 02:25:00 +0400 3595) 		goto unlock;
c663e5d80ebec (Christoph Hellwig       2005-06-23 00:09:49 -0700 3596) 
a8104a9fcdeb8 (Al Viro                 2012-07-20 02:25:00 +0400 3597) 	error = -EEXIST;
b18825a7c8e37 (David Howells           2013-09-12 19:22:53 +0100 3598) 	if (d_is_positive(dentry))
a8104a9fcdeb8 (Al Viro                 2012-07-20 02:25:00 +0400 3599) 		goto fail;
b18825a7c8e37 (David Howells           2013-09-12 19:22:53 +0100 3600) 
c663e5d80ebec (Christoph Hellwig       2005-06-23 00:09:49 -0700 3601) 	/*
c663e5d80ebec (Christoph Hellwig       2005-06-23 00:09:49 -0700 3602) 	 * Special case - lookup gave negative, but... we had foo/bar/
c663e5d80ebec (Christoph Hellwig       2005-06-23 00:09:49 -0700 3603) 	 * From the vfs_mknod() POV we just have a negative dentry -
c663e5d80ebec (Christoph Hellwig       2005-06-23 00:09:49 -0700 3604) 	 * all is fine. Let's be bastards - you had / on the end, you've
c663e5d80ebec (Christoph Hellwig       2005-06-23 00:09:49 -0700 3605) 	 * been asking for (non-existent) directory. -ENOENT for you.
c663e5d80ebec (Christoph Hellwig       2005-06-23 00:09:49 -0700 3606) 	 */
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 3607) 	if (unlikely(!is_dir && last.name[last.len])) {
a8104a9fcdeb8 (Al Viro                 2012-07-20 02:25:00 +0400 3608) 		error = -ENOENT;
ed75e95de574c (Al Viro                 2011-06-27 16:53:43 -0400 3609) 		goto fail;
e9baf6e598422 (Al Viro                 2008-05-15 04:49:12 -0400 3610) 	}
c30dabfe5d10c (Jan Kara                2012-06-12 16:20:30 +0200 3611) 	if (unlikely(err2)) {
c30dabfe5d10c (Jan Kara                2012-06-12 16:20:30 +0200 3612) 		error = err2;
a8104a9fcdeb8 (Al Viro                 2012-07-20 02:25:00 +0400 3613) 		goto fail;
c30dabfe5d10c (Jan Kara                2012-06-12 16:20:30 +0200 3614) 	}
181c37b6e4c1b (Al Viro                 2015-05-12 17:21:25 -0400 3615) 	putname(name);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3616) 	return dentry;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3617) fail:
a8104a9fcdeb8 (Al Viro                 2012-07-20 02:25:00 +0400 3618) 	dput(dentry);
a8104a9fcdeb8 (Al Viro                 2012-07-20 02:25:00 +0400 3619) 	dentry = ERR_PTR(error);
a8104a9fcdeb8 (Al Viro                 2012-07-20 02:25:00 +0400 3620) unlock:
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 3621) 	inode_unlock(path->dentry->d_inode);
c30dabfe5d10c (Jan Kara                2012-06-12 16:20:30 +0200 3622) 	if (!err2)
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 3623) 		mnt_drop_write(path->mnt);
ed75e95de574c (Al Viro                 2011-06-27 16:53:43 -0400 3624) out:
391172c46e6f9 (Al Viro                 2015-05-09 11:19:16 -0400 3625) 	path_put(path);
181c37b6e4c1b (Al Viro                 2015-05-12 17:21:25 -0400 3626) 	putname(name);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3627) 	return dentry;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3628) }
fa14a0b8d2bca (Al Viro                 2015-01-22 02:16:49 -0500 3629) 
fa14a0b8d2bca (Al Viro                 2015-01-22 02:16:49 -0500 3630) struct dentry *kern_path_create(int dfd, const char *pathname,
fa14a0b8d2bca (Al Viro                 2015-01-22 02:16:49 -0500 3631) 				struct path *path, unsigned int lookup_flags)
fa14a0b8d2bca (Al Viro                 2015-01-22 02:16:49 -0500 3632) {
181c37b6e4c1b (Al Viro                 2015-05-12 17:21:25 -0400 3633) 	return filename_create(dfd, getname_kernel(pathname),
181c37b6e4c1b (Al Viro                 2015-05-12 17:21:25 -0400 3634) 				path, lookup_flags);
fa14a0b8d2bca (Al Viro                 2015-01-22 02:16:49 -0500 3635) }
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 3636) EXPORT_SYMBOL(kern_path_create);
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 3637) 
921a1650de9ee (Al Viro                 2012-07-20 01:15:31 +0400 3638) void done_path_create(struct path *path, struct dentry *dentry)
921a1650de9ee (Al Viro                 2012-07-20 01:15:31 +0400 3639) {
921a1650de9ee (Al Viro                 2012-07-20 01:15:31 +0400 3640) 	dput(dentry);
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 3641) 	inode_unlock(path->dentry->d_inode);
a8104a9fcdeb8 (Al Viro                 2012-07-20 02:25:00 +0400 3642) 	mnt_drop_write(path->mnt);
921a1650de9ee (Al Viro                 2012-07-20 01:15:31 +0400 3643) 	path_put(path);
921a1650de9ee (Al Viro                 2012-07-20 01:15:31 +0400 3644) }
921a1650de9ee (Al Viro                 2012-07-20 01:15:31 +0400 3645) EXPORT_SYMBOL(done_path_create);
921a1650de9ee (Al Viro                 2012-07-20 01:15:31 +0400 3646) 
520ae6874726d (Al Viro                 2015-05-13 07:00:28 -0400 3647) inline struct dentry *user_path_create(int dfd, const char __user *pathname,
1ac12b4b6d707 (Jeff Layton             2012-12-11 12:10:06 -0500 3648) 				struct path *path, unsigned int lookup_flags)
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 3649) {
181c37b6e4c1b (Al Viro                 2015-05-12 17:21:25 -0400 3650) 	return filename_create(dfd, getname(pathname), path, lookup_flags);
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 3651) }
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 3652) EXPORT_SYMBOL(user_path_create);
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 3653) 
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3654) /**
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3655)  * vfs_mknod - create device node or file
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3656)  * @mnt_userns:	user namespace of the mount the inode was found from
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3657)  * @dir:	inode of @dentry
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3658)  * @dentry:	pointer to dentry of the base directory
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3659)  * @mode:	mode of the new device node or file
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3660)  * @dev:	device number of device to create
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3661)  *
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3662)  * Create a device node or file.
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3663)  *
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3664)  * If the inode has been found through an idmapped mount the user namespace of
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3665)  * the vfsmount must be passed through @mnt_userns. This function will then take
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3666)  * care to map the inode according to @mnt_userns before checking permissions.
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3667)  * On non-idmapped mounts or if permission checking is to be performed on the
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3668)  * raw inode simply passs init_user_ns.
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3669)  */
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3670) int vfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3671) 	      struct dentry *dentry, umode_t mode, dev_t dev)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3672) {
a3c751a50fe6b (Miklos Szeredi          2020-05-14 16:44:23 +0200 3673) 	bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV;
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3674) 	int error = may_create(mnt_userns, dir, dentry);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3675) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3676) 	if (error)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3677) 		return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3678) 
a3c751a50fe6b (Miklos Szeredi          2020-05-14 16:44:23 +0200 3679) 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !is_whiteout &&
a3c751a50fe6b (Miklos Szeredi          2020-05-14 16:44:23 +0200 3680) 	    !capable(CAP_MKNOD))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3681) 		return -EPERM;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3682) 
acfa4380efe77 (Al Viro                 2008-12-04 10:06:33 -0500 3683) 	if (!dir->i_op->mknod)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3684) 		return -EPERM;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3685) 
08ce5f16ee466 (Serge Hallyn            2008-04-29 01:00:10 -0700 3686) 	error = devcgroup_inode_mknod(mode, dev);
08ce5f16ee466 (Serge Hallyn            2008-04-29 01:00:10 -0700 3687) 	if (error)
08ce5f16ee466 (Serge Hallyn            2008-04-29 01:00:10 -0700 3688) 		return error;
08ce5f16ee466 (Serge Hallyn            2008-04-29 01:00:10 -0700 3689) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3690) 	error = security_inode_mknod(dir, dentry, mode, dev);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3691) 	if (error)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3692) 		return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3693) 
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 3694) 	error = dir->i_op->mknod(mnt_userns, dir, dentry, mode, dev);
a74574aafea3a (Stephen Smalley         2005-09-09 13:01:44 -0700 3695) 	if (!error)
f38aa94224c55 (Amy Griffis             2005-11-03 15:57:06 +0000 3696) 		fsnotify_create(dir, dentry);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3697) 	return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3698) }
4d359507346a1 (Al Viro                 2014-03-14 12:20:17 -0400 3699) EXPORT_SYMBOL(vfs_mknod);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3700) 
f69aac0006c30 (Al Viro                 2011-07-26 04:31:40 -0400 3701) static int may_mknod(umode_t mode)
463c3197263bd (Dave Hansen             2008-02-15 14:37:57 -0800 3702) {
463c3197263bd (Dave Hansen             2008-02-15 14:37:57 -0800 3703) 	switch (mode & S_IFMT) {
463c3197263bd (Dave Hansen             2008-02-15 14:37:57 -0800 3704) 	case S_IFREG:
463c3197263bd (Dave Hansen             2008-02-15 14:37:57 -0800 3705) 	case S_IFCHR:
463c3197263bd (Dave Hansen             2008-02-15 14:37:57 -0800 3706) 	case S_IFBLK:
463c3197263bd (Dave Hansen             2008-02-15 14:37:57 -0800 3707) 	case S_IFIFO:
463c3197263bd (Dave Hansen             2008-02-15 14:37:57 -0800 3708) 	case S_IFSOCK:
463c3197263bd (Dave Hansen             2008-02-15 14:37:57 -0800 3709) 	case 0: /* zero mode translates to S_IFREG */
463c3197263bd (Dave Hansen             2008-02-15 14:37:57 -0800 3710) 		return 0;
463c3197263bd (Dave Hansen             2008-02-15 14:37:57 -0800 3711) 	case S_IFDIR:
463c3197263bd (Dave Hansen             2008-02-15 14:37:57 -0800 3712) 		return -EPERM;
463c3197263bd (Dave Hansen             2008-02-15 14:37:57 -0800 3713) 	default:
463c3197263bd (Dave Hansen             2008-02-15 14:37:57 -0800 3714) 		return -EINVAL;
463c3197263bd (Dave Hansen             2008-02-15 14:37:57 -0800 3715) 	}
463c3197263bd (Dave Hansen             2008-02-15 14:37:57 -0800 3716) }
463c3197263bd (Dave Hansen             2008-02-15 14:37:57 -0800 3717) 
5fee64fcde077 (Christoph Hellwig       2020-07-22 11:41:20 +0200 3718) static long do_mknodat(int dfd, const char __user *filename, umode_t mode,
87c4e19262d81 (Dominik Brodowski       2018-03-11 11:34:50 +0100 3719) 		unsigned int dev)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3720) {
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3721) 	struct user_namespace *mnt_userns;
2ad94ae654f5e (Al Viro                 2008-07-21 09:32:51 -0400 3722) 	struct dentry *dentry;
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 3723) 	struct path path;
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 3724) 	int error;
972567f14cbcd (Jeff Layton             2012-12-20 16:00:10 -0500 3725) 	unsigned int lookup_flags = 0;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3726) 
8e4bfca1d1f0d (Al Viro                 2012-07-20 01:17:26 +0400 3727) 	error = may_mknod(mode);
8e4bfca1d1f0d (Al Viro                 2012-07-20 01:17:26 +0400 3728) 	if (error)
8e4bfca1d1f0d (Al Viro                 2012-07-20 01:17:26 +0400 3729) 		return error;
972567f14cbcd (Jeff Layton             2012-12-20 16:00:10 -0500 3730) retry:
972567f14cbcd (Jeff Layton             2012-12-20 16:00:10 -0500 3731) 	dentry = user_path_create(dfd, filename, &path, lookup_flags);
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 3732) 	if (IS_ERR(dentry))
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 3733) 		return PTR_ERR(dentry);
2ad94ae654f5e (Al Viro                 2008-07-21 09:32:51 -0400 3734) 
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 3735) 	if (!IS_POSIXACL(path.dentry->d_inode))
ce3b0f8d5c220 (Al Viro                 2009-03-29 19:08:22 -0400 3736) 		mode &= ~current_umask();
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 3737) 	error = security_path_mknod(&path, dentry, mode, dev);
be6d3e56a6b9b (Kentaro Takeda          2008-12-17 13:24:15 +0900 3738) 	if (error)
a8104a9fcdeb8 (Al Viro                 2012-07-20 02:25:00 +0400 3739) 		goto out;
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3740) 
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3741) 	mnt_userns = mnt_user_ns(path.mnt);
463c3197263bd (Dave Hansen             2008-02-15 14:37:57 -0800 3742) 	switch (mode & S_IFMT) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3743) 		case 0: case S_IFREG:
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3744) 			error = vfs_create(mnt_userns, path.dentry->d_inode,
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3745) 					   dentry, mode, true);
05d1a717ec043 (Mimi Zohar              2016-02-29 19:52:05 -0500 3746) 			if (!error)
a2d2329e30e22 (Christian Brauner       2021-01-21 14:19:45 +0100 3747) 				ima_post_path_mknod(mnt_userns, dentry);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3748) 			break;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3749) 		case S_IFCHR: case S_IFBLK:
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3750) 			error = vfs_mknod(mnt_userns, path.dentry->d_inode,
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3751) 					  dentry, mode, new_decode_dev(dev));
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3752) 			break;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3753) 		case S_IFIFO: case S_IFSOCK:
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3754) 			error = vfs_mknod(mnt_userns, path.dentry->d_inode,
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3755) 					  dentry, mode, 0);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3756) 			break;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3757) 	}
a8104a9fcdeb8 (Al Viro                 2012-07-20 02:25:00 +0400 3758) out:
921a1650de9ee (Al Viro                 2012-07-20 01:15:31 +0400 3759) 	done_path_create(&path, dentry);
972567f14cbcd (Jeff Layton             2012-12-20 16:00:10 -0500 3760) 	if (retry_estale(error, lookup_flags)) {
972567f14cbcd (Jeff Layton             2012-12-20 16:00:10 -0500 3761) 		lookup_flags |= LOOKUP_REVAL;
972567f14cbcd (Jeff Layton             2012-12-20 16:00:10 -0500 3762) 		goto retry;
972567f14cbcd (Jeff Layton             2012-12-20 16:00:10 -0500 3763) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3764) 	return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3765) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3766) 
87c4e19262d81 (Dominik Brodowski       2018-03-11 11:34:50 +0100 3767) SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
87c4e19262d81 (Dominik Brodowski       2018-03-11 11:34:50 +0100 3768) 		unsigned int, dev)
87c4e19262d81 (Dominik Brodowski       2018-03-11 11:34:50 +0100 3769) {
87c4e19262d81 (Dominik Brodowski       2018-03-11 11:34:50 +0100 3770) 	return do_mknodat(dfd, filename, mode, dev);
87c4e19262d81 (Dominik Brodowski       2018-03-11 11:34:50 +0100 3771) }
87c4e19262d81 (Dominik Brodowski       2018-03-11 11:34:50 +0100 3772) 
8208a22bb8bd3 (Al Viro                 2011-07-25 17:32:17 -0400 3773) SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 3774) {
87c4e19262d81 (Dominik Brodowski       2018-03-11 11:34:50 +0100 3775) 	return do_mknodat(AT_FDCWD, filename, mode, dev);
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 3776) }
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 3777) 
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3778) /**
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3779)  * vfs_mkdir - create directory
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3780)  * @mnt_userns:	user namespace of the mount the inode was found from
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3781)  * @dir:	inode of @dentry
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3782)  * @dentry:	pointer to dentry of the base directory
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3783)  * @mode:	mode of the new directory
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3784)  *
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3785)  * Create a directory.
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3786)  *
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3787)  * If the inode has been found through an idmapped mount the user namespace of
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3788)  * the vfsmount must be passed through @mnt_userns. This function will then take
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3789)  * care to map the inode according to @mnt_userns before checking permissions.
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3790)  * On non-idmapped mounts or if permission checking is to be performed on the
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3791)  * raw inode simply passs init_user_ns.
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3792)  */
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3793) int vfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3794) 	      struct dentry *dentry, umode_t mode)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3795) {
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3796) 	int error = may_create(mnt_userns, dir, dentry);
8de52778798fe (Al Viro                 2012-02-06 12:45:27 -0500 3797) 	unsigned max_links = dir->i_sb->s_max_links;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3798) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3799) 	if (error)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3800) 		return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3801) 
acfa4380efe77 (Al Viro                 2008-12-04 10:06:33 -0500 3802) 	if (!dir->i_op->mkdir)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3803) 		return -EPERM;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3804) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3805) 	mode &= (S_IRWXUGO|S_ISVTX);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3806) 	error = security_inode_mkdir(dir, dentry, mode);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3807) 	if (error)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3808) 		return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3809) 
8de52778798fe (Al Viro                 2012-02-06 12:45:27 -0500 3810) 	if (max_links && dir->i_nlink >= max_links)
8de52778798fe (Al Viro                 2012-02-06 12:45:27 -0500 3811) 		return -EMLINK;
8de52778798fe (Al Viro                 2012-02-06 12:45:27 -0500 3812) 
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 3813) 	error = dir->i_op->mkdir(mnt_userns, dir, dentry, mode);
a74574aafea3a (Stephen Smalley         2005-09-09 13:01:44 -0700 3814) 	if (!error)
f38aa94224c55 (Amy Griffis             2005-11-03 15:57:06 +0000 3815) 		fsnotify_mkdir(dir, dentry);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3816) 	return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3817) }
4d359507346a1 (Al Viro                 2014-03-14 12:20:17 -0400 3818) EXPORT_SYMBOL(vfs_mkdir);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3819) 
83ff98c3e9cd2 (Christoph Hellwig       2020-07-22 11:14:59 +0200 3820) static long do_mkdirat(int dfd, const char __user *pathname, umode_t mode)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3821) {
6902d925d568c (Dave Hansen             2006-09-30 23:29:01 -0700 3822) 	struct dentry *dentry;
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 3823) 	struct path path;
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 3824) 	int error;
b76d8b8226607 (Jeff Layton             2012-12-20 16:04:09 -0500 3825) 	unsigned int lookup_flags = LOOKUP_DIRECTORY;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3826) 
b76d8b8226607 (Jeff Layton             2012-12-20 16:04:09 -0500 3827) retry:
b76d8b8226607 (Jeff Layton             2012-12-20 16:04:09 -0500 3828) 	dentry = user_path_create(dfd, pathname, &path, lookup_flags);
6902d925d568c (Dave Hansen             2006-09-30 23:29:01 -0700 3829) 	if (IS_ERR(dentry))
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 3830) 		return PTR_ERR(dentry);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3831) 
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 3832) 	if (!IS_POSIXACL(path.dentry->d_inode))
ce3b0f8d5c220 (Al Viro                 2009-03-29 19:08:22 -0400 3833) 		mode &= ~current_umask();
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 3834) 	error = security_path_mkdir(&path, dentry, mode);
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3835) 	if (!error) {
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3836) 		struct user_namespace *mnt_userns;
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3837) 		mnt_userns = mnt_user_ns(path.mnt);
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 3838) 		error = vfs_mkdir(mnt_userns, path.dentry->d_inode, dentry,
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 3839) 				  mode);
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3840) 	}
921a1650de9ee (Al Viro                 2012-07-20 01:15:31 +0400 3841) 	done_path_create(&path, dentry);
b76d8b8226607 (Jeff Layton             2012-12-20 16:04:09 -0500 3842) 	if (retry_estale(error, lookup_flags)) {
b76d8b8226607 (Jeff Layton             2012-12-20 16:04:09 -0500 3843) 		lookup_flags |= LOOKUP_REVAL;
b76d8b8226607 (Jeff Layton             2012-12-20 16:04:09 -0500 3844) 		goto retry;
b76d8b8226607 (Jeff Layton             2012-12-20 16:04:09 -0500 3845) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3846) 	return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3847) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3848) 
0101db7a30198 (Dominik Brodowski       2018-03-11 11:34:49 +0100 3849) SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
0101db7a30198 (Dominik Brodowski       2018-03-11 11:34:49 +0100 3850) {
0101db7a30198 (Dominik Brodowski       2018-03-11 11:34:49 +0100 3851) 	return do_mkdirat(dfd, pathname, mode);
0101db7a30198 (Dominik Brodowski       2018-03-11 11:34:49 +0100 3852) }
0101db7a30198 (Dominik Brodowski       2018-03-11 11:34:49 +0100 3853) 
a218d0fdc5f90 (Al Viro                 2011-11-21 14:59:34 -0500 3854) SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 3855) {
0101db7a30198 (Dominik Brodowski       2018-03-11 11:34:49 +0100 3856) 	return do_mkdirat(AT_FDCWD, pathname, mode);
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 3857) }
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 3858) 
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3859) /**
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3860)  * vfs_rmdir - remove directory
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3861)  * @mnt_userns:	user namespace of the mount the inode was found from
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3862)  * @dir:	inode of @dentry
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3863)  * @dentry:	pointer to dentry of the base directory
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3864)  *
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3865)  * Remove a directory.
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3866)  *
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3867)  * If the inode has been found through an idmapped mount the user namespace of
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3868)  * the vfsmount must be passed through @mnt_userns. This function will then take
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3869)  * care to map the inode according to @mnt_userns before checking permissions.
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3870)  * On non-idmapped mounts or if permission checking is to be performed on the
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3871)  * raw inode simply passs init_user_ns.
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3872)  */
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3873) int vfs_rmdir(struct user_namespace *mnt_userns, struct inode *dir,
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3874) 		     struct dentry *dentry)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3875) {
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3876) 	int error = may_delete(mnt_userns, dir, dentry, 1);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3877) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3878) 	if (error)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3879) 		return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3880) 
acfa4380efe77 (Al Viro                 2008-12-04 10:06:33 -0500 3881) 	if (!dir->i_op->rmdir)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3882) 		return -EPERM;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3883) 
1d2ef59014830 (Al Viro                 2011-09-14 18:55:41 +0100 3884) 	dget(dentry);
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 3885) 	inode_lock(dentry->d_inode);
912dbc15d9537 (Sage Weil               2011-05-24 13:06:11 -0700 3886) 
912dbc15d9537 (Sage Weil               2011-05-24 13:06:11 -0700 3887) 	error = -EBUSY;
7af1364ffa64d (Eric W. Biederman       2013-10-04 19:15:13 -0700 3888) 	if (is_local_mountpoint(dentry))
912dbc15d9537 (Sage Weil               2011-05-24 13:06:11 -0700 3889) 		goto out;
912dbc15d9537 (Sage Weil               2011-05-24 13:06:11 -0700 3890) 
912dbc15d9537 (Sage Weil               2011-05-24 13:06:11 -0700 3891) 	error = security_inode_rmdir(dir, dentry);
912dbc15d9537 (Sage Weil               2011-05-24 13:06:11 -0700 3892) 	if (error)
912dbc15d9537 (Sage Weil               2011-05-24 13:06:11 -0700 3893) 		goto out;
912dbc15d9537 (Sage Weil               2011-05-24 13:06:11 -0700 3894) 
912dbc15d9537 (Sage Weil               2011-05-24 13:06:11 -0700 3895) 	error = dir->i_op->rmdir(dir, dentry);
912dbc15d9537 (Sage Weil               2011-05-24 13:06:11 -0700 3896) 	if (error)
912dbc15d9537 (Sage Weil               2011-05-24 13:06:11 -0700 3897) 		goto out;
912dbc15d9537 (Sage Weil               2011-05-24 13:06:11 -0700 3898) 
8767712f26d18 (Al Viro                 2018-05-27 16:23:51 -0400 3899) 	shrink_dcache_parent(dentry);
912dbc15d9537 (Sage Weil               2011-05-24 13:06:11 -0700 3900) 	dentry->d_inode->i_flags |= S_DEAD;
912dbc15d9537 (Sage Weil               2011-05-24 13:06:11 -0700 3901) 	dont_mount(dentry);
8ed936b5671bf (Eric W. Biederman       2013-10-01 18:33:48 -0700 3902) 	detach_mounts(dentry);
116b9731ad761 (Amir Goldstein          2019-05-26 17:34:02 +0300 3903) 	fsnotify_rmdir(dir, dentry);
912dbc15d9537 (Sage Weil               2011-05-24 13:06:11 -0700 3904) 
912dbc15d9537 (Sage Weil               2011-05-24 13:06:11 -0700 3905) out:
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 3906) 	inode_unlock(dentry->d_inode);
1d2ef59014830 (Al Viro                 2011-09-14 18:55:41 +0100 3907) 	dput(dentry);
912dbc15d9537 (Sage Weil               2011-05-24 13:06:11 -0700 3908) 	if (!error)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3909) 		d_delete(dentry);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3910) 	return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3911) }
4d359507346a1 (Al Viro                 2014-03-14 12:20:17 -0400 3912) EXPORT_SYMBOL(vfs_rmdir);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3913) 
e24ab0ef689de (Christoph Hellwig       2020-07-21 10:48:15 +0200 3914) long do_rmdir(int dfd, struct filename *name)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3915) {
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3916) 	struct user_namespace *mnt_userns;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3917) 	int error = 0;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3918) 	struct dentry *dentry;
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 3919) 	struct path path;
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 3920) 	struct qstr last;
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 3921) 	int type;
c6ee920698301 (Jeff Layton             2012-12-20 16:28:33 -0500 3922) 	unsigned int lookup_flags = 0;
c6ee920698301 (Jeff Layton             2012-12-20 16:28:33 -0500 3923) retry:
e24ab0ef689de (Christoph Hellwig       2020-07-21 10:48:15 +0200 3924) 	name = filename_parentat(dfd, name, lookup_flags,
c1d4dd27678fc (Al Viro                 2016-06-05 16:38:18 -0400 3925) 				&path, &last, &type);
91a27b2a75678 (Jeff Layton             2012-10-10 15:25:28 -0400 3926) 	if (IS_ERR(name))
91a27b2a75678 (Jeff Layton             2012-10-10 15:25:28 -0400 3927) 		return PTR_ERR(name);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3928) 
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 3929) 	switch (type) {
0612d9fb270a4 (OGAWA Hirofumi          2008-10-16 07:50:29 +0900 3930) 	case LAST_DOTDOT:
0612d9fb270a4 (OGAWA Hirofumi          2008-10-16 07:50:29 +0900 3931) 		error = -ENOTEMPTY;
0612d9fb270a4 (OGAWA Hirofumi          2008-10-16 07:50:29 +0900 3932) 		goto exit1;
0612d9fb270a4 (OGAWA Hirofumi          2008-10-16 07:50:29 +0900 3933) 	case LAST_DOT:
0612d9fb270a4 (OGAWA Hirofumi          2008-10-16 07:50:29 +0900 3934) 		error = -EINVAL;
0612d9fb270a4 (OGAWA Hirofumi          2008-10-16 07:50:29 +0900 3935) 		goto exit1;
0612d9fb270a4 (OGAWA Hirofumi          2008-10-16 07:50:29 +0900 3936) 	case LAST_ROOT:
0612d9fb270a4 (OGAWA Hirofumi          2008-10-16 07:50:29 +0900 3937) 		error = -EBUSY;
0612d9fb270a4 (OGAWA Hirofumi          2008-10-16 07:50:29 +0900 3938) 		goto exit1;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3939) 	}
0612d9fb270a4 (OGAWA Hirofumi          2008-10-16 07:50:29 +0900 3940) 
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 3941) 	error = mnt_want_write(path.mnt);
c30dabfe5d10c (Jan Kara                2012-06-12 16:20:30 +0200 3942) 	if (error)
c30dabfe5d10c (Jan Kara                2012-06-12 16:20:30 +0200 3943) 		goto exit1;
0612d9fb270a4 (OGAWA Hirofumi          2008-10-16 07:50:29 +0900 3944) 
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 3945) 	inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 3946) 	dentry = __lookup_hash(&last, path.dentry, lookup_flags);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3947) 	error = PTR_ERR(dentry);
6902d925d568c (Dave Hansen             2006-09-30 23:29:01 -0700 3948) 	if (IS_ERR(dentry))
6902d925d568c (Dave Hansen             2006-09-30 23:29:01 -0700 3949) 		goto exit2;
e6bc45d65df85 (Theodore Ts'o           2011-06-06 19:19:40 -0400 3950) 	if (!dentry->d_inode) {
e6bc45d65df85 (Theodore Ts'o           2011-06-06 19:19:40 -0400 3951) 		error = -ENOENT;
e6bc45d65df85 (Theodore Ts'o           2011-06-06 19:19:40 -0400 3952) 		goto exit3;
e6bc45d65df85 (Theodore Ts'o           2011-06-06 19:19:40 -0400 3953) 	}
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 3954) 	error = security_path_rmdir(&path, dentry);
be6d3e56a6b9b (Kentaro Takeda          2008-12-17 13:24:15 +0900 3955) 	if (error)
c30dabfe5d10c (Jan Kara                2012-06-12 16:20:30 +0200 3956) 		goto exit3;
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3957) 	mnt_userns = mnt_user_ns(path.mnt);
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3958) 	error = vfs_rmdir(mnt_userns, path.dentry->d_inode, dentry);
0622753b800e4 (Dave Hansen             2008-02-15 14:37:34 -0800 3959) exit3:
6902d925d568c (Dave Hansen             2006-09-30 23:29:01 -0700 3960) 	dput(dentry);
6902d925d568c (Dave Hansen             2006-09-30 23:29:01 -0700 3961) exit2:
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 3962) 	inode_unlock(path.dentry->d_inode);
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 3963) 	mnt_drop_write(path.mnt);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3964) exit1:
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 3965) 	path_put(&path);
c6ee920698301 (Jeff Layton             2012-12-20 16:28:33 -0500 3966) 	if (retry_estale(error, lookup_flags)) {
c6ee920698301 (Jeff Layton             2012-12-20 16:28:33 -0500 3967) 		lookup_flags |= LOOKUP_REVAL;
c6ee920698301 (Jeff Layton             2012-12-20 16:28:33 -0500 3968) 		goto retry;
c6ee920698301 (Jeff Layton             2012-12-20 16:28:33 -0500 3969) 	}
24fb33d40d60b (Al Viro                 2020-08-12 05:15:18 +0100 3970) 	putname(name);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3971) 	return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3972) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 3973) 
3cdad42884bbd (Heiko Carstens          2009-01-14 14:14:22 +0100 3974) SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 3975) {
e24ab0ef689de (Christoph Hellwig       2020-07-21 10:48:15 +0200 3976) 	return do_rmdir(AT_FDCWD, getname(pathname));
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 3977) }
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 3978) 
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 3979) /**
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 3980)  * vfs_unlink - unlink a filesystem object
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3981)  * @mnt_userns:	user namespace of the mount the inode was found from
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 3982)  * @dir:	parent directory
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 3983)  * @dentry:	victim
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 3984)  * @delegated_inode: returns victim inode, if the inode is delegated.
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 3985)  *
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 3986)  * The caller must hold dir->i_mutex.
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 3987)  *
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 3988)  * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 3989)  * return a reference to the inode in delegated_inode.  The caller
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 3990)  * should then break the delegation on that inode and retry.  Because
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 3991)  * breaking a delegation may take a long time, the caller should drop
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 3992)  * dir->i_mutex before doing so.
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 3993)  *
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 3994)  * Alternatively, a caller may pass NULL for delegated_inode.  This may
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 3995)  * be appropriate for callers that expect the underlying filesystem not
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 3996)  * to be NFS exported.
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3997)  *
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3998)  * If the inode has been found through an idmapped mount the user namespace of
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 3999)  * the vfsmount must be passed through @mnt_userns. This function will then take
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4000)  * care to map the inode according to @mnt_userns before checking permissions.
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4001)  * On non-idmapped mounts or if permission checking is to be performed on the
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4002)  * raw inode simply passs init_user_ns.
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 4003)  */
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4004) int vfs_unlink(struct user_namespace *mnt_userns, struct inode *dir,
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4005) 	       struct dentry *dentry, struct inode **delegated_inode)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4006) {
9accbb977ab78 (J. Bruce Fields         2012-08-28 07:03:24 -0400 4007) 	struct inode *target = dentry->d_inode;
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4008) 	int error = may_delete(mnt_userns, dir, dentry, 0);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4009) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4010) 	if (error)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4011) 		return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4012) 
acfa4380efe77 (Al Viro                 2008-12-04 10:06:33 -0500 4013) 	if (!dir->i_op->unlink)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4014) 		return -EPERM;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4015) 
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 4016) 	inode_lock(target);
8ed936b5671bf (Eric W. Biederman       2013-10-01 18:33:48 -0700 4017) 	if (is_local_mountpoint(dentry))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4018) 		error = -EBUSY;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4019) 	else {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4020) 		error = security_inode_unlink(dir, dentry);
bec1052e5be6a (Al Viro                 2010-03-03 14:12:08 -0500 4021) 		if (!error) {
5a14696c1795d (J. Bruce Fields         2012-08-28 07:50:40 -0700 4022) 			error = try_break_deleg(target, delegated_inode);
5a14696c1795d (J. Bruce Fields         2012-08-28 07:50:40 -0700 4023) 			if (error)
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 4024) 				goto out;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4025) 			error = dir->i_op->unlink(dir, dentry);
8ed936b5671bf (Eric W. Biederman       2013-10-01 18:33:48 -0700 4026) 			if (!error) {
d83c49f3e36ce (Al Viro                 2010-04-30 17:17:09 -0400 4027) 				dont_mount(dentry);
8ed936b5671bf (Eric W. Biederman       2013-10-01 18:33:48 -0700 4028) 				detach_mounts(dentry);
116b9731ad761 (Amir Goldstein          2019-05-26 17:34:02 +0300 4029) 				fsnotify_unlink(dir, dentry);
8ed936b5671bf (Eric W. Biederman       2013-10-01 18:33:48 -0700 4030) 			}
bec1052e5be6a (Al Viro                 2010-03-03 14:12:08 -0500 4031) 		}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4032) 	}
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 4033) out:
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 4034) 	inode_unlock(target);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4035) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4036) 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4037) 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
9accbb977ab78 (J. Bruce Fields         2012-08-28 07:03:24 -0400 4038) 		fsnotify_link_count(target);
e234f35c54a30 (John McCutchan          2005-08-04 13:07:08 -0700 4039) 		d_delete(dentry);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4040) 	}
0eeca28300df1 (Robert Love             2005-07-12 17:06:03 -0400 4041) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4042) 	return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4043) }
4d359507346a1 (Al Viro                 2014-03-14 12:20:17 -0400 4044) EXPORT_SYMBOL(vfs_unlink);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4045) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4046) /*
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4047)  * Make sure that the actual truncation of the file will occur outside its
1b1dcc1b57a49 (Jes Sorensen            2006-01-09 15:59:24 -0800 4048)  * directory's i_mutex.  Truncate can take a long time if there is a lot of
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4049)  * writeout happening, and we don't want to prevent access to the directory
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4050)  * while waiting on the I/O.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4051)  */
da2f1362c8bdf (Christoph Hellwig       2017-11-04 13:44:45 +0300 4052) long do_unlinkat(int dfd, struct filename *name)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4053) {
2ad94ae654f5e (Al Viro                 2008-07-21 09:32:51 -0400 4054) 	int error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4055) 	struct dentry *dentry;
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4056) 	struct path path;
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4057) 	struct qstr last;
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4058) 	int type;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4059) 	struct inode *inode = NULL;
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 4060) 	struct inode *delegated_inode = NULL;
5d18f8133cad8 (Jeff Layton             2012-12-20 16:38:04 -0500 4061) 	unsigned int lookup_flags = 0;
5d18f8133cad8 (Jeff Layton             2012-12-20 16:38:04 -0500 4062) retry:
da2f1362c8bdf (Christoph Hellwig       2017-11-04 13:44:45 +0300 4063) 	name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
91a27b2a75678 (Jeff Layton             2012-10-10 15:25:28 -0400 4064) 	if (IS_ERR(name))
91a27b2a75678 (Jeff Layton             2012-10-10 15:25:28 -0400 4065) 		return PTR_ERR(name);
2ad94ae654f5e (Al Viro                 2008-07-21 09:32:51 -0400 4066) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4067) 	error = -EISDIR;
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4068) 	if (type != LAST_NORM)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4069) 		goto exit1;
0612d9fb270a4 (OGAWA Hirofumi          2008-10-16 07:50:29 +0900 4070) 
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4071) 	error = mnt_want_write(path.mnt);
c30dabfe5d10c (Jan Kara                2012-06-12 16:20:30 +0200 4072) 	if (error)
c30dabfe5d10c (Jan Kara                2012-06-12 16:20:30 +0200 4073) 		goto exit1;
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 4074) retry_deleg:
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 4075) 	inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4076) 	dentry = __lookup_hash(&last, path.dentry, lookup_flags);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4077) 	error = PTR_ERR(dentry);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4078) 	if (!IS_ERR(dentry)) {
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4079) 		struct user_namespace *mnt_userns;
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4080) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4081) 		/* Why not before? Because we want correct error value */
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4082) 		if (last.name[last.len])
50338b889dc50 (Török Edwin             2011-06-16 00:06:14 +0300 4083) 			goto slashes;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4084) 		inode = dentry->d_inode;
b18825a7c8e37 (David Howells           2013-09-12 19:22:53 +0100 4085) 		if (d_is_negative(dentry))
e6bc45d65df85 (Theodore Ts'o           2011-06-06 19:19:40 -0400 4086) 			goto slashes;
e6bc45d65df85 (Theodore Ts'o           2011-06-06 19:19:40 -0400 4087) 		ihold(inode);
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4088) 		error = security_path_unlink(&path, dentry);
be6d3e56a6b9b (Kentaro Takeda          2008-12-17 13:24:15 +0900 4089) 		if (error)
c30dabfe5d10c (Jan Kara                2012-06-12 16:20:30 +0200 4090) 			goto exit2;
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4091) 		mnt_userns = mnt_user_ns(path.mnt);
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 4092) 		error = vfs_unlink(mnt_userns, path.dentry->d_inode, dentry,
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 4093) 				   &delegated_inode);
c30dabfe5d10c (Jan Kara                2012-06-12 16:20:30 +0200 4094) exit2:
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4095) 		dput(dentry);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4096) 	}
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 4097) 	inode_unlock(path.dentry->d_inode);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4098) 	if (inode)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4099) 		iput(inode);	/* truncate the inode here */
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 4100) 	inode = NULL;
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 4101) 	if (delegated_inode) {
5a14696c1795d (J. Bruce Fields         2012-08-28 07:50:40 -0700 4102) 		error = break_deleg_wait(&delegated_inode);
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 4103) 		if (!error)
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 4104) 			goto retry_deleg;
b21996e36c8e3 (J. Bruce Fields         2011-09-20 09:14:34 -0400 4105) 	}
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4106) 	mnt_drop_write(path.mnt);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4107) exit1:
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4108) 	path_put(&path);
5d18f8133cad8 (Jeff Layton             2012-12-20 16:38:04 -0500 4109) 	if (retry_estale(error, lookup_flags)) {
5d18f8133cad8 (Jeff Layton             2012-12-20 16:38:04 -0500 4110) 		lookup_flags |= LOOKUP_REVAL;
5d18f8133cad8 (Jeff Layton             2012-12-20 16:38:04 -0500 4111) 		inode = NULL;
5d18f8133cad8 (Jeff Layton             2012-12-20 16:38:04 -0500 4112) 		goto retry;
5d18f8133cad8 (Jeff Layton             2012-12-20 16:38:04 -0500 4113) 	}
da2f1362c8bdf (Christoph Hellwig       2017-11-04 13:44:45 +0300 4114) 	putname(name);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4115) 	return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4116) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4117) slashes:
b18825a7c8e37 (David Howells           2013-09-12 19:22:53 +0100 4118) 	if (d_is_negative(dentry))
b18825a7c8e37 (David Howells           2013-09-12 19:22:53 +0100 4119) 		error = -ENOENT;
44b1d53043c48 (Miklos Szeredi          2014-04-01 17:08:41 +0200 4120) 	else if (d_is_dir(dentry))
b18825a7c8e37 (David Howells           2013-09-12 19:22:53 +0100 4121) 		error = -EISDIR;
b18825a7c8e37 (David Howells           2013-09-12 19:22:53 +0100 4122) 	else
b18825a7c8e37 (David Howells           2013-09-12 19:22:53 +0100 4123) 		error = -ENOTDIR;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4124) 	goto exit2;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4125) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4126) 
2e4d0924eb0c4 (Heiko Carstens          2009-01-14 14:14:31 +0100 4127) SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 4128) {
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 4129) 	if ((flag & ~AT_REMOVEDIR) != 0)
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 4130) 		return -EINVAL;
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 4131) 
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 4132) 	if (flag & AT_REMOVEDIR)
e24ab0ef689de (Christoph Hellwig       2020-07-21 10:48:15 +0200 4133) 		return do_rmdir(dfd, getname(pathname));
da2f1362c8bdf (Christoph Hellwig       2017-11-04 13:44:45 +0300 4134) 	return do_unlinkat(dfd, getname(pathname));
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 4135) }
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 4136) 
3480b25743cb7 (Heiko Carstens          2009-01-14 14:14:16 +0100 4137) SYSCALL_DEFINE1(unlink, const char __user *, pathname)
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 4138) {
da2f1362c8bdf (Christoph Hellwig       2017-11-04 13:44:45 +0300 4139) 	return do_unlinkat(AT_FDCWD, getname(pathname));
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 4140) }
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 4141) 
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4142) /**
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4143)  * vfs_symlink - create symlink
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4144)  * @mnt_userns:	user namespace of the mount the inode was found from
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4145)  * @dir:	inode of @dentry
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4146)  * @dentry:	pointer to dentry of the base directory
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4147)  * @oldname:	name of the file to link to
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4148)  *
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4149)  * Create a symlink.
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4150)  *
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4151)  * If the inode has been found through an idmapped mount the user namespace of
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4152)  * the vfsmount must be passed through @mnt_userns. This function will then take
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4153)  * care to map the inode according to @mnt_userns before checking permissions.
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4154)  * On non-idmapped mounts or if permission checking is to be performed on the
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4155)  * raw inode simply passs init_user_ns.
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4156)  */
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4157) int vfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4158) 		struct dentry *dentry, const char *oldname)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4159) {
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4160) 	int error = may_create(mnt_userns, dir, dentry);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4161) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4162) 	if (error)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4163) 		return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4164) 
acfa4380efe77 (Al Viro                 2008-12-04 10:06:33 -0500 4165) 	if (!dir->i_op->symlink)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4166) 		return -EPERM;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4167) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4168) 	error = security_inode_symlink(dir, dentry, oldname);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4169) 	if (error)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4170) 		return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4171) 
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 4172) 	error = dir->i_op->symlink(mnt_userns, dir, dentry, oldname);
a74574aafea3a (Stephen Smalley         2005-09-09 13:01:44 -0700 4173) 	if (!error)
f38aa94224c55 (Amy Griffis             2005-11-03 15:57:06 +0000 4174) 		fsnotify_create(dir, dentry);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4175) 	return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4176) }
4d359507346a1 (Al Viro                 2014-03-14 12:20:17 -0400 4177) EXPORT_SYMBOL(vfs_symlink);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4178) 
cd3acb6a79349 (Christoph Hellwig       2020-07-22 11:14:36 +0200 4179) static long do_symlinkat(const char __user *oldname, int newdfd,
b724e846b491e (Dominik Brodowski       2018-03-11 11:34:49 +0100 4180) 		  const char __user *newname)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4181) {
2ad94ae654f5e (Al Viro                 2008-07-21 09:32:51 -0400 4182) 	int error;
91a27b2a75678 (Jeff Layton             2012-10-10 15:25:28 -0400 4183) 	struct filename *from;
6902d925d568c (Dave Hansen             2006-09-30 23:29:01 -0700 4184) 	struct dentry *dentry;
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 4185) 	struct path path;
f46d3567b223e (Jeff Layton             2012-12-11 12:10:08 -0500 4186) 	unsigned int lookup_flags = 0;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4187) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4188) 	from = getname(oldname);
2ad94ae654f5e (Al Viro                 2008-07-21 09:32:51 -0400 4189) 	if (IS_ERR(from))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4190) 		return PTR_ERR(from);
f46d3567b223e (Jeff Layton             2012-12-11 12:10:08 -0500 4191) retry:
f46d3567b223e (Jeff Layton             2012-12-11 12:10:08 -0500 4192) 	dentry = user_path_create(newdfd, newname, &path, lookup_flags);
6902d925d568c (Dave Hansen             2006-09-30 23:29:01 -0700 4193) 	error = PTR_ERR(dentry);
6902d925d568c (Dave Hansen             2006-09-30 23:29:01 -0700 4194) 	if (IS_ERR(dentry))
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 4195) 		goto out_putname;
6902d925d568c (Dave Hansen             2006-09-30 23:29:01 -0700 4196) 
91a27b2a75678 (Jeff Layton             2012-10-10 15:25:28 -0400 4197) 	error = security_path_symlink(&path, dentry, from->name);
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4198) 	if (!error) {
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4199) 		struct user_namespace *mnt_userns;
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4200) 
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4201) 		mnt_userns = mnt_user_ns(path.mnt);
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4202) 		error = vfs_symlink(mnt_userns, path.dentry->d_inode, dentry,
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4203) 				    from->name);
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4204) 	}
921a1650de9ee (Al Viro                 2012-07-20 01:15:31 +0400 4205) 	done_path_create(&path, dentry);
f46d3567b223e (Jeff Layton             2012-12-11 12:10:08 -0500 4206) 	if (retry_estale(error, lookup_flags)) {
f46d3567b223e (Jeff Layton             2012-12-11 12:10:08 -0500 4207) 		lookup_flags |= LOOKUP_REVAL;
f46d3567b223e (Jeff Layton             2012-12-11 12:10:08 -0500 4208) 		goto retry;
f46d3567b223e (Jeff Layton             2012-12-11 12:10:08 -0500 4209) 	}
6902d925d568c (Dave Hansen             2006-09-30 23:29:01 -0700 4210) out_putname:
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4211) 	putname(from);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4212) 	return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4213) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4214) 
b724e846b491e (Dominik Brodowski       2018-03-11 11:34:49 +0100 4215) SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
b724e846b491e (Dominik Brodowski       2018-03-11 11:34:49 +0100 4216) 		int, newdfd, const char __user *, newname)
b724e846b491e (Dominik Brodowski       2018-03-11 11:34:49 +0100 4217) {
b724e846b491e (Dominik Brodowski       2018-03-11 11:34:49 +0100 4218) 	return do_symlinkat(oldname, newdfd, newname);
b724e846b491e (Dominik Brodowski       2018-03-11 11:34:49 +0100 4219) }
b724e846b491e (Dominik Brodowski       2018-03-11 11:34:49 +0100 4220) 
3480b25743cb7 (Heiko Carstens          2009-01-14 14:14:16 +0100 4221) SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 4222) {
b724e846b491e (Dominik Brodowski       2018-03-11 11:34:49 +0100 4223) 	return do_symlinkat(oldname, AT_FDCWD, newname);
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 4224) }
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 4225) 
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4226) /**
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4227)  * vfs_link - create a new link
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4228)  * @old_dentry:	object to be linked
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4229)  * @mnt_userns:	the user namespace of the mount
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4230)  * @dir:	new parent
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4231)  * @new_dentry:	where to create the new link
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4232)  * @delegated_inode: returns inode needing a delegation break
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4233)  *
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4234)  * The caller must hold dir->i_mutex
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4235)  *
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4236)  * If vfs_link discovers a delegation on the to-be-linked file in need
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4237)  * of breaking, it will return -EWOULDBLOCK and return a reference to the
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4238)  * inode in delegated_inode.  The caller should then break the delegation
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4239)  * and retry.  Because breaking a delegation may take a long time, the
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4240)  * caller should drop the i_mutex before doing so.
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4241)  *
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4242)  * Alternatively, a caller may pass NULL for delegated_inode.  This may
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4243)  * be appropriate for callers that expect the underlying filesystem not
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4244)  * to be NFS exported.
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4245)  *
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4246)  * If the inode has been found through an idmapped mount the user namespace of
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4247)  * the vfsmount must be passed through @mnt_userns. This function will then take
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4248)  * care to map the inode according to @mnt_userns before checking permissions.
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4249)  * On non-idmapped mounts or if permission checking is to be performed on the
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4250)  * raw inode simply passs init_user_ns.
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4251)  */
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4252) int vfs_link(struct dentry *old_dentry, struct user_namespace *mnt_userns,
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4253) 	     struct inode *dir, struct dentry *new_dentry,
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4254) 	     struct inode **delegated_inode)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4255) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4256) 	struct inode *inode = old_dentry->d_inode;
8de52778798fe (Al Viro                 2012-02-06 12:45:27 -0500 4257) 	unsigned max_links = dir->i_sb->s_max_links;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4258) 	int error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4259) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4260) 	if (!inode)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4261) 		return -ENOENT;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4262) 
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4263) 	error = may_create(mnt_userns, dir, new_dentry);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4264) 	if (error)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4265) 		return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4266) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4267) 	if (dir->i_sb != inode->i_sb)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4268) 		return -EXDEV;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4269) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4270) 	/*
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4271) 	 * A link to an append-only or immutable file cannot be created.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4272) 	 */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4273) 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4274) 		return -EPERM;
0bd23d09b874e (Eric W. Biederman       2016-06-29 14:54:46 -0500 4275) 	/*
0bd23d09b874e (Eric W. Biederman       2016-06-29 14:54:46 -0500 4276) 	 * Updating the link count will likely cause i_uid and i_gid to
0bd23d09b874e (Eric W. Biederman       2016-06-29 14:54:46 -0500 4277) 	 * be writen back improperly if their true value is unknown to
0bd23d09b874e (Eric W. Biederman       2016-06-29 14:54:46 -0500 4278) 	 * the vfs.
0bd23d09b874e (Eric W. Biederman       2016-06-29 14:54:46 -0500 4279) 	 */
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4280) 	if (HAS_UNMAPPED_ID(mnt_userns, inode))
0bd23d09b874e (Eric W. Biederman       2016-06-29 14:54:46 -0500 4281) 		return -EPERM;
acfa4380efe77 (Al Viro                 2008-12-04 10:06:33 -0500 4282) 	if (!dir->i_op->link)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4283) 		return -EPERM;
7e79eedb3b222 (Tetsuo Handa            2008-06-24 16:50:15 +0200 4284) 	if (S_ISDIR(inode->i_mode))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4285) 		return -EPERM;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4286) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4287) 	error = security_inode_link(old_dentry, dir, new_dentry);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4288) 	if (error)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4289) 		return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4290) 
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 4291) 	inode_lock(inode);
aae8a97d3ec30 (Aneesh Kumar K.V        2011-01-29 18:43:27 +0530 4292) 	/* Make sure we don't allow creating hardlink to an unlinked file */
f4e0c30c191f8 (Al Viro                 2013-06-11 08:34:36 +0400 4293) 	if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
aae8a97d3ec30 (Aneesh Kumar K.V        2011-01-29 18:43:27 +0530 4294) 		error =  -ENOENT;
8de52778798fe (Al Viro                 2012-02-06 12:45:27 -0500 4295) 	else if (max_links && inode->i_nlink >= max_links)
8de52778798fe (Al Viro                 2012-02-06 12:45:27 -0500 4296) 		error = -EMLINK;
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4297) 	else {
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4298) 		error = try_break_deleg(inode, delegated_inode);
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4299) 		if (!error)
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4300) 			error = dir->i_op->link(old_dentry, dir, new_dentry);
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4301) 	}
f4e0c30c191f8 (Al Viro                 2013-06-11 08:34:36 +0400 4302) 
f4e0c30c191f8 (Al Viro                 2013-06-11 08:34:36 +0400 4303) 	if (!error && (inode->i_state & I_LINKABLE)) {
f4e0c30c191f8 (Al Viro                 2013-06-11 08:34:36 +0400 4304) 		spin_lock(&inode->i_lock);
f4e0c30c191f8 (Al Viro                 2013-06-11 08:34:36 +0400 4305) 		inode->i_state &= ~I_LINKABLE;
f4e0c30c191f8 (Al Viro                 2013-06-11 08:34:36 +0400 4306) 		spin_unlock(&inode->i_lock);
f4e0c30c191f8 (Al Viro                 2013-06-11 08:34:36 +0400 4307) 	}
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 4308) 	inode_unlock(inode);
e31e14ec356f3 (Stephen Smalley         2005-09-09 13:01:45 -0700 4309) 	if (!error)
7e79eedb3b222 (Tetsuo Handa            2008-06-24 16:50:15 +0200 4310) 		fsnotify_link(dir, inode, new_dentry);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4311) 	return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4312) }
4d359507346a1 (Al Viro                 2014-03-14 12:20:17 -0400 4313) EXPORT_SYMBOL(vfs_link);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4314) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4315) /*
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4316)  * Hardlinks are often used in delicate situations.  We avoid
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4317)  * security-related surprises by not following symlinks on the
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4318)  * newname.  --KAB
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4319)  *
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4320)  * We don't follow them on the oldname either to be compatible
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4321)  * with linux 2.0, and to avoid hard-linking to directories
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4322)  * and other special files.  --ADM
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4323)  */
812931d693da5 (Christoph Hellwig       2020-07-22 11:14:19 +0200 4324) static int do_linkat(int olddfd, const char __user *oldname, int newdfd,
46ea89eb652a3 (Dominik Brodowski       2018-03-11 11:34:53 +0100 4325) 	      const char __user *newname, int flags)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4326) {
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4327) 	struct user_namespace *mnt_userns;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4328) 	struct dentry *new_dentry;
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 4329) 	struct path old_path, new_path;
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4330) 	struct inode *delegated_inode = NULL;
11a7b371b64ef (Aneesh Kumar K.V        2011-01-29 18:43:42 +0530 4331) 	int how = 0;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4332) 	int error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4333) 
11a7b371b64ef (Aneesh Kumar K.V        2011-01-29 18:43:42 +0530 4334) 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
c04030e16dbea (Ulrich Drepper          2006-02-24 13:04:21 -0800 4335) 		return -EINVAL;
11a7b371b64ef (Aneesh Kumar K.V        2011-01-29 18:43:42 +0530 4336) 	/*
f0cc6ffb8ce89 (Linus Torvalds          2013-08-28 09:18:05 -0700 4337) 	 * To use null names we require CAP_DAC_READ_SEARCH
f0cc6ffb8ce89 (Linus Torvalds          2013-08-28 09:18:05 -0700 4338) 	 * This ensures that not everyone will be able to create
f0cc6ffb8ce89 (Linus Torvalds          2013-08-28 09:18:05 -0700 4339) 	 * handlink using the passed filedescriptor.
11a7b371b64ef (Aneesh Kumar K.V        2011-01-29 18:43:42 +0530 4340) 	 */
f0cc6ffb8ce89 (Linus Torvalds          2013-08-28 09:18:05 -0700 4341) 	if (flags & AT_EMPTY_PATH) {
f0cc6ffb8ce89 (Linus Torvalds          2013-08-28 09:18:05 -0700 4342) 		if (!capable(CAP_DAC_READ_SEARCH))
f0cc6ffb8ce89 (Linus Torvalds          2013-08-28 09:18:05 -0700 4343) 			return -ENOENT;
11a7b371b64ef (Aneesh Kumar K.V        2011-01-29 18:43:42 +0530 4344) 		how = LOOKUP_EMPTY;
f0cc6ffb8ce89 (Linus Torvalds          2013-08-28 09:18:05 -0700 4345) 	}
11a7b371b64ef (Aneesh Kumar K.V        2011-01-29 18:43:42 +0530 4346) 
11a7b371b64ef (Aneesh Kumar K.V        2011-01-29 18:43:42 +0530 4347) 	if (flags & AT_SYMLINK_FOLLOW)
11a7b371b64ef (Aneesh Kumar K.V        2011-01-29 18:43:42 +0530 4348) 		how |= LOOKUP_FOLLOW;
442e31ca5a49e (Jeff Layton             2012-12-20 16:15:38 -0500 4349) retry:
11a7b371b64ef (Aneesh Kumar K.V        2011-01-29 18:43:42 +0530 4350) 	error = user_path_at(olddfd, oldname, how, &old_path);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4351) 	if (error)
2ad94ae654f5e (Al Viro                 2008-07-21 09:32:51 -0400 4352) 		return error;
2ad94ae654f5e (Al Viro                 2008-07-21 09:32:51 -0400 4353) 
442e31ca5a49e (Jeff Layton             2012-12-20 16:15:38 -0500 4354) 	new_dentry = user_path_create(newdfd, newname, &new_path,
442e31ca5a49e (Jeff Layton             2012-12-20 16:15:38 -0500 4355) 					(how & LOOKUP_REVAL));
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4356) 	error = PTR_ERR(new_dentry);
6902d925d568c (Dave Hansen             2006-09-30 23:29:01 -0700 4357) 	if (IS_ERR(new_dentry))
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 4358) 		goto out;
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 4359) 
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 4360) 	error = -EXDEV;
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 4361) 	if (old_path.mnt != new_path.mnt)
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 4362) 		goto out_dput;
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 4363) 	mnt_userns = mnt_user_ns(new_path.mnt);
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 4364) 	error = may_linkat(mnt_userns, &old_path);
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 4365) 	if (unlikely(error))
800179c9b8a1e (Kees Cook               2012-07-25 17:29:07 -0700 4366) 		goto out_dput;
dae6ad8f37529 (Al Viro                 2011-06-26 11:50:15 -0400 4367) 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
be6d3e56a6b9b (Kentaro Takeda          2008-12-17 13:24:15 +0900 4368) 	if (error)
a8104a9fcdeb8 (Al Viro                 2012-07-20 02:25:00 +0400 4369) 		goto out_dput;
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4370) 	error = vfs_link(old_path.dentry, mnt_userns, new_path.dentry->d_inode,
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4371) 			 new_dentry, &delegated_inode);
75c3f29de7451 (Dave Hansen             2008-02-15 14:37:45 -0800 4372) out_dput:
921a1650de9ee (Al Viro                 2012-07-20 01:15:31 +0400 4373) 	done_path_create(&new_path, new_dentry);
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4374) 	if (delegated_inode) {
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4375) 		error = break_deleg_wait(&delegated_inode);
d22e6338db7f6 (Oleg Drokin             2014-01-31 15:41:58 -0500 4376) 		if (!error) {
d22e6338db7f6 (Oleg Drokin             2014-01-31 15:41:58 -0500 4377) 			path_put(&old_path);
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4378) 			goto retry;
d22e6338db7f6 (Oleg Drokin             2014-01-31 15:41:58 -0500 4379) 		}
146a8595c6399 (J. Bruce Fields         2011-09-20 17:14:31 -0400 4380) 	}
442e31ca5a49e (Jeff Layton             2012-12-20 16:15:38 -0500 4381) 	if (retry_estale(error, how)) {
d22e6338db7f6 (Oleg Drokin             2014-01-31 15:41:58 -0500 4382) 		path_put(&old_path);
442e31ca5a49e (Jeff Layton             2012-12-20 16:15:38 -0500 4383) 		how |= LOOKUP_REVAL;
442e31ca5a49e (Jeff Layton             2012-12-20 16:15:38 -0500 4384) 		goto retry;
442e31ca5a49e (Jeff Layton             2012-12-20 16:15:38 -0500 4385) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4386) out:
2d8f30380ab8c (Al Viro                 2008-07-22 09:59:21 -0400 4387) 	path_put(&old_path);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4388) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4389) 	return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4390) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4391) 
46ea89eb652a3 (Dominik Brodowski       2018-03-11 11:34:53 +0100 4392) SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
46ea89eb652a3 (Dominik Brodowski       2018-03-11 11:34:53 +0100 4393) 		int, newdfd, const char __user *, newname, int, flags)
46ea89eb652a3 (Dominik Brodowski       2018-03-11 11:34:53 +0100 4394) {
46ea89eb652a3 (Dominik Brodowski       2018-03-11 11:34:53 +0100 4395) 	return do_linkat(olddfd, oldname, newdfd, newname, flags);
46ea89eb652a3 (Dominik Brodowski       2018-03-11 11:34:53 +0100 4396) }
46ea89eb652a3 (Dominik Brodowski       2018-03-11 11:34:53 +0100 4397) 
3480b25743cb7 (Heiko Carstens          2009-01-14 14:14:16 +0100 4398) SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 4399) {
46ea89eb652a3 (Dominik Brodowski       2018-03-11 11:34:53 +0100 4400) 	return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 4401) }
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 4402) 
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4403) /**
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4404)  * vfs_rename - rename a filesystem object
2111c3c0124f7 (Randy Dunlap            2021-02-15 20:29:28 -0800 4405)  * @rd:		pointer to &struct renamedata info
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4406)  *
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4407)  * The caller must hold multiple mutexes--see lock_rename()).
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4408)  *
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4409)  * If vfs_rename discovers a delegation in need of breaking at either
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4410)  * the source or destination, it will return -EWOULDBLOCK and return a
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4411)  * reference to the inode in delegated_inode.  The caller should then
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4412)  * break the delegation and retry.  Because breaking a delegation may
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4413)  * take a long time, the caller should drop all locks before doing
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4414)  * so.
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4415)  *
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4416)  * Alternatively, a caller may pass NULL for delegated_inode.  This may
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4417)  * be appropriate for callers that expect the underlying filesystem not
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4418)  * to be NFS exported.
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4419)  *
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4420)  * The worst of all namespace operations - renaming directory. "Perverted"
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4421)  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4422)  * Problems:
0117d4272b1ac (Mauro Carvalho Chehab   2017-05-12 07:45:42 -0300 4423)  *
d03b29a271eb1 (J. Bruce Fields         2014-02-17 16:52:33 -0500 4424)  *	a) we can get into loop creation.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4425)  *	b) race potential - two innocent renames can create a loop together.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4426)  *	   That's where 4.4 screws up. Current fix: serialization on
a11f3a0574a57 (Arjan van de Ven        2006-03-23 03:00:33 -0800 4427)  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4428)  *	   story.
6cedba8962f44 (J. Bruce Fields         2012-03-05 11:40:41 -0500 4429)  *	c) we have to lock _four_ objects - parents and victim (if it exists),
6cedba8962f44 (J. Bruce Fields         2012-03-05 11:40:41 -0500 4430)  *	   and source (if it is not a directory).
1b1dcc1b57a49 (Jes Sorensen            2006-01-09 15:59:24 -0800 4431)  *	   And that - after we got ->i_mutex on parents (until then we don't know
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4432)  *	   whether the target exists).  Solution: try to be smart with locking
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4433)  *	   order for inodes.  We rely on the fact that tree topology may change
a11f3a0574a57 (Arjan van de Ven        2006-03-23 03:00:33 -0800 4434)  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4435)  *	   move will be locked.  Thus we can rank directories by the tree
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4436)  *	   (ancestors first) and rank all non-directories after them.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4437)  *	   That works since everybody except rename does "lock parent, lookup,
a11f3a0574a57 (Arjan van de Ven        2006-03-23 03:00:33 -0800 4438)  *	   lock child" and rename is under ->s_vfs_rename_mutex.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4439)  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4440)  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4441)  *	   we'd better make sure that there's no link(2) for them.
e4eaac06bcccb (Sage Weil               2011-05-24 13:06:07 -0700 4442)  *	d) conversion from fhandle to dentry may come in the wrong moment - when
1b1dcc1b57a49 (Jes Sorensen            2006-01-09 15:59:24 -0800 4443)  *	   we are removing the target. Solution: we will have to grab ->i_mutex
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4444)  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
c41b20e721ea4 (Adam Buchbinder         2009-12-11 16:35:39 -0500 4445)  *	   ->i_mutex on parents, which works but leads to some truly excessive
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4446)  *	   locking].
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4447)  */
9fe61450972d3 (Christian Brauner       2021-01-21 14:19:32 +0100 4448) int vfs_rename(struct renamedata *rd)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4449) {
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4450) 	int error;
9fe61450972d3 (Christian Brauner       2021-01-21 14:19:32 +0100 4451) 	struct inode *old_dir = rd->old_dir, *new_dir = rd->new_dir;
9fe61450972d3 (Christian Brauner       2021-01-21 14:19:32 +0100 4452) 	struct dentry *old_dentry = rd->old_dentry;
9fe61450972d3 (Christian Brauner       2021-01-21 14:19:32 +0100 4453) 	struct dentry *new_dentry = rd->new_dentry;
9fe61450972d3 (Christian Brauner       2021-01-21 14:19:32 +0100 4454) 	struct inode **delegated_inode = rd->delegated_inode;
9fe61450972d3 (Christian Brauner       2021-01-21 14:19:32 +0100 4455) 	unsigned int flags = rd->flags;
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4456) 	bool is_dir = d_is_dir(old_dentry);
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4457) 	struct inode *source = old_dentry->d_inode;
9055cba711891 (Sage Weil               2011-05-24 13:06:12 -0700 4458) 	struct inode *target = new_dentry->d_inode;
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4459) 	bool new_is_dir = false;
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4460) 	unsigned max_links = new_dir->i_sb->s_max_links;
49d31c2f389ac (Al Viro                 2017-07-07 14:51:19 -0400 4461) 	struct name_snapshot old_name;
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4462) 
8d3e2936375ba (Miklos Szeredi          2016-12-16 11:02:54 +0100 4463) 	if (source == target)
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4464) 		return 0;
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4465) 
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4466) 	error = may_delete(rd->old_mnt_userns, old_dir, old_dentry, is_dir);
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4467) 	if (error)
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4468) 		return error;
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4469) 
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4470) 	if (!target) {
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4471) 		error = may_create(rd->new_mnt_userns, new_dir, new_dentry);
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4472) 	} else {
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4473) 		new_is_dir = d_is_dir(new_dentry);
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4474) 
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4475) 		if (!(flags & RENAME_EXCHANGE))
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4476) 			error = may_delete(rd->new_mnt_userns, new_dir,
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4477) 					   new_dentry, is_dir);
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4478) 		else
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4479) 			error = may_delete(rd->new_mnt_userns, new_dir,
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4480) 					   new_dentry, new_is_dir);
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4481) 	}
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4482) 	if (error)
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4483) 		return error;
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4484) 
2773bf00aeb9b (Miklos Szeredi          2016-09-27 11:03:58 +0200 4485) 	if (!old_dir->i_op->rename)
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4486) 		return -EPERM;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4487) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4488) 	/*
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4489) 	 * If we are going to change the parent - check write permissions,
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4490) 	 * we'll need to flip '..'.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4491) 	 */
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4492) 	if (new_dir != old_dir) {
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4493) 		if (is_dir) {
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4494) 			error = inode_permission(rd->old_mnt_userns, source,
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100 4495) 						 MAY_WRITE);
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4496) 			if (error)
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4497) 				return error;
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4498) 		}
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4499) 		if ((flags & RENAME_EXCHANGE) && new_is_dir) {
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4500) 			error = inode_permission(rd->new_mnt_userns, target,
47291baa8ddfd (Christian Brauner       2021-01-21 14:19:24 +0100 4501) 						 MAY_WRITE);
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4502) 			if (error)
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4503) 				return error;
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4504) 		}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4505) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4506) 
0b3974eb04c48 (Miklos Szeredi          2014-04-01 17:08:43 +0200 4507) 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
0b3974eb04c48 (Miklos Szeredi          2014-04-01 17:08:43 +0200 4508) 				      flags);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4509) 	if (error)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4510) 		return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4511) 
49d31c2f389ac (Al Viro                 2017-07-07 14:51:19 -0400 4512) 	take_dentry_name_snapshot(&old_name, old_dentry);
1d2ef59014830 (Al Viro                 2011-09-14 18:55:41 +0100 4513) 	dget(new_dentry);
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4514) 	if (!is_dir || (flags & RENAME_EXCHANGE))
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4515) 		lock_two_nondirectories(source, target);
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4516) 	else if (target)
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 4517) 		inode_lock(target);
9055cba711891 (Sage Weil               2011-05-24 13:06:12 -0700 4518) 
9055cba711891 (Sage Weil               2011-05-24 13:06:12 -0700 4519) 	error = -EBUSY;
7af1364ffa64d (Eric W. Biederman       2013-10-04 19:15:13 -0700 4520) 	if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
9055cba711891 (Sage Weil               2011-05-24 13:06:12 -0700 4521) 		goto out;
9055cba711891 (Sage Weil               2011-05-24 13:06:12 -0700 4522) 
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4523) 	if (max_links && new_dir != old_dir) {
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4524) 		error = -EMLINK;
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4525) 		if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4526) 			goto out;
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4527) 		if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4528) 		    old_dir->i_nlink >= max_links)
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4529) 			goto out;
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4530) 	}
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4531) 	if (!is_dir) {
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4532) 		error = try_break_deleg(source, delegated_inode);
8e6d782cab508 (J. Bruce Fields         2011-09-20 16:59:58 -0400 4533) 		if (error)
8e6d782cab508 (J. Bruce Fields         2011-09-20 16:59:58 -0400 4534) 			goto out;
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4535) 	}
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4536) 	if (target && !new_is_dir) {
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4537) 		error = try_break_deleg(target, delegated_inode);
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4538) 		if (error)
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4539) 			goto out;
8e6d782cab508 (J. Bruce Fields         2011-09-20 16:59:58 -0400 4540) 	}
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 4541) 	error = old_dir->i_op->rename(rd->new_mnt_userns, old_dir, old_dentry,
549c7297717c3 (Christian Brauner       2021-01-21 14:19:43 +0100 4542) 				      new_dir, new_dentry, flags);
51892bbb57e87 (Sage Weil               2011-05-24 13:06:13 -0700 4543) 	if (error)
51892bbb57e87 (Sage Weil               2011-05-24 13:06:13 -0700 4544) 		goto out;
51892bbb57e87 (Sage Weil               2011-05-24 13:06:13 -0700 4545) 
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4546) 	if (!(flags & RENAME_EXCHANGE) && target) {
8767712f26d18 (Al Viro                 2018-05-27 16:23:51 -0400 4547) 		if (is_dir) {
8767712f26d18 (Al Viro                 2018-05-27 16:23:51 -0400 4548) 			shrink_dcache_parent(new_dentry);
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4549) 			target->i_flags |= S_DEAD;
8767712f26d18 (Al Viro                 2018-05-27 16:23:51 -0400 4550) 		}
51892bbb57e87 (Sage Weil               2011-05-24 13:06:13 -0700 4551) 		dont_mount(new_dentry);
8ed936b5671bf (Eric W. Biederman       2013-10-01 18:33:48 -0700 4552) 		detach_mounts(new_dentry);
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4553) 	}
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4554) 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4555) 		if (!(flags & RENAME_EXCHANGE))
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4556) 			d_move(old_dentry, new_dentry);
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4557) 		else
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4558) 			d_exchange(old_dentry, new_dentry);
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4559) 	}
51892bbb57e87 (Sage Weil               2011-05-24 13:06:13 -0700 4560) out:
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4561) 	if (!is_dir || (flags & RENAME_EXCHANGE))
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4562) 		unlock_two_nondirectories(source, target);
bc27027a73e8b (Miklos Szeredi          2014-04-01 17:08:42 +0200 4563) 	else if (target)
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 4564) 		inode_unlock(target);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4565) 	dput(new_dentry);
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4566) 	if (!error) {
f4ec3a3d43bcd (Al Viro                 2019-04-26 13:21:24 -0400 4567) 		fsnotify_move(old_dir, new_dir, &old_name.name, is_dir,
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4568) 			      !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4569) 		if (flags & RENAME_EXCHANGE) {
f4ec3a3d43bcd (Al Viro                 2019-04-26 13:21:24 -0400 4570) 			fsnotify_move(new_dir, old_dir, &old_dentry->d_name,
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4571) 				      new_is_dir, NULL, new_dentry);
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4572) 		}
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4573) 	}
49d31c2f389ac (Al Viro                 2017-07-07 14:51:19 -0400 4574) 	release_dentry_name_snapshot(&old_name);
0eeca28300df1 (Robert Love             2005-07-12 17:06:03 -0400 4575) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4576) 	return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4577) }
4d359507346a1 (Al Viro                 2014-03-14 12:20:17 -0400 4578) EXPORT_SYMBOL(vfs_rename);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4579) 
e886663cfd029 (Jens Axboe              2020-09-26 17:20:17 -0600 4580) int do_renameat2(int olddfd, struct filename *from, int newdfd,
e886663cfd029 (Jens Axboe              2020-09-26 17:20:17 -0600 4581) 		 struct filename *to, unsigned int flags)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4582) {
9fe61450972d3 (Christian Brauner       2021-01-21 14:19:32 +0100 4583) 	struct renamedata rd;
2ad94ae654f5e (Al Viro                 2008-07-21 09:32:51 -0400 4584) 	struct dentry *old_dentry, *new_dentry;
2ad94ae654f5e (Al Viro                 2008-07-21 09:32:51 -0400 4585) 	struct dentry *trap;
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4586) 	struct path old_path, new_path;
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4587) 	struct qstr old_last, new_last;
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4588) 	int old_type, new_type;
8e6d782cab508 (J. Bruce Fields         2011-09-20 16:59:58 -0400 4589) 	struct inode *delegated_inode = NULL;
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4590) 	unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
c6a9428401c00 (Jeff Layton             2012-12-11 12:10:10 -0500 4591) 	bool should_retry = false;
e886663cfd029 (Jens Axboe              2020-09-26 17:20:17 -0600 4592) 	int error = -EINVAL;
520c8b1650523 (Miklos Szeredi          2014-04-01 17:08:42 +0200 4593) 
0d7a855526dd6 (Miklos Szeredi          2014-10-24 00:14:37 +0200 4594) 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
e886663cfd029 (Jens Axboe              2020-09-26 17:20:17 -0600 4595) 		goto put_both;
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4596) 
0d7a855526dd6 (Miklos Szeredi          2014-10-24 00:14:37 +0200 4597) 	if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
0d7a855526dd6 (Miklos Szeredi          2014-10-24 00:14:37 +0200 4598) 	    (flags & RENAME_EXCHANGE))
e886663cfd029 (Jens Axboe              2020-09-26 17:20:17 -0600 4599) 		goto put_both;
520c8b1650523 (Miklos Szeredi          2014-04-01 17:08:42 +0200 4600) 
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4601) 	if (flags & RENAME_EXCHANGE)
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4602) 		target_flags = 0;
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4603) 
c6a9428401c00 (Jeff Layton             2012-12-11 12:10:10 -0500 4604) retry:
e886663cfd029 (Jens Axboe              2020-09-26 17:20:17 -0600 4605) 	from = filename_parentat(olddfd, from, lookup_flags, &old_path,
e886663cfd029 (Jens Axboe              2020-09-26 17:20:17 -0600 4606) 					&old_last, &old_type);
91a27b2a75678 (Jeff Layton             2012-10-10 15:25:28 -0400 4607) 	if (IS_ERR(from)) {
91a27b2a75678 (Jeff Layton             2012-10-10 15:25:28 -0400 4608) 		error = PTR_ERR(from);
e886663cfd029 (Jens Axboe              2020-09-26 17:20:17 -0600 4609) 		goto put_new;
91a27b2a75678 (Jeff Layton             2012-10-10 15:25:28 -0400 4610) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4611) 
e886663cfd029 (Jens Axboe              2020-09-26 17:20:17 -0600 4612) 	to = filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last,
e886663cfd029 (Jens Axboe              2020-09-26 17:20:17 -0600 4613) 				&new_type);
91a27b2a75678 (Jeff Layton             2012-10-10 15:25:28 -0400 4614) 	if (IS_ERR(to)) {
91a27b2a75678 (Jeff Layton             2012-10-10 15:25:28 -0400 4615) 		error = PTR_ERR(to);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4616) 		goto exit1;
91a27b2a75678 (Jeff Layton             2012-10-10 15:25:28 -0400 4617) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4618) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4619) 	error = -EXDEV;
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4620) 	if (old_path.mnt != new_path.mnt)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4621) 		goto exit2;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4622) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4623) 	error = -EBUSY;
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4624) 	if (old_type != LAST_NORM)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4625) 		goto exit2;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4626) 
0a7c3937a1f23 (Miklos Szeredi          2014-04-01 17:08:43 +0200 4627) 	if (flags & RENAME_NOREPLACE)
0a7c3937a1f23 (Miklos Szeredi          2014-04-01 17:08:43 +0200 4628) 		error = -EEXIST;
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4629) 	if (new_type != LAST_NORM)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4630) 		goto exit2;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4631) 
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4632) 	error = mnt_want_write(old_path.mnt);
c30dabfe5d10c (Jan Kara                2012-06-12 16:20:30 +0200 4633) 	if (error)
c30dabfe5d10c (Jan Kara                2012-06-12 16:20:30 +0200 4634) 		goto exit2;
c30dabfe5d10c (Jan Kara                2012-06-12 16:20:30 +0200 4635) 
8e6d782cab508 (J. Bruce Fields         2011-09-20 16:59:58 -0400 4636) retry_deleg:
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4637) 	trap = lock_rename(new_path.dentry, old_path.dentry);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4638) 
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4639) 	old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4640) 	error = PTR_ERR(old_dentry);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4641) 	if (IS_ERR(old_dentry))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4642) 		goto exit3;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4643) 	/* source must exist */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4644) 	error = -ENOENT;
b18825a7c8e37 (David Howells           2013-09-12 19:22:53 +0100 4645) 	if (d_is_negative(old_dentry))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4646) 		goto exit4;
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4647) 	new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
0a7c3937a1f23 (Miklos Szeredi          2014-04-01 17:08:43 +0200 4648) 	error = PTR_ERR(new_dentry);
0a7c3937a1f23 (Miklos Szeredi          2014-04-01 17:08:43 +0200 4649) 	if (IS_ERR(new_dentry))
0a7c3937a1f23 (Miklos Szeredi          2014-04-01 17:08:43 +0200 4650) 		goto exit4;
0a7c3937a1f23 (Miklos Szeredi          2014-04-01 17:08:43 +0200 4651) 	error = -EEXIST;
0a7c3937a1f23 (Miklos Szeredi          2014-04-01 17:08:43 +0200 4652) 	if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
0a7c3937a1f23 (Miklos Szeredi          2014-04-01 17:08:43 +0200 4653) 		goto exit5;
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4654) 	if (flags & RENAME_EXCHANGE) {
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4655) 		error = -ENOENT;
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4656) 		if (d_is_negative(new_dentry))
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4657) 			goto exit5;
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4658) 
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4659) 		if (!d_is_dir(new_dentry)) {
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4660) 			error = -ENOTDIR;
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4661) 			if (new_last.name[new_last.len])
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4662) 				goto exit5;
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4663) 		}
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4664) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4665) 	/* unless the source is a directory trailing slashes give -ENOTDIR */
44b1d53043c48 (Miklos Szeredi          2014-04-01 17:08:41 +0200 4666) 	if (!d_is_dir(old_dentry)) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4667) 		error = -ENOTDIR;
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4668) 		if (old_last.name[old_last.len])
0a7c3937a1f23 (Miklos Szeredi          2014-04-01 17:08:43 +0200 4669) 			goto exit5;
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4670) 		if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
0a7c3937a1f23 (Miklos Szeredi          2014-04-01 17:08:43 +0200 4671) 			goto exit5;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4672) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4673) 	/* source should not be ancestor of target */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4674) 	error = -EINVAL;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4675) 	if (old_dentry == trap)
0a7c3937a1f23 (Miklos Szeredi          2014-04-01 17:08:43 +0200 4676) 		goto exit5;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4677) 	/* target should not be an ancestor of source */
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4678) 	if (!(flags & RENAME_EXCHANGE))
da1ce0670c14d (Miklos Szeredi          2014-04-01 17:08:43 +0200 4679) 		error = -ENOTEMPTY;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4680) 	if (new_dentry == trap)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4681) 		goto exit5;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4682) 
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4683) 	error = security_path_rename(&old_path, old_dentry,
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4684) 				     &new_path, new_dentry, flags);
be6d3e56a6b9b (Kentaro Takeda          2008-12-17 13:24:15 +0900 4685) 	if (error)
c30dabfe5d10c (Jan Kara                2012-06-12 16:20:30 +0200 4686) 		goto exit5;
9fe61450972d3 (Christian Brauner       2021-01-21 14:19:32 +0100 4687) 
9fe61450972d3 (Christian Brauner       2021-01-21 14:19:32 +0100 4688) 	rd.old_dir	   = old_path.dentry->d_inode;
9fe61450972d3 (Christian Brauner       2021-01-21 14:19:32 +0100 4689) 	rd.old_dentry	   = old_dentry;
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4690) 	rd.old_mnt_userns  = mnt_user_ns(old_path.mnt);
9fe61450972d3 (Christian Brauner       2021-01-21 14:19:32 +0100 4691) 	rd.new_dir	   = new_path.dentry->d_inode;
9fe61450972d3 (Christian Brauner       2021-01-21 14:19:32 +0100 4692) 	rd.new_dentry	   = new_dentry;
6521f89170829 (Christian Brauner       2021-01-21 14:19:33 +0100 4693) 	rd.new_mnt_userns  = mnt_user_ns(new_path.mnt);
9fe61450972d3 (Christian Brauner       2021-01-21 14:19:32 +0100 4694) 	rd.delegated_inode = &delegated_inode;
9fe61450972d3 (Christian Brauner       2021-01-21 14:19:32 +0100 4695) 	rd.flags	   = flags;
9fe61450972d3 (Christian Brauner       2021-01-21 14:19:32 +0100 4696) 	error = vfs_rename(&rd);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4697) exit5:
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4698) 	dput(new_dentry);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4699) exit4:
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4700) 	dput(old_dentry);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4701) exit3:
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4702) 	unlock_rename(new_path.dentry, old_path.dentry);
8e6d782cab508 (J. Bruce Fields         2011-09-20 16:59:58 -0400 4703) 	if (delegated_inode) {
8e6d782cab508 (J. Bruce Fields         2011-09-20 16:59:58 -0400 4704) 		error = break_deleg_wait(&delegated_inode);
8e6d782cab508 (J. Bruce Fields         2011-09-20 16:59:58 -0400 4705) 		if (!error)
8e6d782cab508 (J. Bruce Fields         2011-09-20 16:59:58 -0400 4706) 			goto retry_deleg;
8e6d782cab508 (J. Bruce Fields         2011-09-20 16:59:58 -0400 4707) 	}
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4708) 	mnt_drop_write(old_path.mnt);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4709) exit2:
c6a9428401c00 (Jeff Layton             2012-12-11 12:10:10 -0500 4710) 	if (retry_estale(error, lookup_flags))
c6a9428401c00 (Jeff Layton             2012-12-11 12:10:10 -0500 4711) 		should_retry = true;
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4712) 	path_put(&new_path);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4713) exit1:
f5beed755bce1 (Al Viro                 2015-04-30 16:09:11 -0400 4714) 	path_put(&old_path);
c6a9428401c00 (Jeff Layton             2012-12-11 12:10:10 -0500 4715) 	if (should_retry) {
c6a9428401c00 (Jeff Layton             2012-12-11 12:10:10 -0500 4716) 		should_retry = false;
c6a9428401c00 (Jeff Layton             2012-12-11 12:10:10 -0500 4717) 		lookup_flags |= LOOKUP_REVAL;
c6a9428401c00 (Jeff Layton             2012-12-11 12:10:10 -0500 4718) 		goto retry;
c6a9428401c00 (Jeff Layton             2012-12-11 12:10:10 -0500 4719) 	}
e886663cfd029 (Jens Axboe              2020-09-26 17:20:17 -0600 4720) put_both:
e886663cfd029 (Jens Axboe              2020-09-26 17:20:17 -0600 4721) 	if (!IS_ERR(from))
e886663cfd029 (Jens Axboe              2020-09-26 17:20:17 -0600 4722) 		putname(from);
e886663cfd029 (Jens Axboe              2020-09-26 17:20:17 -0600 4723) put_new:
e886663cfd029 (Jens Axboe              2020-09-26 17:20:17 -0600 4724) 	if (!IS_ERR(to))
e886663cfd029 (Jens Axboe              2020-09-26 17:20:17 -0600 4725) 		putname(to);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4726) 	return error;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4727) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4728) 
ee81feb64ead8 (Dominik Brodowski       2018-03-11 11:34:28 +0100 4729) SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
ee81feb64ead8 (Dominik Brodowski       2018-03-11 11:34:28 +0100 4730) 		int, newdfd, const char __user *, newname, unsigned int, flags)
ee81feb64ead8 (Dominik Brodowski       2018-03-11 11:34:28 +0100 4731) {
e886663cfd029 (Jens Axboe              2020-09-26 17:20:17 -0600 4732) 	return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname),
e886663cfd029 (Jens Axboe              2020-09-26 17:20:17 -0600 4733) 				flags);
ee81feb64ead8 (Dominik Brodowski       2018-03-11 11:34:28 +0100 4734) }
ee81feb64ead8 (Dominik Brodowski       2018-03-11 11:34:28 +0100 4735) 
520c8b1650523 (Miklos Szeredi          2014-04-01 17:08:42 +0200 4736) SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
520c8b1650523 (Miklos Szeredi          2014-04-01 17:08:42 +0200 4737) 		int, newdfd, const char __user *, newname)
520c8b1650523 (Miklos Szeredi          2014-04-01 17:08:42 +0200 4738) {
e886663cfd029 (Jens Axboe              2020-09-26 17:20:17 -0600 4739) 	return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname),
e886663cfd029 (Jens Axboe              2020-09-26 17:20:17 -0600 4740) 				0);
520c8b1650523 (Miklos Szeredi          2014-04-01 17:08:42 +0200 4741) }
520c8b1650523 (Miklos Szeredi          2014-04-01 17:08:42 +0200 4742) 
a26eab2400f04 (Heiko Carstens          2009-01-14 14:14:17 +0100 4743) SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 4744) {
e886663cfd029 (Jens Axboe              2020-09-26 17:20:17 -0600 4745) 	return do_renameat2(AT_FDCWD, getname(oldname), AT_FDCWD,
e886663cfd029 (Jens Axboe              2020-09-26 17:20:17 -0600 4746) 				getname(newname), 0);
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 4747) }
5590ff0d5528b (Ulrich Drepper          2006-01-18 17:43:53 -0800 4748) 
5d826c847b34d (Al Viro                 2014-03-14 13:42:45 -0400 4749) int readlink_copy(char __user *buffer, int buflen, const char *link)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4750) {
5d826c847b34d (Al Viro                 2014-03-14 13:42:45 -0400 4751) 	int len = PTR_ERR(link);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4752) 	if (IS_ERR(link))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4753) 		goto out;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4754) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4755) 	len = strlen(link);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4756) 	if (len > (unsigned) buflen)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4757) 		len = buflen;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4758) 	if (copy_to_user(buffer, link, len))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4759) 		len = -EFAULT;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4760) out:
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4761) 	return len;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4762) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4763) 
fd4a0edf2a3d7 (Miklos Szeredi          2016-12-09 16:45:04 +0100 4764) /**
fd4a0edf2a3d7 (Miklos Szeredi          2016-12-09 16:45:04 +0100 4765)  * vfs_readlink - copy symlink body into userspace buffer
fd4a0edf2a3d7 (Miklos Szeredi          2016-12-09 16:45:04 +0100 4766)  * @dentry: dentry on which to get symbolic link
fd4a0edf2a3d7 (Miklos Szeredi          2016-12-09 16:45:04 +0100 4767)  * @buffer: user memory pointer
fd4a0edf2a3d7 (Miklos Szeredi          2016-12-09 16:45:04 +0100 4768)  * @buflen: size of buffer
fd4a0edf2a3d7 (Miklos Szeredi          2016-12-09 16:45:04 +0100 4769)  *
fd4a0edf2a3d7 (Miklos Szeredi          2016-12-09 16:45:04 +0100 4770)  * Does not touch atime.  That's up to the caller if necessary
fd4a0edf2a3d7 (Miklos Szeredi          2016-12-09 16:45:04 +0100 4771)  *
fd4a0edf2a3d7 (Miklos Szeredi          2016-12-09 16:45:04 +0100 4772)  * Does not call security hook.
fd4a0edf2a3d7 (Miklos Szeredi          2016-12-09 16:45:04 +0100 4773)  */
fd4a0edf2a3d7 (Miklos Szeredi          2016-12-09 16:45:04 +0100 4774) int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
fd4a0edf2a3d7 (Miklos Szeredi          2016-12-09 16:45:04 +0100 4775) {
fd4a0edf2a3d7 (Miklos Szeredi          2016-12-09 16:45:04 +0100 4776) 	struct inode *inode = d_inode(dentry);
f2df5da66262f (Al Viro                 2018-07-19 17:35:51 -0400 4777) 	DEFINE_DELAYED_CALL(done);
f2df5da66262f (Al Viro                 2018-07-19 17:35:51 -0400 4778) 	const char *link;
f2df5da66262f (Al Viro                 2018-07-19 17:35:51 -0400 4779) 	int res;
fd4a0edf2a3d7 (Miklos Szeredi          2016-12-09 16:45:04 +0100 4780) 
76fca90e9f3ab (Miklos Szeredi          2016-12-09 16:45:04 +0100 4781) 	if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
76fca90e9f3ab (Miklos Szeredi          2016-12-09 16:45:04 +0100 4782) 		if (unlikely(inode->i_op->readlink))
76fca90e9f3ab (Miklos Szeredi          2016-12-09 16:45:04 +0100 4783) 			return inode->i_op->readlink(dentry, buffer, buflen);
76fca90e9f3ab (Miklos Szeredi          2016-12-09 16:45:04 +0100 4784) 
76fca90e9f3ab (Miklos Szeredi          2016-12-09 16:45:04 +0100 4785) 		if (!d_is_symlink(dentry))
76fca90e9f3ab (Miklos Szeredi          2016-12-09 16:45:04 +0100 4786) 			return -EINVAL;
76fca90e9f3ab (Miklos Szeredi          2016-12-09 16:45:04 +0100 4787) 
76fca90e9f3ab (Miklos Szeredi          2016-12-09 16:45:04 +0100 4788) 		spin_lock(&inode->i_lock);
76fca90e9f3ab (Miklos Szeredi          2016-12-09 16:45:04 +0100 4789) 		inode->i_opflags |= IOP_DEFAULT_READLINK;
76fca90e9f3ab (Miklos Szeredi          2016-12-09 16:45:04 +0100 4790) 		spin_unlock(&inode->i_lock);
76fca90e9f3ab (Miklos Szeredi          2016-12-09 16:45:04 +0100 4791) 	}
fd4a0edf2a3d7 (Miklos Szeredi          2016-12-09 16:45:04 +0100 4792) 
4c4f7c19b3c72 (Eric Biggers            2019-04-10 13:21:14 -0700 4793) 	link = READ_ONCE(inode->i_link);
f2df5da66262f (Al Viro                 2018-07-19 17:35:51 -0400 4794) 	if (!link) {
f2df5da66262f (Al Viro                 2018-07-19 17:35:51 -0400 4795) 		link = inode->i_op->get_link(dentry, inode, &done);
f2df5da66262f (Al Viro                 2018-07-19 17:35:51 -0400 4796) 		if (IS_ERR(link))
f2df5da66262f (Al Viro                 2018-07-19 17:35:51 -0400 4797) 			return PTR_ERR(link);
f2df5da66262f (Al Viro                 2018-07-19 17:35:51 -0400 4798) 	}
f2df5da66262f (Al Viro                 2018-07-19 17:35:51 -0400 4799) 	res = readlink_copy(buffer, buflen, link);
f2df5da66262f (Al Viro                 2018-07-19 17:35:51 -0400 4800) 	do_delayed_call(&done);
f2df5da66262f (Al Viro                 2018-07-19 17:35:51 -0400 4801) 	return res;
fd4a0edf2a3d7 (Miklos Szeredi          2016-12-09 16:45:04 +0100 4802) }
fd4a0edf2a3d7 (Miklos Szeredi          2016-12-09 16:45:04 +0100 4803) EXPORT_SYMBOL(vfs_readlink);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4804) 
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4805) /**
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4806)  * vfs_get_link - get symlink body
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4807)  * @dentry: dentry on which to get symbolic link
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4808)  * @done: caller needs to free returned data with this
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4809)  *
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4810)  * Calls security hook and i_op->get_link() on the supplied inode.
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4811)  *
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4812)  * It does not touch atime.  That's up to the caller if necessary.
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4813)  *
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4814)  * Does not work on "special" symlinks like /proc/$$/fd/N
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4815)  */
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4816) const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4817) {
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4818) 	const char *res = ERR_PTR(-EINVAL);
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4819) 	struct inode *inode = d_inode(dentry);
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4820) 
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4821) 	if (d_is_symlink(dentry)) {
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4822) 		res = ERR_PTR(security_inode_readlink(dentry));
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4823) 		if (!res)
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4824) 			res = inode->i_op->get_link(dentry, inode, done);
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4825) 	}
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4826) 	return res;
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4827) }
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4828) EXPORT_SYMBOL(vfs_get_link);
d60874cd58fcb (Miklos Szeredi          2016-10-04 14:40:45 +0200 4829) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4830) /* get the link contents into pagecache */
6b2553918d8b4 (Al Viro                 2015-11-17 10:20:54 -0500 4831) const char *page_get_link(struct dentry *dentry, struct inode *inode,
fceef393a5381 (Al Viro                 2015-12-29 15:58:39 -0500 4832) 			  struct delayed_call *callback)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4833) {
ebd09abbd9699 (Duane Griffin           2008-12-19 20:47:12 +0000 4834) 	char *kaddr;
ebd09abbd9699 (Duane Griffin           2008-12-19 20:47:12 +0000 4835) 	struct page *page;
6b2553918d8b4 (Al Viro                 2015-11-17 10:20:54 -0500 4836) 	struct address_space *mapping = inode->i_mapping;
6b2553918d8b4 (Al Viro                 2015-11-17 10:20:54 -0500 4837) 
d3883d4f93449 (Al Viro                 2015-11-17 10:41:04 -0500 4838) 	if (!dentry) {
d3883d4f93449 (Al Viro                 2015-11-17 10:41:04 -0500 4839) 		page = find_get_page(mapping, 0);
d3883d4f93449 (Al Viro                 2015-11-17 10:41:04 -0500 4840) 		if (!page)
d3883d4f93449 (Al Viro                 2015-11-17 10:41:04 -0500 4841) 			return ERR_PTR(-ECHILD);
d3883d4f93449 (Al Viro                 2015-11-17 10:41:04 -0500 4842) 		if (!PageUptodate(page)) {
d3883d4f93449 (Al Viro                 2015-11-17 10:41:04 -0500 4843) 			put_page(page);
d3883d4f93449 (Al Viro                 2015-11-17 10:41:04 -0500 4844) 			return ERR_PTR(-ECHILD);
d3883d4f93449 (Al Viro                 2015-11-17 10:41:04 -0500 4845) 		}
d3883d4f93449 (Al Viro                 2015-11-17 10:41:04 -0500 4846) 	} else {
d3883d4f93449 (Al Viro                 2015-11-17 10:41:04 -0500 4847) 		page = read_mapping_page(mapping, 0, NULL);
d3883d4f93449 (Al Viro                 2015-11-17 10:41:04 -0500 4848) 		if (IS_ERR(page))
d3883d4f93449 (Al Viro                 2015-11-17 10:41:04 -0500 4849) 			return (char*)page;
d3883d4f93449 (Al Viro                 2015-11-17 10:41:04 -0500 4850) 	}
fceef393a5381 (Al Viro                 2015-12-29 15:58:39 -0500 4851) 	set_delayed_call(callback, page_put_link, page);
21fc61c73c390 (Al Viro                 2015-11-17 01:07:57 -0500 4852) 	BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
21fc61c73c390 (Al Viro                 2015-11-17 01:07:57 -0500 4853) 	kaddr = page_address(page);
6b2553918d8b4 (Al Viro                 2015-11-17 10:20:54 -0500 4854) 	nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
ebd09abbd9699 (Duane Griffin           2008-12-19 20:47:12 +0000 4855) 	return kaddr;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4856) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4857) 
6b2553918d8b4 (Al Viro                 2015-11-17 10:20:54 -0500 4858) EXPORT_SYMBOL(page_get_link);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4859) 
fceef393a5381 (Al Viro                 2015-12-29 15:58:39 -0500 4860) void page_put_link(void *arg)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4861) {
fceef393a5381 (Al Viro                 2015-12-29 15:58:39 -0500 4862) 	put_page(arg);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4863) }
4d359507346a1 (Al Viro                 2014-03-14 12:20:17 -0400 4864) EXPORT_SYMBOL(page_put_link);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4865) 
aa80deab33a8f (Al Viro                 2015-11-16 18:26:34 -0500 4866) int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
aa80deab33a8f (Al Viro                 2015-11-16 18:26:34 -0500 4867) {
fceef393a5381 (Al Viro                 2015-12-29 15:58:39 -0500 4868) 	DEFINE_DELAYED_CALL(done);
6b2553918d8b4 (Al Viro                 2015-11-17 10:20:54 -0500 4869) 	int res = readlink_copy(buffer, buflen,
6b2553918d8b4 (Al Viro                 2015-11-17 10:20:54 -0500 4870) 				page_get_link(dentry, d_inode(dentry),
fceef393a5381 (Al Viro                 2015-12-29 15:58:39 -0500 4871) 					      &done));
fceef393a5381 (Al Viro                 2015-12-29 15:58:39 -0500 4872) 	do_delayed_call(&done);
aa80deab33a8f (Al Viro                 2015-11-16 18:26:34 -0500 4873) 	return res;
aa80deab33a8f (Al Viro                 2015-11-16 18:26:34 -0500 4874) }
aa80deab33a8f (Al Viro                 2015-11-16 18:26:34 -0500 4875) EXPORT_SYMBOL(page_readlink);
aa80deab33a8f (Al Viro                 2015-11-16 18:26:34 -0500 4876) 
54566b2c1594c (Nicholas Piggin         2009-01-04 12:00:53 -0800 4877) /*
54566b2c1594c (Nicholas Piggin         2009-01-04 12:00:53 -0800 4878)  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
54566b2c1594c (Nicholas Piggin         2009-01-04 12:00:53 -0800 4879)  */
54566b2c1594c (Nicholas Piggin         2009-01-04 12:00:53 -0800 4880) int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4881) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4882) 	struct address_space *mapping = inode->i_mapping;
0adb25d2e71ab (Kirill Korotaev         2006-03-11 03:27:13 -0800 4883) 	struct page *page;
afddba49d18f3 (Nicholas Piggin         2007-10-16 01:25:01 -0700 4884) 	void *fsdata;
beb497ab48b16 (Dmitriy Monakhov        2007-02-16 01:27:18 -0800 4885) 	int err;
c718a97514e4d (Tetsuo Handa            2017-05-08 15:58:59 -0700 4886) 	unsigned int flags = 0;
54566b2c1594c (Nicholas Piggin         2009-01-04 12:00:53 -0800 4887) 	if (nofs)
54566b2c1594c (Nicholas Piggin         2009-01-04 12:00:53 -0800 4888) 		flags |= AOP_FLAG_NOFS;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4889) 
7e53cac41da9e (NeilBrown               2006-03-25 03:07:57 -0800 4890) retry:
afddba49d18f3 (Nicholas Piggin         2007-10-16 01:25:01 -0700 4891) 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
54566b2c1594c (Nicholas Piggin         2009-01-04 12:00:53 -0800 4892) 				flags, &page, &fsdata);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4893) 	if (err)
afddba49d18f3 (Nicholas Piggin         2007-10-16 01:25:01 -0700 4894) 		goto fail;
afddba49d18f3 (Nicholas Piggin         2007-10-16 01:25:01 -0700 4895) 
21fc61c73c390 (Al Viro                 2015-11-17 01:07:57 -0500 4896) 	memcpy(page_address(page), symname, len-1);
afddba49d18f3 (Nicholas Piggin         2007-10-16 01:25:01 -0700 4897) 
afddba49d18f3 (Nicholas Piggin         2007-10-16 01:25:01 -0700 4898) 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
afddba49d18f3 (Nicholas Piggin         2007-10-16 01:25:01 -0700 4899) 							page, fsdata);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4900) 	if (err < 0)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4901) 		goto fail;
afddba49d18f3 (Nicholas Piggin         2007-10-16 01:25:01 -0700 4902) 	if (err < len-1)
afddba49d18f3 (Nicholas Piggin         2007-10-16 01:25:01 -0700 4903) 		goto retry;
afddba49d18f3 (Nicholas Piggin         2007-10-16 01:25:01 -0700 4904) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4905) 	mark_inode_dirty(inode);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4906) 	return 0;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4907) fail:
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4908) 	return err;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4909) }
4d359507346a1 (Al Viro                 2014-03-14 12:20:17 -0400 4910) EXPORT_SYMBOL(__page_symlink);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4911) 
0adb25d2e71ab (Kirill Korotaev         2006-03-11 03:27:13 -0800 4912) int page_symlink(struct inode *inode, const char *symname, int len)
0adb25d2e71ab (Kirill Korotaev         2006-03-11 03:27:13 -0800 4913) {
0adb25d2e71ab (Kirill Korotaev         2006-03-11 03:27:13 -0800 4914) 	return __page_symlink(inode, symname, len,
c62d25556be6c (Michal Hocko            2015-11-06 16:28:49 -0800 4915) 			!mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
0adb25d2e71ab (Kirill Korotaev         2006-03-11 03:27:13 -0800 4916) }
4d359507346a1 (Al Viro                 2014-03-14 12:20:17 -0400 4917) EXPORT_SYMBOL(page_symlink);
0adb25d2e71ab (Kirill Korotaev         2006-03-11 03:27:13 -0800 4918) 
92e1d5be91a0e (Arjan van de Ven        2007-02-12 00:55:39 -0800 4919) const struct inode_operations page_symlink_inode_operations = {
6b2553918d8b4 (Al Viro                 2015-11-17 10:20:54 -0500 4920) 	.get_link	= page_get_link,
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4921) };
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 4922) EXPORT_SYMBOL(page_symlink_inode_operations);