xref: /openbmc/linux/fs/namei.c (revision e886663c)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/namei.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds /*
91da177e4SLinus Torvalds  * Some corrections by tytso.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
131da177e4SLinus Torvalds  * lookup logic.
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include <linux/init.h>
19630d9c47SPaul Gortmaker #include <linux/export.h>
2044696908SDavid S. Miller #include <linux/kernel.h>
211da177e4SLinus Torvalds #include <linux/slab.h>
221da177e4SLinus Torvalds #include <linux/fs.h>
231da177e4SLinus Torvalds #include <linux/namei.h>
241da177e4SLinus Torvalds #include <linux/pagemap.h>
250eeca283SRobert Love #include <linux/fsnotify.h>
261da177e4SLinus Torvalds #include <linux/personality.h>
271da177e4SLinus Torvalds #include <linux/security.h>
286146f0d5SMimi Zohar #include <linux/ima.h>
291da177e4SLinus Torvalds #include <linux/syscalls.h>
301da177e4SLinus Torvalds #include <linux/mount.h>
311da177e4SLinus Torvalds #include <linux/audit.h>
3216f7e0feSRandy Dunlap #include <linux/capability.h>
33834f2a4aSTrond Myklebust #include <linux/file.h>
345590ff0dSUlrich Drepper #include <linux/fcntl.h>
3508ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h>
365ad4e53bSAl Viro #include <linux/fs_struct.h>
37e77819e5SLinus Torvalds #include <linux/posix_acl.h>
3899d263d4SLinus Torvalds #include <linux/hash.h>
392a18da7aSGeorge Spelvin #include <linux/bitops.h>
40aeaa4a79SEric W. Biederman #include <linux/init_task.h>
417c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
421da177e4SLinus Torvalds 
43e81e3f4dSEric Paris #include "internal.h"
44c7105365SAl Viro #include "mount.h"
45e81e3f4dSEric Paris 
461da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer]
471da177e4SLinus Torvalds  * Fundamental changes in the pathname lookup mechanisms (namei)
481da177e4SLinus Torvalds  * were necessary because of omirr.  The reason is that omirr needs
491da177e4SLinus Torvalds  * to know the _real_ pathname, not the user-supplied one, in case
501da177e4SLinus Torvalds  * of symlinks (and also when transname replacements occur).
511da177e4SLinus Torvalds  *
521da177e4SLinus Torvalds  * The new code replaces the old recursive symlink resolution with
531da177e4SLinus Torvalds  * an iterative one (in case of non-nested symlink chains).  It does
541da177e4SLinus Torvalds  * this with calls to <fs>_follow_link().
551da177e4SLinus Torvalds  * As a side effect, dir_namei(), _namei() and follow_link() are now
561da177e4SLinus Torvalds  * replaced with a single function lookup_dentry() that can handle all
571da177e4SLinus Torvalds  * the special cases of the former code.
581da177e4SLinus Torvalds  *
591da177e4SLinus Torvalds  * With the new dcache, the pathname is stored at each inode, at least as
601da177e4SLinus Torvalds  * long as the refcount of the inode is positive.  As a side effect, the
611da177e4SLinus Torvalds  * size of the dcache depends on the inode cache and thus is dynamic.
621da177e4SLinus Torvalds  *
631da177e4SLinus Torvalds  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
641da177e4SLinus Torvalds  * resolution to correspond with current state of the code.
651da177e4SLinus Torvalds  *
661da177e4SLinus Torvalds  * Note that the symlink resolution is not *completely* iterative.
671da177e4SLinus Torvalds  * There is still a significant amount of tail- and mid- recursion in
681da177e4SLinus Torvalds  * the algorithm.  Also, note that <fs>_readlink() is not used in
691da177e4SLinus Torvalds  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
701da177e4SLinus Torvalds  * may return different results than <fs>_follow_link().  Many virtual
711da177e4SLinus Torvalds  * filesystems (including /proc) exhibit this behavior.
721da177e4SLinus Torvalds  */
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
751da177e4SLinus Torvalds  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
761da177e4SLinus Torvalds  * and the name already exists in form of a symlink, try to create the new
771da177e4SLinus Torvalds  * name indicated by the symlink. The old code always complained that the
781da177e4SLinus Torvalds  * name already exists, due to not following the symlink even if its target
791da177e4SLinus Torvalds  * is nonexistent.  The new semantics affects also mknod() and link() when
8025985edcSLucas De Marchi  * the name is a symlink pointing to a non-existent name.
811da177e4SLinus Torvalds  *
821da177e4SLinus Torvalds  * I don't know which semantics is the right one, since I have no access
831da177e4SLinus Torvalds  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
841da177e4SLinus Torvalds  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
851da177e4SLinus Torvalds  * "old" one. Personally, I think the new semantics is much more logical.
861da177e4SLinus Torvalds  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
871da177e4SLinus Torvalds  * file does succeed in both HP-UX and SunOs, but not in Solaris
881da177e4SLinus Torvalds  * and in the old Linux semantics.
891da177e4SLinus Torvalds  */
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
921da177e4SLinus Torvalds  * semantics.  See the comments in "open_namei" and "do_link" below.
931da177e4SLinus Torvalds  *
941da177e4SLinus Torvalds  * [10-Sep-98 Alan Modra] Another symlink change.
951da177e4SLinus Torvalds  */
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
981da177e4SLinus Torvalds  *	inside the path - always follow.
991da177e4SLinus Torvalds  *	in the last component in creation/removal/renaming - never follow.
1001da177e4SLinus Torvalds  *	if LOOKUP_FOLLOW passed - follow.
1011da177e4SLinus Torvalds  *	if the pathname has trailing slashes - follow.
1021da177e4SLinus Torvalds  *	otherwise - don't follow.
1031da177e4SLinus Torvalds  * (applied in that order).
1041da177e4SLinus Torvalds  *
1051da177e4SLinus Torvalds  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
1061da177e4SLinus Torvalds  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
1071da177e4SLinus Torvalds  * During the 2.4 we need to fix the userland stuff depending on it -
1081da177e4SLinus Torvalds  * hopefully we will be able to get rid of that wart in 2.5. So far only
1091da177e4SLinus Torvalds  * XEmacs seems to be relying on it...
1101da177e4SLinus Torvalds  */
1111da177e4SLinus Torvalds /*
1121da177e4SLinus Torvalds  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
113a11f3a05SArjan van de Ven  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1141da177e4SLinus Torvalds  * any extra contention...
1151da177e4SLinus Torvalds  */
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional
1181da177e4SLinus Torvalds  * checking and hopefully speeding things up, we copy filenames to the
1191da177e4SLinus Torvalds  * kernel data space before using them..
1201da177e4SLinus Torvalds  *
1211da177e4SLinus Torvalds  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
1221da177e4SLinus Torvalds  * PATH_MAX includes the nul terminator --RR.
1231da177e4SLinus Torvalds  */
1247950e385SJeff Layton 
125fd2f7cb5SAl Viro #define EMBEDDED_NAME_MAX	(PATH_MAX - offsetof(struct filename, iname))
12691a27b2aSJeff Layton 
12751f39a1fSDavid Drysdale struct filename *
12891a27b2aSJeff Layton getname_flags(const char __user *filename, int flags, int *empty)
12991a27b2aSJeff Layton {
13094b5d262SAl Viro 	struct filename *result;
1317950e385SJeff Layton 	char *kname;
13294b5d262SAl Viro 	int len;
1331da177e4SLinus Torvalds 
1347ac86265SJeff Layton 	result = audit_reusename(filename);
1357ac86265SJeff Layton 	if (result)
1367ac86265SJeff Layton 		return result;
1377ac86265SJeff Layton 
1387950e385SJeff Layton 	result = __getname();
1393f9f0aa6SLinus Torvalds 	if (unlikely(!result))
1404043cde8SEric Paris 		return ERR_PTR(-ENOMEM);
1411da177e4SLinus Torvalds 
1427950e385SJeff Layton 	/*
1437950e385SJeff Layton 	 * First, try to embed the struct filename inside the names_cache
1447950e385SJeff Layton 	 * allocation
1457950e385SJeff Layton 	 */
146fd2f7cb5SAl Viro 	kname = (char *)result->iname;
14791a27b2aSJeff Layton 	result->name = kname;
1487950e385SJeff Layton 
14994b5d262SAl Viro 	len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
15091a27b2aSJeff Layton 	if (unlikely(len < 0)) {
15194b5d262SAl Viro 		__putname(result);
15294b5d262SAl Viro 		return ERR_PTR(len);
15391a27b2aSJeff Layton 	}
1543f9f0aa6SLinus Torvalds 
1557950e385SJeff Layton 	/*
1567950e385SJeff Layton 	 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
1577950e385SJeff Layton 	 * separate struct filename so we can dedicate the entire
1587950e385SJeff Layton 	 * names_cache allocation for the pathname, and re-do the copy from
1597950e385SJeff Layton 	 * userland.
1607950e385SJeff Layton 	 */
16194b5d262SAl Viro 	if (unlikely(len == EMBEDDED_NAME_MAX)) {
162fd2f7cb5SAl Viro 		const size_t size = offsetof(struct filename, iname[1]);
1637950e385SJeff Layton 		kname = (char *)result;
1647950e385SJeff Layton 
165fd2f7cb5SAl Viro 		/*
166fd2f7cb5SAl Viro 		 * size is chosen that way we to guarantee that
167fd2f7cb5SAl Viro 		 * result->iname[0] is within the same object and that
168fd2f7cb5SAl Viro 		 * kname can't be equal to result->iname, no matter what.
169fd2f7cb5SAl Viro 		 */
170fd2f7cb5SAl Viro 		result = kzalloc(size, GFP_KERNEL);
17194b5d262SAl Viro 		if (unlikely(!result)) {
17294b5d262SAl Viro 			__putname(kname);
17394b5d262SAl Viro 			return ERR_PTR(-ENOMEM);
1747950e385SJeff Layton 		}
1757950e385SJeff Layton 		result->name = kname;
17694b5d262SAl Viro 		len = strncpy_from_user(kname, filename, PATH_MAX);
17794b5d262SAl Viro 		if (unlikely(len < 0)) {
17894b5d262SAl Viro 			__putname(kname);
17994b5d262SAl Viro 			kfree(result);
18094b5d262SAl Viro 			return ERR_PTR(len);
18194b5d262SAl Viro 		}
18294b5d262SAl Viro 		if (unlikely(len == PATH_MAX)) {
18394b5d262SAl Viro 			__putname(kname);
18494b5d262SAl Viro 			kfree(result);
18594b5d262SAl Viro 			return ERR_PTR(-ENAMETOOLONG);
18694b5d262SAl Viro 		}
1877950e385SJeff Layton 	}
1887950e385SJeff Layton 
18994b5d262SAl Viro 	result->refcnt = 1;
1903f9f0aa6SLinus Torvalds 	/* The empty path is special. */
1913f9f0aa6SLinus Torvalds 	if (unlikely(!len)) {
1923f9f0aa6SLinus Torvalds 		if (empty)
1931fa1e7f6SAndy Whitcroft 			*empty = 1;
19494b5d262SAl Viro 		if (!(flags & LOOKUP_EMPTY)) {
19594b5d262SAl Viro 			putname(result);
19694b5d262SAl Viro 			return ERR_PTR(-ENOENT);
1971da177e4SLinus Torvalds 		}
19894b5d262SAl Viro 	}
1997950e385SJeff Layton 
2007950e385SJeff Layton 	result->uptr = filename;
201c4ad8f98SLinus Torvalds 	result->aname = NULL;
2021da177e4SLinus Torvalds 	audit_getname(result);
2031da177e4SLinus Torvalds 	return result;
2043f9f0aa6SLinus Torvalds }
2053f9f0aa6SLinus Torvalds 
20691a27b2aSJeff Layton struct filename *
20791a27b2aSJeff Layton getname(const char __user * filename)
208f52e0c11SAl Viro {
209f7493e5dSLinus Torvalds 	return getname_flags(filename, 0, NULL);
210f52e0c11SAl Viro }
211f52e0c11SAl Viro 
212c4ad8f98SLinus Torvalds struct filename *
213c4ad8f98SLinus Torvalds getname_kernel(const char * filename)
214c4ad8f98SLinus Torvalds {
215c4ad8f98SLinus Torvalds 	struct filename *result;
21608518549SPaul Moore 	int len = strlen(filename) + 1;
217c4ad8f98SLinus Torvalds 
218c4ad8f98SLinus Torvalds 	result = __getname();
219c4ad8f98SLinus Torvalds 	if (unlikely(!result))
220c4ad8f98SLinus Torvalds 		return ERR_PTR(-ENOMEM);
221c4ad8f98SLinus Torvalds 
22208518549SPaul Moore 	if (len <= EMBEDDED_NAME_MAX) {
223fd2f7cb5SAl Viro 		result->name = (char *)result->iname;
22408518549SPaul Moore 	} else if (len <= PATH_MAX) {
22530ce4d19SAl Viro 		const size_t size = offsetof(struct filename, iname[1]);
22608518549SPaul Moore 		struct filename *tmp;
22708518549SPaul Moore 
22830ce4d19SAl Viro 		tmp = kmalloc(size, GFP_KERNEL);
22908518549SPaul Moore 		if (unlikely(!tmp)) {
23008518549SPaul Moore 			__putname(result);
23108518549SPaul Moore 			return ERR_PTR(-ENOMEM);
23208518549SPaul Moore 		}
23308518549SPaul Moore 		tmp->name = (char *)result;
23408518549SPaul Moore 		result = tmp;
23508518549SPaul Moore 	} else {
23608518549SPaul Moore 		__putname(result);
23708518549SPaul Moore 		return ERR_PTR(-ENAMETOOLONG);
23808518549SPaul Moore 	}
23908518549SPaul Moore 	memcpy((char *)result->name, filename, len);
240c4ad8f98SLinus Torvalds 	result->uptr = NULL;
241c4ad8f98SLinus Torvalds 	result->aname = NULL;
24255422d0bSPaul Moore 	result->refcnt = 1;
243fd3522fdSPaul Moore 	audit_getname(result);
244c4ad8f98SLinus Torvalds 
245c4ad8f98SLinus Torvalds 	return result;
246c4ad8f98SLinus Torvalds }
247c4ad8f98SLinus Torvalds 
24891a27b2aSJeff Layton void putname(struct filename *name)
2491da177e4SLinus Torvalds {
25055422d0bSPaul Moore 	BUG_ON(name->refcnt <= 0);
25155422d0bSPaul Moore 
25255422d0bSPaul Moore 	if (--name->refcnt > 0)
25355422d0bSPaul Moore 		return;
25455422d0bSPaul Moore 
255fd2f7cb5SAl Viro 	if (name->name != name->iname) {
25655422d0bSPaul Moore 		__putname(name->name);
25755422d0bSPaul Moore 		kfree(name);
25855422d0bSPaul Moore 	} else
25955422d0bSPaul Moore 		__putname(name);
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
262e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask)
263e77819e5SLinus Torvalds {
26484635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL
265e77819e5SLinus Torvalds 	struct posix_acl *acl;
266e77819e5SLinus Torvalds 
267e77819e5SLinus Torvalds 	if (mask & MAY_NOT_BLOCK) {
2683567866bSAl Viro 		acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
2693567866bSAl Viro 	        if (!acl)
270e77819e5SLinus Torvalds 	                return -EAGAIN;
2713567866bSAl Viro 		/* no ->get_acl() calls in RCU mode... */
272b8a7a3a6SAndreas Gruenbacher 		if (is_uncached_acl(acl))
273e77819e5SLinus Torvalds 			return -ECHILD;
27463d72b93SLinus Torvalds 	        return posix_acl_permission(inode, acl, mask);
275e77819e5SLinus Torvalds 	}
276e77819e5SLinus Torvalds 
2772982baa2SChristoph Hellwig 	acl = get_acl(inode, ACL_TYPE_ACCESS);
2784e34e719SChristoph Hellwig 	if (IS_ERR(acl))
2794e34e719SChristoph Hellwig 		return PTR_ERR(acl);
280e77819e5SLinus Torvalds 	if (acl) {
281e77819e5SLinus Torvalds 	        int error = posix_acl_permission(inode, acl, mask);
282e77819e5SLinus Torvalds 	        posix_acl_release(acl);
283e77819e5SLinus Torvalds 	        return error;
284e77819e5SLinus Torvalds 	}
28584635d68SLinus Torvalds #endif
286e77819e5SLinus Torvalds 
287e77819e5SLinus Torvalds 	return -EAGAIN;
288e77819e5SLinus Torvalds }
289e77819e5SLinus Torvalds 
2905909ccaaSLinus Torvalds /*
2915fc475b7SLinus Torvalds  * This does the basic UNIX permission checking.
2925fc475b7SLinus Torvalds  *
2935fc475b7SLinus Torvalds  * Note that the POSIX ACL check cares about the MAY_NOT_BLOCK bit,
2945fc475b7SLinus Torvalds  * for RCU walking.
2955909ccaaSLinus Torvalds  */
2967e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask)
2975909ccaaSLinus Torvalds {
29826cf46beSLinus Torvalds 	unsigned int mode = inode->i_mode;
2995909ccaaSLinus Torvalds 
3005fc475b7SLinus Torvalds 	/* Are we the owner? If so, ACL's don't matter */
3015fc475b7SLinus Torvalds 	if (likely(uid_eq(current_fsuid(), inode->i_uid))) {
3025fc475b7SLinus Torvalds 		mask &= 7;
3035909ccaaSLinus Torvalds 		mode >>= 6;
3045fc475b7SLinus Torvalds 		return (mask & ~mode) ? -EACCES : 0;
3055fc475b7SLinus Torvalds 	}
3065fc475b7SLinus Torvalds 
3075fc475b7SLinus Torvalds 	/* Do we have ACL's? */
308e77819e5SLinus Torvalds 	if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
3097e40145eSAl Viro 		int error = check_acl(inode, mask);
3105909ccaaSLinus Torvalds 		if (error != -EAGAIN)
3115909ccaaSLinus Torvalds 			return error;
3125909ccaaSLinus Torvalds 	}
3135909ccaaSLinus Torvalds 
3145fc475b7SLinus Torvalds 	/* Only RWX matters for group/other mode bits */
3155fc475b7SLinus Torvalds 	mask &= 7;
3165fc475b7SLinus Torvalds 
3175fc475b7SLinus Torvalds 	/*
3185fc475b7SLinus Torvalds 	 * Are the group permissions different from
3195fc475b7SLinus Torvalds 	 * the other permissions in the bits we care
3205fc475b7SLinus Torvalds 	 * about? Need to check group ownership if so.
3215fc475b7SLinus Torvalds 	 */
3225fc475b7SLinus Torvalds 	if (mask & (mode ^ (mode >> 3))) {
3235909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
3245909ccaaSLinus Torvalds 			mode >>= 3;
3255909ccaaSLinus Torvalds 	}
3265909ccaaSLinus Torvalds 
3275fc475b7SLinus Torvalds 	/* Bits in 'mode' clear that we require? */
3285fc475b7SLinus Torvalds 	return (mask & ~mode) ? -EACCES : 0;
3295909ccaaSLinus Torvalds }
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds /**
3321da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
3331da177e4SLinus Torvalds  * @inode:	inode to check access rights for
3345fc475b7SLinus Torvalds  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC,
3355fc475b7SLinus Torvalds  *		%MAY_NOT_BLOCK ...)
3361da177e4SLinus Torvalds  *
3371da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
3381da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
3391da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
340b74c79e9SNick Piggin  * are used for other things.
341b74c79e9SNick Piggin  *
342b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
343b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
344b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
3451da177e4SLinus Torvalds  */
3462830ba7fSAl Viro int generic_permission(struct inode *inode, int mask)
3471da177e4SLinus Torvalds {
3485909ccaaSLinus Torvalds 	int ret;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	/*
351948409c7SAndreas Gruenbacher 	 * Do the basic permission checks.
3521da177e4SLinus Torvalds 	 */
3537e40145eSAl Viro 	ret = acl_permission_check(inode, mask);
3545909ccaaSLinus Torvalds 	if (ret != -EACCES)
3555909ccaaSLinus Torvalds 		return ret;
3561da177e4SLinus Torvalds 
357d594e7ecSAl Viro 	if (S_ISDIR(inode->i_mode)) {
358d594e7ecSAl Viro 		/* DACs are overridable for directories */
359d594e7ecSAl Viro 		if (!(mask & MAY_WRITE))
36023adbe12SAndy Lutomirski 			if (capable_wrt_inode_uidgid(inode,
36123adbe12SAndy Lutomirski 						     CAP_DAC_READ_SEARCH))
362d594e7ecSAl Viro 				return 0;
36323adbe12SAndy Lutomirski 		if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
3641da177e4SLinus Torvalds 			return 0;
3652a4c2242SStephen Smalley 		return -EACCES;
3662a4c2242SStephen Smalley 	}
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	/*
3691da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
3701da177e4SLinus Torvalds 	 */
3717ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
372d594e7ecSAl Viro 	if (mask == MAY_READ)
37323adbe12SAndy Lutomirski 		if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
3741da177e4SLinus Torvalds 			return 0;
3752a4c2242SStephen Smalley 	/*
3762a4c2242SStephen Smalley 	 * Read/write DACs are always overridable.
3772a4c2242SStephen Smalley 	 * Executable DACs are overridable when there is
3782a4c2242SStephen Smalley 	 * at least one exec bit set.
3792a4c2242SStephen Smalley 	 */
3802a4c2242SStephen Smalley 	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
3812a4c2242SStephen Smalley 		if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
3822a4c2242SStephen Smalley 			return 0;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	return -EACCES;
3851da177e4SLinus Torvalds }
3864d359507SAl Viro EXPORT_SYMBOL(generic_permission);
3871da177e4SLinus Torvalds 
3883ddcd056SLinus Torvalds /*
3893ddcd056SLinus Torvalds  * We _really_ want to just do "generic_permission()" without
3903ddcd056SLinus Torvalds  * even looking at the inode->i_op values. So we keep a cache
3913ddcd056SLinus Torvalds  * flag in inode->i_opflags, that says "this has not special
3923ddcd056SLinus Torvalds  * permission function, use the fast case".
3933ddcd056SLinus Torvalds  */
3943ddcd056SLinus Torvalds static inline int do_inode_permission(struct inode *inode, int mask)
3953ddcd056SLinus Torvalds {
3963ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
3973ddcd056SLinus Torvalds 		if (likely(inode->i_op->permission))
3983ddcd056SLinus Torvalds 			return inode->i_op->permission(inode, mask);
3993ddcd056SLinus Torvalds 
4003ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
4013ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
4023ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_FASTPERM;
4033ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
4043ddcd056SLinus Torvalds 	}
4053ddcd056SLinus Torvalds 	return generic_permission(inode, mask);
4063ddcd056SLinus Torvalds }
4073ddcd056SLinus Torvalds 
408cb23beb5SChristoph Hellwig /**
4090bdaea90SDavid Howells  * sb_permission - Check superblock-level permissions
4100bdaea90SDavid Howells  * @sb: Superblock of inode to check permission on
41155852635SRandy Dunlap  * @inode: Inode to check permission on
4120bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
4130bdaea90SDavid Howells  *
4140bdaea90SDavid Howells  * Separate out file-system wide checks from inode-specific permission checks.
4150bdaea90SDavid Howells  */
4160bdaea90SDavid Howells static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
4170bdaea90SDavid Howells {
4180bdaea90SDavid Howells 	if (unlikely(mask & MAY_WRITE)) {
4190bdaea90SDavid Howells 		umode_t mode = inode->i_mode;
4200bdaea90SDavid Howells 
4210bdaea90SDavid Howells 		/* Nobody gets write access to a read-only fs. */
422bc98a42cSDavid Howells 		if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
4230bdaea90SDavid Howells 			return -EROFS;
4240bdaea90SDavid Howells 	}
4250bdaea90SDavid Howells 	return 0;
4260bdaea90SDavid Howells }
4270bdaea90SDavid Howells 
4280bdaea90SDavid Howells /**
4290bdaea90SDavid Howells  * inode_permission - Check for access rights to a given inode
4300bdaea90SDavid Howells  * @inode: Inode to check permission on
4310bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
4320bdaea90SDavid Howells  *
4330bdaea90SDavid Howells  * Check for read/write/execute permissions on an inode.  We use fs[ug]id for
4340bdaea90SDavid Howells  * this, letting us set arbitrary permissions for filesystem access without
4350bdaea90SDavid Howells  * changing the "normal" UIDs which are used for other things.
4360bdaea90SDavid Howells  *
4370bdaea90SDavid Howells  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
4380bdaea90SDavid Howells  */
4390bdaea90SDavid Howells int inode_permission(struct inode *inode, int mask)
4400bdaea90SDavid Howells {
4410bdaea90SDavid Howells 	int retval;
4420bdaea90SDavid Howells 
4430bdaea90SDavid Howells 	retval = sb_permission(inode->i_sb, inode, mask);
4440bdaea90SDavid Howells 	if (retval)
4450bdaea90SDavid Howells 		return retval;
4464bfd054aSEric Biggers 
4474bfd054aSEric Biggers 	if (unlikely(mask & MAY_WRITE)) {
4484bfd054aSEric Biggers 		/*
4494bfd054aSEric Biggers 		 * Nobody gets write access to an immutable file.
4504bfd054aSEric Biggers 		 */
4514bfd054aSEric Biggers 		if (IS_IMMUTABLE(inode))
4524bfd054aSEric Biggers 			return -EPERM;
4534bfd054aSEric Biggers 
4544bfd054aSEric Biggers 		/*
4554bfd054aSEric Biggers 		 * Updating mtime will likely cause i_uid and i_gid to be
4564bfd054aSEric Biggers 		 * written back improperly if their true value is unknown
4574bfd054aSEric Biggers 		 * to the vfs.
4584bfd054aSEric Biggers 		 */
4594bfd054aSEric Biggers 		if (HAS_UNMAPPED_ID(inode))
4604bfd054aSEric Biggers 			return -EACCES;
4614bfd054aSEric Biggers 	}
4624bfd054aSEric Biggers 
4634bfd054aSEric Biggers 	retval = do_inode_permission(inode, mask);
4644bfd054aSEric Biggers 	if (retval)
4654bfd054aSEric Biggers 		return retval;
4664bfd054aSEric Biggers 
4674bfd054aSEric Biggers 	retval = devcgroup_inode_permission(inode, mask);
4684bfd054aSEric Biggers 	if (retval)
4694bfd054aSEric Biggers 		return retval;
4704bfd054aSEric Biggers 
4714bfd054aSEric Biggers 	return security_inode_permission(inode, mask);
4720bdaea90SDavid Howells }
4734d359507SAl Viro EXPORT_SYMBOL(inode_permission);
4740bdaea90SDavid Howells 
4750bdaea90SDavid Howells /**
4765dd784d0SJan Blunck  * path_get - get a reference to a path
4775dd784d0SJan Blunck  * @path: path to get the reference to
4785dd784d0SJan Blunck  *
4795dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
4805dd784d0SJan Blunck  */
481dcf787f3SAl Viro void path_get(const struct path *path)
4825dd784d0SJan Blunck {
4835dd784d0SJan Blunck 	mntget(path->mnt);
4845dd784d0SJan Blunck 	dget(path->dentry);
4855dd784d0SJan Blunck }
4865dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
4875dd784d0SJan Blunck 
4885dd784d0SJan Blunck /**
4891d957f9bSJan Blunck  * path_put - put a reference to a path
4901d957f9bSJan Blunck  * @path: path to put the reference to
4911d957f9bSJan Blunck  *
4921d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
4931d957f9bSJan Blunck  */
494dcf787f3SAl Viro void path_put(const struct path *path)
4951da177e4SLinus Torvalds {
4961d957f9bSJan Blunck 	dput(path->dentry);
4971d957f9bSJan Blunck 	mntput(path->mnt);
4981da177e4SLinus Torvalds }
4991d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
5001da177e4SLinus Torvalds 
501894bc8c4SAl Viro #define EMBEDDED_LEVELS 2
5021f55a6ecSAl Viro struct nameidata {
5031f55a6ecSAl Viro 	struct path	path;
5041f55a6ecSAl Viro 	struct qstr	last;
5051f55a6ecSAl Viro 	struct path	root;
5061f55a6ecSAl Viro 	struct inode	*inode; /* path.dentry.d_inode */
5071f55a6ecSAl Viro 	unsigned int	flags;
508ab87f9a5SAleksa Sarai 	unsigned	seq, m_seq, r_seq;
5091f55a6ecSAl Viro 	int		last_type;
5101f55a6ecSAl Viro 	unsigned	depth;
511756daf26SNeilBrown 	int		total_link_count;
512697fc6caSAl Viro 	struct saved {
513697fc6caSAl Viro 		struct path link;
514fceef393SAl Viro 		struct delayed_call done;
515697fc6caSAl Viro 		const char *name;
5160450b2d1SAl Viro 		unsigned seq;
517894bc8c4SAl Viro 	} *stack, internal[EMBEDDED_LEVELS];
5189883d185SAl Viro 	struct filename	*name;
5199883d185SAl Viro 	struct nameidata *saved;
5209883d185SAl Viro 	unsigned	root_seq;
5219883d185SAl Viro 	int		dfd;
5220f705953SAl Viro 	kuid_t		dir_uid;
5230f705953SAl Viro 	umode_t		dir_mode;
5243859a271SKees Cook } __randomize_layout;
5251f55a6ecSAl Viro 
5269883d185SAl Viro static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
527894bc8c4SAl Viro {
528756daf26SNeilBrown 	struct nameidata *old = current->nameidata;
529756daf26SNeilBrown 	p->stack = p->internal;
530c8a53ee5SAl Viro 	p->dfd = dfd;
531c8a53ee5SAl Viro 	p->name = name;
532756daf26SNeilBrown 	p->total_link_count = old ? old->total_link_count : 0;
5339883d185SAl Viro 	p->saved = old;
534756daf26SNeilBrown 	current->nameidata = p;
535894bc8c4SAl Viro }
536894bc8c4SAl Viro 
5379883d185SAl Viro static void restore_nameidata(void)
538894bc8c4SAl Viro {
5399883d185SAl Viro 	struct nameidata *now = current->nameidata, *old = now->saved;
540756daf26SNeilBrown 
541756daf26SNeilBrown 	current->nameidata = old;
542756daf26SNeilBrown 	if (old)
543756daf26SNeilBrown 		old->total_link_count = now->total_link_count;
544e1a63bbcSAl Viro 	if (now->stack != now->internal)
545756daf26SNeilBrown 		kfree(now->stack);
546894bc8c4SAl Viro }
547894bc8c4SAl Viro 
54860ef60c7SAl Viro static bool nd_alloc_stack(struct nameidata *nd)
549894bc8c4SAl Viro {
550bc40aee0SAl Viro 	struct saved *p;
551bc40aee0SAl Viro 
5526da2ec56SKees Cook 	p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved),
55360ef60c7SAl Viro 			 nd->flags & LOOKUP_RCU ? GFP_ATOMIC : GFP_KERNEL);
554bc40aee0SAl Viro 	if (unlikely(!p))
55560ef60c7SAl Viro 		return false;
556894bc8c4SAl Viro 	memcpy(p, nd->internal, sizeof(nd->internal));
557894bc8c4SAl Viro 	nd->stack = p;
55860ef60c7SAl Viro 	return true;
559894bc8c4SAl Viro }
560894bc8c4SAl Viro 
561397d425dSEric W. Biederman /**
5626b03f7edSAl Viro  * path_connected - Verify that a dentry is below mnt.mnt_root
563397d425dSEric W. Biederman  *
564397d425dSEric W. Biederman  * Rename can sometimes move a file or directory outside of a bind
565397d425dSEric W. Biederman  * mount, path_connected allows those cases to be detected.
566397d425dSEric W. Biederman  */
5676b03f7edSAl Viro static bool path_connected(struct vfsmount *mnt, struct dentry *dentry)
568397d425dSEric W. Biederman {
56995dd7758SEric W. Biederman 	struct super_block *sb = mnt->mnt_sb;
570397d425dSEric W. Biederman 
571402dd2cfSChristoph Hellwig 	/* Bind mounts can have disconnected paths */
572402dd2cfSChristoph Hellwig 	if (mnt->mnt_root == sb->s_root)
573397d425dSEric W. Biederman 		return true;
574397d425dSEric W. Biederman 
5756b03f7edSAl Viro 	return is_subdir(dentry, mnt->mnt_root);
576397d425dSEric W. Biederman }
577397d425dSEric W. Biederman 
5787973387aSAl Viro static void drop_links(struct nameidata *nd)
5797973387aSAl Viro {
5807973387aSAl Viro 	int i = nd->depth;
5817973387aSAl Viro 	while (i--) {
5827973387aSAl Viro 		struct saved *last = nd->stack + i;
583fceef393SAl Viro 		do_delayed_call(&last->done);
584fceef393SAl Viro 		clear_delayed_call(&last->done);
5857973387aSAl Viro 	}
5867973387aSAl Viro }
5877973387aSAl Viro 
5887973387aSAl Viro static void terminate_walk(struct nameidata *nd)
5897973387aSAl Viro {
5907973387aSAl Viro 	drop_links(nd);
5917973387aSAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
5927973387aSAl Viro 		int i;
5937973387aSAl Viro 		path_put(&nd->path);
5947973387aSAl Viro 		for (i = 0; i < nd->depth; i++)
5957973387aSAl Viro 			path_put(&nd->stack[i].link);
59684a2bd39SAl Viro 		if (nd->flags & LOOKUP_ROOT_GRABBED) {
597102b8af2SAl Viro 			path_put(&nd->root);
59884a2bd39SAl Viro 			nd->flags &= ~LOOKUP_ROOT_GRABBED;
599102b8af2SAl Viro 		}
6007973387aSAl Viro 	} else {
6017973387aSAl Viro 		nd->flags &= ~LOOKUP_RCU;
6027973387aSAl Viro 		rcu_read_unlock();
6037973387aSAl Viro 	}
6047973387aSAl Viro 	nd->depth = 0;
6057973387aSAl Viro }
6067973387aSAl Viro 
6077973387aSAl Viro /* path_put is needed afterwards regardless of success or failure */
6082aa38470SAl Viro static bool __legitimize_path(struct path *path, unsigned seq, unsigned mseq)
6097973387aSAl Viro {
6102aa38470SAl Viro 	int res = __legitimize_mnt(path->mnt, mseq);
6117973387aSAl Viro 	if (unlikely(res)) {
6127973387aSAl Viro 		if (res > 0)
6137973387aSAl Viro 			path->mnt = NULL;
6147973387aSAl Viro 		path->dentry = NULL;
6157973387aSAl Viro 		return false;
6167973387aSAl Viro 	}
6177973387aSAl Viro 	if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
6187973387aSAl Viro 		path->dentry = NULL;
6197973387aSAl Viro 		return false;
6207973387aSAl Viro 	}
6217973387aSAl Viro 	return !read_seqcount_retry(&path->dentry->d_seq, seq);
6227973387aSAl Viro }
6237973387aSAl Viro 
6242aa38470SAl Viro static inline bool legitimize_path(struct nameidata *nd,
6252aa38470SAl Viro 			    struct path *path, unsigned seq)
6262aa38470SAl Viro {
6275bd73286SAl Viro 	return __legitimize_path(path, seq, nd->m_seq);
6282aa38470SAl Viro }
6292aa38470SAl Viro 
6307973387aSAl Viro static bool legitimize_links(struct nameidata *nd)
6317973387aSAl Viro {
6327973387aSAl Viro 	int i;
6337973387aSAl Viro 	for (i = 0; i < nd->depth; i++) {
6347973387aSAl Viro 		struct saved *last = nd->stack + i;
6357973387aSAl Viro 		if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
6367973387aSAl Viro 			drop_links(nd);
6377973387aSAl Viro 			nd->depth = i + 1;
6387973387aSAl Viro 			return false;
6397973387aSAl Viro 		}
6407973387aSAl Viro 	}
6417973387aSAl Viro 	return true;
6427973387aSAl Viro }
6437973387aSAl Viro 
644ee594bffSAl Viro static bool legitimize_root(struct nameidata *nd)
645ee594bffSAl Viro {
646adb21d2bSAleksa Sarai 	/*
647adb21d2bSAleksa Sarai 	 * For scoped-lookups (where nd->root has been zeroed), we need to
648adb21d2bSAleksa Sarai 	 * restart the whole lookup from scratch -- because set_root() is wrong
649adb21d2bSAleksa Sarai 	 * for these lookups (nd->dfd is the root, not the filesystem root).
650adb21d2bSAleksa Sarai 	 */
651adb21d2bSAleksa Sarai 	if (!nd->root.mnt && (nd->flags & LOOKUP_IS_SCOPED))
652adb21d2bSAleksa Sarai 		return false;
653adb21d2bSAleksa Sarai 	/* Nothing to do if nd->root is zero or is managed by the VFS user. */
654ee594bffSAl Viro 	if (!nd->root.mnt || (nd->flags & LOOKUP_ROOT))
655ee594bffSAl Viro 		return true;
65684a2bd39SAl Viro 	nd->flags |= LOOKUP_ROOT_GRABBED;
657ee594bffSAl Viro 	return legitimize_path(nd, &nd->root, nd->root_seq);
658ee594bffSAl Viro }
659ee594bffSAl Viro 
66019660af7SAl Viro /*
66131e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
66219660af7SAl Viro  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
66319660af7SAl Viro  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
66457e3715cSMike Marshall  * normal reference counts on dentries and vfsmounts to transition to ref-walk
66519660af7SAl Viro  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
66619660af7SAl Viro  * got stuck, so ref-walk may continue from there. If this is not successful
66719660af7SAl Viro  * (eg. a seqcount has changed), then failure is returned and it's up to caller
66819660af7SAl Viro  * to restart the path walk from the beginning in ref-walk mode.
66931e6b01fSNick Piggin  */
67031e6b01fSNick Piggin 
67131e6b01fSNick Piggin /**
67219660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
67319660af7SAl Viro  * @nd: nameidata pathwalk data
67439191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
67531e6b01fSNick Piggin  *
6764675ac39SAl Viro  * unlazy_walk attempts to legitimize the current nd->path and nd->root
6774675ac39SAl Viro  * for ref-walk mode.
6784675ac39SAl Viro  * Must be called from rcu-walk context.
6797973387aSAl Viro  * Nothing should touch nameidata between unlazy_walk() failure and
6807973387aSAl Viro  * terminate_walk().
68131e6b01fSNick Piggin  */
6824675ac39SAl Viro static int unlazy_walk(struct nameidata *nd)
68331e6b01fSNick Piggin {
68431e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
68531e6b01fSNick Piggin 
68631e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
687e5c832d5SLinus Torvalds 
688e5c832d5SLinus Torvalds 	nd->flags &= ~LOOKUP_RCU;
6897973387aSAl Viro 	if (unlikely(!legitimize_links(nd)))
6904675ac39SAl Viro 		goto out1;
69184a2bd39SAl Viro 	if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
69284a2bd39SAl Viro 		goto out;
693ee594bffSAl Viro 	if (unlikely(!legitimize_root(nd)))
6944675ac39SAl Viro 		goto out;
6954675ac39SAl Viro 	rcu_read_unlock();
6964675ac39SAl Viro 	BUG_ON(nd->inode != parent->d_inode);
6974675ac39SAl Viro 	return 0;
6984675ac39SAl Viro 
69984a2bd39SAl Viro out1:
7004675ac39SAl Viro 	nd->path.mnt = NULL;
7014675ac39SAl Viro 	nd->path.dentry = NULL;
7024675ac39SAl Viro out:
7034675ac39SAl Viro 	rcu_read_unlock();
7044675ac39SAl Viro 	return -ECHILD;
7054675ac39SAl Viro }
7064675ac39SAl Viro 
7074675ac39SAl Viro /**
7084675ac39SAl Viro  * unlazy_child - try to switch to ref-walk mode.
7094675ac39SAl Viro  * @nd: nameidata pathwalk data
7104675ac39SAl Viro  * @dentry: child of nd->path.dentry
7114675ac39SAl Viro  * @seq: seq number to check dentry against
7124675ac39SAl Viro  * Returns: 0 on success, -ECHILD on failure
7134675ac39SAl Viro  *
7144675ac39SAl Viro  * unlazy_child attempts to legitimize the current nd->path, nd->root and dentry
7154675ac39SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
7164675ac39SAl Viro  * @nd.  Must be called from rcu-walk context.
7174675ac39SAl Viro  * Nothing should touch nameidata between unlazy_child() failure and
7184675ac39SAl Viro  * terminate_walk().
7194675ac39SAl Viro  */
7204675ac39SAl Viro static int unlazy_child(struct nameidata *nd, struct dentry *dentry, unsigned seq)
7214675ac39SAl Viro {
7224675ac39SAl Viro 	BUG_ON(!(nd->flags & LOOKUP_RCU));
7234675ac39SAl Viro 
7244675ac39SAl Viro 	nd->flags &= ~LOOKUP_RCU;
7254675ac39SAl Viro 	if (unlikely(!legitimize_links(nd)))
7264675ac39SAl Viro 		goto out2;
7277973387aSAl Viro 	if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
7287973387aSAl Viro 		goto out2;
7294675ac39SAl Viro 	if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref)))
7307973387aSAl Viro 		goto out1;
73148a066e7SAl Viro 
73215570086SLinus Torvalds 	/*
7334675ac39SAl Viro 	 * We need to move both the parent and the dentry from the RCU domain
7344675ac39SAl Viro 	 * to be properly refcounted. And the sequence number in the dentry
7354675ac39SAl Viro 	 * validates *both* dentry counters, since we checked the sequence
7364675ac39SAl Viro 	 * number of the parent after we got the child sequence number. So we
7374675ac39SAl Viro 	 * know the parent must still be valid if the child sequence number is
73815570086SLinus Torvalds 	 */
7394675ac39SAl Viro 	if (unlikely(!lockref_get_not_dead(&dentry->d_lockref)))
740e5c832d5SLinus Torvalds 		goto out;
74184a2bd39SAl Viro 	if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
74284a2bd39SAl Viro 		goto out_dput;
743e5c832d5SLinus Torvalds 	/*
744e5c832d5SLinus Torvalds 	 * Sequence counts matched. Now make sure that the root is
745e5c832d5SLinus Torvalds 	 * still valid and get it if required.
746e5c832d5SLinus Torvalds 	 */
74784a2bd39SAl Viro 	if (unlikely(!legitimize_root(nd)))
74884a2bd39SAl Viro 		goto out_dput;
7498b61e74fSAl Viro 	rcu_read_unlock();
75031e6b01fSNick Piggin 	return 0;
75119660af7SAl Viro 
7527973387aSAl Viro out2:
7537973387aSAl Viro 	nd->path.mnt = NULL;
7547973387aSAl Viro out1:
7557973387aSAl Viro 	nd->path.dentry = NULL;
756e5c832d5SLinus Torvalds out:
7578b61e74fSAl Viro 	rcu_read_unlock();
75884a2bd39SAl Viro 	return -ECHILD;
75984a2bd39SAl Viro out_dput:
76084a2bd39SAl Viro 	rcu_read_unlock();
76184a2bd39SAl Viro 	dput(dentry);
76231e6b01fSNick Piggin 	return -ECHILD;
76331e6b01fSNick Piggin }
76431e6b01fSNick Piggin 
7654ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
76634286d66SNick Piggin {
767a89f8337SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
7684ce16ef3SAl Viro 		return dentry->d_op->d_revalidate(dentry, flags);
769a89f8337SAl Viro 	else
770a89f8337SAl Viro 		return 1;
77134286d66SNick Piggin }
77234286d66SNick Piggin 
7739f1fafeeSAl Viro /**
7749f1fafeeSAl Viro  * complete_walk - successful completion of path walk
7759f1fafeeSAl Viro  * @nd:  pointer nameidata
77639159de2SJeff Layton  *
7779f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
7789f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
7799f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
7809f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
7819f1fafeeSAl Viro  * need to drop nd->path.
78239159de2SJeff Layton  */
7839f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
78439159de2SJeff Layton {
78516c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
78639159de2SJeff Layton 	int status;
78739159de2SJeff Layton 
7889f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
789adb21d2bSAleksa Sarai 		/*
790adb21d2bSAleksa Sarai 		 * We don't want to zero nd->root for scoped-lookups or
791adb21d2bSAleksa Sarai 		 * externally-managed nd->root.
792adb21d2bSAleksa Sarai 		 */
793adb21d2bSAleksa Sarai 		if (!(nd->flags & (LOOKUP_ROOT | LOOKUP_IS_SCOPED)))
7949f1fafeeSAl Viro 			nd->root.mnt = NULL;
7954675ac39SAl Viro 		if (unlikely(unlazy_walk(nd)))
79648a066e7SAl Viro 			return -ECHILD;
79748a066e7SAl Viro 	}
7989f1fafeeSAl Viro 
799adb21d2bSAleksa Sarai 	if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
800adb21d2bSAleksa Sarai 		/*
801adb21d2bSAleksa Sarai 		 * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't
802adb21d2bSAleksa Sarai 		 * ever step outside the root during lookup" and should already
803adb21d2bSAleksa Sarai 		 * be guaranteed by the rest of namei, we want to avoid a namei
804adb21d2bSAleksa Sarai 		 * BUG resulting in userspace being given a path that was not
805adb21d2bSAleksa Sarai 		 * scoped within the root at some point during the lookup.
806adb21d2bSAleksa Sarai 		 *
807adb21d2bSAleksa Sarai 		 * So, do a final sanity-check to make sure that in the
808adb21d2bSAleksa Sarai 		 * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED)
809adb21d2bSAleksa Sarai 		 * we won't silently return an fd completely outside of the
810adb21d2bSAleksa Sarai 		 * requested root to userspace.
811adb21d2bSAleksa Sarai 		 *
812adb21d2bSAleksa Sarai 		 * Userspace could move the path outside the root after this
813adb21d2bSAleksa Sarai 		 * check, but as discussed elsewhere this is not a concern (the
814adb21d2bSAleksa Sarai 		 * resolved file was inside the root at some point).
815adb21d2bSAleksa Sarai 		 */
816adb21d2bSAleksa Sarai 		if (!path_is_under(&nd->path, &nd->root))
817adb21d2bSAleksa Sarai 			return -EXDEV;
818adb21d2bSAleksa Sarai 	}
819adb21d2bSAleksa Sarai 
82016c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
82139159de2SJeff Layton 		return 0;
82239159de2SJeff Layton 
823ecf3d1f1SJeff Layton 	if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
82416c2cd71SAl Viro 		return 0;
82516c2cd71SAl Viro 
826ecf3d1f1SJeff Layton 	status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
82739159de2SJeff Layton 	if (status > 0)
82839159de2SJeff Layton 		return 0;
82939159de2SJeff Layton 
83016c2cd71SAl Viro 	if (!status)
83139159de2SJeff Layton 		status = -ESTALE;
83216c2cd71SAl Viro 
83339159de2SJeff Layton 	return status;
83439159de2SJeff Layton }
83539159de2SJeff Layton 
836740a1678SAleksa Sarai static int set_root(struct nameidata *nd)
8372a737871SAl Viro {
83831e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
8399e6697e2SAl Viro 
840adb21d2bSAleksa Sarai 	/*
841adb21d2bSAleksa Sarai 	 * Jumping to the real root in a scoped-lookup is a BUG in namei, but we
842adb21d2bSAleksa Sarai 	 * still have to ensure it doesn't happen because it will cause a breakout
843adb21d2bSAleksa Sarai 	 * from the dirfd.
844adb21d2bSAleksa Sarai 	 */
845adb21d2bSAleksa Sarai 	if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED))
846adb21d2bSAleksa Sarai 		return -ENOTRECOVERABLE;
847adb21d2bSAleksa Sarai 
8489e6697e2SAl Viro 	if (nd->flags & LOOKUP_RCU) {
8498f47a016SAl Viro 		unsigned seq;
850c28cc364SNick Piggin 
851c28cc364SNick Piggin 		do {
852c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
85331e6b01fSNick Piggin 			nd->root = fs->root;
8548f47a016SAl Viro 			nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
855c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
8569e6697e2SAl Viro 	} else {
8579e6697e2SAl Viro 		get_fs_root(fs, &nd->root);
85884a2bd39SAl Viro 		nd->flags |= LOOKUP_ROOT_GRABBED;
8599e6697e2SAl Viro 	}
860740a1678SAleksa Sarai 	return 0;
86131e6b01fSNick Piggin }
86231e6b01fSNick Piggin 
863248fb5b9SAl Viro static int nd_jump_root(struct nameidata *nd)
864248fb5b9SAl Viro {
865adb21d2bSAleksa Sarai 	if (unlikely(nd->flags & LOOKUP_BENEATH))
866adb21d2bSAleksa Sarai 		return -EXDEV;
86772ba2929SAleksa Sarai 	if (unlikely(nd->flags & LOOKUP_NO_XDEV)) {
86872ba2929SAleksa Sarai 		/* Absolute path arguments to path_init() are allowed. */
86972ba2929SAleksa Sarai 		if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt)
87072ba2929SAleksa Sarai 			return -EXDEV;
87172ba2929SAleksa Sarai 	}
872740a1678SAleksa Sarai 	if (!nd->root.mnt) {
873740a1678SAleksa Sarai 		int error = set_root(nd);
874740a1678SAleksa Sarai 		if (error)
875740a1678SAleksa Sarai 			return error;
876740a1678SAleksa Sarai 	}
877248fb5b9SAl Viro 	if (nd->flags & LOOKUP_RCU) {
878248fb5b9SAl Viro 		struct dentry *d;
879248fb5b9SAl Viro 		nd->path = nd->root;
880248fb5b9SAl Viro 		d = nd->path.dentry;
881248fb5b9SAl Viro 		nd->inode = d->d_inode;
882248fb5b9SAl Viro 		nd->seq = nd->root_seq;
883248fb5b9SAl Viro 		if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
884248fb5b9SAl Viro 			return -ECHILD;
885248fb5b9SAl Viro 	} else {
886248fb5b9SAl Viro 		path_put(&nd->path);
887248fb5b9SAl Viro 		nd->path = nd->root;
888248fb5b9SAl Viro 		path_get(&nd->path);
889248fb5b9SAl Viro 		nd->inode = nd->path.dentry->d_inode;
890248fb5b9SAl Viro 	}
891248fb5b9SAl Viro 	nd->flags |= LOOKUP_JUMPED;
892248fb5b9SAl Viro 	return 0;
893248fb5b9SAl Viro }
894248fb5b9SAl Viro 
895b5fb63c1SChristoph Hellwig /*
8966b255391SAl Viro  * Helper to directly jump to a known parsed path from ->get_link,
897b5fb63c1SChristoph Hellwig  * caller must have taken a reference to path beforehand.
898b5fb63c1SChristoph Hellwig  */
8991bc82070SAleksa Sarai int nd_jump_link(struct path *path)
900b5fb63c1SChristoph Hellwig {
9014b99d499SAleksa Sarai 	int error = -ELOOP;
9026e77137bSAl Viro 	struct nameidata *nd = current->nameidata;
903b5fb63c1SChristoph Hellwig 
9044b99d499SAleksa Sarai 	if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS))
9054b99d499SAleksa Sarai 		goto err;
9064b99d499SAleksa Sarai 
90772ba2929SAleksa Sarai 	error = -EXDEV;
90872ba2929SAleksa Sarai 	if (unlikely(nd->flags & LOOKUP_NO_XDEV)) {
90972ba2929SAleksa Sarai 		if (nd->path.mnt != path->mnt)
91072ba2929SAleksa Sarai 			goto err;
91172ba2929SAleksa Sarai 	}
912adb21d2bSAleksa Sarai 	/* Not currently safe for scoped-lookups. */
913adb21d2bSAleksa Sarai 	if (unlikely(nd->flags & LOOKUP_IS_SCOPED))
914adb21d2bSAleksa Sarai 		goto err;
91572ba2929SAleksa Sarai 
9164b99d499SAleksa Sarai 	path_put(&nd->path);
917b5fb63c1SChristoph Hellwig 	nd->path = *path;
918b5fb63c1SChristoph Hellwig 	nd->inode = nd->path.dentry->d_inode;
919b5fb63c1SChristoph Hellwig 	nd->flags |= LOOKUP_JUMPED;
9201bc82070SAleksa Sarai 	return 0;
9214b99d499SAleksa Sarai 
9224b99d499SAleksa Sarai err:
9234b99d499SAleksa Sarai 	path_put(path);
9244b99d499SAleksa Sarai 	return error;
925b5fb63c1SChristoph Hellwig }
926b5fb63c1SChristoph Hellwig 
927b9ff4429SAl Viro static inline void put_link(struct nameidata *nd)
928574197e0SAl Viro {
92921c3003dSAl Viro 	struct saved *last = nd->stack + --nd->depth;
930fceef393SAl Viro 	do_delayed_call(&last->done);
9316548fae2SAl Viro 	if (!(nd->flags & LOOKUP_RCU))
932b9ff4429SAl Viro 		path_put(&last->link);
933574197e0SAl Viro }
934574197e0SAl Viro 
935561ec64aSLinus Torvalds int sysctl_protected_symlinks __read_mostly = 0;
936561ec64aSLinus Torvalds int sysctl_protected_hardlinks __read_mostly = 0;
93730aba665SSalvatore Mesoraca int sysctl_protected_fifos __read_mostly;
93830aba665SSalvatore Mesoraca int sysctl_protected_regular __read_mostly;
939800179c9SKees Cook 
940800179c9SKees Cook /**
941800179c9SKees Cook  * may_follow_link - Check symlink following for unsafe situations
94255852635SRandy Dunlap  * @nd: nameidata pathwalk data
943800179c9SKees Cook  *
944800179c9SKees Cook  * In the case of the sysctl_protected_symlinks sysctl being enabled,
945800179c9SKees Cook  * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
946800179c9SKees Cook  * in a sticky world-writable directory. This is to protect privileged
947800179c9SKees Cook  * processes from failing races against path names that may change out
948800179c9SKees Cook  * from under them by way of other users creating malicious symlinks.
949800179c9SKees Cook  * It will permit symlinks to be followed only when outside a sticky
950800179c9SKees Cook  * world-writable directory, or when the uid of the symlink and follower
951800179c9SKees Cook  * match, or when the directory owner matches the symlink's owner.
952800179c9SKees Cook  *
953800179c9SKees Cook  * Returns 0 if following the symlink is allowed, -ve on error.
954800179c9SKees Cook  */
955ad6cc4c3SAl Viro static inline int may_follow_link(struct nameidata *nd, const struct inode *inode)
956800179c9SKees Cook {
957800179c9SKees Cook 	if (!sysctl_protected_symlinks)
958800179c9SKees Cook 		return 0;
959800179c9SKees Cook 
960800179c9SKees Cook 	/* Allowed if owner and follower match. */
96181abe27bSEric W. Biederman 	if (uid_eq(current_cred()->fsuid, inode->i_uid))
962800179c9SKees Cook 		return 0;
963800179c9SKees Cook 
964800179c9SKees Cook 	/* Allowed if parent directory not sticky and world-writable. */
9650f705953SAl Viro 	if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
966800179c9SKees Cook 		return 0;
967800179c9SKees Cook 
968800179c9SKees Cook 	/* Allowed if parent directory and link owner match. */
9690f705953SAl Viro 	if (uid_valid(nd->dir_uid) && uid_eq(nd->dir_uid, inode->i_uid))
970800179c9SKees Cook 		return 0;
971800179c9SKees Cook 
97231956502SAl Viro 	if (nd->flags & LOOKUP_RCU)
97331956502SAl Viro 		return -ECHILD;
97431956502SAl Viro 
975ea841bafSRichard Guy Briggs 	audit_inode(nd->name, nd->stack[0].link.dentry, 0);
976245d7369SKees Cook 	audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link");
977800179c9SKees Cook 	return -EACCES;
978800179c9SKees Cook }
979800179c9SKees Cook 
980800179c9SKees Cook /**
981800179c9SKees Cook  * safe_hardlink_source - Check for safe hardlink conditions
982800179c9SKees Cook  * @inode: the source inode to hardlink from
983800179c9SKees Cook  *
984800179c9SKees Cook  * Return false if at least one of the following conditions:
985800179c9SKees Cook  *    - inode is not a regular file
986800179c9SKees Cook  *    - inode is setuid
987800179c9SKees Cook  *    - inode is setgid and group-exec
988800179c9SKees Cook  *    - access failure for read and write
989800179c9SKees Cook  *
990800179c9SKees Cook  * Otherwise returns true.
991800179c9SKees Cook  */
992800179c9SKees Cook static bool safe_hardlink_source(struct inode *inode)
993800179c9SKees Cook {
994800179c9SKees Cook 	umode_t mode = inode->i_mode;
995800179c9SKees Cook 
996800179c9SKees Cook 	/* Special files should not get pinned to the filesystem. */
997800179c9SKees Cook 	if (!S_ISREG(mode))
998800179c9SKees Cook 		return false;
999800179c9SKees Cook 
1000800179c9SKees Cook 	/* Setuid files should not get pinned to the filesystem. */
1001800179c9SKees Cook 	if (mode & S_ISUID)
1002800179c9SKees Cook 		return false;
1003800179c9SKees Cook 
1004800179c9SKees Cook 	/* Executable setgid files should not get pinned to the filesystem. */
1005800179c9SKees Cook 	if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
1006800179c9SKees Cook 		return false;
1007800179c9SKees Cook 
1008800179c9SKees Cook 	/* Hardlinking to unreadable or unwritable sources is dangerous. */
1009800179c9SKees Cook 	if (inode_permission(inode, MAY_READ | MAY_WRITE))
1010800179c9SKees Cook 		return false;
1011800179c9SKees Cook 
1012800179c9SKees Cook 	return true;
1013800179c9SKees Cook }
1014800179c9SKees Cook 
1015800179c9SKees Cook /**
1016800179c9SKees Cook  * may_linkat - Check permissions for creating a hardlink
1017800179c9SKees Cook  * @link: the source to hardlink from
1018800179c9SKees Cook  *
1019800179c9SKees Cook  * Block hardlink when all of:
1020800179c9SKees Cook  *  - sysctl_protected_hardlinks enabled
1021800179c9SKees Cook  *  - fsuid does not match inode
1022800179c9SKees Cook  *  - hardlink source is unsafe (see safe_hardlink_source() above)
1023f2ca3796SDirk Steinmetz  *  - not CAP_FOWNER in a namespace with the inode owner uid mapped
1024800179c9SKees Cook  *
1025800179c9SKees Cook  * Returns 0 if successful, -ve on error.
1026800179c9SKees Cook  */
1027812931d6SChristoph Hellwig int may_linkat(struct path *link)
1028800179c9SKees Cook {
1029593d1ce8SEric W. Biederman 	struct inode *inode = link->dentry->d_inode;
1030593d1ce8SEric W. Biederman 
1031593d1ce8SEric W. Biederman 	/* Inode writeback is not safe when the uid or gid are invalid. */
1032593d1ce8SEric W. Biederman 	if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
1033593d1ce8SEric W. Biederman 		return -EOVERFLOW;
1034800179c9SKees Cook 
1035800179c9SKees Cook 	if (!sysctl_protected_hardlinks)
1036800179c9SKees Cook 		return 0;
1037800179c9SKees Cook 
1038800179c9SKees Cook 	/* Source inode owner (or CAP_FOWNER) can hardlink all they like,
1039800179c9SKees Cook 	 * otherwise, it must be a safe source.
1040800179c9SKees Cook 	 */
1041cc658db4SKees Cook 	if (safe_hardlink_source(inode) || inode_owner_or_capable(inode))
1042800179c9SKees Cook 		return 0;
1043800179c9SKees Cook 
1044245d7369SKees Cook 	audit_log_path_denied(AUDIT_ANOM_LINK, "linkat");
1045800179c9SKees Cook 	return -EPERM;
1046800179c9SKees Cook }
1047800179c9SKees Cook 
104830aba665SSalvatore Mesoraca /**
104930aba665SSalvatore Mesoraca  * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
105030aba665SSalvatore Mesoraca  *			  should be allowed, or not, on files that already
105130aba665SSalvatore Mesoraca  *			  exist.
1052d0cb5018SAl Viro  * @dir_mode: mode bits of directory
1053d0cb5018SAl Viro  * @dir_uid: owner of directory
105430aba665SSalvatore Mesoraca  * @inode: the inode of the file to open
105530aba665SSalvatore Mesoraca  *
105630aba665SSalvatore Mesoraca  * Block an O_CREAT open of a FIFO (or a regular file) when:
105730aba665SSalvatore Mesoraca  *   - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
105830aba665SSalvatore Mesoraca  *   - the file already exists
105930aba665SSalvatore Mesoraca  *   - we are in a sticky directory
106030aba665SSalvatore Mesoraca  *   - we don't own the file
106130aba665SSalvatore Mesoraca  *   - the owner of the directory doesn't own the file
106230aba665SSalvatore Mesoraca  *   - the directory is world writable
106330aba665SSalvatore Mesoraca  * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
106430aba665SSalvatore Mesoraca  * the directory doesn't have to be world writable: being group writable will
106530aba665SSalvatore Mesoraca  * be enough.
106630aba665SSalvatore Mesoraca  *
106730aba665SSalvatore Mesoraca  * Returns 0 if the open is allowed, -ve on error.
106830aba665SSalvatore Mesoraca  */
1069d0cb5018SAl Viro static int may_create_in_sticky(umode_t dir_mode, kuid_t dir_uid,
107030aba665SSalvatore Mesoraca 				struct inode * const inode)
107130aba665SSalvatore Mesoraca {
107230aba665SSalvatore Mesoraca 	if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) ||
107330aba665SSalvatore Mesoraca 	    (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
1074d0cb5018SAl Viro 	    likely(!(dir_mode & S_ISVTX)) ||
1075d0cb5018SAl Viro 	    uid_eq(inode->i_uid, dir_uid) ||
107630aba665SSalvatore Mesoraca 	    uid_eq(current_fsuid(), inode->i_uid))
107730aba665SSalvatore Mesoraca 		return 0;
107830aba665SSalvatore Mesoraca 
1079d0cb5018SAl Viro 	if (likely(dir_mode & 0002) ||
1080d0cb5018SAl Viro 	    (dir_mode & 0020 &&
108130aba665SSalvatore Mesoraca 	     ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) ||
108230aba665SSalvatore Mesoraca 	      (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) {
1083245d7369SKees Cook 		const char *operation = S_ISFIFO(inode->i_mode) ?
1084245d7369SKees Cook 					"sticky_create_fifo" :
1085245d7369SKees Cook 					"sticky_create_regular";
1086245d7369SKees Cook 		audit_log_path_denied(AUDIT_ANOM_CREAT, operation);
108730aba665SSalvatore Mesoraca 		return -EACCES;
108830aba665SSalvatore Mesoraca 	}
108930aba665SSalvatore Mesoraca 	return 0;
109030aba665SSalvatore Mesoraca }
109130aba665SSalvatore Mesoraca 
1092f015f126SDavid Howells /*
1093f015f126SDavid Howells  * follow_up - Find the mountpoint of path's vfsmount
1094f015f126SDavid Howells  *
1095f015f126SDavid Howells  * Given a path, find the mountpoint of its source file system.
1096f015f126SDavid Howells  * Replace @path with the path of the mountpoint in the parent mount.
1097f015f126SDavid Howells  * Up is towards /.
1098f015f126SDavid Howells  *
1099f015f126SDavid Howells  * Return 1 if we went up a level and 0 if we were already at the
1100f015f126SDavid Howells  * root.
1101f015f126SDavid Howells  */
1102bab77ebfSAl Viro int follow_up(struct path *path)
11031da177e4SLinus Torvalds {
11040714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
11050714a533SAl Viro 	struct mount *parent;
11061da177e4SLinus Torvalds 	struct dentry *mountpoint;
110799b7db7bSNick Piggin 
110848a066e7SAl Viro 	read_seqlock_excl(&mount_lock);
11090714a533SAl Viro 	parent = mnt->mnt_parent;
11103c0a6163SAl Viro 	if (parent == mnt) {
111148a066e7SAl Viro 		read_sequnlock_excl(&mount_lock);
11121da177e4SLinus Torvalds 		return 0;
11131da177e4SLinus Torvalds 	}
11140714a533SAl Viro 	mntget(&parent->mnt);
1115a73324daSAl Viro 	mountpoint = dget(mnt->mnt_mountpoint);
111648a066e7SAl Viro 	read_sequnlock_excl(&mount_lock);
1117bab77ebfSAl Viro 	dput(path->dentry);
1118bab77ebfSAl Viro 	path->dentry = mountpoint;
1119bab77ebfSAl Viro 	mntput(path->mnt);
11200714a533SAl Viro 	path->mnt = &parent->mnt;
11211da177e4SLinus Torvalds 	return 1;
11221da177e4SLinus Torvalds }
11234d359507SAl Viro EXPORT_SYMBOL(follow_up);
11241da177e4SLinus Torvalds 
11257ef482faSAl Viro static bool choose_mountpoint_rcu(struct mount *m, const struct path *root,
11267ef482faSAl Viro 				  struct path *path, unsigned *seqp)
11277ef482faSAl Viro {
11287ef482faSAl Viro 	while (mnt_has_parent(m)) {
11297ef482faSAl Viro 		struct dentry *mountpoint = m->mnt_mountpoint;
11307ef482faSAl Viro 
11317ef482faSAl Viro 		m = m->mnt_parent;
11327ef482faSAl Viro 		if (unlikely(root->dentry == mountpoint &&
11337ef482faSAl Viro 			     root->mnt == &m->mnt))
11347ef482faSAl Viro 			break;
11357ef482faSAl Viro 		if (mountpoint != m->mnt.mnt_root) {
11367ef482faSAl Viro 			path->mnt = &m->mnt;
11377ef482faSAl Viro 			path->dentry = mountpoint;
11387ef482faSAl Viro 			*seqp = read_seqcount_begin(&mountpoint->d_seq);
11397ef482faSAl Viro 			return true;
11407ef482faSAl Viro 		}
11417ef482faSAl Viro 	}
11427ef482faSAl Viro 	return false;
11437ef482faSAl Viro }
11447ef482faSAl Viro 
11452aa38470SAl Viro static bool choose_mountpoint(struct mount *m, const struct path *root,
11462aa38470SAl Viro 			      struct path *path)
11472aa38470SAl Viro {
11482aa38470SAl Viro 	bool found;
11492aa38470SAl Viro 
11502aa38470SAl Viro 	rcu_read_lock();
11512aa38470SAl Viro 	while (1) {
11522aa38470SAl Viro 		unsigned seq, mseq = read_seqbegin(&mount_lock);
11532aa38470SAl Viro 
11542aa38470SAl Viro 		found = choose_mountpoint_rcu(m, root, path, &seq);
11552aa38470SAl Viro 		if (unlikely(!found)) {
11562aa38470SAl Viro 			if (!read_seqretry(&mount_lock, mseq))
11572aa38470SAl Viro 				break;
11582aa38470SAl Viro 		} else {
11592aa38470SAl Viro 			if (likely(__legitimize_path(path, seq, mseq)))
11602aa38470SAl Viro 				break;
11612aa38470SAl Viro 			rcu_read_unlock();
11622aa38470SAl Viro 			path_put(path);
11632aa38470SAl Viro 			rcu_read_lock();
11642aa38470SAl Viro 		}
11652aa38470SAl Viro 	}
11662aa38470SAl Viro 	rcu_read_unlock();
11672aa38470SAl Viro 	return found;
11682aa38470SAl Viro }
11692aa38470SAl Viro 
1170b5c84bf6SNick Piggin /*
11719875cf80SDavid Howells  * Perform an automount
11729875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
11739875cf80SDavid Howells  *   were called with.
11741da177e4SLinus Torvalds  */
11751c9f5e06SAl Viro static int follow_automount(struct path *path, int *count, unsigned lookup_flags)
117631e6b01fSNick Piggin {
117725e195aaSAl Viro 	struct dentry *dentry = path->dentry;
11789875cf80SDavid Howells 
11790ec26fd0SMiklos Szeredi 	/* We don't want to mount if someone's just doing a stat -
11800ec26fd0SMiklos Szeredi 	 * unless they're stat'ing a directory and appended a '/' to
11810ec26fd0SMiklos Szeredi 	 * the name.
11820ec26fd0SMiklos Szeredi 	 *
11830ec26fd0SMiklos Szeredi 	 * We do, however, want to mount if someone wants to open or
11845a30d8a2SDavid Howells 	 * create a file of any type under the mountpoint, wants to
11850ec26fd0SMiklos Szeredi 	 * traverse through the mountpoint or wants to open the
11860ec26fd0SMiklos Szeredi 	 * mounted directory.  Also, autofs may mark negative dentries
11870ec26fd0SMiklos Szeredi 	 * as being automount points.  These will need the attentions
11880ec26fd0SMiklos Szeredi 	 * of the daemon to instantiate them before they can be used.
11895a30d8a2SDavid Howells 	 */
11901c9f5e06SAl Viro 	if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
11915d38f049SIan Kent 			   LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
119225e195aaSAl Viro 	    dentry->d_inode)
11939875cf80SDavid Howells 		return -EISDIR;
11940ec26fd0SMiklos Szeredi 
11951c9f5e06SAl Viro 	if (count && (*count)++ >= MAXSYMLINKS)
11969875cf80SDavid Howells 		return -ELOOP;
11979875cf80SDavid Howells 
119825e195aaSAl Viro 	return finish_automount(dentry->d_op->d_automount(path), path);
1199ea5b778aSDavid Howells }
12009875cf80SDavid Howells 
12019875cf80SDavid Howells /*
12029deed3ebSAl Viro  * mount traversal - out-of-line part.  One note on ->d_flags accesses -
12039deed3ebSAl Viro  * dentries are pinned but not locked here, so negative dentry can go
12049deed3ebSAl Viro  * positive right under us.  Use of smp_load_acquire() provides a barrier
12059deed3ebSAl Viro  * sufficient for ->d_inode and ->d_flags consistency.
12069875cf80SDavid Howells  */
12079deed3ebSAl Viro static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped,
12089deed3ebSAl Viro 			     int *count, unsigned lookup_flags)
12099875cf80SDavid Howells {
12109deed3ebSAl Viro 	struct vfsmount *mnt = path->mnt;
12119875cf80SDavid Howells 	bool need_mntput = false;
12128aef1884SAl Viro 	int ret = 0;
12139875cf80SDavid Howells 
12149deed3ebSAl Viro 	while (flags & DCACHE_MANAGED_DENTRY) {
1215cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1216cc53ce53SDavid Howells 		 * being held. */
1217d41efb52SAl Viro 		if (flags & DCACHE_MANAGE_TRANSIT) {
1218fb5f51c7SIan Kent 			ret = path->dentry->d_op->d_manage(path, false);
1219508c8772SAl Viro 			flags = smp_load_acquire(&path->dentry->d_flags);
1220cc53ce53SDavid Howells 			if (ret < 0)
12218aef1884SAl Viro 				break;
1222cc53ce53SDavid Howells 		}
1223cc53ce53SDavid Howells 
12249deed3ebSAl Viro 		if (flags & DCACHE_MOUNTED) {	// something's mounted on it..
12259875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
12269deed3ebSAl Viro 			if (mounted) {		// ... in our namespace
12279875cf80SDavid Howells 				dput(path->dentry);
12289875cf80SDavid Howells 				if (need_mntput)
1229463ffb2eSAl Viro 					mntput(path->mnt);
1230463ffb2eSAl Viro 				path->mnt = mounted;
1231463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
12329deed3ebSAl Viro 				// here we know it's positive
12339deed3ebSAl Viro 				flags = path->dentry->d_flags;
12349875cf80SDavid Howells 				need_mntput = true;
12359875cf80SDavid Howells 				continue;
1236463ffb2eSAl Viro 			}
12371da177e4SLinus Torvalds 		}
12389875cf80SDavid Howells 
12399deed3ebSAl Viro 		if (!(flags & DCACHE_NEED_AUTOMOUNT))
12409deed3ebSAl Viro 			break;
12419deed3ebSAl Viro 
12429deed3ebSAl Viro 		// uncovered automount point
12439deed3ebSAl Viro 		ret = follow_automount(path, count, lookup_flags);
12449deed3ebSAl Viro 		flags = smp_load_acquire(&path->dentry->d_flags);
12459875cf80SDavid Howells 		if (ret < 0)
12468aef1884SAl Viro 			break;
12479875cf80SDavid Howells 	}
12489875cf80SDavid Howells 
12499deed3ebSAl Viro 	if (ret == -EISDIR)
12509deed3ebSAl Viro 		ret = 0;
12519deed3ebSAl Viro 	// possible if you race with several mount --move
12529deed3ebSAl Viro 	if (need_mntput && path->mnt == mnt)
12538aef1884SAl Viro 		mntput(path->mnt);
12549deed3ebSAl Viro 	if (!ret && unlikely(d_flags_negative(flags)))
1255d41efb52SAl Viro 		ret = -ENOENT;
12569deed3ebSAl Viro 	*jumped = need_mntput;
12578402752eSAl Viro 	return ret;
12581da177e4SLinus Torvalds }
12591da177e4SLinus Torvalds 
12609deed3ebSAl Viro static inline int traverse_mounts(struct path *path, bool *jumped,
12619deed3ebSAl Viro 				  int *count, unsigned lookup_flags)
12629deed3ebSAl Viro {
12639deed3ebSAl Viro 	unsigned flags = smp_load_acquire(&path->dentry->d_flags);
12649deed3ebSAl Viro 
12659deed3ebSAl Viro 	/* fastpath */
12669deed3ebSAl Viro 	if (likely(!(flags & DCACHE_MANAGED_DENTRY))) {
12679deed3ebSAl Viro 		*jumped = false;
12689deed3ebSAl Viro 		if (unlikely(d_flags_negative(flags)))
12699deed3ebSAl Viro 			return -ENOENT;
12709deed3ebSAl Viro 		return 0;
12719deed3ebSAl Viro 	}
12729deed3ebSAl Viro 	return __traverse_mounts(path, flags, jumped, count, lookup_flags);
12739deed3ebSAl Viro }
12749deed3ebSAl Viro 
1275cc53ce53SDavid Howells int follow_down_one(struct path *path)
12761da177e4SLinus Torvalds {
12771da177e4SLinus Torvalds 	struct vfsmount *mounted;
12781da177e4SLinus Torvalds 
12791c755af4SAl Viro 	mounted = lookup_mnt(path);
12801da177e4SLinus Torvalds 	if (mounted) {
12819393bd07SAl Viro 		dput(path->dentry);
12829393bd07SAl Viro 		mntput(path->mnt);
12839393bd07SAl Viro 		path->mnt = mounted;
12849393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
12851da177e4SLinus Torvalds 		return 1;
12861da177e4SLinus Torvalds 	}
12871da177e4SLinus Torvalds 	return 0;
12881da177e4SLinus Torvalds }
12894d359507SAl Viro EXPORT_SYMBOL(follow_down_one);
12901da177e4SLinus Torvalds 
12919875cf80SDavid Howells /*
12929deed3ebSAl Viro  * Follow down to the covering mount currently visible to userspace.  At each
12939deed3ebSAl Viro  * point, the filesystem owning that dentry may be queried as to whether the
12949deed3ebSAl Viro  * caller is permitted to proceed or not.
12959deed3ebSAl Viro  */
12969deed3ebSAl Viro int follow_down(struct path *path)
12979deed3ebSAl Viro {
12989deed3ebSAl Viro 	struct vfsmount *mnt = path->mnt;
12999deed3ebSAl Viro 	bool jumped;
13009deed3ebSAl Viro 	int ret = traverse_mounts(path, &jumped, NULL, 0);
13019deed3ebSAl Viro 
13029deed3ebSAl Viro 	if (path->mnt != mnt)
13039deed3ebSAl Viro 		mntput(mnt);
13049deed3ebSAl Viro 	return ret;
13059deed3ebSAl Viro }
13069deed3ebSAl Viro EXPORT_SYMBOL(follow_down);
13079deed3ebSAl Viro 
13089deed3ebSAl Viro /*
1309287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
1310287548e4SAl Viro  * we meet a managed dentry that would need blocking.
13119875cf80SDavid Howells  */
13129875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1313254cf582SAl Viro 			       struct inode **inode, unsigned *seqp)
13149875cf80SDavid Howells {
1315ea936aebSAl Viro 	struct dentry *dentry = path->dentry;
1316ea936aebSAl Viro 	unsigned int flags = dentry->d_flags;
1317ea936aebSAl Viro 
1318ea936aebSAl Viro 	if (likely(!(flags & DCACHE_MANAGED_DENTRY)))
1319ea936aebSAl Viro 		return true;
1320ea936aebSAl Viro 
1321ea936aebSAl Viro 	if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1322ea936aebSAl Viro 		return false;
1323ea936aebSAl Viro 
132462a7375eSIan Kent 	for (;;) {
132562a7375eSIan Kent 		/*
132662a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
132762a7375eSIan Kent 		 * that wants to block transit.
132862a7375eSIan Kent 		 */
1329ea936aebSAl Viro 		if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) {
1330ea936aebSAl Viro 			int res = dentry->d_op->d_manage(path, true);
1331ea936aebSAl Viro 			if (res)
1332ea936aebSAl Viro 				return res == -EISDIR;
1333ea936aebSAl Viro 			flags = dentry->d_flags;
1334b8faf035SNeilBrown 		}
133562a7375eSIan Kent 
1336ea936aebSAl Viro 		if (flags & DCACHE_MOUNTED) {
1337ea936aebSAl Viro 			struct mount *mounted = __lookup_mnt(path->mnt, dentry);
1338ea936aebSAl Viro 			if (mounted) {
1339c7105365SAl Viro 				path->mnt = &mounted->mnt;
1340ea936aebSAl Viro 				dentry = path->dentry = mounted->mnt.mnt_root;
1341a3fbbde7SAl Viro 				nd->flags |= LOOKUP_JUMPED;
1342ea936aebSAl Viro 				*seqp = read_seqcount_begin(&dentry->d_seq);
1343ea936aebSAl Viro 				*inode = dentry->d_inode;
134459430262SLinus Torvalds 				/*
1345ea936aebSAl Viro 				 * We don't need to re-check ->d_seq after this
1346ea936aebSAl Viro 				 * ->d_inode read - there will be an RCU delay
1347ea936aebSAl Viro 				 * between mount hash removal and ->mnt_root
1348ea936aebSAl Viro 				 * becoming unpinned.
134959430262SLinus Torvalds 				 */
1350ea936aebSAl Viro 				flags = dentry->d_flags;
1351ea936aebSAl Viro 				continue;
13529875cf80SDavid Howells 			}
1353ea936aebSAl Viro 			if (read_seqretry(&mount_lock, nd->m_seq))
1354ea936aebSAl Viro 				return false;
1355ea936aebSAl Viro 		}
1356ea936aebSAl Viro 		return !(flags & DCACHE_NEED_AUTOMOUNT);
1357ea936aebSAl Viro 	}
1358287548e4SAl Viro }
1359287548e4SAl Viro 
1360db3c9adeSAl Viro static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry,
1361db3c9adeSAl Viro 			  struct path *path, struct inode **inode,
1362db3c9adeSAl Viro 			  unsigned int *seqp)
1363bd7c4b50SAl Viro {
13649deed3ebSAl Viro 	bool jumped;
1365db3c9adeSAl Viro 	int ret;
1366bd7c4b50SAl Viro 
1367db3c9adeSAl Viro 	path->mnt = nd->path.mnt;
1368db3c9adeSAl Viro 	path->dentry = dentry;
1369c153007bSAl Viro 	if (nd->flags & LOOKUP_RCU) {
1370c153007bSAl Viro 		unsigned int seq = *seqp;
1371c153007bSAl Viro 		if (unlikely(!*inode))
1372c153007bSAl Viro 			return -ENOENT;
1373c153007bSAl Viro 		if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
13749deed3ebSAl Viro 			return 0;
1375c153007bSAl Viro 		if (unlazy_child(nd, dentry, seq))
1376c153007bSAl Viro 			return -ECHILD;
1377c153007bSAl Viro 		// *path might've been clobbered by __follow_mount_rcu()
1378c153007bSAl Viro 		path->mnt = nd->path.mnt;
1379c153007bSAl Viro 		path->dentry = dentry;
1380c153007bSAl Viro 	}
13819deed3ebSAl Viro 	ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags);
13829deed3ebSAl Viro 	if (jumped) {
13839deed3ebSAl Viro 		if (unlikely(nd->flags & LOOKUP_NO_XDEV))
13849deed3ebSAl Viro 			ret = -EXDEV;
13859deed3ebSAl Viro 		else
13869deed3ebSAl Viro 			nd->flags |= LOOKUP_JUMPED;
13879deed3ebSAl Viro 	}
13889deed3ebSAl Viro 	if (unlikely(ret)) {
13899deed3ebSAl Viro 		dput(path->dentry);
13909deed3ebSAl Viro 		if (path->mnt != nd->path.mnt)
13919deed3ebSAl Viro 			mntput(path->mnt);
13929deed3ebSAl Viro 	} else {
1393bd7c4b50SAl Viro 		*inode = d_backing_inode(path->dentry);
1394bd7c4b50SAl Viro 		*seqp = 0; /* out of RCU mode, so the value doesn't matter */
1395bd7c4b50SAl Viro 	}
1396bd7c4b50SAl Viro 	return ret;
1397bd7c4b50SAl Viro }
1398bd7c4b50SAl Viro 
13999875cf80SDavid Howells /*
1400f4fdace9SOleg Drokin  * This looks up the name in dcache and possibly revalidates the found dentry.
1401f4fdace9SOleg Drokin  * NULL is returned if the dentry does not exist in the cache.
1402baa03890SNick Piggin  */
1403e3c13928SAl Viro static struct dentry *lookup_dcache(const struct qstr *name,
1404e3c13928SAl Viro 				    struct dentry *dir,
14056c51e513SAl Viro 				    unsigned int flags)
1406baa03890SNick Piggin {
1407a89f8337SAl Viro 	struct dentry *dentry = d_lookup(dir, name);
1408bad61189SMiklos Szeredi 	if (dentry) {
1409a89f8337SAl Viro 		int error = d_revalidate(dentry, flags);
1410bad61189SMiklos Szeredi 		if (unlikely(error <= 0)) {
141174ff0ffcSAl Viro 			if (!error)
14125542aa2fSEric W. Biederman 				d_invalidate(dentry);
1413bad61189SMiklos Szeredi 			dput(dentry);
141474ff0ffcSAl Viro 			return ERR_PTR(error);
1415bad61189SMiklos Szeredi 		}
1416bad61189SMiklos Szeredi 	}
1417baa03890SNick Piggin 	return dentry;
1418baa03890SNick Piggin }
1419baa03890SNick Piggin 
1420baa03890SNick Piggin /*
1421a03ece5fSAl Viro  * Parent directory has inode locked exclusive.  This is one
1422a03ece5fSAl Viro  * and only case when ->lookup() gets called on non in-lookup
1423a03ece5fSAl Viro  * dentries - as the matter of fact, this only gets called
1424a03ece5fSAl Viro  * when directory is guaranteed to have no in-lookup children
1425a03ece5fSAl Viro  * at all.
142644396f4bSJosef Bacik  */
1427a03ece5fSAl Viro static struct dentry *__lookup_hash(const struct qstr *name,
1428a03ece5fSAl Viro 		struct dentry *base, unsigned int flags)
142944396f4bSJosef Bacik {
1430a03ece5fSAl Viro 	struct dentry *dentry = lookup_dcache(name, base, flags);
143144396f4bSJosef Bacik 	struct dentry *old;
1432a03ece5fSAl Viro 	struct inode *dir = base->d_inode;
1433a03ece5fSAl Viro 
1434a03ece5fSAl Viro 	if (dentry)
1435a03ece5fSAl Viro 		return dentry;
143644396f4bSJosef Bacik 
143744396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
1438a03ece5fSAl Viro 	if (unlikely(IS_DEADDIR(dir)))
143944396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
1440a03ece5fSAl Viro 
1441a03ece5fSAl Viro 	dentry = d_alloc(base, name);
1442a03ece5fSAl Viro 	if (unlikely(!dentry))
1443a03ece5fSAl Viro 		return ERR_PTR(-ENOMEM);
144444396f4bSJosef Bacik 
144572bd866aSAl Viro 	old = dir->i_op->lookup(dir, dentry, flags);
144644396f4bSJosef Bacik 	if (unlikely(old)) {
144744396f4bSJosef Bacik 		dput(dentry);
144844396f4bSJosef Bacik 		dentry = old;
144944396f4bSJosef Bacik 	}
145044396f4bSJosef Bacik 	return dentry;
145144396f4bSJosef Bacik }
145244396f4bSJosef Bacik 
145320e34357SAl Viro static struct dentry *lookup_fast(struct nameidata *nd,
145420e34357SAl Viro 				  struct inode **inode,
1455254cf582SAl Viro 			          unsigned *seqp)
14561da177e4SLinus Torvalds {
145731e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
14585a18fff2SAl Viro 	int status = 1;
14599875cf80SDavid Howells 
14603cac260aSAl Viro 	/*
1461b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
14625d0f49c1SAl Viro 	 * of a false negative due to a concurrent rename, the caller is
14635d0f49c1SAl Viro 	 * going to fall back to non-racy lookup.
1464b04f784eSNick Piggin 	 */
146531e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
146631e6b01fSNick Piggin 		unsigned seq;
1467da53be12SLinus Torvalds 		dentry = __d_lookup_rcu(parent, &nd->last, &seq);
14685d0f49c1SAl Viro 		if (unlikely(!dentry)) {
14694675ac39SAl Viro 			if (unlazy_walk(nd))
147020e34357SAl Viro 				return ERR_PTR(-ECHILD);
147120e34357SAl Viro 			return NULL;
14725d0f49c1SAl Viro 		}
14735a18fff2SAl Viro 
147412f8ad4bSLinus Torvalds 		/*
147512f8ad4bSLinus Torvalds 		 * This sequence count validates that the inode matches
147612f8ad4bSLinus Torvalds 		 * the dentry name information from lookup.
147712f8ad4bSLinus Torvalds 		 */
147863afdfc7SDavid Howells 		*inode = d_backing_inode(dentry);
14795d0f49c1SAl Viro 		if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
148020e34357SAl Viro 			return ERR_PTR(-ECHILD);
148112f8ad4bSLinus Torvalds 
148212f8ad4bSLinus Torvalds 		/*
148312f8ad4bSLinus Torvalds 		 * This sequence count validates that the parent had no
148412f8ad4bSLinus Torvalds 		 * changes while we did the lookup of the dentry above.
148512f8ad4bSLinus Torvalds 		 *
148612f8ad4bSLinus Torvalds 		 * The memory barrier in read_seqcount_begin of child is
148712f8ad4bSLinus Torvalds 		 *  enough, we can use __read_seqcount_retry here.
148812f8ad4bSLinus Torvalds 		 */
14895d0f49c1SAl Viro 		if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
149020e34357SAl Viro 			return ERR_PTR(-ECHILD);
14915a18fff2SAl Viro 
1492254cf582SAl Viro 		*seqp = seq;
14934ce16ef3SAl Viro 		status = d_revalidate(dentry, nd->flags);
1494c153007bSAl Viro 		if (likely(status > 0))
149520e34357SAl Viro 			return dentry;
14964675ac39SAl Viro 		if (unlazy_child(nd, dentry, seq))
149720e34357SAl Viro 			return ERR_PTR(-ECHILD);
1498209a7fb2SAl Viro 		if (unlikely(status == -ECHILD))
1499209a7fb2SAl Viro 			/* we'd been told to redo it in non-rcu mode */
1500209a7fb2SAl Viro 			status = d_revalidate(dentry, nd->flags);
15015a18fff2SAl Viro 	} else {
1502e97cdc87SAl Viro 		dentry = __d_lookup(parent, &nd->last);
150381e6f520SAl Viro 		if (unlikely(!dentry))
150420e34357SAl Viro 			return NULL;
15054ce16ef3SAl Viro 		status = d_revalidate(dentry, nd->flags);
15065d0f49c1SAl Viro 	}
15075a18fff2SAl Viro 	if (unlikely(status <= 0)) {
1508e9742b53SAl Viro 		if (!status)
15095d0f49c1SAl Viro 			d_invalidate(dentry);
15105a18fff2SAl Viro 		dput(dentry);
151120e34357SAl Viro 		return ERR_PTR(status);
15125a18fff2SAl Viro 	}
151320e34357SAl Viro 	return dentry;
1514697f514dSMiklos Szeredi }
1515697f514dSMiklos Szeredi 
1516697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */
151788d8331aSAl Viro static struct dentry *__lookup_slow(const struct qstr *name,
1518e3c13928SAl Viro 				    struct dentry *dir,
1519e3c13928SAl Viro 				    unsigned int flags)
1520697f514dSMiklos Szeredi {
152188d8331aSAl Viro 	struct dentry *dentry, *old;
15221936386eSAl Viro 	struct inode *inode = dir->d_inode;
1523d9171b93SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
15241936386eSAl Viro 
15251936386eSAl Viro 	/* Don't go there if it's already dead */
152694bdd655SAl Viro 	if (unlikely(IS_DEADDIR(inode)))
152788d8331aSAl Viro 		return ERR_PTR(-ENOENT);
152894bdd655SAl Viro again:
1529d9171b93SAl Viro 	dentry = d_alloc_parallel(dir, name, &wq);
153094bdd655SAl Viro 	if (IS_ERR(dentry))
153188d8331aSAl Viro 		return dentry;
153294bdd655SAl Viro 	if (unlikely(!d_in_lookup(dentry))) {
1533949a852eSAl Viro 		int error = d_revalidate(dentry, flags);
1534949a852eSAl Viro 		if (unlikely(error <= 0)) {
153594bdd655SAl Viro 			if (!error) {
1536949a852eSAl Viro 				d_invalidate(dentry);
1537949a852eSAl Viro 				dput(dentry);
153894bdd655SAl Viro 				goto again;
153994bdd655SAl Viro 			}
154094bdd655SAl Viro 			dput(dentry);
1541949a852eSAl Viro 			dentry = ERR_PTR(error);
1542949a852eSAl Viro 		}
154394bdd655SAl Viro 	} else {
15441936386eSAl Viro 		old = inode->i_op->lookup(inode, dentry, flags);
154585c7f810SAl Viro 		d_lookup_done(dentry);
15461936386eSAl Viro 		if (unlikely(old)) {
15471936386eSAl Viro 			dput(dentry);
15481936386eSAl Viro 			dentry = old;
1549949a852eSAl Viro 		}
1550949a852eSAl Viro 	}
1551e3c13928SAl Viro 	return dentry;
15521da177e4SLinus Torvalds }
15531da177e4SLinus Torvalds 
155488d8331aSAl Viro static struct dentry *lookup_slow(const struct qstr *name,
155588d8331aSAl Viro 				  struct dentry *dir,
155688d8331aSAl Viro 				  unsigned int flags)
155788d8331aSAl Viro {
155888d8331aSAl Viro 	struct inode *inode = dir->d_inode;
155988d8331aSAl Viro 	struct dentry *res;
156088d8331aSAl Viro 	inode_lock_shared(inode);
156188d8331aSAl Viro 	res = __lookup_slow(name, dir, flags);
156288d8331aSAl Viro 	inode_unlock_shared(inode);
156388d8331aSAl Viro 	return res;
156488d8331aSAl Viro }
156588d8331aSAl Viro 
156652094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
156752094c8aSAl Viro {
156852094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
15694ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
157052094c8aSAl Viro 		if (err != -ECHILD)
157152094c8aSAl Viro 			return err;
15724675ac39SAl Viro 		if (unlazy_walk(nd))
157352094c8aSAl Viro 			return -ECHILD;
157452094c8aSAl Viro 	}
15754ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
157652094c8aSAl Viro }
157752094c8aSAl Viro 
157849055906SAl Viro static int reserve_stack(struct nameidata *nd, struct path *link, unsigned seq)
1579d63ff28fSAl Viro {
158049055906SAl Viro 	if (unlikely(nd->total_link_count++ >= MAXSYMLINKS))
158149055906SAl Viro 		return -ELOOP;
15824542576bSAl Viro 
15834542576bSAl Viro 	if (likely(nd->depth != EMBEDDED_LEVELS))
15844542576bSAl Viro 		return 0;
15854542576bSAl Viro 	if (likely(nd->stack != nd->internal))
15864542576bSAl Viro 		return 0;
158760ef60c7SAl Viro 	if (likely(nd_alloc_stack(nd)))
158849055906SAl Viro 		return 0;
158960ef60c7SAl Viro 
159060ef60c7SAl Viro 	if (nd->flags & LOOKUP_RCU) {
159160ef60c7SAl Viro 		// we need to grab link before we do unlazy.  And we can't skip
159260ef60c7SAl Viro 		// unlazy even if we fail to grab the link - cleanup needs it
1593aef9404dSAl Viro 		bool grabbed_link = legitimize_path(nd, link, seq);
159460ef60c7SAl Viro 
159560ef60c7SAl Viro 		if (unlazy_walk(nd) != 0 || !grabbed_link)
159660ef60c7SAl Viro 			return -ECHILD;
159760ef60c7SAl Viro 
159860ef60c7SAl Viro 		if (nd_alloc_stack(nd))
159960ef60c7SAl Viro 			return 0;
1600bc40aee0SAl Viro 	}
160160ef60c7SAl Viro 	return -ENOMEM;
160249055906SAl Viro }
160349055906SAl Viro 
160449055906SAl Viro enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4};
160549055906SAl Viro 
160649055906SAl Viro static const char *pick_link(struct nameidata *nd, struct path *link,
160749055906SAl Viro 		     struct inode *inode, unsigned seq, int flags)
160849055906SAl Viro {
160949055906SAl Viro 	struct saved *last;
161049055906SAl Viro 	const char *res;
161149055906SAl Viro 	int error = reserve_stack(nd, link, seq);
161249055906SAl Viro 
161349055906SAl Viro 	if (unlikely(error)) {
161449055906SAl Viro 		if (!(nd->flags & LOOKUP_RCU))
1615cd179f44SAl Viro 			path_put(link);
161606708adbSAl Viro 		return ERR_PTR(error);
1617626de996SAl Viro 	}
1618ab104923SAl Viro 	last = nd->stack + nd->depth++;
16191cf2665bSAl Viro 	last->link = *link;
1620fceef393SAl Viro 	clear_delayed_call(&last->done);
16210450b2d1SAl Viro 	last->seq = seq;
1622ad6cc4c3SAl Viro 
1623b1a81972SAl Viro 	if (flags & WALK_TRAILING) {
1624ad6cc4c3SAl Viro 		error = may_follow_link(nd, inode);
1625ad6cc4c3SAl Viro 		if (unlikely(error))
1626ad6cc4c3SAl Viro 			return ERR_PTR(error);
1627ad6cc4c3SAl Viro 	}
1628ad6cc4c3SAl Viro 
1629dab741e0SMattias Nissler 	if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) ||
1630dab741e0SMattias Nissler 			unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW))
1631ad6cc4c3SAl Viro 		return ERR_PTR(-ELOOP);
1632ad6cc4c3SAl Viro 
1633ad6cc4c3SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1634ad6cc4c3SAl Viro 		touch_atime(&last->link);
1635ad6cc4c3SAl Viro 		cond_resched();
1636ad6cc4c3SAl Viro 	} else if (atime_needs_update(&last->link, inode)) {
1637ad6cc4c3SAl Viro 		if (unlikely(unlazy_walk(nd)))
1638ad6cc4c3SAl Viro 			return ERR_PTR(-ECHILD);
1639ad6cc4c3SAl Viro 		touch_atime(&last->link);
1640ad6cc4c3SAl Viro 	}
1641ad6cc4c3SAl Viro 
1642ad6cc4c3SAl Viro 	error = security_inode_follow_link(link->dentry, inode,
1643ad6cc4c3SAl Viro 					   nd->flags & LOOKUP_RCU);
1644ad6cc4c3SAl Viro 	if (unlikely(error))
1645ad6cc4c3SAl Viro 		return ERR_PTR(error);
1646ad6cc4c3SAl Viro 
1647ad6cc4c3SAl Viro 	res = READ_ONCE(inode->i_link);
1648ad6cc4c3SAl Viro 	if (!res) {
1649ad6cc4c3SAl Viro 		const char * (*get)(struct dentry *, struct inode *,
1650ad6cc4c3SAl Viro 				struct delayed_call *);
1651ad6cc4c3SAl Viro 		get = inode->i_op->get_link;
1652ad6cc4c3SAl Viro 		if (nd->flags & LOOKUP_RCU) {
1653ad6cc4c3SAl Viro 			res = get(NULL, inode, &last->done);
1654ad6cc4c3SAl Viro 			if (res == ERR_PTR(-ECHILD)) {
1655ad6cc4c3SAl Viro 				if (unlikely(unlazy_walk(nd)))
1656ad6cc4c3SAl Viro 					return ERR_PTR(-ECHILD);
1657ad6cc4c3SAl Viro 				res = get(link->dentry, inode, &last->done);
1658ad6cc4c3SAl Viro 			}
1659ad6cc4c3SAl Viro 		} else {
1660ad6cc4c3SAl Viro 			res = get(link->dentry, inode, &last->done);
1661ad6cc4c3SAl Viro 		}
1662ad6cc4c3SAl Viro 		if (!res)
1663ad6cc4c3SAl Viro 			goto all_done;
1664ad6cc4c3SAl Viro 		if (IS_ERR(res))
1665ad6cc4c3SAl Viro 			return res;
1666ad6cc4c3SAl Viro 	}
1667ad6cc4c3SAl Viro 	if (*res == '/') {
1668ad6cc4c3SAl Viro 		error = nd_jump_root(nd);
1669ad6cc4c3SAl Viro 		if (unlikely(error))
1670ad6cc4c3SAl Viro 			return ERR_PTR(error);
1671ad6cc4c3SAl Viro 		while (unlikely(*++res == '/'))
1672ad6cc4c3SAl Viro 			;
1673ad6cc4c3SAl Viro 	}
1674ad6cc4c3SAl Viro 	if (*res)
1675ad6cc4c3SAl Viro 		return res;
1676ad6cc4c3SAl Viro all_done: // pure jump
1677ad6cc4c3SAl Viro 	put_link(nd);
1678ad6cc4c3SAl Viro 	return NULL;
1679d63ff28fSAl Viro }
1680d63ff28fSAl Viro 
16813ddcd056SLinus Torvalds /*
16823ddcd056SLinus Torvalds  * Do we need to follow links? We _really_ want to be able
16833ddcd056SLinus Torvalds  * to do this check without having to look at inode->i_op,
16843ddcd056SLinus Torvalds  * so we keep a cache of "no, this doesn't need follow_link"
16853ddcd056SLinus Torvalds  * for the common case.
16863ddcd056SLinus Torvalds  */
1687b0417d2cSAl Viro static const char *step_into(struct nameidata *nd, int flags,
1688cbae4d12SAl Viro 		     struct dentry *dentry, struct inode *inode, unsigned seq)
16893ddcd056SLinus Torvalds {
1690cbae4d12SAl Viro 	struct path path;
1691cbae4d12SAl Viro 	int err = handle_mounts(nd, dentry, &path, &inode, &seq);
1692cbae4d12SAl Viro 
1693cbae4d12SAl Viro 	if (err < 0)
1694b0417d2cSAl Viro 		return ERR_PTR(err);
1695cbae4d12SAl Viro 	if (likely(!d_is_symlink(path.dentry)) ||
16968c4efe22SAl Viro 	   ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) ||
1697aca2903eSAl Viro 	   (flags & WALK_NOFOLLOW)) {
16988f64fb1cSAl Viro 		/* not a symlink or should not follow */
1699c99687a0SAl Viro 		if (!(nd->flags & LOOKUP_RCU)) {
1700c99687a0SAl Viro 			dput(nd->path.dentry);
1701c99687a0SAl Viro 			if (nd->path.mnt != path.mnt)
1702c99687a0SAl Viro 				mntput(nd->path.mnt);
1703c99687a0SAl Viro 		}
1704c99687a0SAl Viro 		nd->path = path;
17058f64fb1cSAl Viro 		nd->inode = inode;
17068f64fb1cSAl Viro 		nd->seq = seq;
1707b0417d2cSAl Viro 		return NULL;
17088f64fb1cSAl Viro 	}
1709a7f77542SAl Viro 	if (nd->flags & LOOKUP_RCU) {
171084f0cd9eSAl Viro 		/* make sure that d_is_symlink above matches inode */
1711cbae4d12SAl Viro 		if (read_seqcount_retry(&path.dentry->d_seq, seq))
1712b0417d2cSAl Viro 			return ERR_PTR(-ECHILD);
171384f0cd9eSAl Viro 	} else {
171484f0cd9eSAl Viro 		if (path.mnt == nd->path.mnt)
171584f0cd9eSAl Viro 			mntget(path.mnt);
1716a7f77542SAl Viro 	}
1717b1a81972SAl Viro 	return pick_link(nd, &path, inode, seq, flags);
17183ddcd056SLinus Torvalds }
17193ddcd056SLinus Torvalds 
1720c2df1968SAl Viro static struct dentry *follow_dotdot_rcu(struct nameidata *nd,
1721c2df1968SAl Viro 					struct inode **inodep,
1722c2df1968SAl Viro 					unsigned *seqp)
1723957dd41dSAl Viro {
172412487f30SAl Viro 	struct dentry *parent, *old;
1725957dd41dSAl Viro 
172612487f30SAl Viro 	if (path_equal(&nd->path, &nd->root))
172712487f30SAl Viro 		goto in_root;
172812487f30SAl Viro 	if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
17297ef482faSAl Viro 		struct path path;
1730efe772d6SAl Viro 		unsigned seq;
17317ef482faSAl Viro 		if (!choose_mountpoint_rcu(real_mount(nd->path.mnt),
17327ef482faSAl Viro 					   &nd->root, &path, &seq))
173312487f30SAl Viro 			goto in_root;
1734efe772d6SAl Viro 		if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1735efe772d6SAl Viro 			return ERR_PTR(-ECHILD);
1736efe772d6SAl Viro 		nd->path = path;
1737efe772d6SAl Viro 		nd->inode = path.dentry->d_inode;
1738efe772d6SAl Viro 		nd->seq = seq;
1739efe772d6SAl Viro 		if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1740efe772d6SAl Viro 			return ERR_PTR(-ECHILD);
1741efe772d6SAl Viro 		/* we know that mountpoint was pinned */
1742957dd41dSAl Viro 	}
174312487f30SAl Viro 	old = nd->path.dentry;
174412487f30SAl Viro 	parent = old->d_parent;
174512487f30SAl Viro 	*inodep = parent->d_inode;
174612487f30SAl Viro 	*seqp = read_seqcount_begin(&parent->d_seq);
174712487f30SAl Viro 	if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
174812487f30SAl Viro 		return ERR_PTR(-ECHILD);
174912487f30SAl Viro 	if (unlikely(!path_connected(nd->path.mnt, parent)))
175012487f30SAl Viro 		return ERR_PTR(-ECHILD);
175112487f30SAl Viro 	return parent;
175212487f30SAl Viro in_root:
1753efe772d6SAl Viro 	if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1754efe772d6SAl Viro 		return ERR_PTR(-ECHILD);
1755957dd41dSAl Viro 	if (unlikely(nd->flags & LOOKUP_BENEATH))
17567521f22bSAl Viro 		return ERR_PTR(-ECHILD);
1757c2df1968SAl Viro 	return NULL;
1758957dd41dSAl Viro }
1759957dd41dSAl Viro 
1760c2df1968SAl Viro static struct dentry *follow_dotdot(struct nameidata *nd,
1761c2df1968SAl Viro 				 struct inode **inodep,
1762c2df1968SAl Viro 				 unsigned *seqp)
1763957dd41dSAl Viro {
176412487f30SAl Viro 	struct dentry *parent;
176512487f30SAl Viro 
1766957dd41dSAl Viro 	if (path_equal(&nd->path, &nd->root))
176712487f30SAl Viro 		goto in_root;
176812487f30SAl Viro 	if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
17692aa38470SAl Viro 		struct path path;
17702aa38470SAl Viro 
17712aa38470SAl Viro 		if (!choose_mountpoint(real_mount(nd->path.mnt),
17722aa38470SAl Viro 				       &nd->root, &path))
177312487f30SAl Viro 			goto in_root;
1774165200d6SAl Viro 		path_put(&nd->path);
1775165200d6SAl Viro 		nd->path = path;
17762aa38470SAl Viro 		nd->inode = path.dentry->d_inode;
1777165200d6SAl Viro 		if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1778165200d6SAl Viro 			return ERR_PTR(-EXDEV);
177912487f30SAl Viro 	}
1780957dd41dSAl Viro 	/* rare case of legitimate dget_parent()... */
178112487f30SAl Viro 	parent = dget_parent(nd->path.dentry);
1782957dd41dSAl Viro 	if (unlikely(!path_connected(nd->path.mnt, parent))) {
1783957dd41dSAl Viro 		dput(parent);
17847521f22bSAl Viro 		return ERR_PTR(-ENOENT);
1785957dd41dSAl Viro 	}
1786c2df1968SAl Viro 	*seqp = 0;
1787c2df1968SAl Viro 	*inodep = parent->d_inode;
1788c2df1968SAl Viro 	return parent;
178912487f30SAl Viro 
179012487f30SAl Viro in_root:
1791957dd41dSAl Viro 	if (unlikely(nd->flags & LOOKUP_BENEATH))
17927521f22bSAl Viro 		return ERR_PTR(-EXDEV);
1793c2df1968SAl Viro 	dget(nd->path.dentry);
1794c2df1968SAl Viro 	return NULL;
1795957dd41dSAl Viro }
1796957dd41dSAl Viro 
17977521f22bSAl Viro static const char *handle_dots(struct nameidata *nd, int type)
1798957dd41dSAl Viro {
1799957dd41dSAl Viro 	if (type == LAST_DOTDOT) {
18007521f22bSAl Viro 		const char *error = NULL;
1801c2df1968SAl Viro 		struct dentry *parent;
1802c2df1968SAl Viro 		struct inode *inode;
1803c2df1968SAl Viro 		unsigned seq;
1804957dd41dSAl Viro 
1805957dd41dSAl Viro 		if (!nd->root.mnt) {
18067521f22bSAl Viro 			error = ERR_PTR(set_root(nd));
1807957dd41dSAl Viro 			if (error)
1808957dd41dSAl Viro 				return error;
1809957dd41dSAl Viro 		}
1810957dd41dSAl Viro 		if (nd->flags & LOOKUP_RCU)
1811c2df1968SAl Viro 			parent = follow_dotdot_rcu(nd, &inode, &seq);
1812957dd41dSAl Viro 		else
1813c2df1968SAl Viro 			parent = follow_dotdot(nd, &inode, &seq);
1814c2df1968SAl Viro 		if (IS_ERR(parent))
1815c2df1968SAl Viro 			return ERR_CAST(parent);
1816c2df1968SAl Viro 		if (unlikely(!parent))
1817c2df1968SAl Viro 			error = step_into(nd, WALK_NOFOLLOW,
1818c2df1968SAl Viro 					 nd->path.dentry, nd->inode, nd->seq);
1819c2df1968SAl Viro 		else
1820c2df1968SAl Viro 			error = step_into(nd, WALK_NOFOLLOW,
1821c2df1968SAl Viro 					 parent, inode, seq);
1822c2df1968SAl Viro 		if (unlikely(error))
1823957dd41dSAl Viro 			return error;
1824957dd41dSAl Viro 
1825957dd41dSAl Viro 		if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
1826957dd41dSAl Viro 			/*
1827957dd41dSAl Viro 			 * If there was a racing rename or mount along our
1828957dd41dSAl Viro 			 * path, then we can't be sure that ".." hasn't jumped
1829957dd41dSAl Viro 			 * above nd->root (and so userspace should retry or use
1830957dd41dSAl Viro 			 * some fallback).
1831957dd41dSAl Viro 			 */
1832957dd41dSAl Viro 			smp_rmb();
1833957dd41dSAl Viro 			if (unlikely(__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq)))
18347521f22bSAl Viro 				return ERR_PTR(-EAGAIN);
1835957dd41dSAl Viro 			if (unlikely(__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq)))
18367521f22bSAl Viro 				return ERR_PTR(-EAGAIN);
1837957dd41dSAl Viro 		}
1838957dd41dSAl Viro 	}
18397521f22bSAl Viro 	return NULL;
1840957dd41dSAl Viro }
1841957dd41dSAl Viro 
184292d27016SAl Viro static const char *walk_component(struct nameidata *nd, int flags)
1843ce57dfc1SAl Viro {
1844db3c9adeSAl Viro 	struct dentry *dentry;
1845ce57dfc1SAl Viro 	struct inode *inode;
1846254cf582SAl Viro 	unsigned seq;
1847ce57dfc1SAl Viro 	/*
1848ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1849ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1850ce57dfc1SAl Viro 	 * parent relationships.
1851ce57dfc1SAl Viro 	 */
18524693a547SAl Viro 	if (unlikely(nd->last_type != LAST_NORM)) {
18531c4ff1a8SAl Viro 		if (!(flags & WALK_MORE) && nd->depth)
18544693a547SAl Viro 			put_link(nd);
18557521f22bSAl Viro 		return handle_dots(nd, nd->last_type);
18564693a547SAl Viro 	}
185720e34357SAl Viro 	dentry = lookup_fast(nd, &inode, &seq);
185820e34357SAl Viro 	if (IS_ERR(dentry))
185992d27016SAl Viro 		return ERR_CAST(dentry);
186020e34357SAl Viro 	if (unlikely(!dentry)) {
1861db3c9adeSAl Viro 		dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags);
1862db3c9adeSAl Viro 		if (IS_ERR(dentry))
186392d27016SAl Viro 			return ERR_CAST(dentry);
186420e34357SAl Viro 	}
186556676ec3SAl Viro 	if (!(flags & WALK_MORE) && nd->depth)
186656676ec3SAl Viro 		put_link(nd);
1867b0417d2cSAl Viro 	return step_into(nd, flags, dentry, inode, seq);
1868ce57dfc1SAl Viro }
1869ce57dfc1SAl Viro 
18701da177e4SLinus Torvalds /*
1871bfcfaa77SLinus Torvalds  * We can do the critical dentry name comparison and hashing
1872bfcfaa77SLinus Torvalds  * operations one word at a time, but we are limited to:
1873bfcfaa77SLinus Torvalds  *
1874bfcfaa77SLinus Torvalds  * - Architectures with fast unaligned word accesses. We could
1875bfcfaa77SLinus Torvalds  *   do a "get_unaligned()" if this helps and is sufficiently
1876bfcfaa77SLinus Torvalds  *   fast.
1877bfcfaa77SLinus Torvalds  *
1878bfcfaa77SLinus Torvalds  * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1879bfcfaa77SLinus Torvalds  *   do not trap on the (extremely unlikely) case of a page
1880bfcfaa77SLinus Torvalds  *   crossing operation.
1881bfcfaa77SLinus Torvalds  *
1882bfcfaa77SLinus Torvalds  * - Furthermore, we need an efficient 64-bit compile for the
1883bfcfaa77SLinus Torvalds  *   64-bit case in order to generate the "number of bytes in
1884bfcfaa77SLinus Torvalds  *   the final mask". Again, that could be replaced with a
1885bfcfaa77SLinus Torvalds  *   efficient population count instruction or similar.
1886bfcfaa77SLinus Torvalds  */
1887bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS
1888bfcfaa77SLinus Torvalds 
1889f68e556eSLinus Torvalds #include <asm/word-at-a-time.h>
1890bfcfaa77SLinus Torvalds 
1891468a9428SGeorge Spelvin #ifdef HASH_MIX
1892bfcfaa77SLinus Torvalds 
1893468a9428SGeorge Spelvin /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
1894468a9428SGeorge Spelvin 
1895468a9428SGeorge Spelvin #elif defined(CONFIG_64BIT)
18962a18da7aSGeorge Spelvin /*
18972a18da7aSGeorge Spelvin  * Register pressure in the mixing function is an issue, particularly
18982a18da7aSGeorge Spelvin  * on 32-bit x86, but almost any function requires one state value and
18992a18da7aSGeorge Spelvin  * one temporary.  Instead, use a function designed for two state values
19002a18da7aSGeorge Spelvin  * and no temporaries.
19012a18da7aSGeorge Spelvin  *
19022a18da7aSGeorge Spelvin  * This function cannot create a collision in only two iterations, so
19032a18da7aSGeorge Spelvin  * we have two iterations to achieve avalanche.  In those two iterations,
19042a18da7aSGeorge Spelvin  * we have six layers of mixing, which is enough to spread one bit's
19052a18da7aSGeorge Spelvin  * influence out to 2^6 = 64 state bits.
19062a18da7aSGeorge Spelvin  *
19072a18da7aSGeorge Spelvin  * Rotate constants are scored by considering either 64 one-bit input
19082a18da7aSGeorge Spelvin  * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
19092a18da7aSGeorge Spelvin  * probability of that delta causing a change to each of the 128 output
19102a18da7aSGeorge Spelvin  * bits, using a sample of random initial states.
19112a18da7aSGeorge Spelvin  *
19122a18da7aSGeorge Spelvin  * The Shannon entropy of the computed probabilities is then summed
19132a18da7aSGeorge Spelvin  * to produce a score.  Ideally, any input change has a 50% chance of
19142a18da7aSGeorge Spelvin  * toggling any given output bit.
19152a18da7aSGeorge Spelvin  *
19162a18da7aSGeorge Spelvin  * Mixing scores (in bits) for (12,45):
19172a18da7aSGeorge Spelvin  * Input delta: 1-bit      2-bit
19182a18da7aSGeorge Spelvin  * 1 round:     713.3    42542.6
19192a18da7aSGeorge Spelvin  * 2 rounds:   2753.7   140389.8
19202a18da7aSGeorge Spelvin  * 3 rounds:   5954.1   233458.2
19212a18da7aSGeorge Spelvin  * 4 rounds:   7862.6   256672.2
19222a18da7aSGeorge Spelvin  * Perfect:    8192     258048
19232a18da7aSGeorge Spelvin  *            (64*128) (64*63/2 * 128)
19242a18da7aSGeorge Spelvin  */
19252a18da7aSGeorge Spelvin #define HASH_MIX(x, y, a)	\
19262a18da7aSGeorge Spelvin 	(	x ^= (a),	\
19272a18da7aSGeorge Spelvin 	y ^= x,	x = rol64(x,12),\
19282a18da7aSGeorge Spelvin 	x += y,	y = rol64(y,45),\
19292a18da7aSGeorge Spelvin 	y *= 9			)
1930bfcfaa77SLinus Torvalds 
19310fed3ac8SGeorge Spelvin /*
19322a18da7aSGeorge Spelvin  * Fold two longs into one 32-bit hash value.  This must be fast, but
19332a18da7aSGeorge Spelvin  * latency isn't quite as critical, as there is a fair bit of additional
19342a18da7aSGeorge Spelvin  * work done before the hash value is used.
19350fed3ac8SGeorge Spelvin  */
19362a18da7aSGeorge Spelvin static inline unsigned int fold_hash(unsigned long x, unsigned long y)
19370fed3ac8SGeorge Spelvin {
19382a18da7aSGeorge Spelvin 	y ^= x * GOLDEN_RATIO_64;
19392a18da7aSGeorge Spelvin 	y *= GOLDEN_RATIO_64;
19402a18da7aSGeorge Spelvin 	return y >> 32;
19410fed3ac8SGeorge Spelvin }
19420fed3ac8SGeorge Spelvin 
1943bfcfaa77SLinus Torvalds #else	/* 32-bit case */
1944bfcfaa77SLinus Torvalds 
19452a18da7aSGeorge Spelvin /*
19462a18da7aSGeorge Spelvin  * Mixing scores (in bits) for (7,20):
19472a18da7aSGeorge Spelvin  * Input delta: 1-bit      2-bit
19482a18da7aSGeorge Spelvin  * 1 round:     330.3     9201.6
19492a18da7aSGeorge Spelvin  * 2 rounds:   1246.4    25475.4
19502a18da7aSGeorge Spelvin  * 3 rounds:   1907.1    31295.1
19512a18da7aSGeorge Spelvin  * 4 rounds:   2042.3    31718.6
19522a18da7aSGeorge Spelvin  * Perfect:    2048      31744
19532a18da7aSGeorge Spelvin  *            (32*64)   (32*31/2 * 64)
19542a18da7aSGeorge Spelvin  */
19552a18da7aSGeorge Spelvin #define HASH_MIX(x, y, a)	\
19562a18da7aSGeorge Spelvin 	(	x ^= (a),	\
19572a18da7aSGeorge Spelvin 	y ^= x,	x = rol32(x, 7),\
19582a18da7aSGeorge Spelvin 	x += y,	y = rol32(y,20),\
19592a18da7aSGeorge Spelvin 	y *= 9			)
1960bfcfaa77SLinus Torvalds 
19612a18da7aSGeorge Spelvin static inline unsigned int fold_hash(unsigned long x, unsigned long y)
19620fed3ac8SGeorge Spelvin {
19632a18da7aSGeorge Spelvin 	/* Use arch-optimized multiply if one exists */
19642a18da7aSGeorge Spelvin 	return __hash_32(y ^ __hash_32(x));
19650fed3ac8SGeorge Spelvin }
19660fed3ac8SGeorge Spelvin 
1967bfcfaa77SLinus Torvalds #endif
1968bfcfaa77SLinus Torvalds 
19692a18da7aSGeorge Spelvin /*
19702a18da7aSGeorge Spelvin  * Return the hash of a string of known length.  This is carfully
19712a18da7aSGeorge Spelvin  * designed to match hash_name(), which is the more critical function.
19722a18da7aSGeorge Spelvin  * In particular, we must end by hashing a final word containing 0..7
19732a18da7aSGeorge Spelvin  * payload bytes, to match the way that hash_name() iterates until it
19742a18da7aSGeorge Spelvin  * finds the delimiter after the name.
19752a18da7aSGeorge Spelvin  */
19768387ff25SLinus Torvalds unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
1977bfcfaa77SLinus Torvalds {
19788387ff25SLinus Torvalds 	unsigned long a, x = 0, y = (unsigned long)salt;
1979bfcfaa77SLinus Torvalds 
1980bfcfaa77SLinus Torvalds 	for (;;) {
1981fcfd2fbfSGeorge Spelvin 		if (!len)
1982fcfd2fbfSGeorge Spelvin 			goto done;
1983e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name);
1984bfcfaa77SLinus Torvalds 		if (len < sizeof(unsigned long))
1985bfcfaa77SLinus Torvalds 			break;
19862a18da7aSGeorge Spelvin 		HASH_MIX(x, y, a);
1987bfcfaa77SLinus Torvalds 		name += sizeof(unsigned long);
1988bfcfaa77SLinus Torvalds 		len -= sizeof(unsigned long);
1989bfcfaa77SLinus Torvalds 	}
19902a18da7aSGeorge Spelvin 	x ^= a & bytemask_from_count(len);
1991bfcfaa77SLinus Torvalds done:
19922a18da7aSGeorge Spelvin 	return fold_hash(x, y);
1993bfcfaa77SLinus Torvalds }
1994bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
1995bfcfaa77SLinus Torvalds 
1996fcfd2fbfSGeorge Spelvin /* Return the "hash_len" (hash and length) of a null-terminated string */
19978387ff25SLinus Torvalds u64 hashlen_string(const void *salt, const char *name)
1998fcfd2fbfSGeorge Spelvin {
19998387ff25SLinus Torvalds 	unsigned long a = 0, x = 0, y = (unsigned long)salt;
20008387ff25SLinus Torvalds 	unsigned long adata, mask, len;
2001fcfd2fbfSGeorge Spelvin 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
2002fcfd2fbfSGeorge Spelvin 
20038387ff25SLinus Torvalds 	len = 0;
20048387ff25SLinus Torvalds 	goto inside;
20058387ff25SLinus Torvalds 
2006fcfd2fbfSGeorge Spelvin 	do {
20072a18da7aSGeorge Spelvin 		HASH_MIX(x, y, a);
2008fcfd2fbfSGeorge Spelvin 		len += sizeof(unsigned long);
20098387ff25SLinus Torvalds inside:
2010fcfd2fbfSGeorge Spelvin 		a = load_unaligned_zeropad(name+len);
2011fcfd2fbfSGeorge Spelvin 	} while (!has_zero(a, &adata, &constants));
2012fcfd2fbfSGeorge Spelvin 
2013fcfd2fbfSGeorge Spelvin 	adata = prep_zero_mask(a, adata, &constants);
2014fcfd2fbfSGeorge Spelvin 	mask = create_zero_mask(adata);
20152a18da7aSGeorge Spelvin 	x ^= a & zero_bytemask(mask);
2016fcfd2fbfSGeorge Spelvin 
20172a18da7aSGeorge Spelvin 	return hashlen_create(fold_hash(x, y), len + find_zero(mask));
2018fcfd2fbfSGeorge Spelvin }
2019fcfd2fbfSGeorge Spelvin EXPORT_SYMBOL(hashlen_string);
2020fcfd2fbfSGeorge Spelvin 
2021bfcfaa77SLinus Torvalds /*
2022bfcfaa77SLinus Torvalds  * Calculate the length and hash of the path component, and
2023d6bb3e90SLinus Torvalds  * return the "hash_len" as the result.
2024bfcfaa77SLinus Torvalds  */
20258387ff25SLinus Torvalds static inline u64 hash_name(const void *salt, const char *name)
2026bfcfaa77SLinus Torvalds {
20278387ff25SLinus Torvalds 	unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
20288387ff25SLinus Torvalds 	unsigned long adata, bdata, mask, len;
202936126f8fSLinus Torvalds 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
2030bfcfaa77SLinus Torvalds 
20318387ff25SLinus Torvalds 	len = 0;
20328387ff25SLinus Torvalds 	goto inside;
20338387ff25SLinus Torvalds 
2034bfcfaa77SLinus Torvalds 	do {
20352a18da7aSGeorge Spelvin 		HASH_MIX(x, y, a);
2036bfcfaa77SLinus Torvalds 		len += sizeof(unsigned long);
20378387ff25SLinus Torvalds inside:
2038e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name+len);
203936126f8fSLinus Torvalds 		b = a ^ REPEAT_BYTE('/');
204036126f8fSLinus Torvalds 	} while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
2041bfcfaa77SLinus Torvalds 
204236126f8fSLinus Torvalds 	adata = prep_zero_mask(a, adata, &constants);
204336126f8fSLinus Torvalds 	bdata = prep_zero_mask(b, bdata, &constants);
204436126f8fSLinus Torvalds 	mask = create_zero_mask(adata | bdata);
20452a18da7aSGeorge Spelvin 	x ^= a & zero_bytemask(mask);
204636126f8fSLinus Torvalds 
20472a18da7aSGeorge Spelvin 	return hashlen_create(fold_hash(x, y), len + find_zero(mask));
2048bfcfaa77SLinus Torvalds }
2049bfcfaa77SLinus Torvalds 
20502a18da7aSGeorge Spelvin #else	/* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
2051bfcfaa77SLinus Torvalds 
2052fcfd2fbfSGeorge Spelvin /* Return the hash of a string of known length */
20538387ff25SLinus Torvalds unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
20540145acc2SLinus Torvalds {
20558387ff25SLinus Torvalds 	unsigned long hash = init_name_hash(salt);
20560145acc2SLinus Torvalds 	while (len--)
2057fcfd2fbfSGeorge Spelvin 		hash = partial_name_hash((unsigned char)*name++, hash);
20580145acc2SLinus Torvalds 	return end_name_hash(hash);
20590145acc2SLinus Torvalds }
2060ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
20610145acc2SLinus Torvalds 
2062fcfd2fbfSGeorge Spelvin /* Return the "hash_len" (hash and length) of a null-terminated string */
20638387ff25SLinus Torvalds u64 hashlen_string(const void *salt, const char *name)
2064fcfd2fbfSGeorge Spelvin {
20658387ff25SLinus Torvalds 	unsigned long hash = init_name_hash(salt);
2066fcfd2fbfSGeorge Spelvin 	unsigned long len = 0, c;
2067fcfd2fbfSGeorge Spelvin 
2068fcfd2fbfSGeorge Spelvin 	c = (unsigned char)*name;
2069e0ab7af9SGeorge Spelvin 	while (c) {
2070fcfd2fbfSGeorge Spelvin 		len++;
2071fcfd2fbfSGeorge Spelvin 		hash = partial_name_hash(c, hash);
2072fcfd2fbfSGeorge Spelvin 		c = (unsigned char)name[len];
2073e0ab7af9SGeorge Spelvin 	}
2074fcfd2fbfSGeorge Spelvin 	return hashlen_create(end_name_hash(hash), len);
2075fcfd2fbfSGeorge Spelvin }
2076f2a031b6SGeorge Spelvin EXPORT_SYMBOL(hashlen_string);
2077fcfd2fbfSGeorge Spelvin 
20783ddcd056SLinus Torvalds /*
2079200e9ef7SLinus Torvalds  * We know there's a real path component here of at least
2080200e9ef7SLinus Torvalds  * one character.
2081200e9ef7SLinus Torvalds  */
20828387ff25SLinus Torvalds static inline u64 hash_name(const void *salt, const char *name)
2083200e9ef7SLinus Torvalds {
20848387ff25SLinus Torvalds 	unsigned long hash = init_name_hash(salt);
2085200e9ef7SLinus Torvalds 	unsigned long len = 0, c;
2086200e9ef7SLinus Torvalds 
2087200e9ef7SLinus Torvalds 	c = (unsigned char)*name;
2088200e9ef7SLinus Torvalds 	do {
2089200e9ef7SLinus Torvalds 		len++;
2090200e9ef7SLinus Torvalds 		hash = partial_name_hash(c, hash);
2091200e9ef7SLinus Torvalds 		c = (unsigned char)name[len];
2092200e9ef7SLinus Torvalds 	} while (c && c != '/');
2093d6bb3e90SLinus Torvalds 	return hashlen_create(end_name_hash(hash), len);
2094200e9ef7SLinus Torvalds }
2095200e9ef7SLinus Torvalds 
2096bfcfaa77SLinus Torvalds #endif
2097bfcfaa77SLinus Torvalds 
2098200e9ef7SLinus Torvalds /*
20991da177e4SLinus Torvalds  * Name resolution.
2100ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
2101ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
21021da177e4SLinus Torvalds  *
2103ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
2104ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
21051da177e4SLinus Torvalds  */
21066de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
21071da177e4SLinus Torvalds {
2108d8d4611aSAl Viro 	int depth = 0; // depth <= nd->depth
21091da177e4SLinus Torvalds 	int err;
21101da177e4SLinus Torvalds 
2111b4c03536SAl Viro 	nd->last_type = LAST_ROOT;
2112c108837eSAl Viro 	nd->flags |= LOOKUP_PARENT;
21139b5858e9SAl Viro 	if (IS_ERR(name))
21149b5858e9SAl Viro 		return PTR_ERR(name);
21151da177e4SLinus Torvalds 	while (*name=='/')
21161da177e4SLinus Torvalds 		name++;
21171da177e4SLinus Torvalds 	if (!*name)
21189e18f10aSAl Viro 		return 0;
21191da177e4SLinus Torvalds 
21201da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
21211da177e4SLinus Torvalds 	for(;;) {
212292d27016SAl Viro 		const char *link;
2123d6bb3e90SLinus Torvalds 		u64 hash_len;
2124fe479a58SAl Viro 		int type;
21251da177e4SLinus Torvalds 
212652094c8aSAl Viro 		err = may_lookup(nd);
21271da177e4SLinus Torvalds 		if (err)
21283595e234SAl Viro 			return err;
21291da177e4SLinus Torvalds 
21308387ff25SLinus Torvalds 		hash_len = hash_name(nd->path.dentry, name);
21311da177e4SLinus Torvalds 
2132fe479a58SAl Viro 		type = LAST_NORM;
2133d6bb3e90SLinus Torvalds 		if (name[0] == '.') switch (hashlen_len(hash_len)) {
2134fe479a58SAl Viro 			case 2:
2135200e9ef7SLinus Torvalds 				if (name[1] == '.') {
2136fe479a58SAl Viro 					type = LAST_DOTDOT;
213716c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
213816c2cd71SAl Viro 				}
2139fe479a58SAl Viro 				break;
2140fe479a58SAl Viro 			case 1:
2141fe479a58SAl Viro 				type = LAST_DOT;
2142fe479a58SAl Viro 		}
21435a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
21445a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
214516c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
21465a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
2147a060dc50SJames Hogan 				struct qstr this = { { .hash_len = hash_len }, .name = name };
2148da53be12SLinus Torvalds 				err = parent->d_op->d_hash(parent, &this);
21495a202bcdSAl Viro 				if (err < 0)
21503595e234SAl Viro 					return err;
2151d6bb3e90SLinus Torvalds 				hash_len = this.hash_len;
2152d6bb3e90SLinus Torvalds 				name = this.name;
21535a202bcdSAl Viro 			}
21545a202bcdSAl Viro 		}
2155fe479a58SAl Viro 
2156d6bb3e90SLinus Torvalds 		nd->last.hash_len = hash_len;
2157d6bb3e90SLinus Torvalds 		nd->last.name = name;
21585f4a6a69SAl Viro 		nd->last_type = type;
21595f4a6a69SAl Viro 
2160d6bb3e90SLinus Torvalds 		name += hashlen_len(hash_len);
2161d6bb3e90SLinus Torvalds 		if (!*name)
2162bdf6cbf1SAl Viro 			goto OK;
2163200e9ef7SLinus Torvalds 		/*
2164200e9ef7SLinus Torvalds 		 * If it wasn't NUL, we know it was '/'. Skip that
2165200e9ef7SLinus Torvalds 		 * slash, and continue until no more slashes.
2166200e9ef7SLinus Torvalds 		 */
2167200e9ef7SLinus Torvalds 		do {
2168d6bb3e90SLinus Torvalds 			name++;
2169d6bb3e90SLinus Torvalds 		} while (unlikely(*name == '/'));
21708620c238SAl Viro 		if (unlikely(!*name)) {
21718620c238SAl Viro OK:
2172d8d4611aSAl Viro 			/* pathname or trailing symlink, done */
2173c108837eSAl Viro 			if (!depth) {
21740f705953SAl Viro 				nd->dir_uid = nd->inode->i_uid;
21750f705953SAl Viro 				nd->dir_mode = nd->inode->i_mode;
2176c108837eSAl Viro 				nd->flags &= ~LOOKUP_PARENT;
21778620c238SAl Viro 				return 0;
2178c108837eSAl Viro 			}
21798620c238SAl Viro 			/* last component of nested symlink */
2180d8d4611aSAl Viro 			name = nd->stack[--depth].name;
21818c4efe22SAl Viro 			link = walk_component(nd, 0);
21821c4ff1a8SAl Viro 		} else {
21831c4ff1a8SAl Viro 			/* not the last component */
21848c4efe22SAl Viro 			link = walk_component(nd, WALK_MORE);
21858620c238SAl Viro 		}
218692d27016SAl Viro 		if (unlikely(link)) {
218792d27016SAl Viro 			if (IS_ERR(link))
218892d27016SAl Viro 				return PTR_ERR(link);
218992d27016SAl Viro 			/* a symlink to follow */
2190d8d4611aSAl Viro 			nd->stack[depth++].name = name;
219192d27016SAl Viro 			name = link;
21929e18f10aSAl Viro 			continue;
219348c8b0c5SAl Viro 		}
219497242f99SAl Viro 		if (unlikely(!d_can_lookup(nd->path.dentry))) {
219597242f99SAl Viro 			if (nd->flags & LOOKUP_RCU) {
21964675ac39SAl Viro 				if (unlazy_walk(nd))
219797242f99SAl Viro 					return -ECHILD;
219897242f99SAl Viro 			}
21993595e234SAl Viro 			return -ENOTDIR;
22005f4a6a69SAl Viro 		}
2201ce57dfc1SAl Viro 	}
220297242f99SAl Viro }
22031da177e4SLinus Torvalds 
2204edc2b1daSAl Viro /* must be paired with terminate_walk() */
2205c8a53ee5SAl Viro static const char *path_init(struct nameidata *nd, unsigned flags)
220631e6b01fSNick Piggin {
2207740a1678SAleksa Sarai 	int error;
2208c8a53ee5SAl Viro 	const char *s = nd->name->name;
220931e6b01fSNick Piggin 
2210c0eb027eSLinus Torvalds 	if (!*s)
2211c0eb027eSLinus Torvalds 		flags &= ~LOOKUP_RCU;
2212edc2b1daSAl Viro 	if (flags & LOOKUP_RCU)
2213edc2b1daSAl Viro 		rcu_read_lock();
2214c0eb027eSLinus Torvalds 
2215c108837eSAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
221631e6b01fSNick Piggin 	nd->depth = 0;
2217ab87f9a5SAleksa Sarai 
2218ab87f9a5SAleksa Sarai 	nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount);
2219ab87f9a5SAleksa Sarai 	nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount);
2220ab87f9a5SAleksa Sarai 	smp_rmb();
2221ab87f9a5SAleksa Sarai 
22225b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
2223b18825a7SDavid Howells 		struct dentry *root = nd->root.dentry;
2224b18825a7SDavid Howells 		struct inode *inode = root->d_inode;
222593893862SAl Viro 		if (*s && unlikely(!d_can_lookup(root)))
2226368ee9baSAl Viro 			return ERR_PTR(-ENOTDIR);
22275b6ca027SAl Viro 		nd->path = nd->root;
22285b6ca027SAl Viro 		nd->inode = inode;
22295b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
2230ab87f9a5SAleksa Sarai 			nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
22318f47a016SAl Viro 			nd->root_seq = nd->seq;
22325b6ca027SAl Viro 		} else {
22335b6ca027SAl Viro 			path_get(&nd->path);
22345b6ca027SAl Viro 		}
2235368ee9baSAl Viro 		return s;
22365b6ca027SAl Viro 	}
22375b6ca027SAl Viro 
223831e6b01fSNick Piggin 	nd->root.mnt = NULL;
2239248fb5b9SAl Viro 	nd->path.mnt = NULL;
2240248fb5b9SAl Viro 	nd->path.dentry = NULL;
224131e6b01fSNick Piggin 
22428db52c7eSAleksa Sarai 	/* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */
22438db52c7eSAleksa Sarai 	if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) {
2244740a1678SAleksa Sarai 		error = nd_jump_root(nd);
2245740a1678SAleksa Sarai 		if (unlikely(error))
2246740a1678SAleksa Sarai 			return ERR_PTR(error);
2247ef55d917SAl Viro 		return s;
22488db52c7eSAleksa Sarai 	}
22498db52c7eSAleksa Sarai 
22508db52c7eSAleksa Sarai 	/* Relative pathname -- get the starting-point it is relative to. */
22518db52c7eSAleksa Sarai 	if (nd->dfd == AT_FDCWD) {
2252e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
225331e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
2254c28cc364SNick Piggin 			unsigned seq;
225531e6b01fSNick Piggin 
2256c28cc364SNick Piggin 			do {
2257c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
225831e6b01fSNick Piggin 				nd->path = fs->pwd;
2259ef55d917SAl Viro 				nd->inode = nd->path.dentry->d_inode;
2260c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2261c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
2262e41f7d4eSAl Viro 		} else {
2263e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
2264ef55d917SAl Viro 			nd->inode = nd->path.dentry->d_inode;
2265e41f7d4eSAl Viro 		}
226631e6b01fSNick Piggin 	} else {
2267582aa64aSJeff Layton 		/* Caller must check execute permissions on the starting path component */
2268c8a53ee5SAl Viro 		struct fd f = fdget_raw(nd->dfd);
226931e6b01fSNick Piggin 		struct dentry *dentry;
227031e6b01fSNick Piggin 
22712903ff01SAl Viro 		if (!f.file)
2272368ee9baSAl Viro 			return ERR_PTR(-EBADF);
227331e6b01fSNick Piggin 
22742903ff01SAl Viro 		dentry = f.file->f_path.dentry;
227531e6b01fSNick Piggin 
2276edc2b1daSAl Viro 		if (*s && unlikely(!d_can_lookup(dentry))) {
22772903ff01SAl Viro 			fdput(f);
2278368ee9baSAl Viro 			return ERR_PTR(-ENOTDIR);
2279f52e0c11SAl Viro 		}
22802903ff01SAl Viro 
22812903ff01SAl Viro 		nd->path = f.file->f_path;
2282e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
228334a26b99SAl Viro 			nd->inode = nd->path.dentry->d_inode;
228434a26b99SAl Viro 			nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
22855590ff0dSUlrich Drepper 		} else {
22862903ff01SAl Viro 			path_get(&nd->path);
228734a26b99SAl Viro 			nd->inode = nd->path.dentry->d_inode;
22881da177e4SLinus Torvalds 		}
228934a26b99SAl Viro 		fdput(f);
2290e41f7d4eSAl Viro 	}
22918db52c7eSAleksa Sarai 
2292adb21d2bSAleksa Sarai 	/* For scoped-lookups we need to set the root to the dirfd as well. */
2293adb21d2bSAleksa Sarai 	if (flags & LOOKUP_IS_SCOPED) {
2294adb21d2bSAleksa Sarai 		nd->root = nd->path;
2295adb21d2bSAleksa Sarai 		if (flags & LOOKUP_RCU) {
2296adb21d2bSAleksa Sarai 			nd->root_seq = nd->seq;
2297adb21d2bSAleksa Sarai 		} else {
2298adb21d2bSAleksa Sarai 			path_get(&nd->root);
2299adb21d2bSAleksa Sarai 			nd->flags |= LOOKUP_ROOT_GRABBED;
2300adb21d2bSAleksa Sarai 		}
2301adb21d2bSAleksa Sarai 	}
2302adb21d2bSAleksa Sarai 	return s;
23039b4a9b14SAl Viro }
23049b4a9b14SAl Viro 
23051ccac622SAl Viro static inline const char *lookup_last(struct nameidata *nd)
230695fa25d9SAl Viro {
2307bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2308bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2309bd92d7feSAl Viro 
2310c108837eSAl Viro 	return walk_component(nd, WALK_TRAILING);
2311bd92d7feSAl Viro }
2312bd92d7feSAl Viro 
23134f757f3cSAl Viro static int handle_lookup_down(struct nameidata *nd)
23144f757f3cSAl Viro {
2315c153007bSAl Viro 	if (!(nd->flags & LOOKUP_RCU))
2316db3c9adeSAl Viro 		dget(nd->path.dentry);
2317b0417d2cSAl Viro 	return PTR_ERR(step_into(nd, WALK_NOFOLLOW,
2318b0417d2cSAl Viro 			nd->path.dentry, nd->inode, nd->seq));
23194f757f3cSAl Viro }
23204f757f3cSAl Viro 
23219b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2322c8a53ee5SAl Viro static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
23239b4a9b14SAl Viro {
2324c8a53ee5SAl Viro 	const char *s = path_init(nd, flags);
2325bd92d7feSAl Viro 	int err;
232631e6b01fSNick Piggin 
23279b5858e9SAl Viro 	if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) {
23284f757f3cSAl Viro 		err = handle_lookup_down(nd);
23295f336e72SAl Viro 		if (unlikely(err < 0))
23305f336e72SAl Viro 			s = ERR_PTR(err);
23314f757f3cSAl Viro 	}
23324f757f3cSAl Viro 
23331ccac622SAl Viro 	while (!(err = link_path_walk(s, nd)) &&
23341ccac622SAl Viro 	       (s = lookup_last(nd)) != NULL)
23351ccac622SAl Viro 		;
23369f1fafeeSAl Viro 	if (!err)
23379f1fafeeSAl Viro 		err = complete_walk(nd);
2338bd92d7feSAl Viro 
2339deb106c6SAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY)
2340deb106c6SAl Viro 		if (!d_can_lookup(nd->path.dentry))
2341bd23a539SAl Viro 			err = -ENOTDIR;
2342161aff1dSAl Viro 	if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) {
2343161aff1dSAl Viro 		err = handle_lookup_down(nd);
2344161aff1dSAl Viro 		nd->flags &= ~LOOKUP_JUMPED; // no d_weak_revalidate(), please...
2345161aff1dSAl Viro 	}
2346625b6d10SAl Viro 	if (!err) {
2347625b6d10SAl Viro 		*path = nd->path;
2348625b6d10SAl Viro 		nd->path.mnt = NULL;
2349625b6d10SAl Viro 		nd->path.dentry = NULL;
2350625b6d10SAl Viro 	}
2351deb106c6SAl Viro 	terminate_walk(nd);
2352bd92d7feSAl Viro 	return err;
235331e6b01fSNick Piggin }
235431e6b01fSNick Piggin 
235531d921c7SDavid Howells int filename_lookup(int dfd, struct filename *name, unsigned flags,
23569ad1aaa6SAl Viro 		    struct path *path, struct path *root)
2357873f1eedSJeff Layton {
2358894bc8c4SAl Viro 	int retval;
23599883d185SAl Viro 	struct nameidata nd;
2360abc9f5beSAl Viro 	if (IS_ERR(name))
2361abc9f5beSAl Viro 		return PTR_ERR(name);
23629ad1aaa6SAl Viro 	if (unlikely(root)) {
23639ad1aaa6SAl Viro 		nd.root = *root;
23649ad1aaa6SAl Viro 		flags |= LOOKUP_ROOT;
23659ad1aaa6SAl Viro 	}
23669883d185SAl Viro 	set_nameidata(&nd, dfd, name);
2367c8a53ee5SAl Viro 	retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
2368873f1eedSJeff Layton 	if (unlikely(retval == -ECHILD))
2369c8a53ee5SAl Viro 		retval = path_lookupat(&nd, flags, path);
2370873f1eedSJeff Layton 	if (unlikely(retval == -ESTALE))
2371c8a53ee5SAl Viro 		retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
2372873f1eedSJeff Layton 
2373873f1eedSJeff Layton 	if (likely(!retval))
2374161aff1dSAl Viro 		audit_inode(name, path->dentry,
2375161aff1dSAl Viro 			    flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0);
23769883d185SAl Viro 	restore_nameidata();
2377e4bd1c1aSAl Viro 	putname(name);
2378873f1eedSJeff Layton 	return retval;
2379873f1eedSJeff Layton }
2380873f1eedSJeff Layton 
23818bcb77faSAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2382c8a53ee5SAl Viro static int path_parentat(struct nameidata *nd, unsigned flags,
2383391172c4SAl Viro 				struct path *parent)
23848bcb77faSAl Viro {
2385c8a53ee5SAl Viro 	const char *s = path_init(nd, flags);
23869b5858e9SAl Viro 	int err = link_path_walk(s, nd);
23878bcb77faSAl Viro 	if (!err)
23888bcb77faSAl Viro 		err = complete_walk(nd);
2389391172c4SAl Viro 	if (!err) {
2390391172c4SAl Viro 		*parent = nd->path;
2391391172c4SAl Viro 		nd->path.mnt = NULL;
2392391172c4SAl Viro 		nd->path.dentry = NULL;
2393391172c4SAl Viro 	}
2394deb106c6SAl Viro 	terminate_walk(nd);
23958bcb77faSAl Viro 	return err;
23968bcb77faSAl Viro }
23978bcb77faSAl Viro 
23985c31b6ceSAl Viro static struct filename *filename_parentat(int dfd, struct filename *name,
2399391172c4SAl Viro 				unsigned int flags, struct path *parent,
2400391172c4SAl Viro 				struct qstr *last, int *type)
24018bcb77faSAl Viro {
24028bcb77faSAl Viro 	int retval;
24039883d185SAl Viro 	struct nameidata nd;
24048bcb77faSAl Viro 
24055c31b6ceSAl Viro 	if (IS_ERR(name))
24065c31b6ceSAl Viro 		return name;
24079883d185SAl Viro 	set_nameidata(&nd, dfd, name);
2408c8a53ee5SAl Viro 	retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
24098bcb77faSAl Viro 	if (unlikely(retval == -ECHILD))
2410c8a53ee5SAl Viro 		retval = path_parentat(&nd, flags, parent);
24118bcb77faSAl Viro 	if (unlikely(retval == -ESTALE))
2412c8a53ee5SAl Viro 		retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
2413391172c4SAl Viro 	if (likely(!retval)) {
2414391172c4SAl Viro 		*last = nd.last;
2415391172c4SAl Viro 		*type = nd.last_type;
2416c9b07eabSAl Viro 		audit_inode(name, parent->dentry, AUDIT_INODE_PARENT);
24175c31b6ceSAl Viro 	} else {
24185c31b6ceSAl Viro 		putname(name);
24195c31b6ceSAl Viro 		name = ERR_PTR(retval);
2420391172c4SAl Viro 	}
24219883d185SAl Viro 	restore_nameidata();
24225c31b6ceSAl Viro 	return name;
24238bcb77faSAl Viro }
24248bcb77faSAl Viro 
242579714f72SAl Viro /* does lookup, returns the object with parent locked */
242679714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path)
24275590ff0dSUlrich Drepper {
24285c31b6ceSAl Viro 	struct filename *filename;
24295c31b6ceSAl Viro 	struct dentry *d;
2430391172c4SAl Viro 	struct qstr last;
2431391172c4SAl Viro 	int type;
243251689104SPaul Moore 
24335c31b6ceSAl Viro 	filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
24345c31b6ceSAl Viro 				    &last, &type);
243551689104SPaul Moore 	if (IS_ERR(filename))
243651689104SPaul Moore 		return ERR_CAST(filename);
24375c31b6ceSAl Viro 	if (unlikely(type != LAST_NORM)) {
2438391172c4SAl Viro 		path_put(path);
24395c31b6ceSAl Viro 		putname(filename);
24405c31b6ceSAl Viro 		return ERR_PTR(-EINVAL);
244179714f72SAl Viro 	}
24425955102cSAl Viro 	inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
2443391172c4SAl Viro 	d = __lookup_hash(&last, path->dentry, 0);
244479714f72SAl Viro 	if (IS_ERR(d)) {
24455955102cSAl Viro 		inode_unlock(path->dentry->d_inode);
2446391172c4SAl Viro 		path_put(path);
244779714f72SAl Viro 	}
244851689104SPaul Moore 	putname(filename);
244979714f72SAl Viro 	return d;
24505590ff0dSUlrich Drepper }
24515590ff0dSUlrich Drepper 
2452d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
2453d1811465SAl Viro {
2454abc9f5beSAl Viro 	return filename_lookup(AT_FDCWD, getname_kernel(name),
2455abc9f5beSAl Viro 			       flags, path, NULL);
2456d1811465SAl Viro }
24574d359507SAl Viro EXPORT_SYMBOL(kern_path);
2458d1811465SAl Viro 
245916f18200SJosef 'Jeff' Sipek /**
246016f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
246116f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
246216f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
246316f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
246416f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
2465e0a01249SAl Viro  * @path: pointer to struct path to fill
246616f18200SJosef 'Jeff' Sipek  */
246716f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
246816f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
2469e0a01249SAl Viro 		    struct path *path)
247016f18200SJosef 'Jeff' Sipek {
24719ad1aaa6SAl Viro 	struct path root = {.mnt = mnt, .dentry = dentry};
24729ad1aaa6SAl Viro 	/* the first argument of filename_lookup() is ignored with root */
2473abc9f5beSAl Viro 	return filename_lookup(AT_FDCWD, getname_kernel(name),
2474abc9f5beSAl Viro 			       flags , path, &root);
247516f18200SJosef 'Jeff' Sipek }
24764d359507SAl Viro EXPORT_SYMBOL(vfs_path_lookup);
247716f18200SJosef 'Jeff' Sipek 
24783c95f0dcSAl Viro static int lookup_one_len_common(const char *name, struct dentry *base,
24793c95f0dcSAl Viro 				 int len, struct qstr *this)
24803c95f0dcSAl Viro {
24813c95f0dcSAl Viro 	this->name = name;
24823c95f0dcSAl Viro 	this->len = len;
24833c95f0dcSAl Viro 	this->hash = full_name_hash(base, name, len);
24843c95f0dcSAl Viro 	if (!len)
24853c95f0dcSAl Viro 		return -EACCES;
24863c95f0dcSAl Viro 
24873c95f0dcSAl Viro 	if (unlikely(name[0] == '.')) {
24883c95f0dcSAl Viro 		if (len < 2 || (len == 2 && name[1] == '.'))
24893c95f0dcSAl Viro 			return -EACCES;
24903c95f0dcSAl Viro 	}
24913c95f0dcSAl Viro 
24923c95f0dcSAl Viro 	while (len--) {
24933c95f0dcSAl Viro 		unsigned int c = *(const unsigned char *)name++;
24943c95f0dcSAl Viro 		if (c == '/' || c == '\0')
24953c95f0dcSAl Viro 			return -EACCES;
24963c95f0dcSAl Viro 	}
24973c95f0dcSAl Viro 	/*
24983c95f0dcSAl Viro 	 * See if the low-level filesystem might want
24993c95f0dcSAl Viro 	 * to use its own hash..
25003c95f0dcSAl Viro 	 */
25013c95f0dcSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
25023c95f0dcSAl Viro 		int err = base->d_op->d_hash(base, this);
25033c95f0dcSAl Viro 		if (err < 0)
25043c95f0dcSAl Viro 			return err;
25053c95f0dcSAl Viro 	}
25063c95f0dcSAl Viro 
25073c95f0dcSAl Viro 	return inode_permission(base->d_inode, MAY_EXEC);
25083c95f0dcSAl Viro }
25093c95f0dcSAl Viro 
2510eead1911SChristoph Hellwig /**
25110da0b7fdSDavid Howells  * try_lookup_one_len - filesystem helper to lookup single pathname component
25120da0b7fdSDavid Howells  * @name:	pathname component to lookup
25130da0b7fdSDavid Howells  * @base:	base directory to lookup from
25140da0b7fdSDavid Howells  * @len:	maximum length @len should be interpreted to
25150da0b7fdSDavid Howells  *
25160da0b7fdSDavid Howells  * Look up a dentry by name in the dcache, returning NULL if it does not
25170da0b7fdSDavid Howells  * currently exist.  The function does not try to create a dentry.
25180da0b7fdSDavid Howells  *
25190da0b7fdSDavid Howells  * Note that this routine is purely a helper for filesystem usage and should
25200da0b7fdSDavid Howells  * not be called by generic code.
25210da0b7fdSDavid Howells  *
25220da0b7fdSDavid Howells  * The caller must hold base->i_mutex.
25230da0b7fdSDavid Howells  */
25240da0b7fdSDavid Howells struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len)
25250da0b7fdSDavid Howells {
25260da0b7fdSDavid Howells 	struct qstr this;
25270da0b7fdSDavid Howells 	int err;
25280da0b7fdSDavid Howells 
25290da0b7fdSDavid Howells 	WARN_ON_ONCE(!inode_is_locked(base->d_inode));
25300da0b7fdSDavid Howells 
25310da0b7fdSDavid Howells 	err = lookup_one_len_common(name, base, len, &this);
25320da0b7fdSDavid Howells 	if (err)
25330da0b7fdSDavid Howells 		return ERR_PTR(err);
25340da0b7fdSDavid Howells 
25350da0b7fdSDavid Howells 	return lookup_dcache(&this, base, 0);
25360da0b7fdSDavid Howells }
25370da0b7fdSDavid Howells EXPORT_SYMBOL(try_lookup_one_len);
25380da0b7fdSDavid Howells 
25390da0b7fdSDavid Howells /**
2540a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
2541eead1911SChristoph Hellwig  * @name:	pathname component to lookup
2542eead1911SChristoph Hellwig  * @base:	base directory to lookup from
2543eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
2544eead1911SChristoph Hellwig  *
2545a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
25469e7543e9SAl Viro  * not be called by generic code.
2547bbddca8eSNeilBrown  *
2548bbddca8eSNeilBrown  * The caller must hold base->i_mutex.
2549eead1911SChristoph Hellwig  */
2550057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2551057f6c01SJames Morris {
25528613a209SAl Viro 	struct dentry *dentry;
2553057f6c01SJames Morris 	struct qstr this;
2554cda309deSMiklos Szeredi 	int err;
2555057f6c01SJames Morris 
25565955102cSAl Viro 	WARN_ON_ONCE(!inode_is_locked(base->d_inode));
25572f9092e1SDavid Woodhouse 
25583c95f0dcSAl Viro 	err = lookup_one_len_common(name, base, len, &this);
2559cda309deSMiklos Szeredi 	if (err)
2560cda309deSMiklos Szeredi 		return ERR_PTR(err);
2561cda309deSMiklos Szeredi 
25628613a209SAl Viro 	dentry = lookup_dcache(&this, base, 0);
25638613a209SAl Viro 	return dentry ? dentry : __lookup_slow(&this, base, 0);
2564057f6c01SJames Morris }
25654d359507SAl Viro EXPORT_SYMBOL(lookup_one_len);
2566057f6c01SJames Morris 
2567bbddca8eSNeilBrown /**
2568bbddca8eSNeilBrown  * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2569bbddca8eSNeilBrown  * @name:	pathname component to lookup
2570bbddca8eSNeilBrown  * @base:	base directory to lookup from
2571bbddca8eSNeilBrown  * @len:	maximum length @len should be interpreted to
2572bbddca8eSNeilBrown  *
2573bbddca8eSNeilBrown  * Note that this routine is purely a helper for filesystem usage and should
2574bbddca8eSNeilBrown  * not be called by generic code.
2575bbddca8eSNeilBrown  *
2576bbddca8eSNeilBrown  * Unlike lookup_one_len, it should be called without the parent
2577bbddca8eSNeilBrown  * i_mutex held, and will take the i_mutex itself if necessary.
2578bbddca8eSNeilBrown  */
2579bbddca8eSNeilBrown struct dentry *lookup_one_len_unlocked(const char *name,
2580bbddca8eSNeilBrown 				       struct dentry *base, int len)
2581bbddca8eSNeilBrown {
2582bbddca8eSNeilBrown 	struct qstr this;
2583bbddca8eSNeilBrown 	int err;
258420d00ee8SLinus Torvalds 	struct dentry *ret;
2585bbddca8eSNeilBrown 
25863c95f0dcSAl Viro 	err = lookup_one_len_common(name, base, len, &this);
2587bbddca8eSNeilBrown 	if (err)
2588bbddca8eSNeilBrown 		return ERR_PTR(err);
2589bbddca8eSNeilBrown 
259020d00ee8SLinus Torvalds 	ret = lookup_dcache(&this, base, 0);
259120d00ee8SLinus Torvalds 	if (!ret)
259220d00ee8SLinus Torvalds 		ret = lookup_slow(&this, base, 0);
259320d00ee8SLinus Torvalds 	return ret;
2594bbddca8eSNeilBrown }
2595bbddca8eSNeilBrown EXPORT_SYMBOL(lookup_one_len_unlocked);
2596bbddca8eSNeilBrown 
25976c2d4798SAl Viro /*
25986c2d4798SAl Viro  * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT)
25996c2d4798SAl Viro  * on negatives.  Returns known positive or ERR_PTR(); that's what
26006c2d4798SAl Viro  * most of the users want.  Note that pinned negative with unlocked parent
26016c2d4798SAl Viro  * _can_ become positive at any time, so callers of lookup_one_len_unlocked()
26026c2d4798SAl Viro  * need to be very careful; pinned positives have ->d_inode stable, so
26036c2d4798SAl Viro  * this one avoids such problems.
26046c2d4798SAl Viro  */
26056c2d4798SAl Viro struct dentry *lookup_positive_unlocked(const char *name,
26066c2d4798SAl Viro 				       struct dentry *base, int len)
26076c2d4798SAl Viro {
26086c2d4798SAl Viro 	struct dentry *ret = lookup_one_len_unlocked(name, base, len);
26092fa6b1e0SAl Viro 	if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
26106c2d4798SAl Viro 		dput(ret);
26116c2d4798SAl Viro 		ret = ERR_PTR(-ENOENT);
26126c2d4798SAl Viro 	}
26136c2d4798SAl Viro 	return ret;
26146c2d4798SAl Viro }
26156c2d4798SAl Viro EXPORT_SYMBOL(lookup_positive_unlocked);
26166c2d4798SAl Viro 
2617eedf265aSEric W. Biederman #ifdef CONFIG_UNIX98_PTYS
2618eedf265aSEric W. Biederman int path_pts(struct path *path)
2619eedf265aSEric W. Biederman {
2620eedf265aSEric W. Biederman 	/* Find something mounted on "pts" in the same directory as
2621eedf265aSEric W. Biederman 	 * the input path.
2622eedf265aSEric W. Biederman 	 */
2623a6a7eb76SAl Viro 	struct dentry *parent = dget_parent(path->dentry);
2624a6a7eb76SAl Viro 	struct dentry *child;
262519f6028aSAl Viro 	struct qstr this = QSTR_INIT("pts", 3);
2626eedf265aSEric W. Biederman 
2627a6a7eb76SAl Viro 	if (unlikely(!path_connected(path->mnt, parent))) {
2628a6a7eb76SAl Viro 		dput(parent);
262963b27720SAl Viro 		return -ENOENT;
2630a6a7eb76SAl Viro 	}
263163b27720SAl Viro 	dput(path->dentry);
263263b27720SAl Viro 	path->dentry = parent;
2633eedf265aSEric W. Biederman 	child = d_hash_and_lookup(parent, &this);
2634eedf265aSEric W. Biederman 	if (!child)
2635eedf265aSEric W. Biederman 		return -ENOENT;
2636eedf265aSEric W. Biederman 
2637eedf265aSEric W. Biederman 	path->dentry = child;
2638eedf265aSEric W. Biederman 	dput(parent);
263919f6028aSAl Viro 	follow_down(path);
2640eedf265aSEric W. Biederman 	return 0;
2641eedf265aSEric W. Biederman }
2642eedf265aSEric W. Biederman #endif
2643eedf265aSEric W. Biederman 
26441fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
26451fa1e7f6SAndy Whitcroft 		 struct path *path, int *empty)
26461da177e4SLinus Torvalds {
2647abc9f5beSAl Viro 	return filename_lookup(dfd, getname_flags(name, flags, empty),
2648abc9f5beSAl Viro 			       flags, path, NULL);
26491da177e4SLinus Torvalds }
2650b853a161SAl Viro EXPORT_SYMBOL(user_path_at_empty);
26511fa1e7f6SAndy Whitcroft 
2652cbdf35bcSMiklos Szeredi int __check_sticky(struct inode *dir, struct inode *inode)
26531da177e4SLinus Torvalds {
26548e96e3b7SEric W. Biederman 	kuid_t fsuid = current_fsuid();
2655da9592edSDavid Howells 
26568e96e3b7SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
26571da177e4SLinus Torvalds 		return 0;
26588e96e3b7SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
26591da177e4SLinus Torvalds 		return 0;
266023adbe12SAndy Lutomirski 	return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
26611da177e4SLinus Torvalds }
2662cbdf35bcSMiklos Szeredi EXPORT_SYMBOL(__check_sticky);
26631da177e4SLinus Torvalds 
26641da177e4SLinus Torvalds /*
26651da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
26661da177e4SLinus Torvalds  *  whether the type of victim is right.
26671da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
26681da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
26691da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
26701da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
26711da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
26721da177e4SLinus Torvalds  *	a. be owner of dir, or
26731da177e4SLinus Torvalds  *	b. be owner of victim, or
26741da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
26751da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
26761da177e4SLinus Torvalds  *     links pointing to it.
26770bd23d09SEric W. Biederman  *  7. If the victim has an unknown uid or gid we can't change the inode.
26780bd23d09SEric W. Biederman  *  8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
26790bd23d09SEric W. Biederman  *  9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
26800bd23d09SEric W. Biederman  * 10. We can't remove a root or mountpoint.
26810bd23d09SEric W. Biederman  * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
26821da177e4SLinus Torvalds  *     nfs_async_unlink().
26831da177e4SLinus Torvalds  */
2684b18825a7SDavid Howells static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
26851da177e4SLinus Torvalds {
268663afdfc7SDavid Howells 	struct inode *inode = d_backing_inode(victim);
26871da177e4SLinus Torvalds 	int error;
26881da177e4SLinus Torvalds 
2689b18825a7SDavid Howells 	if (d_is_negative(victim))
26901da177e4SLinus Torvalds 		return -ENOENT;
2691b18825a7SDavid Howells 	BUG_ON(!inode);
26921da177e4SLinus Torvalds 
26931da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
2694593d1ce8SEric W. Biederman 
2695593d1ce8SEric W. Biederman 	/* Inode writeback is not safe when the uid or gid are invalid. */
2696593d1ce8SEric W. Biederman 	if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
2697593d1ce8SEric W. Biederman 		return -EOVERFLOW;
2698593d1ce8SEric W. Biederman 
26994fa6b5ecSJeff Layton 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
27001da177e4SLinus Torvalds 
2701f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
27021da177e4SLinus Torvalds 	if (error)
27031da177e4SLinus Torvalds 		return error;
27041da177e4SLinus Torvalds 	if (IS_APPEND(dir))
27051da177e4SLinus Torvalds 		return -EPERM;
2706b18825a7SDavid Howells 
2707b18825a7SDavid Howells 	if (check_sticky(dir, inode) || IS_APPEND(inode) ||
27080bd23d09SEric W. Biederman 	    IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode))
27091da177e4SLinus Torvalds 		return -EPERM;
27101da177e4SLinus Torvalds 	if (isdir) {
271144b1d530SMiklos Szeredi 		if (!d_is_dir(victim))
27121da177e4SLinus Torvalds 			return -ENOTDIR;
27131da177e4SLinus Torvalds 		if (IS_ROOT(victim))
27141da177e4SLinus Torvalds 			return -EBUSY;
271544b1d530SMiklos Szeredi 	} else if (d_is_dir(victim))
27161da177e4SLinus Torvalds 		return -EISDIR;
27171da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
27181da177e4SLinus Torvalds 		return -ENOENT;
27191da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
27201da177e4SLinus Torvalds 		return -EBUSY;
27211da177e4SLinus Torvalds 	return 0;
27221da177e4SLinus Torvalds }
27231da177e4SLinus Torvalds 
27241da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
27251da177e4SLinus Torvalds  *  dir.
27261da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
27271da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
27281da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
2729036d5236SEric W. Biederman  *  3. We can't do it if the fs can't represent the fsuid or fsgid.
2730036d5236SEric W. Biederman  *  4. We should have write and exec permissions on dir
2731036d5236SEric W. Biederman  *  5. We can't do it if dir is immutable (done in permission())
27321da177e4SLinus Torvalds  */
2733a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
27341da177e4SLinus Torvalds {
2735036d5236SEric W. Biederman 	struct user_namespace *s_user_ns;
273614e972b4SJeff Layton 	audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
27371da177e4SLinus Torvalds 	if (child->d_inode)
27381da177e4SLinus Torvalds 		return -EEXIST;
27391da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
27401da177e4SLinus Torvalds 		return -ENOENT;
2741036d5236SEric W. Biederman 	s_user_ns = dir->i_sb->s_user_ns;
2742036d5236SEric W. Biederman 	if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2743036d5236SEric W. Biederman 	    !kgid_has_mapping(s_user_ns, current_fsgid()))
2744036d5236SEric W. Biederman 		return -EOVERFLOW;
2745f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
27461da177e4SLinus Torvalds }
27471da177e4SLinus Torvalds 
27481da177e4SLinus Torvalds /*
27491da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
27501da177e4SLinus Torvalds  */
27511da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
27521da177e4SLinus Torvalds {
27531da177e4SLinus Torvalds 	struct dentry *p;
27541da177e4SLinus Torvalds 
27551da177e4SLinus Torvalds 	if (p1 == p2) {
27565955102cSAl Viro 		inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
27571da177e4SLinus Torvalds 		return NULL;
27581da177e4SLinus Torvalds 	}
27591da177e4SLinus Torvalds 
2760fc64005cSAl Viro 	mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
27611da177e4SLinus Torvalds 
2762e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
2763e2761a11SOGAWA Hirofumi 	if (p) {
27645955102cSAl Viro 		inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
27655955102cSAl Viro 		inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
27661da177e4SLinus Torvalds 		return p;
27671da177e4SLinus Torvalds 	}
27681da177e4SLinus Torvalds 
2769e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
2770e2761a11SOGAWA Hirofumi 	if (p) {
27715955102cSAl Viro 		inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
27725955102cSAl Viro 		inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
27731da177e4SLinus Torvalds 		return p;
27741da177e4SLinus Torvalds 	}
27751da177e4SLinus Torvalds 
27765955102cSAl Viro 	inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
27775955102cSAl Viro 	inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
27781da177e4SLinus Torvalds 	return NULL;
27791da177e4SLinus Torvalds }
27804d359507SAl Viro EXPORT_SYMBOL(lock_rename);
27811da177e4SLinus Torvalds 
27821da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
27831da177e4SLinus Torvalds {
27845955102cSAl Viro 	inode_unlock(p1->d_inode);
27851da177e4SLinus Torvalds 	if (p1 != p2) {
27865955102cSAl Viro 		inode_unlock(p2->d_inode);
2787fc64005cSAl Viro 		mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
27881da177e4SLinus Torvalds 	}
27891da177e4SLinus Torvalds }
27904d359507SAl Viro EXPORT_SYMBOL(unlock_rename);
27911da177e4SLinus Torvalds 
27924acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2793312b63fbSAl Viro 		bool want_excl)
27941da177e4SLinus Torvalds {
2795a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
27961da177e4SLinus Torvalds 	if (error)
27971da177e4SLinus Torvalds 		return error;
27981da177e4SLinus Torvalds 
2799acfa4380SAl Viro 	if (!dir->i_op->create)
28001da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
28011da177e4SLinus Torvalds 	mode &= S_IALLUGO;
28021da177e4SLinus Torvalds 	mode |= S_IFREG;
28031da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
28041da177e4SLinus Torvalds 	if (error)
28051da177e4SLinus Torvalds 		return error;
2806312b63fbSAl Viro 	error = dir->i_op->create(dir, dentry, mode, want_excl);
2807a74574aaSStephen Smalley 	if (!error)
2808f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
28091da177e4SLinus Torvalds 	return error;
28101da177e4SLinus Torvalds }
28114d359507SAl Viro EXPORT_SYMBOL(vfs_create);
28121da177e4SLinus Torvalds 
28138e6c848eSAl Viro int vfs_mkobj(struct dentry *dentry, umode_t mode,
28148e6c848eSAl Viro 		int (*f)(struct dentry *, umode_t, void *),
28158e6c848eSAl Viro 		void *arg)
28168e6c848eSAl Viro {
28178e6c848eSAl Viro 	struct inode *dir = dentry->d_parent->d_inode;
28188e6c848eSAl Viro 	int error = may_create(dir, dentry);
28198e6c848eSAl Viro 	if (error)
28208e6c848eSAl Viro 		return error;
28218e6c848eSAl Viro 
28228e6c848eSAl Viro 	mode &= S_IALLUGO;
28238e6c848eSAl Viro 	mode |= S_IFREG;
28248e6c848eSAl Viro 	error = security_inode_create(dir, dentry, mode);
28258e6c848eSAl Viro 	if (error)
28268e6c848eSAl Viro 		return error;
28278e6c848eSAl Viro 	error = f(dentry, mode, arg);
28288e6c848eSAl Viro 	if (!error)
28298e6c848eSAl Viro 		fsnotify_create(dir, dentry);
28308e6c848eSAl Viro 	return error;
28318e6c848eSAl Viro }
28328e6c848eSAl Viro EXPORT_SYMBOL(vfs_mkobj);
28338e6c848eSAl Viro 
2834a2982cc9SEric W. Biederman bool may_open_dev(const struct path *path)
2835a2982cc9SEric W. Biederman {
2836a2982cc9SEric W. Biederman 	return !(path->mnt->mnt_flags & MNT_NODEV) &&
2837a2982cc9SEric W. Biederman 		!(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
2838a2982cc9SEric W. Biederman }
2839a2982cc9SEric W. Biederman 
2840f0bb5aafSAl Viro static int may_open(const struct path *path, int acc_mode, int flag)
28411da177e4SLinus Torvalds {
28423fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
28431da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
28441da177e4SLinus Torvalds 	int error;
28451da177e4SLinus Torvalds 
28461da177e4SLinus Torvalds 	if (!inode)
28471da177e4SLinus Torvalds 		return -ENOENT;
28481da177e4SLinus Torvalds 
2849c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2850c8fe8f30SChristoph Hellwig 	case S_IFLNK:
28511da177e4SLinus Torvalds 		return -ELOOP;
2852c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2853fc4177beSKees Cook 		if (acc_mode & MAY_WRITE)
28541da177e4SLinus Torvalds 			return -EISDIR;
2855fc4177beSKees Cook 		if (acc_mode & MAY_EXEC)
2856fc4177beSKees Cook 			return -EACCES;
2857c8fe8f30SChristoph Hellwig 		break;
2858c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2859c8fe8f30SChristoph Hellwig 	case S_IFCHR:
2860a2982cc9SEric W. Biederman 		if (!may_open_dev(path))
28611da177e4SLinus Torvalds 			return -EACCES;
2862633fb6acSKees Cook 		fallthrough;
2863c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2864c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
2865633fb6acSKees Cook 		if (acc_mode & MAY_EXEC)
2866633fb6acSKees Cook 			return -EACCES;
28671da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2868c8fe8f30SChristoph Hellwig 		break;
28690fd338b2SKees Cook 	case S_IFREG:
28700fd338b2SKees Cook 		if ((acc_mode & MAY_EXEC) && path_noexec(path))
28710fd338b2SKees Cook 			return -EACCES;
28720fd338b2SKees Cook 		break;
28734a3fd211SDave Hansen 	}
2874b41572e9SDave Hansen 
287562fb4a15SAl Viro 	error = inode_permission(inode, MAY_OPEN | acc_mode);
2876b41572e9SDave Hansen 	if (error)
2877b41572e9SDave Hansen 		return error;
28786146f0d5SMimi Zohar 
28791da177e4SLinus Torvalds 	/*
28801da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
28811da177e4SLinus Torvalds 	 */
28821da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
28838737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
28847715b521SAl Viro 			return -EPERM;
28851da177e4SLinus Torvalds 		if (flag & O_TRUNC)
28867715b521SAl Viro 			return -EPERM;
28871da177e4SLinus Torvalds 	}
28881da177e4SLinus Torvalds 
28891da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
28902e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
28917715b521SAl Viro 		return -EPERM;
28921da177e4SLinus Torvalds 
2893f3c7691eSJ. Bruce Fields 	return 0;
28947715b521SAl Viro }
28957715b521SAl Viro 
2896e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
28977715b521SAl Viro {
2898f0bb5aafSAl Viro 	const struct path *path = &filp->f_path;
28997715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
29007715b521SAl Viro 	int error = get_write_access(inode);
29011da177e4SLinus Torvalds 	if (error)
29027715b521SAl Viro 		return error;
29031da177e4SLinus Torvalds 	/*
29041da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
29051da177e4SLinus Torvalds 	 */
2906d7a06983SJeff Layton 	error = locks_verify_locked(filp);
2907be6d3e56SKentaro Takeda 	if (!error)
2908ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
29091da177e4SLinus Torvalds 	if (!error) {
29107715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2911d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2912e1181ee6SJeff Layton 				    filp);
29131da177e4SLinus Torvalds 	}
29141da177e4SLinus Torvalds 	put_write_access(inode);
2915acd0c935SMimi Zohar 	return error;
29161da177e4SLinus Torvalds }
29171da177e4SLinus Torvalds 
2918d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2919d57999e1SDave Hansen {
29208a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
29218a5e929dSAl Viro 		flag--;
2922d57999e1SDave Hansen 	return flag;
2923d57999e1SDave Hansen }
2924d57999e1SDave Hansen 
2925d3607752SAl Viro static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode)
2926d18e9008SMiklos Szeredi {
29271328c727SSeth Forshee 	struct user_namespace *s_user_ns;
2928d18e9008SMiklos Szeredi 	int error = security_path_mknod(dir, dentry, mode, 0);
2929d18e9008SMiklos Szeredi 	if (error)
2930d18e9008SMiklos Szeredi 		return error;
2931d18e9008SMiklos Szeredi 
29321328c727SSeth Forshee 	s_user_ns = dir->dentry->d_sb->s_user_ns;
29331328c727SSeth Forshee 	if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
29341328c727SSeth Forshee 	    !kgid_has_mapping(s_user_ns, current_fsgid()))
29351328c727SSeth Forshee 		return -EOVERFLOW;
29361328c727SSeth Forshee 
2937d18e9008SMiklos Szeredi 	error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2938d18e9008SMiklos Szeredi 	if (error)
2939d18e9008SMiklos Szeredi 		return error;
2940d18e9008SMiklos Szeredi 
2941d18e9008SMiklos Szeredi 	return security_inode_create(dir->dentry->d_inode, dentry, mode);
2942d18e9008SMiklos Szeredi }
2943d18e9008SMiklos Szeredi 
29441acf0af9SDavid Howells /*
29451acf0af9SDavid Howells  * Attempt to atomically look up, create and open a file from a negative
29461acf0af9SDavid Howells  * dentry.
29471acf0af9SDavid Howells  *
29481acf0af9SDavid Howells  * Returns 0 if successful.  The file will have been created and attached to
29491acf0af9SDavid Howells  * @file by the filesystem calling finish_open().
29501acf0af9SDavid Howells  *
295100a07c15SAl Viro  * If the file was looked up only or didn't need creating, FMODE_OPENED won't
295200a07c15SAl Viro  * be set.  The caller will need to perform the open themselves.  @path will
295300a07c15SAl Viro  * have been updated to point to the new dentry.  This may be negative.
29541acf0af9SDavid Howells  *
29551acf0af9SDavid Howells  * Returns an error code otherwise.
29561acf0af9SDavid Howells  */
2957239eb983SAl Viro static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry,
2958239eb983SAl Viro 				  struct file *file,
29593ec2eef1SAl Viro 				  int open_flag, umode_t mode)
2960d18e9008SMiklos Szeredi {
2961d18e9008SMiklos Szeredi 	struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2962d18e9008SMiklos Szeredi 	struct inode *dir =  nd->path.dentry->d_inode;
2963d18e9008SMiklos Szeredi 	int error;
2964d18e9008SMiklos Szeredi 
2965d18e9008SMiklos Szeredi 	if (nd->flags & LOOKUP_DIRECTORY)
2966d18e9008SMiklos Szeredi 		open_flag |= O_DIRECTORY;
2967d18e9008SMiklos Szeredi 
296830d90494SAl Viro 	file->f_path.dentry = DENTRY_NOT_SET;
296930d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
29700fb1ea09SAl Viro 	error = dir->i_op->atomic_open(dir, dentry, file,
297144907d79SAl Viro 				       open_to_namei_flags(open_flag), mode);
29726fbd0714SAl Viro 	d_lookup_done(dentry);
2973384f26e2SAl Viro 	if (!error) {
297464e1ac4dSAl Viro 		if (file->f_mode & FMODE_OPENED) {
29756fb968cdSAl Viro 			if (unlikely(dentry != file->f_path.dentry)) {
29766fb968cdSAl Viro 				dput(dentry);
29776fb968cdSAl Viro 				dentry = dget(file->f_path.dentry);
29786fb968cdSAl Viro 			}
297964e1ac4dSAl Viro 		} else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
29802675a4ebSAl Viro 			error = -EIO;
2981384f26e2SAl Viro 		} else {
298230d90494SAl Viro 			if (file->f_path.dentry) {
2983d18e9008SMiklos Szeredi 				dput(dentry);
2984d18e9008SMiklos Szeredi 				dentry = file->f_path.dentry;
298510c64ceaSAl Viro 			}
2986239eb983SAl Viro 			if (unlikely(d_is_negative(dentry)))
2987a01e718fSAl Viro 				error = -ENOENT;
2988d18e9008SMiklos Szeredi 		}
2989d18e9008SMiklos Szeredi 	}
2990239eb983SAl Viro 	if (error) {
2991d18e9008SMiklos Szeredi 		dput(dentry);
2992239eb983SAl Viro 		dentry = ERR_PTR(error);
2993239eb983SAl Viro 	}
2994239eb983SAl Viro 	return dentry;
2995d18e9008SMiklos Szeredi }
2996d18e9008SMiklos Szeredi 
299731e6b01fSNick Piggin /*
29981acf0af9SDavid Howells  * Look up and maybe create and open the last component.
2999d58ffd35SMiklos Szeredi  *
300000a07c15SAl Viro  * Must be called with parent locked (exclusive in O_CREAT case).
3001d58ffd35SMiklos Szeredi  *
300200a07c15SAl Viro  * Returns 0 on success, that is, if
300300a07c15SAl Viro  *  the file was successfully atomically created (if necessary) and opened, or
300400a07c15SAl Viro  *  the file was not completely opened at this time, though lookups and
300500a07c15SAl Viro  *  creations were performed.
300600a07c15SAl Viro  * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
300700a07c15SAl Viro  * In the latter case dentry returned in @path might be negative if O_CREAT
300800a07c15SAl Viro  * hadn't been specified.
30091acf0af9SDavid Howells  *
301000a07c15SAl Viro  * An error code is returned on failure.
3011d58ffd35SMiklos Szeredi  */
3012da5ebf5aSAl Viro static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
3013d58ffd35SMiklos Szeredi 				  const struct open_flags *op,
30143ec2eef1SAl Viro 				  bool got_write)
3015d58ffd35SMiklos Szeredi {
3016d58ffd35SMiklos Szeredi 	struct dentry *dir = nd->path.dentry;
301754ef4872SMiklos Szeredi 	struct inode *dir_inode = dir->d_inode;
30181643b43fSAl Viro 	int open_flag = op->open_flag;
3019d58ffd35SMiklos Szeredi 	struct dentry *dentry;
30201643b43fSAl Viro 	int error, create_error = 0;
30211643b43fSAl Viro 	umode_t mode = op->mode;
30226fbd0714SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
3023d58ffd35SMiklos Szeredi 
3024ce8644fcSAl Viro 	if (unlikely(IS_DEADDIR(dir_inode)))
3025da5ebf5aSAl Viro 		return ERR_PTR(-ENOENT);
3026d58ffd35SMiklos Szeredi 
302773a09dd9SAl Viro 	file->f_mode &= ~FMODE_CREATED;
30286fbd0714SAl Viro 	dentry = d_lookup(dir, &nd->last);
30296fbd0714SAl Viro 	for (;;) {
30306fbd0714SAl Viro 		if (!dentry) {
30316fbd0714SAl Viro 			dentry = d_alloc_parallel(dir, &nd->last, &wq);
3032d58ffd35SMiklos Szeredi 			if (IS_ERR(dentry))
3033da5ebf5aSAl Viro 				return dentry;
30346fbd0714SAl Viro 		}
30356fbd0714SAl Viro 		if (d_in_lookup(dentry))
30366fbd0714SAl Viro 			break;
3037d58ffd35SMiklos Szeredi 
30386fbd0714SAl Viro 		error = d_revalidate(dentry, nd->flags);
30396fbd0714SAl Viro 		if (likely(error > 0))
30406fbd0714SAl Viro 			break;
30416fbd0714SAl Viro 		if (error)
30426fbd0714SAl Viro 			goto out_dput;
30436fbd0714SAl Viro 		d_invalidate(dentry);
30446fbd0714SAl Viro 		dput(dentry);
30456fbd0714SAl Viro 		dentry = NULL;
30466fbd0714SAl Viro 	}
30476fbd0714SAl Viro 	if (dentry->d_inode) {
3048d18e9008SMiklos Szeredi 		/* Cached positive dentry: will open in f_op->open */
3049da5ebf5aSAl Viro 		return dentry;
30506c51e513SAl Viro 	}
3051d18e9008SMiklos Szeredi 
30521643b43fSAl Viro 	/*
30531643b43fSAl Viro 	 * Checking write permission is tricky, bacuse we don't know if we are
30541643b43fSAl Viro 	 * going to actually need it: O_CREAT opens should work as long as the
30551643b43fSAl Viro 	 * file exists.  But checking existence breaks atomicity.  The trick is
30561643b43fSAl Viro 	 * to check access and if not granted clear O_CREAT from the flags.
30571643b43fSAl Viro 	 *
30581643b43fSAl Viro 	 * Another problem is returing the "right" error value (e.g. for an
30591643b43fSAl Viro 	 * O_EXCL open we want to return EEXIST not EROFS).
30601643b43fSAl Viro 	 */
306199a4a90cSAl Viro 	if (unlikely(!got_write))
306299a4a90cSAl Viro 		open_flag &= ~O_TRUNC;
30631643b43fSAl Viro 	if (open_flag & O_CREAT) {
306499a4a90cSAl Viro 		if (open_flag & O_EXCL)
306599a4a90cSAl Viro 			open_flag &= ~O_TRUNC;
30661643b43fSAl Viro 		if (!IS_POSIXACL(dir->d_inode))
30671643b43fSAl Viro 			mode &= ~current_umask();
306899a4a90cSAl Viro 		if (likely(got_write))
30691643b43fSAl Viro 			create_error = may_o_create(&nd->path, dentry, mode);
307099a4a90cSAl Viro 		else
307199a4a90cSAl Viro 			create_error = -EROFS;
307299a4a90cSAl Viro 	}
307399a4a90cSAl Viro 	if (create_error)
30741643b43fSAl Viro 		open_flag &= ~O_CREAT;
30751643b43fSAl Viro 	if (dir_inode->i_op->atomic_open) {
3076d489cf9aSAl Viro 		dentry = atomic_open(nd, dentry, file, open_flag, mode);
3077da5ebf5aSAl Viro 		if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT))
3078da5ebf5aSAl Viro 			dentry = ERR_PTR(create_error);
3079da5ebf5aSAl Viro 		return dentry;
3080239eb983SAl Viro 	}
308154ef4872SMiklos Szeredi 
30826fbd0714SAl Viro 	if (d_in_lookup(dentry)) {
308312fa5e24SAl Viro 		struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
308412fa5e24SAl Viro 							     nd->flags);
30856fbd0714SAl Viro 		d_lookup_done(dentry);
308612fa5e24SAl Viro 		if (unlikely(res)) {
308712fa5e24SAl Viro 			if (IS_ERR(res)) {
308812fa5e24SAl Viro 				error = PTR_ERR(res);
308912fa5e24SAl Viro 				goto out_dput;
309012fa5e24SAl Viro 			}
309112fa5e24SAl Viro 			dput(dentry);
309212fa5e24SAl Viro 			dentry = res;
309312fa5e24SAl Viro 		}
309454ef4872SMiklos Szeredi 	}
309554ef4872SMiklos Szeredi 
3096d58ffd35SMiklos Szeredi 	/* Negative dentry, just create the file */
30971643b43fSAl Viro 	if (!dentry->d_inode && (open_flag & O_CREAT)) {
309873a09dd9SAl Viro 		file->f_mode |= FMODE_CREATED;
3099ce8644fcSAl Viro 		audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
3100ce8644fcSAl Viro 		if (!dir_inode->i_op->create) {
3101ce8644fcSAl Viro 			error = -EACCES;
3102d58ffd35SMiklos Szeredi 			goto out_dput;
310364894cf8SAl Viro 		}
3104ce8644fcSAl Viro 		error = dir_inode->i_op->create(dir_inode, dentry, mode,
31051643b43fSAl Viro 						open_flag & O_EXCL);
3106d58ffd35SMiklos Szeredi 		if (error)
3107d58ffd35SMiklos Szeredi 			goto out_dput;
3108d58ffd35SMiklos Szeredi 	}
31091643b43fSAl Viro 	if (unlikely(create_error) && !dentry->d_inode) {
31101643b43fSAl Viro 		error = create_error;
3111d58ffd35SMiklos Szeredi 		goto out_dput;
3112d58ffd35SMiklos Szeredi 	}
3113da5ebf5aSAl Viro 	return dentry;
3114d58ffd35SMiklos Szeredi 
3115d58ffd35SMiklos Szeredi out_dput:
3116d58ffd35SMiklos Szeredi 	dput(dentry);
3117da5ebf5aSAl Viro 	return ERR_PTR(error);
3118d58ffd35SMiklos Szeredi }
3119d58ffd35SMiklos Szeredi 
3120c981a482SAl Viro static const char *open_last_lookups(struct nameidata *nd,
31213ec2eef1SAl Viro 		   struct file *file, const struct open_flags *op)
3122fb1cc555SAl Viro {
3123a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
3124ca344a89SAl Viro 	int open_flag = op->open_flag;
312564894cf8SAl Viro 	bool got_write = false;
3126254cf582SAl Viro 	unsigned seq;
3127a1eb3315SMiklos Szeredi 	struct inode *inode;
3128da5ebf5aSAl Viro 	struct dentry *dentry;
3129b0417d2cSAl Viro 	const char *res;
313016c2cd71SAl Viro 	int error;
3131fb1cc555SAl Viro 
3132c3e380b0SAl Viro 	nd->flags |= op->intent;
3133c3e380b0SAl Viro 
3134bc77daa7SAl Viro 	if (nd->last_type != LAST_NORM) {
313556676ec3SAl Viro 		if (nd->depth)
313656676ec3SAl Viro 			put_link(nd);
3137ff326a32SAl Viro 		return handle_dots(nd, nd->last_type);
31381f36f774SAl Viro 	}
3139a2c36b45SAl Viro 
3140ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
3141fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
3142fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3143fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
314420e34357SAl Viro 		dentry = lookup_fast(nd, &inode, &seq);
314520e34357SAl Viro 		if (IS_ERR(dentry))
31461ccac622SAl Viro 			return ERR_CAST(dentry);
314720e34357SAl Viro 		if (likely(dentry))
314871574865SMiklos Szeredi 			goto finish_lookup;
314971574865SMiklos Szeredi 
31506583fe22SAl Viro 		BUG_ON(nd->flags & LOOKUP_RCU);
3151b6183df7SMiklos Szeredi 	} else {
3152fe2d35ffSAl Viro 		/* create side of things */
315372287417SAl Viro 		if (nd->flags & LOOKUP_RCU) {
315472287417SAl Viro 			error = unlazy_walk(nd);
3155c981a482SAl Viro 			if (unlikely(error))
31561ccac622SAl Viro 				return ERR_PTR(error);
315772287417SAl Viro 		}
3158c9b07eabSAl Viro 		audit_inode(nd->name, dir, AUDIT_INODE_PARENT);
31591f36f774SAl Viro 		/* trailing slashes? */
3160deb106c6SAl Viro 		if (unlikely(nd->last.name[nd->last.len]))
31611ccac622SAl Viro 			return ERR_PTR(-EISDIR);
3162b6183df7SMiklos Szeredi 	}
31631f36f774SAl Viro 
31649cf843e3SAl Viro 	if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
316564894cf8SAl Viro 		error = mnt_want_write(nd->path.mnt);
316664894cf8SAl Viro 		if (!error)
316764894cf8SAl Viro 			got_write = true;
316864894cf8SAl Viro 		/*
316964894cf8SAl Viro 		 * do _not_ fail yet - we might not need that or fail with
317064894cf8SAl Viro 		 * a different error; let lookup_open() decide; we'll be
317164894cf8SAl Viro 		 * dropping this one anyway.
317264894cf8SAl Viro 		 */
317364894cf8SAl Viro 	}
31749cf843e3SAl Viro 	if (open_flag & O_CREAT)
31755955102cSAl Viro 		inode_lock(dir->d_inode);
31769cf843e3SAl Viro 	else
31779cf843e3SAl Viro 		inode_lock_shared(dir->d_inode);
3178da5ebf5aSAl Viro 	dentry = lookup_open(nd, file, op, got_write);
3179f7bb959dSAl Viro 	if (!IS_ERR(dentry) && (file->f_mode & FMODE_CREATED))
3180f7bb959dSAl Viro 		fsnotify_create(dir->d_inode, dentry);
31819cf843e3SAl Viro 	if (open_flag & O_CREAT)
31825955102cSAl Viro 		inode_unlock(dir->d_inode);
31839cf843e3SAl Viro 	else
31849cf843e3SAl Viro 		inode_unlock_shared(dir->d_inode);
3185fb1cc555SAl Viro 
3186c981a482SAl Viro 	if (got_write)
318759e96e65SAl Viro 		mnt_drop_write(nd->path.mnt);
31886c0d46c4SAl Viro 
318959e96e65SAl Viro 	if (IS_ERR(dentry))
319059e96e65SAl Viro 		return ERR_CAST(dentry);
319159e96e65SAl Viro 
3192973d4b73SAl Viro 	if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) {
3193e73cabffSAl Viro 		dput(nd->path.dentry);
3194e73cabffSAl Viro 		nd->path.dentry = dentry;
3195c981a482SAl Viro 		return NULL;
3196fb1cc555SAl Viro 	}
3197fb1cc555SAl Viro 
319820e34357SAl Viro finish_lookup:
319956676ec3SAl Viro 	if (nd->depth)
320056676ec3SAl Viro 		put_link(nd);
32018c4efe22SAl Viro 	res = step_into(nd, WALK_TRAILING, dentry, inode, seq);
3202ff326a32SAl Viro 	if (unlikely(res))
32031ccac622SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
3204b0417d2cSAl Viro 	return res;
32051ccac622SAl Viro }
320631d1726dSAl Viro 
3207c981a482SAl Viro /*
3208c981a482SAl Viro  * Handle the last step of open()
3209c981a482SAl Viro  */
3210c5971b8cSAl Viro static int do_open(struct nameidata *nd,
3211c981a482SAl Viro 		   struct file *file, const struct open_flags *op)
3212c981a482SAl Viro {
3213c981a482SAl Viro 	int open_flag = op->open_flag;
3214c981a482SAl Viro 	bool do_truncate;
3215c981a482SAl Viro 	int acc_mode;
3216c981a482SAl Viro 	int error;
3217c981a482SAl Viro 
3218ff326a32SAl Viro 	if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) {
3219ff326a32SAl Viro 		error = complete_walk(nd);
3220ff326a32SAl Viro 		if (error)
3221ff326a32SAl Viro 			return error;
3222ff326a32SAl Viro 	}
3223973d4b73SAl Viro 	if (!(file->f_mode & FMODE_CREATED))
322476ae2a5aSAl Viro 		audit_inode(nd->name, nd->path.dentry, 0);
322530aba665SSalvatore Mesoraca 	if (open_flag & O_CREAT) {
3226b94e0b32SAl Viro 		if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED))
3227b94e0b32SAl Viro 			return -EEXIST;
322830aba665SSalvatore Mesoraca 		if (d_is_dir(nd->path.dentry))
3229c5971b8cSAl Viro 			return -EISDIR;
32300f705953SAl Viro 		error = may_create_in_sticky(nd->dir_mode, nd->dir_uid,
323130aba665SSalvatore Mesoraca 					     d_backing_inode(nd->path.dentry));
323230aba665SSalvatore Mesoraca 		if (unlikely(error))
3233c5971b8cSAl Viro 			return error;
323430aba665SSalvatore Mesoraca 	}
323544b1d530SMiklos Szeredi 	if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
3236c5971b8cSAl Viro 		return -ENOTDIR;
32376c0d46c4SAl Viro 
32388795e7d4SAl Viro 	do_truncate = false;
32398795e7d4SAl Viro 	acc_mode = op->acc_mode;
32405a2d3eddSAl Viro 	if (file->f_mode & FMODE_CREATED) {
32415a2d3eddSAl Viro 		/* Don't check for write permission, don't truncate */
32425a2d3eddSAl Viro 		open_flag &= ~O_TRUNC;
32435a2d3eddSAl Viro 		acc_mode = 0;
32448795e7d4SAl Viro 	} else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) {
32450f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
32460f9d1a10SAl Viro 		if (error)
3247c5971b8cSAl Viro 			return error;
32488795e7d4SAl Viro 		do_truncate = true;
32490f9d1a10SAl Viro 	}
3250bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
32518795e7d4SAl Viro 	if (!error && !(file->f_mode & FMODE_OPENED))
3252ae2bb293SAl Viro 		error = vfs_open(&nd->path, file);
32538795e7d4SAl Viro 	if (!error)
32546035a27bSAl Viro 		error = ima_file_check(file, op->acc_mode);
32558795e7d4SAl Viro 	if (!error && do_truncate)
32562675a4ebSAl Viro 		error = handle_truncate(file);
3257c80567c8SAl Viro 	if (unlikely(error > 0)) {
3258c80567c8SAl Viro 		WARN_ON(1);
3259c80567c8SAl Viro 		error = -EINVAL;
3260c80567c8SAl Viro 	}
32618795e7d4SAl Viro 	if (do_truncate)
32620f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
3263c5971b8cSAl Viro 	return error;
3264fb1cc555SAl Viro }
3265fb1cc555SAl Viro 
3266af7bd4dcSAmir Goldstein struct dentry *vfs_tmpfile(struct dentry *dentry, umode_t mode, int open_flag)
3267af7bd4dcSAmir Goldstein {
3268af7bd4dcSAmir Goldstein 	struct dentry *child = NULL;
3269af7bd4dcSAmir Goldstein 	struct inode *dir = dentry->d_inode;
3270af7bd4dcSAmir Goldstein 	struct inode *inode;
3271af7bd4dcSAmir Goldstein 	int error;
3272af7bd4dcSAmir Goldstein 
3273af7bd4dcSAmir Goldstein 	/* we want directory to be writable */
3274af7bd4dcSAmir Goldstein 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
3275af7bd4dcSAmir Goldstein 	if (error)
3276af7bd4dcSAmir Goldstein 		goto out_err;
3277af7bd4dcSAmir Goldstein 	error = -EOPNOTSUPP;
3278af7bd4dcSAmir Goldstein 	if (!dir->i_op->tmpfile)
3279af7bd4dcSAmir Goldstein 		goto out_err;
3280af7bd4dcSAmir Goldstein 	error = -ENOMEM;
3281cdf01226SDavid Howells 	child = d_alloc(dentry, &slash_name);
3282af7bd4dcSAmir Goldstein 	if (unlikely(!child))
3283af7bd4dcSAmir Goldstein 		goto out_err;
3284af7bd4dcSAmir Goldstein 	error = dir->i_op->tmpfile(dir, child, mode);
3285af7bd4dcSAmir Goldstein 	if (error)
3286af7bd4dcSAmir Goldstein 		goto out_err;
3287af7bd4dcSAmir Goldstein 	error = -ENOENT;
3288af7bd4dcSAmir Goldstein 	inode = child->d_inode;
3289af7bd4dcSAmir Goldstein 	if (unlikely(!inode))
3290af7bd4dcSAmir Goldstein 		goto out_err;
3291af7bd4dcSAmir Goldstein 	if (!(open_flag & O_EXCL)) {
3292af7bd4dcSAmir Goldstein 		spin_lock(&inode->i_lock);
3293af7bd4dcSAmir Goldstein 		inode->i_state |= I_LINKABLE;
3294af7bd4dcSAmir Goldstein 		spin_unlock(&inode->i_lock);
3295af7bd4dcSAmir Goldstein 	}
3296fdb2410fSMimi Zohar 	ima_post_create_tmpfile(inode);
3297af7bd4dcSAmir Goldstein 	return child;
3298af7bd4dcSAmir Goldstein 
3299af7bd4dcSAmir Goldstein out_err:
3300af7bd4dcSAmir Goldstein 	dput(child);
3301af7bd4dcSAmir Goldstein 	return ERR_PTR(error);
3302af7bd4dcSAmir Goldstein }
3303af7bd4dcSAmir Goldstein EXPORT_SYMBOL(vfs_tmpfile);
3304af7bd4dcSAmir Goldstein 
3305c8a53ee5SAl Viro static int do_tmpfile(struct nameidata *nd, unsigned flags,
330660545d0dSAl Viro 		const struct open_flags *op,
33073ec2eef1SAl Viro 		struct file *file)
330860545d0dSAl Viro {
3309625b6d10SAl Viro 	struct dentry *child;
3310625b6d10SAl Viro 	struct path path;
3311c8a53ee5SAl Viro 	int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
331260545d0dSAl Viro 	if (unlikely(error))
331360545d0dSAl Viro 		return error;
3314625b6d10SAl Viro 	error = mnt_want_write(path.mnt);
331560545d0dSAl Viro 	if (unlikely(error))
331660545d0dSAl Viro 		goto out;
3317af7bd4dcSAmir Goldstein 	child = vfs_tmpfile(path.dentry, op->mode, op->open_flag);
3318af7bd4dcSAmir Goldstein 	error = PTR_ERR(child);
3319684e73beSHirofumi Nakagawa 	if (IS_ERR(child))
332060545d0dSAl Viro 		goto out2;
3321625b6d10SAl Viro 	dput(path.dentry);
3322625b6d10SAl Viro 	path.dentry = child;
3323c8a53ee5SAl Viro 	audit_inode(nd->name, child, 0);
332469a91c23SEric Rannaud 	/* Don't check for other permissions, the inode was just created */
332562fb4a15SAl Viro 	error = may_open(&path, 0, op->open_flag);
332660545d0dSAl Viro 	if (error)
332760545d0dSAl Viro 		goto out2;
3328625b6d10SAl Viro 	file->f_path.mnt = path.mnt;
3329be12af3eSAl Viro 	error = finish_open(file, child, NULL);
333060545d0dSAl Viro out2:
3331625b6d10SAl Viro 	mnt_drop_write(path.mnt);
333260545d0dSAl Viro out:
3333625b6d10SAl Viro 	path_put(&path);
333460545d0dSAl Viro 	return error;
333560545d0dSAl Viro }
333660545d0dSAl Viro 
33376ac08709SAl Viro static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
33386ac08709SAl Viro {
33396ac08709SAl Viro 	struct path path;
33406ac08709SAl Viro 	int error = path_lookupat(nd, flags, &path);
33416ac08709SAl Viro 	if (!error) {
33426ac08709SAl Viro 		audit_inode(nd->name, path.dentry, 0);
3343ae2bb293SAl Viro 		error = vfs_open(&path, file);
33446ac08709SAl Viro 		path_put(&path);
33456ac08709SAl Viro 	}
33466ac08709SAl Viro 	return error;
33476ac08709SAl Viro }
33486ac08709SAl Viro 
3349c8a53ee5SAl Viro static struct file *path_openat(struct nameidata *nd,
3350c8a53ee5SAl Viro 			const struct open_flags *op, unsigned flags)
33511da177e4SLinus Torvalds {
335230d90494SAl Viro 	struct file *file;
335313aab428SAl Viro 	int error;
335431e6b01fSNick Piggin 
3355ea73ea72SAl Viro 	file = alloc_empty_file(op->open_flag, current_cred());
33561afc99beSAl Viro 	if (IS_ERR(file))
33571afc99beSAl Viro 		return file;
335831e6b01fSNick Piggin 
3359bb458c64SAl Viro 	if (unlikely(file->f_flags & __O_TMPFILE)) {
33603ec2eef1SAl Viro 		error = do_tmpfile(nd, flags, op, file);
33615f336e72SAl Viro 	} else if (unlikely(file->f_flags & O_PATH)) {
33626ac08709SAl Viro 		error = do_o_path(nd, flags, file);
33635f336e72SAl Viro 	} else {
33645f336e72SAl Viro 		const char *s = path_init(nd, flags);
33653bdba28bSAl Viro 		while (!(error = link_path_walk(s, nd)) &&
3366c5971b8cSAl Viro 		       (s = open_last_lookups(nd, file, op)) != NULL)
33671ccac622SAl Viro 			;
3368c5971b8cSAl Viro 		if (!error)
3369c5971b8cSAl Viro 			error = do_open(nd, file, op);
3370deb106c6SAl Viro 		terminate_walk(nd);
33715f336e72SAl Viro 	}
33727c1c01ecSAl Viro 	if (likely(!error)) {
3373aad888f8SAl Viro 		if (likely(file->f_mode & FMODE_OPENED))
33747c1c01ecSAl Viro 			return file;
33757c1c01ecSAl Viro 		WARN_ON(1);
33767c1c01ecSAl Viro 		error = -EINVAL;
3377015c3bbcSMiklos Szeredi 	}
33787c1c01ecSAl Viro 	fput(file);
33792675a4ebSAl Viro 	if (error == -EOPENSTALE) {
33802675a4ebSAl Viro 		if (flags & LOOKUP_RCU)
33812675a4ebSAl Viro 			error = -ECHILD;
33822675a4ebSAl Viro 		else
33832675a4ebSAl Viro 			error = -ESTALE;
33842675a4ebSAl Viro 	}
33857c1c01ecSAl Viro 	return ERR_PTR(error);
3386de459215SKirill Korotaev }
33871da177e4SLinus Torvalds 
3388669abf4eSJeff Layton struct file *do_filp_open(int dfd, struct filename *pathname,
3389f9652e10SAl Viro 		const struct open_flags *op)
339013aab428SAl Viro {
33919883d185SAl Viro 	struct nameidata nd;
3392f9652e10SAl Viro 	int flags = op->lookup_flags;
339313aab428SAl Viro 	struct file *filp;
339413aab428SAl Viro 
33959883d185SAl Viro 	set_nameidata(&nd, dfd, pathname);
3396c8a53ee5SAl Viro 	filp = path_openat(&nd, op, flags | LOOKUP_RCU);
339713aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
3398c8a53ee5SAl Viro 		filp = path_openat(&nd, op, flags);
339913aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
3400c8a53ee5SAl Viro 		filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
34019883d185SAl Viro 	restore_nameidata();
340213aab428SAl Viro 	return filp;
340313aab428SAl Viro }
340413aab428SAl Viro 
340573d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3406f9652e10SAl Viro 		const char *name, const struct open_flags *op)
340773d049a4SAl Viro {
34089883d185SAl Viro 	struct nameidata nd;
340973d049a4SAl Viro 	struct file *file;
341051689104SPaul Moore 	struct filename *filename;
3411f9652e10SAl Viro 	int flags = op->lookup_flags | LOOKUP_ROOT;
341273d049a4SAl Viro 
341373d049a4SAl Viro 	nd.root.mnt = mnt;
341473d049a4SAl Viro 	nd.root.dentry = dentry;
341573d049a4SAl Viro 
3416b18825a7SDavid Howells 	if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
341773d049a4SAl Viro 		return ERR_PTR(-ELOOP);
341873d049a4SAl Viro 
341951689104SPaul Moore 	filename = getname_kernel(name);
3420a1c83681SViresh Kumar 	if (IS_ERR(filename))
342151689104SPaul Moore 		return ERR_CAST(filename);
342251689104SPaul Moore 
34239883d185SAl Viro 	set_nameidata(&nd, -1, filename);
3424c8a53ee5SAl Viro 	file = path_openat(&nd, op, flags | LOOKUP_RCU);
342573d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
3426c8a53ee5SAl Viro 		file = path_openat(&nd, op, flags);
342773d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
3428c8a53ee5SAl Viro 		file = path_openat(&nd, op, flags | LOOKUP_REVAL);
34299883d185SAl Viro 	restore_nameidata();
343051689104SPaul Moore 	putname(filename);
343173d049a4SAl Viro 	return file;
343273d049a4SAl Viro }
343373d049a4SAl Viro 
3434fa14a0b8SAl Viro static struct dentry *filename_create(int dfd, struct filename *name,
34351ac12b4bSJeff Layton 				struct path *path, unsigned int lookup_flags)
34361da177e4SLinus Torvalds {
3437c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
3438391172c4SAl Viro 	struct qstr last;
3439391172c4SAl Viro 	int type;
3440c30dabfeSJan Kara 	int err2;
34411ac12b4bSJeff Layton 	int error;
34421ac12b4bSJeff Layton 	bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
34431ac12b4bSJeff Layton 
34441ac12b4bSJeff Layton 	/*
34451ac12b4bSJeff Layton 	 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
34461ac12b4bSJeff Layton 	 * other flags passed in are ignored!
34471ac12b4bSJeff Layton 	 */
34481ac12b4bSJeff Layton 	lookup_flags &= LOOKUP_REVAL;
34491ac12b4bSJeff Layton 
34505c31b6ceSAl Viro 	name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
34515c31b6ceSAl Viro 	if (IS_ERR(name))
34525c31b6ceSAl Viro 		return ERR_CAST(name);
34531da177e4SLinus Torvalds 
3454c663e5d8SChristoph Hellwig 	/*
3455c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
3456c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
3457c663e5d8SChristoph Hellwig 	 */
34585c31b6ceSAl Viro 	if (unlikely(type != LAST_NORM))
3459ed75e95dSAl Viro 		goto out;
3460c663e5d8SChristoph Hellwig 
3461c30dabfeSJan Kara 	/* don't fail immediately if it's r/o, at least try to report other errors */
3462391172c4SAl Viro 	err2 = mnt_want_write(path->mnt);
3463c663e5d8SChristoph Hellwig 	/*
3464c663e5d8SChristoph Hellwig 	 * Do the final lookup.
3465c663e5d8SChristoph Hellwig 	 */
3466391172c4SAl Viro 	lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
34675955102cSAl Viro 	inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
3468391172c4SAl Viro 	dentry = __lookup_hash(&last, path->dentry, lookup_flags);
34691da177e4SLinus Torvalds 	if (IS_ERR(dentry))
3470a8104a9fSAl Viro 		goto unlock;
3471c663e5d8SChristoph Hellwig 
3472a8104a9fSAl Viro 	error = -EEXIST;
3473b18825a7SDavid Howells 	if (d_is_positive(dentry))
3474a8104a9fSAl Viro 		goto fail;
3475b18825a7SDavid Howells 
3476c663e5d8SChristoph Hellwig 	/*
3477c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
3478c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
3479c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
3480c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
3481c663e5d8SChristoph Hellwig 	 */
3482391172c4SAl Viro 	if (unlikely(!is_dir && last.name[last.len])) {
3483a8104a9fSAl Viro 		error = -ENOENT;
3484ed75e95dSAl Viro 		goto fail;
3485e9baf6e5SAl Viro 	}
3486c30dabfeSJan Kara 	if (unlikely(err2)) {
3487c30dabfeSJan Kara 		error = err2;
3488a8104a9fSAl Viro 		goto fail;
3489c30dabfeSJan Kara 	}
3490181c37b6SAl Viro 	putname(name);
3491e9baf6e5SAl Viro 	return dentry;
34921da177e4SLinus Torvalds fail:
3493a8104a9fSAl Viro 	dput(dentry);
3494a8104a9fSAl Viro 	dentry = ERR_PTR(error);
3495a8104a9fSAl Viro unlock:
34965955102cSAl Viro 	inode_unlock(path->dentry->d_inode);
3497c30dabfeSJan Kara 	if (!err2)
3498391172c4SAl Viro 		mnt_drop_write(path->mnt);
3499ed75e95dSAl Viro out:
3500391172c4SAl Viro 	path_put(path);
3501181c37b6SAl Viro 	putname(name);
3502ed75e95dSAl Viro 	return dentry;
3503dae6ad8fSAl Viro }
3504fa14a0b8SAl Viro 
3505fa14a0b8SAl Viro struct dentry *kern_path_create(int dfd, const char *pathname,
3506fa14a0b8SAl Viro 				struct path *path, unsigned int lookup_flags)
3507fa14a0b8SAl Viro {
3508181c37b6SAl Viro 	return filename_create(dfd, getname_kernel(pathname),
3509181c37b6SAl Viro 				path, lookup_flags);
3510fa14a0b8SAl Viro }
3511dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
3512dae6ad8fSAl Viro 
3513921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry)
3514921a1650SAl Viro {
3515921a1650SAl Viro 	dput(dentry);
35165955102cSAl Viro 	inode_unlock(path->dentry->d_inode);
3517a8104a9fSAl Viro 	mnt_drop_write(path->mnt);
3518921a1650SAl Viro 	path_put(path);
3519921a1650SAl Viro }
3520921a1650SAl Viro EXPORT_SYMBOL(done_path_create);
3521921a1650SAl Viro 
3522520ae687SAl Viro inline struct dentry *user_path_create(int dfd, const char __user *pathname,
35231ac12b4bSJeff Layton 				struct path *path, unsigned int lookup_flags)
3524dae6ad8fSAl Viro {
3525181c37b6SAl Viro 	return filename_create(dfd, getname(pathname), path, lookup_flags);
3526dae6ad8fSAl Viro }
3527dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
3528dae6ad8fSAl Viro 
35291a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
35301da177e4SLinus Torvalds {
3531a3c751a5SMiklos Szeredi 	bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV;
3532a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
35331da177e4SLinus Torvalds 
35341da177e4SLinus Torvalds 	if (error)
35351da177e4SLinus Torvalds 		return error;
35361da177e4SLinus Torvalds 
3537a3c751a5SMiklos Szeredi 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !is_whiteout &&
3538a3c751a5SMiklos Szeredi 	    !capable(CAP_MKNOD))
35391da177e4SLinus Torvalds 		return -EPERM;
35401da177e4SLinus Torvalds 
3541acfa4380SAl Viro 	if (!dir->i_op->mknod)
35421da177e4SLinus Torvalds 		return -EPERM;
35431da177e4SLinus Torvalds 
354408ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
354508ce5f16SSerge E. Hallyn 	if (error)
354608ce5f16SSerge E. Hallyn 		return error;
354708ce5f16SSerge E. Hallyn 
35481da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
35491da177e4SLinus Torvalds 	if (error)
35501da177e4SLinus Torvalds 		return error;
35511da177e4SLinus Torvalds 
35521da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
3553a74574aaSStephen Smalley 	if (!error)
3554f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
35551da177e4SLinus Torvalds 	return error;
35561da177e4SLinus Torvalds }
35574d359507SAl Viro EXPORT_SYMBOL(vfs_mknod);
35581da177e4SLinus Torvalds 
3559f69aac00SAl Viro static int may_mknod(umode_t mode)
3560463c3197SDave Hansen {
3561463c3197SDave Hansen 	switch (mode & S_IFMT) {
3562463c3197SDave Hansen 	case S_IFREG:
3563463c3197SDave Hansen 	case S_IFCHR:
3564463c3197SDave Hansen 	case S_IFBLK:
3565463c3197SDave Hansen 	case S_IFIFO:
3566463c3197SDave Hansen 	case S_IFSOCK:
3567463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
3568463c3197SDave Hansen 		return 0;
3569463c3197SDave Hansen 	case S_IFDIR:
3570463c3197SDave Hansen 		return -EPERM;
3571463c3197SDave Hansen 	default:
3572463c3197SDave Hansen 		return -EINVAL;
3573463c3197SDave Hansen 	}
3574463c3197SDave Hansen }
3575463c3197SDave Hansen 
35765fee64fcSChristoph Hellwig static long do_mknodat(int dfd, const char __user *filename, umode_t mode,
357787c4e192SDominik Brodowski 		unsigned int dev)
35781da177e4SLinus Torvalds {
35791da177e4SLinus Torvalds 	struct dentry *dentry;
3580dae6ad8fSAl Viro 	struct path path;
3581dae6ad8fSAl Viro 	int error;
3582972567f1SJeff Layton 	unsigned int lookup_flags = 0;
35831da177e4SLinus Torvalds 
35848e4bfca1SAl Viro 	error = may_mknod(mode);
35858e4bfca1SAl Viro 	if (error)
35868e4bfca1SAl Viro 		return error;
3587972567f1SJeff Layton retry:
3588972567f1SJeff Layton 	dentry = user_path_create(dfd, filename, &path, lookup_flags);
3589dae6ad8fSAl Viro 	if (IS_ERR(dentry))
3590dae6ad8fSAl Viro 		return PTR_ERR(dentry);
35912ad94ae6SAl Viro 
3592dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3593ce3b0f8dSAl Viro 		mode &= ~current_umask();
3594dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
3595be6d3e56SKentaro Takeda 	if (error)
3596a8104a9fSAl Viro 		goto out;
35971da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
35981da177e4SLinus Torvalds 		case 0: case S_IFREG:
3599312b63fbSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,true);
360005d1a717SMimi Zohar 			if (!error)
360105d1a717SMimi Zohar 				ima_post_path_mknod(dentry);
36021da177e4SLinus Torvalds 			break;
36031da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
3604dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
36051da177e4SLinus Torvalds 					new_decode_dev(dev));
36061da177e4SLinus Torvalds 			break;
36071da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
3608dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
36091da177e4SLinus Torvalds 			break;
36101da177e4SLinus Torvalds 	}
3611a8104a9fSAl Viro out:
3612921a1650SAl Viro 	done_path_create(&path, dentry);
3613972567f1SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3614972567f1SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3615972567f1SJeff Layton 		goto retry;
3616972567f1SJeff Layton 	}
36171da177e4SLinus Torvalds 	return error;
36181da177e4SLinus Torvalds }
36191da177e4SLinus Torvalds 
362087c4e192SDominik Brodowski SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
362187c4e192SDominik Brodowski 		unsigned int, dev)
362287c4e192SDominik Brodowski {
362387c4e192SDominik Brodowski 	return do_mknodat(dfd, filename, mode, dev);
362487c4e192SDominik Brodowski }
362587c4e192SDominik Brodowski 
36268208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
36275590ff0dSUlrich Drepper {
362887c4e192SDominik Brodowski 	return do_mknodat(AT_FDCWD, filename, mode, dev);
36295590ff0dSUlrich Drepper }
36305590ff0dSUlrich Drepper 
363118bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
36321da177e4SLinus Torvalds {
3633a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
36348de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
36351da177e4SLinus Torvalds 
36361da177e4SLinus Torvalds 	if (error)
36371da177e4SLinus Torvalds 		return error;
36381da177e4SLinus Torvalds 
3639acfa4380SAl Viro 	if (!dir->i_op->mkdir)
36401da177e4SLinus Torvalds 		return -EPERM;
36411da177e4SLinus Torvalds 
36421da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
36431da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
36441da177e4SLinus Torvalds 	if (error)
36451da177e4SLinus Torvalds 		return error;
36461da177e4SLinus Torvalds 
36478de52778SAl Viro 	if (max_links && dir->i_nlink >= max_links)
36488de52778SAl Viro 		return -EMLINK;
36498de52778SAl Viro 
36501da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
3651a74574aaSStephen Smalley 	if (!error)
3652f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
36531da177e4SLinus Torvalds 	return error;
36541da177e4SLinus Torvalds }
36554d359507SAl Viro EXPORT_SYMBOL(vfs_mkdir);
36561da177e4SLinus Torvalds 
365783ff98c3SChristoph Hellwig static long do_mkdirat(int dfd, const char __user *pathname, umode_t mode)
36581da177e4SLinus Torvalds {
36596902d925SDave Hansen 	struct dentry *dentry;
3660dae6ad8fSAl Viro 	struct path path;
3661dae6ad8fSAl Viro 	int error;
3662b76d8b82SJeff Layton 	unsigned int lookup_flags = LOOKUP_DIRECTORY;
36631da177e4SLinus Torvalds 
3664b76d8b82SJeff Layton retry:
3665b76d8b82SJeff Layton 	dentry = user_path_create(dfd, pathname, &path, lookup_flags);
36666902d925SDave Hansen 	if (IS_ERR(dentry))
3667dae6ad8fSAl Viro 		return PTR_ERR(dentry);
36686902d925SDave Hansen 
3669dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3670ce3b0f8dSAl Viro 		mode &= ~current_umask();
3671dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
3672a8104a9fSAl Viro 	if (!error)
3673dae6ad8fSAl Viro 		error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3674921a1650SAl Viro 	done_path_create(&path, dentry);
3675b76d8b82SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3676b76d8b82SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3677b76d8b82SJeff Layton 		goto retry;
3678b76d8b82SJeff Layton 	}
36791da177e4SLinus Torvalds 	return error;
36801da177e4SLinus Torvalds }
36811da177e4SLinus Torvalds 
36820101db7aSDominik Brodowski SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
36830101db7aSDominik Brodowski {
36840101db7aSDominik Brodowski 	return do_mkdirat(dfd, pathname, mode);
36850101db7aSDominik Brodowski }
36860101db7aSDominik Brodowski 
3687a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
36885590ff0dSUlrich Drepper {
36890101db7aSDominik Brodowski 	return do_mkdirat(AT_FDCWD, pathname, mode);
36905590ff0dSUlrich Drepper }
36915590ff0dSUlrich Drepper 
36921da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
36931da177e4SLinus Torvalds {
36941da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
36951da177e4SLinus Torvalds 
36961da177e4SLinus Torvalds 	if (error)
36971da177e4SLinus Torvalds 		return error;
36981da177e4SLinus Torvalds 
3699acfa4380SAl Viro 	if (!dir->i_op->rmdir)
37001da177e4SLinus Torvalds 		return -EPERM;
37011da177e4SLinus Torvalds 
37021d2ef590SAl Viro 	dget(dentry);
37035955102cSAl Viro 	inode_lock(dentry->d_inode);
3704912dbc15SSage Weil 
37051da177e4SLinus Torvalds 	error = -EBUSY;
37067af1364fSEric W. Biederman 	if (is_local_mountpoint(dentry))
3707912dbc15SSage Weil 		goto out;
3708912dbc15SSage Weil 
37091da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
3710912dbc15SSage Weil 	if (error)
3711912dbc15SSage Weil 		goto out;
3712912dbc15SSage Weil 
37131da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
3714912dbc15SSage Weil 	if (error)
3715912dbc15SSage Weil 		goto out;
3716912dbc15SSage Weil 
37178767712fSAl Viro 	shrink_dcache_parent(dentry);
37181da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
3719d83c49f3SAl Viro 	dont_mount(dentry);
37208ed936b5SEric W. Biederman 	detach_mounts(dentry);
3721116b9731SAmir Goldstein 	fsnotify_rmdir(dir, dentry);
37221da177e4SLinus Torvalds 
3723912dbc15SSage Weil out:
37245955102cSAl Viro 	inode_unlock(dentry->d_inode);
37251d2ef590SAl Viro 	dput(dentry);
3726912dbc15SSage Weil 	if (!error)
3727912dbc15SSage Weil 		d_delete(dentry);
37281da177e4SLinus Torvalds 	return error;
37291da177e4SLinus Torvalds }
37304d359507SAl Viro EXPORT_SYMBOL(vfs_rmdir);
37311da177e4SLinus Torvalds 
3732e24ab0efSChristoph Hellwig long do_rmdir(int dfd, struct filename *name)
37331da177e4SLinus Torvalds {
37341da177e4SLinus Torvalds 	int error = 0;
37351da177e4SLinus Torvalds 	struct dentry *dentry;
3736f5beed75SAl Viro 	struct path path;
3737f5beed75SAl Viro 	struct qstr last;
3738f5beed75SAl Viro 	int type;
3739c6ee9206SJeff Layton 	unsigned int lookup_flags = 0;
3740c6ee9206SJeff Layton retry:
3741e24ab0efSChristoph Hellwig 	name = filename_parentat(dfd, name, lookup_flags,
3742c1d4dd27SAl Viro 				&path, &last, &type);
374391a27b2aSJeff Layton 	if (IS_ERR(name))
374491a27b2aSJeff Layton 		return PTR_ERR(name);
37451da177e4SLinus Torvalds 
3746f5beed75SAl Viro 	switch (type) {
37471da177e4SLinus Torvalds 	case LAST_DOTDOT:
37481da177e4SLinus Torvalds 		error = -ENOTEMPTY;
37491da177e4SLinus Torvalds 		goto exit1;
37501da177e4SLinus Torvalds 	case LAST_DOT:
37511da177e4SLinus Torvalds 		error = -EINVAL;
37521da177e4SLinus Torvalds 		goto exit1;
37531da177e4SLinus Torvalds 	case LAST_ROOT:
37541da177e4SLinus Torvalds 		error = -EBUSY;
37551da177e4SLinus Torvalds 		goto exit1;
37561da177e4SLinus Torvalds 	}
37570612d9fbSOGAWA Hirofumi 
3758f5beed75SAl Viro 	error = mnt_want_write(path.mnt);
3759c30dabfeSJan Kara 	if (error)
3760c30dabfeSJan Kara 		goto exit1;
37610612d9fbSOGAWA Hirofumi 
37625955102cSAl Viro 	inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
3763f5beed75SAl Viro 	dentry = __lookup_hash(&last, path.dentry, lookup_flags);
37641da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
37656902d925SDave Hansen 	if (IS_ERR(dentry))
37666902d925SDave Hansen 		goto exit2;
3767e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
3768e6bc45d6STheodore Ts'o 		error = -ENOENT;
3769e6bc45d6STheodore Ts'o 		goto exit3;
3770e6bc45d6STheodore Ts'o 	}
3771f5beed75SAl Viro 	error = security_path_rmdir(&path, dentry);
3772be6d3e56SKentaro Takeda 	if (error)
3773c30dabfeSJan Kara 		goto exit3;
3774f5beed75SAl Viro 	error = vfs_rmdir(path.dentry->d_inode, dentry);
37750622753bSDave Hansen exit3:
37761da177e4SLinus Torvalds 	dput(dentry);
37776902d925SDave Hansen exit2:
37785955102cSAl Viro 	inode_unlock(path.dentry->d_inode);
3779f5beed75SAl Viro 	mnt_drop_write(path.mnt);
37801da177e4SLinus Torvalds exit1:
3781f5beed75SAl Viro 	path_put(&path);
3782c6ee9206SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3783c6ee9206SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3784c6ee9206SJeff Layton 		goto retry;
3785c6ee9206SJeff Layton 	}
378624fb33d4SAl Viro 	putname(name);
37871da177e4SLinus Torvalds 	return error;
37881da177e4SLinus Torvalds }
37891da177e4SLinus Torvalds 
37903cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
37915590ff0dSUlrich Drepper {
3792e24ab0efSChristoph Hellwig 	return do_rmdir(AT_FDCWD, getname(pathname));
37935590ff0dSUlrich Drepper }
37945590ff0dSUlrich Drepper 
3795b21996e3SJ. Bruce Fields /**
3796b21996e3SJ. Bruce Fields  * vfs_unlink - unlink a filesystem object
3797b21996e3SJ. Bruce Fields  * @dir:	parent directory
3798b21996e3SJ. Bruce Fields  * @dentry:	victim
3799b21996e3SJ. Bruce Fields  * @delegated_inode: returns victim inode, if the inode is delegated.
3800b21996e3SJ. Bruce Fields  *
3801b21996e3SJ. Bruce Fields  * The caller must hold dir->i_mutex.
3802b21996e3SJ. Bruce Fields  *
3803b21996e3SJ. Bruce Fields  * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3804b21996e3SJ. Bruce Fields  * return a reference to the inode in delegated_inode.  The caller
3805b21996e3SJ. Bruce Fields  * should then break the delegation on that inode and retry.  Because
3806b21996e3SJ. Bruce Fields  * breaking a delegation may take a long time, the caller should drop
3807b21996e3SJ. Bruce Fields  * dir->i_mutex before doing so.
3808b21996e3SJ. Bruce Fields  *
3809b21996e3SJ. Bruce Fields  * Alternatively, a caller may pass NULL for delegated_inode.  This may
3810b21996e3SJ. Bruce Fields  * be appropriate for callers that expect the underlying filesystem not
3811b21996e3SJ. Bruce Fields  * to be NFS exported.
3812b21996e3SJ. Bruce Fields  */
3813b21996e3SJ. Bruce Fields int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
38141da177e4SLinus Torvalds {
38159accbb97SJ. Bruce Fields 	struct inode *target = dentry->d_inode;
38161da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
38171da177e4SLinus Torvalds 
38181da177e4SLinus Torvalds 	if (error)
38191da177e4SLinus Torvalds 		return error;
38201da177e4SLinus Torvalds 
3821acfa4380SAl Viro 	if (!dir->i_op->unlink)
38221da177e4SLinus Torvalds 		return -EPERM;
38231da177e4SLinus Torvalds 
38245955102cSAl Viro 	inode_lock(target);
38258ed936b5SEric W. Biederman 	if (is_local_mountpoint(dentry))
38261da177e4SLinus Torvalds 		error = -EBUSY;
38271da177e4SLinus Torvalds 	else {
38281da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
3829bec1052eSAl Viro 		if (!error) {
38305a14696cSJ. Bruce Fields 			error = try_break_deleg(target, delegated_inode);
38315a14696cSJ. Bruce Fields 			if (error)
3832b21996e3SJ. Bruce Fields 				goto out;
38331da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
38348ed936b5SEric W. Biederman 			if (!error) {
3835d83c49f3SAl Viro 				dont_mount(dentry);
38368ed936b5SEric W. Biederman 				detach_mounts(dentry);
3837116b9731SAmir Goldstein 				fsnotify_unlink(dir, dentry);
38388ed936b5SEric W. Biederman 			}
3839bec1052eSAl Viro 		}
38401da177e4SLinus Torvalds 	}
3841b21996e3SJ. Bruce Fields out:
38425955102cSAl Viro 	inode_unlock(target);
38431da177e4SLinus Torvalds 
38441da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
38451da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
38469accbb97SJ. Bruce Fields 		fsnotify_link_count(target);
38471da177e4SLinus Torvalds 		d_delete(dentry);
38481da177e4SLinus Torvalds 	}
38490eeca283SRobert Love 
38501da177e4SLinus Torvalds 	return error;
38511da177e4SLinus Torvalds }
38524d359507SAl Viro EXPORT_SYMBOL(vfs_unlink);
38531da177e4SLinus Torvalds 
38541da177e4SLinus Torvalds /*
38551da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
38561b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
38571da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
38581da177e4SLinus Torvalds  * while waiting on the I/O.
38591da177e4SLinus Torvalds  */
3860da2f1362SChristoph Hellwig long do_unlinkat(int dfd, struct filename *name)
38611da177e4SLinus Torvalds {
38622ad94ae6SAl Viro 	int error;
38631da177e4SLinus Torvalds 	struct dentry *dentry;
3864f5beed75SAl Viro 	struct path path;
3865f5beed75SAl Viro 	struct qstr last;
3866f5beed75SAl Viro 	int type;
38671da177e4SLinus Torvalds 	struct inode *inode = NULL;
3868b21996e3SJ. Bruce Fields 	struct inode *delegated_inode = NULL;
38695d18f813SJeff Layton 	unsigned int lookup_flags = 0;
38705d18f813SJeff Layton retry:
3871da2f1362SChristoph Hellwig 	name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
387291a27b2aSJeff Layton 	if (IS_ERR(name))
387391a27b2aSJeff Layton 		return PTR_ERR(name);
38742ad94ae6SAl Viro 
38751da177e4SLinus Torvalds 	error = -EISDIR;
3876f5beed75SAl Viro 	if (type != LAST_NORM)
38771da177e4SLinus Torvalds 		goto exit1;
38780612d9fbSOGAWA Hirofumi 
3879f5beed75SAl Viro 	error = mnt_want_write(path.mnt);
3880c30dabfeSJan Kara 	if (error)
3881c30dabfeSJan Kara 		goto exit1;
3882b21996e3SJ. Bruce Fields retry_deleg:
38835955102cSAl Viro 	inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
3884f5beed75SAl Viro 	dentry = __lookup_hash(&last, path.dentry, lookup_flags);
38851da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
38861da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
38871da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
3888f5beed75SAl Viro 		if (last.name[last.len])
388950338b88STörök Edwin 			goto slashes;
38901da177e4SLinus Torvalds 		inode = dentry->d_inode;
3891b18825a7SDavid Howells 		if (d_is_negative(dentry))
3892e6bc45d6STheodore Ts'o 			goto slashes;
38937de9c6eeSAl Viro 		ihold(inode);
3894f5beed75SAl Viro 		error = security_path_unlink(&path, dentry);
3895be6d3e56SKentaro Takeda 		if (error)
3896c30dabfeSJan Kara 			goto exit2;
3897f5beed75SAl Viro 		error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
38981da177e4SLinus Torvalds exit2:
38991da177e4SLinus Torvalds 		dput(dentry);
39001da177e4SLinus Torvalds 	}
39015955102cSAl Viro 	inode_unlock(path.dentry->d_inode);
39021da177e4SLinus Torvalds 	if (inode)
39031da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
3904b21996e3SJ. Bruce Fields 	inode = NULL;
3905b21996e3SJ. Bruce Fields 	if (delegated_inode) {
39065a14696cSJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
3907b21996e3SJ. Bruce Fields 		if (!error)
3908b21996e3SJ. Bruce Fields 			goto retry_deleg;
3909b21996e3SJ. Bruce Fields 	}
3910f5beed75SAl Viro 	mnt_drop_write(path.mnt);
39111da177e4SLinus Torvalds exit1:
3912f5beed75SAl Viro 	path_put(&path);
39135d18f813SJeff Layton 	if (retry_estale(error, lookup_flags)) {
39145d18f813SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
39155d18f813SJeff Layton 		inode = NULL;
39165d18f813SJeff Layton 		goto retry;
39175d18f813SJeff Layton 	}
3918da2f1362SChristoph Hellwig 	putname(name);
39191da177e4SLinus Torvalds 	return error;
39201da177e4SLinus Torvalds 
39211da177e4SLinus Torvalds slashes:
3922b18825a7SDavid Howells 	if (d_is_negative(dentry))
3923b18825a7SDavid Howells 		error = -ENOENT;
392444b1d530SMiklos Szeredi 	else if (d_is_dir(dentry))
3925b18825a7SDavid Howells 		error = -EISDIR;
3926b18825a7SDavid Howells 	else
3927b18825a7SDavid Howells 		error = -ENOTDIR;
39281da177e4SLinus Torvalds 	goto exit2;
39291da177e4SLinus Torvalds }
39301da177e4SLinus Torvalds 
39312e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
39325590ff0dSUlrich Drepper {
39335590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
39345590ff0dSUlrich Drepper 		return -EINVAL;
39355590ff0dSUlrich Drepper 
39365590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
3937e24ab0efSChristoph Hellwig 		return do_rmdir(dfd, getname(pathname));
3938da2f1362SChristoph Hellwig 	return do_unlinkat(dfd, getname(pathname));
39395590ff0dSUlrich Drepper }
39405590ff0dSUlrich Drepper 
39413480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
39425590ff0dSUlrich Drepper {
3943da2f1362SChristoph Hellwig 	return do_unlinkat(AT_FDCWD, getname(pathname));
39445590ff0dSUlrich Drepper }
39455590ff0dSUlrich Drepper 
3946db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
39471da177e4SLinus Torvalds {
3948a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
39491da177e4SLinus Torvalds 
39501da177e4SLinus Torvalds 	if (error)
39511da177e4SLinus Torvalds 		return error;
39521da177e4SLinus Torvalds 
3953acfa4380SAl Viro 	if (!dir->i_op->symlink)
39541da177e4SLinus Torvalds 		return -EPERM;
39551da177e4SLinus Torvalds 
39561da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
39571da177e4SLinus Torvalds 	if (error)
39581da177e4SLinus Torvalds 		return error;
39591da177e4SLinus Torvalds 
39601da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
3961a74574aaSStephen Smalley 	if (!error)
3962f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
39631da177e4SLinus Torvalds 	return error;
39641da177e4SLinus Torvalds }
39654d359507SAl Viro EXPORT_SYMBOL(vfs_symlink);
39661da177e4SLinus Torvalds 
3967cd3acb6aSChristoph Hellwig static long do_symlinkat(const char __user *oldname, int newdfd,
3968b724e846SDominik Brodowski 		  const char __user *newname)
39691da177e4SLinus Torvalds {
39702ad94ae6SAl Viro 	int error;
397191a27b2aSJeff Layton 	struct filename *from;
39726902d925SDave Hansen 	struct dentry *dentry;
3973dae6ad8fSAl Viro 	struct path path;
3974f46d3567SJeff Layton 	unsigned int lookup_flags = 0;
39751da177e4SLinus Torvalds 
39761da177e4SLinus Torvalds 	from = getname(oldname);
39771da177e4SLinus Torvalds 	if (IS_ERR(from))
39781da177e4SLinus Torvalds 		return PTR_ERR(from);
3979f46d3567SJeff Layton retry:
3980f46d3567SJeff Layton 	dentry = user_path_create(newdfd, newname, &path, lookup_flags);
39811da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
39826902d925SDave Hansen 	if (IS_ERR(dentry))
3983dae6ad8fSAl Viro 		goto out_putname;
39846902d925SDave Hansen 
398591a27b2aSJeff Layton 	error = security_path_symlink(&path, dentry, from->name);
3986a8104a9fSAl Viro 	if (!error)
398791a27b2aSJeff Layton 		error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
3988921a1650SAl Viro 	done_path_create(&path, dentry);
3989f46d3567SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3990f46d3567SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3991f46d3567SJeff Layton 		goto retry;
3992f46d3567SJeff Layton 	}
39936902d925SDave Hansen out_putname:
39941da177e4SLinus Torvalds 	putname(from);
39951da177e4SLinus Torvalds 	return error;
39961da177e4SLinus Torvalds }
39971da177e4SLinus Torvalds 
3998b724e846SDominik Brodowski SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3999b724e846SDominik Brodowski 		int, newdfd, const char __user *, newname)
4000b724e846SDominik Brodowski {
4001b724e846SDominik Brodowski 	return do_symlinkat(oldname, newdfd, newname);
4002b724e846SDominik Brodowski }
4003b724e846SDominik Brodowski 
40043480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
40055590ff0dSUlrich Drepper {
4006b724e846SDominik Brodowski 	return do_symlinkat(oldname, AT_FDCWD, newname);
40075590ff0dSUlrich Drepper }
40085590ff0dSUlrich Drepper 
4009146a8595SJ. Bruce Fields /**
4010146a8595SJ. Bruce Fields  * vfs_link - create a new link
4011146a8595SJ. Bruce Fields  * @old_dentry:	object to be linked
4012146a8595SJ. Bruce Fields  * @dir:	new parent
4013146a8595SJ. Bruce Fields  * @new_dentry:	where to create the new link
4014146a8595SJ. Bruce Fields  * @delegated_inode: returns inode needing a delegation break
4015146a8595SJ. Bruce Fields  *
4016146a8595SJ. Bruce Fields  * The caller must hold dir->i_mutex
4017146a8595SJ. Bruce Fields  *
4018146a8595SJ. Bruce Fields  * If vfs_link discovers a delegation on the to-be-linked file in need
4019146a8595SJ. Bruce Fields  * of breaking, it will return -EWOULDBLOCK and return a reference to the
4020146a8595SJ. Bruce Fields  * inode in delegated_inode.  The caller should then break the delegation
4021146a8595SJ. Bruce Fields  * and retry.  Because breaking a delegation may take a long time, the
4022146a8595SJ. Bruce Fields  * caller should drop the i_mutex before doing so.
4023146a8595SJ. Bruce Fields  *
4024146a8595SJ. Bruce Fields  * Alternatively, a caller may pass NULL for delegated_inode.  This may
4025146a8595SJ. Bruce Fields  * be appropriate for callers that expect the underlying filesystem not
4026146a8595SJ. Bruce Fields  * to be NFS exported.
4027146a8595SJ. Bruce Fields  */
4028146a8595SJ. Bruce Fields int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
40291da177e4SLinus Torvalds {
40301da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
40318de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
40321da177e4SLinus Torvalds 	int error;
40331da177e4SLinus Torvalds 
40341da177e4SLinus Torvalds 	if (!inode)
40351da177e4SLinus Torvalds 		return -ENOENT;
40361da177e4SLinus Torvalds 
4037a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
40381da177e4SLinus Torvalds 	if (error)
40391da177e4SLinus Torvalds 		return error;
40401da177e4SLinus Torvalds 
40411da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
40421da177e4SLinus Torvalds 		return -EXDEV;
40431da177e4SLinus Torvalds 
40441da177e4SLinus Torvalds 	/*
40451da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
40461da177e4SLinus Torvalds 	 */
40471da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
40481da177e4SLinus Torvalds 		return -EPERM;
40490bd23d09SEric W. Biederman 	/*
40500bd23d09SEric W. Biederman 	 * Updating the link count will likely cause i_uid and i_gid to
40510bd23d09SEric W. Biederman 	 * be writen back improperly if their true value is unknown to
40520bd23d09SEric W. Biederman 	 * the vfs.
40530bd23d09SEric W. Biederman 	 */
40540bd23d09SEric W. Biederman 	if (HAS_UNMAPPED_ID(inode))
40550bd23d09SEric W. Biederman 		return -EPERM;
4056acfa4380SAl Viro 	if (!dir->i_op->link)
40571da177e4SLinus Torvalds 		return -EPERM;
40587e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
40591da177e4SLinus Torvalds 		return -EPERM;
40601da177e4SLinus Torvalds 
40611da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
40621da177e4SLinus Torvalds 	if (error)
40631da177e4SLinus Torvalds 		return error;
40641da177e4SLinus Torvalds 
40655955102cSAl Viro 	inode_lock(inode);
4066aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
4067f4e0c30cSAl Viro 	if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
4068aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
40698de52778SAl Viro 	else if (max_links && inode->i_nlink >= max_links)
40708de52778SAl Viro 		error = -EMLINK;
4071146a8595SJ. Bruce Fields 	else {
4072146a8595SJ. Bruce Fields 		error = try_break_deleg(inode, delegated_inode);
4073146a8595SJ. Bruce Fields 		if (!error)
40741da177e4SLinus Torvalds 			error = dir->i_op->link(old_dentry, dir, new_dentry);
4075146a8595SJ. Bruce Fields 	}
4076f4e0c30cSAl Viro 
4077f4e0c30cSAl Viro 	if (!error && (inode->i_state & I_LINKABLE)) {
4078f4e0c30cSAl Viro 		spin_lock(&inode->i_lock);
4079f4e0c30cSAl Viro 		inode->i_state &= ~I_LINKABLE;
4080f4e0c30cSAl Viro 		spin_unlock(&inode->i_lock);
4081f4e0c30cSAl Viro 	}
40825955102cSAl Viro 	inode_unlock(inode);
4083e31e14ecSStephen Smalley 	if (!error)
40847e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
40851da177e4SLinus Torvalds 	return error;
40861da177e4SLinus Torvalds }
40874d359507SAl Viro EXPORT_SYMBOL(vfs_link);
40881da177e4SLinus Torvalds 
40891da177e4SLinus Torvalds /*
40901da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
40911da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
40921da177e4SLinus Torvalds  * newname.  --KAB
40931da177e4SLinus Torvalds  *
40941da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
40951da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
40961da177e4SLinus Torvalds  * and other special files.  --ADM
40971da177e4SLinus Torvalds  */
4098812931d6SChristoph Hellwig static int do_linkat(int olddfd, const char __user *oldname, int newdfd,
409946ea89ebSDominik Brodowski 	      const char __user *newname, int flags)
41001da177e4SLinus Torvalds {
41011da177e4SLinus Torvalds 	struct dentry *new_dentry;
4102dae6ad8fSAl Viro 	struct path old_path, new_path;
4103146a8595SJ. Bruce Fields 	struct inode *delegated_inode = NULL;
410411a7b371SAneesh Kumar K.V 	int how = 0;
41051da177e4SLinus Torvalds 	int error;
41061da177e4SLinus Torvalds 
410711a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
4108c04030e1SUlrich Drepper 		return -EINVAL;
410911a7b371SAneesh Kumar K.V 	/*
4110f0cc6ffbSLinus Torvalds 	 * To use null names we require CAP_DAC_READ_SEARCH
4111f0cc6ffbSLinus Torvalds 	 * This ensures that not everyone will be able to create
4112f0cc6ffbSLinus Torvalds 	 * handlink using the passed filedescriptor.
411311a7b371SAneesh Kumar K.V 	 */
4114f0cc6ffbSLinus Torvalds 	if (flags & AT_EMPTY_PATH) {
4115f0cc6ffbSLinus Torvalds 		if (!capable(CAP_DAC_READ_SEARCH))
4116f0cc6ffbSLinus Torvalds 			return -ENOENT;
411711a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
4118f0cc6ffbSLinus Torvalds 	}
4119c04030e1SUlrich Drepper 
412011a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
412111a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
4122442e31caSJeff Layton retry:
412311a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
41241da177e4SLinus Torvalds 	if (error)
41252ad94ae6SAl Viro 		return error;
41262ad94ae6SAl Viro 
4127442e31caSJeff Layton 	new_dentry = user_path_create(newdfd, newname, &new_path,
4128442e31caSJeff Layton 					(how & LOOKUP_REVAL));
41291da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
41306902d925SDave Hansen 	if (IS_ERR(new_dentry))
4131dae6ad8fSAl Viro 		goto out;
4132dae6ad8fSAl Viro 
4133dae6ad8fSAl Viro 	error = -EXDEV;
4134dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
4135dae6ad8fSAl Viro 		goto out_dput;
4136800179c9SKees Cook 	error = may_linkat(&old_path);
4137800179c9SKees Cook 	if (unlikely(error))
4138800179c9SKees Cook 		goto out_dput;
4139dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
4140be6d3e56SKentaro Takeda 	if (error)
4141a8104a9fSAl Viro 		goto out_dput;
4142146a8595SJ. Bruce Fields 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
414375c3f29dSDave Hansen out_dput:
4144921a1650SAl Viro 	done_path_create(&new_path, new_dentry);
4145146a8595SJ. Bruce Fields 	if (delegated_inode) {
4146146a8595SJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
4147d22e6338SOleg Drokin 		if (!error) {
4148d22e6338SOleg Drokin 			path_put(&old_path);
4149146a8595SJ. Bruce Fields 			goto retry;
4150146a8595SJ. Bruce Fields 		}
4151d22e6338SOleg Drokin 	}
4152442e31caSJeff Layton 	if (retry_estale(error, how)) {
4153d22e6338SOleg Drokin 		path_put(&old_path);
4154442e31caSJeff Layton 		how |= LOOKUP_REVAL;
4155442e31caSJeff Layton 		goto retry;
4156442e31caSJeff Layton 	}
41571da177e4SLinus Torvalds out:
41582d8f3038SAl Viro 	path_put(&old_path);
41591da177e4SLinus Torvalds 
41601da177e4SLinus Torvalds 	return error;
41611da177e4SLinus Torvalds }
41621da177e4SLinus Torvalds 
416346ea89ebSDominik Brodowski SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
416446ea89ebSDominik Brodowski 		int, newdfd, const char __user *, newname, int, flags)
416546ea89ebSDominik Brodowski {
416646ea89ebSDominik Brodowski 	return do_linkat(olddfd, oldname, newdfd, newname, flags);
416746ea89ebSDominik Brodowski }
416846ea89ebSDominik Brodowski 
41693480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
41705590ff0dSUlrich Drepper {
417146ea89ebSDominik Brodowski 	return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
41725590ff0dSUlrich Drepper }
41735590ff0dSUlrich Drepper 
4174bc27027aSMiklos Szeredi /**
4175bc27027aSMiklos Szeredi  * vfs_rename - rename a filesystem object
4176bc27027aSMiklos Szeredi  * @old_dir:	parent of source
4177bc27027aSMiklos Szeredi  * @old_dentry:	source
4178bc27027aSMiklos Szeredi  * @new_dir:	parent of destination
4179bc27027aSMiklos Szeredi  * @new_dentry:	destination
4180bc27027aSMiklos Szeredi  * @delegated_inode: returns an inode needing a delegation break
4181520c8b16SMiklos Szeredi  * @flags:	rename flags
4182bc27027aSMiklos Szeredi  *
4183bc27027aSMiklos Szeredi  * The caller must hold multiple mutexes--see lock_rename()).
4184bc27027aSMiklos Szeredi  *
4185bc27027aSMiklos Szeredi  * If vfs_rename discovers a delegation in need of breaking at either
4186bc27027aSMiklos Szeredi  * the source or destination, it will return -EWOULDBLOCK and return a
4187bc27027aSMiklos Szeredi  * reference to the inode in delegated_inode.  The caller should then
4188bc27027aSMiklos Szeredi  * break the delegation and retry.  Because breaking a delegation may
4189bc27027aSMiklos Szeredi  * take a long time, the caller should drop all locks before doing
4190bc27027aSMiklos Szeredi  * so.
4191bc27027aSMiklos Szeredi  *
4192bc27027aSMiklos Szeredi  * Alternatively, a caller may pass NULL for delegated_inode.  This may
4193bc27027aSMiklos Szeredi  * be appropriate for callers that expect the underlying filesystem not
4194bc27027aSMiklos Szeredi  * to be NFS exported.
4195bc27027aSMiklos Szeredi  *
41961da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
41971da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
41981da177e4SLinus Torvalds  * Problems:
41990117d427SMauro Carvalho Chehab  *
4200d03b29a2SJ. Bruce Fields  *	a) we can get into loop creation.
42011da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
42021da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
4203a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
42041da177e4SLinus Torvalds  *	   story.
42056cedba89SJ. Bruce Fields  *	c) we have to lock _four_ objects - parents and victim (if it exists),
42066cedba89SJ. Bruce Fields  *	   and source (if it is not a directory).
42071b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
42081da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
42091da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
4210a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
42111da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
42121da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
42131da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
4214a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
42151da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
42161da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
42171da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
4218e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
42191b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
42201da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
4221c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
42221da177e4SLinus Torvalds  *	   locking].
42231da177e4SLinus Torvalds  */
42241da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
42258e6d782cSJ. Bruce Fields 	       struct inode *new_dir, struct dentry *new_dentry,
4226520c8b16SMiklos Szeredi 	       struct inode **delegated_inode, unsigned int flags)
42271da177e4SLinus Torvalds {
42281da177e4SLinus Torvalds 	int error;
4229bc27027aSMiklos Szeredi 	bool is_dir = d_is_dir(old_dentry);
4230bc27027aSMiklos Szeredi 	struct inode *source = old_dentry->d_inode;
4231bc27027aSMiklos Szeredi 	struct inode *target = new_dentry->d_inode;
4232da1ce067SMiklos Szeredi 	bool new_is_dir = false;
4233da1ce067SMiklos Szeredi 	unsigned max_links = new_dir->i_sb->s_max_links;
423449d31c2fSAl Viro 	struct name_snapshot old_name;
42351da177e4SLinus Torvalds 
42368d3e2936SMiklos Szeredi 	if (source == target)
42371da177e4SLinus Torvalds 		return 0;
42381da177e4SLinus Torvalds 
42391da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
42401da177e4SLinus Torvalds 	if (error)
42411da177e4SLinus Torvalds 		return error;
42421da177e4SLinus Torvalds 
4243da1ce067SMiklos Szeredi 	if (!target) {
4244a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
4245da1ce067SMiklos Szeredi 	} else {
4246da1ce067SMiklos Szeredi 		new_is_dir = d_is_dir(new_dentry);
4247da1ce067SMiklos Szeredi 
4248da1ce067SMiklos Szeredi 		if (!(flags & RENAME_EXCHANGE))
42491da177e4SLinus Torvalds 			error = may_delete(new_dir, new_dentry, is_dir);
4250da1ce067SMiklos Szeredi 		else
4251da1ce067SMiklos Szeredi 			error = may_delete(new_dir, new_dentry, new_is_dir);
4252da1ce067SMiklos Szeredi 	}
42531da177e4SLinus Torvalds 	if (error)
42541da177e4SLinus Torvalds 		return error;
42551da177e4SLinus Torvalds 
42562773bf00SMiklos Szeredi 	if (!old_dir->i_op->rename)
42571da177e4SLinus Torvalds 		return -EPERM;
42581da177e4SLinus Torvalds 
4259bc27027aSMiklos Szeredi 	/*
4260bc27027aSMiklos Szeredi 	 * If we are going to change the parent - check write permissions,
4261bc27027aSMiklos Szeredi 	 * we'll need to flip '..'.
4262bc27027aSMiklos Szeredi 	 */
4263da1ce067SMiklos Szeredi 	if (new_dir != old_dir) {
4264da1ce067SMiklos Szeredi 		if (is_dir) {
4265bc27027aSMiklos Szeredi 			error = inode_permission(source, MAY_WRITE);
4266bc27027aSMiklos Szeredi 			if (error)
4267bc27027aSMiklos Szeredi 				return error;
4268bc27027aSMiklos Szeredi 		}
4269da1ce067SMiklos Szeredi 		if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4270da1ce067SMiklos Szeredi 			error = inode_permission(target, MAY_WRITE);
4271da1ce067SMiklos Szeredi 			if (error)
4272da1ce067SMiklos Szeredi 				return error;
4273da1ce067SMiklos Szeredi 		}
4274da1ce067SMiklos Szeredi 	}
42750eeca283SRobert Love 
42760b3974ebSMiklos Szeredi 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
42770b3974ebSMiklos Szeredi 				      flags);
4278bc27027aSMiklos Szeredi 	if (error)
4279bc27027aSMiklos Szeredi 		return error;
4280bc27027aSMiklos Szeredi 
428149d31c2fSAl Viro 	take_dentry_name_snapshot(&old_name, old_dentry);
4282bc27027aSMiklos Szeredi 	dget(new_dentry);
4283da1ce067SMiklos Szeredi 	if (!is_dir || (flags & RENAME_EXCHANGE))
4284bc27027aSMiklos Szeredi 		lock_two_nondirectories(source, target);
4285bc27027aSMiklos Szeredi 	else if (target)
42865955102cSAl Viro 		inode_lock(target);
4287bc27027aSMiklos Szeredi 
4288bc27027aSMiklos Szeredi 	error = -EBUSY;
42897af1364fSEric W. Biederman 	if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
4290bc27027aSMiklos Szeredi 		goto out;
4291bc27027aSMiklos Szeredi 
4292da1ce067SMiklos Szeredi 	if (max_links && new_dir != old_dir) {
4293bc27027aSMiklos Szeredi 		error = -EMLINK;
4294da1ce067SMiklos Szeredi 		if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
4295bc27027aSMiklos Szeredi 			goto out;
4296da1ce067SMiklos Szeredi 		if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4297da1ce067SMiklos Szeredi 		    old_dir->i_nlink >= max_links)
4298da1ce067SMiklos Szeredi 			goto out;
4299da1ce067SMiklos Szeredi 	}
4300da1ce067SMiklos Szeredi 	if (!is_dir) {
4301bc27027aSMiklos Szeredi 		error = try_break_deleg(source, delegated_inode);
4302bc27027aSMiklos Szeredi 		if (error)
4303bc27027aSMiklos Szeredi 			goto out;
4304da1ce067SMiklos Szeredi 	}
4305da1ce067SMiklos Szeredi 	if (target && !new_is_dir) {
4306bc27027aSMiklos Szeredi 		error = try_break_deleg(target, delegated_inode);
4307bc27027aSMiklos Szeredi 		if (error)
4308bc27027aSMiklos Szeredi 			goto out;
4309bc27027aSMiklos Szeredi 	}
4310520c8b16SMiklos Szeredi 	error = old_dir->i_op->rename(old_dir, old_dentry,
4311520c8b16SMiklos Szeredi 				       new_dir, new_dentry, flags);
4312bc27027aSMiklos Szeredi 	if (error)
4313bc27027aSMiklos Szeredi 		goto out;
4314bc27027aSMiklos Szeredi 
4315da1ce067SMiklos Szeredi 	if (!(flags & RENAME_EXCHANGE) && target) {
43168767712fSAl Viro 		if (is_dir) {
43178767712fSAl Viro 			shrink_dcache_parent(new_dentry);
4318bc27027aSMiklos Szeredi 			target->i_flags |= S_DEAD;
43198767712fSAl Viro 		}
4320bc27027aSMiklos Szeredi 		dont_mount(new_dentry);
43218ed936b5SEric W. Biederman 		detach_mounts(new_dentry);
4322bc27027aSMiklos Szeredi 	}
4323da1ce067SMiklos Szeredi 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4324da1ce067SMiklos Szeredi 		if (!(flags & RENAME_EXCHANGE))
4325bc27027aSMiklos Szeredi 			d_move(old_dentry, new_dentry);
4326da1ce067SMiklos Szeredi 		else
4327da1ce067SMiklos Szeredi 			d_exchange(old_dentry, new_dentry);
4328da1ce067SMiklos Szeredi 	}
4329bc27027aSMiklos Szeredi out:
4330da1ce067SMiklos Szeredi 	if (!is_dir || (flags & RENAME_EXCHANGE))
4331bc27027aSMiklos Szeredi 		unlock_two_nondirectories(source, target);
4332bc27027aSMiklos Szeredi 	else if (target)
43335955102cSAl Viro 		inode_unlock(target);
4334bc27027aSMiklos Szeredi 	dput(new_dentry);
4335da1ce067SMiklos Szeredi 	if (!error) {
4336f4ec3a3dSAl Viro 		fsnotify_move(old_dir, new_dir, &old_name.name, is_dir,
4337da1ce067SMiklos Szeredi 			      !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4338da1ce067SMiklos Szeredi 		if (flags & RENAME_EXCHANGE) {
4339f4ec3a3dSAl Viro 			fsnotify_move(new_dir, old_dir, &old_dentry->d_name,
4340da1ce067SMiklos Szeredi 				      new_is_dir, NULL, new_dentry);
4341da1ce067SMiklos Szeredi 		}
4342da1ce067SMiklos Szeredi 	}
434349d31c2fSAl Viro 	release_dentry_name_snapshot(&old_name);
43440eeca283SRobert Love 
43451da177e4SLinus Torvalds 	return error;
43461da177e4SLinus Torvalds }
43474d359507SAl Viro EXPORT_SYMBOL(vfs_rename);
43481da177e4SLinus Torvalds 
4349*e886663cSJens Axboe int do_renameat2(int olddfd, struct filename *from, int newdfd,
4350*e886663cSJens Axboe 		 struct filename *to, unsigned int flags)
43511da177e4SLinus Torvalds {
43521da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
43531da177e4SLinus Torvalds 	struct dentry *trap;
4354f5beed75SAl Viro 	struct path old_path, new_path;
4355f5beed75SAl Viro 	struct qstr old_last, new_last;
4356f5beed75SAl Viro 	int old_type, new_type;
43578e6d782cSJ. Bruce Fields 	struct inode *delegated_inode = NULL;
4358f5beed75SAl Viro 	unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
4359c6a94284SJeff Layton 	bool should_retry = false;
4360*e886663cSJens Axboe 	int error = -EINVAL;
4361520c8b16SMiklos Szeredi 
43620d7a8555SMiklos Szeredi 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
4363*e886663cSJens Axboe 		goto put_both;
4364da1ce067SMiklos Szeredi 
43650d7a8555SMiklos Szeredi 	if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
43660d7a8555SMiklos Szeredi 	    (flags & RENAME_EXCHANGE))
4367*e886663cSJens Axboe 		goto put_both;
4368520c8b16SMiklos Szeredi 
4369f5beed75SAl Viro 	if (flags & RENAME_EXCHANGE)
4370f5beed75SAl Viro 		target_flags = 0;
4371f5beed75SAl Viro 
4372c6a94284SJeff Layton retry:
4373*e886663cSJens Axboe 	from = filename_parentat(olddfd, from, lookup_flags, &old_path,
4374*e886663cSJens Axboe 					&old_last, &old_type);
437591a27b2aSJeff Layton 	if (IS_ERR(from)) {
437691a27b2aSJeff Layton 		error = PTR_ERR(from);
4377*e886663cSJens Axboe 		goto put_new;
437891a27b2aSJeff Layton 	}
43791da177e4SLinus Torvalds 
4380*e886663cSJens Axboe 	to = filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last,
4381*e886663cSJens Axboe 				&new_type);
438291a27b2aSJeff Layton 	if (IS_ERR(to)) {
438391a27b2aSJeff Layton 		error = PTR_ERR(to);
43841da177e4SLinus Torvalds 		goto exit1;
438591a27b2aSJeff Layton 	}
43861da177e4SLinus Torvalds 
43871da177e4SLinus Torvalds 	error = -EXDEV;
4388f5beed75SAl Viro 	if (old_path.mnt != new_path.mnt)
43891da177e4SLinus Torvalds 		goto exit2;
43901da177e4SLinus Torvalds 
43911da177e4SLinus Torvalds 	error = -EBUSY;
4392f5beed75SAl Viro 	if (old_type != LAST_NORM)
43931da177e4SLinus Torvalds 		goto exit2;
43941da177e4SLinus Torvalds 
43950a7c3937SMiklos Szeredi 	if (flags & RENAME_NOREPLACE)
43960a7c3937SMiklos Szeredi 		error = -EEXIST;
4397f5beed75SAl Viro 	if (new_type != LAST_NORM)
43981da177e4SLinus Torvalds 		goto exit2;
43991da177e4SLinus Torvalds 
4400f5beed75SAl Viro 	error = mnt_want_write(old_path.mnt);
4401c30dabfeSJan Kara 	if (error)
4402c30dabfeSJan Kara 		goto exit2;
4403c30dabfeSJan Kara 
44048e6d782cSJ. Bruce Fields retry_deleg:
4405f5beed75SAl Viro 	trap = lock_rename(new_path.dentry, old_path.dentry);
44061da177e4SLinus Torvalds 
4407f5beed75SAl Viro 	old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
44081da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
44091da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
44101da177e4SLinus Torvalds 		goto exit3;
44111da177e4SLinus Torvalds 	/* source must exist */
44121da177e4SLinus Torvalds 	error = -ENOENT;
4413b18825a7SDavid Howells 	if (d_is_negative(old_dentry))
44141da177e4SLinus Torvalds 		goto exit4;
4415f5beed75SAl Viro 	new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
44161da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
44171da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
44181da177e4SLinus Torvalds 		goto exit4;
44190a7c3937SMiklos Szeredi 	error = -EEXIST;
44200a7c3937SMiklos Szeredi 	if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
44210a7c3937SMiklos Szeredi 		goto exit5;
4422da1ce067SMiklos Szeredi 	if (flags & RENAME_EXCHANGE) {
4423da1ce067SMiklos Szeredi 		error = -ENOENT;
4424da1ce067SMiklos Szeredi 		if (d_is_negative(new_dentry))
4425da1ce067SMiklos Szeredi 			goto exit5;
4426da1ce067SMiklos Szeredi 
4427da1ce067SMiklos Szeredi 		if (!d_is_dir(new_dentry)) {
4428da1ce067SMiklos Szeredi 			error = -ENOTDIR;
4429f5beed75SAl Viro 			if (new_last.name[new_last.len])
4430da1ce067SMiklos Szeredi 				goto exit5;
4431da1ce067SMiklos Szeredi 		}
4432da1ce067SMiklos Szeredi 	}
44330a7c3937SMiklos Szeredi 	/* unless the source is a directory trailing slashes give -ENOTDIR */
44340a7c3937SMiklos Szeredi 	if (!d_is_dir(old_dentry)) {
44350a7c3937SMiklos Szeredi 		error = -ENOTDIR;
4436f5beed75SAl Viro 		if (old_last.name[old_last.len])
44370a7c3937SMiklos Szeredi 			goto exit5;
4438f5beed75SAl Viro 		if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
44390a7c3937SMiklos Szeredi 			goto exit5;
44400a7c3937SMiklos Szeredi 	}
44410a7c3937SMiklos Szeredi 	/* source should not be ancestor of target */
44420a7c3937SMiklos Szeredi 	error = -EINVAL;
44430a7c3937SMiklos Szeredi 	if (old_dentry == trap)
44440a7c3937SMiklos Szeredi 		goto exit5;
44451da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
4446da1ce067SMiklos Szeredi 	if (!(flags & RENAME_EXCHANGE))
44471da177e4SLinus Torvalds 		error = -ENOTEMPTY;
44481da177e4SLinus Torvalds 	if (new_dentry == trap)
44491da177e4SLinus Torvalds 		goto exit5;
44501da177e4SLinus Torvalds 
4451f5beed75SAl Viro 	error = security_path_rename(&old_path, old_dentry,
4452f5beed75SAl Viro 				     &new_path, new_dentry, flags);
4453be6d3e56SKentaro Takeda 	if (error)
4454c30dabfeSJan Kara 		goto exit5;
4455f5beed75SAl Viro 	error = vfs_rename(old_path.dentry->d_inode, old_dentry,
4456f5beed75SAl Viro 			   new_path.dentry->d_inode, new_dentry,
4457520c8b16SMiklos Szeredi 			   &delegated_inode, flags);
44581da177e4SLinus Torvalds exit5:
44591da177e4SLinus Torvalds 	dput(new_dentry);
44601da177e4SLinus Torvalds exit4:
44611da177e4SLinus Torvalds 	dput(old_dentry);
44621da177e4SLinus Torvalds exit3:
4463f5beed75SAl Viro 	unlock_rename(new_path.dentry, old_path.dentry);
44648e6d782cSJ. Bruce Fields 	if (delegated_inode) {
44658e6d782cSJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
44668e6d782cSJ. Bruce Fields 		if (!error)
44678e6d782cSJ. Bruce Fields 			goto retry_deleg;
44688e6d782cSJ. Bruce Fields 	}
4469f5beed75SAl Viro 	mnt_drop_write(old_path.mnt);
44701da177e4SLinus Torvalds exit2:
4471c6a94284SJeff Layton 	if (retry_estale(error, lookup_flags))
4472c6a94284SJeff Layton 		should_retry = true;
4473f5beed75SAl Viro 	path_put(&new_path);
44741da177e4SLinus Torvalds exit1:
4475f5beed75SAl Viro 	path_put(&old_path);
4476c6a94284SJeff Layton 	if (should_retry) {
4477c6a94284SJeff Layton 		should_retry = false;
4478c6a94284SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
4479c6a94284SJeff Layton 		goto retry;
4480c6a94284SJeff Layton 	}
4481*e886663cSJens Axboe put_both:
4482*e886663cSJens Axboe 	if (!IS_ERR(from))
4483*e886663cSJens Axboe 		putname(from);
4484*e886663cSJens Axboe put_new:
4485*e886663cSJens Axboe 	if (!IS_ERR(to))
4486*e886663cSJens Axboe 		putname(to);
44871da177e4SLinus Torvalds 	return error;
44881da177e4SLinus Torvalds }
44891da177e4SLinus Torvalds 
4490ee81feb6SDominik Brodowski SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4491ee81feb6SDominik Brodowski 		int, newdfd, const char __user *, newname, unsigned int, flags)
4492ee81feb6SDominik Brodowski {
4493*e886663cSJens Axboe 	return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname),
4494*e886663cSJens Axboe 				flags);
4495ee81feb6SDominik Brodowski }
4496ee81feb6SDominik Brodowski 
4497520c8b16SMiklos Szeredi SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4498520c8b16SMiklos Szeredi 		int, newdfd, const char __user *, newname)
4499520c8b16SMiklos Szeredi {
4500*e886663cSJens Axboe 	return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname),
4501*e886663cSJens Axboe 				0);
4502520c8b16SMiklos Szeredi }
4503520c8b16SMiklos Szeredi 
4504a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
45055590ff0dSUlrich Drepper {
4506*e886663cSJens Axboe 	return do_renameat2(AT_FDCWD, getname(oldname), AT_FDCWD,
4507*e886663cSJens Axboe 				getname(newname), 0);
45085590ff0dSUlrich Drepper }
45095590ff0dSUlrich Drepper 
45105d826c84SAl Viro int readlink_copy(char __user *buffer, int buflen, const char *link)
45111da177e4SLinus Torvalds {
45125d826c84SAl Viro 	int len = PTR_ERR(link);
45131da177e4SLinus Torvalds 	if (IS_ERR(link))
45141da177e4SLinus Torvalds 		goto out;
45151da177e4SLinus Torvalds 
45161da177e4SLinus Torvalds 	len = strlen(link);
45171da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
45181da177e4SLinus Torvalds 		len = buflen;
45191da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
45201da177e4SLinus Torvalds 		len = -EFAULT;
45211da177e4SLinus Torvalds out:
45221da177e4SLinus Torvalds 	return len;
45231da177e4SLinus Torvalds }
45241da177e4SLinus Torvalds 
4525d60874cdSMiklos Szeredi /**
4526fd4a0edfSMiklos Szeredi  * vfs_readlink - copy symlink body into userspace buffer
4527fd4a0edfSMiklos Szeredi  * @dentry: dentry on which to get symbolic link
4528fd4a0edfSMiklos Szeredi  * @buffer: user memory pointer
4529fd4a0edfSMiklos Szeredi  * @buflen: size of buffer
4530fd4a0edfSMiklos Szeredi  *
4531fd4a0edfSMiklos Szeredi  * Does not touch atime.  That's up to the caller if necessary
4532fd4a0edfSMiklos Szeredi  *
4533fd4a0edfSMiklos Szeredi  * Does not call security hook.
4534fd4a0edfSMiklos Szeredi  */
4535fd4a0edfSMiklos Szeredi int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4536fd4a0edfSMiklos Szeredi {
4537fd4a0edfSMiklos Szeredi 	struct inode *inode = d_inode(dentry);
4538f2df5da6SAl Viro 	DEFINE_DELAYED_CALL(done);
4539f2df5da6SAl Viro 	const char *link;
4540f2df5da6SAl Viro 	int res;
4541fd4a0edfSMiklos Szeredi 
454276fca90eSMiklos Szeredi 	if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
454376fca90eSMiklos Szeredi 		if (unlikely(inode->i_op->readlink))
454476fca90eSMiklos Szeredi 			return inode->i_op->readlink(dentry, buffer, buflen);
454576fca90eSMiklos Szeredi 
454676fca90eSMiklos Szeredi 		if (!d_is_symlink(dentry))
4547fd4a0edfSMiklos Szeredi 			return -EINVAL;
4548fd4a0edfSMiklos Szeredi 
454976fca90eSMiklos Szeredi 		spin_lock(&inode->i_lock);
455076fca90eSMiklos Szeredi 		inode->i_opflags |= IOP_DEFAULT_READLINK;
455176fca90eSMiklos Szeredi 		spin_unlock(&inode->i_lock);
455276fca90eSMiklos Szeredi 	}
455376fca90eSMiklos Szeredi 
45544c4f7c19SEric Biggers 	link = READ_ONCE(inode->i_link);
4555f2df5da6SAl Viro 	if (!link) {
4556f2df5da6SAl Viro 		link = inode->i_op->get_link(dentry, inode, &done);
4557f2df5da6SAl Viro 		if (IS_ERR(link))
4558f2df5da6SAl Viro 			return PTR_ERR(link);
4559f2df5da6SAl Viro 	}
4560f2df5da6SAl Viro 	res = readlink_copy(buffer, buflen, link);
4561f2df5da6SAl Viro 	do_delayed_call(&done);
4562f2df5da6SAl Viro 	return res;
4563fd4a0edfSMiklos Szeredi }
4564fd4a0edfSMiklos Szeredi EXPORT_SYMBOL(vfs_readlink);
45651da177e4SLinus Torvalds 
4566d60874cdSMiklos Szeredi /**
4567d60874cdSMiklos Szeredi  * vfs_get_link - get symlink body
4568d60874cdSMiklos Szeredi  * @dentry: dentry on which to get symbolic link
4569d60874cdSMiklos Szeredi  * @done: caller needs to free returned data with this
4570d60874cdSMiklos Szeredi  *
4571d60874cdSMiklos Szeredi  * Calls security hook and i_op->get_link() on the supplied inode.
4572d60874cdSMiklos Szeredi  *
4573d60874cdSMiklos Szeredi  * It does not touch atime.  That's up to the caller if necessary.
4574d60874cdSMiklos Szeredi  *
4575d60874cdSMiklos Szeredi  * Does not work on "special" symlinks like /proc/$$/fd/N
4576d60874cdSMiklos Szeredi  */
4577d60874cdSMiklos Szeredi const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4578d60874cdSMiklos Szeredi {
4579d60874cdSMiklos Szeredi 	const char *res = ERR_PTR(-EINVAL);
4580d60874cdSMiklos Szeredi 	struct inode *inode = d_inode(dentry);
4581d60874cdSMiklos Szeredi 
4582d60874cdSMiklos Szeredi 	if (d_is_symlink(dentry)) {
4583d60874cdSMiklos Szeredi 		res = ERR_PTR(security_inode_readlink(dentry));
4584d60874cdSMiklos Szeredi 		if (!res)
4585d60874cdSMiklos Szeredi 			res = inode->i_op->get_link(dentry, inode, done);
4586d60874cdSMiklos Szeredi 	}
4587d60874cdSMiklos Szeredi 	return res;
4588d60874cdSMiklos Szeredi }
4589d60874cdSMiklos Szeredi EXPORT_SYMBOL(vfs_get_link);
4590d60874cdSMiklos Szeredi 
45911da177e4SLinus Torvalds /* get the link contents into pagecache */
45926b255391SAl Viro const char *page_get_link(struct dentry *dentry, struct inode *inode,
4593fceef393SAl Viro 			  struct delayed_call *callback)
45941da177e4SLinus Torvalds {
4595ebd09abbSDuane Griffin 	char *kaddr;
45961da177e4SLinus Torvalds 	struct page *page;
45976b255391SAl Viro 	struct address_space *mapping = inode->i_mapping;
45986b255391SAl Viro 
4599d3883d4fSAl Viro 	if (!dentry) {
4600d3883d4fSAl Viro 		page = find_get_page(mapping, 0);
4601d3883d4fSAl Viro 		if (!page)
46026b255391SAl Viro 			return ERR_PTR(-ECHILD);
4603d3883d4fSAl Viro 		if (!PageUptodate(page)) {
4604d3883d4fSAl Viro 			put_page(page);
4605d3883d4fSAl Viro 			return ERR_PTR(-ECHILD);
4606d3883d4fSAl Viro 		}
4607d3883d4fSAl Viro 	} else {
4608090d2b18SPekka Enberg 		page = read_mapping_page(mapping, 0, NULL);
46091da177e4SLinus Torvalds 		if (IS_ERR(page))
46106fe6900eSNick Piggin 			return (char*)page;
4611d3883d4fSAl Viro 	}
4612fceef393SAl Viro 	set_delayed_call(callback, page_put_link, page);
461321fc61c7SAl Viro 	BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
461421fc61c7SAl Viro 	kaddr = page_address(page);
46156b255391SAl Viro 	nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
4616ebd09abbSDuane Griffin 	return kaddr;
46171da177e4SLinus Torvalds }
46181da177e4SLinus Torvalds 
46196b255391SAl Viro EXPORT_SYMBOL(page_get_link);
46201da177e4SLinus Torvalds 
4621fceef393SAl Viro void page_put_link(void *arg)
46221da177e4SLinus Torvalds {
4623fceef393SAl Viro 	put_page(arg);
46241da177e4SLinus Torvalds }
46254d359507SAl Viro EXPORT_SYMBOL(page_put_link);
46261da177e4SLinus Torvalds 
4627aa80deabSAl Viro int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4628aa80deabSAl Viro {
4629fceef393SAl Viro 	DEFINE_DELAYED_CALL(done);
46306b255391SAl Viro 	int res = readlink_copy(buffer, buflen,
46316b255391SAl Viro 				page_get_link(dentry, d_inode(dentry),
4632fceef393SAl Viro 					      &done));
4633fceef393SAl Viro 	do_delayed_call(&done);
4634aa80deabSAl Viro 	return res;
4635aa80deabSAl Viro }
4636aa80deabSAl Viro EXPORT_SYMBOL(page_readlink);
4637aa80deabSAl Viro 
463854566b2cSNick Piggin /*
463954566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
464054566b2cSNick Piggin  */
464154566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
46421da177e4SLinus Torvalds {
46431da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
46440adb25d2SKirill Korotaev 	struct page *page;
4645afddba49SNick Piggin 	void *fsdata;
4646beb497abSDmitriy Monakhov 	int err;
4647c718a975STetsuo Handa 	unsigned int flags = 0;
464854566b2cSNick Piggin 	if (nofs)
464954566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
46501da177e4SLinus Torvalds 
46517e53cac4SNeilBrown retry:
4652afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
465354566b2cSNick Piggin 				flags, &page, &fsdata);
46541da177e4SLinus Torvalds 	if (err)
4655afddba49SNick Piggin 		goto fail;
4656afddba49SNick Piggin 
465721fc61c7SAl Viro 	memcpy(page_address(page), symname, len-1);
4658afddba49SNick Piggin 
4659afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4660afddba49SNick Piggin 							page, fsdata);
46611da177e4SLinus Torvalds 	if (err < 0)
46621da177e4SLinus Torvalds 		goto fail;
4663afddba49SNick Piggin 	if (err < len-1)
4664afddba49SNick Piggin 		goto retry;
4665afddba49SNick Piggin 
46661da177e4SLinus Torvalds 	mark_inode_dirty(inode);
46671da177e4SLinus Torvalds 	return 0;
46681da177e4SLinus Torvalds fail:
46691da177e4SLinus Torvalds 	return err;
46701da177e4SLinus Torvalds }
46714d359507SAl Viro EXPORT_SYMBOL(__page_symlink);
46721da177e4SLinus Torvalds 
46730adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
46740adb25d2SKirill Korotaev {
46750adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
4676c62d2555SMichal Hocko 			!mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
46770adb25d2SKirill Korotaev }
46784d359507SAl Viro EXPORT_SYMBOL(page_symlink);
46790adb25d2SKirill Korotaev 
468092e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
46816b255391SAl Viro 	.get_link	= page_get_link,
46821da177e4SLinus Torvalds };
46831da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
4684