xref: /openbmc/linux/fs/namei.c (revision ba73d987)
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 
26247291baaSChristian Brauner /**
26347291baaSChristian Brauner  * check_acl - perform ACL permission checking
26447291baaSChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
26547291baaSChristian Brauner  * @inode:	inode to check permissions on
26647291baaSChristian Brauner  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
26747291baaSChristian Brauner  *
26847291baaSChristian Brauner  * This function performs the ACL permission checking. Since this function
26947291baaSChristian Brauner  * retrieve POSIX acls it needs to know whether it is called from a blocking or
27047291baaSChristian Brauner  * non-blocking context and thus cares about the MAY_NOT_BLOCK bit.
27147291baaSChristian Brauner  *
27247291baaSChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
27347291baaSChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then take
27447291baaSChristian Brauner  * care to map the inode according to @mnt_userns before checking permissions.
27547291baaSChristian Brauner  * On non-idmapped mounts or if permission checking is to be performed on the
27647291baaSChristian Brauner  * raw inode simply passs init_user_ns.
27747291baaSChristian Brauner  */
27847291baaSChristian Brauner static int check_acl(struct user_namespace *mnt_userns,
27947291baaSChristian Brauner 		     struct inode *inode, int mask)
280e77819e5SLinus Torvalds {
28184635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL
282e77819e5SLinus Torvalds 	struct posix_acl *acl;
283e77819e5SLinus Torvalds 
284e77819e5SLinus Torvalds 	if (mask & MAY_NOT_BLOCK) {
2853567866bSAl Viro 		acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
2863567866bSAl Viro 	        if (!acl)
287e77819e5SLinus Torvalds 	                return -EAGAIN;
2883567866bSAl Viro 		/* no ->get_acl() calls in RCU mode... */
289b8a7a3a6SAndreas Gruenbacher 		if (is_uncached_acl(acl))
290e77819e5SLinus Torvalds 			return -ECHILD;
29147291baaSChristian Brauner 	        return posix_acl_permission(mnt_userns, inode, acl, mask);
292e77819e5SLinus Torvalds 	}
293e77819e5SLinus Torvalds 
2942982baa2SChristoph Hellwig 	acl = get_acl(inode, ACL_TYPE_ACCESS);
2954e34e719SChristoph Hellwig 	if (IS_ERR(acl))
2964e34e719SChristoph Hellwig 		return PTR_ERR(acl);
297e77819e5SLinus Torvalds 	if (acl) {
29847291baaSChristian Brauner 	        int error = posix_acl_permission(mnt_userns, inode, acl, mask);
299e77819e5SLinus Torvalds 	        posix_acl_release(acl);
300e77819e5SLinus Torvalds 	        return error;
301e77819e5SLinus Torvalds 	}
30284635d68SLinus Torvalds #endif
303e77819e5SLinus Torvalds 
304e77819e5SLinus Torvalds 	return -EAGAIN;
305e77819e5SLinus Torvalds }
306e77819e5SLinus Torvalds 
30747291baaSChristian Brauner /**
30847291baaSChristian Brauner  * acl_permission_check - perform basic UNIX permission checking
30947291baaSChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
31047291baaSChristian Brauner  * @inode:	inode to check permissions on
31147291baaSChristian Brauner  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
3125fc475b7SLinus Torvalds  *
31347291baaSChristian Brauner  * This function performs the basic UNIX permission checking. Since this
31447291baaSChristian Brauner  * function may retrieve POSIX acls it needs to know whether it is called from a
31547291baaSChristian Brauner  * blocking or non-blocking context and thus cares about the MAY_NOT_BLOCK bit.
31647291baaSChristian Brauner  *
31747291baaSChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
31847291baaSChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then take
31947291baaSChristian Brauner  * care to map the inode according to @mnt_userns before checking permissions.
32047291baaSChristian Brauner  * On non-idmapped mounts or if permission checking is to be performed on the
32147291baaSChristian Brauner  * raw inode simply passs init_user_ns.
3225909ccaaSLinus Torvalds  */
32347291baaSChristian Brauner static int acl_permission_check(struct user_namespace *mnt_userns,
32447291baaSChristian Brauner 				struct inode *inode, int mask)
3255909ccaaSLinus Torvalds {
32626cf46beSLinus Torvalds 	unsigned int mode = inode->i_mode;
32747291baaSChristian Brauner 	kuid_t i_uid;
3285909ccaaSLinus Torvalds 
3295fc475b7SLinus Torvalds 	/* Are we the owner? If so, ACL's don't matter */
33047291baaSChristian Brauner 	i_uid = i_uid_into_mnt(mnt_userns, inode);
33147291baaSChristian Brauner 	if (likely(uid_eq(current_fsuid(), i_uid))) {
3325fc475b7SLinus Torvalds 		mask &= 7;
3335909ccaaSLinus Torvalds 		mode >>= 6;
3345fc475b7SLinus Torvalds 		return (mask & ~mode) ? -EACCES : 0;
3355fc475b7SLinus Torvalds 	}
3365fc475b7SLinus Torvalds 
3375fc475b7SLinus Torvalds 	/* Do we have ACL's? */
338e77819e5SLinus Torvalds 	if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
33947291baaSChristian Brauner 		int error = check_acl(mnt_userns, inode, mask);
3405909ccaaSLinus Torvalds 		if (error != -EAGAIN)
3415909ccaaSLinus Torvalds 			return error;
3425909ccaaSLinus Torvalds 	}
3435909ccaaSLinus Torvalds 
3445fc475b7SLinus Torvalds 	/* Only RWX matters for group/other mode bits */
3455fc475b7SLinus Torvalds 	mask &= 7;
3465fc475b7SLinus Torvalds 
3475fc475b7SLinus Torvalds 	/*
3485fc475b7SLinus Torvalds 	 * Are the group permissions different from
3495fc475b7SLinus Torvalds 	 * the other permissions in the bits we care
3505fc475b7SLinus Torvalds 	 * about? Need to check group ownership if so.
3515fc475b7SLinus Torvalds 	 */
3525fc475b7SLinus Torvalds 	if (mask & (mode ^ (mode >> 3))) {
35347291baaSChristian Brauner 		kgid_t kgid = i_gid_into_mnt(mnt_userns, inode);
35447291baaSChristian Brauner 		if (in_group_p(kgid))
3555909ccaaSLinus Torvalds 			mode >>= 3;
3565909ccaaSLinus Torvalds 	}
3575909ccaaSLinus Torvalds 
3585fc475b7SLinus Torvalds 	/* Bits in 'mode' clear that we require? */
3595fc475b7SLinus Torvalds 	return (mask & ~mode) ? -EACCES : 0;
3605909ccaaSLinus Torvalds }
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds /**
3631da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
36447291baaSChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
3651da177e4SLinus Torvalds  * @inode:	inode to check access rights for
3665fc475b7SLinus Torvalds  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC,
3675fc475b7SLinus Torvalds  *		%MAY_NOT_BLOCK ...)
3681da177e4SLinus Torvalds  *
3691da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
3701da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
3711da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
372b74c79e9SNick Piggin  * are used for other things.
373b74c79e9SNick Piggin  *
374b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
375b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
376b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
37747291baaSChristian Brauner  *
37847291baaSChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
37947291baaSChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then take
38047291baaSChristian Brauner  * care to map the inode according to @mnt_userns before checking permissions.
38147291baaSChristian Brauner  * On non-idmapped mounts or if permission checking is to be performed on the
38247291baaSChristian Brauner  * raw inode simply passs init_user_ns.
3831da177e4SLinus Torvalds  */
38447291baaSChristian Brauner int generic_permission(struct user_namespace *mnt_userns, struct inode *inode,
38547291baaSChristian Brauner 		       int mask)
3861da177e4SLinus Torvalds {
3875909ccaaSLinus Torvalds 	int ret;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	/*
390948409c7SAndreas Gruenbacher 	 * Do the basic permission checks.
3911da177e4SLinus Torvalds 	 */
39247291baaSChristian Brauner 	ret = acl_permission_check(mnt_userns, inode, mask);
3935909ccaaSLinus Torvalds 	if (ret != -EACCES)
3945909ccaaSLinus Torvalds 		return ret;
3951da177e4SLinus Torvalds 
396d594e7ecSAl Viro 	if (S_ISDIR(inode->i_mode)) {
397d594e7ecSAl Viro 		/* DACs are overridable for directories */
398d594e7ecSAl Viro 		if (!(mask & MAY_WRITE))
39947291baaSChristian Brauner 			if (capable_wrt_inode_uidgid(mnt_userns, inode,
40023adbe12SAndy Lutomirski 						     CAP_DAC_READ_SEARCH))
401d594e7ecSAl Viro 				return 0;
40247291baaSChristian Brauner 		if (capable_wrt_inode_uidgid(mnt_userns, inode,
4030558c1bfSChristian Brauner 					     CAP_DAC_OVERRIDE))
4041da177e4SLinus Torvalds 			return 0;
4052a4c2242SStephen Smalley 		return -EACCES;
4062a4c2242SStephen Smalley 	}
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	/*
4091da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
4101da177e4SLinus Torvalds 	 */
4117ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
412d594e7ecSAl Viro 	if (mask == MAY_READ)
41347291baaSChristian Brauner 		if (capable_wrt_inode_uidgid(mnt_userns, inode,
4140558c1bfSChristian Brauner 					     CAP_DAC_READ_SEARCH))
4151da177e4SLinus Torvalds 			return 0;
4162a4c2242SStephen Smalley 	/*
4172a4c2242SStephen Smalley 	 * Read/write DACs are always overridable.
4182a4c2242SStephen Smalley 	 * Executable DACs are overridable when there is
4192a4c2242SStephen Smalley 	 * at least one exec bit set.
4202a4c2242SStephen Smalley 	 */
4212a4c2242SStephen Smalley 	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
42247291baaSChristian Brauner 		if (capable_wrt_inode_uidgid(mnt_userns, inode,
4230558c1bfSChristian Brauner 					     CAP_DAC_OVERRIDE))
4242a4c2242SStephen Smalley 			return 0;
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	return -EACCES;
4271da177e4SLinus Torvalds }
4284d359507SAl Viro EXPORT_SYMBOL(generic_permission);
4291da177e4SLinus Torvalds 
43047291baaSChristian Brauner /**
43147291baaSChristian Brauner  * do_inode_permission - UNIX permission checking
43247291baaSChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
43347291baaSChristian Brauner  * @inode:	inode to check permissions on
43447291baaSChristian Brauner  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
43547291baaSChristian Brauner  *
4363ddcd056SLinus Torvalds  * We _really_ want to just do "generic_permission()" without
4373ddcd056SLinus Torvalds  * even looking at the inode->i_op values. So we keep a cache
4383ddcd056SLinus Torvalds  * flag in inode->i_opflags, that says "this has not special
4393ddcd056SLinus Torvalds  * permission function, use the fast case".
4403ddcd056SLinus Torvalds  */
44147291baaSChristian Brauner static inline int do_inode_permission(struct user_namespace *mnt_userns,
44247291baaSChristian Brauner 				      struct inode *inode, int mask)
4433ddcd056SLinus Torvalds {
4443ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
4453ddcd056SLinus Torvalds 		if (likely(inode->i_op->permission))
4463ddcd056SLinus Torvalds 			return inode->i_op->permission(inode, mask);
4473ddcd056SLinus Torvalds 
4483ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
4493ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
4503ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_FASTPERM;
4513ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
4523ddcd056SLinus Torvalds 	}
45347291baaSChristian Brauner 	return generic_permission(mnt_userns, inode, mask);
4543ddcd056SLinus Torvalds }
4553ddcd056SLinus Torvalds 
456cb23beb5SChristoph Hellwig /**
4570bdaea90SDavid Howells  * sb_permission - Check superblock-level permissions
4580bdaea90SDavid Howells  * @sb: Superblock of inode to check permission on
45955852635SRandy Dunlap  * @inode: Inode to check permission on
4600bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
4610bdaea90SDavid Howells  *
4620bdaea90SDavid Howells  * Separate out file-system wide checks from inode-specific permission checks.
4630bdaea90SDavid Howells  */
4640bdaea90SDavid Howells static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
4650bdaea90SDavid Howells {
4660bdaea90SDavid Howells 	if (unlikely(mask & MAY_WRITE)) {
4670bdaea90SDavid Howells 		umode_t mode = inode->i_mode;
4680bdaea90SDavid Howells 
4690bdaea90SDavid Howells 		/* Nobody gets write access to a read-only fs. */
470bc98a42cSDavid Howells 		if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
4710bdaea90SDavid Howells 			return -EROFS;
4720bdaea90SDavid Howells 	}
4730bdaea90SDavid Howells 	return 0;
4740bdaea90SDavid Howells }
4750bdaea90SDavid Howells 
4760bdaea90SDavid Howells /**
4770bdaea90SDavid Howells  * inode_permission - Check for access rights to a given inode
47847291baaSChristian Brauner  * @mnt_userns:	User namespace of the mount the inode was found from
4790bdaea90SDavid Howells  * @inode:	Inode to check permission on
4800bdaea90SDavid Howells  * @mask:	Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
4810bdaea90SDavid Howells  *
4820bdaea90SDavid Howells  * Check for read/write/execute permissions on an inode.  We use fs[ug]id for
4830bdaea90SDavid Howells  * this, letting us set arbitrary permissions for filesystem access without
4840bdaea90SDavid Howells  * changing the "normal" UIDs which are used for other things.
4850bdaea90SDavid Howells  *
4860bdaea90SDavid Howells  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
4870bdaea90SDavid Howells  */
48847291baaSChristian Brauner int inode_permission(struct user_namespace *mnt_userns,
48947291baaSChristian Brauner 		     struct inode *inode, int mask)
4900bdaea90SDavid Howells {
4910bdaea90SDavid Howells 	int retval;
4920bdaea90SDavid Howells 
4930bdaea90SDavid Howells 	retval = sb_permission(inode->i_sb, inode, mask);
4940bdaea90SDavid Howells 	if (retval)
4950bdaea90SDavid Howells 		return retval;
4964bfd054aSEric Biggers 
4974bfd054aSEric Biggers 	if (unlikely(mask & MAY_WRITE)) {
4984bfd054aSEric Biggers 		/*
4994bfd054aSEric Biggers 		 * Nobody gets write access to an immutable file.
5004bfd054aSEric Biggers 		 */
5014bfd054aSEric Biggers 		if (IS_IMMUTABLE(inode))
5024bfd054aSEric Biggers 			return -EPERM;
5034bfd054aSEric Biggers 
5044bfd054aSEric Biggers 		/*
5054bfd054aSEric Biggers 		 * Updating mtime will likely cause i_uid and i_gid to be
5064bfd054aSEric Biggers 		 * written back improperly if their true value is unknown
5074bfd054aSEric Biggers 		 * to the vfs.
5084bfd054aSEric Biggers 		 */
509*ba73d987SChristian Brauner 		if (HAS_UNMAPPED_ID(mnt_userns, inode))
5104bfd054aSEric Biggers 			return -EACCES;
5114bfd054aSEric Biggers 	}
5124bfd054aSEric Biggers 
51347291baaSChristian Brauner 	retval = do_inode_permission(mnt_userns, inode, mask);
5144bfd054aSEric Biggers 	if (retval)
5154bfd054aSEric Biggers 		return retval;
5164bfd054aSEric Biggers 
5174bfd054aSEric Biggers 	retval = devcgroup_inode_permission(inode, mask);
5184bfd054aSEric Biggers 	if (retval)
5194bfd054aSEric Biggers 		return retval;
5204bfd054aSEric Biggers 
5214bfd054aSEric Biggers 	return security_inode_permission(inode, mask);
5220bdaea90SDavid Howells }
5234d359507SAl Viro EXPORT_SYMBOL(inode_permission);
5240bdaea90SDavid Howells 
5250bdaea90SDavid Howells /**
5265dd784d0SJan Blunck  * path_get - get a reference to a path
5275dd784d0SJan Blunck  * @path: path to get the reference to
5285dd784d0SJan Blunck  *
5295dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
5305dd784d0SJan Blunck  */
531dcf787f3SAl Viro void path_get(const struct path *path)
5325dd784d0SJan Blunck {
5335dd784d0SJan Blunck 	mntget(path->mnt);
5345dd784d0SJan Blunck 	dget(path->dentry);
5355dd784d0SJan Blunck }
5365dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
5375dd784d0SJan Blunck 
5385dd784d0SJan Blunck /**
5391d957f9bSJan Blunck  * path_put - put a reference to a path
5401d957f9bSJan Blunck  * @path: path to put the reference to
5411d957f9bSJan Blunck  *
5421d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
5431d957f9bSJan Blunck  */
544dcf787f3SAl Viro void path_put(const struct path *path)
5451da177e4SLinus Torvalds {
5461d957f9bSJan Blunck 	dput(path->dentry);
5471d957f9bSJan Blunck 	mntput(path->mnt);
5481da177e4SLinus Torvalds }
5491d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
5501da177e4SLinus Torvalds 
551894bc8c4SAl Viro #define EMBEDDED_LEVELS 2
5521f55a6ecSAl Viro struct nameidata {
5531f55a6ecSAl Viro 	struct path	path;
5541f55a6ecSAl Viro 	struct qstr	last;
5551f55a6ecSAl Viro 	struct path	root;
5561f55a6ecSAl Viro 	struct inode	*inode; /* path.dentry.d_inode */
5571f55a6ecSAl Viro 	unsigned int	flags;
558ab87f9a5SAleksa Sarai 	unsigned	seq, m_seq, r_seq;
5591f55a6ecSAl Viro 	int		last_type;
5601f55a6ecSAl Viro 	unsigned	depth;
561756daf26SNeilBrown 	int		total_link_count;
562697fc6caSAl Viro 	struct saved {
563697fc6caSAl Viro 		struct path link;
564fceef393SAl Viro 		struct delayed_call done;
565697fc6caSAl Viro 		const char *name;
5660450b2d1SAl Viro 		unsigned seq;
567894bc8c4SAl Viro 	} *stack, internal[EMBEDDED_LEVELS];
5689883d185SAl Viro 	struct filename	*name;
5699883d185SAl Viro 	struct nameidata *saved;
5709883d185SAl Viro 	unsigned	root_seq;
5719883d185SAl Viro 	int		dfd;
5720f705953SAl Viro 	kuid_t		dir_uid;
5730f705953SAl Viro 	umode_t		dir_mode;
5743859a271SKees Cook } __randomize_layout;
5751f55a6ecSAl Viro 
5769883d185SAl Viro static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
577894bc8c4SAl Viro {
578756daf26SNeilBrown 	struct nameidata *old = current->nameidata;
579756daf26SNeilBrown 	p->stack = p->internal;
580c8a53ee5SAl Viro 	p->dfd = dfd;
581c8a53ee5SAl Viro 	p->name = name;
582756daf26SNeilBrown 	p->total_link_count = old ? old->total_link_count : 0;
5839883d185SAl Viro 	p->saved = old;
584756daf26SNeilBrown 	current->nameidata = p;
585894bc8c4SAl Viro }
586894bc8c4SAl Viro 
5879883d185SAl Viro static void restore_nameidata(void)
588894bc8c4SAl Viro {
5899883d185SAl Viro 	struct nameidata *now = current->nameidata, *old = now->saved;
590756daf26SNeilBrown 
591756daf26SNeilBrown 	current->nameidata = old;
592756daf26SNeilBrown 	if (old)
593756daf26SNeilBrown 		old->total_link_count = now->total_link_count;
594e1a63bbcSAl Viro 	if (now->stack != now->internal)
595756daf26SNeilBrown 		kfree(now->stack);
596894bc8c4SAl Viro }
597894bc8c4SAl Viro 
59860ef60c7SAl Viro static bool nd_alloc_stack(struct nameidata *nd)
599894bc8c4SAl Viro {
600bc40aee0SAl Viro 	struct saved *p;
601bc40aee0SAl Viro 
6026da2ec56SKees Cook 	p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved),
60360ef60c7SAl Viro 			 nd->flags & LOOKUP_RCU ? GFP_ATOMIC : GFP_KERNEL);
604bc40aee0SAl Viro 	if (unlikely(!p))
60560ef60c7SAl Viro 		return false;
606894bc8c4SAl Viro 	memcpy(p, nd->internal, sizeof(nd->internal));
607894bc8c4SAl Viro 	nd->stack = p;
60860ef60c7SAl Viro 	return true;
609894bc8c4SAl Viro }
610894bc8c4SAl Viro 
611397d425dSEric W. Biederman /**
6126b03f7edSAl Viro  * path_connected - Verify that a dentry is below mnt.mnt_root
613397d425dSEric W. Biederman  *
614397d425dSEric W. Biederman  * Rename can sometimes move a file or directory outside of a bind
615397d425dSEric W. Biederman  * mount, path_connected allows those cases to be detected.
616397d425dSEric W. Biederman  */
6176b03f7edSAl Viro static bool path_connected(struct vfsmount *mnt, struct dentry *dentry)
618397d425dSEric W. Biederman {
61995dd7758SEric W. Biederman 	struct super_block *sb = mnt->mnt_sb;
620397d425dSEric W. Biederman 
621402dd2cfSChristoph Hellwig 	/* Bind mounts can have disconnected paths */
622402dd2cfSChristoph Hellwig 	if (mnt->mnt_root == sb->s_root)
623397d425dSEric W. Biederman 		return true;
624397d425dSEric W. Biederman 
6256b03f7edSAl Viro 	return is_subdir(dentry, mnt->mnt_root);
626397d425dSEric W. Biederman }
627397d425dSEric W. Biederman 
6287973387aSAl Viro static void drop_links(struct nameidata *nd)
6297973387aSAl Viro {
6307973387aSAl Viro 	int i = nd->depth;
6317973387aSAl Viro 	while (i--) {
6327973387aSAl Viro 		struct saved *last = nd->stack + i;
633fceef393SAl Viro 		do_delayed_call(&last->done);
634fceef393SAl Viro 		clear_delayed_call(&last->done);
6357973387aSAl Viro 	}
6367973387aSAl Viro }
6377973387aSAl Viro 
6387973387aSAl Viro static void terminate_walk(struct nameidata *nd)
6397973387aSAl Viro {
6407973387aSAl Viro 	drop_links(nd);
6417973387aSAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
6427973387aSAl Viro 		int i;
6437973387aSAl Viro 		path_put(&nd->path);
6447973387aSAl Viro 		for (i = 0; i < nd->depth; i++)
6457973387aSAl Viro 			path_put(&nd->stack[i].link);
64684a2bd39SAl Viro 		if (nd->flags & LOOKUP_ROOT_GRABBED) {
647102b8af2SAl Viro 			path_put(&nd->root);
64884a2bd39SAl Viro 			nd->flags &= ~LOOKUP_ROOT_GRABBED;
649102b8af2SAl Viro 		}
6507973387aSAl Viro 	} else {
6517973387aSAl Viro 		nd->flags &= ~LOOKUP_RCU;
6527973387aSAl Viro 		rcu_read_unlock();
6537973387aSAl Viro 	}
6547973387aSAl Viro 	nd->depth = 0;
6557973387aSAl Viro }
6567973387aSAl Viro 
6577973387aSAl Viro /* path_put is needed afterwards regardless of success or failure */
6582aa38470SAl Viro static bool __legitimize_path(struct path *path, unsigned seq, unsigned mseq)
6597973387aSAl Viro {
6602aa38470SAl Viro 	int res = __legitimize_mnt(path->mnt, mseq);
6617973387aSAl Viro 	if (unlikely(res)) {
6627973387aSAl Viro 		if (res > 0)
6637973387aSAl Viro 			path->mnt = NULL;
6647973387aSAl Viro 		path->dentry = NULL;
6657973387aSAl Viro 		return false;
6667973387aSAl Viro 	}
6677973387aSAl Viro 	if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
6687973387aSAl Viro 		path->dentry = NULL;
6697973387aSAl Viro 		return false;
6707973387aSAl Viro 	}
6717973387aSAl Viro 	return !read_seqcount_retry(&path->dentry->d_seq, seq);
6727973387aSAl Viro }
6737973387aSAl Viro 
6742aa38470SAl Viro static inline bool legitimize_path(struct nameidata *nd,
6752aa38470SAl Viro 			    struct path *path, unsigned seq)
6762aa38470SAl Viro {
6775bd73286SAl Viro 	return __legitimize_path(path, seq, nd->m_seq);
6782aa38470SAl Viro }
6792aa38470SAl Viro 
6807973387aSAl Viro static bool legitimize_links(struct nameidata *nd)
6817973387aSAl Viro {
6827973387aSAl Viro 	int i;
6837973387aSAl Viro 	for (i = 0; i < nd->depth; i++) {
6847973387aSAl Viro 		struct saved *last = nd->stack + i;
6857973387aSAl Viro 		if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
6867973387aSAl Viro 			drop_links(nd);
6877973387aSAl Viro 			nd->depth = i + 1;
6887973387aSAl Viro 			return false;
6897973387aSAl Viro 		}
6907973387aSAl Viro 	}
6917973387aSAl Viro 	return true;
6927973387aSAl Viro }
6937973387aSAl Viro 
694ee594bffSAl Viro static bool legitimize_root(struct nameidata *nd)
695ee594bffSAl Viro {
696adb21d2bSAleksa Sarai 	/*
697adb21d2bSAleksa Sarai 	 * For scoped-lookups (where nd->root has been zeroed), we need to
698adb21d2bSAleksa Sarai 	 * restart the whole lookup from scratch -- because set_root() is wrong
699adb21d2bSAleksa Sarai 	 * for these lookups (nd->dfd is the root, not the filesystem root).
700adb21d2bSAleksa Sarai 	 */
701adb21d2bSAleksa Sarai 	if (!nd->root.mnt && (nd->flags & LOOKUP_IS_SCOPED))
702adb21d2bSAleksa Sarai 		return false;
703adb21d2bSAleksa Sarai 	/* Nothing to do if nd->root is zero or is managed by the VFS user. */
704ee594bffSAl Viro 	if (!nd->root.mnt || (nd->flags & LOOKUP_ROOT))
705ee594bffSAl Viro 		return true;
70684a2bd39SAl Viro 	nd->flags |= LOOKUP_ROOT_GRABBED;
707ee594bffSAl Viro 	return legitimize_path(nd, &nd->root, nd->root_seq);
708ee594bffSAl Viro }
709ee594bffSAl Viro 
71019660af7SAl Viro /*
71131e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
71219660af7SAl Viro  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
71319660af7SAl Viro  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
71457e3715cSMike Marshall  * normal reference counts on dentries and vfsmounts to transition to ref-walk
71519660af7SAl Viro  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
71619660af7SAl Viro  * got stuck, so ref-walk may continue from there. If this is not successful
71719660af7SAl Viro  * (eg. a seqcount has changed), then failure is returned and it's up to caller
71819660af7SAl Viro  * to restart the path walk from the beginning in ref-walk mode.
71931e6b01fSNick Piggin  */
72031e6b01fSNick Piggin 
72131e6b01fSNick Piggin /**
72219660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
72319660af7SAl Viro  * @nd: nameidata pathwalk data
72439191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
72531e6b01fSNick Piggin  *
7264675ac39SAl Viro  * unlazy_walk attempts to legitimize the current nd->path and nd->root
7274675ac39SAl Viro  * for ref-walk mode.
7284675ac39SAl Viro  * Must be called from rcu-walk context.
7297973387aSAl Viro  * Nothing should touch nameidata between unlazy_walk() failure and
7307973387aSAl Viro  * terminate_walk().
73131e6b01fSNick Piggin  */
7324675ac39SAl Viro static int unlazy_walk(struct nameidata *nd)
73331e6b01fSNick Piggin {
73431e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
73531e6b01fSNick Piggin 
73631e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
737e5c832d5SLinus Torvalds 
738e5c832d5SLinus Torvalds 	nd->flags &= ~LOOKUP_RCU;
7397973387aSAl Viro 	if (unlikely(!legitimize_links(nd)))
7404675ac39SAl Viro 		goto out1;
74184a2bd39SAl Viro 	if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
74284a2bd39SAl Viro 		goto out;
743ee594bffSAl Viro 	if (unlikely(!legitimize_root(nd)))
7444675ac39SAl Viro 		goto out;
7454675ac39SAl Viro 	rcu_read_unlock();
7464675ac39SAl Viro 	BUG_ON(nd->inode != parent->d_inode);
7474675ac39SAl Viro 	return 0;
7484675ac39SAl Viro 
74984a2bd39SAl Viro out1:
7504675ac39SAl Viro 	nd->path.mnt = NULL;
7514675ac39SAl Viro 	nd->path.dentry = NULL;
7524675ac39SAl Viro out:
7534675ac39SAl Viro 	rcu_read_unlock();
7544675ac39SAl Viro 	return -ECHILD;
7554675ac39SAl Viro }
7564675ac39SAl Viro 
7574675ac39SAl Viro /**
7584675ac39SAl Viro  * unlazy_child - try to switch to ref-walk mode.
7594675ac39SAl Viro  * @nd: nameidata pathwalk data
7604675ac39SAl Viro  * @dentry: child of nd->path.dentry
7614675ac39SAl Viro  * @seq: seq number to check dentry against
7624675ac39SAl Viro  * Returns: 0 on success, -ECHILD on failure
7634675ac39SAl Viro  *
7644675ac39SAl Viro  * unlazy_child attempts to legitimize the current nd->path, nd->root and dentry
7654675ac39SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
7664675ac39SAl Viro  * @nd.  Must be called from rcu-walk context.
7674675ac39SAl Viro  * Nothing should touch nameidata between unlazy_child() failure and
7684675ac39SAl Viro  * terminate_walk().
7694675ac39SAl Viro  */
7704675ac39SAl Viro static int unlazy_child(struct nameidata *nd, struct dentry *dentry, unsigned seq)
7714675ac39SAl Viro {
7724675ac39SAl Viro 	BUG_ON(!(nd->flags & LOOKUP_RCU));
7734675ac39SAl Viro 
7744675ac39SAl Viro 	nd->flags &= ~LOOKUP_RCU;
7754675ac39SAl Viro 	if (unlikely(!legitimize_links(nd)))
7764675ac39SAl Viro 		goto out2;
7777973387aSAl Viro 	if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
7787973387aSAl Viro 		goto out2;
7794675ac39SAl Viro 	if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref)))
7807973387aSAl Viro 		goto out1;
78148a066e7SAl Viro 
78215570086SLinus Torvalds 	/*
7834675ac39SAl Viro 	 * We need to move both the parent and the dentry from the RCU domain
7844675ac39SAl Viro 	 * to be properly refcounted. And the sequence number in the dentry
7854675ac39SAl Viro 	 * validates *both* dentry counters, since we checked the sequence
7864675ac39SAl Viro 	 * number of the parent after we got the child sequence number. So we
7874675ac39SAl Viro 	 * know the parent must still be valid if the child sequence number is
78815570086SLinus Torvalds 	 */
7894675ac39SAl Viro 	if (unlikely(!lockref_get_not_dead(&dentry->d_lockref)))
790e5c832d5SLinus Torvalds 		goto out;
79184a2bd39SAl Viro 	if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
79284a2bd39SAl Viro 		goto out_dput;
793e5c832d5SLinus Torvalds 	/*
794e5c832d5SLinus Torvalds 	 * Sequence counts matched. Now make sure that the root is
795e5c832d5SLinus Torvalds 	 * still valid and get it if required.
796e5c832d5SLinus Torvalds 	 */
79784a2bd39SAl Viro 	if (unlikely(!legitimize_root(nd)))
79884a2bd39SAl Viro 		goto out_dput;
7998b61e74fSAl Viro 	rcu_read_unlock();
80031e6b01fSNick Piggin 	return 0;
80119660af7SAl Viro 
8027973387aSAl Viro out2:
8037973387aSAl Viro 	nd->path.mnt = NULL;
8047973387aSAl Viro out1:
8057973387aSAl Viro 	nd->path.dentry = NULL;
806e5c832d5SLinus Torvalds out:
8078b61e74fSAl Viro 	rcu_read_unlock();
80884a2bd39SAl Viro 	return -ECHILD;
80984a2bd39SAl Viro out_dput:
81084a2bd39SAl Viro 	rcu_read_unlock();
81184a2bd39SAl Viro 	dput(dentry);
81231e6b01fSNick Piggin 	return -ECHILD;
81331e6b01fSNick Piggin }
81431e6b01fSNick Piggin 
8154ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
81634286d66SNick Piggin {
817a89f8337SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
8184ce16ef3SAl Viro 		return dentry->d_op->d_revalidate(dentry, flags);
819a89f8337SAl Viro 	else
820a89f8337SAl Viro 		return 1;
82134286d66SNick Piggin }
82234286d66SNick Piggin 
8239f1fafeeSAl Viro /**
8249f1fafeeSAl Viro  * complete_walk - successful completion of path walk
8259f1fafeeSAl Viro  * @nd:  pointer nameidata
82639159de2SJeff Layton  *
8279f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
8289f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
8299f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
8309f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
8319f1fafeeSAl Viro  * need to drop nd->path.
83239159de2SJeff Layton  */
8339f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
83439159de2SJeff Layton {
83516c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
83639159de2SJeff Layton 	int status;
83739159de2SJeff Layton 
8389f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
839adb21d2bSAleksa Sarai 		/*
840adb21d2bSAleksa Sarai 		 * We don't want to zero nd->root for scoped-lookups or
841adb21d2bSAleksa Sarai 		 * externally-managed nd->root.
842adb21d2bSAleksa Sarai 		 */
843adb21d2bSAleksa Sarai 		if (!(nd->flags & (LOOKUP_ROOT | LOOKUP_IS_SCOPED)))
8449f1fafeeSAl Viro 			nd->root.mnt = NULL;
8454675ac39SAl Viro 		if (unlikely(unlazy_walk(nd)))
84648a066e7SAl Viro 			return -ECHILD;
84748a066e7SAl Viro 	}
8489f1fafeeSAl Viro 
849adb21d2bSAleksa Sarai 	if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
850adb21d2bSAleksa Sarai 		/*
851adb21d2bSAleksa Sarai 		 * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't
852adb21d2bSAleksa Sarai 		 * ever step outside the root during lookup" and should already
853adb21d2bSAleksa Sarai 		 * be guaranteed by the rest of namei, we want to avoid a namei
854adb21d2bSAleksa Sarai 		 * BUG resulting in userspace being given a path that was not
855adb21d2bSAleksa Sarai 		 * scoped within the root at some point during the lookup.
856adb21d2bSAleksa Sarai 		 *
857adb21d2bSAleksa Sarai 		 * So, do a final sanity-check to make sure that in the
858adb21d2bSAleksa Sarai 		 * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED)
859adb21d2bSAleksa Sarai 		 * we won't silently return an fd completely outside of the
860adb21d2bSAleksa Sarai 		 * requested root to userspace.
861adb21d2bSAleksa Sarai 		 *
862adb21d2bSAleksa Sarai 		 * Userspace could move the path outside the root after this
863adb21d2bSAleksa Sarai 		 * check, but as discussed elsewhere this is not a concern (the
864adb21d2bSAleksa Sarai 		 * resolved file was inside the root at some point).
865adb21d2bSAleksa Sarai 		 */
866adb21d2bSAleksa Sarai 		if (!path_is_under(&nd->path, &nd->root))
867adb21d2bSAleksa Sarai 			return -EXDEV;
868adb21d2bSAleksa Sarai 	}
869adb21d2bSAleksa Sarai 
87016c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
87139159de2SJeff Layton 		return 0;
87239159de2SJeff Layton 
873ecf3d1f1SJeff Layton 	if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
87416c2cd71SAl Viro 		return 0;
87516c2cd71SAl Viro 
876ecf3d1f1SJeff Layton 	status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
87739159de2SJeff Layton 	if (status > 0)
87839159de2SJeff Layton 		return 0;
87939159de2SJeff Layton 
88016c2cd71SAl Viro 	if (!status)
88139159de2SJeff Layton 		status = -ESTALE;
88216c2cd71SAl Viro 
88339159de2SJeff Layton 	return status;
88439159de2SJeff Layton }
88539159de2SJeff Layton 
886740a1678SAleksa Sarai static int set_root(struct nameidata *nd)
8872a737871SAl Viro {
88831e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
8899e6697e2SAl Viro 
890adb21d2bSAleksa Sarai 	/*
891adb21d2bSAleksa Sarai 	 * Jumping to the real root in a scoped-lookup is a BUG in namei, but we
892adb21d2bSAleksa Sarai 	 * still have to ensure it doesn't happen because it will cause a breakout
893adb21d2bSAleksa Sarai 	 * from the dirfd.
894adb21d2bSAleksa Sarai 	 */
895adb21d2bSAleksa Sarai 	if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED))
896adb21d2bSAleksa Sarai 		return -ENOTRECOVERABLE;
897adb21d2bSAleksa Sarai 
8989e6697e2SAl Viro 	if (nd->flags & LOOKUP_RCU) {
8998f47a016SAl Viro 		unsigned seq;
900c28cc364SNick Piggin 
901c28cc364SNick Piggin 		do {
902c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
90331e6b01fSNick Piggin 			nd->root = fs->root;
9048f47a016SAl Viro 			nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
905c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
9069e6697e2SAl Viro 	} else {
9079e6697e2SAl Viro 		get_fs_root(fs, &nd->root);
90884a2bd39SAl Viro 		nd->flags |= LOOKUP_ROOT_GRABBED;
9099e6697e2SAl Viro 	}
910740a1678SAleksa Sarai 	return 0;
91131e6b01fSNick Piggin }
91231e6b01fSNick Piggin 
913248fb5b9SAl Viro static int nd_jump_root(struct nameidata *nd)
914248fb5b9SAl Viro {
915adb21d2bSAleksa Sarai 	if (unlikely(nd->flags & LOOKUP_BENEATH))
916adb21d2bSAleksa Sarai 		return -EXDEV;
91772ba2929SAleksa Sarai 	if (unlikely(nd->flags & LOOKUP_NO_XDEV)) {
91872ba2929SAleksa Sarai 		/* Absolute path arguments to path_init() are allowed. */
91972ba2929SAleksa Sarai 		if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt)
92072ba2929SAleksa Sarai 			return -EXDEV;
92172ba2929SAleksa Sarai 	}
922740a1678SAleksa Sarai 	if (!nd->root.mnt) {
923740a1678SAleksa Sarai 		int error = set_root(nd);
924740a1678SAleksa Sarai 		if (error)
925740a1678SAleksa Sarai 			return error;
926740a1678SAleksa Sarai 	}
927248fb5b9SAl Viro 	if (nd->flags & LOOKUP_RCU) {
928248fb5b9SAl Viro 		struct dentry *d;
929248fb5b9SAl Viro 		nd->path = nd->root;
930248fb5b9SAl Viro 		d = nd->path.dentry;
931248fb5b9SAl Viro 		nd->inode = d->d_inode;
932248fb5b9SAl Viro 		nd->seq = nd->root_seq;
933248fb5b9SAl Viro 		if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
934248fb5b9SAl Viro 			return -ECHILD;
935248fb5b9SAl Viro 	} else {
936248fb5b9SAl Viro 		path_put(&nd->path);
937248fb5b9SAl Viro 		nd->path = nd->root;
938248fb5b9SAl Viro 		path_get(&nd->path);
939248fb5b9SAl Viro 		nd->inode = nd->path.dentry->d_inode;
940248fb5b9SAl Viro 	}
941248fb5b9SAl Viro 	nd->flags |= LOOKUP_JUMPED;
942248fb5b9SAl Viro 	return 0;
943248fb5b9SAl Viro }
944248fb5b9SAl Viro 
945b5fb63c1SChristoph Hellwig /*
9466b255391SAl Viro  * Helper to directly jump to a known parsed path from ->get_link,
947b5fb63c1SChristoph Hellwig  * caller must have taken a reference to path beforehand.
948b5fb63c1SChristoph Hellwig  */
9491bc82070SAleksa Sarai int nd_jump_link(struct path *path)
950b5fb63c1SChristoph Hellwig {
9514b99d499SAleksa Sarai 	int error = -ELOOP;
9526e77137bSAl Viro 	struct nameidata *nd = current->nameidata;
953b5fb63c1SChristoph Hellwig 
9544b99d499SAleksa Sarai 	if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS))
9554b99d499SAleksa Sarai 		goto err;
9564b99d499SAleksa Sarai 
95772ba2929SAleksa Sarai 	error = -EXDEV;
95872ba2929SAleksa Sarai 	if (unlikely(nd->flags & LOOKUP_NO_XDEV)) {
95972ba2929SAleksa Sarai 		if (nd->path.mnt != path->mnt)
96072ba2929SAleksa Sarai 			goto err;
96172ba2929SAleksa Sarai 	}
962adb21d2bSAleksa Sarai 	/* Not currently safe for scoped-lookups. */
963adb21d2bSAleksa Sarai 	if (unlikely(nd->flags & LOOKUP_IS_SCOPED))
964adb21d2bSAleksa Sarai 		goto err;
96572ba2929SAleksa Sarai 
9664b99d499SAleksa Sarai 	path_put(&nd->path);
967b5fb63c1SChristoph Hellwig 	nd->path = *path;
968b5fb63c1SChristoph Hellwig 	nd->inode = nd->path.dentry->d_inode;
969b5fb63c1SChristoph Hellwig 	nd->flags |= LOOKUP_JUMPED;
9701bc82070SAleksa Sarai 	return 0;
9714b99d499SAleksa Sarai 
9724b99d499SAleksa Sarai err:
9734b99d499SAleksa Sarai 	path_put(path);
9744b99d499SAleksa Sarai 	return error;
975b5fb63c1SChristoph Hellwig }
976b5fb63c1SChristoph Hellwig 
977b9ff4429SAl Viro static inline void put_link(struct nameidata *nd)
978574197e0SAl Viro {
97921c3003dSAl Viro 	struct saved *last = nd->stack + --nd->depth;
980fceef393SAl Viro 	do_delayed_call(&last->done);
9816548fae2SAl Viro 	if (!(nd->flags & LOOKUP_RCU))
982b9ff4429SAl Viro 		path_put(&last->link);
983574197e0SAl Viro }
984574197e0SAl Viro 
985561ec64aSLinus Torvalds int sysctl_protected_symlinks __read_mostly = 0;
986561ec64aSLinus Torvalds int sysctl_protected_hardlinks __read_mostly = 0;
98730aba665SSalvatore Mesoraca int sysctl_protected_fifos __read_mostly;
98830aba665SSalvatore Mesoraca int sysctl_protected_regular __read_mostly;
989800179c9SKees Cook 
990800179c9SKees Cook /**
991800179c9SKees Cook  * may_follow_link - Check symlink following for unsafe situations
99255852635SRandy Dunlap  * @nd: nameidata pathwalk data
993800179c9SKees Cook  *
994800179c9SKees Cook  * In the case of the sysctl_protected_symlinks sysctl being enabled,
995800179c9SKees Cook  * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
996800179c9SKees Cook  * in a sticky world-writable directory. This is to protect privileged
997800179c9SKees Cook  * processes from failing races against path names that may change out
998800179c9SKees Cook  * from under them by way of other users creating malicious symlinks.
999800179c9SKees Cook  * It will permit symlinks to be followed only when outside a sticky
1000800179c9SKees Cook  * world-writable directory, or when the uid of the symlink and follower
1001800179c9SKees Cook  * match, or when the directory owner matches the symlink's owner.
1002800179c9SKees Cook  *
1003800179c9SKees Cook  * Returns 0 if following the symlink is allowed, -ve on error.
1004800179c9SKees Cook  */
1005ad6cc4c3SAl Viro static inline int may_follow_link(struct nameidata *nd, const struct inode *inode)
1006800179c9SKees Cook {
1007*ba73d987SChristian Brauner 	struct user_namespace *mnt_userns;
1008*ba73d987SChristian Brauner 	kuid_t i_uid;
1009*ba73d987SChristian Brauner 
1010800179c9SKees Cook 	if (!sysctl_protected_symlinks)
1011800179c9SKees Cook 		return 0;
1012800179c9SKees Cook 
1013*ba73d987SChristian Brauner 	mnt_userns = mnt_user_ns(nd->path.mnt);
1014*ba73d987SChristian Brauner 	i_uid = i_uid_into_mnt(mnt_userns, inode);
1015800179c9SKees Cook 	/* Allowed if owner and follower match. */
1016*ba73d987SChristian Brauner 	if (uid_eq(current_cred()->fsuid, i_uid))
1017800179c9SKees Cook 		return 0;
1018800179c9SKees Cook 
1019800179c9SKees Cook 	/* Allowed if parent directory not sticky and world-writable. */
10200f705953SAl Viro 	if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
1021800179c9SKees Cook 		return 0;
1022800179c9SKees Cook 
1023800179c9SKees Cook 	/* Allowed if parent directory and link owner match. */
1024*ba73d987SChristian Brauner 	if (uid_valid(nd->dir_uid) && uid_eq(nd->dir_uid, i_uid))
1025800179c9SKees Cook 		return 0;
1026800179c9SKees Cook 
102731956502SAl Viro 	if (nd->flags & LOOKUP_RCU)
102831956502SAl Viro 		return -ECHILD;
102931956502SAl Viro 
1030ea841bafSRichard Guy Briggs 	audit_inode(nd->name, nd->stack[0].link.dentry, 0);
1031245d7369SKees Cook 	audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link");
1032800179c9SKees Cook 	return -EACCES;
1033800179c9SKees Cook }
1034800179c9SKees Cook 
1035800179c9SKees Cook /**
1036800179c9SKees Cook  * safe_hardlink_source - Check for safe hardlink conditions
1037*ba73d987SChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
1038800179c9SKees Cook  * @inode: the source inode to hardlink from
1039800179c9SKees Cook  *
1040800179c9SKees Cook  * Return false if at least one of the following conditions:
1041800179c9SKees Cook  *    - inode is not a regular file
1042800179c9SKees Cook  *    - inode is setuid
1043800179c9SKees Cook  *    - inode is setgid and group-exec
1044800179c9SKees Cook  *    - access failure for read and write
1045800179c9SKees Cook  *
1046800179c9SKees Cook  * Otherwise returns true.
1047800179c9SKees Cook  */
1048*ba73d987SChristian Brauner static bool safe_hardlink_source(struct user_namespace *mnt_userns,
1049*ba73d987SChristian Brauner 				 struct inode *inode)
1050800179c9SKees Cook {
1051800179c9SKees Cook 	umode_t mode = inode->i_mode;
1052800179c9SKees Cook 
1053800179c9SKees Cook 	/* Special files should not get pinned to the filesystem. */
1054800179c9SKees Cook 	if (!S_ISREG(mode))
1055800179c9SKees Cook 		return false;
1056800179c9SKees Cook 
1057800179c9SKees Cook 	/* Setuid files should not get pinned to the filesystem. */
1058800179c9SKees Cook 	if (mode & S_ISUID)
1059800179c9SKees Cook 		return false;
1060800179c9SKees Cook 
1061800179c9SKees Cook 	/* Executable setgid files should not get pinned to the filesystem. */
1062800179c9SKees Cook 	if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
1063800179c9SKees Cook 		return false;
1064800179c9SKees Cook 
1065800179c9SKees Cook 	/* Hardlinking to unreadable or unwritable sources is dangerous. */
1066*ba73d987SChristian Brauner 	if (inode_permission(mnt_userns, inode, MAY_READ | MAY_WRITE))
1067800179c9SKees Cook 		return false;
1068800179c9SKees Cook 
1069800179c9SKees Cook 	return true;
1070800179c9SKees Cook }
1071800179c9SKees Cook 
1072800179c9SKees Cook /**
1073800179c9SKees Cook  * may_linkat - Check permissions for creating a hardlink
1074*ba73d987SChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
1075800179c9SKees Cook  * @link: the source to hardlink from
1076800179c9SKees Cook  *
1077800179c9SKees Cook  * Block hardlink when all of:
1078800179c9SKees Cook  *  - sysctl_protected_hardlinks enabled
1079800179c9SKees Cook  *  - fsuid does not match inode
1080800179c9SKees Cook  *  - hardlink source is unsafe (see safe_hardlink_source() above)
1081f2ca3796SDirk Steinmetz  *  - not CAP_FOWNER in a namespace with the inode owner uid mapped
1082800179c9SKees Cook  *
1083*ba73d987SChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
1084*ba73d987SChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then take
1085*ba73d987SChristian Brauner  * care to map the inode according to @mnt_userns before checking permissions.
1086*ba73d987SChristian Brauner  * On non-idmapped mounts or if permission checking is to be performed on the
1087*ba73d987SChristian Brauner  * raw inode simply passs init_user_ns.
1088*ba73d987SChristian Brauner  *
1089800179c9SKees Cook  * Returns 0 if successful, -ve on error.
1090800179c9SKees Cook  */
1091*ba73d987SChristian Brauner int may_linkat(struct user_namespace *mnt_userns, struct path *link)
1092800179c9SKees Cook {
1093593d1ce8SEric W. Biederman 	struct inode *inode = link->dentry->d_inode;
1094593d1ce8SEric W. Biederman 
1095593d1ce8SEric W. Biederman 	/* Inode writeback is not safe when the uid or gid are invalid. */
1096*ba73d987SChristian Brauner 	if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) ||
1097*ba73d987SChristian Brauner 	    !gid_valid(i_gid_into_mnt(mnt_userns, inode)))
1098593d1ce8SEric W. Biederman 		return -EOVERFLOW;
1099800179c9SKees Cook 
1100800179c9SKees Cook 	if (!sysctl_protected_hardlinks)
1101800179c9SKees Cook 		return 0;
1102800179c9SKees Cook 
1103800179c9SKees Cook 	/* Source inode owner (or CAP_FOWNER) can hardlink all they like,
1104800179c9SKees Cook 	 * otherwise, it must be a safe source.
1105800179c9SKees Cook 	 */
1106*ba73d987SChristian Brauner 	if (safe_hardlink_source(mnt_userns, inode) ||
1107*ba73d987SChristian Brauner 	    inode_owner_or_capable(mnt_userns, inode))
1108800179c9SKees Cook 		return 0;
1109800179c9SKees Cook 
1110245d7369SKees Cook 	audit_log_path_denied(AUDIT_ANOM_LINK, "linkat");
1111800179c9SKees Cook 	return -EPERM;
1112800179c9SKees Cook }
1113800179c9SKees Cook 
111430aba665SSalvatore Mesoraca /**
111530aba665SSalvatore Mesoraca  * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
111630aba665SSalvatore Mesoraca  *			  should be allowed, or not, on files that already
111730aba665SSalvatore Mesoraca  *			  exist.
1118*ba73d987SChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
1119d0cb5018SAl Viro  * @dir_mode: mode bits of directory
1120d0cb5018SAl Viro  * @dir_uid: owner of directory
112130aba665SSalvatore Mesoraca  * @inode: the inode of the file to open
112230aba665SSalvatore Mesoraca  *
112330aba665SSalvatore Mesoraca  * Block an O_CREAT open of a FIFO (or a regular file) when:
112430aba665SSalvatore Mesoraca  *   - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
112530aba665SSalvatore Mesoraca  *   - the file already exists
112630aba665SSalvatore Mesoraca  *   - we are in a sticky directory
112730aba665SSalvatore Mesoraca  *   - we don't own the file
112830aba665SSalvatore Mesoraca  *   - the owner of the directory doesn't own the file
112930aba665SSalvatore Mesoraca  *   - the directory is world writable
113030aba665SSalvatore Mesoraca  * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
113130aba665SSalvatore Mesoraca  * the directory doesn't have to be world writable: being group writable will
113230aba665SSalvatore Mesoraca  * be enough.
113330aba665SSalvatore Mesoraca  *
1134*ba73d987SChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
1135*ba73d987SChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then take
1136*ba73d987SChristian Brauner  * care to map the inode according to @mnt_userns before checking permissions.
1137*ba73d987SChristian Brauner  * On non-idmapped mounts or if permission checking is to be performed on the
1138*ba73d987SChristian Brauner  * raw inode simply passs init_user_ns.
1139*ba73d987SChristian Brauner  *
114030aba665SSalvatore Mesoraca  * Returns 0 if the open is allowed, -ve on error.
114130aba665SSalvatore Mesoraca  */
1142*ba73d987SChristian Brauner static int may_create_in_sticky(struct user_namespace *mnt_userns,
1143*ba73d987SChristian Brauner 				struct nameidata *nd, struct inode *const inode)
114430aba665SSalvatore Mesoraca {
1145*ba73d987SChristian Brauner 	umode_t dir_mode = nd->dir_mode;
1146*ba73d987SChristian Brauner 	kuid_t dir_uid = nd->dir_uid;
1147*ba73d987SChristian Brauner 
114830aba665SSalvatore Mesoraca 	if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) ||
114930aba665SSalvatore Mesoraca 	    (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
1150d0cb5018SAl Viro 	    likely(!(dir_mode & S_ISVTX)) ||
1151*ba73d987SChristian Brauner 	    uid_eq(i_uid_into_mnt(mnt_userns, inode), dir_uid) ||
1152*ba73d987SChristian Brauner 	    uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)))
115330aba665SSalvatore Mesoraca 		return 0;
115430aba665SSalvatore Mesoraca 
1155d0cb5018SAl Viro 	if (likely(dir_mode & 0002) ||
1156d0cb5018SAl Viro 	    (dir_mode & 0020 &&
115730aba665SSalvatore Mesoraca 	     ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) ||
115830aba665SSalvatore Mesoraca 	      (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) {
1159245d7369SKees Cook 		const char *operation = S_ISFIFO(inode->i_mode) ?
1160245d7369SKees Cook 					"sticky_create_fifo" :
1161245d7369SKees Cook 					"sticky_create_regular";
1162245d7369SKees Cook 		audit_log_path_denied(AUDIT_ANOM_CREAT, operation);
116330aba665SSalvatore Mesoraca 		return -EACCES;
116430aba665SSalvatore Mesoraca 	}
116530aba665SSalvatore Mesoraca 	return 0;
116630aba665SSalvatore Mesoraca }
116730aba665SSalvatore Mesoraca 
1168f015f126SDavid Howells /*
1169f015f126SDavid Howells  * follow_up - Find the mountpoint of path's vfsmount
1170f015f126SDavid Howells  *
1171f015f126SDavid Howells  * Given a path, find the mountpoint of its source file system.
1172f015f126SDavid Howells  * Replace @path with the path of the mountpoint in the parent mount.
1173f015f126SDavid Howells  * Up is towards /.
1174f015f126SDavid Howells  *
1175f015f126SDavid Howells  * Return 1 if we went up a level and 0 if we were already at the
1176f015f126SDavid Howells  * root.
1177f015f126SDavid Howells  */
1178bab77ebfSAl Viro int follow_up(struct path *path)
11791da177e4SLinus Torvalds {
11800714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
11810714a533SAl Viro 	struct mount *parent;
11821da177e4SLinus Torvalds 	struct dentry *mountpoint;
118399b7db7bSNick Piggin 
118448a066e7SAl Viro 	read_seqlock_excl(&mount_lock);
11850714a533SAl Viro 	parent = mnt->mnt_parent;
11863c0a6163SAl Viro 	if (parent == mnt) {
118748a066e7SAl Viro 		read_sequnlock_excl(&mount_lock);
11881da177e4SLinus Torvalds 		return 0;
11891da177e4SLinus Torvalds 	}
11900714a533SAl Viro 	mntget(&parent->mnt);
1191a73324daSAl Viro 	mountpoint = dget(mnt->mnt_mountpoint);
119248a066e7SAl Viro 	read_sequnlock_excl(&mount_lock);
1193bab77ebfSAl Viro 	dput(path->dentry);
1194bab77ebfSAl Viro 	path->dentry = mountpoint;
1195bab77ebfSAl Viro 	mntput(path->mnt);
11960714a533SAl Viro 	path->mnt = &parent->mnt;
11971da177e4SLinus Torvalds 	return 1;
11981da177e4SLinus Torvalds }
11994d359507SAl Viro EXPORT_SYMBOL(follow_up);
12001da177e4SLinus Torvalds 
12017ef482faSAl Viro static bool choose_mountpoint_rcu(struct mount *m, const struct path *root,
12027ef482faSAl Viro 				  struct path *path, unsigned *seqp)
12037ef482faSAl Viro {
12047ef482faSAl Viro 	while (mnt_has_parent(m)) {
12057ef482faSAl Viro 		struct dentry *mountpoint = m->mnt_mountpoint;
12067ef482faSAl Viro 
12077ef482faSAl Viro 		m = m->mnt_parent;
12087ef482faSAl Viro 		if (unlikely(root->dentry == mountpoint &&
12097ef482faSAl Viro 			     root->mnt == &m->mnt))
12107ef482faSAl Viro 			break;
12117ef482faSAl Viro 		if (mountpoint != m->mnt.mnt_root) {
12127ef482faSAl Viro 			path->mnt = &m->mnt;
12137ef482faSAl Viro 			path->dentry = mountpoint;
12147ef482faSAl Viro 			*seqp = read_seqcount_begin(&mountpoint->d_seq);
12157ef482faSAl Viro 			return true;
12167ef482faSAl Viro 		}
12177ef482faSAl Viro 	}
12187ef482faSAl Viro 	return false;
12197ef482faSAl Viro }
12207ef482faSAl Viro 
12212aa38470SAl Viro static bool choose_mountpoint(struct mount *m, const struct path *root,
12222aa38470SAl Viro 			      struct path *path)
12232aa38470SAl Viro {
12242aa38470SAl Viro 	bool found;
12252aa38470SAl Viro 
12262aa38470SAl Viro 	rcu_read_lock();
12272aa38470SAl Viro 	while (1) {
12282aa38470SAl Viro 		unsigned seq, mseq = read_seqbegin(&mount_lock);
12292aa38470SAl Viro 
12302aa38470SAl Viro 		found = choose_mountpoint_rcu(m, root, path, &seq);
12312aa38470SAl Viro 		if (unlikely(!found)) {
12322aa38470SAl Viro 			if (!read_seqretry(&mount_lock, mseq))
12332aa38470SAl Viro 				break;
12342aa38470SAl Viro 		} else {
12352aa38470SAl Viro 			if (likely(__legitimize_path(path, seq, mseq)))
12362aa38470SAl Viro 				break;
12372aa38470SAl Viro 			rcu_read_unlock();
12382aa38470SAl Viro 			path_put(path);
12392aa38470SAl Viro 			rcu_read_lock();
12402aa38470SAl Viro 		}
12412aa38470SAl Viro 	}
12422aa38470SAl Viro 	rcu_read_unlock();
12432aa38470SAl Viro 	return found;
12442aa38470SAl Viro }
12452aa38470SAl Viro 
1246b5c84bf6SNick Piggin /*
12479875cf80SDavid Howells  * Perform an automount
12489875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
12499875cf80SDavid Howells  *   were called with.
12501da177e4SLinus Torvalds  */
12511c9f5e06SAl Viro static int follow_automount(struct path *path, int *count, unsigned lookup_flags)
125231e6b01fSNick Piggin {
125325e195aaSAl Viro 	struct dentry *dentry = path->dentry;
12549875cf80SDavid Howells 
12550ec26fd0SMiklos Szeredi 	/* We don't want to mount if someone's just doing a stat -
12560ec26fd0SMiklos Szeredi 	 * unless they're stat'ing a directory and appended a '/' to
12570ec26fd0SMiklos Szeredi 	 * the name.
12580ec26fd0SMiklos Szeredi 	 *
12590ec26fd0SMiklos Szeredi 	 * We do, however, want to mount if someone wants to open or
12605a30d8a2SDavid Howells 	 * create a file of any type under the mountpoint, wants to
12610ec26fd0SMiklos Szeredi 	 * traverse through the mountpoint or wants to open the
12620ec26fd0SMiklos Szeredi 	 * mounted directory.  Also, autofs may mark negative dentries
12630ec26fd0SMiklos Szeredi 	 * as being automount points.  These will need the attentions
12640ec26fd0SMiklos Szeredi 	 * of the daemon to instantiate them before they can be used.
12655a30d8a2SDavid Howells 	 */
12661c9f5e06SAl Viro 	if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
12675d38f049SIan Kent 			   LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
126825e195aaSAl Viro 	    dentry->d_inode)
12699875cf80SDavid Howells 		return -EISDIR;
12700ec26fd0SMiklos Szeredi 
12711c9f5e06SAl Viro 	if (count && (*count)++ >= MAXSYMLINKS)
12729875cf80SDavid Howells 		return -ELOOP;
12739875cf80SDavid Howells 
127425e195aaSAl Viro 	return finish_automount(dentry->d_op->d_automount(path), path);
1275ea5b778aSDavid Howells }
12769875cf80SDavid Howells 
12779875cf80SDavid Howells /*
12789deed3ebSAl Viro  * mount traversal - out-of-line part.  One note on ->d_flags accesses -
12799deed3ebSAl Viro  * dentries are pinned but not locked here, so negative dentry can go
12809deed3ebSAl Viro  * positive right under us.  Use of smp_load_acquire() provides a barrier
12819deed3ebSAl Viro  * sufficient for ->d_inode and ->d_flags consistency.
12829875cf80SDavid Howells  */
12839deed3ebSAl Viro static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped,
12849deed3ebSAl Viro 			     int *count, unsigned lookup_flags)
12859875cf80SDavid Howells {
12869deed3ebSAl Viro 	struct vfsmount *mnt = path->mnt;
12879875cf80SDavid Howells 	bool need_mntput = false;
12888aef1884SAl Viro 	int ret = 0;
12899875cf80SDavid Howells 
12909deed3ebSAl Viro 	while (flags & DCACHE_MANAGED_DENTRY) {
1291cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1292cc53ce53SDavid Howells 		 * being held. */
1293d41efb52SAl Viro 		if (flags & DCACHE_MANAGE_TRANSIT) {
1294fb5f51c7SIan Kent 			ret = path->dentry->d_op->d_manage(path, false);
1295508c8772SAl Viro 			flags = smp_load_acquire(&path->dentry->d_flags);
1296cc53ce53SDavid Howells 			if (ret < 0)
12978aef1884SAl Viro 				break;
1298cc53ce53SDavid Howells 		}
1299cc53ce53SDavid Howells 
13009deed3ebSAl Viro 		if (flags & DCACHE_MOUNTED) {	// something's mounted on it..
13019875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
13029deed3ebSAl Viro 			if (mounted) {		// ... in our namespace
13039875cf80SDavid Howells 				dput(path->dentry);
13049875cf80SDavid Howells 				if (need_mntput)
1305463ffb2eSAl Viro 					mntput(path->mnt);
1306463ffb2eSAl Viro 				path->mnt = mounted;
1307463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
13089deed3ebSAl Viro 				// here we know it's positive
13099deed3ebSAl Viro 				flags = path->dentry->d_flags;
13109875cf80SDavid Howells 				need_mntput = true;
13119875cf80SDavid Howells 				continue;
1312463ffb2eSAl Viro 			}
13131da177e4SLinus Torvalds 		}
13149875cf80SDavid Howells 
13159deed3ebSAl Viro 		if (!(flags & DCACHE_NEED_AUTOMOUNT))
13169deed3ebSAl Viro 			break;
13179deed3ebSAl Viro 
13189deed3ebSAl Viro 		// uncovered automount point
13199deed3ebSAl Viro 		ret = follow_automount(path, count, lookup_flags);
13209deed3ebSAl Viro 		flags = smp_load_acquire(&path->dentry->d_flags);
13219875cf80SDavid Howells 		if (ret < 0)
13228aef1884SAl Viro 			break;
13239875cf80SDavid Howells 	}
13249875cf80SDavid Howells 
13259deed3ebSAl Viro 	if (ret == -EISDIR)
13269deed3ebSAl Viro 		ret = 0;
13279deed3ebSAl Viro 	// possible if you race with several mount --move
13289deed3ebSAl Viro 	if (need_mntput && path->mnt == mnt)
13298aef1884SAl Viro 		mntput(path->mnt);
13309deed3ebSAl Viro 	if (!ret && unlikely(d_flags_negative(flags)))
1331d41efb52SAl Viro 		ret = -ENOENT;
13329deed3ebSAl Viro 	*jumped = need_mntput;
13338402752eSAl Viro 	return ret;
13341da177e4SLinus Torvalds }
13351da177e4SLinus Torvalds 
13369deed3ebSAl Viro static inline int traverse_mounts(struct path *path, bool *jumped,
13379deed3ebSAl Viro 				  int *count, unsigned lookup_flags)
13389deed3ebSAl Viro {
13399deed3ebSAl Viro 	unsigned flags = smp_load_acquire(&path->dentry->d_flags);
13409deed3ebSAl Viro 
13419deed3ebSAl Viro 	/* fastpath */
13429deed3ebSAl Viro 	if (likely(!(flags & DCACHE_MANAGED_DENTRY))) {
13439deed3ebSAl Viro 		*jumped = false;
13449deed3ebSAl Viro 		if (unlikely(d_flags_negative(flags)))
13459deed3ebSAl Viro 			return -ENOENT;
13469deed3ebSAl Viro 		return 0;
13479deed3ebSAl Viro 	}
13489deed3ebSAl Viro 	return __traverse_mounts(path, flags, jumped, count, lookup_flags);
13499deed3ebSAl Viro }
13509deed3ebSAl Viro 
1351cc53ce53SDavid Howells int follow_down_one(struct path *path)
13521da177e4SLinus Torvalds {
13531da177e4SLinus Torvalds 	struct vfsmount *mounted;
13541da177e4SLinus Torvalds 
13551c755af4SAl Viro 	mounted = lookup_mnt(path);
13561da177e4SLinus Torvalds 	if (mounted) {
13579393bd07SAl Viro 		dput(path->dentry);
13589393bd07SAl Viro 		mntput(path->mnt);
13599393bd07SAl Viro 		path->mnt = mounted;
13609393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
13611da177e4SLinus Torvalds 		return 1;
13621da177e4SLinus Torvalds 	}
13631da177e4SLinus Torvalds 	return 0;
13641da177e4SLinus Torvalds }
13654d359507SAl Viro EXPORT_SYMBOL(follow_down_one);
13661da177e4SLinus Torvalds 
13679875cf80SDavid Howells /*
13689deed3ebSAl Viro  * Follow down to the covering mount currently visible to userspace.  At each
13699deed3ebSAl Viro  * point, the filesystem owning that dentry may be queried as to whether the
13709deed3ebSAl Viro  * caller is permitted to proceed or not.
13719deed3ebSAl Viro  */
13729deed3ebSAl Viro int follow_down(struct path *path)
13739deed3ebSAl Viro {
13749deed3ebSAl Viro 	struct vfsmount *mnt = path->mnt;
13759deed3ebSAl Viro 	bool jumped;
13769deed3ebSAl Viro 	int ret = traverse_mounts(path, &jumped, NULL, 0);
13779deed3ebSAl Viro 
13789deed3ebSAl Viro 	if (path->mnt != mnt)
13799deed3ebSAl Viro 		mntput(mnt);
13809deed3ebSAl Viro 	return ret;
13819deed3ebSAl Viro }
13829deed3ebSAl Viro EXPORT_SYMBOL(follow_down);
13839deed3ebSAl Viro 
13849deed3ebSAl Viro /*
1385287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
1386287548e4SAl Viro  * we meet a managed dentry that would need blocking.
13879875cf80SDavid Howells  */
13889875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1389254cf582SAl Viro 			       struct inode **inode, unsigned *seqp)
13909875cf80SDavid Howells {
1391ea936aebSAl Viro 	struct dentry *dentry = path->dentry;
1392ea936aebSAl Viro 	unsigned int flags = dentry->d_flags;
1393ea936aebSAl Viro 
1394ea936aebSAl Viro 	if (likely(!(flags & DCACHE_MANAGED_DENTRY)))
1395ea936aebSAl Viro 		return true;
1396ea936aebSAl Viro 
1397ea936aebSAl Viro 	if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1398ea936aebSAl Viro 		return false;
1399ea936aebSAl Viro 
140062a7375eSIan Kent 	for (;;) {
140162a7375eSIan Kent 		/*
140262a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
140362a7375eSIan Kent 		 * that wants to block transit.
140462a7375eSIan Kent 		 */
1405ea936aebSAl Viro 		if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) {
1406ea936aebSAl Viro 			int res = dentry->d_op->d_manage(path, true);
1407ea936aebSAl Viro 			if (res)
1408ea936aebSAl Viro 				return res == -EISDIR;
1409ea936aebSAl Viro 			flags = dentry->d_flags;
1410b8faf035SNeilBrown 		}
141162a7375eSIan Kent 
1412ea936aebSAl Viro 		if (flags & DCACHE_MOUNTED) {
1413ea936aebSAl Viro 			struct mount *mounted = __lookup_mnt(path->mnt, dentry);
1414ea936aebSAl Viro 			if (mounted) {
1415c7105365SAl Viro 				path->mnt = &mounted->mnt;
1416ea936aebSAl Viro 				dentry = path->dentry = mounted->mnt.mnt_root;
1417a3fbbde7SAl Viro 				nd->flags |= LOOKUP_JUMPED;
1418ea936aebSAl Viro 				*seqp = read_seqcount_begin(&dentry->d_seq);
1419ea936aebSAl Viro 				*inode = dentry->d_inode;
142059430262SLinus Torvalds 				/*
1421ea936aebSAl Viro 				 * We don't need to re-check ->d_seq after this
1422ea936aebSAl Viro 				 * ->d_inode read - there will be an RCU delay
1423ea936aebSAl Viro 				 * between mount hash removal and ->mnt_root
1424ea936aebSAl Viro 				 * becoming unpinned.
142559430262SLinus Torvalds 				 */
1426ea936aebSAl Viro 				flags = dentry->d_flags;
1427ea936aebSAl Viro 				continue;
14289875cf80SDavid Howells 			}
1429ea936aebSAl Viro 			if (read_seqretry(&mount_lock, nd->m_seq))
1430ea936aebSAl Viro 				return false;
1431ea936aebSAl Viro 		}
1432ea936aebSAl Viro 		return !(flags & DCACHE_NEED_AUTOMOUNT);
1433ea936aebSAl Viro 	}
1434287548e4SAl Viro }
1435287548e4SAl Viro 
1436db3c9adeSAl Viro static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry,
1437db3c9adeSAl Viro 			  struct path *path, struct inode **inode,
1438db3c9adeSAl Viro 			  unsigned int *seqp)
1439bd7c4b50SAl Viro {
14409deed3ebSAl Viro 	bool jumped;
1441db3c9adeSAl Viro 	int ret;
1442bd7c4b50SAl Viro 
1443db3c9adeSAl Viro 	path->mnt = nd->path.mnt;
1444db3c9adeSAl Viro 	path->dentry = dentry;
1445c153007bSAl Viro 	if (nd->flags & LOOKUP_RCU) {
1446c153007bSAl Viro 		unsigned int seq = *seqp;
1447c153007bSAl Viro 		if (unlikely(!*inode))
1448c153007bSAl Viro 			return -ENOENT;
1449c153007bSAl Viro 		if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
14509deed3ebSAl Viro 			return 0;
1451c153007bSAl Viro 		if (unlazy_child(nd, dentry, seq))
1452c153007bSAl Viro 			return -ECHILD;
1453c153007bSAl Viro 		// *path might've been clobbered by __follow_mount_rcu()
1454c153007bSAl Viro 		path->mnt = nd->path.mnt;
1455c153007bSAl Viro 		path->dentry = dentry;
1456c153007bSAl Viro 	}
14579deed3ebSAl Viro 	ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags);
14589deed3ebSAl Viro 	if (jumped) {
14599deed3ebSAl Viro 		if (unlikely(nd->flags & LOOKUP_NO_XDEV))
14609deed3ebSAl Viro 			ret = -EXDEV;
14619deed3ebSAl Viro 		else
14629deed3ebSAl Viro 			nd->flags |= LOOKUP_JUMPED;
14639deed3ebSAl Viro 	}
14649deed3ebSAl Viro 	if (unlikely(ret)) {
14659deed3ebSAl Viro 		dput(path->dentry);
14669deed3ebSAl Viro 		if (path->mnt != nd->path.mnt)
14679deed3ebSAl Viro 			mntput(path->mnt);
14689deed3ebSAl Viro 	} else {
1469bd7c4b50SAl Viro 		*inode = d_backing_inode(path->dentry);
1470bd7c4b50SAl Viro 		*seqp = 0; /* out of RCU mode, so the value doesn't matter */
1471bd7c4b50SAl Viro 	}
1472bd7c4b50SAl Viro 	return ret;
1473bd7c4b50SAl Viro }
1474bd7c4b50SAl Viro 
14759875cf80SDavid Howells /*
1476f4fdace9SOleg Drokin  * This looks up the name in dcache and possibly revalidates the found dentry.
1477f4fdace9SOleg Drokin  * NULL is returned if the dentry does not exist in the cache.
1478baa03890SNick Piggin  */
1479e3c13928SAl Viro static struct dentry *lookup_dcache(const struct qstr *name,
1480e3c13928SAl Viro 				    struct dentry *dir,
14816c51e513SAl Viro 				    unsigned int flags)
1482baa03890SNick Piggin {
1483a89f8337SAl Viro 	struct dentry *dentry = d_lookup(dir, name);
1484bad61189SMiklos Szeredi 	if (dentry) {
1485a89f8337SAl Viro 		int error = d_revalidate(dentry, flags);
1486bad61189SMiklos Szeredi 		if (unlikely(error <= 0)) {
148774ff0ffcSAl Viro 			if (!error)
14885542aa2fSEric W. Biederman 				d_invalidate(dentry);
1489bad61189SMiklos Szeredi 			dput(dentry);
149074ff0ffcSAl Viro 			return ERR_PTR(error);
1491bad61189SMiklos Szeredi 		}
1492bad61189SMiklos Szeredi 	}
1493baa03890SNick Piggin 	return dentry;
1494baa03890SNick Piggin }
1495baa03890SNick Piggin 
1496baa03890SNick Piggin /*
1497a03ece5fSAl Viro  * Parent directory has inode locked exclusive.  This is one
1498a03ece5fSAl Viro  * and only case when ->lookup() gets called on non in-lookup
1499a03ece5fSAl Viro  * dentries - as the matter of fact, this only gets called
1500a03ece5fSAl Viro  * when directory is guaranteed to have no in-lookup children
1501a03ece5fSAl Viro  * at all.
150244396f4bSJosef Bacik  */
1503a03ece5fSAl Viro static struct dentry *__lookup_hash(const struct qstr *name,
1504a03ece5fSAl Viro 		struct dentry *base, unsigned int flags)
150544396f4bSJosef Bacik {
1506a03ece5fSAl Viro 	struct dentry *dentry = lookup_dcache(name, base, flags);
150744396f4bSJosef Bacik 	struct dentry *old;
1508a03ece5fSAl Viro 	struct inode *dir = base->d_inode;
1509a03ece5fSAl Viro 
1510a03ece5fSAl Viro 	if (dentry)
1511a03ece5fSAl Viro 		return dentry;
151244396f4bSJosef Bacik 
151344396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
1514a03ece5fSAl Viro 	if (unlikely(IS_DEADDIR(dir)))
151544396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
1516a03ece5fSAl Viro 
1517a03ece5fSAl Viro 	dentry = d_alloc(base, name);
1518a03ece5fSAl Viro 	if (unlikely(!dentry))
1519a03ece5fSAl Viro 		return ERR_PTR(-ENOMEM);
152044396f4bSJosef Bacik 
152172bd866aSAl Viro 	old = dir->i_op->lookup(dir, dentry, flags);
152244396f4bSJosef Bacik 	if (unlikely(old)) {
152344396f4bSJosef Bacik 		dput(dentry);
152444396f4bSJosef Bacik 		dentry = old;
152544396f4bSJosef Bacik 	}
152644396f4bSJosef Bacik 	return dentry;
152744396f4bSJosef Bacik }
152844396f4bSJosef Bacik 
152920e34357SAl Viro static struct dentry *lookup_fast(struct nameidata *nd,
153020e34357SAl Viro 				  struct inode **inode,
1531254cf582SAl Viro 			          unsigned *seqp)
15321da177e4SLinus Torvalds {
153331e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
15345a18fff2SAl Viro 	int status = 1;
15359875cf80SDavid Howells 
15363cac260aSAl Viro 	/*
1537b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
15385d0f49c1SAl Viro 	 * of a false negative due to a concurrent rename, the caller is
15395d0f49c1SAl Viro 	 * going to fall back to non-racy lookup.
1540b04f784eSNick Piggin 	 */
154131e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
154231e6b01fSNick Piggin 		unsigned seq;
1543da53be12SLinus Torvalds 		dentry = __d_lookup_rcu(parent, &nd->last, &seq);
15445d0f49c1SAl Viro 		if (unlikely(!dentry)) {
15454675ac39SAl Viro 			if (unlazy_walk(nd))
154620e34357SAl Viro 				return ERR_PTR(-ECHILD);
154720e34357SAl Viro 			return NULL;
15485d0f49c1SAl Viro 		}
15495a18fff2SAl Viro 
155012f8ad4bSLinus Torvalds 		/*
155112f8ad4bSLinus Torvalds 		 * This sequence count validates that the inode matches
155212f8ad4bSLinus Torvalds 		 * the dentry name information from lookup.
155312f8ad4bSLinus Torvalds 		 */
155463afdfc7SDavid Howells 		*inode = d_backing_inode(dentry);
15555d0f49c1SAl Viro 		if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
155620e34357SAl Viro 			return ERR_PTR(-ECHILD);
155712f8ad4bSLinus Torvalds 
155812f8ad4bSLinus Torvalds 		/*
155912f8ad4bSLinus Torvalds 		 * This sequence count validates that the parent had no
156012f8ad4bSLinus Torvalds 		 * changes while we did the lookup of the dentry above.
156112f8ad4bSLinus Torvalds 		 *
156212f8ad4bSLinus Torvalds 		 * The memory barrier in read_seqcount_begin of child is
156312f8ad4bSLinus Torvalds 		 *  enough, we can use __read_seqcount_retry here.
156412f8ad4bSLinus Torvalds 		 */
15655d0f49c1SAl Viro 		if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
156620e34357SAl Viro 			return ERR_PTR(-ECHILD);
15675a18fff2SAl Viro 
1568254cf582SAl Viro 		*seqp = seq;
15694ce16ef3SAl Viro 		status = d_revalidate(dentry, nd->flags);
1570c153007bSAl Viro 		if (likely(status > 0))
157120e34357SAl Viro 			return dentry;
15724675ac39SAl Viro 		if (unlazy_child(nd, dentry, seq))
157320e34357SAl Viro 			return ERR_PTR(-ECHILD);
1574209a7fb2SAl Viro 		if (unlikely(status == -ECHILD))
1575209a7fb2SAl Viro 			/* we'd been told to redo it in non-rcu mode */
1576209a7fb2SAl Viro 			status = d_revalidate(dentry, nd->flags);
15775a18fff2SAl Viro 	} else {
1578e97cdc87SAl Viro 		dentry = __d_lookup(parent, &nd->last);
157981e6f520SAl Viro 		if (unlikely(!dentry))
158020e34357SAl Viro 			return NULL;
15814ce16ef3SAl Viro 		status = d_revalidate(dentry, nd->flags);
15825d0f49c1SAl Viro 	}
15835a18fff2SAl Viro 	if (unlikely(status <= 0)) {
1584e9742b53SAl Viro 		if (!status)
15855d0f49c1SAl Viro 			d_invalidate(dentry);
15865a18fff2SAl Viro 		dput(dentry);
158720e34357SAl Viro 		return ERR_PTR(status);
15885a18fff2SAl Viro 	}
158920e34357SAl Viro 	return dentry;
1590697f514dSMiklos Szeredi }
1591697f514dSMiklos Szeredi 
1592697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */
159388d8331aSAl Viro static struct dentry *__lookup_slow(const struct qstr *name,
1594e3c13928SAl Viro 				    struct dentry *dir,
1595e3c13928SAl Viro 				    unsigned int flags)
1596697f514dSMiklos Szeredi {
159788d8331aSAl Viro 	struct dentry *dentry, *old;
15981936386eSAl Viro 	struct inode *inode = dir->d_inode;
1599d9171b93SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
16001936386eSAl Viro 
16011936386eSAl Viro 	/* Don't go there if it's already dead */
160294bdd655SAl Viro 	if (unlikely(IS_DEADDIR(inode)))
160388d8331aSAl Viro 		return ERR_PTR(-ENOENT);
160494bdd655SAl Viro again:
1605d9171b93SAl Viro 	dentry = d_alloc_parallel(dir, name, &wq);
160694bdd655SAl Viro 	if (IS_ERR(dentry))
160788d8331aSAl Viro 		return dentry;
160894bdd655SAl Viro 	if (unlikely(!d_in_lookup(dentry))) {
1609949a852eSAl Viro 		int error = d_revalidate(dentry, flags);
1610949a852eSAl Viro 		if (unlikely(error <= 0)) {
161194bdd655SAl Viro 			if (!error) {
1612949a852eSAl Viro 				d_invalidate(dentry);
1613949a852eSAl Viro 				dput(dentry);
161494bdd655SAl Viro 				goto again;
161594bdd655SAl Viro 			}
161694bdd655SAl Viro 			dput(dentry);
1617949a852eSAl Viro 			dentry = ERR_PTR(error);
1618949a852eSAl Viro 		}
161994bdd655SAl Viro 	} else {
16201936386eSAl Viro 		old = inode->i_op->lookup(inode, dentry, flags);
162185c7f810SAl Viro 		d_lookup_done(dentry);
16221936386eSAl Viro 		if (unlikely(old)) {
16231936386eSAl Viro 			dput(dentry);
16241936386eSAl Viro 			dentry = old;
1625949a852eSAl Viro 		}
1626949a852eSAl Viro 	}
1627e3c13928SAl Viro 	return dentry;
16281da177e4SLinus Torvalds }
16291da177e4SLinus Torvalds 
163088d8331aSAl Viro static struct dentry *lookup_slow(const struct qstr *name,
163188d8331aSAl Viro 				  struct dentry *dir,
163288d8331aSAl Viro 				  unsigned int flags)
163388d8331aSAl Viro {
163488d8331aSAl Viro 	struct inode *inode = dir->d_inode;
163588d8331aSAl Viro 	struct dentry *res;
163688d8331aSAl Viro 	inode_lock_shared(inode);
163788d8331aSAl Viro 	res = __lookup_slow(name, dir, flags);
163888d8331aSAl Viro 	inode_unlock_shared(inode);
163988d8331aSAl Viro 	return res;
164088d8331aSAl Viro }
164188d8331aSAl Viro 
1642*ba73d987SChristian Brauner static inline int may_lookup(struct user_namespace *mnt_userns,
1643*ba73d987SChristian Brauner 			     struct nameidata *nd)
164452094c8aSAl Viro {
164552094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
1646*ba73d987SChristian Brauner 		int err = inode_permission(mnt_userns, nd->inode,
164747291baaSChristian Brauner 					   MAY_EXEC | MAY_NOT_BLOCK);
164852094c8aSAl Viro 		if (err != -ECHILD)
164952094c8aSAl Viro 			return err;
16504675ac39SAl Viro 		if (unlazy_walk(nd))
165152094c8aSAl Viro 			return -ECHILD;
165252094c8aSAl Viro 	}
1653*ba73d987SChristian Brauner 	return inode_permission(mnt_userns, nd->inode, MAY_EXEC);
165452094c8aSAl Viro }
165552094c8aSAl Viro 
165649055906SAl Viro static int reserve_stack(struct nameidata *nd, struct path *link, unsigned seq)
1657d63ff28fSAl Viro {
165849055906SAl Viro 	if (unlikely(nd->total_link_count++ >= MAXSYMLINKS))
165949055906SAl Viro 		return -ELOOP;
16604542576bSAl Viro 
16614542576bSAl Viro 	if (likely(nd->depth != EMBEDDED_LEVELS))
16624542576bSAl Viro 		return 0;
16634542576bSAl Viro 	if (likely(nd->stack != nd->internal))
16644542576bSAl Viro 		return 0;
166560ef60c7SAl Viro 	if (likely(nd_alloc_stack(nd)))
166649055906SAl Viro 		return 0;
166760ef60c7SAl Viro 
166860ef60c7SAl Viro 	if (nd->flags & LOOKUP_RCU) {
166960ef60c7SAl Viro 		// we need to grab link before we do unlazy.  And we can't skip
167060ef60c7SAl Viro 		// unlazy even if we fail to grab the link - cleanup needs it
1671aef9404dSAl Viro 		bool grabbed_link = legitimize_path(nd, link, seq);
167260ef60c7SAl Viro 
167360ef60c7SAl Viro 		if (unlazy_walk(nd) != 0 || !grabbed_link)
167460ef60c7SAl Viro 			return -ECHILD;
167560ef60c7SAl Viro 
167660ef60c7SAl Viro 		if (nd_alloc_stack(nd))
167760ef60c7SAl Viro 			return 0;
1678bc40aee0SAl Viro 	}
167960ef60c7SAl Viro 	return -ENOMEM;
168049055906SAl Viro }
168149055906SAl Viro 
168249055906SAl Viro enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4};
168349055906SAl Viro 
168449055906SAl Viro static const char *pick_link(struct nameidata *nd, struct path *link,
168549055906SAl Viro 		     struct inode *inode, unsigned seq, int flags)
168649055906SAl Viro {
168749055906SAl Viro 	struct saved *last;
168849055906SAl Viro 	const char *res;
168949055906SAl Viro 	int error = reserve_stack(nd, link, seq);
169049055906SAl Viro 
169149055906SAl Viro 	if (unlikely(error)) {
169249055906SAl Viro 		if (!(nd->flags & LOOKUP_RCU))
1693cd179f44SAl Viro 			path_put(link);
169406708adbSAl Viro 		return ERR_PTR(error);
1695626de996SAl Viro 	}
1696ab104923SAl Viro 	last = nd->stack + nd->depth++;
16971cf2665bSAl Viro 	last->link = *link;
1698fceef393SAl Viro 	clear_delayed_call(&last->done);
16990450b2d1SAl Viro 	last->seq = seq;
1700ad6cc4c3SAl Viro 
1701b1a81972SAl Viro 	if (flags & WALK_TRAILING) {
1702ad6cc4c3SAl Viro 		error = may_follow_link(nd, inode);
1703ad6cc4c3SAl Viro 		if (unlikely(error))
1704ad6cc4c3SAl Viro 			return ERR_PTR(error);
1705ad6cc4c3SAl Viro 	}
1706ad6cc4c3SAl Viro 
1707dab741e0SMattias Nissler 	if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) ||
1708dab741e0SMattias Nissler 			unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW))
1709ad6cc4c3SAl Viro 		return ERR_PTR(-ELOOP);
1710ad6cc4c3SAl Viro 
1711ad6cc4c3SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1712ad6cc4c3SAl Viro 		touch_atime(&last->link);
1713ad6cc4c3SAl Viro 		cond_resched();
1714ad6cc4c3SAl Viro 	} else if (atime_needs_update(&last->link, inode)) {
1715ad6cc4c3SAl Viro 		if (unlikely(unlazy_walk(nd)))
1716ad6cc4c3SAl Viro 			return ERR_PTR(-ECHILD);
1717ad6cc4c3SAl Viro 		touch_atime(&last->link);
1718ad6cc4c3SAl Viro 	}
1719ad6cc4c3SAl Viro 
1720ad6cc4c3SAl Viro 	error = security_inode_follow_link(link->dentry, inode,
1721ad6cc4c3SAl Viro 					   nd->flags & LOOKUP_RCU);
1722ad6cc4c3SAl Viro 	if (unlikely(error))
1723ad6cc4c3SAl Viro 		return ERR_PTR(error);
1724ad6cc4c3SAl Viro 
1725ad6cc4c3SAl Viro 	res = READ_ONCE(inode->i_link);
1726ad6cc4c3SAl Viro 	if (!res) {
1727ad6cc4c3SAl Viro 		const char * (*get)(struct dentry *, struct inode *,
1728ad6cc4c3SAl Viro 				struct delayed_call *);
1729ad6cc4c3SAl Viro 		get = inode->i_op->get_link;
1730ad6cc4c3SAl Viro 		if (nd->flags & LOOKUP_RCU) {
1731ad6cc4c3SAl Viro 			res = get(NULL, inode, &last->done);
1732ad6cc4c3SAl Viro 			if (res == ERR_PTR(-ECHILD)) {
1733ad6cc4c3SAl Viro 				if (unlikely(unlazy_walk(nd)))
1734ad6cc4c3SAl Viro 					return ERR_PTR(-ECHILD);
1735ad6cc4c3SAl Viro 				res = get(link->dentry, inode, &last->done);
1736ad6cc4c3SAl Viro 			}
1737ad6cc4c3SAl Viro 		} else {
1738ad6cc4c3SAl Viro 			res = get(link->dentry, inode, &last->done);
1739ad6cc4c3SAl Viro 		}
1740ad6cc4c3SAl Viro 		if (!res)
1741ad6cc4c3SAl Viro 			goto all_done;
1742ad6cc4c3SAl Viro 		if (IS_ERR(res))
1743ad6cc4c3SAl Viro 			return res;
1744ad6cc4c3SAl Viro 	}
1745ad6cc4c3SAl Viro 	if (*res == '/') {
1746ad6cc4c3SAl Viro 		error = nd_jump_root(nd);
1747ad6cc4c3SAl Viro 		if (unlikely(error))
1748ad6cc4c3SAl Viro 			return ERR_PTR(error);
1749ad6cc4c3SAl Viro 		while (unlikely(*++res == '/'))
1750ad6cc4c3SAl Viro 			;
1751ad6cc4c3SAl Viro 	}
1752ad6cc4c3SAl Viro 	if (*res)
1753ad6cc4c3SAl Viro 		return res;
1754ad6cc4c3SAl Viro all_done: // pure jump
1755ad6cc4c3SAl Viro 	put_link(nd);
1756ad6cc4c3SAl Viro 	return NULL;
1757d63ff28fSAl Viro }
1758d63ff28fSAl Viro 
17593ddcd056SLinus Torvalds /*
17603ddcd056SLinus Torvalds  * Do we need to follow links? We _really_ want to be able
17613ddcd056SLinus Torvalds  * to do this check without having to look at inode->i_op,
17623ddcd056SLinus Torvalds  * so we keep a cache of "no, this doesn't need follow_link"
17633ddcd056SLinus Torvalds  * for the common case.
17643ddcd056SLinus Torvalds  */
1765b0417d2cSAl Viro static const char *step_into(struct nameidata *nd, int flags,
1766cbae4d12SAl Viro 		     struct dentry *dentry, struct inode *inode, unsigned seq)
17673ddcd056SLinus Torvalds {
1768cbae4d12SAl Viro 	struct path path;
1769cbae4d12SAl Viro 	int err = handle_mounts(nd, dentry, &path, &inode, &seq);
1770cbae4d12SAl Viro 
1771cbae4d12SAl Viro 	if (err < 0)
1772b0417d2cSAl Viro 		return ERR_PTR(err);
1773cbae4d12SAl Viro 	if (likely(!d_is_symlink(path.dentry)) ||
17748c4efe22SAl Viro 	   ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) ||
1775aca2903eSAl Viro 	   (flags & WALK_NOFOLLOW)) {
17768f64fb1cSAl Viro 		/* not a symlink or should not follow */
1777c99687a0SAl Viro 		if (!(nd->flags & LOOKUP_RCU)) {
1778c99687a0SAl Viro 			dput(nd->path.dentry);
1779c99687a0SAl Viro 			if (nd->path.mnt != path.mnt)
1780c99687a0SAl Viro 				mntput(nd->path.mnt);
1781c99687a0SAl Viro 		}
1782c99687a0SAl Viro 		nd->path = path;
17838f64fb1cSAl Viro 		nd->inode = inode;
17848f64fb1cSAl Viro 		nd->seq = seq;
1785b0417d2cSAl Viro 		return NULL;
17868f64fb1cSAl Viro 	}
1787a7f77542SAl Viro 	if (nd->flags & LOOKUP_RCU) {
178884f0cd9eSAl Viro 		/* make sure that d_is_symlink above matches inode */
1789cbae4d12SAl Viro 		if (read_seqcount_retry(&path.dentry->d_seq, seq))
1790b0417d2cSAl Viro 			return ERR_PTR(-ECHILD);
179184f0cd9eSAl Viro 	} else {
179284f0cd9eSAl Viro 		if (path.mnt == nd->path.mnt)
179384f0cd9eSAl Viro 			mntget(path.mnt);
1794a7f77542SAl Viro 	}
1795b1a81972SAl Viro 	return pick_link(nd, &path, inode, seq, flags);
17963ddcd056SLinus Torvalds }
17973ddcd056SLinus Torvalds 
1798c2df1968SAl Viro static struct dentry *follow_dotdot_rcu(struct nameidata *nd,
1799c2df1968SAl Viro 					struct inode **inodep,
1800c2df1968SAl Viro 					unsigned *seqp)
1801957dd41dSAl Viro {
180212487f30SAl Viro 	struct dentry *parent, *old;
1803957dd41dSAl Viro 
180412487f30SAl Viro 	if (path_equal(&nd->path, &nd->root))
180512487f30SAl Viro 		goto in_root;
180612487f30SAl Viro 	if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
18077ef482faSAl Viro 		struct path path;
1808efe772d6SAl Viro 		unsigned seq;
18097ef482faSAl Viro 		if (!choose_mountpoint_rcu(real_mount(nd->path.mnt),
18107ef482faSAl Viro 					   &nd->root, &path, &seq))
181112487f30SAl Viro 			goto in_root;
1812efe772d6SAl Viro 		if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1813efe772d6SAl Viro 			return ERR_PTR(-ECHILD);
1814efe772d6SAl Viro 		nd->path = path;
1815efe772d6SAl Viro 		nd->inode = path.dentry->d_inode;
1816efe772d6SAl Viro 		nd->seq = seq;
1817efe772d6SAl Viro 		if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1818efe772d6SAl Viro 			return ERR_PTR(-ECHILD);
1819efe772d6SAl Viro 		/* we know that mountpoint was pinned */
1820957dd41dSAl Viro 	}
182112487f30SAl Viro 	old = nd->path.dentry;
182212487f30SAl Viro 	parent = old->d_parent;
182312487f30SAl Viro 	*inodep = parent->d_inode;
182412487f30SAl Viro 	*seqp = read_seqcount_begin(&parent->d_seq);
182512487f30SAl Viro 	if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
182612487f30SAl Viro 		return ERR_PTR(-ECHILD);
182712487f30SAl Viro 	if (unlikely(!path_connected(nd->path.mnt, parent)))
182812487f30SAl Viro 		return ERR_PTR(-ECHILD);
182912487f30SAl Viro 	return parent;
183012487f30SAl Viro in_root:
1831efe772d6SAl Viro 	if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1832efe772d6SAl Viro 		return ERR_PTR(-ECHILD);
1833957dd41dSAl Viro 	if (unlikely(nd->flags & LOOKUP_BENEATH))
18347521f22bSAl Viro 		return ERR_PTR(-ECHILD);
1835c2df1968SAl Viro 	return NULL;
1836957dd41dSAl Viro }
1837957dd41dSAl Viro 
1838c2df1968SAl Viro static struct dentry *follow_dotdot(struct nameidata *nd,
1839c2df1968SAl Viro 				 struct inode **inodep,
1840c2df1968SAl Viro 				 unsigned *seqp)
1841957dd41dSAl Viro {
184212487f30SAl Viro 	struct dentry *parent;
184312487f30SAl Viro 
1844957dd41dSAl Viro 	if (path_equal(&nd->path, &nd->root))
184512487f30SAl Viro 		goto in_root;
184612487f30SAl Viro 	if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
18472aa38470SAl Viro 		struct path path;
18482aa38470SAl Viro 
18492aa38470SAl Viro 		if (!choose_mountpoint(real_mount(nd->path.mnt),
18502aa38470SAl Viro 				       &nd->root, &path))
185112487f30SAl Viro 			goto in_root;
1852165200d6SAl Viro 		path_put(&nd->path);
1853165200d6SAl Viro 		nd->path = path;
18542aa38470SAl Viro 		nd->inode = path.dentry->d_inode;
1855165200d6SAl Viro 		if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1856165200d6SAl Viro 			return ERR_PTR(-EXDEV);
185712487f30SAl Viro 	}
1858957dd41dSAl Viro 	/* rare case of legitimate dget_parent()... */
185912487f30SAl Viro 	parent = dget_parent(nd->path.dentry);
1860957dd41dSAl Viro 	if (unlikely(!path_connected(nd->path.mnt, parent))) {
1861957dd41dSAl Viro 		dput(parent);
18627521f22bSAl Viro 		return ERR_PTR(-ENOENT);
1863957dd41dSAl Viro 	}
1864c2df1968SAl Viro 	*seqp = 0;
1865c2df1968SAl Viro 	*inodep = parent->d_inode;
1866c2df1968SAl Viro 	return parent;
186712487f30SAl Viro 
186812487f30SAl Viro in_root:
1869957dd41dSAl Viro 	if (unlikely(nd->flags & LOOKUP_BENEATH))
18707521f22bSAl Viro 		return ERR_PTR(-EXDEV);
1871c2df1968SAl Viro 	dget(nd->path.dentry);
1872c2df1968SAl Viro 	return NULL;
1873957dd41dSAl Viro }
1874957dd41dSAl Viro 
18757521f22bSAl Viro static const char *handle_dots(struct nameidata *nd, int type)
1876957dd41dSAl Viro {
1877957dd41dSAl Viro 	if (type == LAST_DOTDOT) {
18787521f22bSAl Viro 		const char *error = NULL;
1879c2df1968SAl Viro 		struct dentry *parent;
1880c2df1968SAl Viro 		struct inode *inode;
1881c2df1968SAl Viro 		unsigned seq;
1882957dd41dSAl Viro 
1883957dd41dSAl Viro 		if (!nd->root.mnt) {
18847521f22bSAl Viro 			error = ERR_PTR(set_root(nd));
1885957dd41dSAl Viro 			if (error)
1886957dd41dSAl Viro 				return error;
1887957dd41dSAl Viro 		}
1888957dd41dSAl Viro 		if (nd->flags & LOOKUP_RCU)
1889c2df1968SAl Viro 			parent = follow_dotdot_rcu(nd, &inode, &seq);
1890957dd41dSAl Viro 		else
1891c2df1968SAl Viro 			parent = follow_dotdot(nd, &inode, &seq);
1892c2df1968SAl Viro 		if (IS_ERR(parent))
1893c2df1968SAl Viro 			return ERR_CAST(parent);
1894c2df1968SAl Viro 		if (unlikely(!parent))
1895c2df1968SAl Viro 			error = step_into(nd, WALK_NOFOLLOW,
1896c2df1968SAl Viro 					 nd->path.dentry, nd->inode, nd->seq);
1897c2df1968SAl Viro 		else
1898c2df1968SAl Viro 			error = step_into(nd, WALK_NOFOLLOW,
1899c2df1968SAl Viro 					 parent, inode, seq);
1900c2df1968SAl Viro 		if (unlikely(error))
1901957dd41dSAl Viro 			return error;
1902957dd41dSAl Viro 
1903957dd41dSAl Viro 		if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
1904957dd41dSAl Viro 			/*
1905957dd41dSAl Viro 			 * If there was a racing rename or mount along our
1906957dd41dSAl Viro 			 * path, then we can't be sure that ".." hasn't jumped
1907957dd41dSAl Viro 			 * above nd->root (and so userspace should retry or use
1908957dd41dSAl Viro 			 * some fallback).
1909957dd41dSAl Viro 			 */
1910957dd41dSAl Viro 			smp_rmb();
1911957dd41dSAl Viro 			if (unlikely(__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq)))
19127521f22bSAl Viro 				return ERR_PTR(-EAGAIN);
1913957dd41dSAl Viro 			if (unlikely(__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq)))
19147521f22bSAl Viro 				return ERR_PTR(-EAGAIN);
1915957dd41dSAl Viro 		}
1916957dd41dSAl Viro 	}
19177521f22bSAl Viro 	return NULL;
1918957dd41dSAl Viro }
1919957dd41dSAl Viro 
192092d27016SAl Viro static const char *walk_component(struct nameidata *nd, int flags)
1921ce57dfc1SAl Viro {
1922db3c9adeSAl Viro 	struct dentry *dentry;
1923ce57dfc1SAl Viro 	struct inode *inode;
1924254cf582SAl Viro 	unsigned seq;
1925ce57dfc1SAl Viro 	/*
1926ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1927ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1928ce57dfc1SAl Viro 	 * parent relationships.
1929ce57dfc1SAl Viro 	 */
19304693a547SAl Viro 	if (unlikely(nd->last_type != LAST_NORM)) {
19311c4ff1a8SAl Viro 		if (!(flags & WALK_MORE) && nd->depth)
19324693a547SAl Viro 			put_link(nd);
19337521f22bSAl Viro 		return handle_dots(nd, nd->last_type);
19344693a547SAl Viro 	}
193520e34357SAl Viro 	dentry = lookup_fast(nd, &inode, &seq);
193620e34357SAl Viro 	if (IS_ERR(dentry))
193792d27016SAl Viro 		return ERR_CAST(dentry);
193820e34357SAl Viro 	if (unlikely(!dentry)) {
1939db3c9adeSAl Viro 		dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags);
1940db3c9adeSAl Viro 		if (IS_ERR(dentry))
194192d27016SAl Viro 			return ERR_CAST(dentry);
194220e34357SAl Viro 	}
194356676ec3SAl Viro 	if (!(flags & WALK_MORE) && nd->depth)
194456676ec3SAl Viro 		put_link(nd);
1945b0417d2cSAl Viro 	return step_into(nd, flags, dentry, inode, seq);
1946ce57dfc1SAl Viro }
1947ce57dfc1SAl Viro 
19481da177e4SLinus Torvalds /*
1949bfcfaa77SLinus Torvalds  * We can do the critical dentry name comparison and hashing
1950bfcfaa77SLinus Torvalds  * operations one word at a time, but we are limited to:
1951bfcfaa77SLinus Torvalds  *
1952bfcfaa77SLinus Torvalds  * - Architectures with fast unaligned word accesses. We could
1953bfcfaa77SLinus Torvalds  *   do a "get_unaligned()" if this helps and is sufficiently
1954bfcfaa77SLinus Torvalds  *   fast.
1955bfcfaa77SLinus Torvalds  *
1956bfcfaa77SLinus Torvalds  * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1957bfcfaa77SLinus Torvalds  *   do not trap on the (extremely unlikely) case of a page
1958bfcfaa77SLinus Torvalds  *   crossing operation.
1959bfcfaa77SLinus Torvalds  *
1960bfcfaa77SLinus Torvalds  * - Furthermore, we need an efficient 64-bit compile for the
1961bfcfaa77SLinus Torvalds  *   64-bit case in order to generate the "number of bytes in
1962bfcfaa77SLinus Torvalds  *   the final mask". Again, that could be replaced with a
1963bfcfaa77SLinus Torvalds  *   efficient population count instruction or similar.
1964bfcfaa77SLinus Torvalds  */
1965bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS
1966bfcfaa77SLinus Torvalds 
1967f68e556eSLinus Torvalds #include <asm/word-at-a-time.h>
1968bfcfaa77SLinus Torvalds 
1969468a9428SGeorge Spelvin #ifdef HASH_MIX
1970bfcfaa77SLinus Torvalds 
1971468a9428SGeorge Spelvin /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
1972468a9428SGeorge Spelvin 
1973468a9428SGeorge Spelvin #elif defined(CONFIG_64BIT)
19742a18da7aSGeorge Spelvin /*
19752a18da7aSGeorge Spelvin  * Register pressure in the mixing function is an issue, particularly
19762a18da7aSGeorge Spelvin  * on 32-bit x86, but almost any function requires one state value and
19772a18da7aSGeorge Spelvin  * one temporary.  Instead, use a function designed for two state values
19782a18da7aSGeorge Spelvin  * and no temporaries.
19792a18da7aSGeorge Spelvin  *
19802a18da7aSGeorge Spelvin  * This function cannot create a collision in only two iterations, so
19812a18da7aSGeorge Spelvin  * we have two iterations to achieve avalanche.  In those two iterations,
19822a18da7aSGeorge Spelvin  * we have six layers of mixing, which is enough to spread one bit's
19832a18da7aSGeorge Spelvin  * influence out to 2^6 = 64 state bits.
19842a18da7aSGeorge Spelvin  *
19852a18da7aSGeorge Spelvin  * Rotate constants are scored by considering either 64 one-bit input
19862a18da7aSGeorge Spelvin  * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
19872a18da7aSGeorge Spelvin  * probability of that delta causing a change to each of the 128 output
19882a18da7aSGeorge Spelvin  * bits, using a sample of random initial states.
19892a18da7aSGeorge Spelvin  *
19902a18da7aSGeorge Spelvin  * The Shannon entropy of the computed probabilities is then summed
19912a18da7aSGeorge Spelvin  * to produce a score.  Ideally, any input change has a 50% chance of
19922a18da7aSGeorge Spelvin  * toggling any given output bit.
19932a18da7aSGeorge Spelvin  *
19942a18da7aSGeorge Spelvin  * Mixing scores (in bits) for (12,45):
19952a18da7aSGeorge Spelvin  * Input delta: 1-bit      2-bit
19962a18da7aSGeorge Spelvin  * 1 round:     713.3    42542.6
19972a18da7aSGeorge Spelvin  * 2 rounds:   2753.7   140389.8
19982a18da7aSGeorge Spelvin  * 3 rounds:   5954.1   233458.2
19992a18da7aSGeorge Spelvin  * 4 rounds:   7862.6   256672.2
20002a18da7aSGeorge Spelvin  * Perfect:    8192     258048
20012a18da7aSGeorge Spelvin  *            (64*128) (64*63/2 * 128)
20022a18da7aSGeorge Spelvin  */
20032a18da7aSGeorge Spelvin #define HASH_MIX(x, y, a)	\
20042a18da7aSGeorge Spelvin 	(	x ^= (a),	\
20052a18da7aSGeorge Spelvin 	y ^= x,	x = rol64(x,12),\
20062a18da7aSGeorge Spelvin 	x += y,	y = rol64(y,45),\
20072a18da7aSGeorge Spelvin 	y *= 9			)
2008bfcfaa77SLinus Torvalds 
20090fed3ac8SGeorge Spelvin /*
20102a18da7aSGeorge Spelvin  * Fold two longs into one 32-bit hash value.  This must be fast, but
20112a18da7aSGeorge Spelvin  * latency isn't quite as critical, as there is a fair bit of additional
20122a18da7aSGeorge Spelvin  * work done before the hash value is used.
20130fed3ac8SGeorge Spelvin  */
20142a18da7aSGeorge Spelvin static inline unsigned int fold_hash(unsigned long x, unsigned long y)
20150fed3ac8SGeorge Spelvin {
20162a18da7aSGeorge Spelvin 	y ^= x * GOLDEN_RATIO_64;
20172a18da7aSGeorge Spelvin 	y *= GOLDEN_RATIO_64;
20182a18da7aSGeorge Spelvin 	return y >> 32;
20190fed3ac8SGeorge Spelvin }
20200fed3ac8SGeorge Spelvin 
2021bfcfaa77SLinus Torvalds #else	/* 32-bit case */
2022bfcfaa77SLinus Torvalds 
20232a18da7aSGeorge Spelvin /*
20242a18da7aSGeorge Spelvin  * Mixing scores (in bits) for (7,20):
20252a18da7aSGeorge Spelvin  * Input delta: 1-bit      2-bit
20262a18da7aSGeorge Spelvin  * 1 round:     330.3     9201.6
20272a18da7aSGeorge Spelvin  * 2 rounds:   1246.4    25475.4
20282a18da7aSGeorge Spelvin  * 3 rounds:   1907.1    31295.1
20292a18da7aSGeorge Spelvin  * 4 rounds:   2042.3    31718.6
20302a18da7aSGeorge Spelvin  * Perfect:    2048      31744
20312a18da7aSGeorge Spelvin  *            (32*64)   (32*31/2 * 64)
20322a18da7aSGeorge Spelvin  */
20332a18da7aSGeorge Spelvin #define HASH_MIX(x, y, a)	\
20342a18da7aSGeorge Spelvin 	(	x ^= (a),	\
20352a18da7aSGeorge Spelvin 	y ^= x,	x = rol32(x, 7),\
20362a18da7aSGeorge Spelvin 	x += y,	y = rol32(y,20),\
20372a18da7aSGeorge Spelvin 	y *= 9			)
2038bfcfaa77SLinus Torvalds 
20392a18da7aSGeorge Spelvin static inline unsigned int fold_hash(unsigned long x, unsigned long y)
20400fed3ac8SGeorge Spelvin {
20412a18da7aSGeorge Spelvin 	/* Use arch-optimized multiply if one exists */
20422a18da7aSGeorge Spelvin 	return __hash_32(y ^ __hash_32(x));
20430fed3ac8SGeorge Spelvin }
20440fed3ac8SGeorge Spelvin 
2045bfcfaa77SLinus Torvalds #endif
2046bfcfaa77SLinus Torvalds 
20472a18da7aSGeorge Spelvin /*
20482a18da7aSGeorge Spelvin  * Return the hash of a string of known length.  This is carfully
20492a18da7aSGeorge Spelvin  * designed to match hash_name(), which is the more critical function.
20502a18da7aSGeorge Spelvin  * In particular, we must end by hashing a final word containing 0..7
20512a18da7aSGeorge Spelvin  * payload bytes, to match the way that hash_name() iterates until it
20522a18da7aSGeorge Spelvin  * finds the delimiter after the name.
20532a18da7aSGeorge Spelvin  */
20548387ff25SLinus Torvalds unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
2055bfcfaa77SLinus Torvalds {
20568387ff25SLinus Torvalds 	unsigned long a, x = 0, y = (unsigned long)salt;
2057bfcfaa77SLinus Torvalds 
2058bfcfaa77SLinus Torvalds 	for (;;) {
2059fcfd2fbfSGeorge Spelvin 		if (!len)
2060fcfd2fbfSGeorge Spelvin 			goto done;
2061e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name);
2062bfcfaa77SLinus Torvalds 		if (len < sizeof(unsigned long))
2063bfcfaa77SLinus Torvalds 			break;
20642a18da7aSGeorge Spelvin 		HASH_MIX(x, y, a);
2065bfcfaa77SLinus Torvalds 		name += sizeof(unsigned long);
2066bfcfaa77SLinus Torvalds 		len -= sizeof(unsigned long);
2067bfcfaa77SLinus Torvalds 	}
20682a18da7aSGeorge Spelvin 	x ^= a & bytemask_from_count(len);
2069bfcfaa77SLinus Torvalds done:
20702a18da7aSGeorge Spelvin 	return fold_hash(x, y);
2071bfcfaa77SLinus Torvalds }
2072bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
2073bfcfaa77SLinus Torvalds 
2074fcfd2fbfSGeorge Spelvin /* Return the "hash_len" (hash and length) of a null-terminated string */
20758387ff25SLinus Torvalds u64 hashlen_string(const void *salt, const char *name)
2076fcfd2fbfSGeorge Spelvin {
20778387ff25SLinus Torvalds 	unsigned long a = 0, x = 0, y = (unsigned long)salt;
20788387ff25SLinus Torvalds 	unsigned long adata, mask, len;
2079fcfd2fbfSGeorge Spelvin 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
2080fcfd2fbfSGeorge Spelvin 
20818387ff25SLinus Torvalds 	len = 0;
20828387ff25SLinus Torvalds 	goto inside;
20838387ff25SLinus Torvalds 
2084fcfd2fbfSGeorge Spelvin 	do {
20852a18da7aSGeorge Spelvin 		HASH_MIX(x, y, a);
2086fcfd2fbfSGeorge Spelvin 		len += sizeof(unsigned long);
20878387ff25SLinus Torvalds inside:
2088fcfd2fbfSGeorge Spelvin 		a = load_unaligned_zeropad(name+len);
2089fcfd2fbfSGeorge Spelvin 	} while (!has_zero(a, &adata, &constants));
2090fcfd2fbfSGeorge Spelvin 
2091fcfd2fbfSGeorge Spelvin 	adata = prep_zero_mask(a, adata, &constants);
2092fcfd2fbfSGeorge Spelvin 	mask = create_zero_mask(adata);
20932a18da7aSGeorge Spelvin 	x ^= a & zero_bytemask(mask);
2094fcfd2fbfSGeorge Spelvin 
20952a18da7aSGeorge Spelvin 	return hashlen_create(fold_hash(x, y), len + find_zero(mask));
2096fcfd2fbfSGeorge Spelvin }
2097fcfd2fbfSGeorge Spelvin EXPORT_SYMBOL(hashlen_string);
2098fcfd2fbfSGeorge Spelvin 
2099bfcfaa77SLinus Torvalds /*
2100bfcfaa77SLinus Torvalds  * Calculate the length and hash of the path component, and
2101d6bb3e90SLinus Torvalds  * return the "hash_len" as the result.
2102bfcfaa77SLinus Torvalds  */
21038387ff25SLinus Torvalds static inline u64 hash_name(const void *salt, const char *name)
2104bfcfaa77SLinus Torvalds {
21058387ff25SLinus Torvalds 	unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
21068387ff25SLinus Torvalds 	unsigned long adata, bdata, mask, len;
210736126f8fSLinus Torvalds 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
2108bfcfaa77SLinus Torvalds 
21098387ff25SLinus Torvalds 	len = 0;
21108387ff25SLinus Torvalds 	goto inside;
21118387ff25SLinus Torvalds 
2112bfcfaa77SLinus Torvalds 	do {
21132a18da7aSGeorge Spelvin 		HASH_MIX(x, y, a);
2114bfcfaa77SLinus Torvalds 		len += sizeof(unsigned long);
21158387ff25SLinus Torvalds inside:
2116e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name+len);
211736126f8fSLinus Torvalds 		b = a ^ REPEAT_BYTE('/');
211836126f8fSLinus Torvalds 	} while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
2119bfcfaa77SLinus Torvalds 
212036126f8fSLinus Torvalds 	adata = prep_zero_mask(a, adata, &constants);
212136126f8fSLinus Torvalds 	bdata = prep_zero_mask(b, bdata, &constants);
212236126f8fSLinus Torvalds 	mask = create_zero_mask(adata | bdata);
21232a18da7aSGeorge Spelvin 	x ^= a & zero_bytemask(mask);
212436126f8fSLinus Torvalds 
21252a18da7aSGeorge Spelvin 	return hashlen_create(fold_hash(x, y), len + find_zero(mask));
2126bfcfaa77SLinus Torvalds }
2127bfcfaa77SLinus Torvalds 
21282a18da7aSGeorge Spelvin #else	/* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
2129bfcfaa77SLinus Torvalds 
2130fcfd2fbfSGeorge Spelvin /* Return the hash of a string of known length */
21318387ff25SLinus Torvalds unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
21320145acc2SLinus Torvalds {
21338387ff25SLinus Torvalds 	unsigned long hash = init_name_hash(salt);
21340145acc2SLinus Torvalds 	while (len--)
2135fcfd2fbfSGeorge Spelvin 		hash = partial_name_hash((unsigned char)*name++, hash);
21360145acc2SLinus Torvalds 	return end_name_hash(hash);
21370145acc2SLinus Torvalds }
2138ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
21390145acc2SLinus Torvalds 
2140fcfd2fbfSGeorge Spelvin /* Return the "hash_len" (hash and length) of a null-terminated string */
21418387ff25SLinus Torvalds u64 hashlen_string(const void *salt, const char *name)
2142fcfd2fbfSGeorge Spelvin {
21438387ff25SLinus Torvalds 	unsigned long hash = init_name_hash(salt);
2144fcfd2fbfSGeorge Spelvin 	unsigned long len = 0, c;
2145fcfd2fbfSGeorge Spelvin 
2146fcfd2fbfSGeorge Spelvin 	c = (unsigned char)*name;
2147e0ab7af9SGeorge Spelvin 	while (c) {
2148fcfd2fbfSGeorge Spelvin 		len++;
2149fcfd2fbfSGeorge Spelvin 		hash = partial_name_hash(c, hash);
2150fcfd2fbfSGeorge Spelvin 		c = (unsigned char)name[len];
2151e0ab7af9SGeorge Spelvin 	}
2152fcfd2fbfSGeorge Spelvin 	return hashlen_create(end_name_hash(hash), len);
2153fcfd2fbfSGeorge Spelvin }
2154f2a031b6SGeorge Spelvin EXPORT_SYMBOL(hashlen_string);
2155fcfd2fbfSGeorge Spelvin 
21563ddcd056SLinus Torvalds /*
2157200e9ef7SLinus Torvalds  * We know there's a real path component here of at least
2158200e9ef7SLinus Torvalds  * one character.
2159200e9ef7SLinus Torvalds  */
21608387ff25SLinus Torvalds static inline u64 hash_name(const void *salt, const char *name)
2161200e9ef7SLinus Torvalds {
21628387ff25SLinus Torvalds 	unsigned long hash = init_name_hash(salt);
2163200e9ef7SLinus Torvalds 	unsigned long len = 0, c;
2164200e9ef7SLinus Torvalds 
2165200e9ef7SLinus Torvalds 	c = (unsigned char)*name;
2166200e9ef7SLinus Torvalds 	do {
2167200e9ef7SLinus Torvalds 		len++;
2168200e9ef7SLinus Torvalds 		hash = partial_name_hash(c, hash);
2169200e9ef7SLinus Torvalds 		c = (unsigned char)name[len];
2170200e9ef7SLinus Torvalds 	} while (c && c != '/');
2171d6bb3e90SLinus Torvalds 	return hashlen_create(end_name_hash(hash), len);
2172200e9ef7SLinus Torvalds }
2173200e9ef7SLinus Torvalds 
2174bfcfaa77SLinus Torvalds #endif
2175bfcfaa77SLinus Torvalds 
2176200e9ef7SLinus Torvalds /*
21771da177e4SLinus Torvalds  * Name resolution.
2178ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
2179ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
21801da177e4SLinus Torvalds  *
2181ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
2182ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
21831da177e4SLinus Torvalds  */
21846de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
21851da177e4SLinus Torvalds {
2186d8d4611aSAl Viro 	int depth = 0; // depth <= nd->depth
21871da177e4SLinus Torvalds 	int err;
21881da177e4SLinus Torvalds 
2189b4c03536SAl Viro 	nd->last_type = LAST_ROOT;
2190c108837eSAl Viro 	nd->flags |= LOOKUP_PARENT;
21919b5858e9SAl Viro 	if (IS_ERR(name))
21929b5858e9SAl Viro 		return PTR_ERR(name);
21931da177e4SLinus Torvalds 	while (*name=='/')
21941da177e4SLinus Torvalds 		name++;
21951a97d899SAl Viro 	if (!*name) {
21961a97d899SAl Viro 		nd->dir_mode = 0; // short-circuit the 'hardening' idiocy
21979e18f10aSAl Viro 		return 0;
21981a97d899SAl Viro 	}
21991da177e4SLinus Torvalds 
22001da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
22011da177e4SLinus Torvalds 	for(;;) {
220292d27016SAl Viro 		const char *link;
2203d6bb3e90SLinus Torvalds 		u64 hash_len;
2204fe479a58SAl Viro 		int type;
22051da177e4SLinus Torvalds 
2206*ba73d987SChristian Brauner 		err = may_lookup(&init_user_ns, nd);
22071da177e4SLinus Torvalds 		if (err)
22083595e234SAl Viro 			return err;
22091da177e4SLinus Torvalds 
22108387ff25SLinus Torvalds 		hash_len = hash_name(nd->path.dentry, name);
22111da177e4SLinus Torvalds 
2212fe479a58SAl Viro 		type = LAST_NORM;
2213d6bb3e90SLinus Torvalds 		if (name[0] == '.') switch (hashlen_len(hash_len)) {
2214fe479a58SAl Viro 			case 2:
2215200e9ef7SLinus Torvalds 				if (name[1] == '.') {
2216fe479a58SAl Viro 					type = LAST_DOTDOT;
221716c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
221816c2cd71SAl Viro 				}
2219fe479a58SAl Viro 				break;
2220fe479a58SAl Viro 			case 1:
2221fe479a58SAl Viro 				type = LAST_DOT;
2222fe479a58SAl Viro 		}
22235a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
22245a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
222516c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
22265a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
2227a060dc50SJames Hogan 				struct qstr this = { { .hash_len = hash_len }, .name = name };
2228da53be12SLinus Torvalds 				err = parent->d_op->d_hash(parent, &this);
22295a202bcdSAl Viro 				if (err < 0)
22303595e234SAl Viro 					return err;
2231d6bb3e90SLinus Torvalds 				hash_len = this.hash_len;
2232d6bb3e90SLinus Torvalds 				name = this.name;
22335a202bcdSAl Viro 			}
22345a202bcdSAl Viro 		}
2235fe479a58SAl Viro 
2236d6bb3e90SLinus Torvalds 		nd->last.hash_len = hash_len;
2237d6bb3e90SLinus Torvalds 		nd->last.name = name;
22385f4a6a69SAl Viro 		nd->last_type = type;
22395f4a6a69SAl Viro 
2240d6bb3e90SLinus Torvalds 		name += hashlen_len(hash_len);
2241d6bb3e90SLinus Torvalds 		if (!*name)
2242bdf6cbf1SAl Viro 			goto OK;
2243200e9ef7SLinus Torvalds 		/*
2244200e9ef7SLinus Torvalds 		 * If it wasn't NUL, we know it was '/'. Skip that
2245200e9ef7SLinus Torvalds 		 * slash, and continue until no more slashes.
2246200e9ef7SLinus Torvalds 		 */
2247200e9ef7SLinus Torvalds 		do {
2248d6bb3e90SLinus Torvalds 			name++;
2249d6bb3e90SLinus Torvalds 		} while (unlikely(*name == '/'));
22508620c238SAl Viro 		if (unlikely(!*name)) {
22518620c238SAl Viro OK:
2252d8d4611aSAl Viro 			/* pathname or trailing symlink, done */
2253c108837eSAl Viro 			if (!depth) {
2254*ba73d987SChristian Brauner 				nd->dir_uid = i_uid_into_mnt(&init_user_ns, nd->inode);
22550f705953SAl Viro 				nd->dir_mode = nd->inode->i_mode;
2256c108837eSAl Viro 				nd->flags &= ~LOOKUP_PARENT;
22578620c238SAl Viro 				return 0;
2258c108837eSAl Viro 			}
22598620c238SAl Viro 			/* last component of nested symlink */
2260d8d4611aSAl Viro 			name = nd->stack[--depth].name;
22618c4efe22SAl Viro 			link = walk_component(nd, 0);
22621c4ff1a8SAl Viro 		} else {
22631c4ff1a8SAl Viro 			/* not the last component */
22648c4efe22SAl Viro 			link = walk_component(nd, WALK_MORE);
22658620c238SAl Viro 		}
226692d27016SAl Viro 		if (unlikely(link)) {
226792d27016SAl Viro 			if (IS_ERR(link))
226892d27016SAl Viro 				return PTR_ERR(link);
226992d27016SAl Viro 			/* a symlink to follow */
2270d8d4611aSAl Viro 			nd->stack[depth++].name = name;
227192d27016SAl Viro 			name = link;
22729e18f10aSAl Viro 			continue;
227348c8b0c5SAl Viro 		}
227497242f99SAl Viro 		if (unlikely(!d_can_lookup(nd->path.dentry))) {
227597242f99SAl Viro 			if (nd->flags & LOOKUP_RCU) {
22764675ac39SAl Viro 				if (unlazy_walk(nd))
227797242f99SAl Viro 					return -ECHILD;
227897242f99SAl Viro 			}
22793595e234SAl Viro 			return -ENOTDIR;
22805f4a6a69SAl Viro 		}
2281ce57dfc1SAl Viro 	}
228297242f99SAl Viro }
22831da177e4SLinus Torvalds 
2284edc2b1daSAl Viro /* must be paired with terminate_walk() */
2285c8a53ee5SAl Viro static const char *path_init(struct nameidata *nd, unsigned flags)
228631e6b01fSNick Piggin {
2287740a1678SAleksa Sarai 	int error;
2288c8a53ee5SAl Viro 	const char *s = nd->name->name;
228931e6b01fSNick Piggin 
2290c0eb027eSLinus Torvalds 	if (!*s)
2291c0eb027eSLinus Torvalds 		flags &= ~LOOKUP_RCU;
2292edc2b1daSAl Viro 	if (flags & LOOKUP_RCU)
2293edc2b1daSAl Viro 		rcu_read_lock();
2294c0eb027eSLinus Torvalds 
2295c108837eSAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
229631e6b01fSNick Piggin 	nd->depth = 0;
2297ab87f9a5SAleksa Sarai 
2298ab87f9a5SAleksa Sarai 	nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount);
2299ab87f9a5SAleksa Sarai 	nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount);
2300ab87f9a5SAleksa Sarai 	smp_rmb();
2301ab87f9a5SAleksa Sarai 
23025b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
2303b18825a7SDavid Howells 		struct dentry *root = nd->root.dentry;
2304b18825a7SDavid Howells 		struct inode *inode = root->d_inode;
230593893862SAl Viro 		if (*s && unlikely(!d_can_lookup(root)))
2306368ee9baSAl Viro 			return ERR_PTR(-ENOTDIR);
23075b6ca027SAl Viro 		nd->path = nd->root;
23085b6ca027SAl Viro 		nd->inode = inode;
23095b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
2310ab87f9a5SAleksa Sarai 			nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
23118f47a016SAl Viro 			nd->root_seq = nd->seq;
23125b6ca027SAl Viro 		} else {
23135b6ca027SAl Viro 			path_get(&nd->path);
23145b6ca027SAl Viro 		}
2315368ee9baSAl Viro 		return s;
23165b6ca027SAl Viro 	}
23175b6ca027SAl Viro 
231831e6b01fSNick Piggin 	nd->root.mnt = NULL;
2319248fb5b9SAl Viro 	nd->path.mnt = NULL;
2320248fb5b9SAl Viro 	nd->path.dentry = NULL;
232131e6b01fSNick Piggin 
23228db52c7eSAleksa Sarai 	/* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */
23238db52c7eSAleksa Sarai 	if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) {
2324740a1678SAleksa Sarai 		error = nd_jump_root(nd);
2325740a1678SAleksa Sarai 		if (unlikely(error))
2326740a1678SAleksa Sarai 			return ERR_PTR(error);
2327ef55d917SAl Viro 		return s;
23288db52c7eSAleksa Sarai 	}
23298db52c7eSAleksa Sarai 
23308db52c7eSAleksa Sarai 	/* Relative pathname -- get the starting-point it is relative to. */
23318db52c7eSAleksa Sarai 	if (nd->dfd == AT_FDCWD) {
2332e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
233331e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
2334c28cc364SNick Piggin 			unsigned seq;
233531e6b01fSNick Piggin 
2336c28cc364SNick Piggin 			do {
2337c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
233831e6b01fSNick Piggin 				nd->path = fs->pwd;
2339ef55d917SAl Viro 				nd->inode = nd->path.dentry->d_inode;
2340c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2341c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
2342e41f7d4eSAl Viro 		} else {
2343e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
2344ef55d917SAl Viro 			nd->inode = nd->path.dentry->d_inode;
2345e41f7d4eSAl Viro 		}
234631e6b01fSNick Piggin 	} else {
2347582aa64aSJeff Layton 		/* Caller must check execute permissions on the starting path component */
2348c8a53ee5SAl Viro 		struct fd f = fdget_raw(nd->dfd);
234931e6b01fSNick Piggin 		struct dentry *dentry;
235031e6b01fSNick Piggin 
23512903ff01SAl Viro 		if (!f.file)
2352368ee9baSAl Viro 			return ERR_PTR(-EBADF);
235331e6b01fSNick Piggin 
23542903ff01SAl Viro 		dentry = f.file->f_path.dentry;
235531e6b01fSNick Piggin 
2356edc2b1daSAl Viro 		if (*s && unlikely(!d_can_lookup(dentry))) {
23572903ff01SAl Viro 			fdput(f);
2358368ee9baSAl Viro 			return ERR_PTR(-ENOTDIR);
2359f52e0c11SAl Viro 		}
23602903ff01SAl Viro 
23612903ff01SAl Viro 		nd->path = f.file->f_path;
2362e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
236334a26b99SAl Viro 			nd->inode = nd->path.dentry->d_inode;
236434a26b99SAl Viro 			nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
23655590ff0dSUlrich Drepper 		} else {
23662903ff01SAl Viro 			path_get(&nd->path);
236734a26b99SAl Viro 			nd->inode = nd->path.dentry->d_inode;
23681da177e4SLinus Torvalds 		}
236934a26b99SAl Viro 		fdput(f);
2370e41f7d4eSAl Viro 	}
23718db52c7eSAleksa Sarai 
2372adb21d2bSAleksa Sarai 	/* For scoped-lookups we need to set the root to the dirfd as well. */
2373adb21d2bSAleksa Sarai 	if (flags & LOOKUP_IS_SCOPED) {
2374adb21d2bSAleksa Sarai 		nd->root = nd->path;
2375adb21d2bSAleksa Sarai 		if (flags & LOOKUP_RCU) {
2376adb21d2bSAleksa Sarai 			nd->root_seq = nd->seq;
2377adb21d2bSAleksa Sarai 		} else {
2378adb21d2bSAleksa Sarai 			path_get(&nd->root);
2379adb21d2bSAleksa Sarai 			nd->flags |= LOOKUP_ROOT_GRABBED;
2380adb21d2bSAleksa Sarai 		}
2381adb21d2bSAleksa Sarai 	}
2382adb21d2bSAleksa Sarai 	return s;
23839b4a9b14SAl Viro }
23849b4a9b14SAl Viro 
23851ccac622SAl Viro static inline const char *lookup_last(struct nameidata *nd)
238695fa25d9SAl Viro {
2387bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2388bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2389bd92d7feSAl Viro 
2390c108837eSAl Viro 	return walk_component(nd, WALK_TRAILING);
2391bd92d7feSAl Viro }
2392bd92d7feSAl Viro 
23934f757f3cSAl Viro static int handle_lookup_down(struct nameidata *nd)
23944f757f3cSAl Viro {
2395c153007bSAl Viro 	if (!(nd->flags & LOOKUP_RCU))
2396db3c9adeSAl Viro 		dget(nd->path.dentry);
2397b0417d2cSAl Viro 	return PTR_ERR(step_into(nd, WALK_NOFOLLOW,
2398b0417d2cSAl Viro 			nd->path.dentry, nd->inode, nd->seq));
23994f757f3cSAl Viro }
24004f757f3cSAl Viro 
24019b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2402c8a53ee5SAl Viro static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
24039b4a9b14SAl Viro {
2404c8a53ee5SAl Viro 	const char *s = path_init(nd, flags);
2405bd92d7feSAl Viro 	int err;
240631e6b01fSNick Piggin 
24079b5858e9SAl Viro 	if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) {
24084f757f3cSAl Viro 		err = handle_lookup_down(nd);
24095f336e72SAl Viro 		if (unlikely(err < 0))
24105f336e72SAl Viro 			s = ERR_PTR(err);
24114f757f3cSAl Viro 	}
24124f757f3cSAl Viro 
24131ccac622SAl Viro 	while (!(err = link_path_walk(s, nd)) &&
24141ccac622SAl Viro 	       (s = lookup_last(nd)) != NULL)
24151ccac622SAl Viro 		;
24169f1fafeeSAl Viro 	if (!err)
24179f1fafeeSAl Viro 		err = complete_walk(nd);
2418bd92d7feSAl Viro 
2419deb106c6SAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY)
2420deb106c6SAl Viro 		if (!d_can_lookup(nd->path.dentry))
2421bd23a539SAl Viro 			err = -ENOTDIR;
2422161aff1dSAl Viro 	if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) {
2423161aff1dSAl Viro 		err = handle_lookup_down(nd);
2424161aff1dSAl Viro 		nd->flags &= ~LOOKUP_JUMPED; // no d_weak_revalidate(), please...
2425161aff1dSAl Viro 	}
2426625b6d10SAl Viro 	if (!err) {
2427625b6d10SAl Viro 		*path = nd->path;
2428625b6d10SAl Viro 		nd->path.mnt = NULL;
2429625b6d10SAl Viro 		nd->path.dentry = NULL;
2430625b6d10SAl Viro 	}
2431deb106c6SAl Viro 	terminate_walk(nd);
2432bd92d7feSAl Viro 	return err;
243331e6b01fSNick Piggin }
243431e6b01fSNick Piggin 
243531d921c7SDavid Howells int filename_lookup(int dfd, struct filename *name, unsigned flags,
24369ad1aaa6SAl Viro 		    struct path *path, struct path *root)
2437873f1eedSJeff Layton {
2438894bc8c4SAl Viro 	int retval;
24399883d185SAl Viro 	struct nameidata nd;
2440abc9f5beSAl Viro 	if (IS_ERR(name))
2441abc9f5beSAl Viro 		return PTR_ERR(name);
24429ad1aaa6SAl Viro 	if (unlikely(root)) {
24439ad1aaa6SAl Viro 		nd.root = *root;
24449ad1aaa6SAl Viro 		flags |= LOOKUP_ROOT;
24459ad1aaa6SAl Viro 	}
24469883d185SAl Viro 	set_nameidata(&nd, dfd, name);
2447c8a53ee5SAl Viro 	retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
2448873f1eedSJeff Layton 	if (unlikely(retval == -ECHILD))
2449c8a53ee5SAl Viro 		retval = path_lookupat(&nd, flags, path);
2450873f1eedSJeff Layton 	if (unlikely(retval == -ESTALE))
2451c8a53ee5SAl Viro 		retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
2452873f1eedSJeff Layton 
2453873f1eedSJeff Layton 	if (likely(!retval))
2454161aff1dSAl Viro 		audit_inode(name, path->dentry,
2455161aff1dSAl Viro 			    flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0);
24569883d185SAl Viro 	restore_nameidata();
2457e4bd1c1aSAl Viro 	putname(name);
2458873f1eedSJeff Layton 	return retval;
2459873f1eedSJeff Layton }
2460873f1eedSJeff Layton 
24618bcb77faSAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2462c8a53ee5SAl Viro static int path_parentat(struct nameidata *nd, unsigned flags,
2463391172c4SAl Viro 				struct path *parent)
24648bcb77faSAl Viro {
2465c8a53ee5SAl Viro 	const char *s = path_init(nd, flags);
24669b5858e9SAl Viro 	int err = link_path_walk(s, nd);
24678bcb77faSAl Viro 	if (!err)
24688bcb77faSAl Viro 		err = complete_walk(nd);
2469391172c4SAl Viro 	if (!err) {
2470391172c4SAl Viro 		*parent = nd->path;
2471391172c4SAl Viro 		nd->path.mnt = NULL;
2472391172c4SAl Viro 		nd->path.dentry = NULL;
2473391172c4SAl Viro 	}
2474deb106c6SAl Viro 	terminate_walk(nd);
24758bcb77faSAl Viro 	return err;
24768bcb77faSAl Viro }
24778bcb77faSAl Viro 
24785c31b6ceSAl Viro static struct filename *filename_parentat(int dfd, struct filename *name,
2479391172c4SAl Viro 				unsigned int flags, struct path *parent,
2480391172c4SAl Viro 				struct qstr *last, int *type)
24818bcb77faSAl Viro {
24828bcb77faSAl Viro 	int retval;
24839883d185SAl Viro 	struct nameidata nd;
24848bcb77faSAl Viro 
24855c31b6ceSAl Viro 	if (IS_ERR(name))
24865c31b6ceSAl Viro 		return name;
24879883d185SAl Viro 	set_nameidata(&nd, dfd, name);
2488c8a53ee5SAl Viro 	retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
24898bcb77faSAl Viro 	if (unlikely(retval == -ECHILD))
2490c8a53ee5SAl Viro 		retval = path_parentat(&nd, flags, parent);
24918bcb77faSAl Viro 	if (unlikely(retval == -ESTALE))
2492c8a53ee5SAl Viro 		retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
2493391172c4SAl Viro 	if (likely(!retval)) {
2494391172c4SAl Viro 		*last = nd.last;
2495391172c4SAl Viro 		*type = nd.last_type;
2496c9b07eabSAl Viro 		audit_inode(name, parent->dentry, AUDIT_INODE_PARENT);
24975c31b6ceSAl Viro 	} else {
24985c31b6ceSAl Viro 		putname(name);
24995c31b6ceSAl Viro 		name = ERR_PTR(retval);
2500391172c4SAl Viro 	}
25019883d185SAl Viro 	restore_nameidata();
25025c31b6ceSAl Viro 	return name;
25038bcb77faSAl Viro }
25048bcb77faSAl Viro 
250579714f72SAl Viro /* does lookup, returns the object with parent locked */
250679714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path)
25075590ff0dSUlrich Drepper {
25085c31b6ceSAl Viro 	struct filename *filename;
25095c31b6ceSAl Viro 	struct dentry *d;
2510391172c4SAl Viro 	struct qstr last;
2511391172c4SAl Viro 	int type;
251251689104SPaul Moore 
25135c31b6ceSAl Viro 	filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
25145c31b6ceSAl Viro 				    &last, &type);
251551689104SPaul Moore 	if (IS_ERR(filename))
251651689104SPaul Moore 		return ERR_CAST(filename);
25175c31b6ceSAl Viro 	if (unlikely(type != LAST_NORM)) {
2518391172c4SAl Viro 		path_put(path);
25195c31b6ceSAl Viro 		putname(filename);
25205c31b6ceSAl Viro 		return ERR_PTR(-EINVAL);
252179714f72SAl Viro 	}
25225955102cSAl Viro 	inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
2523391172c4SAl Viro 	d = __lookup_hash(&last, path->dentry, 0);
252479714f72SAl Viro 	if (IS_ERR(d)) {
25255955102cSAl Viro 		inode_unlock(path->dentry->d_inode);
2526391172c4SAl Viro 		path_put(path);
252779714f72SAl Viro 	}
252851689104SPaul Moore 	putname(filename);
252979714f72SAl Viro 	return d;
25305590ff0dSUlrich Drepper }
25315590ff0dSUlrich Drepper 
2532d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
2533d1811465SAl Viro {
2534abc9f5beSAl Viro 	return filename_lookup(AT_FDCWD, getname_kernel(name),
2535abc9f5beSAl Viro 			       flags, path, NULL);
2536d1811465SAl Viro }
25374d359507SAl Viro EXPORT_SYMBOL(kern_path);
2538d1811465SAl Viro 
253916f18200SJosef 'Jeff' Sipek /**
254016f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
254116f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
254216f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
254316f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
254416f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
2545e0a01249SAl Viro  * @path: pointer to struct path to fill
254616f18200SJosef 'Jeff' Sipek  */
254716f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
254816f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
2549e0a01249SAl Viro 		    struct path *path)
255016f18200SJosef 'Jeff' Sipek {
25519ad1aaa6SAl Viro 	struct path root = {.mnt = mnt, .dentry = dentry};
25529ad1aaa6SAl Viro 	/* the first argument of filename_lookup() is ignored with root */
2553abc9f5beSAl Viro 	return filename_lookup(AT_FDCWD, getname_kernel(name),
2554abc9f5beSAl Viro 			       flags , path, &root);
255516f18200SJosef 'Jeff' Sipek }
25564d359507SAl Viro EXPORT_SYMBOL(vfs_path_lookup);
255716f18200SJosef 'Jeff' Sipek 
25583c95f0dcSAl Viro static int lookup_one_len_common(const char *name, struct dentry *base,
25593c95f0dcSAl Viro 				 int len, struct qstr *this)
25603c95f0dcSAl Viro {
25613c95f0dcSAl Viro 	this->name = name;
25623c95f0dcSAl Viro 	this->len = len;
25633c95f0dcSAl Viro 	this->hash = full_name_hash(base, name, len);
25643c95f0dcSAl Viro 	if (!len)
25653c95f0dcSAl Viro 		return -EACCES;
25663c95f0dcSAl Viro 
25673c95f0dcSAl Viro 	if (unlikely(name[0] == '.')) {
25683c95f0dcSAl Viro 		if (len < 2 || (len == 2 && name[1] == '.'))
25693c95f0dcSAl Viro 			return -EACCES;
25703c95f0dcSAl Viro 	}
25713c95f0dcSAl Viro 
25723c95f0dcSAl Viro 	while (len--) {
25733c95f0dcSAl Viro 		unsigned int c = *(const unsigned char *)name++;
25743c95f0dcSAl Viro 		if (c == '/' || c == '\0')
25753c95f0dcSAl Viro 			return -EACCES;
25763c95f0dcSAl Viro 	}
25773c95f0dcSAl Viro 	/*
25783c95f0dcSAl Viro 	 * See if the low-level filesystem might want
25793c95f0dcSAl Viro 	 * to use its own hash..
25803c95f0dcSAl Viro 	 */
25813c95f0dcSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
25823c95f0dcSAl Viro 		int err = base->d_op->d_hash(base, this);
25833c95f0dcSAl Viro 		if (err < 0)
25843c95f0dcSAl Viro 			return err;
25853c95f0dcSAl Viro 	}
25863c95f0dcSAl Viro 
258747291baaSChristian Brauner 	return inode_permission(&init_user_ns, base->d_inode, MAY_EXEC);
25883c95f0dcSAl Viro }
25893c95f0dcSAl Viro 
2590eead1911SChristoph Hellwig /**
25910da0b7fdSDavid Howells  * try_lookup_one_len - filesystem helper to lookup single pathname component
25920da0b7fdSDavid Howells  * @name:	pathname component to lookup
25930da0b7fdSDavid Howells  * @base:	base directory to lookup from
25940da0b7fdSDavid Howells  * @len:	maximum length @len should be interpreted to
25950da0b7fdSDavid Howells  *
25960da0b7fdSDavid Howells  * Look up a dentry by name in the dcache, returning NULL if it does not
25970da0b7fdSDavid Howells  * currently exist.  The function does not try to create a dentry.
25980da0b7fdSDavid Howells  *
25990da0b7fdSDavid Howells  * Note that this routine is purely a helper for filesystem usage and should
26000da0b7fdSDavid Howells  * not be called by generic code.
26010da0b7fdSDavid Howells  *
26020da0b7fdSDavid Howells  * The caller must hold base->i_mutex.
26030da0b7fdSDavid Howells  */
26040da0b7fdSDavid Howells struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len)
26050da0b7fdSDavid Howells {
26060da0b7fdSDavid Howells 	struct qstr this;
26070da0b7fdSDavid Howells 	int err;
26080da0b7fdSDavid Howells 
26090da0b7fdSDavid Howells 	WARN_ON_ONCE(!inode_is_locked(base->d_inode));
26100da0b7fdSDavid Howells 
26110da0b7fdSDavid Howells 	err = lookup_one_len_common(name, base, len, &this);
26120da0b7fdSDavid Howells 	if (err)
26130da0b7fdSDavid Howells 		return ERR_PTR(err);
26140da0b7fdSDavid Howells 
26150da0b7fdSDavid Howells 	return lookup_dcache(&this, base, 0);
26160da0b7fdSDavid Howells }
26170da0b7fdSDavid Howells EXPORT_SYMBOL(try_lookup_one_len);
26180da0b7fdSDavid Howells 
26190da0b7fdSDavid Howells /**
2620a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
2621eead1911SChristoph Hellwig  * @name:	pathname component to lookup
2622eead1911SChristoph Hellwig  * @base:	base directory to lookup from
2623eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
2624eead1911SChristoph Hellwig  *
2625a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
26269e7543e9SAl Viro  * not be called by generic code.
2627bbddca8eSNeilBrown  *
2628bbddca8eSNeilBrown  * The caller must hold base->i_mutex.
2629eead1911SChristoph Hellwig  */
2630057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2631057f6c01SJames Morris {
26328613a209SAl Viro 	struct dentry *dentry;
2633057f6c01SJames Morris 	struct qstr this;
2634cda309deSMiklos Szeredi 	int err;
2635057f6c01SJames Morris 
26365955102cSAl Viro 	WARN_ON_ONCE(!inode_is_locked(base->d_inode));
26372f9092e1SDavid Woodhouse 
26383c95f0dcSAl Viro 	err = lookup_one_len_common(name, base, len, &this);
2639cda309deSMiklos Szeredi 	if (err)
2640cda309deSMiklos Szeredi 		return ERR_PTR(err);
2641cda309deSMiklos Szeredi 
26428613a209SAl Viro 	dentry = lookup_dcache(&this, base, 0);
26438613a209SAl Viro 	return dentry ? dentry : __lookup_slow(&this, base, 0);
2644057f6c01SJames Morris }
26454d359507SAl Viro EXPORT_SYMBOL(lookup_one_len);
2646057f6c01SJames Morris 
2647bbddca8eSNeilBrown /**
2648bbddca8eSNeilBrown  * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2649bbddca8eSNeilBrown  * @name:	pathname component to lookup
2650bbddca8eSNeilBrown  * @base:	base directory to lookup from
2651bbddca8eSNeilBrown  * @len:	maximum length @len should be interpreted to
2652bbddca8eSNeilBrown  *
2653bbddca8eSNeilBrown  * Note that this routine is purely a helper for filesystem usage and should
2654bbddca8eSNeilBrown  * not be called by generic code.
2655bbddca8eSNeilBrown  *
2656bbddca8eSNeilBrown  * Unlike lookup_one_len, it should be called without the parent
2657bbddca8eSNeilBrown  * i_mutex held, and will take the i_mutex itself if necessary.
2658bbddca8eSNeilBrown  */
2659bbddca8eSNeilBrown struct dentry *lookup_one_len_unlocked(const char *name,
2660bbddca8eSNeilBrown 				       struct dentry *base, int len)
2661bbddca8eSNeilBrown {
2662bbddca8eSNeilBrown 	struct qstr this;
2663bbddca8eSNeilBrown 	int err;
266420d00ee8SLinus Torvalds 	struct dentry *ret;
2665bbddca8eSNeilBrown 
26663c95f0dcSAl Viro 	err = lookup_one_len_common(name, base, len, &this);
2667bbddca8eSNeilBrown 	if (err)
2668bbddca8eSNeilBrown 		return ERR_PTR(err);
2669bbddca8eSNeilBrown 
267020d00ee8SLinus Torvalds 	ret = lookup_dcache(&this, base, 0);
267120d00ee8SLinus Torvalds 	if (!ret)
267220d00ee8SLinus Torvalds 		ret = lookup_slow(&this, base, 0);
267320d00ee8SLinus Torvalds 	return ret;
2674bbddca8eSNeilBrown }
2675bbddca8eSNeilBrown EXPORT_SYMBOL(lookup_one_len_unlocked);
2676bbddca8eSNeilBrown 
26776c2d4798SAl Viro /*
26786c2d4798SAl Viro  * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT)
26796c2d4798SAl Viro  * on negatives.  Returns known positive or ERR_PTR(); that's what
26806c2d4798SAl Viro  * most of the users want.  Note that pinned negative with unlocked parent
26816c2d4798SAl Viro  * _can_ become positive at any time, so callers of lookup_one_len_unlocked()
26826c2d4798SAl Viro  * need to be very careful; pinned positives have ->d_inode stable, so
26836c2d4798SAl Viro  * this one avoids such problems.
26846c2d4798SAl Viro  */
26856c2d4798SAl Viro struct dentry *lookup_positive_unlocked(const char *name,
26866c2d4798SAl Viro 				       struct dentry *base, int len)
26876c2d4798SAl Viro {
26886c2d4798SAl Viro 	struct dentry *ret = lookup_one_len_unlocked(name, base, len);
26892fa6b1e0SAl Viro 	if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
26906c2d4798SAl Viro 		dput(ret);
26916c2d4798SAl Viro 		ret = ERR_PTR(-ENOENT);
26926c2d4798SAl Viro 	}
26936c2d4798SAl Viro 	return ret;
26946c2d4798SAl Viro }
26956c2d4798SAl Viro EXPORT_SYMBOL(lookup_positive_unlocked);
26966c2d4798SAl Viro 
2697eedf265aSEric W. Biederman #ifdef CONFIG_UNIX98_PTYS
2698eedf265aSEric W. Biederman int path_pts(struct path *path)
2699eedf265aSEric W. Biederman {
2700eedf265aSEric W. Biederman 	/* Find something mounted on "pts" in the same directory as
2701eedf265aSEric W. Biederman 	 * the input path.
2702eedf265aSEric W. Biederman 	 */
2703a6a7eb76SAl Viro 	struct dentry *parent = dget_parent(path->dentry);
2704a6a7eb76SAl Viro 	struct dentry *child;
270519f6028aSAl Viro 	struct qstr this = QSTR_INIT("pts", 3);
2706eedf265aSEric W. Biederman 
2707a6a7eb76SAl Viro 	if (unlikely(!path_connected(path->mnt, parent))) {
2708a6a7eb76SAl Viro 		dput(parent);
270963b27720SAl Viro 		return -ENOENT;
2710a6a7eb76SAl Viro 	}
271163b27720SAl Viro 	dput(path->dentry);
271263b27720SAl Viro 	path->dentry = parent;
2713eedf265aSEric W. Biederman 	child = d_hash_and_lookup(parent, &this);
2714eedf265aSEric W. Biederman 	if (!child)
2715eedf265aSEric W. Biederman 		return -ENOENT;
2716eedf265aSEric W. Biederman 
2717eedf265aSEric W. Biederman 	path->dentry = child;
2718eedf265aSEric W. Biederman 	dput(parent);
271919f6028aSAl Viro 	follow_down(path);
2720eedf265aSEric W. Biederman 	return 0;
2721eedf265aSEric W. Biederman }
2722eedf265aSEric W. Biederman #endif
2723eedf265aSEric W. Biederman 
27241fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
27251fa1e7f6SAndy Whitcroft 		 struct path *path, int *empty)
27261da177e4SLinus Torvalds {
2727abc9f5beSAl Viro 	return filename_lookup(dfd, getname_flags(name, flags, empty),
2728abc9f5beSAl Viro 			       flags, path, NULL);
27291da177e4SLinus Torvalds }
2730b853a161SAl Viro EXPORT_SYMBOL(user_path_at_empty);
27311fa1e7f6SAndy Whitcroft 
2732*ba73d987SChristian Brauner int __check_sticky(struct user_namespace *mnt_userns, struct inode *dir,
2733*ba73d987SChristian Brauner 		   struct inode *inode)
27341da177e4SLinus Torvalds {
27358e96e3b7SEric W. Biederman 	kuid_t fsuid = current_fsuid();
2736da9592edSDavid Howells 
2737*ba73d987SChristian Brauner 	if (uid_eq(i_uid_into_mnt(mnt_userns, inode), fsuid))
27381da177e4SLinus Torvalds 		return 0;
2739*ba73d987SChristian Brauner 	if (uid_eq(i_uid_into_mnt(mnt_userns, dir), fsuid))
27401da177e4SLinus Torvalds 		return 0;
2741*ba73d987SChristian Brauner 	return !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FOWNER);
27421da177e4SLinus Torvalds }
2743cbdf35bcSMiklos Szeredi EXPORT_SYMBOL(__check_sticky);
27441da177e4SLinus Torvalds 
27451da177e4SLinus Torvalds /*
27461da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
27471da177e4SLinus Torvalds  *  whether the type of victim is right.
27481da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
27491da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
27501da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
27511da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
27521da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
27531da177e4SLinus Torvalds  *	a. be owner of dir, or
27541da177e4SLinus Torvalds  *	b. be owner of victim, or
27551da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
27561da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
27571da177e4SLinus Torvalds  *     links pointing to it.
27580bd23d09SEric W. Biederman  *  7. If the victim has an unknown uid or gid we can't change the inode.
27590bd23d09SEric W. Biederman  *  8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
27600bd23d09SEric W. Biederman  *  9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
27610bd23d09SEric W. Biederman  * 10. We can't remove a root or mountpoint.
27620bd23d09SEric W. Biederman  * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
27631da177e4SLinus Torvalds  *     nfs_async_unlink().
27641da177e4SLinus Torvalds  */
2765*ba73d987SChristian Brauner static int may_delete(struct user_namespace *mnt_userns, struct inode *dir,
2766*ba73d987SChristian Brauner 		      struct dentry *victim, bool isdir)
27671da177e4SLinus Torvalds {
276863afdfc7SDavid Howells 	struct inode *inode = d_backing_inode(victim);
27691da177e4SLinus Torvalds 	int error;
27701da177e4SLinus Torvalds 
2771b18825a7SDavid Howells 	if (d_is_negative(victim))
27721da177e4SLinus Torvalds 		return -ENOENT;
2773b18825a7SDavid Howells 	BUG_ON(!inode);
27741da177e4SLinus Torvalds 
27751da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
2776593d1ce8SEric W. Biederman 
2777593d1ce8SEric W. Biederman 	/* Inode writeback is not safe when the uid or gid are invalid. */
2778*ba73d987SChristian Brauner 	if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) ||
2779*ba73d987SChristian Brauner 	    !gid_valid(i_gid_into_mnt(mnt_userns, inode)))
2780593d1ce8SEric W. Biederman 		return -EOVERFLOW;
2781593d1ce8SEric W. Biederman 
27824fa6b5ecSJeff Layton 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
27831da177e4SLinus Torvalds 
2784*ba73d987SChristian Brauner 	error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
27851da177e4SLinus Torvalds 	if (error)
27861da177e4SLinus Torvalds 		return error;
27871da177e4SLinus Torvalds 	if (IS_APPEND(dir))
27881da177e4SLinus Torvalds 		return -EPERM;
2789b18825a7SDavid Howells 
2790*ba73d987SChristian Brauner 	if (check_sticky(mnt_userns, dir, inode) || IS_APPEND(inode) ||
2791*ba73d987SChristian Brauner 	    IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) ||
2792*ba73d987SChristian Brauner 	    HAS_UNMAPPED_ID(mnt_userns, inode))
27931da177e4SLinus Torvalds 		return -EPERM;
27941da177e4SLinus Torvalds 	if (isdir) {
279544b1d530SMiklos Szeredi 		if (!d_is_dir(victim))
27961da177e4SLinus Torvalds 			return -ENOTDIR;
27971da177e4SLinus Torvalds 		if (IS_ROOT(victim))
27981da177e4SLinus Torvalds 			return -EBUSY;
279944b1d530SMiklos Szeredi 	} else if (d_is_dir(victim))
28001da177e4SLinus Torvalds 		return -EISDIR;
28011da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
28021da177e4SLinus Torvalds 		return -ENOENT;
28031da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
28041da177e4SLinus Torvalds 		return -EBUSY;
28051da177e4SLinus Torvalds 	return 0;
28061da177e4SLinus Torvalds }
28071da177e4SLinus Torvalds 
28081da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
28091da177e4SLinus Torvalds  *  dir.
28101da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
28111da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
28121da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
2813036d5236SEric W. Biederman  *  3. We can't do it if the fs can't represent the fsuid or fsgid.
2814036d5236SEric W. Biederman  *  4. We should have write and exec permissions on dir
2815036d5236SEric W. Biederman  *  5. We can't do it if dir is immutable (done in permission())
28161da177e4SLinus Torvalds  */
2817*ba73d987SChristian Brauner static inline int may_create(struct user_namespace *mnt_userns,
2818*ba73d987SChristian Brauner 			     struct inode *dir, struct dentry *child)
28191da177e4SLinus Torvalds {
2820036d5236SEric W. Biederman 	struct user_namespace *s_user_ns;
282114e972b4SJeff Layton 	audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
28221da177e4SLinus Torvalds 	if (child->d_inode)
28231da177e4SLinus Torvalds 		return -EEXIST;
28241da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
28251da177e4SLinus Torvalds 		return -ENOENT;
2826036d5236SEric W. Biederman 	s_user_ns = dir->i_sb->s_user_ns;
2827*ba73d987SChristian Brauner 	if (!kuid_has_mapping(s_user_ns, fsuid_into_mnt(mnt_userns)) ||
2828*ba73d987SChristian Brauner 	    !kgid_has_mapping(s_user_ns, fsgid_into_mnt(mnt_userns)))
2829036d5236SEric W. Biederman 		return -EOVERFLOW;
2830*ba73d987SChristian Brauner 	return inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
28311da177e4SLinus Torvalds }
28321da177e4SLinus Torvalds 
28331da177e4SLinus Torvalds /*
28341da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
28351da177e4SLinus Torvalds  */
28361da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
28371da177e4SLinus Torvalds {
28381da177e4SLinus Torvalds 	struct dentry *p;
28391da177e4SLinus Torvalds 
28401da177e4SLinus Torvalds 	if (p1 == p2) {
28415955102cSAl Viro 		inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
28421da177e4SLinus Torvalds 		return NULL;
28431da177e4SLinus Torvalds 	}
28441da177e4SLinus Torvalds 
2845fc64005cSAl Viro 	mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
28461da177e4SLinus Torvalds 
2847e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
2848e2761a11SOGAWA Hirofumi 	if (p) {
28495955102cSAl Viro 		inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
28505955102cSAl Viro 		inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
28511da177e4SLinus Torvalds 		return p;
28521da177e4SLinus Torvalds 	}
28531da177e4SLinus Torvalds 
2854e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
2855e2761a11SOGAWA Hirofumi 	if (p) {
28565955102cSAl Viro 		inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
28575955102cSAl Viro 		inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
28581da177e4SLinus Torvalds 		return p;
28591da177e4SLinus Torvalds 	}
28601da177e4SLinus Torvalds 
28615955102cSAl Viro 	inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
28625955102cSAl Viro 	inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
28631da177e4SLinus Torvalds 	return NULL;
28641da177e4SLinus Torvalds }
28654d359507SAl Viro EXPORT_SYMBOL(lock_rename);
28661da177e4SLinus Torvalds 
28671da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
28681da177e4SLinus Torvalds {
28695955102cSAl Viro 	inode_unlock(p1->d_inode);
28701da177e4SLinus Torvalds 	if (p1 != p2) {
28715955102cSAl Viro 		inode_unlock(p2->d_inode);
2872fc64005cSAl Viro 		mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
28731da177e4SLinus Torvalds 	}
28741da177e4SLinus Torvalds }
28754d359507SAl Viro EXPORT_SYMBOL(unlock_rename);
28761da177e4SLinus Torvalds 
28774acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2878312b63fbSAl Viro 		bool want_excl)
28791da177e4SLinus Torvalds {
2880*ba73d987SChristian Brauner 	int error = may_create(&init_user_ns, dir, dentry);
28811da177e4SLinus Torvalds 	if (error)
28821da177e4SLinus Torvalds 		return error;
28831da177e4SLinus Torvalds 
2884acfa4380SAl Viro 	if (!dir->i_op->create)
28851da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
28861da177e4SLinus Torvalds 	mode &= S_IALLUGO;
28871da177e4SLinus Torvalds 	mode |= S_IFREG;
28881da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
28891da177e4SLinus Torvalds 	if (error)
28901da177e4SLinus Torvalds 		return error;
2891312b63fbSAl Viro 	error = dir->i_op->create(dir, dentry, mode, want_excl);
2892a74574aaSStephen Smalley 	if (!error)
2893f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
28941da177e4SLinus Torvalds 	return error;
28951da177e4SLinus Torvalds }
28964d359507SAl Viro EXPORT_SYMBOL(vfs_create);
28971da177e4SLinus Torvalds 
28988e6c848eSAl Viro int vfs_mkobj(struct dentry *dentry, umode_t mode,
28998e6c848eSAl Viro 		int (*f)(struct dentry *, umode_t, void *),
29008e6c848eSAl Viro 		void *arg)
29018e6c848eSAl Viro {
29028e6c848eSAl Viro 	struct inode *dir = dentry->d_parent->d_inode;
2903*ba73d987SChristian Brauner 	int error = may_create(&init_user_ns, dir, dentry);
29048e6c848eSAl Viro 	if (error)
29058e6c848eSAl Viro 		return error;
29068e6c848eSAl Viro 
29078e6c848eSAl Viro 	mode &= S_IALLUGO;
29088e6c848eSAl Viro 	mode |= S_IFREG;
29098e6c848eSAl Viro 	error = security_inode_create(dir, dentry, mode);
29108e6c848eSAl Viro 	if (error)
29118e6c848eSAl Viro 		return error;
29128e6c848eSAl Viro 	error = f(dentry, mode, arg);
29138e6c848eSAl Viro 	if (!error)
29148e6c848eSAl Viro 		fsnotify_create(dir, dentry);
29158e6c848eSAl Viro 	return error;
29168e6c848eSAl Viro }
29178e6c848eSAl Viro EXPORT_SYMBOL(vfs_mkobj);
29188e6c848eSAl Viro 
2919a2982cc9SEric W. Biederman bool may_open_dev(const struct path *path)
2920a2982cc9SEric W. Biederman {
2921a2982cc9SEric W. Biederman 	return !(path->mnt->mnt_flags & MNT_NODEV) &&
2922a2982cc9SEric W. Biederman 		!(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
2923a2982cc9SEric W. Biederman }
2924a2982cc9SEric W. Biederman 
2925*ba73d987SChristian Brauner static int may_open(struct user_namespace *mnt_userns, const struct path *path,
2926*ba73d987SChristian Brauner 		    int acc_mode, int flag)
29271da177e4SLinus Torvalds {
29283fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
29291da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
29301da177e4SLinus Torvalds 	int error;
29311da177e4SLinus Torvalds 
29321da177e4SLinus Torvalds 	if (!inode)
29331da177e4SLinus Torvalds 		return -ENOENT;
29341da177e4SLinus Torvalds 
2935c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2936c8fe8f30SChristoph Hellwig 	case S_IFLNK:
29371da177e4SLinus Torvalds 		return -ELOOP;
2938c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2939fc4177beSKees Cook 		if (acc_mode & MAY_WRITE)
29401da177e4SLinus Torvalds 			return -EISDIR;
2941fc4177beSKees Cook 		if (acc_mode & MAY_EXEC)
2942fc4177beSKees Cook 			return -EACCES;
2943c8fe8f30SChristoph Hellwig 		break;
2944c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2945c8fe8f30SChristoph Hellwig 	case S_IFCHR:
2946a2982cc9SEric W. Biederman 		if (!may_open_dev(path))
29471da177e4SLinus Torvalds 			return -EACCES;
2948633fb6acSKees Cook 		fallthrough;
2949c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2950c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
2951633fb6acSKees Cook 		if (acc_mode & MAY_EXEC)
2952633fb6acSKees Cook 			return -EACCES;
29531da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2954c8fe8f30SChristoph Hellwig 		break;
29550fd338b2SKees Cook 	case S_IFREG:
29560fd338b2SKees Cook 		if ((acc_mode & MAY_EXEC) && path_noexec(path))
29570fd338b2SKees Cook 			return -EACCES;
29580fd338b2SKees Cook 		break;
29594a3fd211SDave Hansen 	}
2960b41572e9SDave Hansen 
2961*ba73d987SChristian Brauner 	error = inode_permission(mnt_userns, inode, MAY_OPEN | acc_mode);
2962b41572e9SDave Hansen 	if (error)
2963b41572e9SDave Hansen 		return error;
29646146f0d5SMimi Zohar 
29651da177e4SLinus Torvalds 	/*
29661da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
29671da177e4SLinus Torvalds 	 */
29681da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
29698737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
29707715b521SAl Viro 			return -EPERM;
29711da177e4SLinus Torvalds 		if (flag & O_TRUNC)
29727715b521SAl Viro 			return -EPERM;
29731da177e4SLinus Torvalds 	}
29741da177e4SLinus Torvalds 
29751da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
2976*ba73d987SChristian Brauner 	if (flag & O_NOATIME && !inode_owner_or_capable(mnt_userns, inode))
29777715b521SAl Viro 		return -EPERM;
29781da177e4SLinus Torvalds 
2979f3c7691eSJ. Bruce Fields 	return 0;
29807715b521SAl Viro }
29817715b521SAl Viro 
2982e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
29837715b521SAl Viro {
2984f0bb5aafSAl Viro 	const struct path *path = &filp->f_path;
29857715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
29867715b521SAl Viro 	int error = get_write_access(inode);
29871da177e4SLinus Torvalds 	if (error)
29887715b521SAl Viro 		return error;
29891da177e4SLinus Torvalds 	/*
29901da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
29911da177e4SLinus Torvalds 	 */
2992d7a06983SJeff Layton 	error = locks_verify_locked(filp);
2993be6d3e56SKentaro Takeda 	if (!error)
2994ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
29951da177e4SLinus Torvalds 	if (!error) {
29967715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2997d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2998e1181ee6SJeff Layton 				    filp);
29991da177e4SLinus Torvalds 	}
30001da177e4SLinus Torvalds 	put_write_access(inode);
3001acd0c935SMimi Zohar 	return error;
30021da177e4SLinus Torvalds }
30031da177e4SLinus Torvalds 
3004d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
3005d57999e1SDave Hansen {
30068a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
30078a5e929dSAl Viro 		flag--;
3008d57999e1SDave Hansen 	return flag;
3009d57999e1SDave Hansen }
3010d57999e1SDave Hansen 
3011*ba73d987SChristian Brauner static int may_o_create(struct user_namespace *mnt_userns,
3012*ba73d987SChristian Brauner 			const struct path *dir, struct dentry *dentry,
3013*ba73d987SChristian Brauner 			umode_t mode)
3014d18e9008SMiklos Szeredi {
30151328c727SSeth Forshee 	struct user_namespace *s_user_ns;
3016d18e9008SMiklos Szeredi 	int error = security_path_mknod(dir, dentry, mode, 0);
3017d18e9008SMiklos Szeredi 	if (error)
3018d18e9008SMiklos Szeredi 		return error;
3019d18e9008SMiklos Szeredi 
30201328c727SSeth Forshee 	s_user_ns = dir->dentry->d_sb->s_user_ns;
3021*ba73d987SChristian Brauner 	if (!kuid_has_mapping(s_user_ns, fsuid_into_mnt(mnt_userns)) ||
3022*ba73d987SChristian Brauner 	    !kgid_has_mapping(s_user_ns, fsgid_into_mnt(mnt_userns)))
30231328c727SSeth Forshee 		return -EOVERFLOW;
30241328c727SSeth Forshee 
3025*ba73d987SChristian Brauner 	error = inode_permission(mnt_userns, dir->dentry->d_inode,
302647291baaSChristian Brauner 				 MAY_WRITE | MAY_EXEC);
3027d18e9008SMiklos Szeredi 	if (error)
3028d18e9008SMiklos Szeredi 		return error;
3029d18e9008SMiklos Szeredi 
3030d18e9008SMiklos Szeredi 	return security_inode_create(dir->dentry->d_inode, dentry, mode);
3031d18e9008SMiklos Szeredi }
3032d18e9008SMiklos Szeredi 
30331acf0af9SDavid Howells /*
30341acf0af9SDavid Howells  * Attempt to atomically look up, create and open a file from a negative
30351acf0af9SDavid Howells  * dentry.
30361acf0af9SDavid Howells  *
30371acf0af9SDavid Howells  * Returns 0 if successful.  The file will have been created and attached to
30381acf0af9SDavid Howells  * @file by the filesystem calling finish_open().
30391acf0af9SDavid Howells  *
304000a07c15SAl Viro  * If the file was looked up only or didn't need creating, FMODE_OPENED won't
304100a07c15SAl Viro  * be set.  The caller will need to perform the open themselves.  @path will
304200a07c15SAl Viro  * have been updated to point to the new dentry.  This may be negative.
30431acf0af9SDavid Howells  *
30441acf0af9SDavid Howells  * Returns an error code otherwise.
30451acf0af9SDavid Howells  */
3046239eb983SAl Viro static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry,
3047239eb983SAl Viro 				  struct file *file,
30483ec2eef1SAl Viro 				  int open_flag, umode_t mode)
3049d18e9008SMiklos Szeredi {
3050d18e9008SMiklos Szeredi 	struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
3051d18e9008SMiklos Szeredi 	struct inode *dir =  nd->path.dentry->d_inode;
3052d18e9008SMiklos Szeredi 	int error;
3053d18e9008SMiklos Szeredi 
3054d18e9008SMiklos Szeredi 	if (nd->flags & LOOKUP_DIRECTORY)
3055d18e9008SMiklos Szeredi 		open_flag |= O_DIRECTORY;
3056d18e9008SMiklos Szeredi 
305730d90494SAl Viro 	file->f_path.dentry = DENTRY_NOT_SET;
305830d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
30590fb1ea09SAl Viro 	error = dir->i_op->atomic_open(dir, dentry, file,
306044907d79SAl Viro 				       open_to_namei_flags(open_flag), mode);
30616fbd0714SAl Viro 	d_lookup_done(dentry);
3062384f26e2SAl Viro 	if (!error) {
306364e1ac4dSAl Viro 		if (file->f_mode & FMODE_OPENED) {
30646fb968cdSAl Viro 			if (unlikely(dentry != file->f_path.dentry)) {
30656fb968cdSAl Viro 				dput(dentry);
30666fb968cdSAl Viro 				dentry = dget(file->f_path.dentry);
30676fb968cdSAl Viro 			}
306864e1ac4dSAl Viro 		} else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
30692675a4ebSAl Viro 			error = -EIO;
3070384f26e2SAl Viro 		} else {
307130d90494SAl Viro 			if (file->f_path.dentry) {
3072d18e9008SMiklos Szeredi 				dput(dentry);
3073d18e9008SMiklos Szeredi 				dentry = file->f_path.dentry;
307410c64ceaSAl Viro 			}
3075239eb983SAl Viro 			if (unlikely(d_is_negative(dentry)))
3076a01e718fSAl Viro 				error = -ENOENT;
3077d18e9008SMiklos Szeredi 		}
3078d18e9008SMiklos Szeredi 	}
3079239eb983SAl Viro 	if (error) {
3080d18e9008SMiklos Szeredi 		dput(dentry);
3081239eb983SAl Viro 		dentry = ERR_PTR(error);
3082239eb983SAl Viro 	}
3083239eb983SAl Viro 	return dentry;
3084d18e9008SMiklos Szeredi }
3085d18e9008SMiklos Szeredi 
308631e6b01fSNick Piggin /*
30871acf0af9SDavid Howells  * Look up and maybe create and open the last component.
3088d58ffd35SMiklos Szeredi  *
308900a07c15SAl Viro  * Must be called with parent locked (exclusive in O_CREAT case).
3090d58ffd35SMiklos Szeredi  *
309100a07c15SAl Viro  * Returns 0 on success, that is, if
309200a07c15SAl Viro  *  the file was successfully atomically created (if necessary) and opened, or
309300a07c15SAl Viro  *  the file was not completely opened at this time, though lookups and
309400a07c15SAl Viro  *  creations were performed.
309500a07c15SAl Viro  * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
309600a07c15SAl Viro  * In the latter case dentry returned in @path might be negative if O_CREAT
309700a07c15SAl Viro  * hadn't been specified.
30981acf0af9SDavid Howells  *
309900a07c15SAl Viro  * An error code is returned on failure.
3100d58ffd35SMiklos Szeredi  */
3101da5ebf5aSAl Viro static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
3102d58ffd35SMiklos Szeredi 				  const struct open_flags *op,
31033ec2eef1SAl Viro 				  bool got_write)
3104d58ffd35SMiklos Szeredi {
3105d58ffd35SMiklos Szeredi 	struct dentry *dir = nd->path.dentry;
310654ef4872SMiklos Szeredi 	struct inode *dir_inode = dir->d_inode;
31071643b43fSAl Viro 	int open_flag = op->open_flag;
3108d58ffd35SMiklos Szeredi 	struct dentry *dentry;
31091643b43fSAl Viro 	int error, create_error = 0;
31101643b43fSAl Viro 	umode_t mode = op->mode;
31116fbd0714SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
3112d58ffd35SMiklos Szeredi 
3113ce8644fcSAl Viro 	if (unlikely(IS_DEADDIR(dir_inode)))
3114da5ebf5aSAl Viro 		return ERR_PTR(-ENOENT);
3115d58ffd35SMiklos Szeredi 
311673a09dd9SAl Viro 	file->f_mode &= ~FMODE_CREATED;
31176fbd0714SAl Viro 	dentry = d_lookup(dir, &nd->last);
31186fbd0714SAl Viro 	for (;;) {
31196fbd0714SAl Viro 		if (!dentry) {
31206fbd0714SAl Viro 			dentry = d_alloc_parallel(dir, &nd->last, &wq);
3121d58ffd35SMiklos Szeredi 			if (IS_ERR(dentry))
3122da5ebf5aSAl Viro 				return dentry;
31236fbd0714SAl Viro 		}
31246fbd0714SAl Viro 		if (d_in_lookup(dentry))
31256fbd0714SAl Viro 			break;
3126d58ffd35SMiklos Szeredi 
31276fbd0714SAl Viro 		error = d_revalidate(dentry, nd->flags);
31286fbd0714SAl Viro 		if (likely(error > 0))
31296fbd0714SAl Viro 			break;
31306fbd0714SAl Viro 		if (error)
31316fbd0714SAl Viro 			goto out_dput;
31326fbd0714SAl Viro 		d_invalidate(dentry);
31336fbd0714SAl Viro 		dput(dentry);
31346fbd0714SAl Viro 		dentry = NULL;
31356fbd0714SAl Viro 	}
31366fbd0714SAl Viro 	if (dentry->d_inode) {
3137d18e9008SMiklos Szeredi 		/* Cached positive dentry: will open in f_op->open */
3138da5ebf5aSAl Viro 		return dentry;
31396c51e513SAl Viro 	}
3140d18e9008SMiklos Szeredi 
31411643b43fSAl Viro 	/*
31421643b43fSAl Viro 	 * Checking write permission is tricky, bacuse we don't know if we are
31431643b43fSAl Viro 	 * going to actually need it: O_CREAT opens should work as long as the
31441643b43fSAl Viro 	 * file exists.  But checking existence breaks atomicity.  The trick is
31451643b43fSAl Viro 	 * to check access and if not granted clear O_CREAT from the flags.
31461643b43fSAl Viro 	 *
31471643b43fSAl Viro 	 * Another problem is returing the "right" error value (e.g. for an
31481643b43fSAl Viro 	 * O_EXCL open we want to return EEXIST not EROFS).
31491643b43fSAl Viro 	 */
315099a4a90cSAl Viro 	if (unlikely(!got_write))
315199a4a90cSAl Viro 		open_flag &= ~O_TRUNC;
31521643b43fSAl Viro 	if (open_flag & O_CREAT) {
315399a4a90cSAl Viro 		if (open_flag & O_EXCL)
315499a4a90cSAl Viro 			open_flag &= ~O_TRUNC;
31551643b43fSAl Viro 		if (!IS_POSIXACL(dir->d_inode))
31561643b43fSAl Viro 			mode &= ~current_umask();
315799a4a90cSAl Viro 		if (likely(got_write))
3158*ba73d987SChristian Brauner 			create_error = may_o_create(&init_user_ns, &nd->path,
3159*ba73d987SChristian Brauner 						    dentry, mode);
316099a4a90cSAl Viro 		else
316199a4a90cSAl Viro 			create_error = -EROFS;
316299a4a90cSAl Viro 	}
316399a4a90cSAl Viro 	if (create_error)
31641643b43fSAl Viro 		open_flag &= ~O_CREAT;
31651643b43fSAl Viro 	if (dir_inode->i_op->atomic_open) {
3166d489cf9aSAl Viro 		dentry = atomic_open(nd, dentry, file, open_flag, mode);
3167da5ebf5aSAl Viro 		if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT))
3168da5ebf5aSAl Viro 			dentry = ERR_PTR(create_error);
3169da5ebf5aSAl Viro 		return dentry;
3170239eb983SAl Viro 	}
317154ef4872SMiklos Szeredi 
31726fbd0714SAl Viro 	if (d_in_lookup(dentry)) {
317312fa5e24SAl Viro 		struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
317412fa5e24SAl Viro 							     nd->flags);
31756fbd0714SAl Viro 		d_lookup_done(dentry);
317612fa5e24SAl Viro 		if (unlikely(res)) {
317712fa5e24SAl Viro 			if (IS_ERR(res)) {
317812fa5e24SAl Viro 				error = PTR_ERR(res);
317912fa5e24SAl Viro 				goto out_dput;
318012fa5e24SAl Viro 			}
318112fa5e24SAl Viro 			dput(dentry);
318212fa5e24SAl Viro 			dentry = res;
318312fa5e24SAl Viro 		}
318454ef4872SMiklos Szeredi 	}
318554ef4872SMiklos Szeredi 
3186d58ffd35SMiklos Szeredi 	/* Negative dentry, just create the file */
31871643b43fSAl Viro 	if (!dentry->d_inode && (open_flag & O_CREAT)) {
318873a09dd9SAl Viro 		file->f_mode |= FMODE_CREATED;
3189ce8644fcSAl Viro 		audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
3190ce8644fcSAl Viro 		if (!dir_inode->i_op->create) {
3191ce8644fcSAl Viro 			error = -EACCES;
3192d58ffd35SMiklos Szeredi 			goto out_dput;
319364894cf8SAl Viro 		}
3194ce8644fcSAl Viro 		error = dir_inode->i_op->create(dir_inode, dentry, mode,
31951643b43fSAl Viro 						open_flag & O_EXCL);
3196d58ffd35SMiklos Szeredi 		if (error)
3197d58ffd35SMiklos Szeredi 			goto out_dput;
3198d58ffd35SMiklos Szeredi 	}
31991643b43fSAl Viro 	if (unlikely(create_error) && !dentry->d_inode) {
32001643b43fSAl Viro 		error = create_error;
3201d58ffd35SMiklos Szeredi 		goto out_dput;
3202d58ffd35SMiklos Szeredi 	}
3203da5ebf5aSAl Viro 	return dentry;
3204d58ffd35SMiklos Szeredi 
3205d58ffd35SMiklos Szeredi out_dput:
3206d58ffd35SMiklos Szeredi 	dput(dentry);
3207da5ebf5aSAl Viro 	return ERR_PTR(error);
3208d58ffd35SMiklos Szeredi }
3209d58ffd35SMiklos Szeredi 
3210c981a482SAl Viro static const char *open_last_lookups(struct nameidata *nd,
32113ec2eef1SAl Viro 		   struct file *file, const struct open_flags *op)
3212fb1cc555SAl Viro {
3213a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
3214ca344a89SAl Viro 	int open_flag = op->open_flag;
321564894cf8SAl Viro 	bool got_write = false;
3216254cf582SAl Viro 	unsigned seq;
3217a1eb3315SMiklos Szeredi 	struct inode *inode;
3218da5ebf5aSAl Viro 	struct dentry *dentry;
3219b0417d2cSAl Viro 	const char *res;
322016c2cd71SAl Viro 	int error;
3221fb1cc555SAl Viro 
3222c3e380b0SAl Viro 	nd->flags |= op->intent;
3223c3e380b0SAl Viro 
3224bc77daa7SAl Viro 	if (nd->last_type != LAST_NORM) {
322556676ec3SAl Viro 		if (nd->depth)
322656676ec3SAl Viro 			put_link(nd);
3227ff326a32SAl Viro 		return handle_dots(nd, nd->last_type);
32281f36f774SAl Viro 	}
3229a2c36b45SAl Viro 
3230ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
3231fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
3232fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3233fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
323420e34357SAl Viro 		dentry = lookup_fast(nd, &inode, &seq);
323520e34357SAl Viro 		if (IS_ERR(dentry))
32361ccac622SAl Viro 			return ERR_CAST(dentry);
323720e34357SAl Viro 		if (likely(dentry))
323871574865SMiklos Szeredi 			goto finish_lookup;
323971574865SMiklos Szeredi 
32406583fe22SAl Viro 		BUG_ON(nd->flags & LOOKUP_RCU);
3241b6183df7SMiklos Szeredi 	} else {
3242fe2d35ffSAl Viro 		/* create side of things */
324372287417SAl Viro 		if (nd->flags & LOOKUP_RCU) {
324472287417SAl Viro 			error = unlazy_walk(nd);
3245c981a482SAl Viro 			if (unlikely(error))
32461ccac622SAl Viro 				return ERR_PTR(error);
324772287417SAl Viro 		}
3248c9b07eabSAl Viro 		audit_inode(nd->name, dir, AUDIT_INODE_PARENT);
32491f36f774SAl Viro 		/* trailing slashes? */
3250deb106c6SAl Viro 		if (unlikely(nd->last.name[nd->last.len]))
32511ccac622SAl Viro 			return ERR_PTR(-EISDIR);
3252b6183df7SMiklos Szeredi 	}
32531f36f774SAl Viro 
32549cf843e3SAl Viro 	if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
325564894cf8SAl Viro 		error = mnt_want_write(nd->path.mnt);
325664894cf8SAl Viro 		if (!error)
325764894cf8SAl Viro 			got_write = true;
325864894cf8SAl Viro 		/*
325964894cf8SAl Viro 		 * do _not_ fail yet - we might not need that or fail with
326064894cf8SAl Viro 		 * a different error; let lookup_open() decide; we'll be
326164894cf8SAl Viro 		 * dropping this one anyway.
326264894cf8SAl Viro 		 */
326364894cf8SAl Viro 	}
32649cf843e3SAl Viro 	if (open_flag & O_CREAT)
32655955102cSAl Viro 		inode_lock(dir->d_inode);
32669cf843e3SAl Viro 	else
32679cf843e3SAl Viro 		inode_lock_shared(dir->d_inode);
3268da5ebf5aSAl Viro 	dentry = lookup_open(nd, file, op, got_write);
3269f7bb959dSAl Viro 	if (!IS_ERR(dentry) && (file->f_mode & FMODE_CREATED))
3270f7bb959dSAl Viro 		fsnotify_create(dir->d_inode, dentry);
32719cf843e3SAl Viro 	if (open_flag & O_CREAT)
32725955102cSAl Viro 		inode_unlock(dir->d_inode);
32739cf843e3SAl Viro 	else
32749cf843e3SAl Viro 		inode_unlock_shared(dir->d_inode);
3275fb1cc555SAl Viro 
3276c981a482SAl Viro 	if (got_write)
327759e96e65SAl Viro 		mnt_drop_write(nd->path.mnt);
32786c0d46c4SAl Viro 
327959e96e65SAl Viro 	if (IS_ERR(dentry))
328059e96e65SAl Viro 		return ERR_CAST(dentry);
328159e96e65SAl Viro 
3282973d4b73SAl Viro 	if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) {
3283e73cabffSAl Viro 		dput(nd->path.dentry);
3284e73cabffSAl Viro 		nd->path.dentry = dentry;
3285c981a482SAl Viro 		return NULL;
3286fb1cc555SAl Viro 	}
3287fb1cc555SAl Viro 
328820e34357SAl Viro finish_lookup:
328956676ec3SAl Viro 	if (nd->depth)
329056676ec3SAl Viro 		put_link(nd);
32918c4efe22SAl Viro 	res = step_into(nd, WALK_TRAILING, dentry, inode, seq);
3292ff326a32SAl Viro 	if (unlikely(res))
32931ccac622SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
3294b0417d2cSAl Viro 	return res;
32951ccac622SAl Viro }
329631d1726dSAl Viro 
3297c981a482SAl Viro /*
3298c981a482SAl Viro  * Handle the last step of open()
3299c981a482SAl Viro  */
3300c5971b8cSAl Viro static int do_open(struct nameidata *nd,
3301c981a482SAl Viro 		   struct file *file, const struct open_flags *op)
3302c981a482SAl Viro {
3303c981a482SAl Viro 	int open_flag = op->open_flag;
3304c981a482SAl Viro 	bool do_truncate;
3305c981a482SAl Viro 	int acc_mode;
3306c981a482SAl Viro 	int error;
3307c981a482SAl Viro 
3308ff326a32SAl Viro 	if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) {
3309ff326a32SAl Viro 		error = complete_walk(nd);
3310ff326a32SAl Viro 		if (error)
3311ff326a32SAl Viro 			return error;
3312ff326a32SAl Viro 	}
3313973d4b73SAl Viro 	if (!(file->f_mode & FMODE_CREATED))
331476ae2a5aSAl Viro 		audit_inode(nd->name, nd->path.dentry, 0);
331530aba665SSalvatore Mesoraca 	if (open_flag & O_CREAT) {
3316b94e0b32SAl Viro 		if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED))
3317b94e0b32SAl Viro 			return -EEXIST;
331830aba665SSalvatore Mesoraca 		if (d_is_dir(nd->path.dentry))
3319c5971b8cSAl Viro 			return -EISDIR;
3320*ba73d987SChristian Brauner 		error = may_create_in_sticky(&init_user_ns, nd,
332130aba665SSalvatore Mesoraca 					     d_backing_inode(nd->path.dentry));
332230aba665SSalvatore Mesoraca 		if (unlikely(error))
3323c5971b8cSAl Viro 			return error;
332430aba665SSalvatore Mesoraca 	}
332544b1d530SMiklos Szeredi 	if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
3326c5971b8cSAl Viro 		return -ENOTDIR;
33276c0d46c4SAl Viro 
33288795e7d4SAl Viro 	do_truncate = false;
33298795e7d4SAl Viro 	acc_mode = op->acc_mode;
33305a2d3eddSAl Viro 	if (file->f_mode & FMODE_CREATED) {
33315a2d3eddSAl Viro 		/* Don't check for write permission, don't truncate */
33325a2d3eddSAl Viro 		open_flag &= ~O_TRUNC;
33335a2d3eddSAl Viro 		acc_mode = 0;
33348795e7d4SAl Viro 	} else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) {
33350f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
33360f9d1a10SAl Viro 		if (error)
3337c5971b8cSAl Viro 			return error;
33388795e7d4SAl Viro 		do_truncate = true;
33390f9d1a10SAl Viro 	}
3340*ba73d987SChristian Brauner 	error = may_open(&init_user_ns, &nd->path, acc_mode, open_flag);
33418795e7d4SAl Viro 	if (!error && !(file->f_mode & FMODE_OPENED))
3342ae2bb293SAl Viro 		error = vfs_open(&nd->path, file);
33438795e7d4SAl Viro 	if (!error)
33446035a27bSAl Viro 		error = ima_file_check(file, op->acc_mode);
33458795e7d4SAl Viro 	if (!error && do_truncate)
33462675a4ebSAl Viro 		error = handle_truncate(file);
3347c80567c8SAl Viro 	if (unlikely(error > 0)) {
3348c80567c8SAl Viro 		WARN_ON(1);
3349c80567c8SAl Viro 		error = -EINVAL;
3350c80567c8SAl Viro 	}
33518795e7d4SAl Viro 	if (do_truncate)
33520f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
3353c5971b8cSAl Viro 	return error;
3354fb1cc555SAl Viro }
3355fb1cc555SAl Viro 
3356af7bd4dcSAmir Goldstein struct dentry *vfs_tmpfile(struct dentry *dentry, umode_t mode, int open_flag)
3357af7bd4dcSAmir Goldstein {
3358af7bd4dcSAmir Goldstein 	struct dentry *child = NULL;
3359af7bd4dcSAmir Goldstein 	struct inode *dir = dentry->d_inode;
3360af7bd4dcSAmir Goldstein 	struct inode *inode;
3361af7bd4dcSAmir Goldstein 	int error;
3362af7bd4dcSAmir Goldstein 
3363af7bd4dcSAmir Goldstein 	/* we want directory to be writable */
336447291baaSChristian Brauner 	error = inode_permission(&init_user_ns, dir, MAY_WRITE | MAY_EXEC);
3365af7bd4dcSAmir Goldstein 	if (error)
3366af7bd4dcSAmir Goldstein 		goto out_err;
3367af7bd4dcSAmir Goldstein 	error = -EOPNOTSUPP;
3368af7bd4dcSAmir Goldstein 	if (!dir->i_op->tmpfile)
3369af7bd4dcSAmir Goldstein 		goto out_err;
3370af7bd4dcSAmir Goldstein 	error = -ENOMEM;
3371cdf01226SDavid Howells 	child = d_alloc(dentry, &slash_name);
3372af7bd4dcSAmir Goldstein 	if (unlikely(!child))
3373af7bd4dcSAmir Goldstein 		goto out_err;
3374af7bd4dcSAmir Goldstein 	error = dir->i_op->tmpfile(dir, child, mode);
3375af7bd4dcSAmir Goldstein 	if (error)
3376af7bd4dcSAmir Goldstein 		goto out_err;
3377af7bd4dcSAmir Goldstein 	error = -ENOENT;
3378af7bd4dcSAmir Goldstein 	inode = child->d_inode;
3379af7bd4dcSAmir Goldstein 	if (unlikely(!inode))
3380af7bd4dcSAmir Goldstein 		goto out_err;
3381af7bd4dcSAmir Goldstein 	if (!(open_flag & O_EXCL)) {
3382af7bd4dcSAmir Goldstein 		spin_lock(&inode->i_lock);
3383af7bd4dcSAmir Goldstein 		inode->i_state |= I_LINKABLE;
3384af7bd4dcSAmir Goldstein 		spin_unlock(&inode->i_lock);
3385af7bd4dcSAmir Goldstein 	}
3386fdb2410fSMimi Zohar 	ima_post_create_tmpfile(inode);
3387af7bd4dcSAmir Goldstein 	return child;
3388af7bd4dcSAmir Goldstein 
3389af7bd4dcSAmir Goldstein out_err:
3390af7bd4dcSAmir Goldstein 	dput(child);
3391af7bd4dcSAmir Goldstein 	return ERR_PTR(error);
3392af7bd4dcSAmir Goldstein }
3393af7bd4dcSAmir Goldstein EXPORT_SYMBOL(vfs_tmpfile);
3394af7bd4dcSAmir Goldstein 
3395c8a53ee5SAl Viro static int do_tmpfile(struct nameidata *nd, unsigned flags,
339660545d0dSAl Viro 		const struct open_flags *op,
33973ec2eef1SAl Viro 		struct file *file)
339860545d0dSAl Viro {
3399625b6d10SAl Viro 	struct dentry *child;
3400625b6d10SAl Viro 	struct path path;
3401c8a53ee5SAl Viro 	int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
340260545d0dSAl Viro 	if (unlikely(error))
340360545d0dSAl Viro 		return error;
3404625b6d10SAl Viro 	error = mnt_want_write(path.mnt);
340560545d0dSAl Viro 	if (unlikely(error))
340660545d0dSAl Viro 		goto out;
3407af7bd4dcSAmir Goldstein 	child = vfs_tmpfile(path.dentry, op->mode, op->open_flag);
3408af7bd4dcSAmir Goldstein 	error = PTR_ERR(child);
3409684e73beSHirofumi Nakagawa 	if (IS_ERR(child))
341060545d0dSAl Viro 		goto out2;
3411625b6d10SAl Viro 	dput(path.dentry);
3412625b6d10SAl Viro 	path.dentry = child;
3413c8a53ee5SAl Viro 	audit_inode(nd->name, child, 0);
341469a91c23SEric Rannaud 	/* Don't check for other permissions, the inode was just created */
3415*ba73d987SChristian Brauner 	error = may_open(&init_user_ns, &path, 0, op->open_flag);
341660545d0dSAl Viro 	if (error)
341760545d0dSAl Viro 		goto out2;
3418625b6d10SAl Viro 	file->f_path.mnt = path.mnt;
3419be12af3eSAl Viro 	error = finish_open(file, child, NULL);
342060545d0dSAl Viro out2:
3421625b6d10SAl Viro 	mnt_drop_write(path.mnt);
342260545d0dSAl Viro out:
3423625b6d10SAl Viro 	path_put(&path);
342460545d0dSAl Viro 	return error;
342560545d0dSAl Viro }
342660545d0dSAl Viro 
34276ac08709SAl Viro static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
34286ac08709SAl Viro {
34296ac08709SAl Viro 	struct path path;
34306ac08709SAl Viro 	int error = path_lookupat(nd, flags, &path);
34316ac08709SAl Viro 	if (!error) {
34326ac08709SAl Viro 		audit_inode(nd->name, path.dentry, 0);
3433ae2bb293SAl Viro 		error = vfs_open(&path, file);
34346ac08709SAl Viro 		path_put(&path);
34356ac08709SAl Viro 	}
34366ac08709SAl Viro 	return error;
34376ac08709SAl Viro }
34386ac08709SAl Viro 
3439c8a53ee5SAl Viro static struct file *path_openat(struct nameidata *nd,
3440c8a53ee5SAl Viro 			const struct open_flags *op, unsigned flags)
34411da177e4SLinus Torvalds {
344230d90494SAl Viro 	struct file *file;
344313aab428SAl Viro 	int error;
344431e6b01fSNick Piggin 
3445ea73ea72SAl Viro 	file = alloc_empty_file(op->open_flag, current_cred());
34461afc99beSAl Viro 	if (IS_ERR(file))
34471afc99beSAl Viro 		return file;
344831e6b01fSNick Piggin 
3449bb458c64SAl Viro 	if (unlikely(file->f_flags & __O_TMPFILE)) {
34503ec2eef1SAl Viro 		error = do_tmpfile(nd, flags, op, file);
34515f336e72SAl Viro 	} else if (unlikely(file->f_flags & O_PATH)) {
34526ac08709SAl Viro 		error = do_o_path(nd, flags, file);
34535f336e72SAl Viro 	} else {
34545f336e72SAl Viro 		const char *s = path_init(nd, flags);
34553bdba28bSAl Viro 		while (!(error = link_path_walk(s, nd)) &&
3456c5971b8cSAl Viro 		       (s = open_last_lookups(nd, file, op)) != NULL)
34571ccac622SAl Viro 			;
3458c5971b8cSAl Viro 		if (!error)
3459c5971b8cSAl Viro 			error = do_open(nd, file, op);
3460deb106c6SAl Viro 		terminate_walk(nd);
34615f336e72SAl Viro 	}
34627c1c01ecSAl Viro 	if (likely(!error)) {
3463aad888f8SAl Viro 		if (likely(file->f_mode & FMODE_OPENED))
34647c1c01ecSAl Viro 			return file;
34657c1c01ecSAl Viro 		WARN_ON(1);
34667c1c01ecSAl Viro 		error = -EINVAL;
3467015c3bbcSMiklos Szeredi 	}
34687c1c01ecSAl Viro 	fput(file);
34692675a4ebSAl Viro 	if (error == -EOPENSTALE) {
34702675a4ebSAl Viro 		if (flags & LOOKUP_RCU)
34712675a4ebSAl Viro 			error = -ECHILD;
34722675a4ebSAl Viro 		else
34732675a4ebSAl Viro 			error = -ESTALE;
34742675a4ebSAl Viro 	}
34757c1c01ecSAl Viro 	return ERR_PTR(error);
3476de459215SKirill Korotaev }
34771da177e4SLinus Torvalds 
3478669abf4eSJeff Layton struct file *do_filp_open(int dfd, struct filename *pathname,
3479f9652e10SAl Viro 		const struct open_flags *op)
348013aab428SAl Viro {
34819883d185SAl Viro 	struct nameidata nd;
3482f9652e10SAl Viro 	int flags = op->lookup_flags;
348313aab428SAl Viro 	struct file *filp;
348413aab428SAl Viro 
34859883d185SAl Viro 	set_nameidata(&nd, dfd, pathname);
3486c8a53ee5SAl Viro 	filp = path_openat(&nd, op, flags | LOOKUP_RCU);
348713aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
3488c8a53ee5SAl Viro 		filp = path_openat(&nd, op, flags);
348913aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
3490c8a53ee5SAl Viro 		filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
34919883d185SAl Viro 	restore_nameidata();
349213aab428SAl Viro 	return filp;
349313aab428SAl Viro }
349413aab428SAl Viro 
349573d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3496f9652e10SAl Viro 		const char *name, const struct open_flags *op)
349773d049a4SAl Viro {
34989883d185SAl Viro 	struct nameidata nd;
349973d049a4SAl Viro 	struct file *file;
350051689104SPaul Moore 	struct filename *filename;
3501f9652e10SAl Viro 	int flags = op->lookup_flags | LOOKUP_ROOT;
350273d049a4SAl Viro 
350373d049a4SAl Viro 	nd.root.mnt = mnt;
350473d049a4SAl Viro 	nd.root.dentry = dentry;
350573d049a4SAl Viro 
3506b18825a7SDavid Howells 	if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
350773d049a4SAl Viro 		return ERR_PTR(-ELOOP);
350873d049a4SAl Viro 
350951689104SPaul Moore 	filename = getname_kernel(name);
3510a1c83681SViresh Kumar 	if (IS_ERR(filename))
351151689104SPaul Moore 		return ERR_CAST(filename);
351251689104SPaul Moore 
35139883d185SAl Viro 	set_nameidata(&nd, -1, filename);
3514c8a53ee5SAl Viro 	file = path_openat(&nd, op, flags | LOOKUP_RCU);
351573d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
3516c8a53ee5SAl Viro 		file = path_openat(&nd, op, flags);
351773d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
3518c8a53ee5SAl Viro 		file = path_openat(&nd, op, flags | LOOKUP_REVAL);
35199883d185SAl Viro 	restore_nameidata();
352051689104SPaul Moore 	putname(filename);
352173d049a4SAl Viro 	return file;
352273d049a4SAl Viro }
352373d049a4SAl Viro 
3524fa14a0b8SAl Viro static struct dentry *filename_create(int dfd, struct filename *name,
35251ac12b4bSJeff Layton 				struct path *path, unsigned int lookup_flags)
35261da177e4SLinus Torvalds {
3527c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
3528391172c4SAl Viro 	struct qstr last;
3529391172c4SAl Viro 	int type;
3530c30dabfeSJan Kara 	int err2;
35311ac12b4bSJeff Layton 	int error;
35321ac12b4bSJeff Layton 	bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
35331ac12b4bSJeff Layton 
35341ac12b4bSJeff Layton 	/*
35351ac12b4bSJeff Layton 	 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
35361ac12b4bSJeff Layton 	 * other flags passed in are ignored!
35371ac12b4bSJeff Layton 	 */
35381ac12b4bSJeff Layton 	lookup_flags &= LOOKUP_REVAL;
35391ac12b4bSJeff Layton 
35405c31b6ceSAl Viro 	name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
35415c31b6ceSAl Viro 	if (IS_ERR(name))
35425c31b6ceSAl Viro 		return ERR_CAST(name);
35431da177e4SLinus Torvalds 
3544c663e5d8SChristoph Hellwig 	/*
3545c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
3546c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
3547c663e5d8SChristoph Hellwig 	 */
35485c31b6ceSAl Viro 	if (unlikely(type != LAST_NORM))
3549ed75e95dSAl Viro 		goto out;
3550c663e5d8SChristoph Hellwig 
3551c30dabfeSJan Kara 	/* don't fail immediately if it's r/o, at least try to report other errors */
3552391172c4SAl Viro 	err2 = mnt_want_write(path->mnt);
3553c663e5d8SChristoph Hellwig 	/*
3554c663e5d8SChristoph Hellwig 	 * Do the final lookup.
3555c663e5d8SChristoph Hellwig 	 */
3556391172c4SAl Viro 	lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
35575955102cSAl Viro 	inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
3558391172c4SAl Viro 	dentry = __lookup_hash(&last, path->dentry, lookup_flags);
35591da177e4SLinus Torvalds 	if (IS_ERR(dentry))
3560a8104a9fSAl Viro 		goto unlock;
3561c663e5d8SChristoph Hellwig 
3562a8104a9fSAl Viro 	error = -EEXIST;
3563b18825a7SDavid Howells 	if (d_is_positive(dentry))
3564a8104a9fSAl Viro 		goto fail;
3565b18825a7SDavid Howells 
3566c663e5d8SChristoph Hellwig 	/*
3567c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
3568c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
3569c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
3570c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
3571c663e5d8SChristoph Hellwig 	 */
3572391172c4SAl Viro 	if (unlikely(!is_dir && last.name[last.len])) {
3573a8104a9fSAl Viro 		error = -ENOENT;
3574ed75e95dSAl Viro 		goto fail;
3575e9baf6e5SAl Viro 	}
3576c30dabfeSJan Kara 	if (unlikely(err2)) {
3577c30dabfeSJan Kara 		error = err2;
3578a8104a9fSAl Viro 		goto fail;
3579c30dabfeSJan Kara 	}
3580181c37b6SAl Viro 	putname(name);
3581e9baf6e5SAl Viro 	return dentry;
35821da177e4SLinus Torvalds fail:
3583a8104a9fSAl Viro 	dput(dentry);
3584a8104a9fSAl Viro 	dentry = ERR_PTR(error);
3585a8104a9fSAl Viro unlock:
35865955102cSAl Viro 	inode_unlock(path->dentry->d_inode);
3587c30dabfeSJan Kara 	if (!err2)
3588391172c4SAl Viro 		mnt_drop_write(path->mnt);
3589ed75e95dSAl Viro out:
3590391172c4SAl Viro 	path_put(path);
3591181c37b6SAl Viro 	putname(name);
3592ed75e95dSAl Viro 	return dentry;
3593dae6ad8fSAl Viro }
3594fa14a0b8SAl Viro 
3595fa14a0b8SAl Viro struct dentry *kern_path_create(int dfd, const char *pathname,
3596fa14a0b8SAl Viro 				struct path *path, unsigned int lookup_flags)
3597fa14a0b8SAl Viro {
3598181c37b6SAl Viro 	return filename_create(dfd, getname_kernel(pathname),
3599181c37b6SAl Viro 				path, lookup_flags);
3600fa14a0b8SAl Viro }
3601dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
3602dae6ad8fSAl Viro 
3603921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry)
3604921a1650SAl Viro {
3605921a1650SAl Viro 	dput(dentry);
36065955102cSAl Viro 	inode_unlock(path->dentry->d_inode);
3607a8104a9fSAl Viro 	mnt_drop_write(path->mnt);
3608921a1650SAl Viro 	path_put(path);
3609921a1650SAl Viro }
3610921a1650SAl Viro EXPORT_SYMBOL(done_path_create);
3611921a1650SAl Viro 
3612520ae687SAl Viro inline struct dentry *user_path_create(int dfd, const char __user *pathname,
36131ac12b4bSJeff Layton 				struct path *path, unsigned int lookup_flags)
3614dae6ad8fSAl Viro {
3615181c37b6SAl Viro 	return filename_create(dfd, getname(pathname), path, lookup_flags);
3616dae6ad8fSAl Viro }
3617dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
3618dae6ad8fSAl Viro 
36191a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
36201da177e4SLinus Torvalds {
3621a3c751a5SMiklos Szeredi 	bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV;
3622*ba73d987SChristian Brauner 	int error = may_create(&init_user_ns, dir, dentry);
36231da177e4SLinus Torvalds 
36241da177e4SLinus Torvalds 	if (error)
36251da177e4SLinus Torvalds 		return error;
36261da177e4SLinus Torvalds 
3627a3c751a5SMiklos Szeredi 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !is_whiteout &&
3628a3c751a5SMiklos Szeredi 	    !capable(CAP_MKNOD))
36291da177e4SLinus Torvalds 		return -EPERM;
36301da177e4SLinus Torvalds 
3631acfa4380SAl Viro 	if (!dir->i_op->mknod)
36321da177e4SLinus Torvalds 		return -EPERM;
36331da177e4SLinus Torvalds 
363408ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
363508ce5f16SSerge E. Hallyn 	if (error)
363608ce5f16SSerge E. Hallyn 		return error;
363708ce5f16SSerge E. Hallyn 
36381da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
36391da177e4SLinus Torvalds 	if (error)
36401da177e4SLinus Torvalds 		return error;
36411da177e4SLinus Torvalds 
36421da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
3643a74574aaSStephen Smalley 	if (!error)
3644f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
36451da177e4SLinus Torvalds 	return error;
36461da177e4SLinus Torvalds }
36474d359507SAl Viro EXPORT_SYMBOL(vfs_mknod);
36481da177e4SLinus Torvalds 
3649f69aac00SAl Viro static int may_mknod(umode_t mode)
3650463c3197SDave Hansen {
3651463c3197SDave Hansen 	switch (mode & S_IFMT) {
3652463c3197SDave Hansen 	case S_IFREG:
3653463c3197SDave Hansen 	case S_IFCHR:
3654463c3197SDave Hansen 	case S_IFBLK:
3655463c3197SDave Hansen 	case S_IFIFO:
3656463c3197SDave Hansen 	case S_IFSOCK:
3657463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
3658463c3197SDave Hansen 		return 0;
3659463c3197SDave Hansen 	case S_IFDIR:
3660463c3197SDave Hansen 		return -EPERM;
3661463c3197SDave Hansen 	default:
3662463c3197SDave Hansen 		return -EINVAL;
3663463c3197SDave Hansen 	}
3664463c3197SDave Hansen }
3665463c3197SDave Hansen 
36665fee64fcSChristoph Hellwig static long do_mknodat(int dfd, const char __user *filename, umode_t mode,
366787c4e192SDominik Brodowski 		unsigned int dev)
36681da177e4SLinus Torvalds {
36691da177e4SLinus Torvalds 	struct dentry *dentry;
3670dae6ad8fSAl Viro 	struct path path;
3671dae6ad8fSAl Viro 	int error;
3672972567f1SJeff Layton 	unsigned int lookup_flags = 0;
36731da177e4SLinus Torvalds 
36748e4bfca1SAl Viro 	error = may_mknod(mode);
36758e4bfca1SAl Viro 	if (error)
36768e4bfca1SAl Viro 		return error;
3677972567f1SJeff Layton retry:
3678972567f1SJeff Layton 	dentry = user_path_create(dfd, filename, &path, lookup_flags);
3679dae6ad8fSAl Viro 	if (IS_ERR(dentry))
3680dae6ad8fSAl Viro 		return PTR_ERR(dentry);
36812ad94ae6SAl Viro 
3682dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3683ce3b0f8dSAl Viro 		mode &= ~current_umask();
3684dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
3685be6d3e56SKentaro Takeda 	if (error)
3686a8104a9fSAl Viro 		goto out;
36871da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
36881da177e4SLinus Torvalds 		case 0: case S_IFREG:
3689312b63fbSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,true);
369005d1a717SMimi Zohar 			if (!error)
369105d1a717SMimi Zohar 				ima_post_path_mknod(dentry);
36921da177e4SLinus Torvalds 			break;
36931da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
3694dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
36951da177e4SLinus Torvalds 					new_decode_dev(dev));
36961da177e4SLinus Torvalds 			break;
36971da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
3698dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
36991da177e4SLinus Torvalds 			break;
37001da177e4SLinus Torvalds 	}
3701a8104a9fSAl Viro out:
3702921a1650SAl Viro 	done_path_create(&path, dentry);
3703972567f1SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3704972567f1SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3705972567f1SJeff Layton 		goto retry;
3706972567f1SJeff Layton 	}
37071da177e4SLinus Torvalds 	return error;
37081da177e4SLinus Torvalds }
37091da177e4SLinus Torvalds 
371087c4e192SDominik Brodowski SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
371187c4e192SDominik Brodowski 		unsigned int, dev)
371287c4e192SDominik Brodowski {
371387c4e192SDominik Brodowski 	return do_mknodat(dfd, filename, mode, dev);
371487c4e192SDominik Brodowski }
371587c4e192SDominik Brodowski 
37168208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
37175590ff0dSUlrich Drepper {
371887c4e192SDominik Brodowski 	return do_mknodat(AT_FDCWD, filename, mode, dev);
37195590ff0dSUlrich Drepper }
37205590ff0dSUlrich Drepper 
372118bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
37221da177e4SLinus Torvalds {
3723*ba73d987SChristian Brauner 	int error = may_create(&init_user_ns, dir, dentry);
37248de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
37251da177e4SLinus Torvalds 
37261da177e4SLinus Torvalds 	if (error)
37271da177e4SLinus Torvalds 		return error;
37281da177e4SLinus Torvalds 
3729acfa4380SAl Viro 	if (!dir->i_op->mkdir)
37301da177e4SLinus Torvalds 		return -EPERM;
37311da177e4SLinus Torvalds 
37321da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
37331da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
37341da177e4SLinus Torvalds 	if (error)
37351da177e4SLinus Torvalds 		return error;
37361da177e4SLinus Torvalds 
37378de52778SAl Viro 	if (max_links && dir->i_nlink >= max_links)
37388de52778SAl Viro 		return -EMLINK;
37398de52778SAl Viro 
37401da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
3741a74574aaSStephen Smalley 	if (!error)
3742f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
37431da177e4SLinus Torvalds 	return error;
37441da177e4SLinus Torvalds }
37454d359507SAl Viro EXPORT_SYMBOL(vfs_mkdir);
37461da177e4SLinus Torvalds 
374783ff98c3SChristoph Hellwig static long do_mkdirat(int dfd, const char __user *pathname, umode_t mode)
37481da177e4SLinus Torvalds {
37496902d925SDave Hansen 	struct dentry *dentry;
3750dae6ad8fSAl Viro 	struct path path;
3751dae6ad8fSAl Viro 	int error;
3752b76d8b82SJeff Layton 	unsigned int lookup_flags = LOOKUP_DIRECTORY;
37531da177e4SLinus Torvalds 
3754b76d8b82SJeff Layton retry:
3755b76d8b82SJeff Layton 	dentry = user_path_create(dfd, pathname, &path, lookup_flags);
37566902d925SDave Hansen 	if (IS_ERR(dentry))
3757dae6ad8fSAl Viro 		return PTR_ERR(dentry);
37586902d925SDave Hansen 
3759dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3760ce3b0f8dSAl Viro 		mode &= ~current_umask();
3761dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
3762a8104a9fSAl Viro 	if (!error)
3763dae6ad8fSAl Viro 		error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3764921a1650SAl Viro 	done_path_create(&path, dentry);
3765b76d8b82SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3766b76d8b82SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3767b76d8b82SJeff Layton 		goto retry;
3768b76d8b82SJeff Layton 	}
37691da177e4SLinus Torvalds 	return error;
37701da177e4SLinus Torvalds }
37711da177e4SLinus Torvalds 
37720101db7aSDominik Brodowski SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
37730101db7aSDominik Brodowski {
37740101db7aSDominik Brodowski 	return do_mkdirat(dfd, pathname, mode);
37750101db7aSDominik Brodowski }
37760101db7aSDominik Brodowski 
3777a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
37785590ff0dSUlrich Drepper {
37790101db7aSDominik Brodowski 	return do_mkdirat(AT_FDCWD, pathname, mode);
37805590ff0dSUlrich Drepper }
37815590ff0dSUlrich Drepper 
37821da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
37831da177e4SLinus Torvalds {
3784*ba73d987SChristian Brauner 	int error = may_delete(&init_user_ns, dir, dentry, 1);
37851da177e4SLinus Torvalds 
37861da177e4SLinus Torvalds 	if (error)
37871da177e4SLinus Torvalds 		return error;
37881da177e4SLinus Torvalds 
3789acfa4380SAl Viro 	if (!dir->i_op->rmdir)
37901da177e4SLinus Torvalds 		return -EPERM;
37911da177e4SLinus Torvalds 
37921d2ef590SAl Viro 	dget(dentry);
37935955102cSAl Viro 	inode_lock(dentry->d_inode);
3794912dbc15SSage Weil 
37951da177e4SLinus Torvalds 	error = -EBUSY;
37967af1364fSEric W. Biederman 	if (is_local_mountpoint(dentry))
3797912dbc15SSage Weil 		goto out;
3798912dbc15SSage Weil 
37991da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
3800912dbc15SSage Weil 	if (error)
3801912dbc15SSage Weil 		goto out;
3802912dbc15SSage Weil 
38031da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
3804912dbc15SSage Weil 	if (error)
3805912dbc15SSage Weil 		goto out;
3806912dbc15SSage Weil 
38078767712fSAl Viro 	shrink_dcache_parent(dentry);
38081da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
3809d83c49f3SAl Viro 	dont_mount(dentry);
38108ed936b5SEric W. Biederman 	detach_mounts(dentry);
3811116b9731SAmir Goldstein 	fsnotify_rmdir(dir, dentry);
38121da177e4SLinus Torvalds 
3813912dbc15SSage Weil out:
38145955102cSAl Viro 	inode_unlock(dentry->d_inode);
38151d2ef590SAl Viro 	dput(dentry);
3816912dbc15SSage Weil 	if (!error)
3817912dbc15SSage Weil 		d_delete(dentry);
38181da177e4SLinus Torvalds 	return error;
38191da177e4SLinus Torvalds }
38204d359507SAl Viro EXPORT_SYMBOL(vfs_rmdir);
38211da177e4SLinus Torvalds 
3822e24ab0efSChristoph Hellwig long do_rmdir(int dfd, struct filename *name)
38231da177e4SLinus Torvalds {
38241da177e4SLinus Torvalds 	int error = 0;
38251da177e4SLinus Torvalds 	struct dentry *dentry;
3826f5beed75SAl Viro 	struct path path;
3827f5beed75SAl Viro 	struct qstr last;
3828f5beed75SAl Viro 	int type;
3829c6ee9206SJeff Layton 	unsigned int lookup_flags = 0;
3830c6ee9206SJeff Layton retry:
3831e24ab0efSChristoph Hellwig 	name = filename_parentat(dfd, name, lookup_flags,
3832c1d4dd27SAl Viro 				&path, &last, &type);
383391a27b2aSJeff Layton 	if (IS_ERR(name))
383491a27b2aSJeff Layton 		return PTR_ERR(name);
38351da177e4SLinus Torvalds 
3836f5beed75SAl Viro 	switch (type) {
38371da177e4SLinus Torvalds 	case LAST_DOTDOT:
38381da177e4SLinus Torvalds 		error = -ENOTEMPTY;
38391da177e4SLinus Torvalds 		goto exit1;
38401da177e4SLinus Torvalds 	case LAST_DOT:
38411da177e4SLinus Torvalds 		error = -EINVAL;
38421da177e4SLinus Torvalds 		goto exit1;
38431da177e4SLinus Torvalds 	case LAST_ROOT:
38441da177e4SLinus Torvalds 		error = -EBUSY;
38451da177e4SLinus Torvalds 		goto exit1;
38461da177e4SLinus Torvalds 	}
38470612d9fbSOGAWA Hirofumi 
3848f5beed75SAl Viro 	error = mnt_want_write(path.mnt);
3849c30dabfeSJan Kara 	if (error)
3850c30dabfeSJan Kara 		goto exit1;
38510612d9fbSOGAWA Hirofumi 
38525955102cSAl Viro 	inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
3853f5beed75SAl Viro 	dentry = __lookup_hash(&last, path.dentry, lookup_flags);
38541da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
38556902d925SDave Hansen 	if (IS_ERR(dentry))
38566902d925SDave Hansen 		goto exit2;
3857e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
3858e6bc45d6STheodore Ts'o 		error = -ENOENT;
3859e6bc45d6STheodore Ts'o 		goto exit3;
3860e6bc45d6STheodore Ts'o 	}
3861f5beed75SAl Viro 	error = security_path_rmdir(&path, dentry);
3862be6d3e56SKentaro Takeda 	if (error)
3863c30dabfeSJan Kara 		goto exit3;
3864f5beed75SAl Viro 	error = vfs_rmdir(path.dentry->d_inode, dentry);
38650622753bSDave Hansen exit3:
38661da177e4SLinus Torvalds 	dput(dentry);
38676902d925SDave Hansen exit2:
38685955102cSAl Viro 	inode_unlock(path.dentry->d_inode);
3869f5beed75SAl Viro 	mnt_drop_write(path.mnt);
38701da177e4SLinus Torvalds exit1:
3871f5beed75SAl Viro 	path_put(&path);
3872c6ee9206SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3873c6ee9206SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3874c6ee9206SJeff Layton 		goto retry;
3875c6ee9206SJeff Layton 	}
387624fb33d4SAl Viro 	putname(name);
38771da177e4SLinus Torvalds 	return error;
38781da177e4SLinus Torvalds }
38791da177e4SLinus Torvalds 
38803cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
38815590ff0dSUlrich Drepper {
3882e24ab0efSChristoph Hellwig 	return do_rmdir(AT_FDCWD, getname(pathname));
38835590ff0dSUlrich Drepper }
38845590ff0dSUlrich Drepper 
3885b21996e3SJ. Bruce Fields /**
3886b21996e3SJ. Bruce Fields  * vfs_unlink - unlink a filesystem object
3887b21996e3SJ. Bruce Fields  * @dir:	parent directory
3888b21996e3SJ. Bruce Fields  * @dentry:	victim
3889b21996e3SJ. Bruce Fields  * @delegated_inode: returns victim inode, if the inode is delegated.
3890b21996e3SJ. Bruce Fields  *
3891b21996e3SJ. Bruce Fields  * The caller must hold dir->i_mutex.
3892b21996e3SJ. Bruce Fields  *
3893b21996e3SJ. Bruce Fields  * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3894b21996e3SJ. Bruce Fields  * return a reference to the inode in delegated_inode.  The caller
3895b21996e3SJ. Bruce Fields  * should then break the delegation on that inode and retry.  Because
3896b21996e3SJ. Bruce Fields  * breaking a delegation may take a long time, the caller should drop
3897b21996e3SJ. Bruce Fields  * dir->i_mutex before doing so.
3898b21996e3SJ. Bruce Fields  *
3899b21996e3SJ. Bruce Fields  * Alternatively, a caller may pass NULL for delegated_inode.  This may
3900b21996e3SJ. Bruce Fields  * be appropriate for callers that expect the underlying filesystem not
3901b21996e3SJ. Bruce Fields  * to be NFS exported.
3902b21996e3SJ. Bruce Fields  */
3903b21996e3SJ. Bruce Fields int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
39041da177e4SLinus Torvalds {
39059accbb97SJ. Bruce Fields 	struct inode *target = dentry->d_inode;
3906*ba73d987SChristian Brauner 	int error = may_delete(&init_user_ns, dir, dentry, 0);
39071da177e4SLinus Torvalds 
39081da177e4SLinus Torvalds 	if (error)
39091da177e4SLinus Torvalds 		return error;
39101da177e4SLinus Torvalds 
3911acfa4380SAl Viro 	if (!dir->i_op->unlink)
39121da177e4SLinus Torvalds 		return -EPERM;
39131da177e4SLinus Torvalds 
39145955102cSAl Viro 	inode_lock(target);
39158ed936b5SEric W. Biederman 	if (is_local_mountpoint(dentry))
39161da177e4SLinus Torvalds 		error = -EBUSY;
39171da177e4SLinus Torvalds 	else {
39181da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
3919bec1052eSAl Viro 		if (!error) {
39205a14696cSJ. Bruce Fields 			error = try_break_deleg(target, delegated_inode);
39215a14696cSJ. Bruce Fields 			if (error)
3922b21996e3SJ. Bruce Fields 				goto out;
39231da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
39248ed936b5SEric W. Biederman 			if (!error) {
3925d83c49f3SAl Viro 				dont_mount(dentry);
39268ed936b5SEric W. Biederman 				detach_mounts(dentry);
3927116b9731SAmir Goldstein 				fsnotify_unlink(dir, dentry);
39288ed936b5SEric W. Biederman 			}
3929bec1052eSAl Viro 		}
39301da177e4SLinus Torvalds 	}
3931b21996e3SJ. Bruce Fields out:
39325955102cSAl Viro 	inode_unlock(target);
39331da177e4SLinus Torvalds 
39341da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
39351da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
39369accbb97SJ. Bruce Fields 		fsnotify_link_count(target);
39371da177e4SLinus Torvalds 		d_delete(dentry);
39381da177e4SLinus Torvalds 	}
39390eeca283SRobert Love 
39401da177e4SLinus Torvalds 	return error;
39411da177e4SLinus Torvalds }
39424d359507SAl Viro EXPORT_SYMBOL(vfs_unlink);
39431da177e4SLinus Torvalds 
39441da177e4SLinus Torvalds /*
39451da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
39461b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
39471da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
39481da177e4SLinus Torvalds  * while waiting on the I/O.
39491da177e4SLinus Torvalds  */
3950da2f1362SChristoph Hellwig long do_unlinkat(int dfd, struct filename *name)
39511da177e4SLinus Torvalds {
39522ad94ae6SAl Viro 	int error;
39531da177e4SLinus Torvalds 	struct dentry *dentry;
3954f5beed75SAl Viro 	struct path path;
3955f5beed75SAl Viro 	struct qstr last;
3956f5beed75SAl Viro 	int type;
39571da177e4SLinus Torvalds 	struct inode *inode = NULL;
3958b21996e3SJ. Bruce Fields 	struct inode *delegated_inode = NULL;
39595d18f813SJeff Layton 	unsigned int lookup_flags = 0;
39605d18f813SJeff Layton retry:
3961da2f1362SChristoph Hellwig 	name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
396291a27b2aSJeff Layton 	if (IS_ERR(name))
396391a27b2aSJeff Layton 		return PTR_ERR(name);
39642ad94ae6SAl Viro 
39651da177e4SLinus Torvalds 	error = -EISDIR;
3966f5beed75SAl Viro 	if (type != LAST_NORM)
39671da177e4SLinus Torvalds 		goto exit1;
39680612d9fbSOGAWA Hirofumi 
3969f5beed75SAl Viro 	error = mnt_want_write(path.mnt);
3970c30dabfeSJan Kara 	if (error)
3971c30dabfeSJan Kara 		goto exit1;
3972b21996e3SJ. Bruce Fields retry_deleg:
39735955102cSAl Viro 	inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
3974f5beed75SAl Viro 	dentry = __lookup_hash(&last, path.dentry, lookup_flags);
39751da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
39761da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
39771da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
3978f5beed75SAl Viro 		if (last.name[last.len])
397950338b88STörök Edwin 			goto slashes;
39801da177e4SLinus Torvalds 		inode = dentry->d_inode;
3981b18825a7SDavid Howells 		if (d_is_negative(dentry))
3982e6bc45d6STheodore Ts'o 			goto slashes;
39837de9c6eeSAl Viro 		ihold(inode);
3984f5beed75SAl Viro 		error = security_path_unlink(&path, dentry);
3985be6d3e56SKentaro Takeda 		if (error)
3986c30dabfeSJan Kara 			goto exit2;
3987f5beed75SAl Viro 		error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
39881da177e4SLinus Torvalds exit2:
39891da177e4SLinus Torvalds 		dput(dentry);
39901da177e4SLinus Torvalds 	}
39915955102cSAl Viro 	inode_unlock(path.dentry->d_inode);
39921da177e4SLinus Torvalds 	if (inode)
39931da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
3994b21996e3SJ. Bruce Fields 	inode = NULL;
3995b21996e3SJ. Bruce Fields 	if (delegated_inode) {
39965a14696cSJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
3997b21996e3SJ. Bruce Fields 		if (!error)
3998b21996e3SJ. Bruce Fields 			goto retry_deleg;
3999b21996e3SJ. Bruce Fields 	}
4000f5beed75SAl Viro 	mnt_drop_write(path.mnt);
40011da177e4SLinus Torvalds exit1:
4002f5beed75SAl Viro 	path_put(&path);
40035d18f813SJeff Layton 	if (retry_estale(error, lookup_flags)) {
40045d18f813SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
40055d18f813SJeff Layton 		inode = NULL;
40065d18f813SJeff Layton 		goto retry;
40075d18f813SJeff Layton 	}
4008da2f1362SChristoph Hellwig 	putname(name);
40091da177e4SLinus Torvalds 	return error;
40101da177e4SLinus Torvalds 
40111da177e4SLinus Torvalds slashes:
4012b18825a7SDavid Howells 	if (d_is_negative(dentry))
4013b18825a7SDavid Howells 		error = -ENOENT;
401444b1d530SMiklos Szeredi 	else if (d_is_dir(dentry))
4015b18825a7SDavid Howells 		error = -EISDIR;
4016b18825a7SDavid Howells 	else
4017b18825a7SDavid Howells 		error = -ENOTDIR;
40181da177e4SLinus Torvalds 	goto exit2;
40191da177e4SLinus Torvalds }
40201da177e4SLinus Torvalds 
40212e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
40225590ff0dSUlrich Drepper {
40235590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
40245590ff0dSUlrich Drepper 		return -EINVAL;
40255590ff0dSUlrich Drepper 
40265590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
4027e24ab0efSChristoph Hellwig 		return do_rmdir(dfd, getname(pathname));
4028da2f1362SChristoph Hellwig 	return do_unlinkat(dfd, getname(pathname));
40295590ff0dSUlrich Drepper }
40305590ff0dSUlrich Drepper 
40313480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
40325590ff0dSUlrich Drepper {
4033da2f1362SChristoph Hellwig 	return do_unlinkat(AT_FDCWD, getname(pathname));
40345590ff0dSUlrich Drepper }
40355590ff0dSUlrich Drepper 
4036db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
40371da177e4SLinus Torvalds {
4038*ba73d987SChristian Brauner 	int error = may_create(&init_user_ns, dir, dentry);
40391da177e4SLinus Torvalds 
40401da177e4SLinus Torvalds 	if (error)
40411da177e4SLinus Torvalds 		return error;
40421da177e4SLinus Torvalds 
4043acfa4380SAl Viro 	if (!dir->i_op->symlink)
40441da177e4SLinus Torvalds 		return -EPERM;
40451da177e4SLinus Torvalds 
40461da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
40471da177e4SLinus Torvalds 	if (error)
40481da177e4SLinus Torvalds 		return error;
40491da177e4SLinus Torvalds 
40501da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
4051a74574aaSStephen Smalley 	if (!error)
4052f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
40531da177e4SLinus Torvalds 	return error;
40541da177e4SLinus Torvalds }
40554d359507SAl Viro EXPORT_SYMBOL(vfs_symlink);
40561da177e4SLinus Torvalds 
4057cd3acb6aSChristoph Hellwig static long do_symlinkat(const char __user *oldname, int newdfd,
4058b724e846SDominik Brodowski 		  const char __user *newname)
40591da177e4SLinus Torvalds {
40602ad94ae6SAl Viro 	int error;
406191a27b2aSJeff Layton 	struct filename *from;
40626902d925SDave Hansen 	struct dentry *dentry;
4063dae6ad8fSAl Viro 	struct path path;
4064f46d3567SJeff Layton 	unsigned int lookup_flags = 0;
40651da177e4SLinus Torvalds 
40661da177e4SLinus Torvalds 	from = getname(oldname);
40671da177e4SLinus Torvalds 	if (IS_ERR(from))
40681da177e4SLinus Torvalds 		return PTR_ERR(from);
4069f46d3567SJeff Layton retry:
4070f46d3567SJeff Layton 	dentry = user_path_create(newdfd, newname, &path, lookup_flags);
40711da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
40726902d925SDave Hansen 	if (IS_ERR(dentry))
4073dae6ad8fSAl Viro 		goto out_putname;
40746902d925SDave Hansen 
407591a27b2aSJeff Layton 	error = security_path_symlink(&path, dentry, from->name);
4076a8104a9fSAl Viro 	if (!error)
407791a27b2aSJeff Layton 		error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
4078921a1650SAl Viro 	done_path_create(&path, dentry);
4079f46d3567SJeff Layton 	if (retry_estale(error, lookup_flags)) {
4080f46d3567SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
4081f46d3567SJeff Layton 		goto retry;
4082f46d3567SJeff Layton 	}
40836902d925SDave Hansen out_putname:
40841da177e4SLinus Torvalds 	putname(from);
40851da177e4SLinus Torvalds 	return error;
40861da177e4SLinus Torvalds }
40871da177e4SLinus Torvalds 
4088b724e846SDominik Brodowski SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4089b724e846SDominik Brodowski 		int, newdfd, const char __user *, newname)
4090b724e846SDominik Brodowski {
4091b724e846SDominik Brodowski 	return do_symlinkat(oldname, newdfd, newname);
4092b724e846SDominik Brodowski }
4093b724e846SDominik Brodowski 
40943480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
40955590ff0dSUlrich Drepper {
4096b724e846SDominik Brodowski 	return do_symlinkat(oldname, AT_FDCWD, newname);
40975590ff0dSUlrich Drepper }
40985590ff0dSUlrich Drepper 
4099146a8595SJ. Bruce Fields /**
4100146a8595SJ. Bruce Fields  * vfs_link - create a new link
4101146a8595SJ. Bruce Fields  * @old_dentry:	object to be linked
4102146a8595SJ. Bruce Fields  * @dir:	new parent
4103146a8595SJ. Bruce Fields  * @new_dentry:	where to create the new link
4104146a8595SJ. Bruce Fields  * @delegated_inode: returns inode needing a delegation break
4105146a8595SJ. Bruce Fields  *
4106146a8595SJ. Bruce Fields  * The caller must hold dir->i_mutex
4107146a8595SJ. Bruce Fields  *
4108146a8595SJ. Bruce Fields  * If vfs_link discovers a delegation on the to-be-linked file in need
4109146a8595SJ. Bruce Fields  * of breaking, it will return -EWOULDBLOCK and return a reference to the
4110146a8595SJ. Bruce Fields  * inode in delegated_inode.  The caller should then break the delegation
4111146a8595SJ. Bruce Fields  * and retry.  Because breaking a delegation may take a long time, the
4112146a8595SJ. Bruce Fields  * caller should drop the i_mutex before doing so.
4113146a8595SJ. Bruce Fields  *
4114146a8595SJ. Bruce Fields  * Alternatively, a caller may pass NULL for delegated_inode.  This may
4115146a8595SJ. Bruce Fields  * be appropriate for callers that expect the underlying filesystem not
4116146a8595SJ. Bruce Fields  * to be NFS exported.
4117146a8595SJ. Bruce Fields  */
4118146a8595SJ. Bruce Fields int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
41191da177e4SLinus Torvalds {
41201da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
41218de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
41221da177e4SLinus Torvalds 	int error;
41231da177e4SLinus Torvalds 
41241da177e4SLinus Torvalds 	if (!inode)
41251da177e4SLinus Torvalds 		return -ENOENT;
41261da177e4SLinus Torvalds 
4127*ba73d987SChristian Brauner 	error = may_create(&init_user_ns, dir, new_dentry);
41281da177e4SLinus Torvalds 	if (error)
41291da177e4SLinus Torvalds 		return error;
41301da177e4SLinus Torvalds 
41311da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
41321da177e4SLinus Torvalds 		return -EXDEV;
41331da177e4SLinus Torvalds 
41341da177e4SLinus Torvalds 	/*
41351da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
41361da177e4SLinus Torvalds 	 */
41371da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
41381da177e4SLinus Torvalds 		return -EPERM;
41390bd23d09SEric W. Biederman 	/*
41400bd23d09SEric W. Biederman 	 * Updating the link count will likely cause i_uid and i_gid to
41410bd23d09SEric W. Biederman 	 * be writen back improperly if their true value is unknown to
41420bd23d09SEric W. Biederman 	 * the vfs.
41430bd23d09SEric W. Biederman 	 */
4144*ba73d987SChristian Brauner 	if (HAS_UNMAPPED_ID(&init_user_ns, inode))
41450bd23d09SEric W. Biederman 		return -EPERM;
4146acfa4380SAl Viro 	if (!dir->i_op->link)
41471da177e4SLinus Torvalds 		return -EPERM;
41487e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
41491da177e4SLinus Torvalds 		return -EPERM;
41501da177e4SLinus Torvalds 
41511da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
41521da177e4SLinus Torvalds 	if (error)
41531da177e4SLinus Torvalds 		return error;
41541da177e4SLinus Torvalds 
41555955102cSAl Viro 	inode_lock(inode);
4156aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
4157f4e0c30cSAl Viro 	if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
4158aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
41598de52778SAl Viro 	else if (max_links && inode->i_nlink >= max_links)
41608de52778SAl Viro 		error = -EMLINK;
4161146a8595SJ. Bruce Fields 	else {
4162146a8595SJ. Bruce Fields 		error = try_break_deleg(inode, delegated_inode);
4163146a8595SJ. Bruce Fields 		if (!error)
41641da177e4SLinus Torvalds 			error = dir->i_op->link(old_dentry, dir, new_dentry);
4165146a8595SJ. Bruce Fields 	}
4166f4e0c30cSAl Viro 
4167f4e0c30cSAl Viro 	if (!error && (inode->i_state & I_LINKABLE)) {
4168f4e0c30cSAl Viro 		spin_lock(&inode->i_lock);
4169f4e0c30cSAl Viro 		inode->i_state &= ~I_LINKABLE;
4170f4e0c30cSAl Viro 		spin_unlock(&inode->i_lock);
4171f4e0c30cSAl Viro 	}
41725955102cSAl Viro 	inode_unlock(inode);
4173e31e14ecSStephen Smalley 	if (!error)
41747e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
41751da177e4SLinus Torvalds 	return error;
41761da177e4SLinus Torvalds }
41774d359507SAl Viro EXPORT_SYMBOL(vfs_link);
41781da177e4SLinus Torvalds 
41791da177e4SLinus Torvalds /*
41801da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
41811da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
41821da177e4SLinus Torvalds  * newname.  --KAB
41831da177e4SLinus Torvalds  *
41841da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
41851da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
41861da177e4SLinus Torvalds  * and other special files.  --ADM
41871da177e4SLinus Torvalds  */
4188812931d6SChristoph Hellwig static int do_linkat(int olddfd, const char __user *oldname, int newdfd,
418946ea89ebSDominik Brodowski 	      const char __user *newname, int flags)
41901da177e4SLinus Torvalds {
41911da177e4SLinus Torvalds 	struct dentry *new_dentry;
4192dae6ad8fSAl Viro 	struct path old_path, new_path;
4193146a8595SJ. Bruce Fields 	struct inode *delegated_inode = NULL;
419411a7b371SAneesh Kumar K.V 	int how = 0;
41951da177e4SLinus Torvalds 	int error;
41961da177e4SLinus Torvalds 
419711a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
4198c04030e1SUlrich Drepper 		return -EINVAL;
419911a7b371SAneesh Kumar K.V 	/*
4200f0cc6ffbSLinus Torvalds 	 * To use null names we require CAP_DAC_READ_SEARCH
4201f0cc6ffbSLinus Torvalds 	 * This ensures that not everyone will be able to create
4202f0cc6ffbSLinus Torvalds 	 * handlink using the passed filedescriptor.
420311a7b371SAneesh Kumar K.V 	 */
4204f0cc6ffbSLinus Torvalds 	if (flags & AT_EMPTY_PATH) {
4205f0cc6ffbSLinus Torvalds 		if (!capable(CAP_DAC_READ_SEARCH))
4206f0cc6ffbSLinus Torvalds 			return -ENOENT;
420711a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
4208f0cc6ffbSLinus Torvalds 	}
4209c04030e1SUlrich Drepper 
421011a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
421111a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
4212442e31caSJeff Layton retry:
421311a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
42141da177e4SLinus Torvalds 	if (error)
42152ad94ae6SAl Viro 		return error;
42162ad94ae6SAl Viro 
4217442e31caSJeff Layton 	new_dentry = user_path_create(newdfd, newname, &new_path,
4218442e31caSJeff Layton 					(how & LOOKUP_REVAL));
42191da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
42206902d925SDave Hansen 	if (IS_ERR(new_dentry))
4221dae6ad8fSAl Viro 		goto out;
4222dae6ad8fSAl Viro 
4223dae6ad8fSAl Viro 	error = -EXDEV;
4224dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
4225dae6ad8fSAl Viro 		goto out_dput;
4226*ba73d987SChristian Brauner 	error = may_linkat(&init_user_ns, &old_path);
4227800179c9SKees Cook 	if (unlikely(error))
4228800179c9SKees Cook 		goto out_dput;
4229dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
4230be6d3e56SKentaro Takeda 	if (error)
4231a8104a9fSAl Viro 		goto out_dput;
4232146a8595SJ. Bruce Fields 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
423375c3f29dSDave Hansen out_dput:
4234921a1650SAl Viro 	done_path_create(&new_path, new_dentry);
4235146a8595SJ. Bruce Fields 	if (delegated_inode) {
4236146a8595SJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
4237d22e6338SOleg Drokin 		if (!error) {
4238d22e6338SOleg Drokin 			path_put(&old_path);
4239146a8595SJ. Bruce Fields 			goto retry;
4240146a8595SJ. Bruce Fields 		}
4241d22e6338SOleg Drokin 	}
4242442e31caSJeff Layton 	if (retry_estale(error, how)) {
4243d22e6338SOleg Drokin 		path_put(&old_path);
4244442e31caSJeff Layton 		how |= LOOKUP_REVAL;
4245442e31caSJeff Layton 		goto retry;
4246442e31caSJeff Layton 	}
42471da177e4SLinus Torvalds out:
42482d8f3038SAl Viro 	path_put(&old_path);
42491da177e4SLinus Torvalds 
42501da177e4SLinus Torvalds 	return error;
42511da177e4SLinus Torvalds }
42521da177e4SLinus Torvalds 
425346ea89ebSDominik Brodowski SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
425446ea89ebSDominik Brodowski 		int, newdfd, const char __user *, newname, int, flags)
425546ea89ebSDominik Brodowski {
425646ea89ebSDominik Brodowski 	return do_linkat(olddfd, oldname, newdfd, newname, flags);
425746ea89ebSDominik Brodowski }
425846ea89ebSDominik Brodowski 
42593480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
42605590ff0dSUlrich Drepper {
426146ea89ebSDominik Brodowski 	return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
42625590ff0dSUlrich Drepper }
42635590ff0dSUlrich Drepper 
4264bc27027aSMiklos Szeredi /**
4265bc27027aSMiklos Szeredi  * vfs_rename - rename a filesystem object
4266bc27027aSMiklos Szeredi  * @old_dir:	parent of source
4267bc27027aSMiklos Szeredi  * @old_dentry:	source
4268bc27027aSMiklos Szeredi  * @new_dir:	parent of destination
4269bc27027aSMiklos Szeredi  * @new_dentry:	destination
4270bc27027aSMiklos Szeredi  * @delegated_inode: returns an inode needing a delegation break
4271520c8b16SMiklos Szeredi  * @flags:	rename flags
4272bc27027aSMiklos Szeredi  *
4273bc27027aSMiklos Szeredi  * The caller must hold multiple mutexes--see lock_rename()).
4274bc27027aSMiklos Szeredi  *
4275bc27027aSMiklos Szeredi  * If vfs_rename discovers a delegation in need of breaking at either
4276bc27027aSMiklos Szeredi  * the source or destination, it will return -EWOULDBLOCK and return a
4277bc27027aSMiklos Szeredi  * reference to the inode in delegated_inode.  The caller should then
4278bc27027aSMiklos Szeredi  * break the delegation and retry.  Because breaking a delegation may
4279bc27027aSMiklos Szeredi  * take a long time, the caller should drop all locks before doing
4280bc27027aSMiklos Szeredi  * so.
4281bc27027aSMiklos Szeredi  *
4282bc27027aSMiklos Szeredi  * Alternatively, a caller may pass NULL for delegated_inode.  This may
4283bc27027aSMiklos Szeredi  * be appropriate for callers that expect the underlying filesystem not
4284bc27027aSMiklos Szeredi  * to be NFS exported.
4285bc27027aSMiklos Szeredi  *
42861da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
42871da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
42881da177e4SLinus Torvalds  * Problems:
42890117d427SMauro Carvalho Chehab  *
4290d03b29a2SJ. Bruce Fields  *	a) we can get into loop creation.
42911da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
42921da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
4293a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
42941da177e4SLinus Torvalds  *	   story.
42956cedba89SJ. Bruce Fields  *	c) we have to lock _four_ objects - parents and victim (if it exists),
42966cedba89SJ. Bruce Fields  *	   and source (if it is not a directory).
42971b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
42981da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
42991da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
4300a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
43011da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
43021da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
43031da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
4304a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
43051da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
43061da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
43071da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
4308e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
43091b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
43101da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
4311c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
43121da177e4SLinus Torvalds  *	   locking].
43131da177e4SLinus Torvalds  */
43141da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
43158e6d782cSJ. Bruce Fields 	       struct inode *new_dir, struct dentry *new_dentry,
4316520c8b16SMiklos Szeredi 	       struct inode **delegated_inode, unsigned int flags)
43171da177e4SLinus Torvalds {
43181da177e4SLinus Torvalds 	int error;
4319*ba73d987SChristian Brauner 	struct user_namespace *mnt_userns = &init_user_ns;
4320bc27027aSMiklos Szeredi 	bool is_dir = d_is_dir(old_dentry);
4321bc27027aSMiklos Szeredi 	struct inode *source = old_dentry->d_inode;
4322bc27027aSMiklos Szeredi 	struct inode *target = new_dentry->d_inode;
4323da1ce067SMiklos Szeredi 	bool new_is_dir = false;
4324da1ce067SMiklos Szeredi 	unsigned max_links = new_dir->i_sb->s_max_links;
432549d31c2fSAl Viro 	struct name_snapshot old_name;
43261da177e4SLinus Torvalds 
43278d3e2936SMiklos Szeredi 	if (source == target)
43281da177e4SLinus Torvalds 		return 0;
43291da177e4SLinus Torvalds 
4330*ba73d987SChristian Brauner 	error = may_delete(mnt_userns, old_dir, old_dentry, is_dir);
43311da177e4SLinus Torvalds 	if (error)
43321da177e4SLinus Torvalds 		return error;
43331da177e4SLinus Torvalds 
4334da1ce067SMiklos Szeredi 	if (!target) {
4335*ba73d987SChristian Brauner 		error = may_create(mnt_userns, new_dir, new_dentry);
4336da1ce067SMiklos Szeredi 	} else {
4337da1ce067SMiklos Szeredi 		new_is_dir = d_is_dir(new_dentry);
4338da1ce067SMiklos Szeredi 
4339da1ce067SMiklos Szeredi 		if (!(flags & RENAME_EXCHANGE))
4340*ba73d987SChristian Brauner 			error = may_delete(mnt_userns, new_dir, new_dentry, is_dir);
4341da1ce067SMiklos Szeredi 		else
4342*ba73d987SChristian Brauner 			error = may_delete(mnt_userns, new_dir, new_dentry, new_is_dir);
4343da1ce067SMiklos Szeredi 	}
43441da177e4SLinus Torvalds 	if (error)
43451da177e4SLinus Torvalds 		return error;
43461da177e4SLinus Torvalds 
43472773bf00SMiklos Szeredi 	if (!old_dir->i_op->rename)
43481da177e4SLinus Torvalds 		return -EPERM;
43491da177e4SLinus Torvalds 
4350bc27027aSMiklos Szeredi 	/*
4351bc27027aSMiklos Szeredi 	 * If we are going to change the parent - check write permissions,
4352bc27027aSMiklos Szeredi 	 * we'll need to flip '..'.
4353bc27027aSMiklos Szeredi 	 */
4354da1ce067SMiklos Szeredi 	if (new_dir != old_dir) {
4355da1ce067SMiklos Szeredi 		if (is_dir) {
435647291baaSChristian Brauner 			error = inode_permission(&init_user_ns, source,
435747291baaSChristian Brauner 						 MAY_WRITE);
4358bc27027aSMiklos Szeredi 			if (error)
4359bc27027aSMiklos Szeredi 				return error;
4360bc27027aSMiklos Szeredi 		}
4361da1ce067SMiklos Szeredi 		if ((flags & RENAME_EXCHANGE) && new_is_dir) {
436247291baaSChristian Brauner 			error = inode_permission(&init_user_ns, target,
436347291baaSChristian Brauner 						 MAY_WRITE);
4364da1ce067SMiklos Szeredi 			if (error)
4365da1ce067SMiklos Szeredi 				return error;
4366da1ce067SMiklos Szeredi 		}
4367da1ce067SMiklos Szeredi 	}
43680eeca283SRobert Love 
43690b3974ebSMiklos Szeredi 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
43700b3974ebSMiklos Szeredi 				      flags);
4371bc27027aSMiklos Szeredi 	if (error)
4372bc27027aSMiklos Szeredi 		return error;
4373bc27027aSMiklos Szeredi 
437449d31c2fSAl Viro 	take_dentry_name_snapshot(&old_name, old_dentry);
4375bc27027aSMiklos Szeredi 	dget(new_dentry);
4376da1ce067SMiklos Szeredi 	if (!is_dir || (flags & RENAME_EXCHANGE))
4377bc27027aSMiklos Szeredi 		lock_two_nondirectories(source, target);
4378bc27027aSMiklos Szeredi 	else if (target)
43795955102cSAl Viro 		inode_lock(target);
4380bc27027aSMiklos Szeredi 
4381bc27027aSMiklos Szeredi 	error = -EBUSY;
43827af1364fSEric W. Biederman 	if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
4383bc27027aSMiklos Szeredi 		goto out;
4384bc27027aSMiklos Szeredi 
4385da1ce067SMiklos Szeredi 	if (max_links && new_dir != old_dir) {
4386bc27027aSMiklos Szeredi 		error = -EMLINK;
4387da1ce067SMiklos Szeredi 		if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
4388bc27027aSMiklos Szeredi 			goto out;
4389da1ce067SMiklos Szeredi 		if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4390da1ce067SMiklos Szeredi 		    old_dir->i_nlink >= max_links)
4391da1ce067SMiklos Szeredi 			goto out;
4392da1ce067SMiklos Szeredi 	}
4393da1ce067SMiklos Szeredi 	if (!is_dir) {
4394bc27027aSMiklos Szeredi 		error = try_break_deleg(source, delegated_inode);
4395bc27027aSMiklos Szeredi 		if (error)
4396bc27027aSMiklos Szeredi 			goto out;
4397da1ce067SMiklos Szeredi 	}
4398da1ce067SMiklos Szeredi 	if (target && !new_is_dir) {
4399bc27027aSMiklos Szeredi 		error = try_break_deleg(target, delegated_inode);
4400bc27027aSMiklos Szeredi 		if (error)
4401bc27027aSMiklos Szeredi 			goto out;
4402bc27027aSMiklos Szeredi 	}
4403520c8b16SMiklos Szeredi 	error = old_dir->i_op->rename(old_dir, old_dentry,
4404520c8b16SMiklos Szeredi 				       new_dir, new_dentry, flags);
4405bc27027aSMiklos Szeredi 	if (error)
4406bc27027aSMiklos Szeredi 		goto out;
4407bc27027aSMiklos Szeredi 
4408da1ce067SMiklos Szeredi 	if (!(flags & RENAME_EXCHANGE) && target) {
44098767712fSAl Viro 		if (is_dir) {
44108767712fSAl Viro 			shrink_dcache_parent(new_dentry);
4411bc27027aSMiklos Szeredi 			target->i_flags |= S_DEAD;
44128767712fSAl Viro 		}
4413bc27027aSMiklos Szeredi 		dont_mount(new_dentry);
44148ed936b5SEric W. Biederman 		detach_mounts(new_dentry);
4415bc27027aSMiklos Szeredi 	}
4416da1ce067SMiklos Szeredi 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4417da1ce067SMiklos Szeredi 		if (!(flags & RENAME_EXCHANGE))
4418bc27027aSMiklos Szeredi 			d_move(old_dentry, new_dentry);
4419da1ce067SMiklos Szeredi 		else
4420da1ce067SMiklos Szeredi 			d_exchange(old_dentry, new_dentry);
4421da1ce067SMiklos Szeredi 	}
4422bc27027aSMiklos Szeredi out:
4423da1ce067SMiklos Szeredi 	if (!is_dir || (flags & RENAME_EXCHANGE))
4424bc27027aSMiklos Szeredi 		unlock_two_nondirectories(source, target);
4425bc27027aSMiklos Szeredi 	else if (target)
44265955102cSAl Viro 		inode_unlock(target);
4427bc27027aSMiklos Szeredi 	dput(new_dentry);
4428da1ce067SMiklos Szeredi 	if (!error) {
4429f4ec3a3dSAl Viro 		fsnotify_move(old_dir, new_dir, &old_name.name, is_dir,
4430da1ce067SMiklos Szeredi 			      !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4431da1ce067SMiklos Szeredi 		if (flags & RENAME_EXCHANGE) {
4432f4ec3a3dSAl Viro 			fsnotify_move(new_dir, old_dir, &old_dentry->d_name,
4433da1ce067SMiklos Szeredi 				      new_is_dir, NULL, new_dentry);
4434da1ce067SMiklos Szeredi 		}
4435da1ce067SMiklos Szeredi 	}
443649d31c2fSAl Viro 	release_dentry_name_snapshot(&old_name);
44370eeca283SRobert Love 
44381da177e4SLinus Torvalds 	return error;
44391da177e4SLinus Torvalds }
44404d359507SAl Viro EXPORT_SYMBOL(vfs_rename);
44411da177e4SLinus Torvalds 
4442e886663cSJens Axboe int do_renameat2(int olddfd, struct filename *from, int newdfd,
4443e886663cSJens Axboe 		 struct filename *to, unsigned int flags)
44441da177e4SLinus Torvalds {
44451da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
44461da177e4SLinus Torvalds 	struct dentry *trap;
4447f5beed75SAl Viro 	struct path old_path, new_path;
4448f5beed75SAl Viro 	struct qstr old_last, new_last;
4449f5beed75SAl Viro 	int old_type, new_type;
44508e6d782cSJ. Bruce Fields 	struct inode *delegated_inode = NULL;
4451f5beed75SAl Viro 	unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
4452c6a94284SJeff Layton 	bool should_retry = false;
4453e886663cSJens Axboe 	int error = -EINVAL;
4454520c8b16SMiklos Szeredi 
44550d7a8555SMiklos Szeredi 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
4456e886663cSJens Axboe 		goto put_both;
4457da1ce067SMiklos Szeredi 
44580d7a8555SMiklos Szeredi 	if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
44590d7a8555SMiklos Szeredi 	    (flags & RENAME_EXCHANGE))
4460e886663cSJens Axboe 		goto put_both;
4461520c8b16SMiklos Szeredi 
4462f5beed75SAl Viro 	if (flags & RENAME_EXCHANGE)
4463f5beed75SAl Viro 		target_flags = 0;
4464f5beed75SAl Viro 
4465c6a94284SJeff Layton retry:
4466e886663cSJens Axboe 	from = filename_parentat(olddfd, from, lookup_flags, &old_path,
4467e886663cSJens Axboe 					&old_last, &old_type);
446891a27b2aSJeff Layton 	if (IS_ERR(from)) {
446991a27b2aSJeff Layton 		error = PTR_ERR(from);
4470e886663cSJens Axboe 		goto put_new;
447191a27b2aSJeff Layton 	}
44721da177e4SLinus Torvalds 
4473e886663cSJens Axboe 	to = filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last,
4474e886663cSJens Axboe 				&new_type);
447591a27b2aSJeff Layton 	if (IS_ERR(to)) {
447691a27b2aSJeff Layton 		error = PTR_ERR(to);
44771da177e4SLinus Torvalds 		goto exit1;
447891a27b2aSJeff Layton 	}
44791da177e4SLinus Torvalds 
44801da177e4SLinus Torvalds 	error = -EXDEV;
4481f5beed75SAl Viro 	if (old_path.mnt != new_path.mnt)
44821da177e4SLinus Torvalds 		goto exit2;
44831da177e4SLinus Torvalds 
44841da177e4SLinus Torvalds 	error = -EBUSY;
4485f5beed75SAl Viro 	if (old_type != LAST_NORM)
44861da177e4SLinus Torvalds 		goto exit2;
44871da177e4SLinus Torvalds 
44880a7c3937SMiklos Szeredi 	if (flags & RENAME_NOREPLACE)
44890a7c3937SMiklos Szeredi 		error = -EEXIST;
4490f5beed75SAl Viro 	if (new_type != LAST_NORM)
44911da177e4SLinus Torvalds 		goto exit2;
44921da177e4SLinus Torvalds 
4493f5beed75SAl Viro 	error = mnt_want_write(old_path.mnt);
4494c30dabfeSJan Kara 	if (error)
4495c30dabfeSJan Kara 		goto exit2;
4496c30dabfeSJan Kara 
44978e6d782cSJ. Bruce Fields retry_deleg:
4498f5beed75SAl Viro 	trap = lock_rename(new_path.dentry, old_path.dentry);
44991da177e4SLinus Torvalds 
4500f5beed75SAl Viro 	old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
45011da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
45021da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
45031da177e4SLinus Torvalds 		goto exit3;
45041da177e4SLinus Torvalds 	/* source must exist */
45051da177e4SLinus Torvalds 	error = -ENOENT;
4506b18825a7SDavid Howells 	if (d_is_negative(old_dentry))
45071da177e4SLinus Torvalds 		goto exit4;
4508f5beed75SAl Viro 	new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
45091da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
45101da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
45111da177e4SLinus Torvalds 		goto exit4;
45120a7c3937SMiklos Szeredi 	error = -EEXIST;
45130a7c3937SMiklos Szeredi 	if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
45140a7c3937SMiklos Szeredi 		goto exit5;
4515da1ce067SMiklos Szeredi 	if (flags & RENAME_EXCHANGE) {
4516da1ce067SMiklos Szeredi 		error = -ENOENT;
4517da1ce067SMiklos Szeredi 		if (d_is_negative(new_dentry))
4518da1ce067SMiklos Szeredi 			goto exit5;
4519da1ce067SMiklos Szeredi 
4520da1ce067SMiklos Szeredi 		if (!d_is_dir(new_dentry)) {
4521da1ce067SMiklos Szeredi 			error = -ENOTDIR;
4522f5beed75SAl Viro 			if (new_last.name[new_last.len])
4523da1ce067SMiklos Szeredi 				goto exit5;
4524da1ce067SMiklos Szeredi 		}
4525da1ce067SMiklos Szeredi 	}
45260a7c3937SMiklos Szeredi 	/* unless the source is a directory trailing slashes give -ENOTDIR */
45270a7c3937SMiklos Szeredi 	if (!d_is_dir(old_dentry)) {
45280a7c3937SMiklos Szeredi 		error = -ENOTDIR;
4529f5beed75SAl Viro 		if (old_last.name[old_last.len])
45300a7c3937SMiklos Szeredi 			goto exit5;
4531f5beed75SAl Viro 		if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
45320a7c3937SMiklos Szeredi 			goto exit5;
45330a7c3937SMiklos Szeredi 	}
45340a7c3937SMiklos Szeredi 	/* source should not be ancestor of target */
45350a7c3937SMiklos Szeredi 	error = -EINVAL;
45360a7c3937SMiklos Szeredi 	if (old_dentry == trap)
45370a7c3937SMiklos Szeredi 		goto exit5;
45381da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
4539da1ce067SMiklos Szeredi 	if (!(flags & RENAME_EXCHANGE))
45401da177e4SLinus Torvalds 		error = -ENOTEMPTY;
45411da177e4SLinus Torvalds 	if (new_dentry == trap)
45421da177e4SLinus Torvalds 		goto exit5;
45431da177e4SLinus Torvalds 
4544f5beed75SAl Viro 	error = security_path_rename(&old_path, old_dentry,
4545f5beed75SAl Viro 				     &new_path, new_dentry, flags);
4546be6d3e56SKentaro Takeda 	if (error)
4547c30dabfeSJan Kara 		goto exit5;
4548f5beed75SAl Viro 	error = vfs_rename(old_path.dentry->d_inode, old_dentry,
4549f5beed75SAl Viro 			   new_path.dentry->d_inode, new_dentry,
4550520c8b16SMiklos Szeredi 			   &delegated_inode, flags);
45511da177e4SLinus Torvalds exit5:
45521da177e4SLinus Torvalds 	dput(new_dentry);
45531da177e4SLinus Torvalds exit4:
45541da177e4SLinus Torvalds 	dput(old_dentry);
45551da177e4SLinus Torvalds exit3:
4556f5beed75SAl Viro 	unlock_rename(new_path.dentry, old_path.dentry);
45578e6d782cSJ. Bruce Fields 	if (delegated_inode) {
45588e6d782cSJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
45598e6d782cSJ. Bruce Fields 		if (!error)
45608e6d782cSJ. Bruce Fields 			goto retry_deleg;
45618e6d782cSJ. Bruce Fields 	}
4562f5beed75SAl Viro 	mnt_drop_write(old_path.mnt);
45631da177e4SLinus Torvalds exit2:
4564c6a94284SJeff Layton 	if (retry_estale(error, lookup_flags))
4565c6a94284SJeff Layton 		should_retry = true;
4566f5beed75SAl Viro 	path_put(&new_path);
45671da177e4SLinus Torvalds exit1:
4568f5beed75SAl Viro 	path_put(&old_path);
4569c6a94284SJeff Layton 	if (should_retry) {
4570c6a94284SJeff Layton 		should_retry = false;
4571c6a94284SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
4572c6a94284SJeff Layton 		goto retry;
4573c6a94284SJeff Layton 	}
4574e886663cSJens Axboe put_both:
4575e886663cSJens Axboe 	if (!IS_ERR(from))
4576e886663cSJens Axboe 		putname(from);
4577e886663cSJens Axboe put_new:
4578e886663cSJens Axboe 	if (!IS_ERR(to))
4579e886663cSJens Axboe 		putname(to);
45801da177e4SLinus Torvalds 	return error;
45811da177e4SLinus Torvalds }
45821da177e4SLinus Torvalds 
4583ee81feb6SDominik Brodowski SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4584ee81feb6SDominik Brodowski 		int, newdfd, const char __user *, newname, unsigned int, flags)
4585ee81feb6SDominik Brodowski {
4586e886663cSJens Axboe 	return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname),
4587e886663cSJens Axboe 				flags);
4588ee81feb6SDominik Brodowski }
4589ee81feb6SDominik Brodowski 
4590520c8b16SMiklos Szeredi SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4591520c8b16SMiklos Szeredi 		int, newdfd, const char __user *, newname)
4592520c8b16SMiklos Szeredi {
4593e886663cSJens Axboe 	return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname),
4594e886663cSJens Axboe 				0);
4595520c8b16SMiklos Szeredi }
4596520c8b16SMiklos Szeredi 
4597a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
45985590ff0dSUlrich Drepper {
4599e886663cSJens Axboe 	return do_renameat2(AT_FDCWD, getname(oldname), AT_FDCWD,
4600e886663cSJens Axboe 				getname(newname), 0);
46015590ff0dSUlrich Drepper }
46025590ff0dSUlrich Drepper 
46035d826c84SAl Viro int readlink_copy(char __user *buffer, int buflen, const char *link)
46041da177e4SLinus Torvalds {
46055d826c84SAl Viro 	int len = PTR_ERR(link);
46061da177e4SLinus Torvalds 	if (IS_ERR(link))
46071da177e4SLinus Torvalds 		goto out;
46081da177e4SLinus Torvalds 
46091da177e4SLinus Torvalds 	len = strlen(link);
46101da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
46111da177e4SLinus Torvalds 		len = buflen;
46121da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
46131da177e4SLinus Torvalds 		len = -EFAULT;
46141da177e4SLinus Torvalds out:
46151da177e4SLinus Torvalds 	return len;
46161da177e4SLinus Torvalds }
46171da177e4SLinus Torvalds 
4618d60874cdSMiklos Szeredi /**
4619fd4a0edfSMiklos Szeredi  * vfs_readlink - copy symlink body into userspace buffer
4620fd4a0edfSMiklos Szeredi  * @dentry: dentry on which to get symbolic link
4621fd4a0edfSMiklos Szeredi  * @buffer: user memory pointer
4622fd4a0edfSMiklos Szeredi  * @buflen: size of buffer
4623fd4a0edfSMiklos Szeredi  *
4624fd4a0edfSMiklos Szeredi  * Does not touch atime.  That's up to the caller if necessary
4625fd4a0edfSMiklos Szeredi  *
4626fd4a0edfSMiklos Szeredi  * Does not call security hook.
4627fd4a0edfSMiklos Szeredi  */
4628fd4a0edfSMiklos Szeredi int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4629fd4a0edfSMiklos Szeredi {
4630fd4a0edfSMiklos Szeredi 	struct inode *inode = d_inode(dentry);
4631f2df5da6SAl Viro 	DEFINE_DELAYED_CALL(done);
4632f2df5da6SAl Viro 	const char *link;
4633f2df5da6SAl Viro 	int res;
4634fd4a0edfSMiklos Szeredi 
463576fca90eSMiklos Szeredi 	if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
463676fca90eSMiklos Szeredi 		if (unlikely(inode->i_op->readlink))
463776fca90eSMiklos Szeredi 			return inode->i_op->readlink(dentry, buffer, buflen);
463876fca90eSMiklos Szeredi 
463976fca90eSMiklos Szeredi 		if (!d_is_symlink(dentry))
4640fd4a0edfSMiklos Szeredi 			return -EINVAL;
4641fd4a0edfSMiklos Szeredi 
464276fca90eSMiklos Szeredi 		spin_lock(&inode->i_lock);
464376fca90eSMiklos Szeredi 		inode->i_opflags |= IOP_DEFAULT_READLINK;
464476fca90eSMiklos Szeredi 		spin_unlock(&inode->i_lock);
464576fca90eSMiklos Szeredi 	}
464676fca90eSMiklos Szeredi 
46474c4f7c19SEric Biggers 	link = READ_ONCE(inode->i_link);
4648f2df5da6SAl Viro 	if (!link) {
4649f2df5da6SAl Viro 		link = inode->i_op->get_link(dentry, inode, &done);
4650f2df5da6SAl Viro 		if (IS_ERR(link))
4651f2df5da6SAl Viro 			return PTR_ERR(link);
4652f2df5da6SAl Viro 	}
4653f2df5da6SAl Viro 	res = readlink_copy(buffer, buflen, link);
4654f2df5da6SAl Viro 	do_delayed_call(&done);
4655f2df5da6SAl Viro 	return res;
4656fd4a0edfSMiklos Szeredi }
4657fd4a0edfSMiklos Szeredi EXPORT_SYMBOL(vfs_readlink);
46581da177e4SLinus Torvalds 
4659d60874cdSMiklos Szeredi /**
4660d60874cdSMiklos Szeredi  * vfs_get_link - get symlink body
4661d60874cdSMiklos Szeredi  * @dentry: dentry on which to get symbolic link
4662d60874cdSMiklos Szeredi  * @done: caller needs to free returned data with this
4663d60874cdSMiklos Szeredi  *
4664d60874cdSMiklos Szeredi  * Calls security hook and i_op->get_link() on the supplied inode.
4665d60874cdSMiklos Szeredi  *
4666d60874cdSMiklos Szeredi  * It does not touch atime.  That's up to the caller if necessary.
4667d60874cdSMiklos Szeredi  *
4668d60874cdSMiklos Szeredi  * Does not work on "special" symlinks like /proc/$$/fd/N
4669d60874cdSMiklos Szeredi  */
4670d60874cdSMiklos Szeredi const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4671d60874cdSMiklos Szeredi {
4672d60874cdSMiklos Szeredi 	const char *res = ERR_PTR(-EINVAL);
4673d60874cdSMiklos Szeredi 	struct inode *inode = d_inode(dentry);
4674d60874cdSMiklos Szeredi 
4675d60874cdSMiklos Szeredi 	if (d_is_symlink(dentry)) {
4676d60874cdSMiklos Szeredi 		res = ERR_PTR(security_inode_readlink(dentry));
4677d60874cdSMiklos Szeredi 		if (!res)
4678d60874cdSMiklos Szeredi 			res = inode->i_op->get_link(dentry, inode, done);
4679d60874cdSMiklos Szeredi 	}
4680d60874cdSMiklos Szeredi 	return res;
4681d60874cdSMiklos Szeredi }
4682d60874cdSMiklos Szeredi EXPORT_SYMBOL(vfs_get_link);
4683d60874cdSMiklos Szeredi 
46841da177e4SLinus Torvalds /* get the link contents into pagecache */
46856b255391SAl Viro const char *page_get_link(struct dentry *dentry, struct inode *inode,
4686fceef393SAl Viro 			  struct delayed_call *callback)
46871da177e4SLinus Torvalds {
4688ebd09abbSDuane Griffin 	char *kaddr;
46891da177e4SLinus Torvalds 	struct page *page;
46906b255391SAl Viro 	struct address_space *mapping = inode->i_mapping;
46916b255391SAl Viro 
4692d3883d4fSAl Viro 	if (!dentry) {
4693d3883d4fSAl Viro 		page = find_get_page(mapping, 0);
4694d3883d4fSAl Viro 		if (!page)
46956b255391SAl Viro 			return ERR_PTR(-ECHILD);
4696d3883d4fSAl Viro 		if (!PageUptodate(page)) {
4697d3883d4fSAl Viro 			put_page(page);
4698d3883d4fSAl Viro 			return ERR_PTR(-ECHILD);
4699d3883d4fSAl Viro 		}
4700d3883d4fSAl Viro 	} else {
4701090d2b18SPekka Enberg 		page = read_mapping_page(mapping, 0, NULL);
47021da177e4SLinus Torvalds 		if (IS_ERR(page))
47036fe6900eSNick Piggin 			return (char*)page;
4704d3883d4fSAl Viro 	}
4705fceef393SAl Viro 	set_delayed_call(callback, page_put_link, page);
470621fc61c7SAl Viro 	BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
470721fc61c7SAl Viro 	kaddr = page_address(page);
47086b255391SAl Viro 	nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
4709ebd09abbSDuane Griffin 	return kaddr;
47101da177e4SLinus Torvalds }
47111da177e4SLinus Torvalds 
47126b255391SAl Viro EXPORT_SYMBOL(page_get_link);
47131da177e4SLinus Torvalds 
4714fceef393SAl Viro void page_put_link(void *arg)
47151da177e4SLinus Torvalds {
4716fceef393SAl Viro 	put_page(arg);
47171da177e4SLinus Torvalds }
47184d359507SAl Viro EXPORT_SYMBOL(page_put_link);
47191da177e4SLinus Torvalds 
4720aa80deabSAl Viro int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4721aa80deabSAl Viro {
4722fceef393SAl Viro 	DEFINE_DELAYED_CALL(done);
47236b255391SAl Viro 	int res = readlink_copy(buffer, buflen,
47246b255391SAl Viro 				page_get_link(dentry, d_inode(dentry),
4725fceef393SAl Viro 					      &done));
4726fceef393SAl Viro 	do_delayed_call(&done);
4727aa80deabSAl Viro 	return res;
4728aa80deabSAl Viro }
4729aa80deabSAl Viro EXPORT_SYMBOL(page_readlink);
4730aa80deabSAl Viro 
473154566b2cSNick Piggin /*
473254566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
473354566b2cSNick Piggin  */
473454566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
47351da177e4SLinus Torvalds {
47361da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
47370adb25d2SKirill Korotaev 	struct page *page;
4738afddba49SNick Piggin 	void *fsdata;
4739beb497abSDmitriy Monakhov 	int err;
4740c718a975STetsuo Handa 	unsigned int flags = 0;
474154566b2cSNick Piggin 	if (nofs)
474254566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
47431da177e4SLinus Torvalds 
47447e53cac4SNeilBrown retry:
4745afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
474654566b2cSNick Piggin 				flags, &page, &fsdata);
47471da177e4SLinus Torvalds 	if (err)
4748afddba49SNick Piggin 		goto fail;
4749afddba49SNick Piggin 
475021fc61c7SAl Viro 	memcpy(page_address(page), symname, len-1);
4751afddba49SNick Piggin 
4752afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4753afddba49SNick Piggin 							page, fsdata);
47541da177e4SLinus Torvalds 	if (err < 0)
47551da177e4SLinus Torvalds 		goto fail;
4756afddba49SNick Piggin 	if (err < len-1)
4757afddba49SNick Piggin 		goto retry;
4758afddba49SNick Piggin 
47591da177e4SLinus Torvalds 	mark_inode_dirty(inode);
47601da177e4SLinus Torvalds 	return 0;
47611da177e4SLinus Torvalds fail:
47621da177e4SLinus Torvalds 	return err;
47631da177e4SLinus Torvalds }
47644d359507SAl Viro EXPORT_SYMBOL(__page_symlink);
47651da177e4SLinus Torvalds 
47660adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
47670adb25d2SKirill Korotaev {
47680adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
4769c62d2555SMichal Hocko 			!mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
47700adb25d2SKirill Korotaev }
47714d359507SAl Viro EXPORT_SYMBOL(page_symlink);
47720adb25d2SKirill Korotaev 
477392e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
47746b255391SAl Viro 	.get_link	= page_get_link,
47751da177e4SLinus Torvalds };
47761da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
4777