xref: /openbmc/linux/fs/namei.c (revision 47291baa)
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 
262*47291baaSChristian Brauner /**
263*47291baaSChristian Brauner  * check_acl - perform ACL permission checking
264*47291baaSChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
265*47291baaSChristian Brauner  * @inode:	inode to check permissions on
266*47291baaSChristian Brauner  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
267*47291baaSChristian Brauner  *
268*47291baaSChristian Brauner  * This function performs the ACL permission checking. Since this function
269*47291baaSChristian Brauner  * retrieve POSIX acls it needs to know whether it is called from a blocking or
270*47291baaSChristian Brauner  * non-blocking context and thus cares about the MAY_NOT_BLOCK bit.
271*47291baaSChristian Brauner  *
272*47291baaSChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
273*47291baaSChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then take
274*47291baaSChristian Brauner  * care to map the inode according to @mnt_userns before checking permissions.
275*47291baaSChristian Brauner  * On non-idmapped mounts or if permission checking is to be performed on the
276*47291baaSChristian Brauner  * raw inode simply passs init_user_ns.
277*47291baaSChristian Brauner  */
278*47291baaSChristian Brauner static int check_acl(struct user_namespace *mnt_userns,
279*47291baaSChristian 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;
291*47291baaSChristian 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) {
298*47291baaSChristian 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 
307*47291baaSChristian Brauner /**
308*47291baaSChristian Brauner  * acl_permission_check - perform basic UNIX permission checking
309*47291baaSChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
310*47291baaSChristian Brauner  * @inode:	inode to check permissions on
311*47291baaSChristian Brauner  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
3125fc475b7SLinus Torvalds  *
313*47291baaSChristian Brauner  * This function performs the basic UNIX permission checking. Since this
314*47291baaSChristian Brauner  * function may retrieve POSIX acls it needs to know whether it is called from a
315*47291baaSChristian Brauner  * blocking or non-blocking context and thus cares about the MAY_NOT_BLOCK bit.
316*47291baaSChristian Brauner  *
317*47291baaSChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
318*47291baaSChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then take
319*47291baaSChristian Brauner  * care to map the inode according to @mnt_userns before checking permissions.
320*47291baaSChristian Brauner  * On non-idmapped mounts or if permission checking is to be performed on the
321*47291baaSChristian Brauner  * raw inode simply passs init_user_ns.
3225909ccaaSLinus Torvalds  */
323*47291baaSChristian Brauner static int acl_permission_check(struct user_namespace *mnt_userns,
324*47291baaSChristian Brauner 				struct inode *inode, int mask)
3255909ccaaSLinus Torvalds {
32626cf46beSLinus Torvalds 	unsigned int mode = inode->i_mode;
327*47291baaSChristian Brauner 	kuid_t i_uid;
3285909ccaaSLinus Torvalds 
3295fc475b7SLinus Torvalds 	/* Are we the owner? If so, ACL's don't matter */
330*47291baaSChristian Brauner 	i_uid = i_uid_into_mnt(mnt_userns, inode);
331*47291baaSChristian 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)) {
339*47291baaSChristian 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))) {
353*47291baaSChristian Brauner 		kgid_t kgid = i_gid_into_mnt(mnt_userns, inode);
354*47291baaSChristian 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
364*47291baaSChristian 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.
377*47291baaSChristian Brauner  *
378*47291baaSChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
379*47291baaSChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then take
380*47291baaSChristian Brauner  * care to map the inode according to @mnt_userns before checking permissions.
381*47291baaSChristian Brauner  * On non-idmapped mounts or if permission checking is to be performed on the
382*47291baaSChristian Brauner  * raw inode simply passs init_user_ns.
3831da177e4SLinus Torvalds  */
384*47291baaSChristian Brauner int generic_permission(struct user_namespace *mnt_userns, struct inode *inode,
385*47291baaSChristian Brauner 		       int mask)
3861da177e4SLinus Torvalds {
3875909ccaaSLinus Torvalds 	int ret;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	/*
390948409c7SAndreas Gruenbacher 	 * Do the basic permission checks.
3911da177e4SLinus Torvalds 	 */
392*47291baaSChristian 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))
399*47291baaSChristian Brauner 			if (capable_wrt_inode_uidgid(mnt_userns, inode,
40023adbe12SAndy Lutomirski 						     CAP_DAC_READ_SEARCH))
401d594e7ecSAl Viro 				return 0;
402*47291baaSChristian 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)
413*47291baaSChristian 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))
422*47291baaSChristian 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 
430*47291baaSChristian Brauner /**
431*47291baaSChristian Brauner  * do_inode_permission - UNIX permission checking
432*47291baaSChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
433*47291baaSChristian Brauner  * @inode:	inode to check permissions on
434*47291baaSChristian Brauner  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
435*47291baaSChristian 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  */
441*47291baaSChristian Brauner static inline int do_inode_permission(struct user_namespace *mnt_userns,
442*47291baaSChristian 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 	}
453*47291baaSChristian 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
478*47291baaSChristian 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  */
488*47291baaSChristian Brauner int inode_permission(struct user_namespace *mnt_userns,
489*47291baaSChristian 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 		 */
5094bfd054aSEric Biggers 		if (HAS_UNMAPPED_ID(inode))
5104bfd054aSEric Biggers 			return -EACCES;
5114bfd054aSEric Biggers 	}
5124bfd054aSEric Biggers 
513*47291baaSChristian 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 {
1007800179c9SKees Cook 	if (!sysctl_protected_symlinks)
1008800179c9SKees Cook 		return 0;
1009800179c9SKees Cook 
1010800179c9SKees Cook 	/* Allowed if owner and follower match. */
101181abe27bSEric W. Biederman 	if (uid_eq(current_cred()->fsuid, inode->i_uid))
1012800179c9SKees Cook 		return 0;
1013800179c9SKees Cook 
1014800179c9SKees Cook 	/* Allowed if parent directory not sticky and world-writable. */
10150f705953SAl Viro 	if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
1016800179c9SKees Cook 		return 0;
1017800179c9SKees Cook 
1018800179c9SKees Cook 	/* Allowed if parent directory and link owner match. */
10190f705953SAl Viro 	if (uid_valid(nd->dir_uid) && uid_eq(nd->dir_uid, inode->i_uid))
1020800179c9SKees Cook 		return 0;
1021800179c9SKees Cook 
102231956502SAl Viro 	if (nd->flags & LOOKUP_RCU)
102331956502SAl Viro 		return -ECHILD;
102431956502SAl Viro 
1025ea841bafSRichard Guy Briggs 	audit_inode(nd->name, nd->stack[0].link.dentry, 0);
1026245d7369SKees Cook 	audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link");
1027800179c9SKees Cook 	return -EACCES;
1028800179c9SKees Cook }
1029800179c9SKees Cook 
1030800179c9SKees Cook /**
1031800179c9SKees Cook  * safe_hardlink_source - Check for safe hardlink conditions
1032800179c9SKees Cook  * @inode: the source inode to hardlink from
1033800179c9SKees Cook  *
1034800179c9SKees Cook  * Return false if at least one of the following conditions:
1035800179c9SKees Cook  *    - inode is not a regular file
1036800179c9SKees Cook  *    - inode is setuid
1037800179c9SKees Cook  *    - inode is setgid and group-exec
1038800179c9SKees Cook  *    - access failure for read and write
1039800179c9SKees Cook  *
1040800179c9SKees Cook  * Otherwise returns true.
1041800179c9SKees Cook  */
1042800179c9SKees Cook static bool safe_hardlink_source(struct inode *inode)
1043800179c9SKees Cook {
1044800179c9SKees Cook 	umode_t mode = inode->i_mode;
1045800179c9SKees Cook 
1046800179c9SKees Cook 	/* Special files should not get pinned to the filesystem. */
1047800179c9SKees Cook 	if (!S_ISREG(mode))
1048800179c9SKees Cook 		return false;
1049800179c9SKees Cook 
1050800179c9SKees Cook 	/* Setuid files should not get pinned to the filesystem. */
1051800179c9SKees Cook 	if (mode & S_ISUID)
1052800179c9SKees Cook 		return false;
1053800179c9SKees Cook 
1054800179c9SKees Cook 	/* Executable setgid files should not get pinned to the filesystem. */
1055800179c9SKees Cook 	if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
1056800179c9SKees Cook 		return false;
1057800179c9SKees Cook 
1058800179c9SKees Cook 	/* Hardlinking to unreadable or unwritable sources is dangerous. */
1059*47291baaSChristian Brauner 	if (inode_permission(&init_user_ns, inode, MAY_READ | MAY_WRITE))
1060800179c9SKees Cook 		return false;
1061800179c9SKees Cook 
1062800179c9SKees Cook 	return true;
1063800179c9SKees Cook }
1064800179c9SKees Cook 
1065800179c9SKees Cook /**
1066800179c9SKees Cook  * may_linkat - Check permissions for creating a hardlink
1067800179c9SKees Cook  * @link: the source to hardlink from
1068800179c9SKees Cook  *
1069800179c9SKees Cook  * Block hardlink when all of:
1070800179c9SKees Cook  *  - sysctl_protected_hardlinks enabled
1071800179c9SKees Cook  *  - fsuid does not match inode
1072800179c9SKees Cook  *  - hardlink source is unsafe (see safe_hardlink_source() above)
1073f2ca3796SDirk Steinmetz  *  - not CAP_FOWNER in a namespace with the inode owner uid mapped
1074800179c9SKees Cook  *
1075800179c9SKees Cook  * Returns 0 if successful, -ve on error.
1076800179c9SKees Cook  */
1077812931d6SChristoph Hellwig int may_linkat(struct path *link)
1078800179c9SKees Cook {
1079593d1ce8SEric W. Biederman 	struct inode *inode = link->dentry->d_inode;
1080593d1ce8SEric W. Biederman 
1081593d1ce8SEric W. Biederman 	/* Inode writeback is not safe when the uid or gid are invalid. */
1082593d1ce8SEric W. Biederman 	if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
1083593d1ce8SEric W. Biederman 		return -EOVERFLOW;
1084800179c9SKees Cook 
1085800179c9SKees Cook 	if (!sysctl_protected_hardlinks)
1086800179c9SKees Cook 		return 0;
1087800179c9SKees Cook 
1088800179c9SKees Cook 	/* Source inode owner (or CAP_FOWNER) can hardlink all they like,
1089800179c9SKees Cook 	 * otherwise, it must be a safe source.
1090800179c9SKees Cook 	 */
1091cc658db4SKees Cook 	if (safe_hardlink_source(inode) || inode_owner_or_capable(inode))
1092800179c9SKees Cook 		return 0;
1093800179c9SKees Cook 
1094245d7369SKees Cook 	audit_log_path_denied(AUDIT_ANOM_LINK, "linkat");
1095800179c9SKees Cook 	return -EPERM;
1096800179c9SKees Cook }
1097800179c9SKees Cook 
109830aba665SSalvatore Mesoraca /**
109930aba665SSalvatore Mesoraca  * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
110030aba665SSalvatore Mesoraca  *			  should be allowed, or not, on files that already
110130aba665SSalvatore Mesoraca  *			  exist.
1102d0cb5018SAl Viro  * @dir_mode: mode bits of directory
1103d0cb5018SAl Viro  * @dir_uid: owner of directory
110430aba665SSalvatore Mesoraca  * @inode: the inode of the file to open
110530aba665SSalvatore Mesoraca  *
110630aba665SSalvatore Mesoraca  * Block an O_CREAT open of a FIFO (or a regular file) when:
110730aba665SSalvatore Mesoraca  *   - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
110830aba665SSalvatore Mesoraca  *   - the file already exists
110930aba665SSalvatore Mesoraca  *   - we are in a sticky directory
111030aba665SSalvatore Mesoraca  *   - we don't own the file
111130aba665SSalvatore Mesoraca  *   - the owner of the directory doesn't own the file
111230aba665SSalvatore Mesoraca  *   - the directory is world writable
111330aba665SSalvatore Mesoraca  * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
111430aba665SSalvatore Mesoraca  * the directory doesn't have to be world writable: being group writable will
111530aba665SSalvatore Mesoraca  * be enough.
111630aba665SSalvatore Mesoraca  *
111730aba665SSalvatore Mesoraca  * Returns 0 if the open is allowed, -ve on error.
111830aba665SSalvatore Mesoraca  */
1119d0cb5018SAl Viro static int may_create_in_sticky(umode_t dir_mode, kuid_t dir_uid,
112030aba665SSalvatore Mesoraca 				struct inode * const inode)
112130aba665SSalvatore Mesoraca {
112230aba665SSalvatore Mesoraca 	if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) ||
112330aba665SSalvatore Mesoraca 	    (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
1124d0cb5018SAl Viro 	    likely(!(dir_mode & S_ISVTX)) ||
1125d0cb5018SAl Viro 	    uid_eq(inode->i_uid, dir_uid) ||
112630aba665SSalvatore Mesoraca 	    uid_eq(current_fsuid(), inode->i_uid))
112730aba665SSalvatore Mesoraca 		return 0;
112830aba665SSalvatore Mesoraca 
1129d0cb5018SAl Viro 	if (likely(dir_mode & 0002) ||
1130d0cb5018SAl Viro 	    (dir_mode & 0020 &&
113130aba665SSalvatore Mesoraca 	     ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) ||
113230aba665SSalvatore Mesoraca 	      (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) {
1133245d7369SKees Cook 		const char *operation = S_ISFIFO(inode->i_mode) ?
1134245d7369SKees Cook 					"sticky_create_fifo" :
1135245d7369SKees Cook 					"sticky_create_regular";
1136245d7369SKees Cook 		audit_log_path_denied(AUDIT_ANOM_CREAT, operation);
113730aba665SSalvatore Mesoraca 		return -EACCES;
113830aba665SSalvatore Mesoraca 	}
113930aba665SSalvatore Mesoraca 	return 0;
114030aba665SSalvatore Mesoraca }
114130aba665SSalvatore Mesoraca 
1142f015f126SDavid Howells /*
1143f015f126SDavid Howells  * follow_up - Find the mountpoint of path's vfsmount
1144f015f126SDavid Howells  *
1145f015f126SDavid Howells  * Given a path, find the mountpoint of its source file system.
1146f015f126SDavid Howells  * Replace @path with the path of the mountpoint in the parent mount.
1147f015f126SDavid Howells  * Up is towards /.
1148f015f126SDavid Howells  *
1149f015f126SDavid Howells  * Return 1 if we went up a level and 0 if we were already at the
1150f015f126SDavid Howells  * root.
1151f015f126SDavid Howells  */
1152bab77ebfSAl Viro int follow_up(struct path *path)
11531da177e4SLinus Torvalds {
11540714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
11550714a533SAl Viro 	struct mount *parent;
11561da177e4SLinus Torvalds 	struct dentry *mountpoint;
115799b7db7bSNick Piggin 
115848a066e7SAl Viro 	read_seqlock_excl(&mount_lock);
11590714a533SAl Viro 	parent = mnt->mnt_parent;
11603c0a6163SAl Viro 	if (parent == mnt) {
116148a066e7SAl Viro 		read_sequnlock_excl(&mount_lock);
11621da177e4SLinus Torvalds 		return 0;
11631da177e4SLinus Torvalds 	}
11640714a533SAl Viro 	mntget(&parent->mnt);
1165a73324daSAl Viro 	mountpoint = dget(mnt->mnt_mountpoint);
116648a066e7SAl Viro 	read_sequnlock_excl(&mount_lock);
1167bab77ebfSAl Viro 	dput(path->dentry);
1168bab77ebfSAl Viro 	path->dentry = mountpoint;
1169bab77ebfSAl Viro 	mntput(path->mnt);
11700714a533SAl Viro 	path->mnt = &parent->mnt;
11711da177e4SLinus Torvalds 	return 1;
11721da177e4SLinus Torvalds }
11734d359507SAl Viro EXPORT_SYMBOL(follow_up);
11741da177e4SLinus Torvalds 
11757ef482faSAl Viro static bool choose_mountpoint_rcu(struct mount *m, const struct path *root,
11767ef482faSAl Viro 				  struct path *path, unsigned *seqp)
11777ef482faSAl Viro {
11787ef482faSAl Viro 	while (mnt_has_parent(m)) {
11797ef482faSAl Viro 		struct dentry *mountpoint = m->mnt_mountpoint;
11807ef482faSAl Viro 
11817ef482faSAl Viro 		m = m->mnt_parent;
11827ef482faSAl Viro 		if (unlikely(root->dentry == mountpoint &&
11837ef482faSAl Viro 			     root->mnt == &m->mnt))
11847ef482faSAl Viro 			break;
11857ef482faSAl Viro 		if (mountpoint != m->mnt.mnt_root) {
11867ef482faSAl Viro 			path->mnt = &m->mnt;
11877ef482faSAl Viro 			path->dentry = mountpoint;
11887ef482faSAl Viro 			*seqp = read_seqcount_begin(&mountpoint->d_seq);
11897ef482faSAl Viro 			return true;
11907ef482faSAl Viro 		}
11917ef482faSAl Viro 	}
11927ef482faSAl Viro 	return false;
11937ef482faSAl Viro }
11947ef482faSAl Viro 
11952aa38470SAl Viro static bool choose_mountpoint(struct mount *m, const struct path *root,
11962aa38470SAl Viro 			      struct path *path)
11972aa38470SAl Viro {
11982aa38470SAl Viro 	bool found;
11992aa38470SAl Viro 
12002aa38470SAl Viro 	rcu_read_lock();
12012aa38470SAl Viro 	while (1) {
12022aa38470SAl Viro 		unsigned seq, mseq = read_seqbegin(&mount_lock);
12032aa38470SAl Viro 
12042aa38470SAl Viro 		found = choose_mountpoint_rcu(m, root, path, &seq);
12052aa38470SAl Viro 		if (unlikely(!found)) {
12062aa38470SAl Viro 			if (!read_seqretry(&mount_lock, mseq))
12072aa38470SAl Viro 				break;
12082aa38470SAl Viro 		} else {
12092aa38470SAl Viro 			if (likely(__legitimize_path(path, seq, mseq)))
12102aa38470SAl Viro 				break;
12112aa38470SAl Viro 			rcu_read_unlock();
12122aa38470SAl Viro 			path_put(path);
12132aa38470SAl Viro 			rcu_read_lock();
12142aa38470SAl Viro 		}
12152aa38470SAl Viro 	}
12162aa38470SAl Viro 	rcu_read_unlock();
12172aa38470SAl Viro 	return found;
12182aa38470SAl Viro }
12192aa38470SAl Viro 
1220b5c84bf6SNick Piggin /*
12219875cf80SDavid Howells  * Perform an automount
12229875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
12239875cf80SDavid Howells  *   were called with.
12241da177e4SLinus Torvalds  */
12251c9f5e06SAl Viro static int follow_automount(struct path *path, int *count, unsigned lookup_flags)
122631e6b01fSNick Piggin {
122725e195aaSAl Viro 	struct dentry *dentry = path->dentry;
12289875cf80SDavid Howells 
12290ec26fd0SMiklos Szeredi 	/* We don't want to mount if someone's just doing a stat -
12300ec26fd0SMiklos Szeredi 	 * unless they're stat'ing a directory and appended a '/' to
12310ec26fd0SMiklos Szeredi 	 * the name.
12320ec26fd0SMiklos Szeredi 	 *
12330ec26fd0SMiklos Szeredi 	 * We do, however, want to mount if someone wants to open or
12345a30d8a2SDavid Howells 	 * create a file of any type under the mountpoint, wants to
12350ec26fd0SMiklos Szeredi 	 * traverse through the mountpoint or wants to open the
12360ec26fd0SMiklos Szeredi 	 * mounted directory.  Also, autofs may mark negative dentries
12370ec26fd0SMiklos Szeredi 	 * as being automount points.  These will need the attentions
12380ec26fd0SMiklos Szeredi 	 * of the daemon to instantiate them before they can be used.
12395a30d8a2SDavid Howells 	 */
12401c9f5e06SAl Viro 	if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
12415d38f049SIan Kent 			   LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
124225e195aaSAl Viro 	    dentry->d_inode)
12439875cf80SDavid Howells 		return -EISDIR;
12440ec26fd0SMiklos Szeredi 
12451c9f5e06SAl Viro 	if (count && (*count)++ >= MAXSYMLINKS)
12469875cf80SDavid Howells 		return -ELOOP;
12479875cf80SDavid Howells 
124825e195aaSAl Viro 	return finish_automount(dentry->d_op->d_automount(path), path);
1249ea5b778aSDavid Howells }
12509875cf80SDavid Howells 
12519875cf80SDavid Howells /*
12529deed3ebSAl Viro  * mount traversal - out-of-line part.  One note on ->d_flags accesses -
12539deed3ebSAl Viro  * dentries are pinned but not locked here, so negative dentry can go
12549deed3ebSAl Viro  * positive right under us.  Use of smp_load_acquire() provides a barrier
12559deed3ebSAl Viro  * sufficient for ->d_inode and ->d_flags consistency.
12569875cf80SDavid Howells  */
12579deed3ebSAl Viro static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped,
12589deed3ebSAl Viro 			     int *count, unsigned lookup_flags)
12599875cf80SDavid Howells {
12609deed3ebSAl Viro 	struct vfsmount *mnt = path->mnt;
12619875cf80SDavid Howells 	bool need_mntput = false;
12628aef1884SAl Viro 	int ret = 0;
12639875cf80SDavid Howells 
12649deed3ebSAl Viro 	while (flags & DCACHE_MANAGED_DENTRY) {
1265cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1266cc53ce53SDavid Howells 		 * being held. */
1267d41efb52SAl Viro 		if (flags & DCACHE_MANAGE_TRANSIT) {
1268fb5f51c7SIan Kent 			ret = path->dentry->d_op->d_manage(path, false);
1269508c8772SAl Viro 			flags = smp_load_acquire(&path->dentry->d_flags);
1270cc53ce53SDavid Howells 			if (ret < 0)
12718aef1884SAl Viro 				break;
1272cc53ce53SDavid Howells 		}
1273cc53ce53SDavid Howells 
12749deed3ebSAl Viro 		if (flags & DCACHE_MOUNTED) {	// something's mounted on it..
12759875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
12769deed3ebSAl Viro 			if (mounted) {		// ... in our namespace
12779875cf80SDavid Howells 				dput(path->dentry);
12789875cf80SDavid Howells 				if (need_mntput)
1279463ffb2eSAl Viro 					mntput(path->mnt);
1280463ffb2eSAl Viro 				path->mnt = mounted;
1281463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
12829deed3ebSAl Viro 				// here we know it's positive
12839deed3ebSAl Viro 				flags = path->dentry->d_flags;
12849875cf80SDavid Howells 				need_mntput = true;
12859875cf80SDavid Howells 				continue;
1286463ffb2eSAl Viro 			}
12871da177e4SLinus Torvalds 		}
12889875cf80SDavid Howells 
12899deed3ebSAl Viro 		if (!(flags & DCACHE_NEED_AUTOMOUNT))
12909deed3ebSAl Viro 			break;
12919deed3ebSAl Viro 
12929deed3ebSAl Viro 		// uncovered automount point
12939deed3ebSAl Viro 		ret = follow_automount(path, count, lookup_flags);
12949deed3ebSAl Viro 		flags = smp_load_acquire(&path->dentry->d_flags);
12959875cf80SDavid Howells 		if (ret < 0)
12968aef1884SAl Viro 			break;
12979875cf80SDavid Howells 	}
12989875cf80SDavid Howells 
12999deed3ebSAl Viro 	if (ret == -EISDIR)
13009deed3ebSAl Viro 		ret = 0;
13019deed3ebSAl Viro 	// possible if you race with several mount --move
13029deed3ebSAl Viro 	if (need_mntput && path->mnt == mnt)
13038aef1884SAl Viro 		mntput(path->mnt);
13049deed3ebSAl Viro 	if (!ret && unlikely(d_flags_negative(flags)))
1305d41efb52SAl Viro 		ret = -ENOENT;
13069deed3ebSAl Viro 	*jumped = need_mntput;
13078402752eSAl Viro 	return ret;
13081da177e4SLinus Torvalds }
13091da177e4SLinus Torvalds 
13109deed3ebSAl Viro static inline int traverse_mounts(struct path *path, bool *jumped,
13119deed3ebSAl Viro 				  int *count, unsigned lookup_flags)
13129deed3ebSAl Viro {
13139deed3ebSAl Viro 	unsigned flags = smp_load_acquire(&path->dentry->d_flags);
13149deed3ebSAl Viro 
13159deed3ebSAl Viro 	/* fastpath */
13169deed3ebSAl Viro 	if (likely(!(flags & DCACHE_MANAGED_DENTRY))) {
13179deed3ebSAl Viro 		*jumped = false;
13189deed3ebSAl Viro 		if (unlikely(d_flags_negative(flags)))
13199deed3ebSAl Viro 			return -ENOENT;
13209deed3ebSAl Viro 		return 0;
13219deed3ebSAl Viro 	}
13229deed3ebSAl Viro 	return __traverse_mounts(path, flags, jumped, count, lookup_flags);
13239deed3ebSAl Viro }
13249deed3ebSAl Viro 
1325cc53ce53SDavid Howells int follow_down_one(struct path *path)
13261da177e4SLinus Torvalds {
13271da177e4SLinus Torvalds 	struct vfsmount *mounted;
13281da177e4SLinus Torvalds 
13291c755af4SAl Viro 	mounted = lookup_mnt(path);
13301da177e4SLinus Torvalds 	if (mounted) {
13319393bd07SAl Viro 		dput(path->dentry);
13329393bd07SAl Viro 		mntput(path->mnt);
13339393bd07SAl Viro 		path->mnt = mounted;
13349393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
13351da177e4SLinus Torvalds 		return 1;
13361da177e4SLinus Torvalds 	}
13371da177e4SLinus Torvalds 	return 0;
13381da177e4SLinus Torvalds }
13394d359507SAl Viro EXPORT_SYMBOL(follow_down_one);
13401da177e4SLinus Torvalds 
13419875cf80SDavid Howells /*
13429deed3ebSAl Viro  * Follow down to the covering mount currently visible to userspace.  At each
13439deed3ebSAl Viro  * point, the filesystem owning that dentry may be queried as to whether the
13449deed3ebSAl Viro  * caller is permitted to proceed or not.
13459deed3ebSAl Viro  */
13469deed3ebSAl Viro int follow_down(struct path *path)
13479deed3ebSAl Viro {
13489deed3ebSAl Viro 	struct vfsmount *mnt = path->mnt;
13499deed3ebSAl Viro 	bool jumped;
13509deed3ebSAl Viro 	int ret = traverse_mounts(path, &jumped, NULL, 0);
13519deed3ebSAl Viro 
13529deed3ebSAl Viro 	if (path->mnt != mnt)
13539deed3ebSAl Viro 		mntput(mnt);
13549deed3ebSAl Viro 	return ret;
13559deed3ebSAl Viro }
13569deed3ebSAl Viro EXPORT_SYMBOL(follow_down);
13579deed3ebSAl Viro 
13589deed3ebSAl Viro /*
1359287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
1360287548e4SAl Viro  * we meet a managed dentry that would need blocking.
13619875cf80SDavid Howells  */
13629875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1363254cf582SAl Viro 			       struct inode **inode, unsigned *seqp)
13649875cf80SDavid Howells {
1365ea936aebSAl Viro 	struct dentry *dentry = path->dentry;
1366ea936aebSAl Viro 	unsigned int flags = dentry->d_flags;
1367ea936aebSAl Viro 
1368ea936aebSAl Viro 	if (likely(!(flags & DCACHE_MANAGED_DENTRY)))
1369ea936aebSAl Viro 		return true;
1370ea936aebSAl Viro 
1371ea936aebSAl Viro 	if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1372ea936aebSAl Viro 		return false;
1373ea936aebSAl Viro 
137462a7375eSIan Kent 	for (;;) {
137562a7375eSIan Kent 		/*
137662a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
137762a7375eSIan Kent 		 * that wants to block transit.
137862a7375eSIan Kent 		 */
1379ea936aebSAl Viro 		if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) {
1380ea936aebSAl Viro 			int res = dentry->d_op->d_manage(path, true);
1381ea936aebSAl Viro 			if (res)
1382ea936aebSAl Viro 				return res == -EISDIR;
1383ea936aebSAl Viro 			flags = dentry->d_flags;
1384b8faf035SNeilBrown 		}
138562a7375eSIan Kent 
1386ea936aebSAl Viro 		if (flags & DCACHE_MOUNTED) {
1387ea936aebSAl Viro 			struct mount *mounted = __lookup_mnt(path->mnt, dentry);
1388ea936aebSAl Viro 			if (mounted) {
1389c7105365SAl Viro 				path->mnt = &mounted->mnt;
1390ea936aebSAl Viro 				dentry = path->dentry = mounted->mnt.mnt_root;
1391a3fbbde7SAl Viro 				nd->flags |= LOOKUP_JUMPED;
1392ea936aebSAl Viro 				*seqp = read_seqcount_begin(&dentry->d_seq);
1393ea936aebSAl Viro 				*inode = dentry->d_inode;
139459430262SLinus Torvalds 				/*
1395ea936aebSAl Viro 				 * We don't need to re-check ->d_seq after this
1396ea936aebSAl Viro 				 * ->d_inode read - there will be an RCU delay
1397ea936aebSAl Viro 				 * between mount hash removal and ->mnt_root
1398ea936aebSAl Viro 				 * becoming unpinned.
139959430262SLinus Torvalds 				 */
1400ea936aebSAl Viro 				flags = dentry->d_flags;
1401ea936aebSAl Viro 				continue;
14029875cf80SDavid Howells 			}
1403ea936aebSAl Viro 			if (read_seqretry(&mount_lock, nd->m_seq))
1404ea936aebSAl Viro 				return false;
1405ea936aebSAl Viro 		}
1406ea936aebSAl Viro 		return !(flags & DCACHE_NEED_AUTOMOUNT);
1407ea936aebSAl Viro 	}
1408287548e4SAl Viro }
1409287548e4SAl Viro 
1410db3c9adeSAl Viro static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry,
1411db3c9adeSAl Viro 			  struct path *path, struct inode **inode,
1412db3c9adeSAl Viro 			  unsigned int *seqp)
1413bd7c4b50SAl Viro {
14149deed3ebSAl Viro 	bool jumped;
1415db3c9adeSAl Viro 	int ret;
1416bd7c4b50SAl Viro 
1417db3c9adeSAl Viro 	path->mnt = nd->path.mnt;
1418db3c9adeSAl Viro 	path->dentry = dentry;
1419c153007bSAl Viro 	if (nd->flags & LOOKUP_RCU) {
1420c153007bSAl Viro 		unsigned int seq = *seqp;
1421c153007bSAl Viro 		if (unlikely(!*inode))
1422c153007bSAl Viro 			return -ENOENT;
1423c153007bSAl Viro 		if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
14249deed3ebSAl Viro 			return 0;
1425c153007bSAl Viro 		if (unlazy_child(nd, dentry, seq))
1426c153007bSAl Viro 			return -ECHILD;
1427c153007bSAl Viro 		// *path might've been clobbered by __follow_mount_rcu()
1428c153007bSAl Viro 		path->mnt = nd->path.mnt;
1429c153007bSAl Viro 		path->dentry = dentry;
1430c153007bSAl Viro 	}
14319deed3ebSAl Viro 	ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags);
14329deed3ebSAl Viro 	if (jumped) {
14339deed3ebSAl Viro 		if (unlikely(nd->flags & LOOKUP_NO_XDEV))
14349deed3ebSAl Viro 			ret = -EXDEV;
14359deed3ebSAl Viro 		else
14369deed3ebSAl Viro 			nd->flags |= LOOKUP_JUMPED;
14379deed3ebSAl Viro 	}
14389deed3ebSAl Viro 	if (unlikely(ret)) {
14399deed3ebSAl Viro 		dput(path->dentry);
14409deed3ebSAl Viro 		if (path->mnt != nd->path.mnt)
14419deed3ebSAl Viro 			mntput(path->mnt);
14429deed3ebSAl Viro 	} else {
1443bd7c4b50SAl Viro 		*inode = d_backing_inode(path->dentry);
1444bd7c4b50SAl Viro 		*seqp = 0; /* out of RCU mode, so the value doesn't matter */
1445bd7c4b50SAl Viro 	}
1446bd7c4b50SAl Viro 	return ret;
1447bd7c4b50SAl Viro }
1448bd7c4b50SAl Viro 
14499875cf80SDavid Howells /*
1450f4fdace9SOleg Drokin  * This looks up the name in dcache and possibly revalidates the found dentry.
1451f4fdace9SOleg Drokin  * NULL is returned if the dentry does not exist in the cache.
1452baa03890SNick Piggin  */
1453e3c13928SAl Viro static struct dentry *lookup_dcache(const struct qstr *name,
1454e3c13928SAl Viro 				    struct dentry *dir,
14556c51e513SAl Viro 				    unsigned int flags)
1456baa03890SNick Piggin {
1457a89f8337SAl Viro 	struct dentry *dentry = d_lookup(dir, name);
1458bad61189SMiklos Szeredi 	if (dentry) {
1459a89f8337SAl Viro 		int error = d_revalidate(dentry, flags);
1460bad61189SMiklos Szeredi 		if (unlikely(error <= 0)) {
146174ff0ffcSAl Viro 			if (!error)
14625542aa2fSEric W. Biederman 				d_invalidate(dentry);
1463bad61189SMiklos Szeredi 			dput(dentry);
146474ff0ffcSAl Viro 			return ERR_PTR(error);
1465bad61189SMiklos Szeredi 		}
1466bad61189SMiklos Szeredi 	}
1467baa03890SNick Piggin 	return dentry;
1468baa03890SNick Piggin }
1469baa03890SNick Piggin 
1470baa03890SNick Piggin /*
1471a03ece5fSAl Viro  * Parent directory has inode locked exclusive.  This is one
1472a03ece5fSAl Viro  * and only case when ->lookup() gets called on non in-lookup
1473a03ece5fSAl Viro  * dentries - as the matter of fact, this only gets called
1474a03ece5fSAl Viro  * when directory is guaranteed to have no in-lookup children
1475a03ece5fSAl Viro  * at all.
147644396f4bSJosef Bacik  */
1477a03ece5fSAl Viro static struct dentry *__lookup_hash(const struct qstr *name,
1478a03ece5fSAl Viro 		struct dentry *base, unsigned int flags)
147944396f4bSJosef Bacik {
1480a03ece5fSAl Viro 	struct dentry *dentry = lookup_dcache(name, base, flags);
148144396f4bSJosef Bacik 	struct dentry *old;
1482a03ece5fSAl Viro 	struct inode *dir = base->d_inode;
1483a03ece5fSAl Viro 
1484a03ece5fSAl Viro 	if (dentry)
1485a03ece5fSAl Viro 		return dentry;
148644396f4bSJosef Bacik 
148744396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
1488a03ece5fSAl Viro 	if (unlikely(IS_DEADDIR(dir)))
148944396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
1490a03ece5fSAl Viro 
1491a03ece5fSAl Viro 	dentry = d_alloc(base, name);
1492a03ece5fSAl Viro 	if (unlikely(!dentry))
1493a03ece5fSAl Viro 		return ERR_PTR(-ENOMEM);
149444396f4bSJosef Bacik 
149572bd866aSAl Viro 	old = dir->i_op->lookup(dir, dentry, flags);
149644396f4bSJosef Bacik 	if (unlikely(old)) {
149744396f4bSJosef Bacik 		dput(dentry);
149844396f4bSJosef Bacik 		dentry = old;
149944396f4bSJosef Bacik 	}
150044396f4bSJosef Bacik 	return dentry;
150144396f4bSJosef Bacik }
150244396f4bSJosef Bacik 
150320e34357SAl Viro static struct dentry *lookup_fast(struct nameidata *nd,
150420e34357SAl Viro 				  struct inode **inode,
1505254cf582SAl Viro 			          unsigned *seqp)
15061da177e4SLinus Torvalds {
150731e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
15085a18fff2SAl Viro 	int status = 1;
15099875cf80SDavid Howells 
15103cac260aSAl Viro 	/*
1511b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
15125d0f49c1SAl Viro 	 * of a false negative due to a concurrent rename, the caller is
15135d0f49c1SAl Viro 	 * going to fall back to non-racy lookup.
1514b04f784eSNick Piggin 	 */
151531e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
151631e6b01fSNick Piggin 		unsigned seq;
1517da53be12SLinus Torvalds 		dentry = __d_lookup_rcu(parent, &nd->last, &seq);
15185d0f49c1SAl Viro 		if (unlikely(!dentry)) {
15194675ac39SAl Viro 			if (unlazy_walk(nd))
152020e34357SAl Viro 				return ERR_PTR(-ECHILD);
152120e34357SAl Viro 			return NULL;
15225d0f49c1SAl Viro 		}
15235a18fff2SAl Viro 
152412f8ad4bSLinus Torvalds 		/*
152512f8ad4bSLinus Torvalds 		 * This sequence count validates that the inode matches
152612f8ad4bSLinus Torvalds 		 * the dentry name information from lookup.
152712f8ad4bSLinus Torvalds 		 */
152863afdfc7SDavid Howells 		*inode = d_backing_inode(dentry);
15295d0f49c1SAl Viro 		if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
153020e34357SAl Viro 			return ERR_PTR(-ECHILD);
153112f8ad4bSLinus Torvalds 
153212f8ad4bSLinus Torvalds 		/*
153312f8ad4bSLinus Torvalds 		 * This sequence count validates that the parent had no
153412f8ad4bSLinus Torvalds 		 * changes while we did the lookup of the dentry above.
153512f8ad4bSLinus Torvalds 		 *
153612f8ad4bSLinus Torvalds 		 * The memory barrier in read_seqcount_begin of child is
153712f8ad4bSLinus Torvalds 		 *  enough, we can use __read_seqcount_retry here.
153812f8ad4bSLinus Torvalds 		 */
15395d0f49c1SAl Viro 		if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
154020e34357SAl Viro 			return ERR_PTR(-ECHILD);
15415a18fff2SAl Viro 
1542254cf582SAl Viro 		*seqp = seq;
15434ce16ef3SAl Viro 		status = d_revalidate(dentry, nd->flags);
1544c153007bSAl Viro 		if (likely(status > 0))
154520e34357SAl Viro 			return dentry;
15464675ac39SAl Viro 		if (unlazy_child(nd, dentry, seq))
154720e34357SAl Viro 			return ERR_PTR(-ECHILD);
1548209a7fb2SAl Viro 		if (unlikely(status == -ECHILD))
1549209a7fb2SAl Viro 			/* we'd been told to redo it in non-rcu mode */
1550209a7fb2SAl Viro 			status = d_revalidate(dentry, nd->flags);
15515a18fff2SAl Viro 	} else {
1552e97cdc87SAl Viro 		dentry = __d_lookup(parent, &nd->last);
155381e6f520SAl Viro 		if (unlikely(!dentry))
155420e34357SAl Viro 			return NULL;
15554ce16ef3SAl Viro 		status = d_revalidate(dentry, nd->flags);
15565d0f49c1SAl Viro 	}
15575a18fff2SAl Viro 	if (unlikely(status <= 0)) {
1558e9742b53SAl Viro 		if (!status)
15595d0f49c1SAl Viro 			d_invalidate(dentry);
15605a18fff2SAl Viro 		dput(dentry);
156120e34357SAl Viro 		return ERR_PTR(status);
15625a18fff2SAl Viro 	}
156320e34357SAl Viro 	return dentry;
1564697f514dSMiklos Szeredi }
1565697f514dSMiklos Szeredi 
1566697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */
156788d8331aSAl Viro static struct dentry *__lookup_slow(const struct qstr *name,
1568e3c13928SAl Viro 				    struct dentry *dir,
1569e3c13928SAl Viro 				    unsigned int flags)
1570697f514dSMiklos Szeredi {
157188d8331aSAl Viro 	struct dentry *dentry, *old;
15721936386eSAl Viro 	struct inode *inode = dir->d_inode;
1573d9171b93SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
15741936386eSAl Viro 
15751936386eSAl Viro 	/* Don't go there if it's already dead */
157694bdd655SAl Viro 	if (unlikely(IS_DEADDIR(inode)))
157788d8331aSAl Viro 		return ERR_PTR(-ENOENT);
157894bdd655SAl Viro again:
1579d9171b93SAl Viro 	dentry = d_alloc_parallel(dir, name, &wq);
158094bdd655SAl Viro 	if (IS_ERR(dentry))
158188d8331aSAl Viro 		return dentry;
158294bdd655SAl Viro 	if (unlikely(!d_in_lookup(dentry))) {
1583949a852eSAl Viro 		int error = d_revalidate(dentry, flags);
1584949a852eSAl Viro 		if (unlikely(error <= 0)) {
158594bdd655SAl Viro 			if (!error) {
1586949a852eSAl Viro 				d_invalidate(dentry);
1587949a852eSAl Viro 				dput(dentry);
158894bdd655SAl Viro 				goto again;
158994bdd655SAl Viro 			}
159094bdd655SAl Viro 			dput(dentry);
1591949a852eSAl Viro 			dentry = ERR_PTR(error);
1592949a852eSAl Viro 		}
159394bdd655SAl Viro 	} else {
15941936386eSAl Viro 		old = inode->i_op->lookup(inode, dentry, flags);
159585c7f810SAl Viro 		d_lookup_done(dentry);
15961936386eSAl Viro 		if (unlikely(old)) {
15971936386eSAl Viro 			dput(dentry);
15981936386eSAl Viro 			dentry = old;
1599949a852eSAl Viro 		}
1600949a852eSAl Viro 	}
1601e3c13928SAl Viro 	return dentry;
16021da177e4SLinus Torvalds }
16031da177e4SLinus Torvalds 
160488d8331aSAl Viro static struct dentry *lookup_slow(const struct qstr *name,
160588d8331aSAl Viro 				  struct dentry *dir,
160688d8331aSAl Viro 				  unsigned int flags)
160788d8331aSAl Viro {
160888d8331aSAl Viro 	struct inode *inode = dir->d_inode;
160988d8331aSAl Viro 	struct dentry *res;
161088d8331aSAl Viro 	inode_lock_shared(inode);
161188d8331aSAl Viro 	res = __lookup_slow(name, dir, flags);
161288d8331aSAl Viro 	inode_unlock_shared(inode);
161388d8331aSAl Viro 	return res;
161488d8331aSAl Viro }
161588d8331aSAl Viro 
161652094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
161752094c8aSAl Viro {
161852094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
1619*47291baaSChristian Brauner 		int err = inode_permission(&init_user_ns, nd->inode,
1620*47291baaSChristian Brauner 					   MAY_EXEC | MAY_NOT_BLOCK);
162152094c8aSAl Viro 		if (err != -ECHILD)
162252094c8aSAl Viro 			return err;
16234675ac39SAl Viro 		if (unlazy_walk(nd))
162452094c8aSAl Viro 			return -ECHILD;
162552094c8aSAl Viro 	}
1626*47291baaSChristian Brauner 	return inode_permission(&init_user_ns, nd->inode, MAY_EXEC);
162752094c8aSAl Viro }
162852094c8aSAl Viro 
162949055906SAl Viro static int reserve_stack(struct nameidata *nd, struct path *link, unsigned seq)
1630d63ff28fSAl Viro {
163149055906SAl Viro 	if (unlikely(nd->total_link_count++ >= MAXSYMLINKS))
163249055906SAl Viro 		return -ELOOP;
16334542576bSAl Viro 
16344542576bSAl Viro 	if (likely(nd->depth != EMBEDDED_LEVELS))
16354542576bSAl Viro 		return 0;
16364542576bSAl Viro 	if (likely(nd->stack != nd->internal))
16374542576bSAl Viro 		return 0;
163860ef60c7SAl Viro 	if (likely(nd_alloc_stack(nd)))
163949055906SAl Viro 		return 0;
164060ef60c7SAl Viro 
164160ef60c7SAl Viro 	if (nd->flags & LOOKUP_RCU) {
164260ef60c7SAl Viro 		// we need to grab link before we do unlazy.  And we can't skip
164360ef60c7SAl Viro 		// unlazy even if we fail to grab the link - cleanup needs it
1644aef9404dSAl Viro 		bool grabbed_link = legitimize_path(nd, link, seq);
164560ef60c7SAl Viro 
164660ef60c7SAl Viro 		if (unlazy_walk(nd) != 0 || !grabbed_link)
164760ef60c7SAl Viro 			return -ECHILD;
164860ef60c7SAl Viro 
164960ef60c7SAl Viro 		if (nd_alloc_stack(nd))
165060ef60c7SAl Viro 			return 0;
1651bc40aee0SAl Viro 	}
165260ef60c7SAl Viro 	return -ENOMEM;
165349055906SAl Viro }
165449055906SAl Viro 
165549055906SAl Viro enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4};
165649055906SAl Viro 
165749055906SAl Viro static const char *pick_link(struct nameidata *nd, struct path *link,
165849055906SAl Viro 		     struct inode *inode, unsigned seq, int flags)
165949055906SAl Viro {
166049055906SAl Viro 	struct saved *last;
166149055906SAl Viro 	const char *res;
166249055906SAl Viro 	int error = reserve_stack(nd, link, seq);
166349055906SAl Viro 
166449055906SAl Viro 	if (unlikely(error)) {
166549055906SAl Viro 		if (!(nd->flags & LOOKUP_RCU))
1666cd179f44SAl Viro 			path_put(link);
166706708adbSAl Viro 		return ERR_PTR(error);
1668626de996SAl Viro 	}
1669ab104923SAl Viro 	last = nd->stack + nd->depth++;
16701cf2665bSAl Viro 	last->link = *link;
1671fceef393SAl Viro 	clear_delayed_call(&last->done);
16720450b2d1SAl Viro 	last->seq = seq;
1673ad6cc4c3SAl Viro 
1674b1a81972SAl Viro 	if (flags & WALK_TRAILING) {
1675ad6cc4c3SAl Viro 		error = may_follow_link(nd, inode);
1676ad6cc4c3SAl Viro 		if (unlikely(error))
1677ad6cc4c3SAl Viro 			return ERR_PTR(error);
1678ad6cc4c3SAl Viro 	}
1679ad6cc4c3SAl Viro 
1680dab741e0SMattias Nissler 	if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) ||
1681dab741e0SMattias Nissler 			unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW))
1682ad6cc4c3SAl Viro 		return ERR_PTR(-ELOOP);
1683ad6cc4c3SAl Viro 
1684ad6cc4c3SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1685ad6cc4c3SAl Viro 		touch_atime(&last->link);
1686ad6cc4c3SAl Viro 		cond_resched();
1687ad6cc4c3SAl Viro 	} else if (atime_needs_update(&last->link, inode)) {
1688ad6cc4c3SAl Viro 		if (unlikely(unlazy_walk(nd)))
1689ad6cc4c3SAl Viro 			return ERR_PTR(-ECHILD);
1690ad6cc4c3SAl Viro 		touch_atime(&last->link);
1691ad6cc4c3SAl Viro 	}
1692ad6cc4c3SAl Viro 
1693ad6cc4c3SAl Viro 	error = security_inode_follow_link(link->dentry, inode,
1694ad6cc4c3SAl Viro 					   nd->flags & LOOKUP_RCU);
1695ad6cc4c3SAl Viro 	if (unlikely(error))
1696ad6cc4c3SAl Viro 		return ERR_PTR(error);
1697ad6cc4c3SAl Viro 
1698ad6cc4c3SAl Viro 	res = READ_ONCE(inode->i_link);
1699ad6cc4c3SAl Viro 	if (!res) {
1700ad6cc4c3SAl Viro 		const char * (*get)(struct dentry *, struct inode *,
1701ad6cc4c3SAl Viro 				struct delayed_call *);
1702ad6cc4c3SAl Viro 		get = inode->i_op->get_link;
1703ad6cc4c3SAl Viro 		if (nd->flags & LOOKUP_RCU) {
1704ad6cc4c3SAl Viro 			res = get(NULL, inode, &last->done);
1705ad6cc4c3SAl Viro 			if (res == ERR_PTR(-ECHILD)) {
1706ad6cc4c3SAl Viro 				if (unlikely(unlazy_walk(nd)))
1707ad6cc4c3SAl Viro 					return ERR_PTR(-ECHILD);
1708ad6cc4c3SAl Viro 				res = get(link->dentry, inode, &last->done);
1709ad6cc4c3SAl Viro 			}
1710ad6cc4c3SAl Viro 		} else {
1711ad6cc4c3SAl Viro 			res = get(link->dentry, inode, &last->done);
1712ad6cc4c3SAl Viro 		}
1713ad6cc4c3SAl Viro 		if (!res)
1714ad6cc4c3SAl Viro 			goto all_done;
1715ad6cc4c3SAl Viro 		if (IS_ERR(res))
1716ad6cc4c3SAl Viro 			return res;
1717ad6cc4c3SAl Viro 	}
1718ad6cc4c3SAl Viro 	if (*res == '/') {
1719ad6cc4c3SAl Viro 		error = nd_jump_root(nd);
1720ad6cc4c3SAl Viro 		if (unlikely(error))
1721ad6cc4c3SAl Viro 			return ERR_PTR(error);
1722ad6cc4c3SAl Viro 		while (unlikely(*++res == '/'))
1723ad6cc4c3SAl Viro 			;
1724ad6cc4c3SAl Viro 	}
1725ad6cc4c3SAl Viro 	if (*res)
1726ad6cc4c3SAl Viro 		return res;
1727ad6cc4c3SAl Viro all_done: // pure jump
1728ad6cc4c3SAl Viro 	put_link(nd);
1729ad6cc4c3SAl Viro 	return NULL;
1730d63ff28fSAl Viro }
1731d63ff28fSAl Viro 
17323ddcd056SLinus Torvalds /*
17333ddcd056SLinus Torvalds  * Do we need to follow links? We _really_ want to be able
17343ddcd056SLinus Torvalds  * to do this check without having to look at inode->i_op,
17353ddcd056SLinus Torvalds  * so we keep a cache of "no, this doesn't need follow_link"
17363ddcd056SLinus Torvalds  * for the common case.
17373ddcd056SLinus Torvalds  */
1738b0417d2cSAl Viro static const char *step_into(struct nameidata *nd, int flags,
1739cbae4d12SAl Viro 		     struct dentry *dentry, struct inode *inode, unsigned seq)
17403ddcd056SLinus Torvalds {
1741cbae4d12SAl Viro 	struct path path;
1742cbae4d12SAl Viro 	int err = handle_mounts(nd, dentry, &path, &inode, &seq);
1743cbae4d12SAl Viro 
1744cbae4d12SAl Viro 	if (err < 0)
1745b0417d2cSAl Viro 		return ERR_PTR(err);
1746cbae4d12SAl Viro 	if (likely(!d_is_symlink(path.dentry)) ||
17478c4efe22SAl Viro 	   ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) ||
1748aca2903eSAl Viro 	   (flags & WALK_NOFOLLOW)) {
17498f64fb1cSAl Viro 		/* not a symlink or should not follow */
1750c99687a0SAl Viro 		if (!(nd->flags & LOOKUP_RCU)) {
1751c99687a0SAl Viro 			dput(nd->path.dentry);
1752c99687a0SAl Viro 			if (nd->path.mnt != path.mnt)
1753c99687a0SAl Viro 				mntput(nd->path.mnt);
1754c99687a0SAl Viro 		}
1755c99687a0SAl Viro 		nd->path = path;
17568f64fb1cSAl Viro 		nd->inode = inode;
17578f64fb1cSAl Viro 		nd->seq = seq;
1758b0417d2cSAl Viro 		return NULL;
17598f64fb1cSAl Viro 	}
1760a7f77542SAl Viro 	if (nd->flags & LOOKUP_RCU) {
176184f0cd9eSAl Viro 		/* make sure that d_is_symlink above matches inode */
1762cbae4d12SAl Viro 		if (read_seqcount_retry(&path.dentry->d_seq, seq))
1763b0417d2cSAl Viro 			return ERR_PTR(-ECHILD);
176484f0cd9eSAl Viro 	} else {
176584f0cd9eSAl Viro 		if (path.mnt == nd->path.mnt)
176684f0cd9eSAl Viro 			mntget(path.mnt);
1767a7f77542SAl Viro 	}
1768b1a81972SAl Viro 	return pick_link(nd, &path, inode, seq, flags);
17693ddcd056SLinus Torvalds }
17703ddcd056SLinus Torvalds 
1771c2df1968SAl Viro static struct dentry *follow_dotdot_rcu(struct nameidata *nd,
1772c2df1968SAl Viro 					struct inode **inodep,
1773c2df1968SAl Viro 					unsigned *seqp)
1774957dd41dSAl Viro {
177512487f30SAl Viro 	struct dentry *parent, *old;
1776957dd41dSAl Viro 
177712487f30SAl Viro 	if (path_equal(&nd->path, &nd->root))
177812487f30SAl Viro 		goto in_root;
177912487f30SAl Viro 	if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
17807ef482faSAl Viro 		struct path path;
1781efe772d6SAl Viro 		unsigned seq;
17827ef482faSAl Viro 		if (!choose_mountpoint_rcu(real_mount(nd->path.mnt),
17837ef482faSAl Viro 					   &nd->root, &path, &seq))
178412487f30SAl Viro 			goto in_root;
1785efe772d6SAl Viro 		if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1786efe772d6SAl Viro 			return ERR_PTR(-ECHILD);
1787efe772d6SAl Viro 		nd->path = path;
1788efe772d6SAl Viro 		nd->inode = path.dentry->d_inode;
1789efe772d6SAl Viro 		nd->seq = seq;
1790efe772d6SAl Viro 		if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1791efe772d6SAl Viro 			return ERR_PTR(-ECHILD);
1792efe772d6SAl Viro 		/* we know that mountpoint was pinned */
1793957dd41dSAl Viro 	}
179412487f30SAl Viro 	old = nd->path.dentry;
179512487f30SAl Viro 	parent = old->d_parent;
179612487f30SAl Viro 	*inodep = parent->d_inode;
179712487f30SAl Viro 	*seqp = read_seqcount_begin(&parent->d_seq);
179812487f30SAl Viro 	if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
179912487f30SAl Viro 		return ERR_PTR(-ECHILD);
180012487f30SAl Viro 	if (unlikely(!path_connected(nd->path.mnt, parent)))
180112487f30SAl Viro 		return ERR_PTR(-ECHILD);
180212487f30SAl Viro 	return parent;
180312487f30SAl Viro in_root:
1804efe772d6SAl Viro 	if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1805efe772d6SAl Viro 		return ERR_PTR(-ECHILD);
1806957dd41dSAl Viro 	if (unlikely(nd->flags & LOOKUP_BENEATH))
18077521f22bSAl Viro 		return ERR_PTR(-ECHILD);
1808c2df1968SAl Viro 	return NULL;
1809957dd41dSAl Viro }
1810957dd41dSAl Viro 
1811c2df1968SAl Viro static struct dentry *follow_dotdot(struct nameidata *nd,
1812c2df1968SAl Viro 				 struct inode **inodep,
1813c2df1968SAl Viro 				 unsigned *seqp)
1814957dd41dSAl Viro {
181512487f30SAl Viro 	struct dentry *parent;
181612487f30SAl Viro 
1817957dd41dSAl Viro 	if (path_equal(&nd->path, &nd->root))
181812487f30SAl Viro 		goto in_root;
181912487f30SAl Viro 	if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
18202aa38470SAl Viro 		struct path path;
18212aa38470SAl Viro 
18222aa38470SAl Viro 		if (!choose_mountpoint(real_mount(nd->path.mnt),
18232aa38470SAl Viro 				       &nd->root, &path))
182412487f30SAl Viro 			goto in_root;
1825165200d6SAl Viro 		path_put(&nd->path);
1826165200d6SAl Viro 		nd->path = path;
18272aa38470SAl Viro 		nd->inode = path.dentry->d_inode;
1828165200d6SAl Viro 		if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1829165200d6SAl Viro 			return ERR_PTR(-EXDEV);
183012487f30SAl Viro 	}
1831957dd41dSAl Viro 	/* rare case of legitimate dget_parent()... */
183212487f30SAl Viro 	parent = dget_parent(nd->path.dentry);
1833957dd41dSAl Viro 	if (unlikely(!path_connected(nd->path.mnt, parent))) {
1834957dd41dSAl Viro 		dput(parent);
18357521f22bSAl Viro 		return ERR_PTR(-ENOENT);
1836957dd41dSAl Viro 	}
1837c2df1968SAl Viro 	*seqp = 0;
1838c2df1968SAl Viro 	*inodep = parent->d_inode;
1839c2df1968SAl Viro 	return parent;
184012487f30SAl Viro 
184112487f30SAl Viro in_root:
1842957dd41dSAl Viro 	if (unlikely(nd->flags & LOOKUP_BENEATH))
18437521f22bSAl Viro 		return ERR_PTR(-EXDEV);
1844c2df1968SAl Viro 	dget(nd->path.dentry);
1845c2df1968SAl Viro 	return NULL;
1846957dd41dSAl Viro }
1847957dd41dSAl Viro 
18487521f22bSAl Viro static const char *handle_dots(struct nameidata *nd, int type)
1849957dd41dSAl Viro {
1850957dd41dSAl Viro 	if (type == LAST_DOTDOT) {
18517521f22bSAl Viro 		const char *error = NULL;
1852c2df1968SAl Viro 		struct dentry *parent;
1853c2df1968SAl Viro 		struct inode *inode;
1854c2df1968SAl Viro 		unsigned seq;
1855957dd41dSAl Viro 
1856957dd41dSAl Viro 		if (!nd->root.mnt) {
18577521f22bSAl Viro 			error = ERR_PTR(set_root(nd));
1858957dd41dSAl Viro 			if (error)
1859957dd41dSAl Viro 				return error;
1860957dd41dSAl Viro 		}
1861957dd41dSAl Viro 		if (nd->flags & LOOKUP_RCU)
1862c2df1968SAl Viro 			parent = follow_dotdot_rcu(nd, &inode, &seq);
1863957dd41dSAl Viro 		else
1864c2df1968SAl Viro 			parent = follow_dotdot(nd, &inode, &seq);
1865c2df1968SAl Viro 		if (IS_ERR(parent))
1866c2df1968SAl Viro 			return ERR_CAST(parent);
1867c2df1968SAl Viro 		if (unlikely(!parent))
1868c2df1968SAl Viro 			error = step_into(nd, WALK_NOFOLLOW,
1869c2df1968SAl Viro 					 nd->path.dentry, nd->inode, nd->seq);
1870c2df1968SAl Viro 		else
1871c2df1968SAl Viro 			error = step_into(nd, WALK_NOFOLLOW,
1872c2df1968SAl Viro 					 parent, inode, seq);
1873c2df1968SAl Viro 		if (unlikely(error))
1874957dd41dSAl Viro 			return error;
1875957dd41dSAl Viro 
1876957dd41dSAl Viro 		if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
1877957dd41dSAl Viro 			/*
1878957dd41dSAl Viro 			 * If there was a racing rename or mount along our
1879957dd41dSAl Viro 			 * path, then we can't be sure that ".." hasn't jumped
1880957dd41dSAl Viro 			 * above nd->root (and so userspace should retry or use
1881957dd41dSAl Viro 			 * some fallback).
1882957dd41dSAl Viro 			 */
1883957dd41dSAl Viro 			smp_rmb();
1884957dd41dSAl Viro 			if (unlikely(__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq)))
18857521f22bSAl Viro 				return ERR_PTR(-EAGAIN);
1886957dd41dSAl Viro 			if (unlikely(__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq)))
18877521f22bSAl Viro 				return ERR_PTR(-EAGAIN);
1888957dd41dSAl Viro 		}
1889957dd41dSAl Viro 	}
18907521f22bSAl Viro 	return NULL;
1891957dd41dSAl Viro }
1892957dd41dSAl Viro 
189392d27016SAl Viro static const char *walk_component(struct nameidata *nd, int flags)
1894ce57dfc1SAl Viro {
1895db3c9adeSAl Viro 	struct dentry *dentry;
1896ce57dfc1SAl Viro 	struct inode *inode;
1897254cf582SAl Viro 	unsigned seq;
1898ce57dfc1SAl Viro 	/*
1899ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1900ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1901ce57dfc1SAl Viro 	 * parent relationships.
1902ce57dfc1SAl Viro 	 */
19034693a547SAl Viro 	if (unlikely(nd->last_type != LAST_NORM)) {
19041c4ff1a8SAl Viro 		if (!(flags & WALK_MORE) && nd->depth)
19054693a547SAl Viro 			put_link(nd);
19067521f22bSAl Viro 		return handle_dots(nd, nd->last_type);
19074693a547SAl Viro 	}
190820e34357SAl Viro 	dentry = lookup_fast(nd, &inode, &seq);
190920e34357SAl Viro 	if (IS_ERR(dentry))
191092d27016SAl Viro 		return ERR_CAST(dentry);
191120e34357SAl Viro 	if (unlikely(!dentry)) {
1912db3c9adeSAl Viro 		dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags);
1913db3c9adeSAl Viro 		if (IS_ERR(dentry))
191492d27016SAl Viro 			return ERR_CAST(dentry);
191520e34357SAl Viro 	}
191656676ec3SAl Viro 	if (!(flags & WALK_MORE) && nd->depth)
191756676ec3SAl Viro 		put_link(nd);
1918b0417d2cSAl Viro 	return step_into(nd, flags, dentry, inode, seq);
1919ce57dfc1SAl Viro }
1920ce57dfc1SAl Viro 
19211da177e4SLinus Torvalds /*
1922bfcfaa77SLinus Torvalds  * We can do the critical dentry name comparison and hashing
1923bfcfaa77SLinus Torvalds  * operations one word at a time, but we are limited to:
1924bfcfaa77SLinus Torvalds  *
1925bfcfaa77SLinus Torvalds  * - Architectures with fast unaligned word accesses. We could
1926bfcfaa77SLinus Torvalds  *   do a "get_unaligned()" if this helps and is sufficiently
1927bfcfaa77SLinus Torvalds  *   fast.
1928bfcfaa77SLinus Torvalds  *
1929bfcfaa77SLinus Torvalds  * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1930bfcfaa77SLinus Torvalds  *   do not trap on the (extremely unlikely) case of a page
1931bfcfaa77SLinus Torvalds  *   crossing operation.
1932bfcfaa77SLinus Torvalds  *
1933bfcfaa77SLinus Torvalds  * - Furthermore, we need an efficient 64-bit compile for the
1934bfcfaa77SLinus Torvalds  *   64-bit case in order to generate the "number of bytes in
1935bfcfaa77SLinus Torvalds  *   the final mask". Again, that could be replaced with a
1936bfcfaa77SLinus Torvalds  *   efficient population count instruction or similar.
1937bfcfaa77SLinus Torvalds  */
1938bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS
1939bfcfaa77SLinus Torvalds 
1940f68e556eSLinus Torvalds #include <asm/word-at-a-time.h>
1941bfcfaa77SLinus Torvalds 
1942468a9428SGeorge Spelvin #ifdef HASH_MIX
1943bfcfaa77SLinus Torvalds 
1944468a9428SGeorge Spelvin /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
1945468a9428SGeorge Spelvin 
1946468a9428SGeorge Spelvin #elif defined(CONFIG_64BIT)
19472a18da7aSGeorge Spelvin /*
19482a18da7aSGeorge Spelvin  * Register pressure in the mixing function is an issue, particularly
19492a18da7aSGeorge Spelvin  * on 32-bit x86, but almost any function requires one state value and
19502a18da7aSGeorge Spelvin  * one temporary.  Instead, use a function designed for two state values
19512a18da7aSGeorge Spelvin  * and no temporaries.
19522a18da7aSGeorge Spelvin  *
19532a18da7aSGeorge Spelvin  * This function cannot create a collision in only two iterations, so
19542a18da7aSGeorge Spelvin  * we have two iterations to achieve avalanche.  In those two iterations,
19552a18da7aSGeorge Spelvin  * we have six layers of mixing, which is enough to spread one bit's
19562a18da7aSGeorge Spelvin  * influence out to 2^6 = 64 state bits.
19572a18da7aSGeorge Spelvin  *
19582a18da7aSGeorge Spelvin  * Rotate constants are scored by considering either 64 one-bit input
19592a18da7aSGeorge Spelvin  * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
19602a18da7aSGeorge Spelvin  * probability of that delta causing a change to each of the 128 output
19612a18da7aSGeorge Spelvin  * bits, using a sample of random initial states.
19622a18da7aSGeorge Spelvin  *
19632a18da7aSGeorge Spelvin  * The Shannon entropy of the computed probabilities is then summed
19642a18da7aSGeorge Spelvin  * to produce a score.  Ideally, any input change has a 50% chance of
19652a18da7aSGeorge Spelvin  * toggling any given output bit.
19662a18da7aSGeorge Spelvin  *
19672a18da7aSGeorge Spelvin  * Mixing scores (in bits) for (12,45):
19682a18da7aSGeorge Spelvin  * Input delta: 1-bit      2-bit
19692a18da7aSGeorge Spelvin  * 1 round:     713.3    42542.6
19702a18da7aSGeorge Spelvin  * 2 rounds:   2753.7   140389.8
19712a18da7aSGeorge Spelvin  * 3 rounds:   5954.1   233458.2
19722a18da7aSGeorge Spelvin  * 4 rounds:   7862.6   256672.2
19732a18da7aSGeorge Spelvin  * Perfect:    8192     258048
19742a18da7aSGeorge Spelvin  *            (64*128) (64*63/2 * 128)
19752a18da7aSGeorge Spelvin  */
19762a18da7aSGeorge Spelvin #define HASH_MIX(x, y, a)	\
19772a18da7aSGeorge Spelvin 	(	x ^= (a),	\
19782a18da7aSGeorge Spelvin 	y ^= x,	x = rol64(x,12),\
19792a18da7aSGeorge Spelvin 	x += y,	y = rol64(y,45),\
19802a18da7aSGeorge Spelvin 	y *= 9			)
1981bfcfaa77SLinus Torvalds 
19820fed3ac8SGeorge Spelvin /*
19832a18da7aSGeorge Spelvin  * Fold two longs into one 32-bit hash value.  This must be fast, but
19842a18da7aSGeorge Spelvin  * latency isn't quite as critical, as there is a fair bit of additional
19852a18da7aSGeorge Spelvin  * work done before the hash value is used.
19860fed3ac8SGeorge Spelvin  */
19872a18da7aSGeorge Spelvin static inline unsigned int fold_hash(unsigned long x, unsigned long y)
19880fed3ac8SGeorge Spelvin {
19892a18da7aSGeorge Spelvin 	y ^= x * GOLDEN_RATIO_64;
19902a18da7aSGeorge Spelvin 	y *= GOLDEN_RATIO_64;
19912a18da7aSGeorge Spelvin 	return y >> 32;
19920fed3ac8SGeorge Spelvin }
19930fed3ac8SGeorge Spelvin 
1994bfcfaa77SLinus Torvalds #else	/* 32-bit case */
1995bfcfaa77SLinus Torvalds 
19962a18da7aSGeorge Spelvin /*
19972a18da7aSGeorge Spelvin  * Mixing scores (in bits) for (7,20):
19982a18da7aSGeorge Spelvin  * Input delta: 1-bit      2-bit
19992a18da7aSGeorge Spelvin  * 1 round:     330.3     9201.6
20002a18da7aSGeorge Spelvin  * 2 rounds:   1246.4    25475.4
20012a18da7aSGeorge Spelvin  * 3 rounds:   1907.1    31295.1
20022a18da7aSGeorge Spelvin  * 4 rounds:   2042.3    31718.6
20032a18da7aSGeorge Spelvin  * Perfect:    2048      31744
20042a18da7aSGeorge Spelvin  *            (32*64)   (32*31/2 * 64)
20052a18da7aSGeorge Spelvin  */
20062a18da7aSGeorge Spelvin #define HASH_MIX(x, y, a)	\
20072a18da7aSGeorge Spelvin 	(	x ^= (a),	\
20082a18da7aSGeorge Spelvin 	y ^= x,	x = rol32(x, 7),\
20092a18da7aSGeorge Spelvin 	x += y,	y = rol32(y,20),\
20102a18da7aSGeorge Spelvin 	y *= 9			)
2011bfcfaa77SLinus Torvalds 
20122a18da7aSGeorge Spelvin static inline unsigned int fold_hash(unsigned long x, unsigned long y)
20130fed3ac8SGeorge Spelvin {
20142a18da7aSGeorge Spelvin 	/* Use arch-optimized multiply if one exists */
20152a18da7aSGeorge Spelvin 	return __hash_32(y ^ __hash_32(x));
20160fed3ac8SGeorge Spelvin }
20170fed3ac8SGeorge Spelvin 
2018bfcfaa77SLinus Torvalds #endif
2019bfcfaa77SLinus Torvalds 
20202a18da7aSGeorge Spelvin /*
20212a18da7aSGeorge Spelvin  * Return the hash of a string of known length.  This is carfully
20222a18da7aSGeorge Spelvin  * designed to match hash_name(), which is the more critical function.
20232a18da7aSGeorge Spelvin  * In particular, we must end by hashing a final word containing 0..7
20242a18da7aSGeorge Spelvin  * payload bytes, to match the way that hash_name() iterates until it
20252a18da7aSGeorge Spelvin  * finds the delimiter after the name.
20262a18da7aSGeorge Spelvin  */
20278387ff25SLinus Torvalds unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
2028bfcfaa77SLinus Torvalds {
20298387ff25SLinus Torvalds 	unsigned long a, x = 0, y = (unsigned long)salt;
2030bfcfaa77SLinus Torvalds 
2031bfcfaa77SLinus Torvalds 	for (;;) {
2032fcfd2fbfSGeorge Spelvin 		if (!len)
2033fcfd2fbfSGeorge Spelvin 			goto done;
2034e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name);
2035bfcfaa77SLinus Torvalds 		if (len < sizeof(unsigned long))
2036bfcfaa77SLinus Torvalds 			break;
20372a18da7aSGeorge Spelvin 		HASH_MIX(x, y, a);
2038bfcfaa77SLinus Torvalds 		name += sizeof(unsigned long);
2039bfcfaa77SLinus Torvalds 		len -= sizeof(unsigned long);
2040bfcfaa77SLinus Torvalds 	}
20412a18da7aSGeorge Spelvin 	x ^= a & bytemask_from_count(len);
2042bfcfaa77SLinus Torvalds done:
20432a18da7aSGeorge Spelvin 	return fold_hash(x, y);
2044bfcfaa77SLinus Torvalds }
2045bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
2046bfcfaa77SLinus Torvalds 
2047fcfd2fbfSGeorge Spelvin /* Return the "hash_len" (hash and length) of a null-terminated string */
20488387ff25SLinus Torvalds u64 hashlen_string(const void *salt, const char *name)
2049fcfd2fbfSGeorge Spelvin {
20508387ff25SLinus Torvalds 	unsigned long a = 0, x = 0, y = (unsigned long)salt;
20518387ff25SLinus Torvalds 	unsigned long adata, mask, len;
2052fcfd2fbfSGeorge Spelvin 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
2053fcfd2fbfSGeorge Spelvin 
20548387ff25SLinus Torvalds 	len = 0;
20558387ff25SLinus Torvalds 	goto inside;
20568387ff25SLinus Torvalds 
2057fcfd2fbfSGeorge Spelvin 	do {
20582a18da7aSGeorge Spelvin 		HASH_MIX(x, y, a);
2059fcfd2fbfSGeorge Spelvin 		len += sizeof(unsigned long);
20608387ff25SLinus Torvalds inside:
2061fcfd2fbfSGeorge Spelvin 		a = load_unaligned_zeropad(name+len);
2062fcfd2fbfSGeorge Spelvin 	} while (!has_zero(a, &adata, &constants));
2063fcfd2fbfSGeorge Spelvin 
2064fcfd2fbfSGeorge Spelvin 	adata = prep_zero_mask(a, adata, &constants);
2065fcfd2fbfSGeorge Spelvin 	mask = create_zero_mask(adata);
20662a18da7aSGeorge Spelvin 	x ^= a & zero_bytemask(mask);
2067fcfd2fbfSGeorge Spelvin 
20682a18da7aSGeorge Spelvin 	return hashlen_create(fold_hash(x, y), len + find_zero(mask));
2069fcfd2fbfSGeorge Spelvin }
2070fcfd2fbfSGeorge Spelvin EXPORT_SYMBOL(hashlen_string);
2071fcfd2fbfSGeorge Spelvin 
2072bfcfaa77SLinus Torvalds /*
2073bfcfaa77SLinus Torvalds  * Calculate the length and hash of the path component, and
2074d6bb3e90SLinus Torvalds  * return the "hash_len" as the result.
2075bfcfaa77SLinus Torvalds  */
20768387ff25SLinus Torvalds static inline u64 hash_name(const void *salt, const char *name)
2077bfcfaa77SLinus Torvalds {
20788387ff25SLinus Torvalds 	unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
20798387ff25SLinus Torvalds 	unsigned long adata, bdata, mask, len;
208036126f8fSLinus Torvalds 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
2081bfcfaa77SLinus Torvalds 
20828387ff25SLinus Torvalds 	len = 0;
20838387ff25SLinus Torvalds 	goto inside;
20848387ff25SLinus Torvalds 
2085bfcfaa77SLinus Torvalds 	do {
20862a18da7aSGeorge Spelvin 		HASH_MIX(x, y, a);
2087bfcfaa77SLinus Torvalds 		len += sizeof(unsigned long);
20888387ff25SLinus Torvalds inside:
2089e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name+len);
209036126f8fSLinus Torvalds 		b = a ^ REPEAT_BYTE('/');
209136126f8fSLinus Torvalds 	} while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
2092bfcfaa77SLinus Torvalds 
209336126f8fSLinus Torvalds 	adata = prep_zero_mask(a, adata, &constants);
209436126f8fSLinus Torvalds 	bdata = prep_zero_mask(b, bdata, &constants);
209536126f8fSLinus Torvalds 	mask = create_zero_mask(adata | bdata);
20962a18da7aSGeorge Spelvin 	x ^= a & zero_bytemask(mask);
209736126f8fSLinus Torvalds 
20982a18da7aSGeorge Spelvin 	return hashlen_create(fold_hash(x, y), len + find_zero(mask));
2099bfcfaa77SLinus Torvalds }
2100bfcfaa77SLinus Torvalds 
21012a18da7aSGeorge Spelvin #else	/* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
2102bfcfaa77SLinus Torvalds 
2103fcfd2fbfSGeorge Spelvin /* Return the hash of a string of known length */
21048387ff25SLinus Torvalds unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
21050145acc2SLinus Torvalds {
21068387ff25SLinus Torvalds 	unsigned long hash = init_name_hash(salt);
21070145acc2SLinus Torvalds 	while (len--)
2108fcfd2fbfSGeorge Spelvin 		hash = partial_name_hash((unsigned char)*name++, hash);
21090145acc2SLinus Torvalds 	return end_name_hash(hash);
21100145acc2SLinus Torvalds }
2111ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
21120145acc2SLinus Torvalds 
2113fcfd2fbfSGeorge Spelvin /* Return the "hash_len" (hash and length) of a null-terminated string */
21148387ff25SLinus Torvalds u64 hashlen_string(const void *salt, const char *name)
2115fcfd2fbfSGeorge Spelvin {
21168387ff25SLinus Torvalds 	unsigned long hash = init_name_hash(salt);
2117fcfd2fbfSGeorge Spelvin 	unsigned long len = 0, c;
2118fcfd2fbfSGeorge Spelvin 
2119fcfd2fbfSGeorge Spelvin 	c = (unsigned char)*name;
2120e0ab7af9SGeorge Spelvin 	while (c) {
2121fcfd2fbfSGeorge Spelvin 		len++;
2122fcfd2fbfSGeorge Spelvin 		hash = partial_name_hash(c, hash);
2123fcfd2fbfSGeorge Spelvin 		c = (unsigned char)name[len];
2124e0ab7af9SGeorge Spelvin 	}
2125fcfd2fbfSGeorge Spelvin 	return hashlen_create(end_name_hash(hash), len);
2126fcfd2fbfSGeorge Spelvin }
2127f2a031b6SGeorge Spelvin EXPORT_SYMBOL(hashlen_string);
2128fcfd2fbfSGeorge Spelvin 
21293ddcd056SLinus Torvalds /*
2130200e9ef7SLinus Torvalds  * We know there's a real path component here of at least
2131200e9ef7SLinus Torvalds  * one character.
2132200e9ef7SLinus Torvalds  */
21338387ff25SLinus Torvalds static inline u64 hash_name(const void *salt, const char *name)
2134200e9ef7SLinus Torvalds {
21358387ff25SLinus Torvalds 	unsigned long hash = init_name_hash(salt);
2136200e9ef7SLinus Torvalds 	unsigned long len = 0, c;
2137200e9ef7SLinus Torvalds 
2138200e9ef7SLinus Torvalds 	c = (unsigned char)*name;
2139200e9ef7SLinus Torvalds 	do {
2140200e9ef7SLinus Torvalds 		len++;
2141200e9ef7SLinus Torvalds 		hash = partial_name_hash(c, hash);
2142200e9ef7SLinus Torvalds 		c = (unsigned char)name[len];
2143200e9ef7SLinus Torvalds 	} while (c && c != '/');
2144d6bb3e90SLinus Torvalds 	return hashlen_create(end_name_hash(hash), len);
2145200e9ef7SLinus Torvalds }
2146200e9ef7SLinus Torvalds 
2147bfcfaa77SLinus Torvalds #endif
2148bfcfaa77SLinus Torvalds 
2149200e9ef7SLinus Torvalds /*
21501da177e4SLinus Torvalds  * Name resolution.
2151ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
2152ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
21531da177e4SLinus Torvalds  *
2154ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
2155ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
21561da177e4SLinus Torvalds  */
21576de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
21581da177e4SLinus Torvalds {
2159d8d4611aSAl Viro 	int depth = 0; // depth <= nd->depth
21601da177e4SLinus Torvalds 	int err;
21611da177e4SLinus Torvalds 
2162b4c03536SAl Viro 	nd->last_type = LAST_ROOT;
2163c108837eSAl Viro 	nd->flags |= LOOKUP_PARENT;
21649b5858e9SAl Viro 	if (IS_ERR(name))
21659b5858e9SAl Viro 		return PTR_ERR(name);
21661da177e4SLinus Torvalds 	while (*name=='/')
21671da177e4SLinus Torvalds 		name++;
21681a97d899SAl Viro 	if (!*name) {
21691a97d899SAl Viro 		nd->dir_mode = 0; // short-circuit the 'hardening' idiocy
21709e18f10aSAl Viro 		return 0;
21711a97d899SAl Viro 	}
21721da177e4SLinus Torvalds 
21731da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
21741da177e4SLinus Torvalds 	for(;;) {
217592d27016SAl Viro 		const char *link;
2176d6bb3e90SLinus Torvalds 		u64 hash_len;
2177fe479a58SAl Viro 		int type;
21781da177e4SLinus Torvalds 
217952094c8aSAl Viro 		err = may_lookup(nd);
21801da177e4SLinus Torvalds 		if (err)
21813595e234SAl Viro 			return err;
21821da177e4SLinus Torvalds 
21838387ff25SLinus Torvalds 		hash_len = hash_name(nd->path.dentry, name);
21841da177e4SLinus Torvalds 
2185fe479a58SAl Viro 		type = LAST_NORM;
2186d6bb3e90SLinus Torvalds 		if (name[0] == '.') switch (hashlen_len(hash_len)) {
2187fe479a58SAl Viro 			case 2:
2188200e9ef7SLinus Torvalds 				if (name[1] == '.') {
2189fe479a58SAl Viro 					type = LAST_DOTDOT;
219016c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
219116c2cd71SAl Viro 				}
2192fe479a58SAl Viro 				break;
2193fe479a58SAl Viro 			case 1:
2194fe479a58SAl Viro 				type = LAST_DOT;
2195fe479a58SAl Viro 		}
21965a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
21975a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
219816c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
21995a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
2200a060dc50SJames Hogan 				struct qstr this = { { .hash_len = hash_len }, .name = name };
2201da53be12SLinus Torvalds 				err = parent->d_op->d_hash(parent, &this);
22025a202bcdSAl Viro 				if (err < 0)
22033595e234SAl Viro 					return err;
2204d6bb3e90SLinus Torvalds 				hash_len = this.hash_len;
2205d6bb3e90SLinus Torvalds 				name = this.name;
22065a202bcdSAl Viro 			}
22075a202bcdSAl Viro 		}
2208fe479a58SAl Viro 
2209d6bb3e90SLinus Torvalds 		nd->last.hash_len = hash_len;
2210d6bb3e90SLinus Torvalds 		nd->last.name = name;
22115f4a6a69SAl Viro 		nd->last_type = type;
22125f4a6a69SAl Viro 
2213d6bb3e90SLinus Torvalds 		name += hashlen_len(hash_len);
2214d6bb3e90SLinus Torvalds 		if (!*name)
2215bdf6cbf1SAl Viro 			goto OK;
2216200e9ef7SLinus Torvalds 		/*
2217200e9ef7SLinus Torvalds 		 * If it wasn't NUL, we know it was '/'. Skip that
2218200e9ef7SLinus Torvalds 		 * slash, and continue until no more slashes.
2219200e9ef7SLinus Torvalds 		 */
2220200e9ef7SLinus Torvalds 		do {
2221d6bb3e90SLinus Torvalds 			name++;
2222d6bb3e90SLinus Torvalds 		} while (unlikely(*name == '/'));
22238620c238SAl Viro 		if (unlikely(!*name)) {
22248620c238SAl Viro OK:
2225d8d4611aSAl Viro 			/* pathname or trailing symlink, done */
2226c108837eSAl Viro 			if (!depth) {
22270f705953SAl Viro 				nd->dir_uid = nd->inode->i_uid;
22280f705953SAl Viro 				nd->dir_mode = nd->inode->i_mode;
2229c108837eSAl Viro 				nd->flags &= ~LOOKUP_PARENT;
22308620c238SAl Viro 				return 0;
2231c108837eSAl Viro 			}
22328620c238SAl Viro 			/* last component of nested symlink */
2233d8d4611aSAl Viro 			name = nd->stack[--depth].name;
22348c4efe22SAl Viro 			link = walk_component(nd, 0);
22351c4ff1a8SAl Viro 		} else {
22361c4ff1a8SAl Viro 			/* not the last component */
22378c4efe22SAl Viro 			link = walk_component(nd, WALK_MORE);
22388620c238SAl Viro 		}
223992d27016SAl Viro 		if (unlikely(link)) {
224092d27016SAl Viro 			if (IS_ERR(link))
224192d27016SAl Viro 				return PTR_ERR(link);
224292d27016SAl Viro 			/* a symlink to follow */
2243d8d4611aSAl Viro 			nd->stack[depth++].name = name;
224492d27016SAl Viro 			name = link;
22459e18f10aSAl Viro 			continue;
224648c8b0c5SAl Viro 		}
224797242f99SAl Viro 		if (unlikely(!d_can_lookup(nd->path.dentry))) {
224897242f99SAl Viro 			if (nd->flags & LOOKUP_RCU) {
22494675ac39SAl Viro 				if (unlazy_walk(nd))
225097242f99SAl Viro 					return -ECHILD;
225197242f99SAl Viro 			}
22523595e234SAl Viro 			return -ENOTDIR;
22535f4a6a69SAl Viro 		}
2254ce57dfc1SAl Viro 	}
225597242f99SAl Viro }
22561da177e4SLinus Torvalds 
2257edc2b1daSAl Viro /* must be paired with terminate_walk() */
2258c8a53ee5SAl Viro static const char *path_init(struct nameidata *nd, unsigned flags)
225931e6b01fSNick Piggin {
2260740a1678SAleksa Sarai 	int error;
2261c8a53ee5SAl Viro 	const char *s = nd->name->name;
226231e6b01fSNick Piggin 
2263c0eb027eSLinus Torvalds 	if (!*s)
2264c0eb027eSLinus Torvalds 		flags &= ~LOOKUP_RCU;
2265edc2b1daSAl Viro 	if (flags & LOOKUP_RCU)
2266edc2b1daSAl Viro 		rcu_read_lock();
2267c0eb027eSLinus Torvalds 
2268c108837eSAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
226931e6b01fSNick Piggin 	nd->depth = 0;
2270ab87f9a5SAleksa Sarai 
2271ab87f9a5SAleksa Sarai 	nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount);
2272ab87f9a5SAleksa Sarai 	nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount);
2273ab87f9a5SAleksa Sarai 	smp_rmb();
2274ab87f9a5SAleksa Sarai 
22755b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
2276b18825a7SDavid Howells 		struct dentry *root = nd->root.dentry;
2277b18825a7SDavid Howells 		struct inode *inode = root->d_inode;
227893893862SAl Viro 		if (*s && unlikely(!d_can_lookup(root)))
2279368ee9baSAl Viro 			return ERR_PTR(-ENOTDIR);
22805b6ca027SAl Viro 		nd->path = nd->root;
22815b6ca027SAl Viro 		nd->inode = inode;
22825b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
2283ab87f9a5SAleksa Sarai 			nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
22848f47a016SAl Viro 			nd->root_seq = nd->seq;
22855b6ca027SAl Viro 		} else {
22865b6ca027SAl Viro 			path_get(&nd->path);
22875b6ca027SAl Viro 		}
2288368ee9baSAl Viro 		return s;
22895b6ca027SAl Viro 	}
22905b6ca027SAl Viro 
229131e6b01fSNick Piggin 	nd->root.mnt = NULL;
2292248fb5b9SAl Viro 	nd->path.mnt = NULL;
2293248fb5b9SAl Viro 	nd->path.dentry = NULL;
229431e6b01fSNick Piggin 
22958db52c7eSAleksa Sarai 	/* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */
22968db52c7eSAleksa Sarai 	if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) {
2297740a1678SAleksa Sarai 		error = nd_jump_root(nd);
2298740a1678SAleksa Sarai 		if (unlikely(error))
2299740a1678SAleksa Sarai 			return ERR_PTR(error);
2300ef55d917SAl Viro 		return s;
23018db52c7eSAleksa Sarai 	}
23028db52c7eSAleksa Sarai 
23038db52c7eSAleksa Sarai 	/* Relative pathname -- get the starting-point it is relative to. */
23048db52c7eSAleksa Sarai 	if (nd->dfd == AT_FDCWD) {
2305e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
230631e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
2307c28cc364SNick Piggin 			unsigned seq;
230831e6b01fSNick Piggin 
2309c28cc364SNick Piggin 			do {
2310c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
231131e6b01fSNick Piggin 				nd->path = fs->pwd;
2312ef55d917SAl Viro 				nd->inode = nd->path.dentry->d_inode;
2313c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2314c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
2315e41f7d4eSAl Viro 		} else {
2316e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
2317ef55d917SAl Viro 			nd->inode = nd->path.dentry->d_inode;
2318e41f7d4eSAl Viro 		}
231931e6b01fSNick Piggin 	} else {
2320582aa64aSJeff Layton 		/* Caller must check execute permissions on the starting path component */
2321c8a53ee5SAl Viro 		struct fd f = fdget_raw(nd->dfd);
232231e6b01fSNick Piggin 		struct dentry *dentry;
232331e6b01fSNick Piggin 
23242903ff01SAl Viro 		if (!f.file)
2325368ee9baSAl Viro 			return ERR_PTR(-EBADF);
232631e6b01fSNick Piggin 
23272903ff01SAl Viro 		dentry = f.file->f_path.dentry;
232831e6b01fSNick Piggin 
2329edc2b1daSAl Viro 		if (*s && unlikely(!d_can_lookup(dentry))) {
23302903ff01SAl Viro 			fdput(f);
2331368ee9baSAl Viro 			return ERR_PTR(-ENOTDIR);
2332f52e0c11SAl Viro 		}
23332903ff01SAl Viro 
23342903ff01SAl Viro 		nd->path = f.file->f_path;
2335e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
233634a26b99SAl Viro 			nd->inode = nd->path.dentry->d_inode;
233734a26b99SAl Viro 			nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
23385590ff0dSUlrich Drepper 		} else {
23392903ff01SAl Viro 			path_get(&nd->path);
234034a26b99SAl Viro 			nd->inode = nd->path.dentry->d_inode;
23411da177e4SLinus Torvalds 		}
234234a26b99SAl Viro 		fdput(f);
2343e41f7d4eSAl Viro 	}
23448db52c7eSAleksa Sarai 
2345adb21d2bSAleksa Sarai 	/* For scoped-lookups we need to set the root to the dirfd as well. */
2346adb21d2bSAleksa Sarai 	if (flags & LOOKUP_IS_SCOPED) {
2347adb21d2bSAleksa Sarai 		nd->root = nd->path;
2348adb21d2bSAleksa Sarai 		if (flags & LOOKUP_RCU) {
2349adb21d2bSAleksa Sarai 			nd->root_seq = nd->seq;
2350adb21d2bSAleksa Sarai 		} else {
2351adb21d2bSAleksa Sarai 			path_get(&nd->root);
2352adb21d2bSAleksa Sarai 			nd->flags |= LOOKUP_ROOT_GRABBED;
2353adb21d2bSAleksa Sarai 		}
2354adb21d2bSAleksa Sarai 	}
2355adb21d2bSAleksa Sarai 	return s;
23569b4a9b14SAl Viro }
23579b4a9b14SAl Viro 
23581ccac622SAl Viro static inline const char *lookup_last(struct nameidata *nd)
235995fa25d9SAl Viro {
2360bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2361bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2362bd92d7feSAl Viro 
2363c108837eSAl Viro 	return walk_component(nd, WALK_TRAILING);
2364bd92d7feSAl Viro }
2365bd92d7feSAl Viro 
23664f757f3cSAl Viro static int handle_lookup_down(struct nameidata *nd)
23674f757f3cSAl Viro {
2368c153007bSAl Viro 	if (!(nd->flags & LOOKUP_RCU))
2369db3c9adeSAl Viro 		dget(nd->path.dentry);
2370b0417d2cSAl Viro 	return PTR_ERR(step_into(nd, WALK_NOFOLLOW,
2371b0417d2cSAl Viro 			nd->path.dentry, nd->inode, nd->seq));
23724f757f3cSAl Viro }
23734f757f3cSAl Viro 
23749b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2375c8a53ee5SAl Viro static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
23769b4a9b14SAl Viro {
2377c8a53ee5SAl Viro 	const char *s = path_init(nd, flags);
2378bd92d7feSAl Viro 	int err;
237931e6b01fSNick Piggin 
23809b5858e9SAl Viro 	if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) {
23814f757f3cSAl Viro 		err = handle_lookup_down(nd);
23825f336e72SAl Viro 		if (unlikely(err < 0))
23835f336e72SAl Viro 			s = ERR_PTR(err);
23844f757f3cSAl Viro 	}
23854f757f3cSAl Viro 
23861ccac622SAl Viro 	while (!(err = link_path_walk(s, nd)) &&
23871ccac622SAl Viro 	       (s = lookup_last(nd)) != NULL)
23881ccac622SAl Viro 		;
23899f1fafeeSAl Viro 	if (!err)
23909f1fafeeSAl Viro 		err = complete_walk(nd);
2391bd92d7feSAl Viro 
2392deb106c6SAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY)
2393deb106c6SAl Viro 		if (!d_can_lookup(nd->path.dentry))
2394bd23a539SAl Viro 			err = -ENOTDIR;
2395161aff1dSAl Viro 	if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) {
2396161aff1dSAl Viro 		err = handle_lookup_down(nd);
2397161aff1dSAl Viro 		nd->flags &= ~LOOKUP_JUMPED; // no d_weak_revalidate(), please...
2398161aff1dSAl Viro 	}
2399625b6d10SAl Viro 	if (!err) {
2400625b6d10SAl Viro 		*path = nd->path;
2401625b6d10SAl Viro 		nd->path.mnt = NULL;
2402625b6d10SAl Viro 		nd->path.dentry = NULL;
2403625b6d10SAl Viro 	}
2404deb106c6SAl Viro 	terminate_walk(nd);
2405bd92d7feSAl Viro 	return err;
240631e6b01fSNick Piggin }
240731e6b01fSNick Piggin 
240831d921c7SDavid Howells int filename_lookup(int dfd, struct filename *name, unsigned flags,
24099ad1aaa6SAl Viro 		    struct path *path, struct path *root)
2410873f1eedSJeff Layton {
2411894bc8c4SAl Viro 	int retval;
24129883d185SAl Viro 	struct nameidata nd;
2413abc9f5beSAl Viro 	if (IS_ERR(name))
2414abc9f5beSAl Viro 		return PTR_ERR(name);
24159ad1aaa6SAl Viro 	if (unlikely(root)) {
24169ad1aaa6SAl Viro 		nd.root = *root;
24179ad1aaa6SAl Viro 		flags |= LOOKUP_ROOT;
24189ad1aaa6SAl Viro 	}
24199883d185SAl Viro 	set_nameidata(&nd, dfd, name);
2420c8a53ee5SAl Viro 	retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
2421873f1eedSJeff Layton 	if (unlikely(retval == -ECHILD))
2422c8a53ee5SAl Viro 		retval = path_lookupat(&nd, flags, path);
2423873f1eedSJeff Layton 	if (unlikely(retval == -ESTALE))
2424c8a53ee5SAl Viro 		retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
2425873f1eedSJeff Layton 
2426873f1eedSJeff Layton 	if (likely(!retval))
2427161aff1dSAl Viro 		audit_inode(name, path->dentry,
2428161aff1dSAl Viro 			    flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0);
24299883d185SAl Viro 	restore_nameidata();
2430e4bd1c1aSAl Viro 	putname(name);
2431873f1eedSJeff Layton 	return retval;
2432873f1eedSJeff Layton }
2433873f1eedSJeff Layton 
24348bcb77faSAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2435c8a53ee5SAl Viro static int path_parentat(struct nameidata *nd, unsigned flags,
2436391172c4SAl Viro 				struct path *parent)
24378bcb77faSAl Viro {
2438c8a53ee5SAl Viro 	const char *s = path_init(nd, flags);
24399b5858e9SAl Viro 	int err = link_path_walk(s, nd);
24408bcb77faSAl Viro 	if (!err)
24418bcb77faSAl Viro 		err = complete_walk(nd);
2442391172c4SAl Viro 	if (!err) {
2443391172c4SAl Viro 		*parent = nd->path;
2444391172c4SAl Viro 		nd->path.mnt = NULL;
2445391172c4SAl Viro 		nd->path.dentry = NULL;
2446391172c4SAl Viro 	}
2447deb106c6SAl Viro 	terminate_walk(nd);
24488bcb77faSAl Viro 	return err;
24498bcb77faSAl Viro }
24508bcb77faSAl Viro 
24515c31b6ceSAl Viro static struct filename *filename_parentat(int dfd, struct filename *name,
2452391172c4SAl Viro 				unsigned int flags, struct path *parent,
2453391172c4SAl Viro 				struct qstr *last, int *type)
24548bcb77faSAl Viro {
24558bcb77faSAl Viro 	int retval;
24569883d185SAl Viro 	struct nameidata nd;
24578bcb77faSAl Viro 
24585c31b6ceSAl Viro 	if (IS_ERR(name))
24595c31b6ceSAl Viro 		return name;
24609883d185SAl Viro 	set_nameidata(&nd, dfd, name);
2461c8a53ee5SAl Viro 	retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
24628bcb77faSAl Viro 	if (unlikely(retval == -ECHILD))
2463c8a53ee5SAl Viro 		retval = path_parentat(&nd, flags, parent);
24648bcb77faSAl Viro 	if (unlikely(retval == -ESTALE))
2465c8a53ee5SAl Viro 		retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
2466391172c4SAl Viro 	if (likely(!retval)) {
2467391172c4SAl Viro 		*last = nd.last;
2468391172c4SAl Viro 		*type = nd.last_type;
2469c9b07eabSAl Viro 		audit_inode(name, parent->dentry, AUDIT_INODE_PARENT);
24705c31b6ceSAl Viro 	} else {
24715c31b6ceSAl Viro 		putname(name);
24725c31b6ceSAl Viro 		name = ERR_PTR(retval);
2473391172c4SAl Viro 	}
24749883d185SAl Viro 	restore_nameidata();
24755c31b6ceSAl Viro 	return name;
24768bcb77faSAl Viro }
24778bcb77faSAl Viro 
247879714f72SAl Viro /* does lookup, returns the object with parent locked */
247979714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path)
24805590ff0dSUlrich Drepper {
24815c31b6ceSAl Viro 	struct filename *filename;
24825c31b6ceSAl Viro 	struct dentry *d;
2483391172c4SAl Viro 	struct qstr last;
2484391172c4SAl Viro 	int type;
248551689104SPaul Moore 
24865c31b6ceSAl Viro 	filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
24875c31b6ceSAl Viro 				    &last, &type);
248851689104SPaul Moore 	if (IS_ERR(filename))
248951689104SPaul Moore 		return ERR_CAST(filename);
24905c31b6ceSAl Viro 	if (unlikely(type != LAST_NORM)) {
2491391172c4SAl Viro 		path_put(path);
24925c31b6ceSAl Viro 		putname(filename);
24935c31b6ceSAl Viro 		return ERR_PTR(-EINVAL);
249479714f72SAl Viro 	}
24955955102cSAl Viro 	inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
2496391172c4SAl Viro 	d = __lookup_hash(&last, path->dentry, 0);
249779714f72SAl Viro 	if (IS_ERR(d)) {
24985955102cSAl Viro 		inode_unlock(path->dentry->d_inode);
2499391172c4SAl Viro 		path_put(path);
250079714f72SAl Viro 	}
250151689104SPaul Moore 	putname(filename);
250279714f72SAl Viro 	return d;
25035590ff0dSUlrich Drepper }
25045590ff0dSUlrich Drepper 
2505d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
2506d1811465SAl Viro {
2507abc9f5beSAl Viro 	return filename_lookup(AT_FDCWD, getname_kernel(name),
2508abc9f5beSAl Viro 			       flags, path, NULL);
2509d1811465SAl Viro }
25104d359507SAl Viro EXPORT_SYMBOL(kern_path);
2511d1811465SAl Viro 
251216f18200SJosef 'Jeff' Sipek /**
251316f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
251416f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
251516f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
251616f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
251716f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
2518e0a01249SAl Viro  * @path: pointer to struct path to fill
251916f18200SJosef 'Jeff' Sipek  */
252016f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
252116f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
2522e0a01249SAl Viro 		    struct path *path)
252316f18200SJosef 'Jeff' Sipek {
25249ad1aaa6SAl Viro 	struct path root = {.mnt = mnt, .dentry = dentry};
25259ad1aaa6SAl Viro 	/* the first argument of filename_lookup() is ignored with root */
2526abc9f5beSAl Viro 	return filename_lookup(AT_FDCWD, getname_kernel(name),
2527abc9f5beSAl Viro 			       flags , path, &root);
252816f18200SJosef 'Jeff' Sipek }
25294d359507SAl Viro EXPORT_SYMBOL(vfs_path_lookup);
253016f18200SJosef 'Jeff' Sipek 
25313c95f0dcSAl Viro static int lookup_one_len_common(const char *name, struct dentry *base,
25323c95f0dcSAl Viro 				 int len, struct qstr *this)
25333c95f0dcSAl Viro {
25343c95f0dcSAl Viro 	this->name = name;
25353c95f0dcSAl Viro 	this->len = len;
25363c95f0dcSAl Viro 	this->hash = full_name_hash(base, name, len);
25373c95f0dcSAl Viro 	if (!len)
25383c95f0dcSAl Viro 		return -EACCES;
25393c95f0dcSAl Viro 
25403c95f0dcSAl Viro 	if (unlikely(name[0] == '.')) {
25413c95f0dcSAl Viro 		if (len < 2 || (len == 2 && name[1] == '.'))
25423c95f0dcSAl Viro 			return -EACCES;
25433c95f0dcSAl Viro 	}
25443c95f0dcSAl Viro 
25453c95f0dcSAl Viro 	while (len--) {
25463c95f0dcSAl Viro 		unsigned int c = *(const unsigned char *)name++;
25473c95f0dcSAl Viro 		if (c == '/' || c == '\0')
25483c95f0dcSAl Viro 			return -EACCES;
25493c95f0dcSAl Viro 	}
25503c95f0dcSAl Viro 	/*
25513c95f0dcSAl Viro 	 * See if the low-level filesystem might want
25523c95f0dcSAl Viro 	 * to use its own hash..
25533c95f0dcSAl Viro 	 */
25543c95f0dcSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
25553c95f0dcSAl Viro 		int err = base->d_op->d_hash(base, this);
25563c95f0dcSAl Viro 		if (err < 0)
25573c95f0dcSAl Viro 			return err;
25583c95f0dcSAl Viro 	}
25593c95f0dcSAl Viro 
2560*47291baaSChristian Brauner 	return inode_permission(&init_user_ns, base->d_inode, MAY_EXEC);
25613c95f0dcSAl Viro }
25623c95f0dcSAl Viro 
2563eead1911SChristoph Hellwig /**
25640da0b7fdSDavid Howells  * try_lookup_one_len - filesystem helper to lookup single pathname component
25650da0b7fdSDavid Howells  * @name:	pathname component to lookup
25660da0b7fdSDavid Howells  * @base:	base directory to lookup from
25670da0b7fdSDavid Howells  * @len:	maximum length @len should be interpreted to
25680da0b7fdSDavid Howells  *
25690da0b7fdSDavid Howells  * Look up a dentry by name in the dcache, returning NULL if it does not
25700da0b7fdSDavid Howells  * currently exist.  The function does not try to create a dentry.
25710da0b7fdSDavid Howells  *
25720da0b7fdSDavid Howells  * Note that this routine is purely a helper for filesystem usage and should
25730da0b7fdSDavid Howells  * not be called by generic code.
25740da0b7fdSDavid Howells  *
25750da0b7fdSDavid Howells  * The caller must hold base->i_mutex.
25760da0b7fdSDavid Howells  */
25770da0b7fdSDavid Howells struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len)
25780da0b7fdSDavid Howells {
25790da0b7fdSDavid Howells 	struct qstr this;
25800da0b7fdSDavid Howells 	int err;
25810da0b7fdSDavid Howells 
25820da0b7fdSDavid Howells 	WARN_ON_ONCE(!inode_is_locked(base->d_inode));
25830da0b7fdSDavid Howells 
25840da0b7fdSDavid Howells 	err = lookup_one_len_common(name, base, len, &this);
25850da0b7fdSDavid Howells 	if (err)
25860da0b7fdSDavid Howells 		return ERR_PTR(err);
25870da0b7fdSDavid Howells 
25880da0b7fdSDavid Howells 	return lookup_dcache(&this, base, 0);
25890da0b7fdSDavid Howells }
25900da0b7fdSDavid Howells EXPORT_SYMBOL(try_lookup_one_len);
25910da0b7fdSDavid Howells 
25920da0b7fdSDavid Howells /**
2593a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
2594eead1911SChristoph Hellwig  * @name:	pathname component to lookup
2595eead1911SChristoph Hellwig  * @base:	base directory to lookup from
2596eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
2597eead1911SChristoph Hellwig  *
2598a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
25999e7543e9SAl Viro  * not be called by generic code.
2600bbddca8eSNeilBrown  *
2601bbddca8eSNeilBrown  * The caller must hold base->i_mutex.
2602eead1911SChristoph Hellwig  */
2603057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2604057f6c01SJames Morris {
26058613a209SAl Viro 	struct dentry *dentry;
2606057f6c01SJames Morris 	struct qstr this;
2607cda309deSMiklos Szeredi 	int err;
2608057f6c01SJames Morris 
26095955102cSAl Viro 	WARN_ON_ONCE(!inode_is_locked(base->d_inode));
26102f9092e1SDavid Woodhouse 
26113c95f0dcSAl Viro 	err = lookup_one_len_common(name, base, len, &this);
2612cda309deSMiklos Szeredi 	if (err)
2613cda309deSMiklos Szeredi 		return ERR_PTR(err);
2614cda309deSMiklos Szeredi 
26158613a209SAl Viro 	dentry = lookup_dcache(&this, base, 0);
26168613a209SAl Viro 	return dentry ? dentry : __lookup_slow(&this, base, 0);
2617057f6c01SJames Morris }
26184d359507SAl Viro EXPORT_SYMBOL(lookup_one_len);
2619057f6c01SJames Morris 
2620bbddca8eSNeilBrown /**
2621bbddca8eSNeilBrown  * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2622bbddca8eSNeilBrown  * @name:	pathname component to lookup
2623bbddca8eSNeilBrown  * @base:	base directory to lookup from
2624bbddca8eSNeilBrown  * @len:	maximum length @len should be interpreted to
2625bbddca8eSNeilBrown  *
2626bbddca8eSNeilBrown  * Note that this routine is purely a helper for filesystem usage and should
2627bbddca8eSNeilBrown  * not be called by generic code.
2628bbddca8eSNeilBrown  *
2629bbddca8eSNeilBrown  * Unlike lookup_one_len, it should be called without the parent
2630bbddca8eSNeilBrown  * i_mutex held, and will take the i_mutex itself if necessary.
2631bbddca8eSNeilBrown  */
2632bbddca8eSNeilBrown struct dentry *lookup_one_len_unlocked(const char *name,
2633bbddca8eSNeilBrown 				       struct dentry *base, int len)
2634bbddca8eSNeilBrown {
2635bbddca8eSNeilBrown 	struct qstr this;
2636bbddca8eSNeilBrown 	int err;
263720d00ee8SLinus Torvalds 	struct dentry *ret;
2638bbddca8eSNeilBrown 
26393c95f0dcSAl Viro 	err = lookup_one_len_common(name, base, len, &this);
2640bbddca8eSNeilBrown 	if (err)
2641bbddca8eSNeilBrown 		return ERR_PTR(err);
2642bbddca8eSNeilBrown 
264320d00ee8SLinus Torvalds 	ret = lookup_dcache(&this, base, 0);
264420d00ee8SLinus Torvalds 	if (!ret)
264520d00ee8SLinus Torvalds 		ret = lookup_slow(&this, base, 0);
264620d00ee8SLinus Torvalds 	return ret;
2647bbddca8eSNeilBrown }
2648bbddca8eSNeilBrown EXPORT_SYMBOL(lookup_one_len_unlocked);
2649bbddca8eSNeilBrown 
26506c2d4798SAl Viro /*
26516c2d4798SAl Viro  * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT)
26526c2d4798SAl Viro  * on negatives.  Returns known positive or ERR_PTR(); that's what
26536c2d4798SAl Viro  * most of the users want.  Note that pinned negative with unlocked parent
26546c2d4798SAl Viro  * _can_ become positive at any time, so callers of lookup_one_len_unlocked()
26556c2d4798SAl Viro  * need to be very careful; pinned positives have ->d_inode stable, so
26566c2d4798SAl Viro  * this one avoids such problems.
26576c2d4798SAl Viro  */
26586c2d4798SAl Viro struct dentry *lookup_positive_unlocked(const char *name,
26596c2d4798SAl Viro 				       struct dentry *base, int len)
26606c2d4798SAl Viro {
26616c2d4798SAl Viro 	struct dentry *ret = lookup_one_len_unlocked(name, base, len);
26622fa6b1e0SAl Viro 	if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
26636c2d4798SAl Viro 		dput(ret);
26646c2d4798SAl Viro 		ret = ERR_PTR(-ENOENT);
26656c2d4798SAl Viro 	}
26666c2d4798SAl Viro 	return ret;
26676c2d4798SAl Viro }
26686c2d4798SAl Viro EXPORT_SYMBOL(lookup_positive_unlocked);
26696c2d4798SAl Viro 
2670eedf265aSEric W. Biederman #ifdef CONFIG_UNIX98_PTYS
2671eedf265aSEric W. Biederman int path_pts(struct path *path)
2672eedf265aSEric W. Biederman {
2673eedf265aSEric W. Biederman 	/* Find something mounted on "pts" in the same directory as
2674eedf265aSEric W. Biederman 	 * the input path.
2675eedf265aSEric W. Biederman 	 */
2676a6a7eb76SAl Viro 	struct dentry *parent = dget_parent(path->dentry);
2677a6a7eb76SAl Viro 	struct dentry *child;
267819f6028aSAl Viro 	struct qstr this = QSTR_INIT("pts", 3);
2679eedf265aSEric W. Biederman 
2680a6a7eb76SAl Viro 	if (unlikely(!path_connected(path->mnt, parent))) {
2681a6a7eb76SAl Viro 		dput(parent);
268263b27720SAl Viro 		return -ENOENT;
2683a6a7eb76SAl Viro 	}
268463b27720SAl Viro 	dput(path->dentry);
268563b27720SAl Viro 	path->dentry = parent;
2686eedf265aSEric W. Biederman 	child = d_hash_and_lookup(parent, &this);
2687eedf265aSEric W. Biederman 	if (!child)
2688eedf265aSEric W. Biederman 		return -ENOENT;
2689eedf265aSEric W. Biederman 
2690eedf265aSEric W. Biederman 	path->dentry = child;
2691eedf265aSEric W. Biederman 	dput(parent);
269219f6028aSAl Viro 	follow_down(path);
2693eedf265aSEric W. Biederman 	return 0;
2694eedf265aSEric W. Biederman }
2695eedf265aSEric W. Biederman #endif
2696eedf265aSEric W. Biederman 
26971fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
26981fa1e7f6SAndy Whitcroft 		 struct path *path, int *empty)
26991da177e4SLinus Torvalds {
2700abc9f5beSAl Viro 	return filename_lookup(dfd, getname_flags(name, flags, empty),
2701abc9f5beSAl Viro 			       flags, path, NULL);
27021da177e4SLinus Torvalds }
2703b853a161SAl Viro EXPORT_SYMBOL(user_path_at_empty);
27041fa1e7f6SAndy Whitcroft 
2705cbdf35bcSMiklos Szeredi int __check_sticky(struct inode *dir, struct inode *inode)
27061da177e4SLinus Torvalds {
27078e96e3b7SEric W. Biederman 	kuid_t fsuid = current_fsuid();
2708da9592edSDavid Howells 
27098e96e3b7SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
27101da177e4SLinus Torvalds 		return 0;
27118e96e3b7SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
27121da177e4SLinus Torvalds 		return 0;
27130558c1bfSChristian Brauner 	return !capable_wrt_inode_uidgid(&init_user_ns, inode, CAP_FOWNER);
27141da177e4SLinus Torvalds }
2715cbdf35bcSMiklos Szeredi EXPORT_SYMBOL(__check_sticky);
27161da177e4SLinus Torvalds 
27171da177e4SLinus Torvalds /*
27181da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
27191da177e4SLinus Torvalds  *  whether the type of victim is right.
27201da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
27211da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
27221da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
27231da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
27241da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
27251da177e4SLinus Torvalds  *	a. be owner of dir, or
27261da177e4SLinus Torvalds  *	b. be owner of victim, or
27271da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
27281da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
27291da177e4SLinus Torvalds  *     links pointing to it.
27300bd23d09SEric W. Biederman  *  7. If the victim has an unknown uid or gid we can't change the inode.
27310bd23d09SEric W. Biederman  *  8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
27320bd23d09SEric W. Biederman  *  9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
27330bd23d09SEric W. Biederman  * 10. We can't remove a root or mountpoint.
27340bd23d09SEric W. Biederman  * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
27351da177e4SLinus Torvalds  *     nfs_async_unlink().
27361da177e4SLinus Torvalds  */
2737b18825a7SDavid Howells static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
27381da177e4SLinus Torvalds {
273963afdfc7SDavid Howells 	struct inode *inode = d_backing_inode(victim);
27401da177e4SLinus Torvalds 	int error;
27411da177e4SLinus Torvalds 
2742b18825a7SDavid Howells 	if (d_is_negative(victim))
27431da177e4SLinus Torvalds 		return -ENOENT;
2744b18825a7SDavid Howells 	BUG_ON(!inode);
27451da177e4SLinus Torvalds 
27461da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
2747593d1ce8SEric W. Biederman 
2748593d1ce8SEric W. Biederman 	/* Inode writeback is not safe when the uid or gid are invalid. */
2749593d1ce8SEric W. Biederman 	if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
2750593d1ce8SEric W. Biederman 		return -EOVERFLOW;
2751593d1ce8SEric W. Biederman 
27524fa6b5ecSJeff Layton 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
27531da177e4SLinus Torvalds 
2754*47291baaSChristian Brauner 	error = inode_permission(&init_user_ns, dir, MAY_WRITE | MAY_EXEC);
27551da177e4SLinus Torvalds 	if (error)
27561da177e4SLinus Torvalds 		return error;
27571da177e4SLinus Torvalds 	if (IS_APPEND(dir))
27581da177e4SLinus Torvalds 		return -EPERM;
2759b18825a7SDavid Howells 
2760b18825a7SDavid Howells 	if (check_sticky(dir, inode) || IS_APPEND(inode) ||
27610bd23d09SEric W. Biederman 	    IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode))
27621da177e4SLinus Torvalds 		return -EPERM;
27631da177e4SLinus Torvalds 	if (isdir) {
276444b1d530SMiklos Szeredi 		if (!d_is_dir(victim))
27651da177e4SLinus Torvalds 			return -ENOTDIR;
27661da177e4SLinus Torvalds 		if (IS_ROOT(victim))
27671da177e4SLinus Torvalds 			return -EBUSY;
276844b1d530SMiklos Szeredi 	} else if (d_is_dir(victim))
27691da177e4SLinus Torvalds 		return -EISDIR;
27701da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
27711da177e4SLinus Torvalds 		return -ENOENT;
27721da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
27731da177e4SLinus Torvalds 		return -EBUSY;
27741da177e4SLinus Torvalds 	return 0;
27751da177e4SLinus Torvalds }
27761da177e4SLinus Torvalds 
27771da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
27781da177e4SLinus Torvalds  *  dir.
27791da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
27801da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
27811da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
2782036d5236SEric W. Biederman  *  3. We can't do it if the fs can't represent the fsuid or fsgid.
2783036d5236SEric W. Biederman  *  4. We should have write and exec permissions on dir
2784036d5236SEric W. Biederman  *  5. We can't do it if dir is immutable (done in permission())
27851da177e4SLinus Torvalds  */
2786a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
27871da177e4SLinus Torvalds {
2788036d5236SEric W. Biederman 	struct user_namespace *s_user_ns;
278914e972b4SJeff Layton 	audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
27901da177e4SLinus Torvalds 	if (child->d_inode)
27911da177e4SLinus Torvalds 		return -EEXIST;
27921da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
27931da177e4SLinus Torvalds 		return -ENOENT;
2794036d5236SEric W. Biederman 	s_user_ns = dir->i_sb->s_user_ns;
2795036d5236SEric W. Biederman 	if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2796036d5236SEric W. Biederman 	    !kgid_has_mapping(s_user_ns, current_fsgid()))
2797036d5236SEric W. Biederman 		return -EOVERFLOW;
2798*47291baaSChristian Brauner 	return inode_permission(&init_user_ns, dir, MAY_WRITE | MAY_EXEC);
27991da177e4SLinus Torvalds }
28001da177e4SLinus Torvalds 
28011da177e4SLinus Torvalds /*
28021da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
28031da177e4SLinus Torvalds  */
28041da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
28051da177e4SLinus Torvalds {
28061da177e4SLinus Torvalds 	struct dentry *p;
28071da177e4SLinus Torvalds 
28081da177e4SLinus Torvalds 	if (p1 == p2) {
28095955102cSAl Viro 		inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
28101da177e4SLinus Torvalds 		return NULL;
28111da177e4SLinus Torvalds 	}
28121da177e4SLinus Torvalds 
2813fc64005cSAl Viro 	mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
28141da177e4SLinus Torvalds 
2815e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
2816e2761a11SOGAWA Hirofumi 	if (p) {
28175955102cSAl Viro 		inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
28185955102cSAl Viro 		inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
28191da177e4SLinus Torvalds 		return p;
28201da177e4SLinus Torvalds 	}
28211da177e4SLinus Torvalds 
2822e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
2823e2761a11SOGAWA Hirofumi 	if (p) {
28245955102cSAl Viro 		inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
28255955102cSAl Viro 		inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
28261da177e4SLinus Torvalds 		return p;
28271da177e4SLinus Torvalds 	}
28281da177e4SLinus Torvalds 
28295955102cSAl Viro 	inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
28305955102cSAl Viro 	inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
28311da177e4SLinus Torvalds 	return NULL;
28321da177e4SLinus Torvalds }
28334d359507SAl Viro EXPORT_SYMBOL(lock_rename);
28341da177e4SLinus Torvalds 
28351da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
28361da177e4SLinus Torvalds {
28375955102cSAl Viro 	inode_unlock(p1->d_inode);
28381da177e4SLinus Torvalds 	if (p1 != p2) {
28395955102cSAl Viro 		inode_unlock(p2->d_inode);
2840fc64005cSAl Viro 		mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
28411da177e4SLinus Torvalds 	}
28421da177e4SLinus Torvalds }
28434d359507SAl Viro EXPORT_SYMBOL(unlock_rename);
28441da177e4SLinus Torvalds 
28454acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2846312b63fbSAl Viro 		bool want_excl)
28471da177e4SLinus Torvalds {
2848a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
28491da177e4SLinus Torvalds 	if (error)
28501da177e4SLinus Torvalds 		return error;
28511da177e4SLinus Torvalds 
2852acfa4380SAl Viro 	if (!dir->i_op->create)
28531da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
28541da177e4SLinus Torvalds 	mode &= S_IALLUGO;
28551da177e4SLinus Torvalds 	mode |= S_IFREG;
28561da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
28571da177e4SLinus Torvalds 	if (error)
28581da177e4SLinus Torvalds 		return error;
2859312b63fbSAl Viro 	error = dir->i_op->create(dir, dentry, mode, want_excl);
2860a74574aaSStephen Smalley 	if (!error)
2861f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
28621da177e4SLinus Torvalds 	return error;
28631da177e4SLinus Torvalds }
28644d359507SAl Viro EXPORT_SYMBOL(vfs_create);
28651da177e4SLinus Torvalds 
28668e6c848eSAl Viro int vfs_mkobj(struct dentry *dentry, umode_t mode,
28678e6c848eSAl Viro 		int (*f)(struct dentry *, umode_t, void *),
28688e6c848eSAl Viro 		void *arg)
28698e6c848eSAl Viro {
28708e6c848eSAl Viro 	struct inode *dir = dentry->d_parent->d_inode;
28718e6c848eSAl Viro 	int error = may_create(dir, dentry);
28728e6c848eSAl Viro 	if (error)
28738e6c848eSAl Viro 		return error;
28748e6c848eSAl Viro 
28758e6c848eSAl Viro 	mode &= S_IALLUGO;
28768e6c848eSAl Viro 	mode |= S_IFREG;
28778e6c848eSAl Viro 	error = security_inode_create(dir, dentry, mode);
28788e6c848eSAl Viro 	if (error)
28798e6c848eSAl Viro 		return error;
28808e6c848eSAl Viro 	error = f(dentry, mode, arg);
28818e6c848eSAl Viro 	if (!error)
28828e6c848eSAl Viro 		fsnotify_create(dir, dentry);
28838e6c848eSAl Viro 	return error;
28848e6c848eSAl Viro }
28858e6c848eSAl Viro EXPORT_SYMBOL(vfs_mkobj);
28868e6c848eSAl Viro 
2887a2982cc9SEric W. Biederman bool may_open_dev(const struct path *path)
2888a2982cc9SEric W. Biederman {
2889a2982cc9SEric W. Biederman 	return !(path->mnt->mnt_flags & MNT_NODEV) &&
2890a2982cc9SEric W. Biederman 		!(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
2891a2982cc9SEric W. Biederman }
2892a2982cc9SEric W. Biederman 
2893f0bb5aafSAl Viro static int may_open(const struct path *path, int acc_mode, int flag)
28941da177e4SLinus Torvalds {
28953fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
28961da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
28971da177e4SLinus Torvalds 	int error;
28981da177e4SLinus Torvalds 
28991da177e4SLinus Torvalds 	if (!inode)
29001da177e4SLinus Torvalds 		return -ENOENT;
29011da177e4SLinus Torvalds 
2902c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2903c8fe8f30SChristoph Hellwig 	case S_IFLNK:
29041da177e4SLinus Torvalds 		return -ELOOP;
2905c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2906fc4177beSKees Cook 		if (acc_mode & MAY_WRITE)
29071da177e4SLinus Torvalds 			return -EISDIR;
2908fc4177beSKees Cook 		if (acc_mode & MAY_EXEC)
2909fc4177beSKees Cook 			return -EACCES;
2910c8fe8f30SChristoph Hellwig 		break;
2911c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2912c8fe8f30SChristoph Hellwig 	case S_IFCHR:
2913a2982cc9SEric W. Biederman 		if (!may_open_dev(path))
29141da177e4SLinus Torvalds 			return -EACCES;
2915633fb6acSKees Cook 		fallthrough;
2916c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2917c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
2918633fb6acSKees Cook 		if (acc_mode & MAY_EXEC)
2919633fb6acSKees Cook 			return -EACCES;
29201da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2921c8fe8f30SChristoph Hellwig 		break;
29220fd338b2SKees Cook 	case S_IFREG:
29230fd338b2SKees Cook 		if ((acc_mode & MAY_EXEC) && path_noexec(path))
29240fd338b2SKees Cook 			return -EACCES;
29250fd338b2SKees Cook 		break;
29264a3fd211SDave Hansen 	}
2927b41572e9SDave Hansen 
2928*47291baaSChristian Brauner 	error = inode_permission(&init_user_ns, inode, MAY_OPEN | acc_mode);
2929b41572e9SDave Hansen 	if (error)
2930b41572e9SDave Hansen 		return error;
29316146f0d5SMimi Zohar 
29321da177e4SLinus Torvalds 	/*
29331da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
29341da177e4SLinus Torvalds 	 */
29351da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
29368737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
29377715b521SAl Viro 			return -EPERM;
29381da177e4SLinus Torvalds 		if (flag & O_TRUNC)
29397715b521SAl Viro 			return -EPERM;
29401da177e4SLinus Torvalds 	}
29411da177e4SLinus Torvalds 
29421da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
29432e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
29447715b521SAl Viro 		return -EPERM;
29451da177e4SLinus Torvalds 
2946f3c7691eSJ. Bruce Fields 	return 0;
29477715b521SAl Viro }
29487715b521SAl Viro 
2949e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
29507715b521SAl Viro {
2951f0bb5aafSAl Viro 	const struct path *path = &filp->f_path;
29527715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
29537715b521SAl Viro 	int error = get_write_access(inode);
29541da177e4SLinus Torvalds 	if (error)
29557715b521SAl Viro 		return error;
29561da177e4SLinus Torvalds 	/*
29571da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
29581da177e4SLinus Torvalds 	 */
2959d7a06983SJeff Layton 	error = locks_verify_locked(filp);
2960be6d3e56SKentaro Takeda 	if (!error)
2961ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
29621da177e4SLinus Torvalds 	if (!error) {
29637715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2964d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2965e1181ee6SJeff Layton 				    filp);
29661da177e4SLinus Torvalds 	}
29671da177e4SLinus Torvalds 	put_write_access(inode);
2968acd0c935SMimi Zohar 	return error;
29691da177e4SLinus Torvalds }
29701da177e4SLinus Torvalds 
2971d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2972d57999e1SDave Hansen {
29738a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
29748a5e929dSAl Viro 		flag--;
2975d57999e1SDave Hansen 	return flag;
2976d57999e1SDave Hansen }
2977d57999e1SDave Hansen 
2978d3607752SAl Viro static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode)
2979d18e9008SMiklos Szeredi {
29801328c727SSeth Forshee 	struct user_namespace *s_user_ns;
2981d18e9008SMiklos Szeredi 	int error = security_path_mknod(dir, dentry, mode, 0);
2982d18e9008SMiklos Szeredi 	if (error)
2983d18e9008SMiklos Szeredi 		return error;
2984d18e9008SMiklos Szeredi 
29851328c727SSeth Forshee 	s_user_ns = dir->dentry->d_sb->s_user_ns;
29861328c727SSeth Forshee 	if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
29871328c727SSeth Forshee 	    !kgid_has_mapping(s_user_ns, current_fsgid()))
29881328c727SSeth Forshee 		return -EOVERFLOW;
29891328c727SSeth Forshee 
2990*47291baaSChristian Brauner 	error = inode_permission(&init_user_ns, dir->dentry->d_inode,
2991*47291baaSChristian Brauner 				 MAY_WRITE | MAY_EXEC);
2992d18e9008SMiklos Szeredi 	if (error)
2993d18e9008SMiklos Szeredi 		return error;
2994d18e9008SMiklos Szeredi 
2995d18e9008SMiklos Szeredi 	return security_inode_create(dir->dentry->d_inode, dentry, mode);
2996d18e9008SMiklos Szeredi }
2997d18e9008SMiklos Szeredi 
29981acf0af9SDavid Howells /*
29991acf0af9SDavid Howells  * Attempt to atomically look up, create and open a file from a negative
30001acf0af9SDavid Howells  * dentry.
30011acf0af9SDavid Howells  *
30021acf0af9SDavid Howells  * Returns 0 if successful.  The file will have been created and attached to
30031acf0af9SDavid Howells  * @file by the filesystem calling finish_open().
30041acf0af9SDavid Howells  *
300500a07c15SAl Viro  * If the file was looked up only or didn't need creating, FMODE_OPENED won't
300600a07c15SAl Viro  * be set.  The caller will need to perform the open themselves.  @path will
300700a07c15SAl Viro  * have been updated to point to the new dentry.  This may be negative.
30081acf0af9SDavid Howells  *
30091acf0af9SDavid Howells  * Returns an error code otherwise.
30101acf0af9SDavid Howells  */
3011239eb983SAl Viro static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry,
3012239eb983SAl Viro 				  struct file *file,
30133ec2eef1SAl Viro 				  int open_flag, umode_t mode)
3014d18e9008SMiklos Szeredi {
3015d18e9008SMiklos Szeredi 	struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
3016d18e9008SMiklos Szeredi 	struct inode *dir =  nd->path.dentry->d_inode;
3017d18e9008SMiklos Szeredi 	int error;
3018d18e9008SMiklos Szeredi 
3019d18e9008SMiklos Szeredi 	if (nd->flags & LOOKUP_DIRECTORY)
3020d18e9008SMiklos Szeredi 		open_flag |= O_DIRECTORY;
3021d18e9008SMiklos Szeredi 
302230d90494SAl Viro 	file->f_path.dentry = DENTRY_NOT_SET;
302330d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
30240fb1ea09SAl Viro 	error = dir->i_op->atomic_open(dir, dentry, file,
302544907d79SAl Viro 				       open_to_namei_flags(open_flag), mode);
30266fbd0714SAl Viro 	d_lookup_done(dentry);
3027384f26e2SAl Viro 	if (!error) {
302864e1ac4dSAl Viro 		if (file->f_mode & FMODE_OPENED) {
30296fb968cdSAl Viro 			if (unlikely(dentry != file->f_path.dentry)) {
30306fb968cdSAl Viro 				dput(dentry);
30316fb968cdSAl Viro 				dentry = dget(file->f_path.dentry);
30326fb968cdSAl Viro 			}
303364e1ac4dSAl Viro 		} else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
30342675a4ebSAl Viro 			error = -EIO;
3035384f26e2SAl Viro 		} else {
303630d90494SAl Viro 			if (file->f_path.dentry) {
3037d18e9008SMiklos Szeredi 				dput(dentry);
3038d18e9008SMiklos Szeredi 				dentry = file->f_path.dentry;
303910c64ceaSAl Viro 			}
3040239eb983SAl Viro 			if (unlikely(d_is_negative(dentry)))
3041a01e718fSAl Viro 				error = -ENOENT;
3042d18e9008SMiklos Szeredi 		}
3043d18e9008SMiklos Szeredi 	}
3044239eb983SAl Viro 	if (error) {
3045d18e9008SMiklos Szeredi 		dput(dentry);
3046239eb983SAl Viro 		dentry = ERR_PTR(error);
3047239eb983SAl Viro 	}
3048239eb983SAl Viro 	return dentry;
3049d18e9008SMiklos Szeredi }
3050d18e9008SMiklos Szeredi 
305131e6b01fSNick Piggin /*
30521acf0af9SDavid Howells  * Look up and maybe create and open the last component.
3053d58ffd35SMiklos Szeredi  *
305400a07c15SAl Viro  * Must be called with parent locked (exclusive in O_CREAT case).
3055d58ffd35SMiklos Szeredi  *
305600a07c15SAl Viro  * Returns 0 on success, that is, if
305700a07c15SAl Viro  *  the file was successfully atomically created (if necessary) and opened, or
305800a07c15SAl Viro  *  the file was not completely opened at this time, though lookups and
305900a07c15SAl Viro  *  creations were performed.
306000a07c15SAl Viro  * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
306100a07c15SAl Viro  * In the latter case dentry returned in @path might be negative if O_CREAT
306200a07c15SAl Viro  * hadn't been specified.
30631acf0af9SDavid Howells  *
306400a07c15SAl Viro  * An error code is returned on failure.
3065d58ffd35SMiklos Szeredi  */
3066da5ebf5aSAl Viro static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
3067d58ffd35SMiklos Szeredi 				  const struct open_flags *op,
30683ec2eef1SAl Viro 				  bool got_write)
3069d58ffd35SMiklos Szeredi {
3070d58ffd35SMiklos Szeredi 	struct dentry *dir = nd->path.dentry;
307154ef4872SMiklos Szeredi 	struct inode *dir_inode = dir->d_inode;
30721643b43fSAl Viro 	int open_flag = op->open_flag;
3073d58ffd35SMiklos Szeredi 	struct dentry *dentry;
30741643b43fSAl Viro 	int error, create_error = 0;
30751643b43fSAl Viro 	umode_t mode = op->mode;
30766fbd0714SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
3077d58ffd35SMiklos Szeredi 
3078ce8644fcSAl Viro 	if (unlikely(IS_DEADDIR(dir_inode)))
3079da5ebf5aSAl Viro 		return ERR_PTR(-ENOENT);
3080d58ffd35SMiklos Szeredi 
308173a09dd9SAl Viro 	file->f_mode &= ~FMODE_CREATED;
30826fbd0714SAl Viro 	dentry = d_lookup(dir, &nd->last);
30836fbd0714SAl Viro 	for (;;) {
30846fbd0714SAl Viro 		if (!dentry) {
30856fbd0714SAl Viro 			dentry = d_alloc_parallel(dir, &nd->last, &wq);
3086d58ffd35SMiklos Szeredi 			if (IS_ERR(dentry))
3087da5ebf5aSAl Viro 				return dentry;
30886fbd0714SAl Viro 		}
30896fbd0714SAl Viro 		if (d_in_lookup(dentry))
30906fbd0714SAl Viro 			break;
3091d58ffd35SMiklos Szeredi 
30926fbd0714SAl Viro 		error = d_revalidate(dentry, nd->flags);
30936fbd0714SAl Viro 		if (likely(error > 0))
30946fbd0714SAl Viro 			break;
30956fbd0714SAl Viro 		if (error)
30966fbd0714SAl Viro 			goto out_dput;
30976fbd0714SAl Viro 		d_invalidate(dentry);
30986fbd0714SAl Viro 		dput(dentry);
30996fbd0714SAl Viro 		dentry = NULL;
31006fbd0714SAl Viro 	}
31016fbd0714SAl Viro 	if (dentry->d_inode) {
3102d18e9008SMiklos Szeredi 		/* Cached positive dentry: will open in f_op->open */
3103da5ebf5aSAl Viro 		return dentry;
31046c51e513SAl Viro 	}
3105d18e9008SMiklos Szeredi 
31061643b43fSAl Viro 	/*
31071643b43fSAl Viro 	 * Checking write permission is tricky, bacuse we don't know if we are
31081643b43fSAl Viro 	 * going to actually need it: O_CREAT opens should work as long as the
31091643b43fSAl Viro 	 * file exists.  But checking existence breaks atomicity.  The trick is
31101643b43fSAl Viro 	 * to check access and if not granted clear O_CREAT from the flags.
31111643b43fSAl Viro 	 *
31121643b43fSAl Viro 	 * Another problem is returing the "right" error value (e.g. for an
31131643b43fSAl Viro 	 * O_EXCL open we want to return EEXIST not EROFS).
31141643b43fSAl Viro 	 */
311599a4a90cSAl Viro 	if (unlikely(!got_write))
311699a4a90cSAl Viro 		open_flag &= ~O_TRUNC;
31171643b43fSAl Viro 	if (open_flag & O_CREAT) {
311899a4a90cSAl Viro 		if (open_flag & O_EXCL)
311999a4a90cSAl Viro 			open_flag &= ~O_TRUNC;
31201643b43fSAl Viro 		if (!IS_POSIXACL(dir->d_inode))
31211643b43fSAl Viro 			mode &= ~current_umask();
312299a4a90cSAl Viro 		if (likely(got_write))
31231643b43fSAl Viro 			create_error = may_o_create(&nd->path, dentry, mode);
312499a4a90cSAl Viro 		else
312599a4a90cSAl Viro 			create_error = -EROFS;
312699a4a90cSAl Viro 	}
312799a4a90cSAl Viro 	if (create_error)
31281643b43fSAl Viro 		open_flag &= ~O_CREAT;
31291643b43fSAl Viro 	if (dir_inode->i_op->atomic_open) {
3130d489cf9aSAl Viro 		dentry = atomic_open(nd, dentry, file, open_flag, mode);
3131da5ebf5aSAl Viro 		if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT))
3132da5ebf5aSAl Viro 			dentry = ERR_PTR(create_error);
3133da5ebf5aSAl Viro 		return dentry;
3134239eb983SAl Viro 	}
313554ef4872SMiklos Szeredi 
31366fbd0714SAl Viro 	if (d_in_lookup(dentry)) {
313712fa5e24SAl Viro 		struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
313812fa5e24SAl Viro 							     nd->flags);
31396fbd0714SAl Viro 		d_lookup_done(dentry);
314012fa5e24SAl Viro 		if (unlikely(res)) {
314112fa5e24SAl Viro 			if (IS_ERR(res)) {
314212fa5e24SAl Viro 				error = PTR_ERR(res);
314312fa5e24SAl Viro 				goto out_dput;
314412fa5e24SAl Viro 			}
314512fa5e24SAl Viro 			dput(dentry);
314612fa5e24SAl Viro 			dentry = res;
314712fa5e24SAl Viro 		}
314854ef4872SMiklos Szeredi 	}
314954ef4872SMiklos Szeredi 
3150d58ffd35SMiklos Szeredi 	/* Negative dentry, just create the file */
31511643b43fSAl Viro 	if (!dentry->d_inode && (open_flag & O_CREAT)) {
315273a09dd9SAl Viro 		file->f_mode |= FMODE_CREATED;
3153ce8644fcSAl Viro 		audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
3154ce8644fcSAl Viro 		if (!dir_inode->i_op->create) {
3155ce8644fcSAl Viro 			error = -EACCES;
3156d58ffd35SMiklos Szeredi 			goto out_dput;
315764894cf8SAl Viro 		}
3158ce8644fcSAl Viro 		error = dir_inode->i_op->create(dir_inode, dentry, mode,
31591643b43fSAl Viro 						open_flag & O_EXCL);
3160d58ffd35SMiklos Szeredi 		if (error)
3161d58ffd35SMiklos Szeredi 			goto out_dput;
3162d58ffd35SMiklos Szeredi 	}
31631643b43fSAl Viro 	if (unlikely(create_error) && !dentry->d_inode) {
31641643b43fSAl Viro 		error = create_error;
3165d58ffd35SMiklos Szeredi 		goto out_dput;
3166d58ffd35SMiklos Szeredi 	}
3167da5ebf5aSAl Viro 	return dentry;
3168d58ffd35SMiklos Szeredi 
3169d58ffd35SMiklos Szeredi out_dput:
3170d58ffd35SMiklos Szeredi 	dput(dentry);
3171da5ebf5aSAl Viro 	return ERR_PTR(error);
3172d58ffd35SMiklos Szeredi }
3173d58ffd35SMiklos Szeredi 
3174c981a482SAl Viro static const char *open_last_lookups(struct nameidata *nd,
31753ec2eef1SAl Viro 		   struct file *file, const struct open_flags *op)
3176fb1cc555SAl Viro {
3177a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
3178ca344a89SAl Viro 	int open_flag = op->open_flag;
317964894cf8SAl Viro 	bool got_write = false;
3180254cf582SAl Viro 	unsigned seq;
3181a1eb3315SMiklos Szeredi 	struct inode *inode;
3182da5ebf5aSAl Viro 	struct dentry *dentry;
3183b0417d2cSAl Viro 	const char *res;
318416c2cd71SAl Viro 	int error;
3185fb1cc555SAl Viro 
3186c3e380b0SAl Viro 	nd->flags |= op->intent;
3187c3e380b0SAl Viro 
3188bc77daa7SAl Viro 	if (nd->last_type != LAST_NORM) {
318956676ec3SAl Viro 		if (nd->depth)
319056676ec3SAl Viro 			put_link(nd);
3191ff326a32SAl Viro 		return handle_dots(nd, nd->last_type);
31921f36f774SAl Viro 	}
3193a2c36b45SAl Viro 
3194ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
3195fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
3196fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3197fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
319820e34357SAl Viro 		dentry = lookup_fast(nd, &inode, &seq);
319920e34357SAl Viro 		if (IS_ERR(dentry))
32001ccac622SAl Viro 			return ERR_CAST(dentry);
320120e34357SAl Viro 		if (likely(dentry))
320271574865SMiklos Szeredi 			goto finish_lookup;
320371574865SMiklos Szeredi 
32046583fe22SAl Viro 		BUG_ON(nd->flags & LOOKUP_RCU);
3205b6183df7SMiklos Szeredi 	} else {
3206fe2d35ffSAl Viro 		/* create side of things */
320772287417SAl Viro 		if (nd->flags & LOOKUP_RCU) {
320872287417SAl Viro 			error = unlazy_walk(nd);
3209c981a482SAl Viro 			if (unlikely(error))
32101ccac622SAl Viro 				return ERR_PTR(error);
321172287417SAl Viro 		}
3212c9b07eabSAl Viro 		audit_inode(nd->name, dir, AUDIT_INODE_PARENT);
32131f36f774SAl Viro 		/* trailing slashes? */
3214deb106c6SAl Viro 		if (unlikely(nd->last.name[nd->last.len]))
32151ccac622SAl Viro 			return ERR_PTR(-EISDIR);
3216b6183df7SMiklos Szeredi 	}
32171f36f774SAl Viro 
32189cf843e3SAl Viro 	if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
321964894cf8SAl Viro 		error = mnt_want_write(nd->path.mnt);
322064894cf8SAl Viro 		if (!error)
322164894cf8SAl Viro 			got_write = true;
322264894cf8SAl Viro 		/*
322364894cf8SAl Viro 		 * do _not_ fail yet - we might not need that or fail with
322464894cf8SAl Viro 		 * a different error; let lookup_open() decide; we'll be
322564894cf8SAl Viro 		 * dropping this one anyway.
322664894cf8SAl Viro 		 */
322764894cf8SAl Viro 	}
32289cf843e3SAl Viro 	if (open_flag & O_CREAT)
32295955102cSAl Viro 		inode_lock(dir->d_inode);
32309cf843e3SAl Viro 	else
32319cf843e3SAl Viro 		inode_lock_shared(dir->d_inode);
3232da5ebf5aSAl Viro 	dentry = lookup_open(nd, file, op, got_write);
3233f7bb959dSAl Viro 	if (!IS_ERR(dentry) && (file->f_mode & FMODE_CREATED))
3234f7bb959dSAl Viro 		fsnotify_create(dir->d_inode, dentry);
32359cf843e3SAl Viro 	if (open_flag & O_CREAT)
32365955102cSAl Viro 		inode_unlock(dir->d_inode);
32379cf843e3SAl Viro 	else
32389cf843e3SAl Viro 		inode_unlock_shared(dir->d_inode);
3239fb1cc555SAl Viro 
3240c981a482SAl Viro 	if (got_write)
324159e96e65SAl Viro 		mnt_drop_write(nd->path.mnt);
32426c0d46c4SAl Viro 
324359e96e65SAl Viro 	if (IS_ERR(dentry))
324459e96e65SAl Viro 		return ERR_CAST(dentry);
324559e96e65SAl Viro 
3246973d4b73SAl Viro 	if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) {
3247e73cabffSAl Viro 		dput(nd->path.dentry);
3248e73cabffSAl Viro 		nd->path.dentry = dentry;
3249c981a482SAl Viro 		return NULL;
3250fb1cc555SAl Viro 	}
3251fb1cc555SAl Viro 
325220e34357SAl Viro finish_lookup:
325356676ec3SAl Viro 	if (nd->depth)
325456676ec3SAl Viro 		put_link(nd);
32558c4efe22SAl Viro 	res = step_into(nd, WALK_TRAILING, dentry, inode, seq);
3256ff326a32SAl Viro 	if (unlikely(res))
32571ccac622SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
3258b0417d2cSAl Viro 	return res;
32591ccac622SAl Viro }
326031d1726dSAl Viro 
3261c981a482SAl Viro /*
3262c981a482SAl Viro  * Handle the last step of open()
3263c981a482SAl Viro  */
3264c5971b8cSAl Viro static int do_open(struct nameidata *nd,
3265c981a482SAl Viro 		   struct file *file, const struct open_flags *op)
3266c981a482SAl Viro {
3267c981a482SAl Viro 	int open_flag = op->open_flag;
3268c981a482SAl Viro 	bool do_truncate;
3269c981a482SAl Viro 	int acc_mode;
3270c981a482SAl Viro 	int error;
3271c981a482SAl Viro 
3272ff326a32SAl Viro 	if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) {
3273ff326a32SAl Viro 		error = complete_walk(nd);
3274ff326a32SAl Viro 		if (error)
3275ff326a32SAl Viro 			return error;
3276ff326a32SAl Viro 	}
3277973d4b73SAl Viro 	if (!(file->f_mode & FMODE_CREATED))
327876ae2a5aSAl Viro 		audit_inode(nd->name, nd->path.dentry, 0);
327930aba665SSalvatore Mesoraca 	if (open_flag & O_CREAT) {
3280b94e0b32SAl Viro 		if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED))
3281b94e0b32SAl Viro 			return -EEXIST;
328230aba665SSalvatore Mesoraca 		if (d_is_dir(nd->path.dentry))
3283c5971b8cSAl Viro 			return -EISDIR;
32840f705953SAl Viro 		error = may_create_in_sticky(nd->dir_mode, nd->dir_uid,
328530aba665SSalvatore Mesoraca 					     d_backing_inode(nd->path.dentry));
328630aba665SSalvatore Mesoraca 		if (unlikely(error))
3287c5971b8cSAl Viro 			return error;
328830aba665SSalvatore Mesoraca 	}
328944b1d530SMiklos Szeredi 	if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
3290c5971b8cSAl Viro 		return -ENOTDIR;
32916c0d46c4SAl Viro 
32928795e7d4SAl Viro 	do_truncate = false;
32938795e7d4SAl Viro 	acc_mode = op->acc_mode;
32945a2d3eddSAl Viro 	if (file->f_mode & FMODE_CREATED) {
32955a2d3eddSAl Viro 		/* Don't check for write permission, don't truncate */
32965a2d3eddSAl Viro 		open_flag &= ~O_TRUNC;
32975a2d3eddSAl Viro 		acc_mode = 0;
32988795e7d4SAl Viro 	} else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) {
32990f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
33000f9d1a10SAl Viro 		if (error)
3301c5971b8cSAl Viro 			return error;
33028795e7d4SAl Viro 		do_truncate = true;
33030f9d1a10SAl Viro 	}
3304bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
33058795e7d4SAl Viro 	if (!error && !(file->f_mode & FMODE_OPENED))
3306ae2bb293SAl Viro 		error = vfs_open(&nd->path, file);
33078795e7d4SAl Viro 	if (!error)
33086035a27bSAl Viro 		error = ima_file_check(file, op->acc_mode);
33098795e7d4SAl Viro 	if (!error && do_truncate)
33102675a4ebSAl Viro 		error = handle_truncate(file);
3311c80567c8SAl Viro 	if (unlikely(error > 0)) {
3312c80567c8SAl Viro 		WARN_ON(1);
3313c80567c8SAl Viro 		error = -EINVAL;
3314c80567c8SAl Viro 	}
33158795e7d4SAl Viro 	if (do_truncate)
33160f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
3317c5971b8cSAl Viro 	return error;
3318fb1cc555SAl Viro }
3319fb1cc555SAl Viro 
3320af7bd4dcSAmir Goldstein struct dentry *vfs_tmpfile(struct dentry *dentry, umode_t mode, int open_flag)
3321af7bd4dcSAmir Goldstein {
3322af7bd4dcSAmir Goldstein 	struct dentry *child = NULL;
3323af7bd4dcSAmir Goldstein 	struct inode *dir = dentry->d_inode;
3324af7bd4dcSAmir Goldstein 	struct inode *inode;
3325af7bd4dcSAmir Goldstein 	int error;
3326af7bd4dcSAmir Goldstein 
3327af7bd4dcSAmir Goldstein 	/* we want directory to be writable */
3328*47291baaSChristian Brauner 	error = inode_permission(&init_user_ns, dir, MAY_WRITE | MAY_EXEC);
3329af7bd4dcSAmir Goldstein 	if (error)
3330af7bd4dcSAmir Goldstein 		goto out_err;
3331af7bd4dcSAmir Goldstein 	error = -EOPNOTSUPP;
3332af7bd4dcSAmir Goldstein 	if (!dir->i_op->tmpfile)
3333af7bd4dcSAmir Goldstein 		goto out_err;
3334af7bd4dcSAmir Goldstein 	error = -ENOMEM;
3335cdf01226SDavid Howells 	child = d_alloc(dentry, &slash_name);
3336af7bd4dcSAmir Goldstein 	if (unlikely(!child))
3337af7bd4dcSAmir Goldstein 		goto out_err;
3338af7bd4dcSAmir Goldstein 	error = dir->i_op->tmpfile(dir, child, mode);
3339af7bd4dcSAmir Goldstein 	if (error)
3340af7bd4dcSAmir Goldstein 		goto out_err;
3341af7bd4dcSAmir Goldstein 	error = -ENOENT;
3342af7bd4dcSAmir Goldstein 	inode = child->d_inode;
3343af7bd4dcSAmir Goldstein 	if (unlikely(!inode))
3344af7bd4dcSAmir Goldstein 		goto out_err;
3345af7bd4dcSAmir Goldstein 	if (!(open_flag & O_EXCL)) {
3346af7bd4dcSAmir Goldstein 		spin_lock(&inode->i_lock);
3347af7bd4dcSAmir Goldstein 		inode->i_state |= I_LINKABLE;
3348af7bd4dcSAmir Goldstein 		spin_unlock(&inode->i_lock);
3349af7bd4dcSAmir Goldstein 	}
3350fdb2410fSMimi Zohar 	ima_post_create_tmpfile(inode);
3351af7bd4dcSAmir Goldstein 	return child;
3352af7bd4dcSAmir Goldstein 
3353af7bd4dcSAmir Goldstein out_err:
3354af7bd4dcSAmir Goldstein 	dput(child);
3355af7bd4dcSAmir Goldstein 	return ERR_PTR(error);
3356af7bd4dcSAmir Goldstein }
3357af7bd4dcSAmir Goldstein EXPORT_SYMBOL(vfs_tmpfile);
3358af7bd4dcSAmir Goldstein 
3359c8a53ee5SAl Viro static int do_tmpfile(struct nameidata *nd, unsigned flags,
336060545d0dSAl Viro 		const struct open_flags *op,
33613ec2eef1SAl Viro 		struct file *file)
336260545d0dSAl Viro {
3363625b6d10SAl Viro 	struct dentry *child;
3364625b6d10SAl Viro 	struct path path;
3365c8a53ee5SAl Viro 	int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
336660545d0dSAl Viro 	if (unlikely(error))
336760545d0dSAl Viro 		return error;
3368625b6d10SAl Viro 	error = mnt_want_write(path.mnt);
336960545d0dSAl Viro 	if (unlikely(error))
337060545d0dSAl Viro 		goto out;
3371af7bd4dcSAmir Goldstein 	child = vfs_tmpfile(path.dentry, op->mode, op->open_flag);
3372af7bd4dcSAmir Goldstein 	error = PTR_ERR(child);
3373684e73beSHirofumi Nakagawa 	if (IS_ERR(child))
337460545d0dSAl Viro 		goto out2;
3375625b6d10SAl Viro 	dput(path.dentry);
3376625b6d10SAl Viro 	path.dentry = child;
3377c8a53ee5SAl Viro 	audit_inode(nd->name, child, 0);
337869a91c23SEric Rannaud 	/* Don't check for other permissions, the inode was just created */
337962fb4a15SAl Viro 	error = may_open(&path, 0, op->open_flag);
338060545d0dSAl Viro 	if (error)
338160545d0dSAl Viro 		goto out2;
3382625b6d10SAl Viro 	file->f_path.mnt = path.mnt;
3383be12af3eSAl Viro 	error = finish_open(file, child, NULL);
338460545d0dSAl Viro out2:
3385625b6d10SAl Viro 	mnt_drop_write(path.mnt);
338660545d0dSAl Viro out:
3387625b6d10SAl Viro 	path_put(&path);
338860545d0dSAl Viro 	return error;
338960545d0dSAl Viro }
339060545d0dSAl Viro 
33916ac08709SAl Viro static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
33926ac08709SAl Viro {
33936ac08709SAl Viro 	struct path path;
33946ac08709SAl Viro 	int error = path_lookupat(nd, flags, &path);
33956ac08709SAl Viro 	if (!error) {
33966ac08709SAl Viro 		audit_inode(nd->name, path.dentry, 0);
3397ae2bb293SAl Viro 		error = vfs_open(&path, file);
33986ac08709SAl Viro 		path_put(&path);
33996ac08709SAl Viro 	}
34006ac08709SAl Viro 	return error;
34016ac08709SAl Viro }
34026ac08709SAl Viro 
3403c8a53ee5SAl Viro static struct file *path_openat(struct nameidata *nd,
3404c8a53ee5SAl Viro 			const struct open_flags *op, unsigned flags)
34051da177e4SLinus Torvalds {
340630d90494SAl Viro 	struct file *file;
340713aab428SAl Viro 	int error;
340831e6b01fSNick Piggin 
3409ea73ea72SAl Viro 	file = alloc_empty_file(op->open_flag, current_cred());
34101afc99beSAl Viro 	if (IS_ERR(file))
34111afc99beSAl Viro 		return file;
341231e6b01fSNick Piggin 
3413bb458c64SAl Viro 	if (unlikely(file->f_flags & __O_TMPFILE)) {
34143ec2eef1SAl Viro 		error = do_tmpfile(nd, flags, op, file);
34155f336e72SAl Viro 	} else if (unlikely(file->f_flags & O_PATH)) {
34166ac08709SAl Viro 		error = do_o_path(nd, flags, file);
34175f336e72SAl Viro 	} else {
34185f336e72SAl Viro 		const char *s = path_init(nd, flags);
34193bdba28bSAl Viro 		while (!(error = link_path_walk(s, nd)) &&
3420c5971b8cSAl Viro 		       (s = open_last_lookups(nd, file, op)) != NULL)
34211ccac622SAl Viro 			;
3422c5971b8cSAl Viro 		if (!error)
3423c5971b8cSAl Viro 			error = do_open(nd, file, op);
3424deb106c6SAl Viro 		terminate_walk(nd);
34255f336e72SAl Viro 	}
34267c1c01ecSAl Viro 	if (likely(!error)) {
3427aad888f8SAl Viro 		if (likely(file->f_mode & FMODE_OPENED))
34287c1c01ecSAl Viro 			return file;
34297c1c01ecSAl Viro 		WARN_ON(1);
34307c1c01ecSAl Viro 		error = -EINVAL;
3431015c3bbcSMiklos Szeredi 	}
34327c1c01ecSAl Viro 	fput(file);
34332675a4ebSAl Viro 	if (error == -EOPENSTALE) {
34342675a4ebSAl Viro 		if (flags & LOOKUP_RCU)
34352675a4ebSAl Viro 			error = -ECHILD;
34362675a4ebSAl Viro 		else
34372675a4ebSAl Viro 			error = -ESTALE;
34382675a4ebSAl Viro 	}
34397c1c01ecSAl Viro 	return ERR_PTR(error);
3440de459215SKirill Korotaev }
34411da177e4SLinus Torvalds 
3442669abf4eSJeff Layton struct file *do_filp_open(int dfd, struct filename *pathname,
3443f9652e10SAl Viro 		const struct open_flags *op)
344413aab428SAl Viro {
34459883d185SAl Viro 	struct nameidata nd;
3446f9652e10SAl Viro 	int flags = op->lookup_flags;
344713aab428SAl Viro 	struct file *filp;
344813aab428SAl Viro 
34499883d185SAl Viro 	set_nameidata(&nd, dfd, pathname);
3450c8a53ee5SAl Viro 	filp = path_openat(&nd, op, flags | LOOKUP_RCU);
345113aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
3452c8a53ee5SAl Viro 		filp = path_openat(&nd, op, flags);
345313aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
3454c8a53ee5SAl Viro 		filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
34559883d185SAl Viro 	restore_nameidata();
345613aab428SAl Viro 	return filp;
345713aab428SAl Viro }
345813aab428SAl Viro 
345973d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3460f9652e10SAl Viro 		const char *name, const struct open_flags *op)
346173d049a4SAl Viro {
34629883d185SAl Viro 	struct nameidata nd;
346373d049a4SAl Viro 	struct file *file;
346451689104SPaul Moore 	struct filename *filename;
3465f9652e10SAl Viro 	int flags = op->lookup_flags | LOOKUP_ROOT;
346673d049a4SAl Viro 
346773d049a4SAl Viro 	nd.root.mnt = mnt;
346873d049a4SAl Viro 	nd.root.dentry = dentry;
346973d049a4SAl Viro 
3470b18825a7SDavid Howells 	if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
347173d049a4SAl Viro 		return ERR_PTR(-ELOOP);
347273d049a4SAl Viro 
347351689104SPaul Moore 	filename = getname_kernel(name);
3474a1c83681SViresh Kumar 	if (IS_ERR(filename))
347551689104SPaul Moore 		return ERR_CAST(filename);
347651689104SPaul Moore 
34779883d185SAl Viro 	set_nameidata(&nd, -1, filename);
3478c8a53ee5SAl Viro 	file = path_openat(&nd, op, flags | LOOKUP_RCU);
347973d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
3480c8a53ee5SAl Viro 		file = path_openat(&nd, op, flags);
348173d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
3482c8a53ee5SAl Viro 		file = path_openat(&nd, op, flags | LOOKUP_REVAL);
34839883d185SAl Viro 	restore_nameidata();
348451689104SPaul Moore 	putname(filename);
348573d049a4SAl Viro 	return file;
348673d049a4SAl Viro }
348773d049a4SAl Viro 
3488fa14a0b8SAl Viro static struct dentry *filename_create(int dfd, struct filename *name,
34891ac12b4bSJeff Layton 				struct path *path, unsigned int lookup_flags)
34901da177e4SLinus Torvalds {
3491c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
3492391172c4SAl Viro 	struct qstr last;
3493391172c4SAl Viro 	int type;
3494c30dabfeSJan Kara 	int err2;
34951ac12b4bSJeff Layton 	int error;
34961ac12b4bSJeff Layton 	bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
34971ac12b4bSJeff Layton 
34981ac12b4bSJeff Layton 	/*
34991ac12b4bSJeff Layton 	 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
35001ac12b4bSJeff Layton 	 * other flags passed in are ignored!
35011ac12b4bSJeff Layton 	 */
35021ac12b4bSJeff Layton 	lookup_flags &= LOOKUP_REVAL;
35031ac12b4bSJeff Layton 
35045c31b6ceSAl Viro 	name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
35055c31b6ceSAl Viro 	if (IS_ERR(name))
35065c31b6ceSAl Viro 		return ERR_CAST(name);
35071da177e4SLinus Torvalds 
3508c663e5d8SChristoph Hellwig 	/*
3509c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
3510c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
3511c663e5d8SChristoph Hellwig 	 */
35125c31b6ceSAl Viro 	if (unlikely(type != LAST_NORM))
3513ed75e95dSAl Viro 		goto out;
3514c663e5d8SChristoph Hellwig 
3515c30dabfeSJan Kara 	/* don't fail immediately if it's r/o, at least try to report other errors */
3516391172c4SAl Viro 	err2 = mnt_want_write(path->mnt);
3517c663e5d8SChristoph Hellwig 	/*
3518c663e5d8SChristoph Hellwig 	 * Do the final lookup.
3519c663e5d8SChristoph Hellwig 	 */
3520391172c4SAl Viro 	lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
35215955102cSAl Viro 	inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
3522391172c4SAl Viro 	dentry = __lookup_hash(&last, path->dentry, lookup_flags);
35231da177e4SLinus Torvalds 	if (IS_ERR(dentry))
3524a8104a9fSAl Viro 		goto unlock;
3525c663e5d8SChristoph Hellwig 
3526a8104a9fSAl Viro 	error = -EEXIST;
3527b18825a7SDavid Howells 	if (d_is_positive(dentry))
3528a8104a9fSAl Viro 		goto fail;
3529b18825a7SDavid Howells 
3530c663e5d8SChristoph Hellwig 	/*
3531c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
3532c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
3533c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
3534c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
3535c663e5d8SChristoph Hellwig 	 */
3536391172c4SAl Viro 	if (unlikely(!is_dir && last.name[last.len])) {
3537a8104a9fSAl Viro 		error = -ENOENT;
3538ed75e95dSAl Viro 		goto fail;
3539e9baf6e5SAl Viro 	}
3540c30dabfeSJan Kara 	if (unlikely(err2)) {
3541c30dabfeSJan Kara 		error = err2;
3542a8104a9fSAl Viro 		goto fail;
3543c30dabfeSJan Kara 	}
3544181c37b6SAl Viro 	putname(name);
3545e9baf6e5SAl Viro 	return dentry;
35461da177e4SLinus Torvalds fail:
3547a8104a9fSAl Viro 	dput(dentry);
3548a8104a9fSAl Viro 	dentry = ERR_PTR(error);
3549a8104a9fSAl Viro unlock:
35505955102cSAl Viro 	inode_unlock(path->dentry->d_inode);
3551c30dabfeSJan Kara 	if (!err2)
3552391172c4SAl Viro 		mnt_drop_write(path->mnt);
3553ed75e95dSAl Viro out:
3554391172c4SAl Viro 	path_put(path);
3555181c37b6SAl Viro 	putname(name);
3556ed75e95dSAl Viro 	return dentry;
3557dae6ad8fSAl Viro }
3558fa14a0b8SAl Viro 
3559fa14a0b8SAl Viro struct dentry *kern_path_create(int dfd, const char *pathname,
3560fa14a0b8SAl Viro 				struct path *path, unsigned int lookup_flags)
3561fa14a0b8SAl Viro {
3562181c37b6SAl Viro 	return filename_create(dfd, getname_kernel(pathname),
3563181c37b6SAl Viro 				path, lookup_flags);
3564fa14a0b8SAl Viro }
3565dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
3566dae6ad8fSAl Viro 
3567921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry)
3568921a1650SAl Viro {
3569921a1650SAl Viro 	dput(dentry);
35705955102cSAl Viro 	inode_unlock(path->dentry->d_inode);
3571a8104a9fSAl Viro 	mnt_drop_write(path->mnt);
3572921a1650SAl Viro 	path_put(path);
3573921a1650SAl Viro }
3574921a1650SAl Viro EXPORT_SYMBOL(done_path_create);
3575921a1650SAl Viro 
3576520ae687SAl Viro inline struct dentry *user_path_create(int dfd, const char __user *pathname,
35771ac12b4bSJeff Layton 				struct path *path, unsigned int lookup_flags)
3578dae6ad8fSAl Viro {
3579181c37b6SAl Viro 	return filename_create(dfd, getname(pathname), path, lookup_flags);
3580dae6ad8fSAl Viro }
3581dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
3582dae6ad8fSAl Viro 
35831a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
35841da177e4SLinus Torvalds {
3585a3c751a5SMiklos Szeredi 	bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV;
3586a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
35871da177e4SLinus Torvalds 
35881da177e4SLinus Torvalds 	if (error)
35891da177e4SLinus Torvalds 		return error;
35901da177e4SLinus Torvalds 
3591a3c751a5SMiklos Szeredi 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !is_whiteout &&
3592a3c751a5SMiklos Szeredi 	    !capable(CAP_MKNOD))
35931da177e4SLinus Torvalds 		return -EPERM;
35941da177e4SLinus Torvalds 
3595acfa4380SAl Viro 	if (!dir->i_op->mknod)
35961da177e4SLinus Torvalds 		return -EPERM;
35971da177e4SLinus Torvalds 
359808ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
359908ce5f16SSerge E. Hallyn 	if (error)
360008ce5f16SSerge E. Hallyn 		return error;
360108ce5f16SSerge E. Hallyn 
36021da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
36031da177e4SLinus Torvalds 	if (error)
36041da177e4SLinus Torvalds 		return error;
36051da177e4SLinus Torvalds 
36061da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
3607a74574aaSStephen Smalley 	if (!error)
3608f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
36091da177e4SLinus Torvalds 	return error;
36101da177e4SLinus Torvalds }
36114d359507SAl Viro EXPORT_SYMBOL(vfs_mknod);
36121da177e4SLinus Torvalds 
3613f69aac00SAl Viro static int may_mknod(umode_t mode)
3614463c3197SDave Hansen {
3615463c3197SDave Hansen 	switch (mode & S_IFMT) {
3616463c3197SDave Hansen 	case S_IFREG:
3617463c3197SDave Hansen 	case S_IFCHR:
3618463c3197SDave Hansen 	case S_IFBLK:
3619463c3197SDave Hansen 	case S_IFIFO:
3620463c3197SDave Hansen 	case S_IFSOCK:
3621463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
3622463c3197SDave Hansen 		return 0;
3623463c3197SDave Hansen 	case S_IFDIR:
3624463c3197SDave Hansen 		return -EPERM;
3625463c3197SDave Hansen 	default:
3626463c3197SDave Hansen 		return -EINVAL;
3627463c3197SDave Hansen 	}
3628463c3197SDave Hansen }
3629463c3197SDave Hansen 
36305fee64fcSChristoph Hellwig static long do_mknodat(int dfd, const char __user *filename, umode_t mode,
363187c4e192SDominik Brodowski 		unsigned int dev)
36321da177e4SLinus Torvalds {
36331da177e4SLinus Torvalds 	struct dentry *dentry;
3634dae6ad8fSAl Viro 	struct path path;
3635dae6ad8fSAl Viro 	int error;
3636972567f1SJeff Layton 	unsigned int lookup_flags = 0;
36371da177e4SLinus Torvalds 
36388e4bfca1SAl Viro 	error = may_mknod(mode);
36398e4bfca1SAl Viro 	if (error)
36408e4bfca1SAl Viro 		return error;
3641972567f1SJeff Layton retry:
3642972567f1SJeff Layton 	dentry = user_path_create(dfd, filename, &path, lookup_flags);
3643dae6ad8fSAl Viro 	if (IS_ERR(dentry))
3644dae6ad8fSAl Viro 		return PTR_ERR(dentry);
36452ad94ae6SAl Viro 
3646dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3647ce3b0f8dSAl Viro 		mode &= ~current_umask();
3648dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
3649be6d3e56SKentaro Takeda 	if (error)
3650a8104a9fSAl Viro 		goto out;
36511da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
36521da177e4SLinus Torvalds 		case 0: case S_IFREG:
3653312b63fbSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,true);
365405d1a717SMimi Zohar 			if (!error)
365505d1a717SMimi Zohar 				ima_post_path_mknod(dentry);
36561da177e4SLinus Torvalds 			break;
36571da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
3658dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
36591da177e4SLinus Torvalds 					new_decode_dev(dev));
36601da177e4SLinus Torvalds 			break;
36611da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
3662dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
36631da177e4SLinus Torvalds 			break;
36641da177e4SLinus Torvalds 	}
3665a8104a9fSAl Viro out:
3666921a1650SAl Viro 	done_path_create(&path, dentry);
3667972567f1SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3668972567f1SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3669972567f1SJeff Layton 		goto retry;
3670972567f1SJeff Layton 	}
36711da177e4SLinus Torvalds 	return error;
36721da177e4SLinus Torvalds }
36731da177e4SLinus Torvalds 
367487c4e192SDominik Brodowski SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
367587c4e192SDominik Brodowski 		unsigned int, dev)
367687c4e192SDominik Brodowski {
367787c4e192SDominik Brodowski 	return do_mknodat(dfd, filename, mode, dev);
367887c4e192SDominik Brodowski }
367987c4e192SDominik Brodowski 
36808208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
36815590ff0dSUlrich Drepper {
368287c4e192SDominik Brodowski 	return do_mknodat(AT_FDCWD, filename, mode, dev);
36835590ff0dSUlrich Drepper }
36845590ff0dSUlrich Drepper 
368518bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
36861da177e4SLinus Torvalds {
3687a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
36888de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
36891da177e4SLinus Torvalds 
36901da177e4SLinus Torvalds 	if (error)
36911da177e4SLinus Torvalds 		return error;
36921da177e4SLinus Torvalds 
3693acfa4380SAl Viro 	if (!dir->i_op->mkdir)
36941da177e4SLinus Torvalds 		return -EPERM;
36951da177e4SLinus Torvalds 
36961da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
36971da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
36981da177e4SLinus Torvalds 	if (error)
36991da177e4SLinus Torvalds 		return error;
37001da177e4SLinus Torvalds 
37018de52778SAl Viro 	if (max_links && dir->i_nlink >= max_links)
37028de52778SAl Viro 		return -EMLINK;
37038de52778SAl Viro 
37041da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
3705a74574aaSStephen Smalley 	if (!error)
3706f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
37071da177e4SLinus Torvalds 	return error;
37081da177e4SLinus Torvalds }
37094d359507SAl Viro EXPORT_SYMBOL(vfs_mkdir);
37101da177e4SLinus Torvalds 
371183ff98c3SChristoph Hellwig static long do_mkdirat(int dfd, const char __user *pathname, umode_t mode)
37121da177e4SLinus Torvalds {
37136902d925SDave Hansen 	struct dentry *dentry;
3714dae6ad8fSAl Viro 	struct path path;
3715dae6ad8fSAl Viro 	int error;
3716b76d8b82SJeff Layton 	unsigned int lookup_flags = LOOKUP_DIRECTORY;
37171da177e4SLinus Torvalds 
3718b76d8b82SJeff Layton retry:
3719b76d8b82SJeff Layton 	dentry = user_path_create(dfd, pathname, &path, lookup_flags);
37206902d925SDave Hansen 	if (IS_ERR(dentry))
3721dae6ad8fSAl Viro 		return PTR_ERR(dentry);
37226902d925SDave Hansen 
3723dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3724ce3b0f8dSAl Viro 		mode &= ~current_umask();
3725dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
3726a8104a9fSAl Viro 	if (!error)
3727dae6ad8fSAl Viro 		error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3728921a1650SAl Viro 	done_path_create(&path, dentry);
3729b76d8b82SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3730b76d8b82SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3731b76d8b82SJeff Layton 		goto retry;
3732b76d8b82SJeff Layton 	}
37331da177e4SLinus Torvalds 	return error;
37341da177e4SLinus Torvalds }
37351da177e4SLinus Torvalds 
37360101db7aSDominik Brodowski SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
37370101db7aSDominik Brodowski {
37380101db7aSDominik Brodowski 	return do_mkdirat(dfd, pathname, mode);
37390101db7aSDominik Brodowski }
37400101db7aSDominik Brodowski 
3741a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
37425590ff0dSUlrich Drepper {
37430101db7aSDominik Brodowski 	return do_mkdirat(AT_FDCWD, pathname, mode);
37445590ff0dSUlrich Drepper }
37455590ff0dSUlrich Drepper 
37461da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
37471da177e4SLinus Torvalds {
37481da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
37491da177e4SLinus Torvalds 
37501da177e4SLinus Torvalds 	if (error)
37511da177e4SLinus Torvalds 		return error;
37521da177e4SLinus Torvalds 
3753acfa4380SAl Viro 	if (!dir->i_op->rmdir)
37541da177e4SLinus Torvalds 		return -EPERM;
37551da177e4SLinus Torvalds 
37561d2ef590SAl Viro 	dget(dentry);
37575955102cSAl Viro 	inode_lock(dentry->d_inode);
3758912dbc15SSage Weil 
37591da177e4SLinus Torvalds 	error = -EBUSY;
37607af1364fSEric W. Biederman 	if (is_local_mountpoint(dentry))
3761912dbc15SSage Weil 		goto out;
3762912dbc15SSage Weil 
37631da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
3764912dbc15SSage Weil 	if (error)
3765912dbc15SSage Weil 		goto out;
3766912dbc15SSage Weil 
37671da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
3768912dbc15SSage Weil 	if (error)
3769912dbc15SSage Weil 		goto out;
3770912dbc15SSage Weil 
37718767712fSAl Viro 	shrink_dcache_parent(dentry);
37721da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
3773d83c49f3SAl Viro 	dont_mount(dentry);
37748ed936b5SEric W. Biederman 	detach_mounts(dentry);
3775116b9731SAmir Goldstein 	fsnotify_rmdir(dir, dentry);
37761da177e4SLinus Torvalds 
3777912dbc15SSage Weil out:
37785955102cSAl Viro 	inode_unlock(dentry->d_inode);
37791d2ef590SAl Viro 	dput(dentry);
3780912dbc15SSage Weil 	if (!error)
3781912dbc15SSage Weil 		d_delete(dentry);
37821da177e4SLinus Torvalds 	return error;
37831da177e4SLinus Torvalds }
37844d359507SAl Viro EXPORT_SYMBOL(vfs_rmdir);
37851da177e4SLinus Torvalds 
3786e24ab0efSChristoph Hellwig long do_rmdir(int dfd, struct filename *name)
37871da177e4SLinus Torvalds {
37881da177e4SLinus Torvalds 	int error = 0;
37891da177e4SLinus Torvalds 	struct dentry *dentry;
3790f5beed75SAl Viro 	struct path path;
3791f5beed75SAl Viro 	struct qstr last;
3792f5beed75SAl Viro 	int type;
3793c6ee9206SJeff Layton 	unsigned int lookup_flags = 0;
3794c6ee9206SJeff Layton retry:
3795e24ab0efSChristoph Hellwig 	name = filename_parentat(dfd, name, lookup_flags,
3796c1d4dd27SAl Viro 				&path, &last, &type);
379791a27b2aSJeff Layton 	if (IS_ERR(name))
379891a27b2aSJeff Layton 		return PTR_ERR(name);
37991da177e4SLinus Torvalds 
3800f5beed75SAl Viro 	switch (type) {
38011da177e4SLinus Torvalds 	case LAST_DOTDOT:
38021da177e4SLinus Torvalds 		error = -ENOTEMPTY;
38031da177e4SLinus Torvalds 		goto exit1;
38041da177e4SLinus Torvalds 	case LAST_DOT:
38051da177e4SLinus Torvalds 		error = -EINVAL;
38061da177e4SLinus Torvalds 		goto exit1;
38071da177e4SLinus Torvalds 	case LAST_ROOT:
38081da177e4SLinus Torvalds 		error = -EBUSY;
38091da177e4SLinus Torvalds 		goto exit1;
38101da177e4SLinus Torvalds 	}
38110612d9fbSOGAWA Hirofumi 
3812f5beed75SAl Viro 	error = mnt_want_write(path.mnt);
3813c30dabfeSJan Kara 	if (error)
3814c30dabfeSJan Kara 		goto exit1;
38150612d9fbSOGAWA Hirofumi 
38165955102cSAl Viro 	inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
3817f5beed75SAl Viro 	dentry = __lookup_hash(&last, path.dentry, lookup_flags);
38181da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
38196902d925SDave Hansen 	if (IS_ERR(dentry))
38206902d925SDave Hansen 		goto exit2;
3821e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
3822e6bc45d6STheodore Ts'o 		error = -ENOENT;
3823e6bc45d6STheodore Ts'o 		goto exit3;
3824e6bc45d6STheodore Ts'o 	}
3825f5beed75SAl Viro 	error = security_path_rmdir(&path, dentry);
3826be6d3e56SKentaro Takeda 	if (error)
3827c30dabfeSJan Kara 		goto exit3;
3828f5beed75SAl Viro 	error = vfs_rmdir(path.dentry->d_inode, dentry);
38290622753bSDave Hansen exit3:
38301da177e4SLinus Torvalds 	dput(dentry);
38316902d925SDave Hansen exit2:
38325955102cSAl Viro 	inode_unlock(path.dentry->d_inode);
3833f5beed75SAl Viro 	mnt_drop_write(path.mnt);
38341da177e4SLinus Torvalds exit1:
3835f5beed75SAl Viro 	path_put(&path);
3836c6ee9206SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3837c6ee9206SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3838c6ee9206SJeff Layton 		goto retry;
3839c6ee9206SJeff Layton 	}
384024fb33d4SAl Viro 	putname(name);
38411da177e4SLinus Torvalds 	return error;
38421da177e4SLinus Torvalds }
38431da177e4SLinus Torvalds 
38443cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
38455590ff0dSUlrich Drepper {
3846e24ab0efSChristoph Hellwig 	return do_rmdir(AT_FDCWD, getname(pathname));
38475590ff0dSUlrich Drepper }
38485590ff0dSUlrich Drepper 
3849b21996e3SJ. Bruce Fields /**
3850b21996e3SJ. Bruce Fields  * vfs_unlink - unlink a filesystem object
3851b21996e3SJ. Bruce Fields  * @dir:	parent directory
3852b21996e3SJ. Bruce Fields  * @dentry:	victim
3853b21996e3SJ. Bruce Fields  * @delegated_inode: returns victim inode, if the inode is delegated.
3854b21996e3SJ. Bruce Fields  *
3855b21996e3SJ. Bruce Fields  * The caller must hold dir->i_mutex.
3856b21996e3SJ. Bruce Fields  *
3857b21996e3SJ. Bruce Fields  * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3858b21996e3SJ. Bruce Fields  * return a reference to the inode in delegated_inode.  The caller
3859b21996e3SJ. Bruce Fields  * should then break the delegation on that inode and retry.  Because
3860b21996e3SJ. Bruce Fields  * breaking a delegation may take a long time, the caller should drop
3861b21996e3SJ. Bruce Fields  * dir->i_mutex before doing so.
3862b21996e3SJ. Bruce Fields  *
3863b21996e3SJ. Bruce Fields  * Alternatively, a caller may pass NULL for delegated_inode.  This may
3864b21996e3SJ. Bruce Fields  * be appropriate for callers that expect the underlying filesystem not
3865b21996e3SJ. Bruce Fields  * to be NFS exported.
3866b21996e3SJ. Bruce Fields  */
3867b21996e3SJ. Bruce Fields int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
38681da177e4SLinus Torvalds {
38699accbb97SJ. Bruce Fields 	struct inode *target = dentry->d_inode;
38701da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
38711da177e4SLinus Torvalds 
38721da177e4SLinus Torvalds 	if (error)
38731da177e4SLinus Torvalds 		return error;
38741da177e4SLinus Torvalds 
3875acfa4380SAl Viro 	if (!dir->i_op->unlink)
38761da177e4SLinus Torvalds 		return -EPERM;
38771da177e4SLinus Torvalds 
38785955102cSAl Viro 	inode_lock(target);
38798ed936b5SEric W. Biederman 	if (is_local_mountpoint(dentry))
38801da177e4SLinus Torvalds 		error = -EBUSY;
38811da177e4SLinus Torvalds 	else {
38821da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
3883bec1052eSAl Viro 		if (!error) {
38845a14696cSJ. Bruce Fields 			error = try_break_deleg(target, delegated_inode);
38855a14696cSJ. Bruce Fields 			if (error)
3886b21996e3SJ. Bruce Fields 				goto out;
38871da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
38888ed936b5SEric W. Biederman 			if (!error) {
3889d83c49f3SAl Viro 				dont_mount(dentry);
38908ed936b5SEric W. Biederman 				detach_mounts(dentry);
3891116b9731SAmir Goldstein 				fsnotify_unlink(dir, dentry);
38928ed936b5SEric W. Biederman 			}
3893bec1052eSAl Viro 		}
38941da177e4SLinus Torvalds 	}
3895b21996e3SJ. Bruce Fields out:
38965955102cSAl Viro 	inode_unlock(target);
38971da177e4SLinus Torvalds 
38981da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
38991da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
39009accbb97SJ. Bruce Fields 		fsnotify_link_count(target);
39011da177e4SLinus Torvalds 		d_delete(dentry);
39021da177e4SLinus Torvalds 	}
39030eeca283SRobert Love 
39041da177e4SLinus Torvalds 	return error;
39051da177e4SLinus Torvalds }
39064d359507SAl Viro EXPORT_SYMBOL(vfs_unlink);
39071da177e4SLinus Torvalds 
39081da177e4SLinus Torvalds /*
39091da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
39101b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
39111da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
39121da177e4SLinus Torvalds  * while waiting on the I/O.
39131da177e4SLinus Torvalds  */
3914da2f1362SChristoph Hellwig long do_unlinkat(int dfd, struct filename *name)
39151da177e4SLinus Torvalds {
39162ad94ae6SAl Viro 	int error;
39171da177e4SLinus Torvalds 	struct dentry *dentry;
3918f5beed75SAl Viro 	struct path path;
3919f5beed75SAl Viro 	struct qstr last;
3920f5beed75SAl Viro 	int type;
39211da177e4SLinus Torvalds 	struct inode *inode = NULL;
3922b21996e3SJ. Bruce Fields 	struct inode *delegated_inode = NULL;
39235d18f813SJeff Layton 	unsigned int lookup_flags = 0;
39245d18f813SJeff Layton retry:
3925da2f1362SChristoph Hellwig 	name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
392691a27b2aSJeff Layton 	if (IS_ERR(name))
392791a27b2aSJeff Layton 		return PTR_ERR(name);
39282ad94ae6SAl Viro 
39291da177e4SLinus Torvalds 	error = -EISDIR;
3930f5beed75SAl Viro 	if (type != LAST_NORM)
39311da177e4SLinus Torvalds 		goto exit1;
39320612d9fbSOGAWA Hirofumi 
3933f5beed75SAl Viro 	error = mnt_want_write(path.mnt);
3934c30dabfeSJan Kara 	if (error)
3935c30dabfeSJan Kara 		goto exit1;
3936b21996e3SJ. Bruce Fields retry_deleg:
39375955102cSAl Viro 	inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
3938f5beed75SAl Viro 	dentry = __lookup_hash(&last, path.dentry, lookup_flags);
39391da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
39401da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
39411da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
3942f5beed75SAl Viro 		if (last.name[last.len])
394350338b88STörök Edwin 			goto slashes;
39441da177e4SLinus Torvalds 		inode = dentry->d_inode;
3945b18825a7SDavid Howells 		if (d_is_negative(dentry))
3946e6bc45d6STheodore Ts'o 			goto slashes;
39477de9c6eeSAl Viro 		ihold(inode);
3948f5beed75SAl Viro 		error = security_path_unlink(&path, dentry);
3949be6d3e56SKentaro Takeda 		if (error)
3950c30dabfeSJan Kara 			goto exit2;
3951f5beed75SAl Viro 		error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
39521da177e4SLinus Torvalds exit2:
39531da177e4SLinus Torvalds 		dput(dentry);
39541da177e4SLinus Torvalds 	}
39555955102cSAl Viro 	inode_unlock(path.dentry->d_inode);
39561da177e4SLinus Torvalds 	if (inode)
39571da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
3958b21996e3SJ. Bruce Fields 	inode = NULL;
3959b21996e3SJ. Bruce Fields 	if (delegated_inode) {
39605a14696cSJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
3961b21996e3SJ. Bruce Fields 		if (!error)
3962b21996e3SJ. Bruce Fields 			goto retry_deleg;
3963b21996e3SJ. Bruce Fields 	}
3964f5beed75SAl Viro 	mnt_drop_write(path.mnt);
39651da177e4SLinus Torvalds exit1:
3966f5beed75SAl Viro 	path_put(&path);
39675d18f813SJeff Layton 	if (retry_estale(error, lookup_flags)) {
39685d18f813SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
39695d18f813SJeff Layton 		inode = NULL;
39705d18f813SJeff Layton 		goto retry;
39715d18f813SJeff Layton 	}
3972da2f1362SChristoph Hellwig 	putname(name);
39731da177e4SLinus Torvalds 	return error;
39741da177e4SLinus Torvalds 
39751da177e4SLinus Torvalds slashes:
3976b18825a7SDavid Howells 	if (d_is_negative(dentry))
3977b18825a7SDavid Howells 		error = -ENOENT;
397844b1d530SMiklos Szeredi 	else if (d_is_dir(dentry))
3979b18825a7SDavid Howells 		error = -EISDIR;
3980b18825a7SDavid Howells 	else
3981b18825a7SDavid Howells 		error = -ENOTDIR;
39821da177e4SLinus Torvalds 	goto exit2;
39831da177e4SLinus Torvalds }
39841da177e4SLinus Torvalds 
39852e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
39865590ff0dSUlrich Drepper {
39875590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
39885590ff0dSUlrich Drepper 		return -EINVAL;
39895590ff0dSUlrich Drepper 
39905590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
3991e24ab0efSChristoph Hellwig 		return do_rmdir(dfd, getname(pathname));
3992da2f1362SChristoph Hellwig 	return do_unlinkat(dfd, getname(pathname));
39935590ff0dSUlrich Drepper }
39945590ff0dSUlrich Drepper 
39953480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
39965590ff0dSUlrich Drepper {
3997da2f1362SChristoph Hellwig 	return do_unlinkat(AT_FDCWD, getname(pathname));
39985590ff0dSUlrich Drepper }
39995590ff0dSUlrich Drepper 
4000db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
40011da177e4SLinus Torvalds {
4002a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
40031da177e4SLinus Torvalds 
40041da177e4SLinus Torvalds 	if (error)
40051da177e4SLinus Torvalds 		return error;
40061da177e4SLinus Torvalds 
4007acfa4380SAl Viro 	if (!dir->i_op->symlink)
40081da177e4SLinus Torvalds 		return -EPERM;
40091da177e4SLinus Torvalds 
40101da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
40111da177e4SLinus Torvalds 	if (error)
40121da177e4SLinus Torvalds 		return error;
40131da177e4SLinus Torvalds 
40141da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
4015a74574aaSStephen Smalley 	if (!error)
4016f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
40171da177e4SLinus Torvalds 	return error;
40181da177e4SLinus Torvalds }
40194d359507SAl Viro EXPORT_SYMBOL(vfs_symlink);
40201da177e4SLinus Torvalds 
4021cd3acb6aSChristoph Hellwig static long do_symlinkat(const char __user *oldname, int newdfd,
4022b724e846SDominik Brodowski 		  const char __user *newname)
40231da177e4SLinus Torvalds {
40242ad94ae6SAl Viro 	int error;
402591a27b2aSJeff Layton 	struct filename *from;
40266902d925SDave Hansen 	struct dentry *dentry;
4027dae6ad8fSAl Viro 	struct path path;
4028f46d3567SJeff Layton 	unsigned int lookup_flags = 0;
40291da177e4SLinus Torvalds 
40301da177e4SLinus Torvalds 	from = getname(oldname);
40311da177e4SLinus Torvalds 	if (IS_ERR(from))
40321da177e4SLinus Torvalds 		return PTR_ERR(from);
4033f46d3567SJeff Layton retry:
4034f46d3567SJeff Layton 	dentry = user_path_create(newdfd, newname, &path, lookup_flags);
40351da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
40366902d925SDave Hansen 	if (IS_ERR(dentry))
4037dae6ad8fSAl Viro 		goto out_putname;
40386902d925SDave Hansen 
403991a27b2aSJeff Layton 	error = security_path_symlink(&path, dentry, from->name);
4040a8104a9fSAl Viro 	if (!error)
404191a27b2aSJeff Layton 		error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
4042921a1650SAl Viro 	done_path_create(&path, dentry);
4043f46d3567SJeff Layton 	if (retry_estale(error, lookup_flags)) {
4044f46d3567SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
4045f46d3567SJeff Layton 		goto retry;
4046f46d3567SJeff Layton 	}
40476902d925SDave Hansen out_putname:
40481da177e4SLinus Torvalds 	putname(from);
40491da177e4SLinus Torvalds 	return error;
40501da177e4SLinus Torvalds }
40511da177e4SLinus Torvalds 
4052b724e846SDominik Brodowski SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4053b724e846SDominik Brodowski 		int, newdfd, const char __user *, newname)
4054b724e846SDominik Brodowski {
4055b724e846SDominik Brodowski 	return do_symlinkat(oldname, newdfd, newname);
4056b724e846SDominik Brodowski }
4057b724e846SDominik Brodowski 
40583480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
40595590ff0dSUlrich Drepper {
4060b724e846SDominik Brodowski 	return do_symlinkat(oldname, AT_FDCWD, newname);
40615590ff0dSUlrich Drepper }
40625590ff0dSUlrich Drepper 
4063146a8595SJ. Bruce Fields /**
4064146a8595SJ. Bruce Fields  * vfs_link - create a new link
4065146a8595SJ. Bruce Fields  * @old_dentry:	object to be linked
4066146a8595SJ. Bruce Fields  * @dir:	new parent
4067146a8595SJ. Bruce Fields  * @new_dentry:	where to create the new link
4068146a8595SJ. Bruce Fields  * @delegated_inode: returns inode needing a delegation break
4069146a8595SJ. Bruce Fields  *
4070146a8595SJ. Bruce Fields  * The caller must hold dir->i_mutex
4071146a8595SJ. Bruce Fields  *
4072146a8595SJ. Bruce Fields  * If vfs_link discovers a delegation on the to-be-linked file in need
4073146a8595SJ. Bruce Fields  * of breaking, it will return -EWOULDBLOCK and return a reference to the
4074146a8595SJ. Bruce Fields  * inode in delegated_inode.  The caller should then break the delegation
4075146a8595SJ. Bruce Fields  * and retry.  Because breaking a delegation may take a long time, the
4076146a8595SJ. Bruce Fields  * caller should drop the i_mutex before doing so.
4077146a8595SJ. Bruce Fields  *
4078146a8595SJ. Bruce Fields  * Alternatively, a caller may pass NULL for delegated_inode.  This may
4079146a8595SJ. Bruce Fields  * be appropriate for callers that expect the underlying filesystem not
4080146a8595SJ. Bruce Fields  * to be NFS exported.
4081146a8595SJ. Bruce Fields  */
4082146a8595SJ. Bruce Fields int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
40831da177e4SLinus Torvalds {
40841da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
40858de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
40861da177e4SLinus Torvalds 	int error;
40871da177e4SLinus Torvalds 
40881da177e4SLinus Torvalds 	if (!inode)
40891da177e4SLinus Torvalds 		return -ENOENT;
40901da177e4SLinus Torvalds 
4091a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
40921da177e4SLinus Torvalds 	if (error)
40931da177e4SLinus Torvalds 		return error;
40941da177e4SLinus Torvalds 
40951da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
40961da177e4SLinus Torvalds 		return -EXDEV;
40971da177e4SLinus Torvalds 
40981da177e4SLinus Torvalds 	/*
40991da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
41001da177e4SLinus Torvalds 	 */
41011da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
41021da177e4SLinus Torvalds 		return -EPERM;
41030bd23d09SEric W. Biederman 	/*
41040bd23d09SEric W. Biederman 	 * Updating the link count will likely cause i_uid and i_gid to
41050bd23d09SEric W. Biederman 	 * be writen back improperly if their true value is unknown to
41060bd23d09SEric W. Biederman 	 * the vfs.
41070bd23d09SEric W. Biederman 	 */
41080bd23d09SEric W. Biederman 	if (HAS_UNMAPPED_ID(inode))
41090bd23d09SEric W. Biederman 		return -EPERM;
4110acfa4380SAl Viro 	if (!dir->i_op->link)
41111da177e4SLinus Torvalds 		return -EPERM;
41127e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
41131da177e4SLinus Torvalds 		return -EPERM;
41141da177e4SLinus Torvalds 
41151da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
41161da177e4SLinus Torvalds 	if (error)
41171da177e4SLinus Torvalds 		return error;
41181da177e4SLinus Torvalds 
41195955102cSAl Viro 	inode_lock(inode);
4120aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
4121f4e0c30cSAl Viro 	if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
4122aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
41238de52778SAl Viro 	else if (max_links && inode->i_nlink >= max_links)
41248de52778SAl Viro 		error = -EMLINK;
4125146a8595SJ. Bruce Fields 	else {
4126146a8595SJ. Bruce Fields 		error = try_break_deleg(inode, delegated_inode);
4127146a8595SJ. Bruce Fields 		if (!error)
41281da177e4SLinus Torvalds 			error = dir->i_op->link(old_dentry, dir, new_dentry);
4129146a8595SJ. Bruce Fields 	}
4130f4e0c30cSAl Viro 
4131f4e0c30cSAl Viro 	if (!error && (inode->i_state & I_LINKABLE)) {
4132f4e0c30cSAl Viro 		spin_lock(&inode->i_lock);
4133f4e0c30cSAl Viro 		inode->i_state &= ~I_LINKABLE;
4134f4e0c30cSAl Viro 		spin_unlock(&inode->i_lock);
4135f4e0c30cSAl Viro 	}
41365955102cSAl Viro 	inode_unlock(inode);
4137e31e14ecSStephen Smalley 	if (!error)
41387e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
41391da177e4SLinus Torvalds 	return error;
41401da177e4SLinus Torvalds }
41414d359507SAl Viro EXPORT_SYMBOL(vfs_link);
41421da177e4SLinus Torvalds 
41431da177e4SLinus Torvalds /*
41441da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
41451da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
41461da177e4SLinus Torvalds  * newname.  --KAB
41471da177e4SLinus Torvalds  *
41481da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
41491da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
41501da177e4SLinus Torvalds  * and other special files.  --ADM
41511da177e4SLinus Torvalds  */
4152812931d6SChristoph Hellwig static int do_linkat(int olddfd, const char __user *oldname, int newdfd,
415346ea89ebSDominik Brodowski 	      const char __user *newname, int flags)
41541da177e4SLinus Torvalds {
41551da177e4SLinus Torvalds 	struct dentry *new_dentry;
4156dae6ad8fSAl Viro 	struct path old_path, new_path;
4157146a8595SJ. Bruce Fields 	struct inode *delegated_inode = NULL;
415811a7b371SAneesh Kumar K.V 	int how = 0;
41591da177e4SLinus Torvalds 	int error;
41601da177e4SLinus Torvalds 
416111a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
4162c04030e1SUlrich Drepper 		return -EINVAL;
416311a7b371SAneesh Kumar K.V 	/*
4164f0cc6ffbSLinus Torvalds 	 * To use null names we require CAP_DAC_READ_SEARCH
4165f0cc6ffbSLinus Torvalds 	 * This ensures that not everyone will be able to create
4166f0cc6ffbSLinus Torvalds 	 * handlink using the passed filedescriptor.
416711a7b371SAneesh Kumar K.V 	 */
4168f0cc6ffbSLinus Torvalds 	if (flags & AT_EMPTY_PATH) {
4169f0cc6ffbSLinus Torvalds 		if (!capable(CAP_DAC_READ_SEARCH))
4170f0cc6ffbSLinus Torvalds 			return -ENOENT;
417111a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
4172f0cc6ffbSLinus Torvalds 	}
4173c04030e1SUlrich Drepper 
417411a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
417511a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
4176442e31caSJeff Layton retry:
417711a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
41781da177e4SLinus Torvalds 	if (error)
41792ad94ae6SAl Viro 		return error;
41802ad94ae6SAl Viro 
4181442e31caSJeff Layton 	new_dentry = user_path_create(newdfd, newname, &new_path,
4182442e31caSJeff Layton 					(how & LOOKUP_REVAL));
41831da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
41846902d925SDave Hansen 	if (IS_ERR(new_dentry))
4185dae6ad8fSAl Viro 		goto out;
4186dae6ad8fSAl Viro 
4187dae6ad8fSAl Viro 	error = -EXDEV;
4188dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
4189dae6ad8fSAl Viro 		goto out_dput;
4190800179c9SKees Cook 	error = may_linkat(&old_path);
4191800179c9SKees Cook 	if (unlikely(error))
4192800179c9SKees Cook 		goto out_dput;
4193dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
4194be6d3e56SKentaro Takeda 	if (error)
4195a8104a9fSAl Viro 		goto out_dput;
4196146a8595SJ. Bruce Fields 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
419775c3f29dSDave Hansen out_dput:
4198921a1650SAl Viro 	done_path_create(&new_path, new_dentry);
4199146a8595SJ. Bruce Fields 	if (delegated_inode) {
4200146a8595SJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
4201d22e6338SOleg Drokin 		if (!error) {
4202d22e6338SOleg Drokin 			path_put(&old_path);
4203146a8595SJ. Bruce Fields 			goto retry;
4204146a8595SJ. Bruce Fields 		}
4205d22e6338SOleg Drokin 	}
4206442e31caSJeff Layton 	if (retry_estale(error, how)) {
4207d22e6338SOleg Drokin 		path_put(&old_path);
4208442e31caSJeff Layton 		how |= LOOKUP_REVAL;
4209442e31caSJeff Layton 		goto retry;
4210442e31caSJeff Layton 	}
42111da177e4SLinus Torvalds out:
42122d8f3038SAl Viro 	path_put(&old_path);
42131da177e4SLinus Torvalds 
42141da177e4SLinus Torvalds 	return error;
42151da177e4SLinus Torvalds }
42161da177e4SLinus Torvalds 
421746ea89ebSDominik Brodowski SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
421846ea89ebSDominik Brodowski 		int, newdfd, const char __user *, newname, int, flags)
421946ea89ebSDominik Brodowski {
422046ea89ebSDominik Brodowski 	return do_linkat(olddfd, oldname, newdfd, newname, flags);
422146ea89ebSDominik Brodowski }
422246ea89ebSDominik Brodowski 
42233480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
42245590ff0dSUlrich Drepper {
422546ea89ebSDominik Brodowski 	return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
42265590ff0dSUlrich Drepper }
42275590ff0dSUlrich Drepper 
4228bc27027aSMiklos Szeredi /**
4229bc27027aSMiklos Szeredi  * vfs_rename - rename a filesystem object
4230bc27027aSMiklos Szeredi  * @old_dir:	parent of source
4231bc27027aSMiklos Szeredi  * @old_dentry:	source
4232bc27027aSMiklos Szeredi  * @new_dir:	parent of destination
4233bc27027aSMiklos Szeredi  * @new_dentry:	destination
4234bc27027aSMiklos Szeredi  * @delegated_inode: returns an inode needing a delegation break
4235520c8b16SMiklos Szeredi  * @flags:	rename flags
4236bc27027aSMiklos Szeredi  *
4237bc27027aSMiklos Szeredi  * The caller must hold multiple mutexes--see lock_rename()).
4238bc27027aSMiklos Szeredi  *
4239bc27027aSMiklos Szeredi  * If vfs_rename discovers a delegation in need of breaking at either
4240bc27027aSMiklos Szeredi  * the source or destination, it will return -EWOULDBLOCK and return a
4241bc27027aSMiklos Szeredi  * reference to the inode in delegated_inode.  The caller should then
4242bc27027aSMiklos Szeredi  * break the delegation and retry.  Because breaking a delegation may
4243bc27027aSMiklos Szeredi  * take a long time, the caller should drop all locks before doing
4244bc27027aSMiklos Szeredi  * so.
4245bc27027aSMiklos Szeredi  *
4246bc27027aSMiklos Szeredi  * Alternatively, a caller may pass NULL for delegated_inode.  This may
4247bc27027aSMiklos Szeredi  * be appropriate for callers that expect the underlying filesystem not
4248bc27027aSMiklos Szeredi  * to be NFS exported.
4249bc27027aSMiklos Szeredi  *
42501da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
42511da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
42521da177e4SLinus Torvalds  * Problems:
42530117d427SMauro Carvalho Chehab  *
4254d03b29a2SJ. Bruce Fields  *	a) we can get into loop creation.
42551da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
42561da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
4257a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
42581da177e4SLinus Torvalds  *	   story.
42596cedba89SJ. Bruce Fields  *	c) we have to lock _four_ objects - parents and victim (if it exists),
42606cedba89SJ. Bruce Fields  *	   and source (if it is not a directory).
42611b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
42621da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
42631da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
4264a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
42651da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
42661da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
42671da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
4268a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
42691da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
42701da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
42711da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
4272e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
42731b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
42741da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
4275c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
42761da177e4SLinus Torvalds  *	   locking].
42771da177e4SLinus Torvalds  */
42781da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
42798e6d782cSJ. Bruce Fields 	       struct inode *new_dir, struct dentry *new_dentry,
4280520c8b16SMiklos Szeredi 	       struct inode **delegated_inode, unsigned int flags)
42811da177e4SLinus Torvalds {
42821da177e4SLinus Torvalds 	int error;
4283bc27027aSMiklos Szeredi 	bool is_dir = d_is_dir(old_dentry);
4284bc27027aSMiklos Szeredi 	struct inode *source = old_dentry->d_inode;
4285bc27027aSMiklos Szeredi 	struct inode *target = new_dentry->d_inode;
4286da1ce067SMiklos Szeredi 	bool new_is_dir = false;
4287da1ce067SMiklos Szeredi 	unsigned max_links = new_dir->i_sb->s_max_links;
428849d31c2fSAl Viro 	struct name_snapshot old_name;
42891da177e4SLinus Torvalds 
42908d3e2936SMiklos Szeredi 	if (source == target)
42911da177e4SLinus Torvalds 		return 0;
42921da177e4SLinus Torvalds 
42931da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
42941da177e4SLinus Torvalds 	if (error)
42951da177e4SLinus Torvalds 		return error;
42961da177e4SLinus Torvalds 
4297da1ce067SMiklos Szeredi 	if (!target) {
4298a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
4299da1ce067SMiklos Szeredi 	} else {
4300da1ce067SMiklos Szeredi 		new_is_dir = d_is_dir(new_dentry);
4301da1ce067SMiklos Szeredi 
4302da1ce067SMiklos Szeredi 		if (!(flags & RENAME_EXCHANGE))
43031da177e4SLinus Torvalds 			error = may_delete(new_dir, new_dentry, is_dir);
4304da1ce067SMiklos Szeredi 		else
4305da1ce067SMiklos Szeredi 			error = may_delete(new_dir, new_dentry, new_is_dir);
4306da1ce067SMiklos Szeredi 	}
43071da177e4SLinus Torvalds 	if (error)
43081da177e4SLinus Torvalds 		return error;
43091da177e4SLinus Torvalds 
43102773bf00SMiklos Szeredi 	if (!old_dir->i_op->rename)
43111da177e4SLinus Torvalds 		return -EPERM;
43121da177e4SLinus Torvalds 
4313bc27027aSMiklos Szeredi 	/*
4314bc27027aSMiklos Szeredi 	 * If we are going to change the parent - check write permissions,
4315bc27027aSMiklos Szeredi 	 * we'll need to flip '..'.
4316bc27027aSMiklos Szeredi 	 */
4317da1ce067SMiklos Szeredi 	if (new_dir != old_dir) {
4318da1ce067SMiklos Szeredi 		if (is_dir) {
4319*47291baaSChristian Brauner 			error = inode_permission(&init_user_ns, source,
4320*47291baaSChristian Brauner 						 MAY_WRITE);
4321bc27027aSMiklos Szeredi 			if (error)
4322bc27027aSMiklos Szeredi 				return error;
4323bc27027aSMiklos Szeredi 		}
4324da1ce067SMiklos Szeredi 		if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4325*47291baaSChristian Brauner 			error = inode_permission(&init_user_ns, target,
4326*47291baaSChristian Brauner 						 MAY_WRITE);
4327da1ce067SMiklos Szeredi 			if (error)
4328da1ce067SMiklos Szeredi 				return error;
4329da1ce067SMiklos Szeredi 		}
4330da1ce067SMiklos Szeredi 	}
43310eeca283SRobert Love 
43320b3974ebSMiklos Szeredi 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
43330b3974ebSMiklos Szeredi 				      flags);
4334bc27027aSMiklos Szeredi 	if (error)
4335bc27027aSMiklos Szeredi 		return error;
4336bc27027aSMiklos Szeredi 
433749d31c2fSAl Viro 	take_dentry_name_snapshot(&old_name, old_dentry);
4338bc27027aSMiklos Szeredi 	dget(new_dentry);
4339da1ce067SMiklos Szeredi 	if (!is_dir || (flags & RENAME_EXCHANGE))
4340bc27027aSMiklos Szeredi 		lock_two_nondirectories(source, target);
4341bc27027aSMiklos Szeredi 	else if (target)
43425955102cSAl Viro 		inode_lock(target);
4343bc27027aSMiklos Szeredi 
4344bc27027aSMiklos Szeredi 	error = -EBUSY;
43457af1364fSEric W. Biederman 	if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
4346bc27027aSMiklos Szeredi 		goto out;
4347bc27027aSMiklos Szeredi 
4348da1ce067SMiklos Szeredi 	if (max_links && new_dir != old_dir) {
4349bc27027aSMiklos Szeredi 		error = -EMLINK;
4350da1ce067SMiklos Szeredi 		if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
4351bc27027aSMiklos Szeredi 			goto out;
4352da1ce067SMiklos Szeredi 		if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4353da1ce067SMiklos Szeredi 		    old_dir->i_nlink >= max_links)
4354da1ce067SMiklos Szeredi 			goto out;
4355da1ce067SMiklos Szeredi 	}
4356da1ce067SMiklos Szeredi 	if (!is_dir) {
4357bc27027aSMiklos Szeredi 		error = try_break_deleg(source, delegated_inode);
4358bc27027aSMiklos Szeredi 		if (error)
4359bc27027aSMiklos Szeredi 			goto out;
4360da1ce067SMiklos Szeredi 	}
4361da1ce067SMiklos Szeredi 	if (target && !new_is_dir) {
4362bc27027aSMiklos Szeredi 		error = try_break_deleg(target, delegated_inode);
4363bc27027aSMiklos Szeredi 		if (error)
4364bc27027aSMiklos Szeredi 			goto out;
4365bc27027aSMiklos Szeredi 	}
4366520c8b16SMiklos Szeredi 	error = old_dir->i_op->rename(old_dir, old_dentry,
4367520c8b16SMiklos Szeredi 				       new_dir, new_dentry, flags);
4368bc27027aSMiklos Szeredi 	if (error)
4369bc27027aSMiklos Szeredi 		goto out;
4370bc27027aSMiklos Szeredi 
4371da1ce067SMiklos Szeredi 	if (!(flags & RENAME_EXCHANGE) && target) {
43728767712fSAl Viro 		if (is_dir) {
43738767712fSAl Viro 			shrink_dcache_parent(new_dentry);
4374bc27027aSMiklos Szeredi 			target->i_flags |= S_DEAD;
43758767712fSAl Viro 		}
4376bc27027aSMiklos Szeredi 		dont_mount(new_dentry);
43778ed936b5SEric W. Biederman 		detach_mounts(new_dentry);
4378bc27027aSMiklos Szeredi 	}
4379da1ce067SMiklos Szeredi 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4380da1ce067SMiklos Szeredi 		if (!(flags & RENAME_EXCHANGE))
4381bc27027aSMiklos Szeredi 			d_move(old_dentry, new_dentry);
4382da1ce067SMiklos Szeredi 		else
4383da1ce067SMiklos Szeredi 			d_exchange(old_dentry, new_dentry);
4384da1ce067SMiklos Szeredi 	}
4385bc27027aSMiklos Szeredi out:
4386da1ce067SMiklos Szeredi 	if (!is_dir || (flags & RENAME_EXCHANGE))
4387bc27027aSMiklos Szeredi 		unlock_two_nondirectories(source, target);
4388bc27027aSMiklos Szeredi 	else if (target)
43895955102cSAl Viro 		inode_unlock(target);
4390bc27027aSMiklos Szeredi 	dput(new_dentry);
4391da1ce067SMiklos Szeredi 	if (!error) {
4392f4ec3a3dSAl Viro 		fsnotify_move(old_dir, new_dir, &old_name.name, is_dir,
4393da1ce067SMiklos Szeredi 			      !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4394da1ce067SMiklos Szeredi 		if (flags & RENAME_EXCHANGE) {
4395f4ec3a3dSAl Viro 			fsnotify_move(new_dir, old_dir, &old_dentry->d_name,
4396da1ce067SMiklos Szeredi 				      new_is_dir, NULL, new_dentry);
4397da1ce067SMiklos Szeredi 		}
4398da1ce067SMiklos Szeredi 	}
439949d31c2fSAl Viro 	release_dentry_name_snapshot(&old_name);
44000eeca283SRobert Love 
44011da177e4SLinus Torvalds 	return error;
44021da177e4SLinus Torvalds }
44034d359507SAl Viro EXPORT_SYMBOL(vfs_rename);
44041da177e4SLinus Torvalds 
4405e886663cSJens Axboe int do_renameat2(int olddfd, struct filename *from, int newdfd,
4406e886663cSJens Axboe 		 struct filename *to, unsigned int flags)
44071da177e4SLinus Torvalds {
44081da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
44091da177e4SLinus Torvalds 	struct dentry *trap;
4410f5beed75SAl Viro 	struct path old_path, new_path;
4411f5beed75SAl Viro 	struct qstr old_last, new_last;
4412f5beed75SAl Viro 	int old_type, new_type;
44138e6d782cSJ. Bruce Fields 	struct inode *delegated_inode = NULL;
4414f5beed75SAl Viro 	unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
4415c6a94284SJeff Layton 	bool should_retry = false;
4416e886663cSJens Axboe 	int error = -EINVAL;
4417520c8b16SMiklos Szeredi 
44180d7a8555SMiklos Szeredi 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
4419e886663cSJens Axboe 		goto put_both;
4420da1ce067SMiklos Szeredi 
44210d7a8555SMiklos Szeredi 	if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
44220d7a8555SMiklos Szeredi 	    (flags & RENAME_EXCHANGE))
4423e886663cSJens Axboe 		goto put_both;
4424520c8b16SMiklos Szeredi 
4425f5beed75SAl Viro 	if (flags & RENAME_EXCHANGE)
4426f5beed75SAl Viro 		target_flags = 0;
4427f5beed75SAl Viro 
4428c6a94284SJeff Layton retry:
4429e886663cSJens Axboe 	from = filename_parentat(olddfd, from, lookup_flags, &old_path,
4430e886663cSJens Axboe 					&old_last, &old_type);
443191a27b2aSJeff Layton 	if (IS_ERR(from)) {
443291a27b2aSJeff Layton 		error = PTR_ERR(from);
4433e886663cSJens Axboe 		goto put_new;
443491a27b2aSJeff Layton 	}
44351da177e4SLinus Torvalds 
4436e886663cSJens Axboe 	to = filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last,
4437e886663cSJens Axboe 				&new_type);
443891a27b2aSJeff Layton 	if (IS_ERR(to)) {
443991a27b2aSJeff Layton 		error = PTR_ERR(to);
44401da177e4SLinus Torvalds 		goto exit1;
444191a27b2aSJeff Layton 	}
44421da177e4SLinus Torvalds 
44431da177e4SLinus Torvalds 	error = -EXDEV;
4444f5beed75SAl Viro 	if (old_path.mnt != new_path.mnt)
44451da177e4SLinus Torvalds 		goto exit2;
44461da177e4SLinus Torvalds 
44471da177e4SLinus Torvalds 	error = -EBUSY;
4448f5beed75SAl Viro 	if (old_type != LAST_NORM)
44491da177e4SLinus Torvalds 		goto exit2;
44501da177e4SLinus Torvalds 
44510a7c3937SMiklos Szeredi 	if (flags & RENAME_NOREPLACE)
44520a7c3937SMiklos Szeredi 		error = -EEXIST;
4453f5beed75SAl Viro 	if (new_type != LAST_NORM)
44541da177e4SLinus Torvalds 		goto exit2;
44551da177e4SLinus Torvalds 
4456f5beed75SAl Viro 	error = mnt_want_write(old_path.mnt);
4457c30dabfeSJan Kara 	if (error)
4458c30dabfeSJan Kara 		goto exit2;
4459c30dabfeSJan Kara 
44608e6d782cSJ. Bruce Fields retry_deleg:
4461f5beed75SAl Viro 	trap = lock_rename(new_path.dentry, old_path.dentry);
44621da177e4SLinus Torvalds 
4463f5beed75SAl Viro 	old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
44641da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
44651da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
44661da177e4SLinus Torvalds 		goto exit3;
44671da177e4SLinus Torvalds 	/* source must exist */
44681da177e4SLinus Torvalds 	error = -ENOENT;
4469b18825a7SDavid Howells 	if (d_is_negative(old_dentry))
44701da177e4SLinus Torvalds 		goto exit4;
4471f5beed75SAl Viro 	new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
44721da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
44731da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
44741da177e4SLinus Torvalds 		goto exit4;
44750a7c3937SMiklos Szeredi 	error = -EEXIST;
44760a7c3937SMiklos Szeredi 	if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
44770a7c3937SMiklos Szeredi 		goto exit5;
4478da1ce067SMiklos Szeredi 	if (flags & RENAME_EXCHANGE) {
4479da1ce067SMiklos Szeredi 		error = -ENOENT;
4480da1ce067SMiklos Szeredi 		if (d_is_negative(new_dentry))
4481da1ce067SMiklos Szeredi 			goto exit5;
4482da1ce067SMiklos Szeredi 
4483da1ce067SMiklos Szeredi 		if (!d_is_dir(new_dentry)) {
4484da1ce067SMiklos Szeredi 			error = -ENOTDIR;
4485f5beed75SAl Viro 			if (new_last.name[new_last.len])
4486da1ce067SMiklos Szeredi 				goto exit5;
4487da1ce067SMiklos Szeredi 		}
4488da1ce067SMiklos Szeredi 	}
44890a7c3937SMiklos Szeredi 	/* unless the source is a directory trailing slashes give -ENOTDIR */
44900a7c3937SMiklos Szeredi 	if (!d_is_dir(old_dentry)) {
44910a7c3937SMiklos Szeredi 		error = -ENOTDIR;
4492f5beed75SAl Viro 		if (old_last.name[old_last.len])
44930a7c3937SMiklos Szeredi 			goto exit5;
4494f5beed75SAl Viro 		if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
44950a7c3937SMiklos Szeredi 			goto exit5;
44960a7c3937SMiklos Szeredi 	}
44970a7c3937SMiklos Szeredi 	/* source should not be ancestor of target */
44980a7c3937SMiklos Szeredi 	error = -EINVAL;
44990a7c3937SMiklos Szeredi 	if (old_dentry == trap)
45000a7c3937SMiklos Szeredi 		goto exit5;
45011da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
4502da1ce067SMiklos Szeredi 	if (!(flags & RENAME_EXCHANGE))
45031da177e4SLinus Torvalds 		error = -ENOTEMPTY;
45041da177e4SLinus Torvalds 	if (new_dentry == trap)
45051da177e4SLinus Torvalds 		goto exit5;
45061da177e4SLinus Torvalds 
4507f5beed75SAl Viro 	error = security_path_rename(&old_path, old_dentry,
4508f5beed75SAl Viro 				     &new_path, new_dentry, flags);
4509be6d3e56SKentaro Takeda 	if (error)
4510c30dabfeSJan Kara 		goto exit5;
4511f5beed75SAl Viro 	error = vfs_rename(old_path.dentry->d_inode, old_dentry,
4512f5beed75SAl Viro 			   new_path.dentry->d_inode, new_dentry,
4513520c8b16SMiklos Szeredi 			   &delegated_inode, flags);
45141da177e4SLinus Torvalds exit5:
45151da177e4SLinus Torvalds 	dput(new_dentry);
45161da177e4SLinus Torvalds exit4:
45171da177e4SLinus Torvalds 	dput(old_dentry);
45181da177e4SLinus Torvalds exit3:
4519f5beed75SAl Viro 	unlock_rename(new_path.dentry, old_path.dentry);
45208e6d782cSJ. Bruce Fields 	if (delegated_inode) {
45218e6d782cSJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
45228e6d782cSJ. Bruce Fields 		if (!error)
45238e6d782cSJ. Bruce Fields 			goto retry_deleg;
45248e6d782cSJ. Bruce Fields 	}
4525f5beed75SAl Viro 	mnt_drop_write(old_path.mnt);
45261da177e4SLinus Torvalds exit2:
4527c6a94284SJeff Layton 	if (retry_estale(error, lookup_flags))
4528c6a94284SJeff Layton 		should_retry = true;
4529f5beed75SAl Viro 	path_put(&new_path);
45301da177e4SLinus Torvalds exit1:
4531f5beed75SAl Viro 	path_put(&old_path);
4532c6a94284SJeff Layton 	if (should_retry) {
4533c6a94284SJeff Layton 		should_retry = false;
4534c6a94284SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
4535c6a94284SJeff Layton 		goto retry;
4536c6a94284SJeff Layton 	}
4537e886663cSJens Axboe put_both:
4538e886663cSJens Axboe 	if (!IS_ERR(from))
4539e886663cSJens Axboe 		putname(from);
4540e886663cSJens Axboe put_new:
4541e886663cSJens Axboe 	if (!IS_ERR(to))
4542e886663cSJens Axboe 		putname(to);
45431da177e4SLinus Torvalds 	return error;
45441da177e4SLinus Torvalds }
45451da177e4SLinus Torvalds 
4546ee81feb6SDominik Brodowski SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4547ee81feb6SDominik Brodowski 		int, newdfd, const char __user *, newname, unsigned int, flags)
4548ee81feb6SDominik Brodowski {
4549e886663cSJens Axboe 	return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname),
4550e886663cSJens Axboe 				flags);
4551ee81feb6SDominik Brodowski }
4552ee81feb6SDominik Brodowski 
4553520c8b16SMiklos Szeredi SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4554520c8b16SMiklos Szeredi 		int, newdfd, const char __user *, newname)
4555520c8b16SMiklos Szeredi {
4556e886663cSJens Axboe 	return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname),
4557e886663cSJens Axboe 				0);
4558520c8b16SMiklos Szeredi }
4559520c8b16SMiklos Szeredi 
4560a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
45615590ff0dSUlrich Drepper {
4562e886663cSJens Axboe 	return do_renameat2(AT_FDCWD, getname(oldname), AT_FDCWD,
4563e886663cSJens Axboe 				getname(newname), 0);
45645590ff0dSUlrich Drepper }
45655590ff0dSUlrich Drepper 
45665d826c84SAl Viro int readlink_copy(char __user *buffer, int buflen, const char *link)
45671da177e4SLinus Torvalds {
45685d826c84SAl Viro 	int len = PTR_ERR(link);
45691da177e4SLinus Torvalds 	if (IS_ERR(link))
45701da177e4SLinus Torvalds 		goto out;
45711da177e4SLinus Torvalds 
45721da177e4SLinus Torvalds 	len = strlen(link);
45731da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
45741da177e4SLinus Torvalds 		len = buflen;
45751da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
45761da177e4SLinus Torvalds 		len = -EFAULT;
45771da177e4SLinus Torvalds out:
45781da177e4SLinus Torvalds 	return len;
45791da177e4SLinus Torvalds }
45801da177e4SLinus Torvalds 
4581d60874cdSMiklos Szeredi /**
4582fd4a0edfSMiklos Szeredi  * vfs_readlink - copy symlink body into userspace buffer
4583fd4a0edfSMiklos Szeredi  * @dentry: dentry on which to get symbolic link
4584fd4a0edfSMiklos Szeredi  * @buffer: user memory pointer
4585fd4a0edfSMiklos Szeredi  * @buflen: size of buffer
4586fd4a0edfSMiklos Szeredi  *
4587fd4a0edfSMiklos Szeredi  * Does not touch atime.  That's up to the caller if necessary
4588fd4a0edfSMiklos Szeredi  *
4589fd4a0edfSMiklos Szeredi  * Does not call security hook.
4590fd4a0edfSMiklos Szeredi  */
4591fd4a0edfSMiklos Szeredi int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4592fd4a0edfSMiklos Szeredi {
4593fd4a0edfSMiklos Szeredi 	struct inode *inode = d_inode(dentry);
4594f2df5da6SAl Viro 	DEFINE_DELAYED_CALL(done);
4595f2df5da6SAl Viro 	const char *link;
4596f2df5da6SAl Viro 	int res;
4597fd4a0edfSMiklos Szeredi 
459876fca90eSMiklos Szeredi 	if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
459976fca90eSMiklos Szeredi 		if (unlikely(inode->i_op->readlink))
460076fca90eSMiklos Szeredi 			return inode->i_op->readlink(dentry, buffer, buflen);
460176fca90eSMiklos Szeredi 
460276fca90eSMiklos Szeredi 		if (!d_is_symlink(dentry))
4603fd4a0edfSMiklos Szeredi 			return -EINVAL;
4604fd4a0edfSMiklos Szeredi 
460576fca90eSMiklos Szeredi 		spin_lock(&inode->i_lock);
460676fca90eSMiklos Szeredi 		inode->i_opflags |= IOP_DEFAULT_READLINK;
460776fca90eSMiklos Szeredi 		spin_unlock(&inode->i_lock);
460876fca90eSMiklos Szeredi 	}
460976fca90eSMiklos Szeredi 
46104c4f7c19SEric Biggers 	link = READ_ONCE(inode->i_link);
4611f2df5da6SAl Viro 	if (!link) {
4612f2df5da6SAl Viro 		link = inode->i_op->get_link(dentry, inode, &done);
4613f2df5da6SAl Viro 		if (IS_ERR(link))
4614f2df5da6SAl Viro 			return PTR_ERR(link);
4615f2df5da6SAl Viro 	}
4616f2df5da6SAl Viro 	res = readlink_copy(buffer, buflen, link);
4617f2df5da6SAl Viro 	do_delayed_call(&done);
4618f2df5da6SAl Viro 	return res;
4619fd4a0edfSMiklos Szeredi }
4620fd4a0edfSMiklos Szeredi EXPORT_SYMBOL(vfs_readlink);
46211da177e4SLinus Torvalds 
4622d60874cdSMiklos Szeredi /**
4623d60874cdSMiklos Szeredi  * vfs_get_link - get symlink body
4624d60874cdSMiklos Szeredi  * @dentry: dentry on which to get symbolic link
4625d60874cdSMiklos Szeredi  * @done: caller needs to free returned data with this
4626d60874cdSMiklos Szeredi  *
4627d60874cdSMiklos Szeredi  * Calls security hook and i_op->get_link() on the supplied inode.
4628d60874cdSMiklos Szeredi  *
4629d60874cdSMiklos Szeredi  * It does not touch atime.  That's up to the caller if necessary.
4630d60874cdSMiklos Szeredi  *
4631d60874cdSMiklos Szeredi  * Does not work on "special" symlinks like /proc/$$/fd/N
4632d60874cdSMiklos Szeredi  */
4633d60874cdSMiklos Szeredi const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4634d60874cdSMiklos Szeredi {
4635d60874cdSMiklos Szeredi 	const char *res = ERR_PTR(-EINVAL);
4636d60874cdSMiklos Szeredi 	struct inode *inode = d_inode(dentry);
4637d60874cdSMiklos Szeredi 
4638d60874cdSMiklos Szeredi 	if (d_is_symlink(dentry)) {
4639d60874cdSMiklos Szeredi 		res = ERR_PTR(security_inode_readlink(dentry));
4640d60874cdSMiklos Szeredi 		if (!res)
4641d60874cdSMiklos Szeredi 			res = inode->i_op->get_link(dentry, inode, done);
4642d60874cdSMiklos Szeredi 	}
4643d60874cdSMiklos Szeredi 	return res;
4644d60874cdSMiklos Szeredi }
4645d60874cdSMiklos Szeredi EXPORT_SYMBOL(vfs_get_link);
4646d60874cdSMiklos Szeredi 
46471da177e4SLinus Torvalds /* get the link contents into pagecache */
46486b255391SAl Viro const char *page_get_link(struct dentry *dentry, struct inode *inode,
4649fceef393SAl Viro 			  struct delayed_call *callback)
46501da177e4SLinus Torvalds {
4651ebd09abbSDuane Griffin 	char *kaddr;
46521da177e4SLinus Torvalds 	struct page *page;
46536b255391SAl Viro 	struct address_space *mapping = inode->i_mapping;
46546b255391SAl Viro 
4655d3883d4fSAl Viro 	if (!dentry) {
4656d3883d4fSAl Viro 		page = find_get_page(mapping, 0);
4657d3883d4fSAl Viro 		if (!page)
46586b255391SAl Viro 			return ERR_PTR(-ECHILD);
4659d3883d4fSAl Viro 		if (!PageUptodate(page)) {
4660d3883d4fSAl Viro 			put_page(page);
4661d3883d4fSAl Viro 			return ERR_PTR(-ECHILD);
4662d3883d4fSAl Viro 		}
4663d3883d4fSAl Viro 	} else {
4664090d2b18SPekka Enberg 		page = read_mapping_page(mapping, 0, NULL);
46651da177e4SLinus Torvalds 		if (IS_ERR(page))
46666fe6900eSNick Piggin 			return (char*)page;
4667d3883d4fSAl Viro 	}
4668fceef393SAl Viro 	set_delayed_call(callback, page_put_link, page);
466921fc61c7SAl Viro 	BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
467021fc61c7SAl Viro 	kaddr = page_address(page);
46716b255391SAl Viro 	nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
4672ebd09abbSDuane Griffin 	return kaddr;
46731da177e4SLinus Torvalds }
46741da177e4SLinus Torvalds 
46756b255391SAl Viro EXPORT_SYMBOL(page_get_link);
46761da177e4SLinus Torvalds 
4677fceef393SAl Viro void page_put_link(void *arg)
46781da177e4SLinus Torvalds {
4679fceef393SAl Viro 	put_page(arg);
46801da177e4SLinus Torvalds }
46814d359507SAl Viro EXPORT_SYMBOL(page_put_link);
46821da177e4SLinus Torvalds 
4683aa80deabSAl Viro int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4684aa80deabSAl Viro {
4685fceef393SAl Viro 	DEFINE_DELAYED_CALL(done);
46866b255391SAl Viro 	int res = readlink_copy(buffer, buflen,
46876b255391SAl Viro 				page_get_link(dentry, d_inode(dentry),
4688fceef393SAl Viro 					      &done));
4689fceef393SAl Viro 	do_delayed_call(&done);
4690aa80deabSAl Viro 	return res;
4691aa80deabSAl Viro }
4692aa80deabSAl Viro EXPORT_SYMBOL(page_readlink);
4693aa80deabSAl Viro 
469454566b2cSNick Piggin /*
469554566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
469654566b2cSNick Piggin  */
469754566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
46981da177e4SLinus Torvalds {
46991da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
47000adb25d2SKirill Korotaev 	struct page *page;
4701afddba49SNick Piggin 	void *fsdata;
4702beb497abSDmitriy Monakhov 	int err;
4703c718a975STetsuo Handa 	unsigned int flags = 0;
470454566b2cSNick Piggin 	if (nofs)
470554566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
47061da177e4SLinus Torvalds 
47077e53cac4SNeilBrown retry:
4708afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
470954566b2cSNick Piggin 				flags, &page, &fsdata);
47101da177e4SLinus Torvalds 	if (err)
4711afddba49SNick Piggin 		goto fail;
4712afddba49SNick Piggin 
471321fc61c7SAl Viro 	memcpy(page_address(page), symname, len-1);
4714afddba49SNick Piggin 
4715afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4716afddba49SNick Piggin 							page, fsdata);
47171da177e4SLinus Torvalds 	if (err < 0)
47181da177e4SLinus Torvalds 		goto fail;
4719afddba49SNick Piggin 	if (err < len-1)
4720afddba49SNick Piggin 		goto retry;
4721afddba49SNick Piggin 
47221da177e4SLinus Torvalds 	mark_inode_dirty(inode);
47231da177e4SLinus Torvalds 	return 0;
47241da177e4SLinus Torvalds fail:
47251da177e4SLinus Torvalds 	return err;
47261da177e4SLinus Torvalds }
47274d359507SAl Viro EXPORT_SYMBOL(__page_symlink);
47281da177e4SLinus Torvalds 
47290adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
47300adb25d2SKirill Korotaev {
47310adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
4732c62d2555SMichal Hocko 			!mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
47330adb25d2SKirill Korotaev }
47344d359507SAl Viro EXPORT_SYMBOL(page_symlink);
47350adb25d2SKirill Korotaev 
473692e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
47376b255391SAl Viro 	.get_link	= page_get_link,
47381da177e4SLinus Torvalds };
47391da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
4740