xref: /openbmc/linux/fs/namei.c (revision d22e6338)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/namei.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  * Some corrections by tytso.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
121da177e4SLinus Torvalds  * lookup logic.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/init.h>
18630d9c47SPaul Gortmaker #include <linux/export.h>
1944696908SDavid S. Miller #include <linux/kernel.h>
201da177e4SLinus Torvalds #include <linux/slab.h>
211da177e4SLinus Torvalds #include <linux/fs.h>
221da177e4SLinus Torvalds #include <linux/namei.h>
231da177e4SLinus Torvalds #include <linux/pagemap.h>
240eeca283SRobert Love #include <linux/fsnotify.h>
251da177e4SLinus Torvalds #include <linux/personality.h>
261da177e4SLinus Torvalds #include <linux/security.h>
276146f0d5SMimi Zohar #include <linux/ima.h>
281da177e4SLinus Torvalds #include <linux/syscalls.h>
291da177e4SLinus Torvalds #include <linux/mount.h>
301da177e4SLinus Torvalds #include <linux/audit.h>
3116f7e0feSRandy Dunlap #include <linux/capability.h>
32834f2a4aSTrond Myklebust #include <linux/file.h>
335590ff0dSUlrich Drepper #include <linux/fcntl.h>
3408ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h>
355ad4e53bSAl Viro #include <linux/fs_struct.h>
36e77819e5SLinus Torvalds #include <linux/posix_acl.h>
371da177e4SLinus Torvalds #include <asm/uaccess.h>
381da177e4SLinus Torvalds 
39e81e3f4dSEric Paris #include "internal.h"
40c7105365SAl Viro #include "mount.h"
41e81e3f4dSEric Paris 
421da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer]
431da177e4SLinus Torvalds  * Fundamental changes in the pathname lookup mechanisms (namei)
441da177e4SLinus Torvalds  * were necessary because of omirr.  The reason is that omirr needs
451da177e4SLinus Torvalds  * to know the _real_ pathname, not the user-supplied one, in case
461da177e4SLinus Torvalds  * of symlinks (and also when transname replacements occur).
471da177e4SLinus Torvalds  *
481da177e4SLinus Torvalds  * The new code replaces the old recursive symlink resolution with
491da177e4SLinus Torvalds  * an iterative one (in case of non-nested symlink chains).  It does
501da177e4SLinus Torvalds  * this with calls to <fs>_follow_link().
511da177e4SLinus Torvalds  * As a side effect, dir_namei(), _namei() and follow_link() are now
521da177e4SLinus Torvalds  * replaced with a single function lookup_dentry() that can handle all
531da177e4SLinus Torvalds  * the special cases of the former code.
541da177e4SLinus Torvalds  *
551da177e4SLinus Torvalds  * With the new dcache, the pathname is stored at each inode, at least as
561da177e4SLinus Torvalds  * long as the refcount of the inode is positive.  As a side effect, the
571da177e4SLinus Torvalds  * size of the dcache depends on the inode cache and thus is dynamic.
581da177e4SLinus Torvalds  *
591da177e4SLinus Torvalds  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
601da177e4SLinus Torvalds  * resolution to correspond with current state of the code.
611da177e4SLinus Torvalds  *
621da177e4SLinus Torvalds  * Note that the symlink resolution is not *completely* iterative.
631da177e4SLinus Torvalds  * There is still a significant amount of tail- and mid- recursion in
641da177e4SLinus Torvalds  * the algorithm.  Also, note that <fs>_readlink() is not used in
651da177e4SLinus Torvalds  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
661da177e4SLinus Torvalds  * may return different results than <fs>_follow_link().  Many virtual
671da177e4SLinus Torvalds  * filesystems (including /proc) exhibit this behavior.
681da177e4SLinus Torvalds  */
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
711da177e4SLinus Torvalds  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
721da177e4SLinus Torvalds  * and the name already exists in form of a symlink, try to create the new
731da177e4SLinus Torvalds  * name indicated by the symlink. The old code always complained that the
741da177e4SLinus Torvalds  * name already exists, due to not following the symlink even if its target
751da177e4SLinus Torvalds  * is nonexistent.  The new semantics affects also mknod() and link() when
7625985edcSLucas De Marchi  * the name is a symlink pointing to a non-existent name.
771da177e4SLinus Torvalds  *
781da177e4SLinus Torvalds  * I don't know which semantics is the right one, since I have no access
791da177e4SLinus Torvalds  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
801da177e4SLinus Torvalds  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
811da177e4SLinus Torvalds  * "old" one. Personally, I think the new semantics is much more logical.
821da177e4SLinus Torvalds  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
831da177e4SLinus Torvalds  * file does succeed in both HP-UX and SunOs, but not in Solaris
841da177e4SLinus Torvalds  * and in the old Linux semantics.
851da177e4SLinus Torvalds  */
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
881da177e4SLinus Torvalds  * semantics.  See the comments in "open_namei" and "do_link" below.
891da177e4SLinus Torvalds  *
901da177e4SLinus Torvalds  * [10-Sep-98 Alan Modra] Another symlink change.
911da177e4SLinus Torvalds  */
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
941da177e4SLinus Torvalds  *	inside the path - always follow.
951da177e4SLinus Torvalds  *	in the last component in creation/removal/renaming - never follow.
961da177e4SLinus Torvalds  *	if LOOKUP_FOLLOW passed - follow.
971da177e4SLinus Torvalds  *	if the pathname has trailing slashes - follow.
981da177e4SLinus Torvalds  *	otherwise - don't follow.
991da177e4SLinus Torvalds  * (applied in that order).
1001da177e4SLinus Torvalds  *
1011da177e4SLinus Torvalds  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
1021da177e4SLinus Torvalds  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
1031da177e4SLinus Torvalds  * During the 2.4 we need to fix the userland stuff depending on it -
1041da177e4SLinus Torvalds  * hopefully we will be able to get rid of that wart in 2.5. So far only
1051da177e4SLinus Torvalds  * XEmacs seems to be relying on it...
1061da177e4SLinus Torvalds  */
1071da177e4SLinus Torvalds /*
1081da177e4SLinus Torvalds  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
109a11f3a05SArjan van de Ven  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1101da177e4SLinus Torvalds  * any extra contention...
1111da177e4SLinus Torvalds  */
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional
1141da177e4SLinus Torvalds  * checking and hopefully speeding things up, we copy filenames to the
1151da177e4SLinus Torvalds  * kernel data space before using them..
1161da177e4SLinus Torvalds  *
1171da177e4SLinus Torvalds  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
1181da177e4SLinus Torvalds  * PATH_MAX includes the nul terminator --RR.
1191da177e4SLinus Torvalds  */
12091a27b2aSJeff Layton void final_putname(struct filename *name)
1211da177e4SLinus Torvalds {
1227950e385SJeff Layton 	if (name->separate) {
12391a27b2aSJeff Layton 		__putname(name->name);
12491a27b2aSJeff Layton 		kfree(name);
1257950e385SJeff Layton 	} else {
1267950e385SJeff Layton 		__putname(name);
12791a27b2aSJeff Layton 	}
1287950e385SJeff Layton }
1297950e385SJeff Layton 
1307950e385SJeff Layton #define EMBEDDED_NAME_MAX	(PATH_MAX - sizeof(struct filename))
13191a27b2aSJeff Layton 
13291a27b2aSJeff Layton static struct filename *
13391a27b2aSJeff Layton getname_flags(const char __user *filename, int flags, int *empty)
13491a27b2aSJeff Layton {
13591a27b2aSJeff Layton 	struct filename *result, *err;
1363f9f0aa6SLinus Torvalds 	int len;
1377950e385SJeff Layton 	long max;
1387950e385SJeff Layton 	char *kname;
1391da177e4SLinus Torvalds 
1407ac86265SJeff Layton 	result = audit_reusename(filename);
1417ac86265SJeff Layton 	if (result)
1427ac86265SJeff Layton 		return result;
1437ac86265SJeff Layton 
1447950e385SJeff Layton 	result = __getname();
1453f9f0aa6SLinus Torvalds 	if (unlikely(!result))
1464043cde8SEric Paris 		return ERR_PTR(-ENOMEM);
1471da177e4SLinus Torvalds 
1487950e385SJeff Layton 	/*
1497950e385SJeff Layton 	 * First, try to embed the struct filename inside the names_cache
1507950e385SJeff Layton 	 * allocation
1517950e385SJeff Layton 	 */
1527950e385SJeff Layton 	kname = (char *)result + sizeof(*result);
15391a27b2aSJeff Layton 	result->name = kname;
1547950e385SJeff Layton 	result->separate = false;
1557950e385SJeff Layton 	max = EMBEDDED_NAME_MAX;
1567950e385SJeff Layton 
1577950e385SJeff Layton recopy:
1587950e385SJeff Layton 	len = strncpy_from_user(kname, filename, max);
15991a27b2aSJeff Layton 	if (unlikely(len < 0)) {
1603f9f0aa6SLinus Torvalds 		err = ERR_PTR(len);
1613f9f0aa6SLinus Torvalds 		goto error;
16291a27b2aSJeff Layton 	}
1633f9f0aa6SLinus Torvalds 
1647950e385SJeff Layton 	/*
1657950e385SJeff Layton 	 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
1667950e385SJeff Layton 	 * separate struct filename so we can dedicate the entire
1677950e385SJeff Layton 	 * names_cache allocation for the pathname, and re-do the copy from
1687950e385SJeff Layton 	 * userland.
1697950e385SJeff Layton 	 */
1707950e385SJeff Layton 	if (len == EMBEDDED_NAME_MAX && max == EMBEDDED_NAME_MAX) {
1717950e385SJeff Layton 		kname = (char *)result;
1727950e385SJeff Layton 
1737950e385SJeff Layton 		result = kzalloc(sizeof(*result), GFP_KERNEL);
1747950e385SJeff Layton 		if (!result) {
1757950e385SJeff Layton 			err = ERR_PTR(-ENOMEM);
1767950e385SJeff Layton 			result = (struct filename *)kname;
1777950e385SJeff Layton 			goto error;
1787950e385SJeff Layton 		}
1797950e385SJeff Layton 		result->name = kname;
1807950e385SJeff Layton 		result->separate = true;
1817950e385SJeff Layton 		max = PATH_MAX;
1827950e385SJeff Layton 		goto recopy;
1837950e385SJeff Layton 	}
1847950e385SJeff Layton 
1853f9f0aa6SLinus Torvalds 	/* The empty path is special. */
1863f9f0aa6SLinus Torvalds 	if (unlikely(!len)) {
1873f9f0aa6SLinus Torvalds 		if (empty)
1881fa1e7f6SAndy Whitcroft 			*empty = 1;
1893f9f0aa6SLinus Torvalds 		err = ERR_PTR(-ENOENT);
1903f9f0aa6SLinus Torvalds 		if (!(flags & LOOKUP_EMPTY))
1913f9f0aa6SLinus Torvalds 			goto error;
1921da177e4SLinus Torvalds 	}
1933f9f0aa6SLinus Torvalds 
1943f9f0aa6SLinus Torvalds 	err = ERR_PTR(-ENAMETOOLONG);
1957950e385SJeff Layton 	if (unlikely(len >= PATH_MAX))
1967950e385SJeff Layton 		goto error;
1977950e385SJeff Layton 
1987950e385SJeff Layton 	result->uptr = filename;
1991da177e4SLinus Torvalds 	audit_getname(result);
2001da177e4SLinus Torvalds 	return result;
2011da177e4SLinus Torvalds 
2023f9f0aa6SLinus Torvalds error:
2037950e385SJeff Layton 	final_putname(result);
2043f9f0aa6SLinus Torvalds 	return err;
2053f9f0aa6SLinus Torvalds }
2063f9f0aa6SLinus Torvalds 
20791a27b2aSJeff Layton struct filename *
20891a27b2aSJeff Layton getname(const char __user * filename)
209f52e0c11SAl Viro {
210f7493e5dSLinus Torvalds 	return getname_flags(filename, 0, NULL);
211f52e0c11SAl Viro }
212f52e0c11SAl Viro 
2131da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
21491a27b2aSJeff Layton void putname(struct filename *name)
2151da177e4SLinus Torvalds {
2165ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
21791a27b2aSJeff Layton 		return audit_putname(name);
21891a27b2aSJeff Layton 	final_putname(name);
2191da177e4SLinus Torvalds }
2201da177e4SLinus Torvalds #endif
2211da177e4SLinus Torvalds 
222e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask)
223e77819e5SLinus Torvalds {
22484635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL
225e77819e5SLinus Torvalds 	struct posix_acl *acl;
226e77819e5SLinus Torvalds 
227e77819e5SLinus Torvalds 	if (mask & MAY_NOT_BLOCK) {
2283567866bSAl Viro 		acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
2293567866bSAl Viro 	        if (!acl)
230e77819e5SLinus Torvalds 	                return -EAGAIN;
2313567866bSAl Viro 		/* no ->get_acl() calls in RCU mode... */
2323567866bSAl Viro 		if (acl == ACL_NOT_CACHED)
233e77819e5SLinus Torvalds 			return -ECHILD;
234206b1d09SAri Savolainen 	        return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
235e77819e5SLinus Torvalds 	}
236e77819e5SLinus Torvalds 
2372982baa2SChristoph Hellwig 	acl = get_acl(inode, ACL_TYPE_ACCESS);
2384e34e719SChristoph Hellwig 	if (IS_ERR(acl))
2394e34e719SChristoph Hellwig 		return PTR_ERR(acl);
240e77819e5SLinus Torvalds 	if (acl) {
241e77819e5SLinus Torvalds 	        int error = posix_acl_permission(inode, acl, mask);
242e77819e5SLinus Torvalds 	        posix_acl_release(acl);
243e77819e5SLinus Torvalds 	        return error;
244e77819e5SLinus Torvalds 	}
24584635d68SLinus Torvalds #endif
246e77819e5SLinus Torvalds 
247e77819e5SLinus Torvalds 	return -EAGAIN;
248e77819e5SLinus Torvalds }
249e77819e5SLinus Torvalds 
2505909ccaaSLinus Torvalds /*
251948409c7SAndreas Gruenbacher  * This does the basic permission checking
2525909ccaaSLinus Torvalds  */
2537e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask)
2545909ccaaSLinus Torvalds {
25526cf46beSLinus Torvalds 	unsigned int mode = inode->i_mode;
2565909ccaaSLinus Torvalds 
2578e96e3b7SEric W. Biederman 	if (likely(uid_eq(current_fsuid(), inode->i_uid)))
2585909ccaaSLinus Torvalds 		mode >>= 6;
2595909ccaaSLinus Torvalds 	else {
260e77819e5SLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
2617e40145eSAl Viro 			int error = check_acl(inode, mask);
2625909ccaaSLinus Torvalds 			if (error != -EAGAIN)
2635909ccaaSLinus Torvalds 				return error;
2645909ccaaSLinus Torvalds 		}
2655909ccaaSLinus Torvalds 
2665909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
2675909ccaaSLinus Torvalds 			mode >>= 3;
2685909ccaaSLinus Torvalds 	}
2695909ccaaSLinus Torvalds 
2705909ccaaSLinus Torvalds 	/*
2715909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
2725909ccaaSLinus Torvalds 	 */
2739c2c7039SAl Viro 	if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
2745909ccaaSLinus Torvalds 		return 0;
2755909ccaaSLinus Torvalds 	return -EACCES;
2765909ccaaSLinus Torvalds }
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds /**
2791da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
2801da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2818fd90c8dSAndreas Gruenbacher  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
2821da177e4SLinus Torvalds  *
2831da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
2841da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
2851da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
286b74c79e9SNick Piggin  * are used for other things.
287b74c79e9SNick Piggin  *
288b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
289b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
290b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
2911da177e4SLinus Torvalds  */
2922830ba7fSAl Viro int generic_permission(struct inode *inode, int mask)
2931da177e4SLinus Torvalds {
2945909ccaaSLinus Torvalds 	int ret;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	/*
297948409c7SAndreas Gruenbacher 	 * Do the basic permission checks.
2981da177e4SLinus Torvalds 	 */
2997e40145eSAl Viro 	ret = acl_permission_check(inode, mask);
3005909ccaaSLinus Torvalds 	if (ret != -EACCES)
3015909ccaaSLinus Torvalds 		return ret;
3021da177e4SLinus Torvalds 
303d594e7ecSAl Viro 	if (S_ISDIR(inode->i_mode)) {
304d594e7ecSAl Viro 		/* DACs are overridable for directories */
3051a48e2acSEric W. Biederman 		if (inode_capable(inode, CAP_DAC_OVERRIDE))
306d594e7ecSAl Viro 			return 0;
307d594e7ecSAl Viro 		if (!(mask & MAY_WRITE))
3081a48e2acSEric W. Biederman 			if (inode_capable(inode, CAP_DAC_READ_SEARCH))
309d594e7ecSAl Viro 				return 0;
310d594e7ecSAl Viro 		return -EACCES;
311d594e7ecSAl Viro 	}
3121da177e4SLinus Torvalds 	/*
3131da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
314d594e7ecSAl Viro 	 * Executable DACs are overridable when there is
315d594e7ecSAl Viro 	 * at least one exec bit set.
3161da177e4SLinus Torvalds 	 */
317d594e7ecSAl Viro 	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
3181a48e2acSEric W. Biederman 		if (inode_capable(inode, CAP_DAC_OVERRIDE))
3191da177e4SLinus Torvalds 			return 0;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	/*
3221da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
3231da177e4SLinus Torvalds 	 */
3247ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
325d594e7ecSAl Viro 	if (mask == MAY_READ)
3261a48e2acSEric W. Biederman 		if (inode_capable(inode, CAP_DAC_READ_SEARCH))
3271da177e4SLinus Torvalds 			return 0;
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	return -EACCES;
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
3323ddcd056SLinus Torvalds /*
3333ddcd056SLinus Torvalds  * We _really_ want to just do "generic_permission()" without
3343ddcd056SLinus Torvalds  * even looking at the inode->i_op values. So we keep a cache
3353ddcd056SLinus Torvalds  * flag in inode->i_opflags, that says "this has not special
3363ddcd056SLinus Torvalds  * permission function, use the fast case".
3373ddcd056SLinus Torvalds  */
3383ddcd056SLinus Torvalds static inline int do_inode_permission(struct inode *inode, int mask)
3393ddcd056SLinus Torvalds {
3403ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
3413ddcd056SLinus Torvalds 		if (likely(inode->i_op->permission))
3423ddcd056SLinus Torvalds 			return inode->i_op->permission(inode, mask);
3433ddcd056SLinus Torvalds 
3443ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
3453ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
3463ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_FASTPERM;
3473ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
3483ddcd056SLinus Torvalds 	}
3493ddcd056SLinus Torvalds 	return generic_permission(inode, mask);
3503ddcd056SLinus Torvalds }
3513ddcd056SLinus Torvalds 
352cb23beb5SChristoph Hellwig /**
3530bdaea90SDavid Howells  * __inode_permission - Check for access rights to a given inode
3540bdaea90SDavid Howells  * @inode: Inode to check permission on
3550bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
356cb23beb5SChristoph Hellwig  *
3570bdaea90SDavid Howells  * Check for read/write/execute permissions on an inode.
358948409c7SAndreas Gruenbacher  *
359948409c7SAndreas Gruenbacher  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
3600bdaea90SDavid Howells  *
3610bdaea90SDavid Howells  * This does not check for a read-only file system.  You probably want
3620bdaea90SDavid Howells  * inode_permission().
363cb23beb5SChristoph Hellwig  */
3640bdaea90SDavid Howells int __inode_permission(struct inode *inode, int mask)
3651da177e4SLinus Torvalds {
366e6305c43SAl Viro 	int retval;
3671da177e4SLinus Torvalds 
3683ddcd056SLinus Torvalds 	if (unlikely(mask & MAY_WRITE)) {
3691da177e4SLinus Torvalds 		/*
3701da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
3711da177e4SLinus Torvalds 		 */
3721da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
3731da177e4SLinus Torvalds 			return -EACCES;
3741da177e4SLinus Torvalds 	}
3751da177e4SLinus Torvalds 
3763ddcd056SLinus Torvalds 	retval = do_inode_permission(inode, mask);
3771da177e4SLinus Torvalds 	if (retval)
3781da177e4SLinus Torvalds 		return retval;
3791da177e4SLinus Torvalds 
38008ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
38108ce5f16SSerge E. Hallyn 	if (retval)
38208ce5f16SSerge E. Hallyn 		return retval;
38308ce5f16SSerge E. Hallyn 
384d09ca739SEric Paris 	return security_inode_permission(inode, mask);
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds 
387f4d6ff89SAl Viro /**
3880bdaea90SDavid Howells  * sb_permission - Check superblock-level permissions
3890bdaea90SDavid Howells  * @sb: Superblock of inode to check permission on
39055852635SRandy Dunlap  * @inode: Inode to check permission on
3910bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
3920bdaea90SDavid Howells  *
3930bdaea90SDavid Howells  * Separate out file-system wide checks from inode-specific permission checks.
3940bdaea90SDavid Howells  */
3950bdaea90SDavid Howells static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
3960bdaea90SDavid Howells {
3970bdaea90SDavid Howells 	if (unlikely(mask & MAY_WRITE)) {
3980bdaea90SDavid Howells 		umode_t mode = inode->i_mode;
3990bdaea90SDavid Howells 
4000bdaea90SDavid Howells 		/* Nobody gets write access to a read-only fs. */
4010bdaea90SDavid Howells 		if ((sb->s_flags & MS_RDONLY) &&
4020bdaea90SDavid Howells 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
4030bdaea90SDavid Howells 			return -EROFS;
4040bdaea90SDavid Howells 	}
4050bdaea90SDavid Howells 	return 0;
4060bdaea90SDavid Howells }
4070bdaea90SDavid Howells 
4080bdaea90SDavid Howells /**
4090bdaea90SDavid Howells  * inode_permission - Check for access rights to a given inode
4100bdaea90SDavid Howells  * @inode: Inode to check permission on
4110bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
4120bdaea90SDavid Howells  *
4130bdaea90SDavid Howells  * Check for read/write/execute permissions on an inode.  We use fs[ug]id for
4140bdaea90SDavid Howells  * this, letting us set arbitrary permissions for filesystem access without
4150bdaea90SDavid Howells  * changing the "normal" UIDs which are used for other things.
4160bdaea90SDavid Howells  *
4170bdaea90SDavid Howells  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
4180bdaea90SDavid Howells  */
4190bdaea90SDavid Howells int inode_permission(struct inode *inode, int mask)
4200bdaea90SDavid Howells {
4210bdaea90SDavid Howells 	int retval;
4220bdaea90SDavid Howells 
4230bdaea90SDavid Howells 	retval = sb_permission(inode->i_sb, inode, mask);
4240bdaea90SDavid Howells 	if (retval)
4250bdaea90SDavid Howells 		return retval;
4260bdaea90SDavid Howells 	return __inode_permission(inode, mask);
4270bdaea90SDavid Howells }
4280bdaea90SDavid Howells 
4290bdaea90SDavid Howells /**
4305dd784d0SJan Blunck  * path_get - get a reference to a path
4315dd784d0SJan Blunck  * @path: path to get the reference to
4325dd784d0SJan Blunck  *
4335dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
4345dd784d0SJan Blunck  */
435dcf787f3SAl Viro void path_get(const struct path *path)
4365dd784d0SJan Blunck {
4375dd784d0SJan Blunck 	mntget(path->mnt);
4385dd784d0SJan Blunck 	dget(path->dentry);
4395dd784d0SJan Blunck }
4405dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
4415dd784d0SJan Blunck 
4425dd784d0SJan Blunck /**
4431d957f9bSJan Blunck  * path_put - put a reference to a path
4441d957f9bSJan Blunck  * @path: path to put the reference to
4451d957f9bSJan Blunck  *
4461d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
4471d957f9bSJan Blunck  */
448dcf787f3SAl Viro void path_put(const struct path *path)
4491da177e4SLinus Torvalds {
4501d957f9bSJan Blunck 	dput(path->dentry);
4511d957f9bSJan Blunck 	mntput(path->mnt);
4521da177e4SLinus Torvalds }
4531d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
4541da177e4SLinus Torvalds 
45519660af7SAl Viro /*
45631e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
45719660af7SAl Viro  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
45819660af7SAl Viro  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
45919660af7SAl Viro  * normal reference counts on dentries and vfsmounts to transition to rcu-walk
46019660af7SAl Viro  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
46119660af7SAl Viro  * got stuck, so ref-walk may continue from there. If this is not successful
46219660af7SAl Viro  * (eg. a seqcount has changed), then failure is returned and it's up to caller
46319660af7SAl Viro  * to restart the path walk from the beginning in ref-walk mode.
46431e6b01fSNick Piggin  */
46531e6b01fSNick Piggin 
46631e6b01fSNick Piggin /**
46719660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
46819660af7SAl Viro  * @nd: nameidata pathwalk data
46919660af7SAl Viro  * @dentry: child of nd->path.dentry or NULL
47039191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
47131e6b01fSNick Piggin  *
47219660af7SAl Viro  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
47319660af7SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
47419660af7SAl Viro  * @nd or NULL.  Must be called from rcu-walk context.
47531e6b01fSNick Piggin  */
47619660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
47731e6b01fSNick Piggin {
47831e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
47931e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
48031e6b01fSNick Piggin 
48131e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
482e5c832d5SLinus Torvalds 
483e5c832d5SLinus Torvalds 	/*
48448a066e7SAl Viro 	 * After legitimizing the bastards, terminate_walk()
48548a066e7SAl Viro 	 * will do the right thing for non-RCU mode, and all our
48648a066e7SAl Viro 	 * subsequent exit cases should rcu_read_unlock()
48748a066e7SAl Viro 	 * before returning.  Do vfsmount first; if dentry
48848a066e7SAl Viro 	 * can't be legitimized, just set nd->path.dentry to NULL
48948a066e7SAl Viro 	 * and rely on dput(NULL) being a no-op.
490e5c832d5SLinus Torvalds 	 */
49148a066e7SAl Viro 	if (!legitimize_mnt(nd->path.mnt, nd->m_seq))
492e5c832d5SLinus Torvalds 		return -ECHILD;
493e5c832d5SLinus Torvalds 	nd->flags &= ~LOOKUP_RCU;
49415570086SLinus Torvalds 
49548a066e7SAl Viro 	if (!lockref_get_not_dead(&parent->d_lockref)) {
49648a066e7SAl Viro 		nd->path.dentry = NULL;
497d870b4a1SAl Viro 		goto out;
49848a066e7SAl Viro 	}
49948a066e7SAl Viro 
50015570086SLinus Torvalds 	/*
50115570086SLinus Torvalds 	 * For a negative lookup, the lookup sequence point is the parents
50215570086SLinus Torvalds 	 * sequence point, and it only needs to revalidate the parent dentry.
50315570086SLinus Torvalds 	 *
50415570086SLinus Torvalds 	 * For a positive lookup, we need to move both the parent and the
50515570086SLinus Torvalds 	 * dentry from the RCU domain to be properly refcounted. And the
50615570086SLinus Torvalds 	 * sequence number in the dentry validates *both* dentry counters,
50715570086SLinus Torvalds 	 * since we checked the sequence number of the parent after we got
50815570086SLinus Torvalds 	 * the child sequence number. So we know the parent must still
50915570086SLinus Torvalds 	 * be valid if the child sequence number is still valid.
51015570086SLinus Torvalds 	 */
51119660af7SAl Viro 	if (!dentry) {
512e5c832d5SLinus Torvalds 		if (read_seqcount_retry(&parent->d_seq, nd->seq))
513e5c832d5SLinus Torvalds 			goto out;
51419660af7SAl Viro 		BUG_ON(nd->inode != parent->d_inode);
51519660af7SAl Viro 	} else {
516e5c832d5SLinus Torvalds 		if (!lockref_get_not_dead(&dentry->d_lockref))
517e5c832d5SLinus Torvalds 			goto out;
518e5c832d5SLinus Torvalds 		if (read_seqcount_retry(&dentry->d_seq, nd->seq))
519e5c832d5SLinus Torvalds 			goto drop_dentry;
52019660af7SAl Viro 	}
521e5c832d5SLinus Torvalds 
522e5c832d5SLinus Torvalds 	/*
523e5c832d5SLinus Torvalds 	 * Sequence counts matched. Now make sure that the root is
524e5c832d5SLinus Torvalds 	 * still valid and get it if required.
525e5c832d5SLinus Torvalds 	 */
526e5c832d5SLinus Torvalds 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
527e5c832d5SLinus Torvalds 		spin_lock(&fs->lock);
528e5c832d5SLinus Torvalds 		if (nd->root.mnt != fs->root.mnt || nd->root.dentry != fs->root.dentry)
529e5c832d5SLinus Torvalds 			goto unlock_and_drop_dentry;
53031e6b01fSNick Piggin 		path_get(&nd->root);
53131e6b01fSNick Piggin 		spin_unlock(&fs->lock);
53231e6b01fSNick Piggin 	}
53331e6b01fSNick Piggin 
5348b61e74fSAl Viro 	rcu_read_unlock();
53531e6b01fSNick Piggin 	return 0;
53619660af7SAl Viro 
537e5c832d5SLinus Torvalds unlock_and_drop_dentry:
53831e6b01fSNick Piggin 	spin_unlock(&fs->lock);
539e5c832d5SLinus Torvalds drop_dentry:
5408b61e74fSAl Viro 	rcu_read_unlock();
541e5c832d5SLinus Torvalds 	dput(dentry);
542d0d27277SLinus Torvalds 	goto drop_root_mnt;
543e5c832d5SLinus Torvalds out:
5448b61e74fSAl Viro 	rcu_read_unlock();
545d0d27277SLinus Torvalds drop_root_mnt:
546d0d27277SLinus Torvalds 	if (!(nd->flags & LOOKUP_ROOT))
547d0d27277SLinus Torvalds 		nd->root.mnt = NULL;
54831e6b01fSNick Piggin 	return -ECHILD;
54931e6b01fSNick Piggin }
55031e6b01fSNick Piggin 
5514ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
55234286d66SNick Piggin {
5534ce16ef3SAl Viro 	return dentry->d_op->d_revalidate(dentry, flags);
55434286d66SNick Piggin }
55534286d66SNick Piggin 
5569f1fafeeSAl Viro /**
5579f1fafeeSAl Viro  * complete_walk - successful completion of path walk
5589f1fafeeSAl Viro  * @nd:  pointer nameidata
55939159de2SJeff Layton  *
5609f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
5619f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
5629f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
5639f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
5649f1fafeeSAl Viro  * need to drop nd->path.
56539159de2SJeff Layton  */
5669f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
56739159de2SJeff Layton {
56816c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
56939159de2SJeff Layton 	int status;
57039159de2SJeff Layton 
5719f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
5729f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
5739f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
5749f1fafeeSAl Viro 			nd->root.mnt = NULL;
57515570086SLinus Torvalds 
57648a066e7SAl Viro 		if (!legitimize_mnt(nd->path.mnt, nd->m_seq)) {
5778b61e74fSAl Viro 			rcu_read_unlock();
57848a066e7SAl Viro 			return -ECHILD;
57948a066e7SAl Viro 		}
580e5c832d5SLinus Torvalds 		if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) {
5818b61e74fSAl Viro 			rcu_read_unlock();
58248a066e7SAl Viro 			mntput(nd->path.mnt);
5839f1fafeeSAl Viro 			return -ECHILD;
5849f1fafeeSAl Viro 		}
585e5c832d5SLinus Torvalds 		if (read_seqcount_retry(&dentry->d_seq, nd->seq)) {
5868b61e74fSAl Viro 			rcu_read_unlock();
587e5c832d5SLinus Torvalds 			dput(dentry);
58848a066e7SAl Viro 			mntput(nd->path.mnt);
589e5c832d5SLinus Torvalds 			return -ECHILD;
590e5c832d5SLinus Torvalds 		}
5918b61e74fSAl Viro 		rcu_read_unlock();
5929f1fafeeSAl Viro 	}
5939f1fafeeSAl Viro 
59416c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
59539159de2SJeff Layton 		return 0;
59639159de2SJeff Layton 
597ecf3d1f1SJeff Layton 	if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
59816c2cd71SAl Viro 		return 0;
59916c2cd71SAl Viro 
600ecf3d1f1SJeff Layton 	status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
60139159de2SJeff Layton 	if (status > 0)
60239159de2SJeff Layton 		return 0;
60339159de2SJeff Layton 
60416c2cd71SAl Viro 	if (!status)
60539159de2SJeff Layton 		status = -ESTALE;
60616c2cd71SAl Viro 
6079f1fafeeSAl Viro 	path_put(&nd->path);
60839159de2SJeff Layton 	return status;
60939159de2SJeff Layton }
61039159de2SJeff Layton 
6112a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
6122a737871SAl Viro {
613f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
614f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
6152a737871SAl Viro }
6162a737871SAl Viro 
6176de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
6186de88d72SAl Viro 
61931e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
62031e6b01fSNick Piggin {
62131e6b01fSNick Piggin 	if (!nd->root.mnt) {
62231e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
623c28cc364SNick Piggin 		unsigned seq;
624c28cc364SNick Piggin 
625c28cc364SNick Piggin 		do {
626c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
62731e6b01fSNick Piggin 			nd->root = fs->root;
628c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
629c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
63031e6b01fSNick Piggin 	}
63131e6b01fSNick Piggin }
63231e6b01fSNick Piggin 
6331d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
634051d3812SIan Kent {
635051d3812SIan Kent 	dput(path->dentry);
6364ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
637051d3812SIan Kent 		mntput(path->mnt);
638051d3812SIan Kent }
639051d3812SIan Kent 
6407b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
6417b9337aaSNick Piggin 					struct nameidata *nd)
642051d3812SIan Kent {
64331e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
6444ac91378SJan Blunck 		dput(nd->path.dentry);
64531e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
6464ac91378SJan Blunck 			mntput(nd->path.mnt);
6479a229683SHuang Shijie 	}
64831e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
6494ac91378SJan Blunck 	nd->path.dentry = path->dentry;
650051d3812SIan Kent }
651051d3812SIan Kent 
652b5fb63c1SChristoph Hellwig /*
653b5fb63c1SChristoph Hellwig  * Helper to directly jump to a known parsed path from ->follow_link,
654b5fb63c1SChristoph Hellwig  * caller must have taken a reference to path beforehand.
655b5fb63c1SChristoph Hellwig  */
656b5fb63c1SChristoph Hellwig void nd_jump_link(struct nameidata *nd, struct path *path)
657b5fb63c1SChristoph Hellwig {
658b5fb63c1SChristoph Hellwig 	path_put(&nd->path);
659b5fb63c1SChristoph Hellwig 
660b5fb63c1SChristoph Hellwig 	nd->path = *path;
661b5fb63c1SChristoph Hellwig 	nd->inode = nd->path.dentry->d_inode;
662b5fb63c1SChristoph Hellwig 	nd->flags |= LOOKUP_JUMPED;
663b5fb63c1SChristoph Hellwig }
664b5fb63c1SChristoph Hellwig 
665574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
666574197e0SAl Viro {
667574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
6686d7b5aaeSAl Viro 	if (inode->i_op->put_link)
669574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
670574197e0SAl Viro 	path_put(link);
671574197e0SAl Viro }
672574197e0SAl Viro 
673561ec64aSLinus Torvalds int sysctl_protected_symlinks __read_mostly = 0;
674561ec64aSLinus Torvalds int sysctl_protected_hardlinks __read_mostly = 0;
675800179c9SKees Cook 
676800179c9SKees Cook /**
677800179c9SKees Cook  * may_follow_link - Check symlink following for unsafe situations
678800179c9SKees Cook  * @link: The path of the symlink
67955852635SRandy Dunlap  * @nd: nameidata pathwalk data
680800179c9SKees Cook  *
681800179c9SKees Cook  * In the case of the sysctl_protected_symlinks sysctl being enabled,
682800179c9SKees Cook  * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
683800179c9SKees Cook  * in a sticky world-writable directory. This is to protect privileged
684800179c9SKees Cook  * processes from failing races against path names that may change out
685800179c9SKees Cook  * from under them by way of other users creating malicious symlinks.
686800179c9SKees Cook  * It will permit symlinks to be followed only when outside a sticky
687800179c9SKees Cook  * world-writable directory, or when the uid of the symlink and follower
688800179c9SKees Cook  * match, or when the directory owner matches the symlink's owner.
689800179c9SKees Cook  *
690800179c9SKees Cook  * Returns 0 if following the symlink is allowed, -ve on error.
691800179c9SKees Cook  */
692800179c9SKees Cook static inline int may_follow_link(struct path *link, struct nameidata *nd)
693800179c9SKees Cook {
694800179c9SKees Cook 	const struct inode *inode;
695800179c9SKees Cook 	const struct inode *parent;
696800179c9SKees Cook 
697800179c9SKees Cook 	if (!sysctl_protected_symlinks)
698800179c9SKees Cook 		return 0;
699800179c9SKees Cook 
700800179c9SKees Cook 	/* Allowed if owner and follower match. */
701800179c9SKees Cook 	inode = link->dentry->d_inode;
70281abe27bSEric W. Biederman 	if (uid_eq(current_cred()->fsuid, inode->i_uid))
703800179c9SKees Cook 		return 0;
704800179c9SKees Cook 
705800179c9SKees Cook 	/* Allowed if parent directory not sticky and world-writable. */
706800179c9SKees Cook 	parent = nd->path.dentry->d_inode;
707800179c9SKees Cook 	if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
708800179c9SKees Cook 		return 0;
709800179c9SKees Cook 
710800179c9SKees Cook 	/* Allowed if parent directory and link owner match. */
71181abe27bSEric W. Biederman 	if (uid_eq(parent->i_uid, inode->i_uid))
712800179c9SKees Cook 		return 0;
713800179c9SKees Cook 
714ffd8d101SSasha Levin 	audit_log_link_denied("follow_link", link);
715800179c9SKees Cook 	path_put_conditional(link, nd);
716800179c9SKees Cook 	path_put(&nd->path);
717800179c9SKees Cook 	return -EACCES;
718800179c9SKees Cook }
719800179c9SKees Cook 
720800179c9SKees Cook /**
721800179c9SKees Cook  * safe_hardlink_source - Check for safe hardlink conditions
722800179c9SKees Cook  * @inode: the source inode to hardlink from
723800179c9SKees Cook  *
724800179c9SKees Cook  * Return false if at least one of the following conditions:
725800179c9SKees Cook  *    - inode is not a regular file
726800179c9SKees Cook  *    - inode is setuid
727800179c9SKees Cook  *    - inode is setgid and group-exec
728800179c9SKees Cook  *    - access failure for read and write
729800179c9SKees Cook  *
730800179c9SKees Cook  * Otherwise returns true.
731800179c9SKees Cook  */
732800179c9SKees Cook static bool safe_hardlink_source(struct inode *inode)
733800179c9SKees Cook {
734800179c9SKees Cook 	umode_t mode = inode->i_mode;
735800179c9SKees Cook 
736800179c9SKees Cook 	/* Special files should not get pinned to the filesystem. */
737800179c9SKees Cook 	if (!S_ISREG(mode))
738800179c9SKees Cook 		return false;
739800179c9SKees Cook 
740800179c9SKees Cook 	/* Setuid files should not get pinned to the filesystem. */
741800179c9SKees Cook 	if (mode & S_ISUID)
742800179c9SKees Cook 		return false;
743800179c9SKees Cook 
744800179c9SKees Cook 	/* Executable setgid files should not get pinned to the filesystem. */
745800179c9SKees Cook 	if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
746800179c9SKees Cook 		return false;
747800179c9SKees Cook 
748800179c9SKees Cook 	/* Hardlinking to unreadable or unwritable sources is dangerous. */
749800179c9SKees Cook 	if (inode_permission(inode, MAY_READ | MAY_WRITE))
750800179c9SKees Cook 		return false;
751800179c9SKees Cook 
752800179c9SKees Cook 	return true;
753800179c9SKees Cook }
754800179c9SKees Cook 
755800179c9SKees Cook /**
756800179c9SKees Cook  * may_linkat - Check permissions for creating a hardlink
757800179c9SKees Cook  * @link: the source to hardlink from
758800179c9SKees Cook  *
759800179c9SKees Cook  * Block hardlink when all of:
760800179c9SKees Cook  *  - sysctl_protected_hardlinks enabled
761800179c9SKees Cook  *  - fsuid does not match inode
762800179c9SKees Cook  *  - hardlink source is unsafe (see safe_hardlink_source() above)
763800179c9SKees Cook  *  - not CAP_FOWNER
764800179c9SKees Cook  *
765800179c9SKees Cook  * Returns 0 if successful, -ve on error.
766800179c9SKees Cook  */
767800179c9SKees Cook static int may_linkat(struct path *link)
768800179c9SKees Cook {
769800179c9SKees Cook 	const struct cred *cred;
770800179c9SKees Cook 	struct inode *inode;
771800179c9SKees Cook 
772800179c9SKees Cook 	if (!sysctl_protected_hardlinks)
773800179c9SKees Cook 		return 0;
774800179c9SKees Cook 
775800179c9SKees Cook 	cred = current_cred();
776800179c9SKees Cook 	inode = link->dentry->d_inode;
777800179c9SKees Cook 
778800179c9SKees Cook 	/* Source inode owner (or CAP_FOWNER) can hardlink all they like,
779800179c9SKees Cook 	 * otherwise, it must be a safe source.
780800179c9SKees Cook 	 */
78181abe27bSEric W. Biederman 	if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
782800179c9SKees Cook 	    capable(CAP_FOWNER))
783800179c9SKees Cook 		return 0;
784800179c9SKees Cook 
785a51d9eaaSKees Cook 	audit_log_link_denied("linkat", link);
786800179c9SKees Cook 	return -EPERM;
787800179c9SKees Cook }
788800179c9SKees Cook 
789def4af30SAl Viro static __always_inline int
790574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
7911da177e4SLinus Torvalds {
7927b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
7936d7b5aaeSAl Viro 	int error;
7946d7b5aaeSAl Viro 	char *s;
7951da177e4SLinus Torvalds 
796844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
797844a3917SAl Viro 
7980e794589SAl Viro 	if (link->mnt == nd->path.mnt)
7990e794589SAl Viro 		mntget(link->mnt);
8000e794589SAl Viro 
8016d7b5aaeSAl Viro 	error = -ELOOP;
8026d7b5aaeSAl Viro 	if (unlikely(current->total_link_count >= 40))
8036d7b5aaeSAl Viro 		goto out_put_nd_path;
8046d7b5aaeSAl Viro 
805574197e0SAl Viro 	cond_resched();
806574197e0SAl Viro 	current->total_link_count++;
807574197e0SAl Viro 
80868ac1234SAl Viro 	touch_atime(link);
8091da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
810cd4e91d3SAl Viro 
81136f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
8126d7b5aaeSAl Viro 	if (error)
8136d7b5aaeSAl Viro 		goto out_put_nd_path;
81436f3b4f6SAl Viro 
81586acdca1SAl Viro 	nd->last_type = LAST_BIND;
816def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
817def4af30SAl Viro 	error = PTR_ERR(*p);
8186d7b5aaeSAl Viro 	if (IS_ERR(*p))
819408ef013SChristoph Hellwig 		goto out_put_nd_path;
8206d7b5aaeSAl Viro 
821cc314eefSLinus Torvalds 	error = 0;
8226d7b5aaeSAl Viro 	s = nd_get_link(nd);
8236d7b5aaeSAl Viro 	if (s) {
824443ed254SAl Viro 		if (unlikely(IS_ERR(s))) {
825443ed254SAl Viro 			path_put(&nd->path);
826443ed254SAl Viro 			put_link(nd, link, *p);
827443ed254SAl Viro 			return PTR_ERR(s);
828443ed254SAl Viro 		}
829443ed254SAl Viro 		if (*s == '/') {
830443ed254SAl Viro 			set_root(nd);
831443ed254SAl Viro 			path_put(&nd->path);
832443ed254SAl Viro 			nd->path = nd->root;
833443ed254SAl Viro 			path_get(&nd->root);
834443ed254SAl Viro 			nd->flags |= LOOKUP_JUMPED;
835443ed254SAl Viro 		}
836443ed254SAl Viro 		nd->inode = nd->path.dentry->d_inode;
837443ed254SAl Viro 		error = link_path_walk(s, nd);
8386d7b5aaeSAl Viro 		if (unlikely(error))
8396d7b5aaeSAl Viro 			put_link(nd, link, *p);
840b5fb63c1SChristoph Hellwig 	}
8416d7b5aaeSAl Viro 
8426d7b5aaeSAl Viro 	return error;
8436d7b5aaeSAl Viro 
8446d7b5aaeSAl Viro out_put_nd_path:
84598f6ef64SArnd Bergmann 	*p = NULL;
8466d7b5aaeSAl Viro 	path_put(&nd->path);
8476d7b5aaeSAl Viro 	path_put(link);
8481da177e4SLinus Torvalds 	return error;
8491da177e4SLinus Torvalds }
8501da177e4SLinus Torvalds 
85131e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
85231e6b01fSNick Piggin {
8530714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
8540714a533SAl Viro 	struct mount *parent;
85531e6b01fSNick Piggin 	struct dentry *mountpoint;
85631e6b01fSNick Piggin 
8570714a533SAl Viro 	parent = mnt->mnt_parent;
8580714a533SAl Viro 	if (&parent->mnt == path->mnt)
85931e6b01fSNick Piggin 		return 0;
860a73324daSAl Viro 	mountpoint = mnt->mnt_mountpoint;
86131e6b01fSNick Piggin 	path->dentry = mountpoint;
8620714a533SAl Viro 	path->mnt = &parent->mnt;
86331e6b01fSNick Piggin 	return 1;
86431e6b01fSNick Piggin }
86531e6b01fSNick Piggin 
866f015f126SDavid Howells /*
867f015f126SDavid Howells  * follow_up - Find the mountpoint of path's vfsmount
868f015f126SDavid Howells  *
869f015f126SDavid Howells  * Given a path, find the mountpoint of its source file system.
870f015f126SDavid Howells  * Replace @path with the path of the mountpoint in the parent mount.
871f015f126SDavid Howells  * Up is towards /.
872f015f126SDavid Howells  *
873f015f126SDavid Howells  * Return 1 if we went up a level and 0 if we were already at the
874f015f126SDavid Howells  * root.
875f015f126SDavid Howells  */
876bab77ebfSAl Viro int follow_up(struct path *path)
8771da177e4SLinus Torvalds {
8780714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
8790714a533SAl Viro 	struct mount *parent;
8801da177e4SLinus Torvalds 	struct dentry *mountpoint;
88199b7db7bSNick Piggin 
88248a066e7SAl Viro 	read_seqlock_excl(&mount_lock);
8830714a533SAl Viro 	parent = mnt->mnt_parent;
8843c0a6163SAl Viro 	if (parent == mnt) {
88548a066e7SAl Viro 		read_sequnlock_excl(&mount_lock);
8861da177e4SLinus Torvalds 		return 0;
8871da177e4SLinus Torvalds 	}
8880714a533SAl Viro 	mntget(&parent->mnt);
889a73324daSAl Viro 	mountpoint = dget(mnt->mnt_mountpoint);
89048a066e7SAl Viro 	read_sequnlock_excl(&mount_lock);
891bab77ebfSAl Viro 	dput(path->dentry);
892bab77ebfSAl Viro 	path->dentry = mountpoint;
893bab77ebfSAl Viro 	mntput(path->mnt);
8940714a533SAl Viro 	path->mnt = &parent->mnt;
8951da177e4SLinus Torvalds 	return 1;
8961da177e4SLinus Torvalds }
8971da177e4SLinus Torvalds 
898b5c84bf6SNick Piggin /*
8999875cf80SDavid Howells  * Perform an automount
9009875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
9019875cf80SDavid Howells  *   were called with.
9021da177e4SLinus Torvalds  */
9039875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
9049875cf80SDavid Howells 			    bool *need_mntput)
90531e6b01fSNick Piggin {
9069875cf80SDavid Howells 	struct vfsmount *mnt;
907ea5b778aSDavid Howells 	int err;
9089875cf80SDavid Howells 
9099875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
9109875cf80SDavid Howells 		return -EREMOTE;
9119875cf80SDavid Howells 
9120ec26fd0SMiklos Szeredi 	/* We don't want to mount if someone's just doing a stat -
9130ec26fd0SMiklos Szeredi 	 * unless they're stat'ing a directory and appended a '/' to
9140ec26fd0SMiklos Szeredi 	 * the name.
9150ec26fd0SMiklos Szeredi 	 *
9160ec26fd0SMiklos Szeredi 	 * We do, however, want to mount if someone wants to open or
9175a30d8a2SDavid Howells 	 * create a file of any type under the mountpoint, wants to
9180ec26fd0SMiklos Szeredi 	 * traverse through the mountpoint or wants to open the
9190ec26fd0SMiklos Szeredi 	 * mounted directory.  Also, autofs may mark negative dentries
9200ec26fd0SMiklos Szeredi 	 * as being automount points.  These will need the attentions
9210ec26fd0SMiklos Szeredi 	 * of the daemon to instantiate them before they can be used.
9225a30d8a2SDavid Howells 	 */
9235a30d8a2SDavid Howells 	if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
924d94c177bSLinus Torvalds 		     LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
9255a30d8a2SDavid Howells 	    path->dentry->d_inode)
9269875cf80SDavid Howells 		return -EISDIR;
9270ec26fd0SMiklos Szeredi 
9289875cf80SDavid Howells 	current->total_link_count++;
9299875cf80SDavid Howells 	if (current->total_link_count >= 40)
9309875cf80SDavid Howells 		return -ELOOP;
9319875cf80SDavid Howells 
9329875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
9339875cf80SDavid Howells 	if (IS_ERR(mnt)) {
9349875cf80SDavid Howells 		/*
9359875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
9369875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
9379875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
9389875cf80SDavid Howells 		 *
9399875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
9409875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
9419875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
9429875cf80SDavid Howells 		 */
94349084c3bSAl Viro 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
9449875cf80SDavid Howells 			return -EREMOTE;
9459875cf80SDavid Howells 		return PTR_ERR(mnt);
94631e6b01fSNick Piggin 	}
947ea5b778aSDavid Howells 
9489875cf80SDavid Howells 	if (!mnt) /* mount collision */
9499875cf80SDavid Howells 		return 0;
9509875cf80SDavid Howells 
9518aef1884SAl Viro 	if (!*need_mntput) {
9528aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
9538aef1884SAl Viro 		mntget(path->mnt);
9548aef1884SAl Viro 		*need_mntput = true;
9558aef1884SAl Viro 	}
95619a167afSAl Viro 	err = finish_automount(mnt, path);
957ea5b778aSDavid Howells 
958ea5b778aSDavid Howells 	switch (err) {
959ea5b778aSDavid Howells 	case -EBUSY:
960ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
96119a167afSAl Viro 		return 0;
962ea5b778aSDavid Howells 	case 0:
9638aef1884SAl Viro 		path_put(path);
9649875cf80SDavid Howells 		path->mnt = mnt;
9659875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
9669875cf80SDavid Howells 		return 0;
96719a167afSAl Viro 	default:
96819a167afSAl Viro 		return err;
9699875cf80SDavid Howells 	}
97019a167afSAl Viro 
971ea5b778aSDavid Howells }
9729875cf80SDavid Howells 
9739875cf80SDavid Howells /*
9749875cf80SDavid Howells  * Handle a dentry that is managed in some way.
975cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
9769875cf80SDavid Howells  * - Flagged as mountpoint
9779875cf80SDavid Howells  * - Flagged as automount point
9789875cf80SDavid Howells  *
9799875cf80SDavid Howells  * This may only be called in refwalk mode.
9809875cf80SDavid Howells  *
9819875cf80SDavid Howells  * Serialization is taken care of in namespace.c
9829875cf80SDavid Howells  */
9839875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
9849875cf80SDavid Howells {
9858aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
9869875cf80SDavid Howells 	unsigned managed;
9879875cf80SDavid Howells 	bool need_mntput = false;
9888aef1884SAl Viro 	int ret = 0;
9899875cf80SDavid Howells 
9909875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
9919875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
9929875cf80SDavid Howells 	 * the components of that value change under us */
9939875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
9949875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
9959875cf80SDavid Howells 	       unlikely(managed != 0)) {
996cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
997cc53ce53SDavid Howells 		 * being held. */
998cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
999cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1000cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
10011aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
1002cc53ce53SDavid Howells 			if (ret < 0)
10038aef1884SAl Viro 				break;
1004cc53ce53SDavid Howells 		}
1005cc53ce53SDavid Howells 
10069875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
10079875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
10089875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
10099875cf80SDavid Howells 			if (mounted) {
10109875cf80SDavid Howells 				dput(path->dentry);
10119875cf80SDavid Howells 				if (need_mntput)
1012463ffb2eSAl Viro 					mntput(path->mnt);
1013463ffb2eSAl Viro 				path->mnt = mounted;
1014463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
10159875cf80SDavid Howells 				need_mntput = true;
10169875cf80SDavid Howells 				continue;
1017463ffb2eSAl Viro 			}
1018463ffb2eSAl Viro 
10199875cf80SDavid Howells 			/* Something is mounted on this dentry in another
10209875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
102148a066e7SAl Viro 			 * namespace got unmounted before lookup_mnt() could
102248a066e7SAl Viro 			 * get it */
10231da177e4SLinus Torvalds 		}
10249875cf80SDavid Howells 
10259875cf80SDavid Howells 		/* Handle an automount point */
10269875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
10279875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
10289875cf80SDavid Howells 			if (ret < 0)
10298aef1884SAl Viro 				break;
10309875cf80SDavid Howells 			continue;
10319875cf80SDavid Howells 		}
10329875cf80SDavid Howells 
10339875cf80SDavid Howells 		/* We didn't change the current path point */
10349875cf80SDavid Howells 		break;
10359875cf80SDavid Howells 	}
10368aef1884SAl Viro 
10378aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
10388aef1884SAl Viro 		mntput(path->mnt);
10398aef1884SAl Viro 	if (ret == -EISDIR)
10408aef1884SAl Viro 		ret = 0;
1041a3fbbde7SAl Viro 	return ret < 0 ? ret : need_mntput;
10421da177e4SLinus Torvalds }
10431da177e4SLinus Torvalds 
1044cc53ce53SDavid Howells int follow_down_one(struct path *path)
10451da177e4SLinus Torvalds {
10461da177e4SLinus Torvalds 	struct vfsmount *mounted;
10471da177e4SLinus Torvalds 
10481c755af4SAl Viro 	mounted = lookup_mnt(path);
10491da177e4SLinus Torvalds 	if (mounted) {
10509393bd07SAl Viro 		dput(path->dentry);
10519393bd07SAl Viro 		mntput(path->mnt);
10529393bd07SAl Viro 		path->mnt = mounted;
10539393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
10541da177e4SLinus Torvalds 		return 1;
10551da177e4SLinus Torvalds 	}
10561da177e4SLinus Torvalds 	return 0;
10571da177e4SLinus Torvalds }
10581da177e4SLinus Torvalds 
105962a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
106062a7375eSIan Kent {
106162a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
106262a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
106362a7375eSIan Kent }
106462a7375eSIan Kent 
10659875cf80SDavid Howells /*
1066287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
1067287548e4SAl Viro  * we meet a managed dentry that would need blocking.
10689875cf80SDavid Howells  */
10699875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1070287548e4SAl Viro 			       struct inode **inode)
10719875cf80SDavid Howells {
107262a7375eSIan Kent 	for (;;) {
1073c7105365SAl Viro 		struct mount *mounted;
107462a7375eSIan Kent 		/*
107562a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
107662a7375eSIan Kent 		 * that wants to block transit.
107762a7375eSIan Kent 		 */
1078287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
1079ab90911fSDavid Howells 			return false;
108062a7375eSIan Kent 
108162a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
108262a7375eSIan Kent 			break;
108362a7375eSIan Kent 
1084474279dcSAl Viro 		mounted = __lookup_mnt(path->mnt, path->dentry);
10859875cf80SDavid Howells 		if (!mounted)
10869875cf80SDavid Howells 			break;
1087c7105365SAl Viro 		path->mnt = &mounted->mnt;
1088c7105365SAl Viro 		path->dentry = mounted->mnt.mnt_root;
1089a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
10909875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
109159430262SLinus Torvalds 		/*
109259430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
109359430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
109459430262SLinus Torvalds 		 * because a mount-point is always pinned.
109559430262SLinus Torvalds 		 */
109659430262SLinus Torvalds 		*inode = path->dentry->d_inode;
10979875cf80SDavid Howells 	}
10989875cf80SDavid Howells 	return true;
10999875cf80SDavid Howells }
11009875cf80SDavid Howells 
1101dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
1102287548e4SAl Viro {
1103dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
1104c7105365SAl Viro 		struct mount *mounted;
1105474279dcSAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
1106287548e4SAl Viro 		if (!mounted)
1107287548e4SAl Viro 			break;
1108c7105365SAl Viro 		nd->path.mnt = &mounted->mnt;
1109c7105365SAl Viro 		nd->path.dentry = mounted->mnt.mnt_root;
1110dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1111287548e4SAl Viro 	}
1112287548e4SAl Viro }
1113287548e4SAl Viro 
111431e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
111531e6b01fSNick Piggin {
111631e6b01fSNick Piggin 	set_root_rcu(nd);
111731e6b01fSNick Piggin 
111831e6b01fSNick Piggin 	while (1) {
111931e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
112031e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
112131e6b01fSNick Piggin 			break;
112231e6b01fSNick Piggin 		}
112331e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
112431e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
112531e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
112631e6b01fSNick Piggin 			unsigned seq;
112731e6b01fSNick Piggin 
112831e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
112931e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
1130ef7562d5SAl Viro 				goto failed;
113131e6b01fSNick Piggin 			nd->path.dentry = parent;
113231e6b01fSNick Piggin 			nd->seq = seq;
113331e6b01fSNick Piggin 			break;
113431e6b01fSNick Piggin 		}
113531e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
113631e6b01fSNick Piggin 			break;
113731e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
113831e6b01fSNick Piggin 	}
1139dea39376SAl Viro 	follow_mount_rcu(nd);
1140dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
114131e6b01fSNick Piggin 	return 0;
1142ef7562d5SAl Viro 
1143ef7562d5SAl Viro failed:
1144ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
11455b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
1146ef7562d5SAl Viro 		nd->root.mnt = NULL;
11478b61e74fSAl Viro 	rcu_read_unlock();
1148ef7562d5SAl Viro 	return -ECHILD;
114931e6b01fSNick Piggin }
115031e6b01fSNick Piggin 
11519875cf80SDavid Howells /*
1152cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
1153cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
1154cc53ce53SDavid Howells  * caller is permitted to proceed or not.
1155cc53ce53SDavid Howells  */
11567cc90cc3SAl Viro int follow_down(struct path *path)
1157cc53ce53SDavid Howells {
1158cc53ce53SDavid Howells 	unsigned managed;
1159cc53ce53SDavid Howells 	int ret;
1160cc53ce53SDavid Howells 
1161cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
1162cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1163cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1164cc53ce53SDavid Howells 		 * being held.
1165cc53ce53SDavid Howells 		 *
1166cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
1167cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
1168cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
1169cc53ce53SDavid Howells 		 * superstructure.
1170cc53ce53SDavid Howells 		 *
1171cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
1172cc53ce53SDavid Howells 		 */
1173cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1174cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1175cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
1176ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
11771aed3e42SAl Viro 				path->dentry, false);
1178cc53ce53SDavid Howells 			if (ret < 0)
1179cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
1180cc53ce53SDavid Howells 		}
1181cc53ce53SDavid Howells 
1182cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
1183cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
1184cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
1185cc53ce53SDavid Howells 			if (!mounted)
1186cc53ce53SDavid Howells 				break;
1187cc53ce53SDavid Howells 			dput(path->dentry);
1188cc53ce53SDavid Howells 			mntput(path->mnt);
1189cc53ce53SDavid Howells 			path->mnt = mounted;
1190cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1191cc53ce53SDavid Howells 			continue;
1192cc53ce53SDavid Howells 		}
1193cc53ce53SDavid Howells 
1194cc53ce53SDavid Howells 		/* Don't handle automount points here */
1195cc53ce53SDavid Howells 		break;
1196cc53ce53SDavid Howells 	}
1197cc53ce53SDavid Howells 	return 0;
1198cc53ce53SDavid Howells }
1199cc53ce53SDavid Howells 
1200cc53ce53SDavid Howells /*
12019875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
12029875cf80SDavid Howells  */
12039875cf80SDavid Howells static void follow_mount(struct path *path)
12049875cf80SDavid Howells {
12059875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
12069875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
12079875cf80SDavid Howells 		if (!mounted)
12089875cf80SDavid Howells 			break;
12099875cf80SDavid Howells 		dput(path->dentry);
12109875cf80SDavid Howells 		mntput(path->mnt);
12119875cf80SDavid Howells 		path->mnt = mounted;
12129875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
12139875cf80SDavid Howells 	}
12149875cf80SDavid Howells }
12159875cf80SDavid Howells 
121631e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
12171da177e4SLinus Torvalds {
12182a737871SAl Viro 	set_root(nd);
1219e518ddb7SAndreas Mohr 
12201da177e4SLinus Torvalds 	while(1) {
12214ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
12221da177e4SLinus Torvalds 
12232a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
12242a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
12251da177e4SLinus Torvalds 			break;
12261da177e4SLinus Torvalds 		}
12274ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
12283088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
12293088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
12301da177e4SLinus Torvalds 			dput(old);
12311da177e4SLinus Torvalds 			break;
12321da177e4SLinus Torvalds 		}
12333088dd70SAl Viro 		if (!follow_up(&nd->path))
12341da177e4SLinus Torvalds 			break;
12351da177e4SLinus Torvalds 	}
123679ed0226SAl Viro 	follow_mount(&nd->path);
123731e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
12381da177e4SLinus Torvalds }
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds /*
1241bad61189SMiklos Szeredi  * This looks up the name in dcache, possibly revalidates the old dentry and
1242bad61189SMiklos Szeredi  * allocates a new one if not found or not valid.  In the need_lookup argument
1243bad61189SMiklos Szeredi  * returns whether i_op->lookup is necessary.
1244bad61189SMiklos Szeredi  *
1245bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
1246baa03890SNick Piggin  */
1247bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1248201f956eSAl Viro 				    unsigned int flags, bool *need_lookup)
1249baa03890SNick Piggin {
1250baa03890SNick Piggin 	struct dentry *dentry;
1251bad61189SMiklos Szeredi 	int error;
1252baa03890SNick Piggin 
1253bad61189SMiklos Szeredi 	*need_lookup = false;
1254bad61189SMiklos Szeredi 	dentry = d_lookup(dir, name);
1255bad61189SMiklos Szeredi 	if (dentry) {
125639e3c955SJeff Layton 		if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1257201f956eSAl Viro 			error = d_revalidate(dentry, flags);
1258bad61189SMiklos Szeredi 			if (unlikely(error <= 0)) {
1259bad61189SMiklos Szeredi 				if (error < 0) {
1260bad61189SMiklos Szeredi 					dput(dentry);
1261bad61189SMiklos Szeredi 					return ERR_PTR(error);
1262bad61189SMiklos Szeredi 				} else if (!d_invalidate(dentry)) {
1263bad61189SMiklos Szeredi 					dput(dentry);
1264bad61189SMiklos Szeredi 					dentry = NULL;
1265bad61189SMiklos Szeredi 				}
1266bad61189SMiklos Szeredi 			}
1267bad61189SMiklos Szeredi 		}
1268bad61189SMiklos Szeredi 	}
1269baa03890SNick Piggin 
1270bad61189SMiklos Szeredi 	if (!dentry) {
1271bad61189SMiklos Szeredi 		dentry = d_alloc(dir, name);
1272baa03890SNick Piggin 		if (unlikely(!dentry))
1273baa03890SNick Piggin 			return ERR_PTR(-ENOMEM);
1274baa03890SNick Piggin 
1275bad61189SMiklos Szeredi 		*need_lookup = true;
1276baa03890SNick Piggin 	}
1277baa03890SNick Piggin 	return dentry;
1278baa03890SNick Piggin }
1279baa03890SNick Piggin 
1280baa03890SNick Piggin /*
128113a2c3beSJ. Bruce Fields  * Call i_op->lookup on the dentry.  The dentry must be negative and
128213a2c3beSJ. Bruce Fields  * unhashed.
1283bad61189SMiklos Szeredi  *
1284bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
128544396f4bSJosef Bacik  */
1286bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
128772bd866aSAl Viro 				  unsigned int flags)
128844396f4bSJosef Bacik {
128944396f4bSJosef Bacik 	struct dentry *old;
129044396f4bSJosef Bacik 
129144396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
1292bad61189SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
1293e188dc02SMiklos Szeredi 		dput(dentry);
129444396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
1295e188dc02SMiklos Szeredi 	}
129644396f4bSJosef Bacik 
129772bd866aSAl Viro 	old = dir->i_op->lookup(dir, dentry, flags);
129844396f4bSJosef Bacik 	if (unlikely(old)) {
129944396f4bSJosef Bacik 		dput(dentry);
130044396f4bSJosef Bacik 		dentry = old;
130144396f4bSJosef Bacik 	}
130244396f4bSJosef Bacik 	return dentry;
130344396f4bSJosef Bacik }
130444396f4bSJosef Bacik 
1305a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name,
130672bd866aSAl Viro 		struct dentry *base, unsigned int flags)
1307a3255546SAl Viro {
1308bad61189SMiklos Szeredi 	bool need_lookup;
1309a3255546SAl Viro 	struct dentry *dentry;
1310a3255546SAl Viro 
131172bd866aSAl Viro 	dentry = lookup_dcache(name, base, flags, &need_lookup);
1312bad61189SMiklos Szeredi 	if (!need_lookup)
1313a3255546SAl Viro 		return dentry;
1314bad61189SMiklos Szeredi 
131572bd866aSAl Viro 	return lookup_real(base->d_inode, dentry, flags);
1316a3255546SAl Viro }
1317a3255546SAl Viro 
131844396f4bSJosef Bacik /*
13191da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
13201da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
13211da177e4SLinus Torvalds  *  It _is_ time-critical.
13221da177e4SLinus Torvalds  */
1323e97cdc87SAl Viro static int lookup_fast(struct nameidata *nd,
132431e6b01fSNick Piggin 		       struct path *path, struct inode **inode)
13251da177e4SLinus Torvalds {
13264ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
132731e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
13285a18fff2SAl Viro 	int need_reval = 1;
13295a18fff2SAl Viro 	int status = 1;
13309875cf80SDavid Howells 	int err;
13319875cf80SDavid Howells 
13323cac260aSAl Viro 	/*
1333b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1334b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1335b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1336b04f784eSNick Piggin 	 */
133731e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
133831e6b01fSNick Piggin 		unsigned seq;
1339da53be12SLinus Torvalds 		dentry = __d_lookup_rcu(parent, &nd->last, &seq);
13405a18fff2SAl Viro 		if (!dentry)
13415a18fff2SAl Viro 			goto unlazy;
13425a18fff2SAl Viro 
134312f8ad4bSLinus Torvalds 		/*
134412f8ad4bSLinus Torvalds 		 * This sequence count validates that the inode matches
134512f8ad4bSLinus Torvalds 		 * the dentry name information from lookup.
134612f8ad4bSLinus Torvalds 		 */
134712f8ad4bSLinus Torvalds 		*inode = dentry->d_inode;
134812f8ad4bSLinus Torvalds 		if (read_seqcount_retry(&dentry->d_seq, seq))
134912f8ad4bSLinus Torvalds 			return -ECHILD;
135012f8ad4bSLinus Torvalds 
135112f8ad4bSLinus Torvalds 		/*
135212f8ad4bSLinus Torvalds 		 * This sequence count validates that the parent had no
135312f8ad4bSLinus Torvalds 		 * changes while we did the lookup of the dentry above.
135412f8ad4bSLinus Torvalds 		 *
135512f8ad4bSLinus Torvalds 		 * The memory barrier in read_seqcount_begin of child is
135612f8ad4bSLinus Torvalds 		 *  enough, we can use __read_seqcount_retry here.
135712f8ad4bSLinus Torvalds 		 */
135831e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
135931e6b01fSNick Piggin 			return -ECHILD;
136031e6b01fSNick Piggin 		nd->seq = seq;
13615a18fff2SAl Viro 
136224643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
13634ce16ef3SAl Viro 			status = d_revalidate(dentry, nd->flags);
13645a18fff2SAl Viro 			if (unlikely(status <= 0)) {
13655a18fff2SAl Viro 				if (status != -ECHILD)
13665a18fff2SAl Viro 					need_reval = 0;
13675a18fff2SAl Viro 				goto unlazy;
13685a18fff2SAl Viro 			}
136924643087SAl Viro 		}
137031e6b01fSNick Piggin 		path->mnt = mnt;
137131e6b01fSNick Piggin 		path->dentry = dentry;
1372d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1373d6e9bd25SAl Viro 			goto unlazy;
1374d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1375d6e9bd25SAl Viro 			goto unlazy;
13769875cf80SDavid Howells 		return 0;
13775a18fff2SAl Viro unlazy:
137819660af7SAl Viro 		if (unlazy_walk(nd, dentry))
13795a18fff2SAl Viro 			return -ECHILD;
13805a18fff2SAl Viro 	} else {
1381e97cdc87SAl Viro 		dentry = __d_lookup(parent, &nd->last);
138224643087SAl Viro 	}
13835a18fff2SAl Viro 
138481e6f520SAl Viro 	if (unlikely(!dentry))
138581e6f520SAl Viro 		goto need_lookup;
13865a18fff2SAl Viro 
13875a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
13884ce16ef3SAl Viro 		status = d_revalidate(dentry, nd->flags);
13895a18fff2SAl Viro 	if (unlikely(status <= 0)) {
13905a18fff2SAl Viro 		if (status < 0) {
13915a18fff2SAl Viro 			dput(dentry);
13925a18fff2SAl Viro 			return status;
13935a18fff2SAl Viro 		}
13945a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
13955a18fff2SAl Viro 			dput(dentry);
139681e6f520SAl Viro 			goto need_lookup;
13975a18fff2SAl Viro 		}
13985a18fff2SAl Viro 	}
1399697f514dSMiklos Szeredi 
14001da177e4SLinus Torvalds 	path->mnt = mnt;
14011da177e4SLinus Torvalds 	path->dentry = dentry;
14029875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
140389312214SIan Kent 	if (unlikely(err < 0)) {
140489312214SIan Kent 		path_put_conditional(path, nd);
14059875cf80SDavid Howells 		return err;
140689312214SIan Kent 	}
1407a3fbbde7SAl Viro 	if (err)
1408a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
140931e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
14101da177e4SLinus Torvalds 	return 0;
141181e6f520SAl Viro 
141281e6f520SAl Viro need_lookup:
1413697f514dSMiklos Szeredi 	return 1;
1414697f514dSMiklos Szeredi }
1415697f514dSMiklos Szeredi 
1416697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */
1417cc2a5271SAl Viro static int lookup_slow(struct nameidata *nd, struct path *path)
1418697f514dSMiklos Szeredi {
1419697f514dSMiklos Szeredi 	struct dentry *dentry, *parent;
1420697f514dSMiklos Szeredi 	int err;
1421697f514dSMiklos Szeredi 
1422697f514dSMiklos Szeredi 	parent = nd->path.dentry;
142381e6f520SAl Viro 	BUG_ON(nd->inode != parent->d_inode);
142481e6f520SAl Viro 
142581e6f520SAl Viro 	mutex_lock(&parent->d_inode->i_mutex);
1426cc2a5271SAl Viro 	dentry = __lookup_hash(&nd->last, parent, nd->flags);
142781e6f520SAl Viro 	mutex_unlock(&parent->d_inode->i_mutex);
142881e6f520SAl Viro 	if (IS_ERR(dentry))
142981e6f520SAl Viro 		return PTR_ERR(dentry);
1430697f514dSMiklos Szeredi 	path->mnt = nd->path.mnt;
1431697f514dSMiklos Szeredi 	path->dentry = dentry;
1432697f514dSMiklos Szeredi 	err = follow_managed(path, nd->flags);
1433697f514dSMiklos Szeredi 	if (unlikely(err < 0)) {
1434697f514dSMiklos Szeredi 		path_put_conditional(path, nd);
1435697f514dSMiklos Szeredi 		return err;
1436697f514dSMiklos Szeredi 	}
1437697f514dSMiklos Szeredi 	if (err)
1438697f514dSMiklos Szeredi 		nd->flags |= LOOKUP_JUMPED;
1439697f514dSMiklos Szeredi 	return 0;
14401da177e4SLinus Torvalds }
14411da177e4SLinus Torvalds 
144252094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
144352094c8aSAl Viro {
144452094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
14454ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
144652094c8aSAl Viro 		if (err != -ECHILD)
144752094c8aSAl Viro 			return err;
144819660af7SAl Viro 		if (unlazy_walk(nd, NULL))
144952094c8aSAl Viro 			return -ECHILD;
145052094c8aSAl Viro 	}
14514ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
145252094c8aSAl Viro }
145352094c8aSAl Viro 
14549856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
14559856fa1bSAl Viro {
14569856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
14579856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
14589856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
14599856fa1bSAl Viro 				return -ECHILD;
14609856fa1bSAl Viro 		} else
14619856fa1bSAl Viro 			follow_dotdot(nd);
14629856fa1bSAl Viro 	}
14639856fa1bSAl Viro 	return 0;
14649856fa1bSAl Viro }
14659856fa1bSAl Viro 
1466951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1467951361f9SAl Viro {
1468951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1469951361f9SAl Viro 		path_put(&nd->path);
1470951361f9SAl Viro 	} else {
1471951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
14725b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1473951361f9SAl Viro 			nd->root.mnt = NULL;
14748b61e74fSAl Viro 		rcu_read_unlock();
1475951361f9SAl Viro 	}
1476951361f9SAl Viro }
1477951361f9SAl Viro 
14783ddcd056SLinus Torvalds /*
14793ddcd056SLinus Torvalds  * Do we need to follow links? We _really_ want to be able
14803ddcd056SLinus Torvalds  * to do this check without having to look at inode->i_op,
14813ddcd056SLinus Torvalds  * so we keep a cache of "no, this doesn't need follow_link"
14823ddcd056SLinus Torvalds  * for the common case.
14833ddcd056SLinus Torvalds  */
1484b18825a7SDavid Howells static inline int should_follow_link(struct dentry *dentry, int follow)
14853ddcd056SLinus Torvalds {
1486b18825a7SDavid Howells 	return unlikely(d_is_symlink(dentry)) ? follow : 0;
14873ddcd056SLinus Torvalds }
14883ddcd056SLinus Torvalds 
1489ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
149021b9b073SAl Viro 		int follow)
1491ce57dfc1SAl Viro {
1492ce57dfc1SAl Viro 	struct inode *inode;
1493ce57dfc1SAl Viro 	int err;
1494ce57dfc1SAl Viro 	/*
1495ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1496ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1497ce57dfc1SAl Viro 	 * parent relationships.
1498ce57dfc1SAl Viro 	 */
149921b9b073SAl Viro 	if (unlikely(nd->last_type != LAST_NORM))
150021b9b073SAl Viro 		return handle_dots(nd, nd->last_type);
1501e97cdc87SAl Viro 	err = lookup_fast(nd, path, &inode);
1502ce57dfc1SAl Viro 	if (unlikely(err)) {
1503697f514dSMiklos Szeredi 		if (err < 0)
1504697f514dSMiklos Szeredi 			goto out_err;
1505697f514dSMiklos Szeredi 
1506cc2a5271SAl Viro 		err = lookup_slow(nd, path);
1507697f514dSMiklos Szeredi 		if (err < 0)
1508697f514dSMiklos Szeredi 			goto out_err;
1509697f514dSMiklos Szeredi 
1510697f514dSMiklos Szeredi 		inode = path->dentry->d_inode;
1511ce57dfc1SAl Viro 	}
1512697f514dSMiklos Szeredi 	err = -ENOENT;
1513697f514dSMiklos Szeredi 	if (!inode)
1514697f514dSMiklos Szeredi 		goto out_path_put;
1515697f514dSMiklos Szeredi 
1516b18825a7SDavid Howells 	if (should_follow_link(path->dentry, follow)) {
151719660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
151819660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
1519697f514dSMiklos Szeredi 				err = -ECHILD;
1520697f514dSMiklos Szeredi 				goto out_err;
152119660af7SAl Viro 			}
152219660af7SAl Viro 		}
1523ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1524ce57dfc1SAl Viro 		return 1;
1525ce57dfc1SAl Viro 	}
1526ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1527ce57dfc1SAl Viro 	nd->inode = inode;
1528ce57dfc1SAl Viro 	return 0;
1529697f514dSMiklos Szeredi 
1530697f514dSMiklos Szeredi out_path_put:
1531697f514dSMiklos Szeredi 	path_to_nameidata(path, nd);
1532697f514dSMiklos Szeredi out_err:
1533697f514dSMiklos Szeredi 	terminate_walk(nd);
1534697f514dSMiklos Szeredi 	return err;
1535ce57dfc1SAl Viro }
1536ce57dfc1SAl Viro 
15371da177e4SLinus Torvalds /*
1538b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1539b356379aSAl Viro  * limiting consecutive symlinks to 40.
1540b356379aSAl Viro  *
1541b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1542b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1543b356379aSAl Viro  */
1544b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1545b356379aSAl Viro {
1546b356379aSAl Viro 	int res;
1547b356379aSAl Viro 
1548b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1549b356379aSAl Viro 		path_put_conditional(path, nd);
1550b356379aSAl Viro 		path_put(&nd->path);
1551b356379aSAl Viro 		return -ELOOP;
1552b356379aSAl Viro 	}
15531a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1554b356379aSAl Viro 
1555b356379aSAl Viro 	nd->depth++;
1556b356379aSAl Viro 	current->link_count++;
1557b356379aSAl Viro 
1558b356379aSAl Viro 	do {
1559b356379aSAl Viro 		struct path link = *path;
1560b356379aSAl Viro 		void *cookie;
1561574197e0SAl Viro 
1562574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
15636d7b5aaeSAl Viro 		if (res)
15646d7b5aaeSAl Viro 			break;
156521b9b073SAl Viro 		res = walk_component(nd, path, LOOKUP_FOLLOW);
1566574197e0SAl Viro 		put_link(nd, &link, cookie);
1567b356379aSAl Viro 	} while (res > 0);
1568b356379aSAl Viro 
1569b356379aSAl Viro 	current->link_count--;
1570b356379aSAl Viro 	nd->depth--;
1571b356379aSAl Viro 	return res;
1572b356379aSAl Viro }
1573b356379aSAl Viro 
1574b356379aSAl Viro /*
1575bfcfaa77SLinus Torvalds  * We can do the critical dentry name comparison and hashing
1576bfcfaa77SLinus Torvalds  * operations one word at a time, but we are limited to:
1577bfcfaa77SLinus Torvalds  *
1578bfcfaa77SLinus Torvalds  * - Architectures with fast unaligned word accesses. We could
1579bfcfaa77SLinus Torvalds  *   do a "get_unaligned()" if this helps and is sufficiently
1580bfcfaa77SLinus Torvalds  *   fast.
1581bfcfaa77SLinus Torvalds  *
1582bfcfaa77SLinus Torvalds  * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1583bfcfaa77SLinus Torvalds  *   do not trap on the (extremely unlikely) case of a page
1584bfcfaa77SLinus Torvalds  *   crossing operation.
1585bfcfaa77SLinus Torvalds  *
1586bfcfaa77SLinus Torvalds  * - Furthermore, we need an efficient 64-bit compile for the
1587bfcfaa77SLinus Torvalds  *   64-bit case in order to generate the "number of bytes in
1588bfcfaa77SLinus Torvalds  *   the final mask". Again, that could be replaced with a
1589bfcfaa77SLinus Torvalds  *   efficient population count instruction or similar.
1590bfcfaa77SLinus Torvalds  */
1591bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS
1592bfcfaa77SLinus Torvalds 
1593f68e556eSLinus Torvalds #include <asm/word-at-a-time.h>
1594bfcfaa77SLinus Torvalds 
1595f68e556eSLinus Torvalds #ifdef CONFIG_64BIT
1596bfcfaa77SLinus Torvalds 
1597bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash)
1598bfcfaa77SLinus Torvalds {
1599bfcfaa77SLinus Torvalds 	hash += hash >> (8*sizeof(int));
1600bfcfaa77SLinus Torvalds 	return hash;
1601bfcfaa77SLinus Torvalds }
1602bfcfaa77SLinus Torvalds 
1603bfcfaa77SLinus Torvalds #else	/* 32-bit case */
1604bfcfaa77SLinus Torvalds 
1605bfcfaa77SLinus Torvalds #define fold_hash(x) (x)
1606bfcfaa77SLinus Torvalds 
1607bfcfaa77SLinus Torvalds #endif
1608bfcfaa77SLinus Torvalds 
1609bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1610bfcfaa77SLinus Torvalds {
1611bfcfaa77SLinus Torvalds 	unsigned long a, mask;
1612bfcfaa77SLinus Torvalds 	unsigned long hash = 0;
1613bfcfaa77SLinus Torvalds 
1614bfcfaa77SLinus Torvalds 	for (;;) {
1615e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name);
1616bfcfaa77SLinus Torvalds 		if (len < sizeof(unsigned long))
1617bfcfaa77SLinus Torvalds 			break;
1618bfcfaa77SLinus Torvalds 		hash += a;
1619f132c5beSAl Viro 		hash *= 9;
1620bfcfaa77SLinus Torvalds 		name += sizeof(unsigned long);
1621bfcfaa77SLinus Torvalds 		len -= sizeof(unsigned long);
1622bfcfaa77SLinus Torvalds 		if (!len)
1623bfcfaa77SLinus Torvalds 			goto done;
1624bfcfaa77SLinus Torvalds 	}
1625a5c21dceSWill Deacon 	mask = bytemask_from_count(len);
1626bfcfaa77SLinus Torvalds 	hash += mask & a;
1627bfcfaa77SLinus Torvalds done:
1628bfcfaa77SLinus Torvalds 	return fold_hash(hash);
1629bfcfaa77SLinus Torvalds }
1630bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
1631bfcfaa77SLinus Torvalds 
1632bfcfaa77SLinus Torvalds /*
1633bfcfaa77SLinus Torvalds  * Calculate the length and hash of the path component, and
1634bfcfaa77SLinus Torvalds  * return the length of the component;
1635bfcfaa77SLinus Torvalds  */
1636bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1637bfcfaa77SLinus Torvalds {
163836126f8fSLinus Torvalds 	unsigned long a, b, adata, bdata, mask, hash, len;
163936126f8fSLinus Torvalds 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1640bfcfaa77SLinus Torvalds 
1641bfcfaa77SLinus Torvalds 	hash = a = 0;
1642bfcfaa77SLinus Torvalds 	len = -sizeof(unsigned long);
1643bfcfaa77SLinus Torvalds 	do {
1644bfcfaa77SLinus Torvalds 		hash = (hash + a) * 9;
1645bfcfaa77SLinus Torvalds 		len += sizeof(unsigned long);
1646e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name+len);
164736126f8fSLinus Torvalds 		b = a ^ REPEAT_BYTE('/');
164836126f8fSLinus Torvalds 	} while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1649bfcfaa77SLinus Torvalds 
165036126f8fSLinus Torvalds 	adata = prep_zero_mask(a, adata, &constants);
165136126f8fSLinus Torvalds 	bdata = prep_zero_mask(b, bdata, &constants);
165236126f8fSLinus Torvalds 
165336126f8fSLinus Torvalds 	mask = create_zero_mask(adata | bdata);
165436126f8fSLinus Torvalds 
165536126f8fSLinus Torvalds 	hash += a & zero_bytemask(mask);
1656bfcfaa77SLinus Torvalds 	*hashp = fold_hash(hash);
1657bfcfaa77SLinus Torvalds 
165836126f8fSLinus Torvalds 	return len + find_zero(mask);
1659bfcfaa77SLinus Torvalds }
1660bfcfaa77SLinus Torvalds 
1661bfcfaa77SLinus Torvalds #else
1662bfcfaa77SLinus Torvalds 
16630145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
16640145acc2SLinus Torvalds {
16650145acc2SLinus Torvalds 	unsigned long hash = init_name_hash();
16660145acc2SLinus Torvalds 	while (len--)
16670145acc2SLinus Torvalds 		hash = partial_name_hash(*name++, hash);
16680145acc2SLinus Torvalds 	return end_name_hash(hash);
16690145acc2SLinus Torvalds }
1670ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
16710145acc2SLinus Torvalds 
16723ddcd056SLinus Torvalds /*
1673200e9ef7SLinus Torvalds  * We know there's a real path component here of at least
1674200e9ef7SLinus Torvalds  * one character.
1675200e9ef7SLinus Torvalds  */
1676200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1677200e9ef7SLinus Torvalds {
1678200e9ef7SLinus Torvalds 	unsigned long hash = init_name_hash();
1679200e9ef7SLinus Torvalds 	unsigned long len = 0, c;
1680200e9ef7SLinus Torvalds 
1681200e9ef7SLinus Torvalds 	c = (unsigned char)*name;
1682200e9ef7SLinus Torvalds 	do {
1683200e9ef7SLinus Torvalds 		len++;
1684200e9ef7SLinus Torvalds 		hash = partial_name_hash(c, hash);
1685200e9ef7SLinus Torvalds 		c = (unsigned char)name[len];
1686200e9ef7SLinus Torvalds 	} while (c && c != '/');
1687200e9ef7SLinus Torvalds 	*hashp = end_name_hash(hash);
1688200e9ef7SLinus Torvalds 	return len;
1689200e9ef7SLinus Torvalds }
1690200e9ef7SLinus Torvalds 
1691bfcfaa77SLinus Torvalds #endif
1692bfcfaa77SLinus Torvalds 
1693200e9ef7SLinus Torvalds /*
16941da177e4SLinus Torvalds  * Name resolution.
1695ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1696ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
16971da177e4SLinus Torvalds  *
1698ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1699ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
17001da177e4SLinus Torvalds  */
17016de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
17021da177e4SLinus Torvalds {
17031da177e4SLinus Torvalds 	struct path next;
17041da177e4SLinus Torvalds 	int err;
17051da177e4SLinus Torvalds 
17061da177e4SLinus Torvalds 	while (*name=='/')
17071da177e4SLinus Torvalds 		name++;
17081da177e4SLinus Torvalds 	if (!*name)
1709086e183aSAl Viro 		return 0;
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
17121da177e4SLinus Torvalds 	for(;;) {
17131da177e4SLinus Torvalds 		struct qstr this;
1714200e9ef7SLinus Torvalds 		long len;
1715fe479a58SAl Viro 		int type;
17161da177e4SLinus Torvalds 
171752094c8aSAl Viro 		err = may_lookup(nd);
17181da177e4SLinus Torvalds  		if (err)
17191da177e4SLinus Torvalds 			break;
17201da177e4SLinus Torvalds 
1721200e9ef7SLinus Torvalds 		len = hash_name(name, &this.hash);
17221da177e4SLinus Torvalds 		this.name = name;
1723200e9ef7SLinus Torvalds 		this.len = len;
17241da177e4SLinus Torvalds 
1725fe479a58SAl Viro 		type = LAST_NORM;
1726200e9ef7SLinus Torvalds 		if (name[0] == '.') switch (len) {
1727fe479a58SAl Viro 			case 2:
1728200e9ef7SLinus Torvalds 				if (name[1] == '.') {
1729fe479a58SAl Viro 					type = LAST_DOTDOT;
173016c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
173116c2cd71SAl Viro 				}
1732fe479a58SAl Viro 				break;
1733fe479a58SAl Viro 			case 1:
1734fe479a58SAl Viro 				type = LAST_DOT;
1735fe479a58SAl Viro 		}
17365a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
17375a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
173816c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
17395a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1740da53be12SLinus Torvalds 				err = parent->d_op->d_hash(parent, &this);
17415a202bcdSAl Viro 				if (err < 0)
17425a202bcdSAl Viro 					break;
17435a202bcdSAl Viro 			}
17445a202bcdSAl Viro 		}
1745fe479a58SAl Viro 
17465f4a6a69SAl Viro 		nd->last = this;
17475f4a6a69SAl Viro 		nd->last_type = type;
17485f4a6a69SAl Viro 
1749200e9ef7SLinus Torvalds 		if (!name[len])
17505f4a6a69SAl Viro 			return 0;
1751200e9ef7SLinus Torvalds 		/*
1752200e9ef7SLinus Torvalds 		 * If it wasn't NUL, we know it was '/'. Skip that
1753200e9ef7SLinus Torvalds 		 * slash, and continue until no more slashes.
1754200e9ef7SLinus Torvalds 		 */
1755200e9ef7SLinus Torvalds 		do {
1756200e9ef7SLinus Torvalds 			len++;
1757200e9ef7SLinus Torvalds 		} while (unlikely(name[len] == '/'));
1758200e9ef7SLinus Torvalds 		if (!name[len])
17595f4a6a69SAl Viro 			return 0;
17605f4a6a69SAl Viro 
1761200e9ef7SLinus Torvalds 		name += len;
17621da177e4SLinus Torvalds 
176321b9b073SAl Viro 		err = walk_component(nd, &next, LOOKUP_FOLLOW);
1764ce57dfc1SAl Viro 		if (err < 0)
1765ce57dfc1SAl Viro 			return err;
1766fe479a58SAl Viro 
1767ce57dfc1SAl Viro 		if (err) {
1768b356379aSAl Viro 			err = nested_symlink(&next, nd);
17691da177e4SLinus Torvalds 			if (err)
1770a7472babSAl Viro 				return err;
177131e6b01fSNick Piggin 		}
1772b18825a7SDavid Howells 		if (!d_is_directory(nd->path.dentry)) {
17733ddcd056SLinus Torvalds 			err = -ENOTDIR;
17743ddcd056SLinus Torvalds 			break;
17755f4a6a69SAl Viro 		}
1776ce57dfc1SAl Viro 	}
1777951361f9SAl Viro 	terminate_walk(nd);
17781da177e4SLinus Torvalds 	return err;
17791da177e4SLinus Torvalds }
17801da177e4SLinus Torvalds 
178170e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
178270e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
178331e6b01fSNick Piggin {
178431e6b01fSNick Piggin 	int retval = 0;
178531e6b01fSNick Piggin 
178631e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
178716c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
178831e6b01fSNick Piggin 	nd->depth = 0;
17895b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
1790b18825a7SDavid Howells 		struct dentry *root = nd->root.dentry;
1791b18825a7SDavid Howells 		struct inode *inode = root->d_inode;
179273d049a4SAl Viro 		if (*name) {
1793b18825a7SDavid Howells 			if (!d_is_directory(root))
17945b6ca027SAl Viro 				return -ENOTDIR;
17955b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
17965b6ca027SAl Viro 			if (retval)
17975b6ca027SAl Viro 				return retval;
179873d049a4SAl Viro 		}
17995b6ca027SAl Viro 		nd->path = nd->root;
18005b6ca027SAl Viro 		nd->inode = inode;
18015b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
18028b61e74fSAl Viro 			rcu_read_lock();
18035b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
180448a066e7SAl Viro 			nd->m_seq = read_seqbegin(&mount_lock);
18055b6ca027SAl Viro 		} else {
18065b6ca027SAl Viro 			path_get(&nd->path);
18075b6ca027SAl Viro 		}
18085b6ca027SAl Viro 		return 0;
18095b6ca027SAl Viro 	}
18105b6ca027SAl Viro 
181131e6b01fSNick Piggin 	nd->root.mnt = NULL;
181231e6b01fSNick Piggin 
181348a066e7SAl Viro 	nd->m_seq = read_seqbegin(&mount_lock);
181431e6b01fSNick Piggin 	if (*name=='/') {
1815e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
18168b61e74fSAl Viro 			rcu_read_lock();
1817e41f7d4eSAl Viro 			set_root_rcu(nd);
1818e41f7d4eSAl Viro 		} else {
1819e41f7d4eSAl Viro 			set_root(nd);
1820e41f7d4eSAl Viro 			path_get(&nd->root);
1821e41f7d4eSAl Viro 		}
182231e6b01fSNick Piggin 		nd->path = nd->root;
182331e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1824e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
182531e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1826c28cc364SNick Piggin 			unsigned seq;
182731e6b01fSNick Piggin 
18288b61e74fSAl Viro 			rcu_read_lock();
182931e6b01fSNick Piggin 
1830c28cc364SNick Piggin 			do {
1831c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
183231e6b01fSNick Piggin 				nd->path = fs->pwd;
1833c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1834c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1835e41f7d4eSAl Viro 		} else {
1836e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1837e41f7d4eSAl Viro 		}
183831e6b01fSNick Piggin 	} else {
1839582aa64aSJeff Layton 		/* Caller must check execute permissions on the starting path component */
18402903ff01SAl Viro 		struct fd f = fdget_raw(dfd);
184131e6b01fSNick Piggin 		struct dentry *dentry;
184231e6b01fSNick Piggin 
18432903ff01SAl Viro 		if (!f.file)
18442903ff01SAl Viro 			return -EBADF;
184531e6b01fSNick Piggin 
18462903ff01SAl Viro 		dentry = f.file->f_path.dentry;
184731e6b01fSNick Piggin 
1848f52e0c11SAl Viro 		if (*name) {
1849b18825a7SDavid Howells 			if (!d_is_directory(dentry)) {
18502903ff01SAl Viro 				fdput(f);
18512903ff01SAl Viro 				return -ENOTDIR;
1852f52e0c11SAl Viro 			}
18532903ff01SAl Viro 		}
18542903ff01SAl Viro 
18552903ff01SAl Viro 		nd->path = f.file->f_path;
1856e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
18572903ff01SAl Viro 			if (f.need_put)
18582903ff01SAl Viro 				*fp = f.file;
1859c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
18608b61e74fSAl Viro 			rcu_read_lock();
18615590ff0dSUlrich Drepper 		} else {
18622903ff01SAl Viro 			path_get(&nd->path);
18632903ff01SAl Viro 			fdput(f);
18641da177e4SLinus Torvalds 		}
1865e41f7d4eSAl Viro 	}
1866e41f7d4eSAl Viro 
186731e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
18689b4a9b14SAl Viro 	return 0;
18699b4a9b14SAl Viro }
18709b4a9b14SAl Viro 
1871bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1872bd92d7feSAl Viro {
1873bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1874bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1875bd92d7feSAl Viro 
1876bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
187721b9b073SAl Viro 	return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
1878bd92d7feSAl Viro }
1879bd92d7feSAl Viro 
18809b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1881ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
18829b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
18839b4a9b14SAl Viro {
188470e9b357SAl Viro 	struct file *base = NULL;
1885bd92d7feSAl Viro 	struct path path;
1886bd92d7feSAl Viro 	int err;
188731e6b01fSNick Piggin 
188831e6b01fSNick Piggin 	/*
188931e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
189031e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
189131e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
189231e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
189331e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
189431e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
189531e6b01fSNick Piggin 	 * analogue, foo_rcu().
189631e6b01fSNick Piggin 	 *
189731e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
189831e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
189931e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
190031e6b01fSNick Piggin 	 * be able to complete).
190131e6b01fSNick Piggin 	 */
1902bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1903ee0827cdSAl Viro 
1904bd92d7feSAl Viro 	if (unlikely(err))
1905bd92d7feSAl Viro 		return err;
1906ee0827cdSAl Viro 
1907ee0827cdSAl Viro 	current->total_link_count = 0;
1908bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1909bd92d7feSAl Viro 
1910bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1911bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1912bd92d7feSAl Viro 		while (err > 0) {
1913bd92d7feSAl Viro 			void *cookie;
1914bd92d7feSAl Viro 			struct path link = path;
1915800179c9SKees Cook 			err = may_follow_link(&link, nd);
1916800179c9SKees Cook 			if (unlikely(err))
1917800179c9SKees Cook 				break;
1918bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1919574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
19206d7b5aaeSAl Viro 			if (err)
19216d7b5aaeSAl Viro 				break;
1922bd92d7feSAl Viro 			err = lookup_last(nd, &path);
1923574197e0SAl Viro 			put_link(nd, &link, cookie);
1924bd92d7feSAl Viro 		}
1925bd92d7feSAl Viro 	}
1926ee0827cdSAl Viro 
19279f1fafeeSAl Viro 	if (!err)
19289f1fafeeSAl Viro 		err = complete_walk(nd);
1929bd92d7feSAl Viro 
1930bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1931b18825a7SDavid Howells 		if (!d_is_directory(nd->path.dentry)) {
1932bd92d7feSAl Viro 			path_put(&nd->path);
1933bd23a539SAl Viro 			err = -ENOTDIR;
1934bd92d7feSAl Viro 		}
1935bd92d7feSAl Viro 	}
193616c2cd71SAl Viro 
193770e9b357SAl Viro 	if (base)
193870e9b357SAl Viro 		fput(base);
1939ee0827cdSAl Viro 
19405b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
194131e6b01fSNick Piggin 		path_put(&nd->root);
194231e6b01fSNick Piggin 		nd->root.mnt = NULL;
194331e6b01fSNick Piggin 	}
1944bd92d7feSAl Viro 	return err;
194531e6b01fSNick Piggin }
194631e6b01fSNick Piggin 
1947873f1eedSJeff Layton static int filename_lookup(int dfd, struct filename *name,
1948873f1eedSJeff Layton 				unsigned int flags, struct nameidata *nd)
1949873f1eedSJeff Layton {
1950873f1eedSJeff Layton 	int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
1951873f1eedSJeff Layton 	if (unlikely(retval == -ECHILD))
1952873f1eedSJeff Layton 		retval = path_lookupat(dfd, name->name, flags, nd);
1953873f1eedSJeff Layton 	if (unlikely(retval == -ESTALE))
1954873f1eedSJeff Layton 		retval = path_lookupat(dfd, name->name,
1955873f1eedSJeff Layton 						flags | LOOKUP_REVAL, nd);
1956873f1eedSJeff Layton 
1957873f1eedSJeff Layton 	if (likely(!retval))
1958adb5c247SJeff Layton 		audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
1959873f1eedSJeff Layton 	return retval;
1960873f1eedSJeff Layton }
1961873f1eedSJeff Layton 
1962ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1963ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1964ee0827cdSAl Viro {
1965873f1eedSJeff Layton 	struct filename filename = { .name = name };
1966ee0827cdSAl Viro 
1967873f1eedSJeff Layton 	return filename_lookup(dfd, &filename, flags, nd);
19681da177e4SLinus Torvalds }
19691da177e4SLinus Torvalds 
197079714f72SAl Viro /* does lookup, returns the object with parent locked */
197179714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path)
19725590ff0dSUlrich Drepper {
197379714f72SAl Viro 	struct nameidata nd;
197479714f72SAl Viro 	struct dentry *d;
197579714f72SAl Viro 	int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
197679714f72SAl Viro 	if (err)
197779714f72SAl Viro 		return ERR_PTR(err);
197879714f72SAl Viro 	if (nd.last_type != LAST_NORM) {
197979714f72SAl Viro 		path_put(&nd.path);
198079714f72SAl Viro 		return ERR_PTR(-EINVAL);
198179714f72SAl Viro 	}
198279714f72SAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
19831e0ea001SAl Viro 	d = __lookup_hash(&nd.last, nd.path.dentry, 0);
198479714f72SAl Viro 	if (IS_ERR(d)) {
198579714f72SAl Viro 		mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
198679714f72SAl Viro 		path_put(&nd.path);
198779714f72SAl Viro 		return d;
198879714f72SAl Viro 	}
198979714f72SAl Viro 	*path = nd.path;
199079714f72SAl Viro 	return d;
19915590ff0dSUlrich Drepper }
19925590ff0dSUlrich Drepper 
1993d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1994d1811465SAl Viro {
1995d1811465SAl Viro 	struct nameidata nd;
1996d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1997d1811465SAl Viro 	if (!res)
1998d1811465SAl Viro 		*path = nd.path;
1999d1811465SAl Viro 	return res;
2000d1811465SAl Viro }
2001d1811465SAl Viro 
200216f18200SJosef 'Jeff' Sipek /**
200316f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
200416f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
200516f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
200616f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
200716f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
2008e0a01249SAl Viro  * @path: pointer to struct path to fill
200916f18200SJosef 'Jeff' Sipek  */
201016f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
201116f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
2012e0a01249SAl Viro 		    struct path *path)
201316f18200SJosef 'Jeff' Sipek {
2014e0a01249SAl Viro 	struct nameidata nd;
2015e0a01249SAl Viro 	int err;
2016e0a01249SAl Viro 	nd.root.dentry = dentry;
2017e0a01249SAl Viro 	nd.root.mnt = mnt;
2018e0a01249SAl Viro 	BUG_ON(flags & LOOKUP_PARENT);
20195b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
2020e0a01249SAl Viro 	err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2021e0a01249SAl Viro 	if (!err)
2022e0a01249SAl Viro 		*path = nd.path;
2023e0a01249SAl Viro 	return err;
202416f18200SJosef 'Jeff' Sipek }
202516f18200SJosef 'Jeff' Sipek 
2026057f6c01SJames Morris /*
2027057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
2028057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
2029057f6c01SJames Morris  * SMP-safe.
2030057f6c01SJames Morris  */
2031a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
20321da177e4SLinus Torvalds {
203372bd866aSAl Viro 	return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
20341da177e4SLinus Torvalds }
20351da177e4SLinus Torvalds 
2036eead1911SChristoph Hellwig /**
2037a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
2038eead1911SChristoph Hellwig  * @name:	pathname component to lookup
2039eead1911SChristoph Hellwig  * @base:	base directory to lookup from
2040eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
2041eead1911SChristoph Hellwig  *
2042a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
2043a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
2044eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
2045eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
2046eead1911SChristoph Hellwig  */
2047057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2048057f6c01SJames Morris {
2049057f6c01SJames Morris 	struct qstr this;
20506a96ba54SAl Viro 	unsigned int c;
2051cda309deSMiklos Szeredi 	int err;
2052057f6c01SJames Morris 
20532f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
20542f9092e1SDavid Woodhouse 
20556a96ba54SAl Viro 	this.name = name;
20566a96ba54SAl Viro 	this.len = len;
20570145acc2SLinus Torvalds 	this.hash = full_name_hash(name, len);
20586a96ba54SAl Viro 	if (!len)
20596a96ba54SAl Viro 		return ERR_PTR(-EACCES);
20606a96ba54SAl Viro 
206121d8a15aSAl Viro 	if (unlikely(name[0] == '.')) {
206221d8a15aSAl Viro 		if (len < 2 || (len == 2 && name[1] == '.'))
206321d8a15aSAl Viro 			return ERR_PTR(-EACCES);
206421d8a15aSAl Viro 	}
206521d8a15aSAl Viro 
20666a96ba54SAl Viro 	while (len--) {
20676a96ba54SAl Viro 		c = *(const unsigned char *)name++;
20686a96ba54SAl Viro 		if (c == '/' || c == '\0')
20696a96ba54SAl Viro 			return ERR_PTR(-EACCES);
20706a96ba54SAl Viro 	}
20715a202bcdSAl Viro 	/*
20725a202bcdSAl Viro 	 * See if the low-level filesystem might want
20735a202bcdSAl Viro 	 * to use its own hash..
20745a202bcdSAl Viro 	 */
20755a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
2076da53be12SLinus Torvalds 		int err = base->d_op->d_hash(base, &this);
20775a202bcdSAl Viro 		if (err < 0)
20785a202bcdSAl Viro 			return ERR_PTR(err);
20795a202bcdSAl Viro 	}
2080eead1911SChristoph Hellwig 
2081cda309deSMiklos Szeredi 	err = inode_permission(base->d_inode, MAY_EXEC);
2082cda309deSMiklos Szeredi 	if (err)
2083cda309deSMiklos Szeredi 		return ERR_PTR(err);
2084cda309deSMiklos Szeredi 
208572bd866aSAl Viro 	return __lookup_hash(&this, base, 0);
2086057f6c01SJames Morris }
2087057f6c01SJames Morris 
20881fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
20891fa1e7f6SAndy Whitcroft 		 struct path *path, int *empty)
20901da177e4SLinus Torvalds {
20912d8f3038SAl Viro 	struct nameidata nd;
209291a27b2aSJeff Layton 	struct filename *tmp = getname_flags(name, flags, empty);
20931da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
20941da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
20952d8f3038SAl Viro 
20962d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
20972d8f3038SAl Viro 
2098873f1eedSJeff Layton 		err = filename_lookup(dfd, tmp, flags, &nd);
20991da177e4SLinus Torvalds 		putname(tmp);
21002d8f3038SAl Viro 		if (!err)
21012d8f3038SAl Viro 			*path = nd.path;
21021da177e4SLinus Torvalds 	}
21031da177e4SLinus Torvalds 	return err;
21041da177e4SLinus Torvalds }
21051da177e4SLinus Torvalds 
21061fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags,
21071fa1e7f6SAndy Whitcroft 		 struct path *path)
21081fa1e7f6SAndy Whitcroft {
2109f7493e5dSLinus Torvalds 	return user_path_at_empty(dfd, name, flags, path, NULL);
21101fa1e7f6SAndy Whitcroft }
21111fa1e7f6SAndy Whitcroft 
2112873f1eedSJeff Layton /*
2113873f1eedSJeff Layton  * NB: most callers don't do anything directly with the reference to the
2114873f1eedSJeff Layton  *     to struct filename, but the nd->last pointer points into the name string
2115873f1eedSJeff Layton  *     allocated by getname. So we must hold the reference to it until all
2116873f1eedSJeff Layton  *     path-walking is complete.
2117873f1eedSJeff Layton  */
211891a27b2aSJeff Layton static struct filename *
21199e790bd6SJeff Layton user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
21209e790bd6SJeff Layton 		 unsigned int flags)
21212ad94ae6SAl Viro {
212291a27b2aSJeff Layton 	struct filename *s = getname(path);
21232ad94ae6SAl Viro 	int error;
21242ad94ae6SAl Viro 
21259e790bd6SJeff Layton 	/* only LOOKUP_REVAL is allowed in extra flags */
21269e790bd6SJeff Layton 	flags &= LOOKUP_REVAL;
21279e790bd6SJeff Layton 
21282ad94ae6SAl Viro 	if (IS_ERR(s))
212991a27b2aSJeff Layton 		return s;
21302ad94ae6SAl Viro 
21319e790bd6SJeff Layton 	error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
213291a27b2aSJeff Layton 	if (error) {
21332ad94ae6SAl Viro 		putname(s);
213491a27b2aSJeff Layton 		return ERR_PTR(error);
213591a27b2aSJeff Layton 	}
21362ad94ae6SAl Viro 
213791a27b2aSJeff Layton 	return s;
21382ad94ae6SAl Viro }
21392ad94ae6SAl Viro 
21408033426eSJeff Layton /**
2141197df04cSAl Viro  * mountpoint_last - look up last component for umount
21428033426eSJeff Layton  * @nd:   pathwalk nameidata - currently pointing at parent directory of "last"
21438033426eSJeff Layton  * @path: pointer to container for result
21448033426eSJeff Layton  *
21458033426eSJeff Layton  * This is a special lookup_last function just for umount. In this case, we
21468033426eSJeff Layton  * need to resolve the path without doing any revalidation.
21478033426eSJeff Layton  *
21488033426eSJeff Layton  * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
21498033426eSJeff Layton  * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
21508033426eSJeff Layton  * in almost all cases, this lookup will be served out of the dcache. The only
21518033426eSJeff Layton  * cases where it won't are if nd->last refers to a symlink or the path is
21528033426eSJeff Layton  * bogus and it doesn't exist.
21538033426eSJeff Layton  *
21548033426eSJeff Layton  * Returns:
21558033426eSJeff Layton  * -error: if there was an error during lookup. This includes -ENOENT if the
21568033426eSJeff Layton  *         lookup found a negative dentry. The nd->path reference will also be
21578033426eSJeff Layton  *         put in this case.
21588033426eSJeff Layton  *
21598033426eSJeff Layton  * 0:      if we successfully resolved nd->path and found it to not to be a
21608033426eSJeff Layton  *         symlink that needs to be followed. "path" will also be populated.
21618033426eSJeff Layton  *         The nd->path reference will also be put.
21628033426eSJeff Layton  *
21638033426eSJeff Layton  * 1:      if we successfully resolved nd->last and found it to be a symlink
21648033426eSJeff Layton  *         that needs to be followed. "path" will be populated with the path
21658033426eSJeff Layton  *         to the link, and nd->path will *not* be put.
21668033426eSJeff Layton  */
21678033426eSJeff Layton static int
2168197df04cSAl Viro mountpoint_last(struct nameidata *nd, struct path *path)
21698033426eSJeff Layton {
21708033426eSJeff Layton 	int error = 0;
21718033426eSJeff Layton 	struct dentry *dentry;
21728033426eSJeff Layton 	struct dentry *dir = nd->path.dentry;
21738033426eSJeff Layton 
217435759521SAl Viro 	/* If we're in rcuwalk, drop out of it to handle last component */
217535759521SAl Viro 	if (nd->flags & LOOKUP_RCU) {
217635759521SAl Viro 		if (unlazy_walk(nd, NULL)) {
21778033426eSJeff Layton 			error = -ECHILD;
217835759521SAl Viro 			goto out;
217935759521SAl Viro 		}
21808033426eSJeff Layton 	}
21818033426eSJeff Layton 
21828033426eSJeff Layton 	nd->flags &= ~LOOKUP_PARENT;
21838033426eSJeff Layton 
21848033426eSJeff Layton 	if (unlikely(nd->last_type != LAST_NORM)) {
21858033426eSJeff Layton 		error = handle_dots(nd, nd->last_type);
218635759521SAl Viro 		if (error)
218735759521SAl Viro 			goto out;
21888033426eSJeff Layton 		dentry = dget(nd->path.dentry);
218935759521SAl Viro 		goto done;
21908033426eSJeff Layton 	}
21918033426eSJeff Layton 
21928033426eSJeff Layton 	mutex_lock(&dir->d_inode->i_mutex);
21938033426eSJeff Layton 	dentry = d_lookup(dir, &nd->last);
21948033426eSJeff Layton 	if (!dentry) {
21958033426eSJeff Layton 		/*
21968033426eSJeff Layton 		 * No cached dentry. Mounted dentries are pinned in the cache,
21978033426eSJeff Layton 		 * so that means that this dentry is probably a symlink or the
21988033426eSJeff Layton 		 * path doesn't actually point to a mounted dentry.
21998033426eSJeff Layton 		 */
22008033426eSJeff Layton 		dentry = d_alloc(dir, &nd->last);
22018033426eSJeff Layton 		if (!dentry) {
22028033426eSJeff Layton 			error = -ENOMEM;
2203bcceeebaSDave Jones 			mutex_unlock(&dir->d_inode->i_mutex);
220435759521SAl Viro 			goto out;
22058033426eSJeff Layton 		}
220635759521SAl Viro 		dentry = lookup_real(dir->d_inode, dentry, nd->flags);
220735759521SAl Viro 		error = PTR_ERR(dentry);
2208bcceeebaSDave Jones 		if (IS_ERR(dentry)) {
2209bcceeebaSDave Jones 			mutex_unlock(&dir->d_inode->i_mutex);
221035759521SAl Viro 			goto out;
22118033426eSJeff Layton 		}
2212bcceeebaSDave Jones 	}
22138033426eSJeff Layton 	mutex_unlock(&dir->d_inode->i_mutex);
22148033426eSJeff Layton 
221535759521SAl Viro done:
22168033426eSJeff Layton 	if (!dentry->d_inode) {
22178033426eSJeff Layton 		error = -ENOENT;
22188033426eSJeff Layton 		dput(dentry);
221935759521SAl Viro 		goto out;
222035759521SAl Viro 	}
22218033426eSJeff Layton 	path->dentry = dentry;
22228033426eSJeff Layton 	path->mnt = mntget(nd->path.mnt);
2223b18825a7SDavid Howells 	if (should_follow_link(dentry, nd->flags & LOOKUP_FOLLOW))
22248033426eSJeff Layton 		return 1;
22258033426eSJeff Layton 	follow_mount(path);
222635759521SAl Viro 	error = 0;
222735759521SAl Viro out:
22288033426eSJeff Layton 	terminate_walk(nd);
22298033426eSJeff Layton 	return error;
22308033426eSJeff Layton }
22318033426eSJeff Layton 
22328033426eSJeff Layton /**
2233197df04cSAl Viro  * path_mountpoint - look up a path to be umounted
22348033426eSJeff Layton  * @dfd:	directory file descriptor to start walk from
22358033426eSJeff Layton  * @name:	full pathname to walk
2236606d6fe3SRandy Dunlap  * @path:	pointer to container for result
22378033426eSJeff Layton  * @flags:	lookup flags
22388033426eSJeff Layton  *
22398033426eSJeff Layton  * Look up the given name, but don't attempt to revalidate the last component.
2240606d6fe3SRandy Dunlap  * Returns 0 and "path" will be valid on success; Returns error otherwise.
22418033426eSJeff Layton  */
22428033426eSJeff Layton static int
2243197df04cSAl Viro path_mountpoint(int dfd, const char *name, struct path *path, unsigned int flags)
22448033426eSJeff Layton {
22458033426eSJeff Layton 	struct file *base = NULL;
22468033426eSJeff Layton 	struct nameidata nd;
22478033426eSJeff Layton 	int err;
22488033426eSJeff Layton 
22498033426eSJeff Layton 	err = path_init(dfd, name, flags | LOOKUP_PARENT, &nd, &base);
22508033426eSJeff Layton 	if (unlikely(err))
22518033426eSJeff Layton 		return err;
22528033426eSJeff Layton 
22538033426eSJeff Layton 	current->total_link_count = 0;
22548033426eSJeff Layton 	err = link_path_walk(name, &nd);
22558033426eSJeff Layton 	if (err)
22568033426eSJeff Layton 		goto out;
22578033426eSJeff Layton 
2258197df04cSAl Viro 	err = mountpoint_last(&nd, path);
22598033426eSJeff Layton 	while (err > 0) {
22608033426eSJeff Layton 		void *cookie;
22618033426eSJeff Layton 		struct path link = *path;
22628033426eSJeff Layton 		err = may_follow_link(&link, &nd);
22638033426eSJeff Layton 		if (unlikely(err))
22648033426eSJeff Layton 			break;
22658033426eSJeff Layton 		nd.flags |= LOOKUP_PARENT;
22668033426eSJeff Layton 		err = follow_link(&link, &nd, &cookie);
22678033426eSJeff Layton 		if (err)
22688033426eSJeff Layton 			break;
2269197df04cSAl Viro 		err = mountpoint_last(&nd, path);
22708033426eSJeff Layton 		put_link(&nd, &link, cookie);
22718033426eSJeff Layton 	}
22728033426eSJeff Layton out:
22738033426eSJeff Layton 	if (base)
22748033426eSJeff Layton 		fput(base);
22758033426eSJeff Layton 
22768033426eSJeff Layton 	if (nd.root.mnt && !(nd.flags & LOOKUP_ROOT))
22778033426eSJeff Layton 		path_put(&nd.root);
22788033426eSJeff Layton 
22798033426eSJeff Layton 	return err;
22808033426eSJeff Layton }
22818033426eSJeff Layton 
22822d864651SAl Viro static int
22832d864651SAl Viro filename_mountpoint(int dfd, struct filename *s, struct path *path,
22842d864651SAl Viro 			unsigned int flags)
22852d864651SAl Viro {
22862d864651SAl Viro 	int error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_RCU);
22872d864651SAl Viro 	if (unlikely(error == -ECHILD))
22882d864651SAl Viro 		error = path_mountpoint(dfd, s->name, path, flags);
22892d864651SAl Viro 	if (unlikely(error == -ESTALE))
22902d864651SAl Viro 		error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_REVAL);
22912d864651SAl Viro 	if (likely(!error))
22922d864651SAl Viro 		audit_inode(s, path->dentry, 0);
22932d864651SAl Viro 	return error;
22942d864651SAl Viro }
22952d864651SAl Viro 
22968033426eSJeff Layton /**
2297197df04cSAl Viro  * user_path_mountpoint_at - lookup a path from userland in order to umount it
22988033426eSJeff Layton  * @dfd:	directory file descriptor
22998033426eSJeff Layton  * @name:	pathname from userland
23008033426eSJeff Layton  * @flags:	lookup flags
23018033426eSJeff Layton  * @path:	pointer to container to hold result
23028033426eSJeff Layton  *
23038033426eSJeff Layton  * A umount is a special case for path walking. We're not actually interested
23048033426eSJeff Layton  * in the inode in this situation, and ESTALE errors can be a problem. We
23058033426eSJeff Layton  * simply want track down the dentry and vfsmount attached at the mountpoint
23068033426eSJeff Layton  * and avoid revalidating the last component.
23078033426eSJeff Layton  *
23088033426eSJeff Layton  * Returns 0 and populates "path" on success.
23098033426eSJeff Layton  */
23108033426eSJeff Layton int
2311197df04cSAl Viro user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
23128033426eSJeff Layton 			struct path *path)
23138033426eSJeff Layton {
23148033426eSJeff Layton 	struct filename *s = getname(name);
23158033426eSJeff Layton 	int error;
23168033426eSJeff Layton 	if (IS_ERR(s))
23178033426eSJeff Layton 		return PTR_ERR(s);
23182d864651SAl Viro 	error = filename_mountpoint(dfd, s, path, flags);
23198033426eSJeff Layton 	putname(s);
23208033426eSJeff Layton 	return error;
23218033426eSJeff Layton }
23228033426eSJeff Layton 
23232d864651SAl Viro int
23242d864651SAl Viro kern_path_mountpoint(int dfd, const char *name, struct path *path,
23252d864651SAl Viro 			unsigned int flags)
23262d864651SAl Viro {
23272d864651SAl Viro 	struct filename s = {.name = name};
23282d864651SAl Viro 	return filename_mountpoint(dfd, &s, path, flags);
23292d864651SAl Viro }
23302d864651SAl Viro EXPORT_SYMBOL(kern_path_mountpoint);
23312d864651SAl Viro 
23321da177e4SLinus Torvalds /*
23331da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
23341da177e4SLinus Torvalds  * minimal.
23351da177e4SLinus Torvalds  */
23361da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
23371da177e4SLinus Torvalds {
23388e96e3b7SEric W. Biederman 	kuid_t fsuid = current_fsuid();
2339da9592edSDavid Howells 
23401da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
23411da177e4SLinus Torvalds 		return 0;
23428e96e3b7SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
23431da177e4SLinus Torvalds 		return 0;
23448e96e3b7SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
23451da177e4SLinus Torvalds 		return 0;
23461a48e2acSEric W. Biederman 	return !inode_capable(inode, CAP_FOWNER);
23471da177e4SLinus Torvalds }
23481da177e4SLinus Torvalds 
23491da177e4SLinus Torvalds /*
23501da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
23511da177e4SLinus Torvalds  *  whether the type of victim is right.
23521da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
23531da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
23541da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
23551da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
23561da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
23571da177e4SLinus Torvalds  *	a. be owner of dir, or
23581da177e4SLinus Torvalds  *	b. be owner of victim, or
23591da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
23601da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
23611da177e4SLinus Torvalds  *     links pointing to it.
23621da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
23631da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
23641da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
23651da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
23661da177e4SLinus Torvalds  *     nfs_async_unlink().
23671da177e4SLinus Torvalds  */
2368b18825a7SDavid Howells static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
23691da177e4SLinus Torvalds {
2370b18825a7SDavid Howells 	struct inode *inode = victim->d_inode;
23711da177e4SLinus Torvalds 	int error;
23721da177e4SLinus Torvalds 
2373b18825a7SDavid Howells 	if (d_is_negative(victim))
23741da177e4SLinus Torvalds 		return -ENOENT;
2375b18825a7SDavid Howells 	BUG_ON(!inode);
23761da177e4SLinus Torvalds 
23771da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
23784fa6b5ecSJeff Layton 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
23791da177e4SLinus Torvalds 
2380f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
23811da177e4SLinus Torvalds 	if (error)
23821da177e4SLinus Torvalds 		return error;
23831da177e4SLinus Torvalds 	if (IS_APPEND(dir))
23841da177e4SLinus Torvalds 		return -EPERM;
2385b18825a7SDavid Howells 
2386b18825a7SDavid Howells 	if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2387b18825a7SDavid Howells 	    IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
23881da177e4SLinus Torvalds 		return -EPERM;
23891da177e4SLinus Torvalds 	if (isdir) {
2390b18825a7SDavid Howells 		if (!d_is_directory(victim) && !d_is_autodir(victim))
23911da177e4SLinus Torvalds 			return -ENOTDIR;
23921da177e4SLinus Torvalds 		if (IS_ROOT(victim))
23931da177e4SLinus Torvalds 			return -EBUSY;
2394b18825a7SDavid Howells 	} else if (d_is_directory(victim) || d_is_autodir(victim))
23951da177e4SLinus Torvalds 		return -EISDIR;
23961da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
23971da177e4SLinus Torvalds 		return -ENOENT;
23981da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
23991da177e4SLinus Torvalds 		return -EBUSY;
24001da177e4SLinus Torvalds 	return 0;
24011da177e4SLinus Torvalds }
24021da177e4SLinus Torvalds 
24031da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
24041da177e4SLinus Torvalds  *  dir.
24051da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
24061da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
24071da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
24081da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
24091da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
24101da177e4SLinus Torvalds  */
2411a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
24121da177e4SLinus Torvalds {
241314e972b4SJeff Layton 	audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
24141da177e4SLinus Torvalds 	if (child->d_inode)
24151da177e4SLinus Torvalds 		return -EEXIST;
24161da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
24171da177e4SLinus Torvalds 		return -ENOENT;
2418f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
24191da177e4SLinus Torvalds }
24201da177e4SLinus Torvalds 
24211da177e4SLinus Torvalds /*
24221da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
24231da177e4SLinus Torvalds  */
24241da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
24251da177e4SLinus Torvalds {
24261da177e4SLinus Torvalds 	struct dentry *p;
24271da177e4SLinus Torvalds 
24281da177e4SLinus Torvalds 	if (p1 == p2) {
2429f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
24301da177e4SLinus Torvalds 		return NULL;
24311da177e4SLinus Torvalds 	}
24321da177e4SLinus Torvalds 
2433a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
24341da177e4SLinus Torvalds 
2435e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
2436e2761a11SOGAWA Hirofumi 	if (p) {
2437f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2438f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
24391da177e4SLinus Torvalds 		return p;
24401da177e4SLinus Torvalds 	}
24411da177e4SLinus Torvalds 
2442e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
2443e2761a11SOGAWA Hirofumi 	if (p) {
2444f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2445f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
24461da177e4SLinus Torvalds 		return p;
24471da177e4SLinus Torvalds 	}
24481da177e4SLinus Torvalds 
2449f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2450f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
24511da177e4SLinus Torvalds 	return NULL;
24521da177e4SLinus Torvalds }
24531da177e4SLinus Torvalds 
24541da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
24551da177e4SLinus Torvalds {
24561b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
24571da177e4SLinus Torvalds 	if (p1 != p2) {
24581b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
2459a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
24601da177e4SLinus Torvalds 	}
24611da177e4SLinus Torvalds }
24621da177e4SLinus Torvalds 
24634acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2464312b63fbSAl Viro 		bool want_excl)
24651da177e4SLinus Torvalds {
2466a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
24671da177e4SLinus Torvalds 	if (error)
24681da177e4SLinus Torvalds 		return error;
24691da177e4SLinus Torvalds 
2470acfa4380SAl Viro 	if (!dir->i_op->create)
24711da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
24721da177e4SLinus Torvalds 	mode &= S_IALLUGO;
24731da177e4SLinus Torvalds 	mode |= S_IFREG;
24741da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
24751da177e4SLinus Torvalds 	if (error)
24761da177e4SLinus Torvalds 		return error;
2477312b63fbSAl Viro 	error = dir->i_op->create(dir, dentry, mode, want_excl);
2478a74574aaSStephen Smalley 	if (!error)
2479f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
24801da177e4SLinus Torvalds 	return error;
24811da177e4SLinus Torvalds }
24821da177e4SLinus Torvalds 
248373d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
24841da177e4SLinus Torvalds {
24853fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
24861da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
24871da177e4SLinus Torvalds 	int error;
24881da177e4SLinus Torvalds 
2489bcda7652SAl Viro 	/* O_PATH? */
2490bcda7652SAl Viro 	if (!acc_mode)
2491bcda7652SAl Viro 		return 0;
2492bcda7652SAl Viro 
24931da177e4SLinus Torvalds 	if (!inode)
24941da177e4SLinus Torvalds 		return -ENOENT;
24951da177e4SLinus Torvalds 
2496c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2497c8fe8f30SChristoph Hellwig 	case S_IFLNK:
24981da177e4SLinus Torvalds 		return -ELOOP;
2499c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2500c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
25011da177e4SLinus Torvalds 			return -EISDIR;
2502c8fe8f30SChristoph Hellwig 		break;
2503c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2504c8fe8f30SChristoph Hellwig 	case S_IFCHR:
25053fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
25061da177e4SLinus Torvalds 			return -EACCES;
2507c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
2508c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2509c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
25101da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2511c8fe8f30SChristoph Hellwig 		break;
25124a3fd211SDave Hansen 	}
2513b41572e9SDave Hansen 
25143fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2515b41572e9SDave Hansen 	if (error)
2516b41572e9SDave Hansen 		return error;
25176146f0d5SMimi Zohar 
25181da177e4SLinus Torvalds 	/*
25191da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
25201da177e4SLinus Torvalds 	 */
25211da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
25228737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
25237715b521SAl Viro 			return -EPERM;
25241da177e4SLinus Torvalds 		if (flag & O_TRUNC)
25257715b521SAl Viro 			return -EPERM;
25261da177e4SLinus Torvalds 	}
25271da177e4SLinus Torvalds 
25281da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
25292e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
25307715b521SAl Viro 		return -EPERM;
25311da177e4SLinus Torvalds 
2532f3c7691eSJ. Bruce Fields 	return 0;
25337715b521SAl Viro }
25347715b521SAl Viro 
2535e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
25367715b521SAl Viro {
2537e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
25387715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
25397715b521SAl Viro 	int error = get_write_access(inode);
25401da177e4SLinus Torvalds 	if (error)
25417715b521SAl Viro 		return error;
25421da177e4SLinus Torvalds 	/*
25431da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
25441da177e4SLinus Torvalds 	 */
25451da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2546be6d3e56SKentaro Takeda 	if (!error)
2547ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
25481da177e4SLinus Torvalds 	if (!error) {
25497715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2550d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2551e1181ee6SJeff Layton 				    filp);
25521da177e4SLinus Torvalds 	}
25531da177e4SLinus Torvalds 	put_write_access(inode);
2554acd0c935SMimi Zohar 	return error;
25551da177e4SLinus Torvalds }
25561da177e4SLinus Torvalds 
2557d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2558d57999e1SDave Hansen {
25598a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
25608a5e929dSAl Viro 		flag--;
2561d57999e1SDave Hansen 	return flag;
2562d57999e1SDave Hansen }
2563d57999e1SDave Hansen 
2564d18e9008SMiklos Szeredi static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2565d18e9008SMiklos Szeredi {
2566d18e9008SMiklos Szeredi 	int error = security_path_mknod(dir, dentry, mode, 0);
2567d18e9008SMiklos Szeredi 	if (error)
2568d18e9008SMiklos Szeredi 		return error;
2569d18e9008SMiklos Szeredi 
2570d18e9008SMiklos Szeredi 	error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2571d18e9008SMiklos Szeredi 	if (error)
2572d18e9008SMiklos Szeredi 		return error;
2573d18e9008SMiklos Szeredi 
2574d18e9008SMiklos Szeredi 	return security_inode_create(dir->dentry->d_inode, dentry, mode);
2575d18e9008SMiklos Szeredi }
2576d18e9008SMiklos Szeredi 
25771acf0af9SDavid Howells /*
25781acf0af9SDavid Howells  * Attempt to atomically look up, create and open a file from a negative
25791acf0af9SDavid Howells  * dentry.
25801acf0af9SDavid Howells  *
25811acf0af9SDavid Howells  * Returns 0 if successful.  The file will have been created and attached to
25821acf0af9SDavid Howells  * @file by the filesystem calling finish_open().
25831acf0af9SDavid Howells  *
25841acf0af9SDavid Howells  * Returns 1 if the file was looked up only or didn't need creating.  The
25851acf0af9SDavid Howells  * caller will need to perform the open themselves.  @path will have been
25861acf0af9SDavid Howells  * updated to point to the new dentry.  This may be negative.
25871acf0af9SDavid Howells  *
25881acf0af9SDavid Howells  * Returns an error code otherwise.
25891acf0af9SDavid Howells  */
25902675a4ebSAl Viro static int atomic_open(struct nameidata *nd, struct dentry *dentry,
259130d90494SAl Viro 			struct path *path, struct file *file,
2592015c3bbcSMiklos Szeredi 			const struct open_flags *op,
259364894cf8SAl Viro 			bool got_write, bool need_lookup,
259447237687SAl Viro 			int *opened)
2595d18e9008SMiklos Szeredi {
2596d18e9008SMiklos Szeredi 	struct inode *dir =  nd->path.dentry->d_inode;
2597d18e9008SMiklos Szeredi 	unsigned open_flag = open_to_namei_flags(op->open_flag);
2598d18e9008SMiklos Szeredi 	umode_t mode;
2599d18e9008SMiklos Szeredi 	int error;
2600d18e9008SMiklos Szeredi 	int acc_mode;
2601d18e9008SMiklos Szeredi 	int create_error = 0;
2602d18e9008SMiklos Szeredi 	struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2603116cc022SMiklos Szeredi 	bool excl;
2604d18e9008SMiklos Szeredi 
2605d18e9008SMiklos Szeredi 	BUG_ON(dentry->d_inode);
2606d18e9008SMiklos Szeredi 
2607d18e9008SMiklos Szeredi 	/* Don't create child dentry for a dead directory. */
2608d18e9008SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
26092675a4ebSAl Viro 		error = -ENOENT;
2610d18e9008SMiklos Szeredi 		goto out;
2611d18e9008SMiklos Szeredi 	}
2612d18e9008SMiklos Szeredi 
261362b259d8SMiklos Szeredi 	mode = op->mode;
2614d18e9008SMiklos Szeredi 	if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2615d18e9008SMiklos Szeredi 		mode &= ~current_umask();
2616d18e9008SMiklos Szeredi 
2617116cc022SMiklos Szeredi 	excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
2618116cc022SMiklos Szeredi 	if (excl)
2619d18e9008SMiklos Szeredi 		open_flag &= ~O_TRUNC;
2620d18e9008SMiklos Szeredi 
2621d18e9008SMiklos Szeredi 	/*
2622d18e9008SMiklos Szeredi 	 * Checking write permission is tricky, bacuse we don't know if we are
2623d18e9008SMiklos Szeredi 	 * going to actually need it: O_CREAT opens should work as long as the
2624d18e9008SMiklos Szeredi 	 * file exists.  But checking existence breaks atomicity.  The trick is
2625d18e9008SMiklos Szeredi 	 * to check access and if not granted clear O_CREAT from the flags.
2626d18e9008SMiklos Szeredi 	 *
2627d18e9008SMiklos Szeredi 	 * Another problem is returing the "right" error value (e.g. for an
2628d18e9008SMiklos Szeredi 	 * O_EXCL open we want to return EEXIST not EROFS).
2629d18e9008SMiklos Szeredi 	 */
263064894cf8SAl Viro 	if (((open_flag & (O_CREAT | O_TRUNC)) ||
263164894cf8SAl Viro 	    (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
263264894cf8SAl Viro 		if (!(open_flag & O_CREAT)) {
2633d18e9008SMiklos Szeredi 			/*
2634d18e9008SMiklos Szeredi 			 * No O_CREATE -> atomicity not a requirement -> fall
2635d18e9008SMiklos Szeredi 			 * back to lookup + open
2636d18e9008SMiklos Szeredi 			 */
2637d18e9008SMiklos Szeredi 			goto no_open;
2638d18e9008SMiklos Szeredi 		} else if (open_flag & (O_EXCL | O_TRUNC)) {
2639d18e9008SMiklos Szeredi 			/* Fall back and fail with the right error */
264064894cf8SAl Viro 			create_error = -EROFS;
2641d18e9008SMiklos Szeredi 			goto no_open;
2642d18e9008SMiklos Szeredi 		} else {
2643d18e9008SMiklos Szeredi 			/* No side effects, safe to clear O_CREAT */
264464894cf8SAl Viro 			create_error = -EROFS;
2645d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2646d18e9008SMiklos Szeredi 		}
2647d18e9008SMiklos Szeredi 	}
2648d18e9008SMiklos Szeredi 
2649d18e9008SMiklos Szeredi 	if (open_flag & O_CREAT) {
265038227f78SMiklos Szeredi 		error = may_o_create(&nd->path, dentry, mode);
2651d18e9008SMiklos Szeredi 		if (error) {
2652d18e9008SMiklos Szeredi 			create_error = error;
2653d18e9008SMiklos Szeredi 			if (open_flag & O_EXCL)
2654d18e9008SMiklos Szeredi 				goto no_open;
2655d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2656d18e9008SMiklos Szeredi 		}
2657d18e9008SMiklos Szeredi 	}
2658d18e9008SMiklos Szeredi 
2659d18e9008SMiklos Szeredi 	if (nd->flags & LOOKUP_DIRECTORY)
2660d18e9008SMiklos Szeredi 		open_flag |= O_DIRECTORY;
2661d18e9008SMiklos Szeredi 
266230d90494SAl Viro 	file->f_path.dentry = DENTRY_NOT_SET;
266330d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
266430d90494SAl Viro 	error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
266547237687SAl Viro 				      opened);
2666d9585277SAl Viro 	if (error < 0) {
2667d9585277SAl Viro 		if (create_error && error == -ENOENT)
2668d9585277SAl Viro 			error = create_error;
2669d18e9008SMiklos Szeredi 		goto out;
2670d18e9008SMiklos Szeredi 	}
2671d18e9008SMiklos Szeredi 
2672d9585277SAl Viro 	if (error) {	/* returned 1, that is */
267330d90494SAl Viro 		if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
26742675a4ebSAl Viro 			error = -EIO;
2675d18e9008SMiklos Szeredi 			goto out;
2676d18e9008SMiklos Szeredi 		}
267730d90494SAl Viro 		if (file->f_path.dentry) {
2678d18e9008SMiklos Szeredi 			dput(dentry);
267930d90494SAl Viro 			dentry = file->f_path.dentry;
2680d18e9008SMiklos Szeredi 		}
268103da633aSAl Viro 		if (*opened & FILE_CREATED)
268203da633aSAl Viro 			fsnotify_create(dir, dentry);
268303da633aSAl Viro 		if (!dentry->d_inode) {
268403da633aSAl Viro 			WARN_ON(*opened & FILE_CREATED);
268503da633aSAl Viro 			if (create_error) {
268662b2ce96SSage Weil 				error = create_error;
268762b2ce96SSage Weil 				goto out;
268862b2ce96SSage Weil 			}
268903da633aSAl Viro 		} else {
269003da633aSAl Viro 			if (excl && !(*opened & FILE_CREATED)) {
269103da633aSAl Viro 				error = -EEXIST;
269203da633aSAl Viro 				goto out;
269303da633aSAl Viro 			}
269403da633aSAl Viro 		}
2695d18e9008SMiklos Szeredi 		goto looked_up;
2696d18e9008SMiklos Szeredi 	}
2697d18e9008SMiklos Szeredi 
2698d18e9008SMiklos Szeredi 	/*
2699d18e9008SMiklos Szeredi 	 * We didn't have the inode before the open, so check open permission
2700d18e9008SMiklos Szeredi 	 * here.
2701d18e9008SMiklos Szeredi 	 */
270203da633aSAl Viro 	acc_mode = op->acc_mode;
270303da633aSAl Viro 	if (*opened & FILE_CREATED) {
270403da633aSAl Viro 		WARN_ON(!(open_flag & O_CREAT));
270503da633aSAl Viro 		fsnotify_create(dir, dentry);
270603da633aSAl Viro 		acc_mode = MAY_OPEN;
270703da633aSAl Viro 	}
27082675a4ebSAl Viro 	error = may_open(&file->f_path, acc_mode, open_flag);
27092675a4ebSAl Viro 	if (error)
27102675a4ebSAl Viro 		fput(file);
2711d18e9008SMiklos Szeredi 
2712d18e9008SMiklos Szeredi out:
2713d18e9008SMiklos Szeredi 	dput(dentry);
27142675a4ebSAl Viro 	return error;
2715d18e9008SMiklos Szeredi 
2716d18e9008SMiklos Szeredi no_open:
2717d18e9008SMiklos Szeredi 	if (need_lookup) {
271872bd866aSAl Viro 		dentry = lookup_real(dir, dentry, nd->flags);
2719d18e9008SMiklos Szeredi 		if (IS_ERR(dentry))
27202675a4ebSAl Viro 			return PTR_ERR(dentry);
2721d18e9008SMiklos Szeredi 
2722d18e9008SMiklos Szeredi 		if (create_error) {
2723d18e9008SMiklos Szeredi 			int open_flag = op->open_flag;
2724d18e9008SMiklos Szeredi 
27252675a4ebSAl Viro 			error = create_error;
2726d18e9008SMiklos Szeredi 			if ((open_flag & O_EXCL)) {
2727d18e9008SMiklos Szeredi 				if (!dentry->d_inode)
2728d18e9008SMiklos Szeredi 					goto out;
2729d18e9008SMiklos Szeredi 			} else if (!dentry->d_inode) {
2730d18e9008SMiklos Szeredi 				goto out;
2731d18e9008SMiklos Szeredi 			} else if ((open_flag & O_TRUNC) &&
2732d18e9008SMiklos Szeredi 				   S_ISREG(dentry->d_inode->i_mode)) {
2733d18e9008SMiklos Szeredi 				goto out;
2734d18e9008SMiklos Szeredi 			}
2735d18e9008SMiklos Szeredi 			/* will fail later, go on to get the right error */
2736d18e9008SMiklos Szeredi 		}
2737d18e9008SMiklos Szeredi 	}
2738d18e9008SMiklos Szeredi looked_up:
2739d18e9008SMiklos Szeredi 	path->dentry = dentry;
2740d18e9008SMiklos Szeredi 	path->mnt = nd->path.mnt;
27412675a4ebSAl Viro 	return 1;
2742d18e9008SMiklos Szeredi }
2743d18e9008SMiklos Szeredi 
274431e6b01fSNick Piggin /*
27451acf0af9SDavid Howells  * Look up and maybe create and open the last component.
2746d58ffd35SMiklos Szeredi  *
2747d58ffd35SMiklos Szeredi  * Must be called with i_mutex held on parent.
2748d58ffd35SMiklos Szeredi  *
27491acf0af9SDavid Howells  * Returns 0 if the file was successfully atomically created (if necessary) and
27501acf0af9SDavid Howells  * opened.  In this case the file will be returned attached to @file.
27511acf0af9SDavid Howells  *
27521acf0af9SDavid Howells  * Returns 1 if the file was not completely opened at this time, though lookups
27531acf0af9SDavid Howells  * and creations will have been performed and the dentry returned in @path will
27541acf0af9SDavid Howells  * be positive upon return if O_CREAT was specified.  If O_CREAT wasn't
27551acf0af9SDavid Howells  * specified then a negative dentry may be returned.
27561acf0af9SDavid Howells  *
27571acf0af9SDavid Howells  * An error code is returned otherwise.
27581acf0af9SDavid Howells  *
27591acf0af9SDavid Howells  * FILE_CREATE will be set in @*opened if the dentry was created and will be
27601acf0af9SDavid Howells  * cleared otherwise prior to returning.
2761d58ffd35SMiklos Szeredi  */
27622675a4ebSAl Viro static int lookup_open(struct nameidata *nd, struct path *path,
276330d90494SAl Viro 			struct file *file,
2764d58ffd35SMiklos Szeredi 			const struct open_flags *op,
276564894cf8SAl Viro 			bool got_write, int *opened)
2766d58ffd35SMiklos Szeredi {
2767d58ffd35SMiklos Szeredi 	struct dentry *dir = nd->path.dentry;
276854ef4872SMiklos Szeredi 	struct inode *dir_inode = dir->d_inode;
2769d58ffd35SMiklos Szeredi 	struct dentry *dentry;
2770d58ffd35SMiklos Szeredi 	int error;
277154ef4872SMiklos Szeredi 	bool need_lookup;
2772d58ffd35SMiklos Szeredi 
277347237687SAl Viro 	*opened &= ~FILE_CREATED;
2774201f956eSAl Viro 	dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
2775d58ffd35SMiklos Szeredi 	if (IS_ERR(dentry))
27762675a4ebSAl Viro 		return PTR_ERR(dentry);
2777d58ffd35SMiklos Szeredi 
2778d18e9008SMiklos Szeredi 	/* Cached positive dentry: will open in f_op->open */
2779d18e9008SMiklos Szeredi 	if (!need_lookup && dentry->d_inode)
2780d18e9008SMiklos Szeredi 		goto out_no_open;
2781d18e9008SMiklos Szeredi 
2782d18e9008SMiklos Szeredi 	if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
278364894cf8SAl Viro 		return atomic_open(nd, dentry, path, file, op, got_write,
278447237687SAl Viro 				   need_lookup, opened);
2785d18e9008SMiklos Szeredi 	}
2786d18e9008SMiklos Szeredi 
278754ef4872SMiklos Szeredi 	if (need_lookup) {
278854ef4872SMiklos Szeredi 		BUG_ON(dentry->d_inode);
278954ef4872SMiklos Szeredi 
279072bd866aSAl Viro 		dentry = lookup_real(dir_inode, dentry, nd->flags);
279154ef4872SMiklos Szeredi 		if (IS_ERR(dentry))
27922675a4ebSAl Viro 			return PTR_ERR(dentry);
279354ef4872SMiklos Szeredi 	}
279454ef4872SMiklos Szeredi 
2795d58ffd35SMiklos Szeredi 	/* Negative dentry, just create the file */
2796d58ffd35SMiklos Szeredi 	if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2797d58ffd35SMiklos Szeredi 		umode_t mode = op->mode;
2798d58ffd35SMiklos Szeredi 		if (!IS_POSIXACL(dir->d_inode))
2799d58ffd35SMiklos Szeredi 			mode &= ~current_umask();
2800d58ffd35SMiklos Szeredi 		/*
2801d58ffd35SMiklos Szeredi 		 * This write is needed to ensure that a
2802d58ffd35SMiklos Szeredi 		 * rw->ro transition does not occur between
2803d58ffd35SMiklos Szeredi 		 * the time when the file is created and when
2804d58ffd35SMiklos Szeredi 		 * a permanent write count is taken through
2805015c3bbcSMiklos Szeredi 		 * the 'struct file' in finish_open().
2806d58ffd35SMiklos Szeredi 		 */
280764894cf8SAl Viro 		if (!got_write) {
280864894cf8SAl Viro 			error = -EROFS;
2809d58ffd35SMiklos Szeredi 			goto out_dput;
281064894cf8SAl Viro 		}
281147237687SAl Viro 		*opened |= FILE_CREATED;
2812d58ffd35SMiklos Szeredi 		error = security_path_mknod(&nd->path, dentry, mode, 0);
2813d58ffd35SMiklos Szeredi 		if (error)
2814d58ffd35SMiklos Szeredi 			goto out_dput;
2815312b63fbSAl Viro 		error = vfs_create(dir->d_inode, dentry, mode,
2816312b63fbSAl Viro 				   nd->flags & LOOKUP_EXCL);
2817d58ffd35SMiklos Szeredi 		if (error)
2818d58ffd35SMiklos Szeredi 			goto out_dput;
2819d58ffd35SMiklos Szeredi 	}
2820d18e9008SMiklos Szeredi out_no_open:
2821d58ffd35SMiklos Szeredi 	path->dentry = dentry;
2822d58ffd35SMiklos Szeredi 	path->mnt = nd->path.mnt;
28232675a4ebSAl Viro 	return 1;
2824d58ffd35SMiklos Szeredi 
2825d58ffd35SMiklos Szeredi out_dput:
2826d58ffd35SMiklos Szeredi 	dput(dentry);
28272675a4ebSAl Viro 	return error;
2828d58ffd35SMiklos Szeredi }
2829d58ffd35SMiklos Szeredi 
2830d58ffd35SMiklos Szeredi /*
2831fe2d35ffSAl Viro  * Handle the last step of open()
283231e6b01fSNick Piggin  */
28332675a4ebSAl Viro static int do_last(struct nameidata *nd, struct path *path,
283430d90494SAl Viro 		   struct file *file, const struct open_flags *op,
2835669abf4eSJeff Layton 		   int *opened, struct filename *name)
2836fb1cc555SAl Viro {
2837a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
2838ca344a89SAl Viro 	int open_flag = op->open_flag;
283977d660a8SMiklos Szeredi 	bool will_truncate = (open_flag & O_TRUNC) != 0;
284064894cf8SAl Viro 	bool got_write = false;
2841bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2842a1eb3315SMiklos Szeredi 	struct inode *inode;
284377d660a8SMiklos Szeredi 	bool symlink_ok = false;
284416b1c1cdSMiklos Szeredi 	struct path save_parent = { .dentry = NULL, .mnt = NULL };
284516b1c1cdSMiklos Szeredi 	bool retried = false;
284616c2cd71SAl Viro 	int error;
2847fb1cc555SAl Viro 
2848c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2849c3e380b0SAl Viro 	nd->flags |= op->intent;
2850c3e380b0SAl Viro 
2851bc77daa7SAl Viro 	if (nd->last_type != LAST_NORM) {
2852fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2853fe2d35ffSAl Viro 		if (error)
28542675a4ebSAl Viro 			return error;
2855e83db167SMiklos Szeredi 		goto finish_open;
28561f36f774SAl Viro 	}
2857a2c36b45SAl Viro 
2858ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2859fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2860fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2861bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
286277d660a8SMiklos Szeredi 			symlink_ok = true;
2863fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2864e97cdc87SAl Viro 		error = lookup_fast(nd, path, &inode);
286571574865SMiklos Szeredi 		if (likely(!error))
286671574865SMiklos Szeredi 			goto finish_lookup;
286771574865SMiklos Szeredi 
2868ce57dfc1SAl Viro 		if (error < 0)
28692675a4ebSAl Viro 			goto out;
2870a1eb3315SMiklos Szeredi 
287137d7fffcSMiklos Szeredi 		BUG_ON(nd->inode != dir->d_inode);
2872b6183df7SMiklos Szeredi 	} else {
2873fe2d35ffSAl Viro 		/* create side of things */
2874a3fbbde7SAl Viro 		/*
2875b6183df7SMiklos Szeredi 		 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2876b6183df7SMiklos Szeredi 		 * has been cleared when we got to the last component we are
2877b6183df7SMiklos Szeredi 		 * about to look up
2878a3fbbde7SAl Viro 		 */
28799f1fafeeSAl Viro 		error = complete_walk(nd);
28809f1fafeeSAl Viro 		if (error)
28812675a4ebSAl Viro 			return error;
2882fe2d35ffSAl Viro 
288333e2208aSJeff Layton 		audit_inode(name, dir, LOOKUP_PARENT);
288416c2cd71SAl Viro 		error = -EISDIR;
28851f36f774SAl Viro 		/* trailing slashes? */
288631e6b01fSNick Piggin 		if (nd->last.name[nd->last.len])
28872675a4ebSAl Viro 			goto out;
2888b6183df7SMiklos Szeredi 	}
28891f36f774SAl Viro 
289016b1c1cdSMiklos Szeredi retry_lookup:
289164894cf8SAl Viro 	if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
289264894cf8SAl Viro 		error = mnt_want_write(nd->path.mnt);
289364894cf8SAl Viro 		if (!error)
289464894cf8SAl Viro 			got_write = true;
289564894cf8SAl Viro 		/*
289664894cf8SAl Viro 		 * do _not_ fail yet - we might not need that or fail with
289764894cf8SAl Viro 		 * a different error; let lookup_open() decide; we'll be
289864894cf8SAl Viro 		 * dropping this one anyway.
289964894cf8SAl Viro 		 */
290064894cf8SAl Viro 	}
2901a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
290264894cf8SAl Viro 	error = lookup_open(nd, path, file, op, got_write, opened);
2903fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2904fb1cc555SAl Viro 
29052675a4ebSAl Viro 	if (error <= 0) {
29062675a4ebSAl Viro 		if (error)
2907d58ffd35SMiklos Szeredi 			goto out;
29086c0d46c4SAl Viro 
290947237687SAl Viro 		if ((*opened & FILE_CREATED) ||
2910496ad9aaSAl Viro 		    !S_ISREG(file_inode(file)->i_mode))
291177d660a8SMiklos Szeredi 			will_truncate = false;
2912d18e9008SMiklos Szeredi 
2913adb5c247SJeff Layton 		audit_inode(name, file->f_path.dentry, 0);
2914d18e9008SMiklos Szeredi 		goto opened;
2915d18e9008SMiklos Szeredi 	}
2916d18e9008SMiklos Szeredi 
291747237687SAl Viro 	if (*opened & FILE_CREATED) {
29189b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2919ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
292077d660a8SMiklos Szeredi 		will_truncate = false;
2921bcda7652SAl Viro 		acc_mode = MAY_OPEN;
2922d58ffd35SMiklos Szeredi 		path_to_nameidata(path, nd);
2923e83db167SMiklos Szeredi 		goto finish_open_created;
2924fb1cc555SAl Viro 	}
2925fb1cc555SAl Viro 
2926fb1cc555SAl Viro 	/*
29273134f37eSJeff Layton 	 * create/update audit record if it already exists.
2928fb1cc555SAl Viro 	 */
2929b18825a7SDavid Howells 	if (d_is_positive(path->dentry))
2930adb5c247SJeff Layton 		audit_inode(name, path->dentry, 0);
2931fb1cc555SAl Viro 
2932d18e9008SMiklos Szeredi 	/*
2933d18e9008SMiklos Szeredi 	 * If atomic_open() acquired write access it is dropped now due to
2934d18e9008SMiklos Szeredi 	 * possible mount and symlink following (this might be optimized away if
2935d18e9008SMiklos Szeredi 	 * necessary...)
2936d18e9008SMiklos Szeredi 	 */
293764894cf8SAl Viro 	if (got_write) {
2938d18e9008SMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
293964894cf8SAl Viro 		got_write = false;
2940d18e9008SMiklos Szeredi 	}
2941d18e9008SMiklos Szeredi 
2942fb1cc555SAl Viro 	error = -EEXIST;
2943f8310c59SAl Viro 	if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
2944fb1cc555SAl Viro 		goto exit_dput;
2945fb1cc555SAl Viro 
29469875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
29479875cf80SDavid Howells 	if (error < 0)
2948fb1cc555SAl Viro 		goto exit_dput;
2949fb1cc555SAl Viro 
2950a3fbbde7SAl Viro 	if (error)
2951a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
2952a3fbbde7SAl Viro 
2953decf3400SMiklos Szeredi 	BUG_ON(nd->flags & LOOKUP_RCU);
2954decf3400SMiklos Szeredi 	inode = path->dentry->d_inode;
29555f5daac1SMiklos Szeredi finish_lookup:
29565f5daac1SMiklos Szeredi 	/* we _can_ be in RCU mode here */
2957fb1cc555SAl Viro 	error = -ENOENT;
2958b18825a7SDavid Howells 	if (d_is_negative(path->dentry)) {
295954c33e7fSMiklos Szeredi 		path_to_nameidata(path, nd);
29602675a4ebSAl Viro 		goto out;
296154c33e7fSMiklos Szeredi 	}
29629e67f361SAl Viro 
2963b18825a7SDavid Howells 	if (should_follow_link(path->dentry, !symlink_ok)) {
2964d45ea867SMiklos Szeredi 		if (nd->flags & LOOKUP_RCU) {
2965d45ea867SMiklos Szeredi 			if (unlikely(unlazy_walk(nd, path->dentry))) {
2966d45ea867SMiklos Szeredi 				error = -ECHILD;
29672675a4ebSAl Viro 				goto out;
2968d45ea867SMiklos Szeredi 			}
2969d45ea867SMiklos Szeredi 		}
2970d45ea867SMiklos Szeredi 		BUG_ON(inode != path->dentry->d_inode);
29712675a4ebSAl Viro 		return 1;
2972d45ea867SMiklos Szeredi 	}
2973fb1cc555SAl Viro 
297416b1c1cdSMiklos Szeredi 	if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
2975fb1cc555SAl Viro 		path_to_nameidata(path, nd);
297616b1c1cdSMiklos Szeredi 	} else {
297716b1c1cdSMiklos Szeredi 		save_parent.dentry = nd->path.dentry;
297816b1c1cdSMiklos Szeredi 		save_parent.mnt = mntget(path->mnt);
297916b1c1cdSMiklos Szeredi 		nd->path.dentry = path->dentry;
298016b1c1cdSMiklos Szeredi 
298116b1c1cdSMiklos Szeredi 	}
2982decf3400SMiklos Szeredi 	nd->inode = inode;
2983a3fbbde7SAl Viro 	/* Why this, you ask?  _Now_ we might have grown LOOKUP_JUMPED... */
2984bc77daa7SAl Viro finish_open:
2985a3fbbde7SAl Viro 	error = complete_walk(nd);
298616b1c1cdSMiklos Szeredi 	if (error) {
298716b1c1cdSMiklos Szeredi 		path_put(&save_parent);
29882675a4ebSAl Viro 		return error;
298916b1c1cdSMiklos Szeredi 	}
2990bc77daa7SAl Viro 	audit_inode(name, nd->path.dentry, 0);
2991fb1cc555SAl Viro 	error = -EISDIR;
2992b18825a7SDavid Howells 	if ((open_flag & O_CREAT) &&
2993b18825a7SDavid Howells 	    (d_is_directory(nd->path.dentry) || d_is_autodir(nd->path.dentry)))
29942675a4ebSAl Viro 		goto out;
2995af2f5542SMiklos Szeredi 	error = -ENOTDIR;
2996b18825a7SDavid Howells 	if ((nd->flags & LOOKUP_DIRECTORY) && !d_is_directory(nd->path.dentry))
29972675a4ebSAl Viro 		goto out;
29986c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
299977d660a8SMiklos Szeredi 		will_truncate = false;
30006c0d46c4SAl Viro 
30010f9d1a10SAl Viro 	if (will_truncate) {
30020f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
30030f9d1a10SAl Viro 		if (error)
30042675a4ebSAl Viro 			goto out;
300564894cf8SAl Viro 		got_write = true;
30060f9d1a10SAl Viro 	}
3007e83db167SMiklos Szeredi finish_open_created:
3008bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
3009ca344a89SAl Viro 	if (error)
30102675a4ebSAl Viro 		goto out;
301130d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
301230d90494SAl Viro 	error = finish_open(file, nd->path.dentry, NULL, opened);
301330d90494SAl Viro 	if (error) {
301430d90494SAl Viro 		if (error == -EOPENSTALE)
3015f60dc3dbSMiklos Szeredi 			goto stale_open;
3016015c3bbcSMiklos Szeredi 		goto out;
3017f60dc3dbSMiklos Szeredi 	}
3018a8277b9bSMiklos Szeredi opened:
30192675a4ebSAl Viro 	error = open_check_o_direct(file);
3020015c3bbcSMiklos Szeredi 	if (error)
3021015c3bbcSMiklos Szeredi 		goto exit_fput;
30222675a4ebSAl Viro 	error = ima_file_check(file, op->acc_mode);
3023aa4caadbSMiklos Szeredi 	if (error)
3024aa4caadbSMiklos Szeredi 		goto exit_fput;
3025aa4caadbSMiklos Szeredi 
30260f9d1a10SAl Viro 	if (will_truncate) {
30272675a4ebSAl Viro 		error = handle_truncate(file);
3028aa4caadbSMiklos Szeredi 		if (error)
3029aa4caadbSMiklos Szeredi 			goto exit_fput;
30300f9d1a10SAl Viro 	}
3031ca344a89SAl Viro out:
303264894cf8SAl Viro 	if (got_write)
30330f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
303416b1c1cdSMiklos Szeredi 	path_put(&save_parent);
3035e276ae67SMiklos Szeredi 	terminate_walk(nd);
30362675a4ebSAl Viro 	return error;
3037fb1cc555SAl Viro 
3038fb1cc555SAl Viro exit_dput:
3039fb1cc555SAl Viro 	path_put_conditional(path, nd);
3040ca344a89SAl Viro 	goto out;
3041015c3bbcSMiklos Szeredi exit_fput:
30422675a4ebSAl Viro 	fput(file);
30432675a4ebSAl Viro 	goto out;
3044015c3bbcSMiklos Szeredi 
3045f60dc3dbSMiklos Szeredi stale_open:
3046f60dc3dbSMiklos Szeredi 	/* If no saved parent or already retried then can't retry */
3047f60dc3dbSMiklos Szeredi 	if (!save_parent.dentry || retried)
3048f60dc3dbSMiklos Szeredi 		goto out;
3049f60dc3dbSMiklos Szeredi 
3050f60dc3dbSMiklos Szeredi 	BUG_ON(save_parent.dentry != dir);
3051f60dc3dbSMiklos Szeredi 	path_put(&nd->path);
3052f60dc3dbSMiklos Szeredi 	nd->path = save_parent;
3053f60dc3dbSMiklos Szeredi 	nd->inode = dir->d_inode;
3054f60dc3dbSMiklos Szeredi 	save_parent.mnt = NULL;
3055f60dc3dbSMiklos Szeredi 	save_parent.dentry = NULL;
305664894cf8SAl Viro 	if (got_write) {
3057f60dc3dbSMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
305864894cf8SAl Viro 		got_write = false;
3059f60dc3dbSMiklos Szeredi 	}
3060f60dc3dbSMiklos Szeredi 	retried = true;
3061f60dc3dbSMiklos Szeredi 	goto retry_lookup;
3062fb1cc555SAl Viro }
3063fb1cc555SAl Viro 
306460545d0dSAl Viro static int do_tmpfile(int dfd, struct filename *pathname,
306560545d0dSAl Viro 		struct nameidata *nd, int flags,
306660545d0dSAl Viro 		const struct open_flags *op,
306760545d0dSAl Viro 		struct file *file, int *opened)
306860545d0dSAl Viro {
306960545d0dSAl Viro 	static const struct qstr name = QSTR_INIT("/", 1);
307060545d0dSAl Viro 	struct dentry *dentry, *child;
307160545d0dSAl Viro 	struct inode *dir;
307260545d0dSAl Viro 	int error = path_lookupat(dfd, pathname->name,
307360545d0dSAl Viro 				  flags | LOOKUP_DIRECTORY, nd);
307460545d0dSAl Viro 	if (unlikely(error))
307560545d0dSAl Viro 		return error;
307660545d0dSAl Viro 	error = mnt_want_write(nd->path.mnt);
307760545d0dSAl Viro 	if (unlikely(error))
307860545d0dSAl Viro 		goto out;
307960545d0dSAl Viro 	/* we want directory to be writable */
308060545d0dSAl Viro 	error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
308160545d0dSAl Viro 	if (error)
308260545d0dSAl Viro 		goto out2;
308360545d0dSAl Viro 	dentry = nd->path.dentry;
308460545d0dSAl Viro 	dir = dentry->d_inode;
308560545d0dSAl Viro 	if (!dir->i_op->tmpfile) {
308660545d0dSAl Viro 		error = -EOPNOTSUPP;
308760545d0dSAl Viro 		goto out2;
308860545d0dSAl Viro 	}
308960545d0dSAl Viro 	child = d_alloc(dentry, &name);
309060545d0dSAl Viro 	if (unlikely(!child)) {
309160545d0dSAl Viro 		error = -ENOMEM;
309260545d0dSAl Viro 		goto out2;
309360545d0dSAl Viro 	}
309460545d0dSAl Viro 	nd->flags &= ~LOOKUP_DIRECTORY;
309560545d0dSAl Viro 	nd->flags |= op->intent;
309660545d0dSAl Viro 	dput(nd->path.dentry);
309760545d0dSAl Viro 	nd->path.dentry = child;
309860545d0dSAl Viro 	error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
309960545d0dSAl Viro 	if (error)
310060545d0dSAl Viro 		goto out2;
310160545d0dSAl Viro 	audit_inode(pathname, nd->path.dentry, 0);
310260545d0dSAl Viro 	error = may_open(&nd->path, op->acc_mode, op->open_flag);
310360545d0dSAl Viro 	if (error)
310460545d0dSAl Viro 		goto out2;
310560545d0dSAl Viro 	file->f_path.mnt = nd->path.mnt;
310660545d0dSAl Viro 	error = finish_open(file, nd->path.dentry, NULL, opened);
310760545d0dSAl Viro 	if (error)
310860545d0dSAl Viro 		goto out2;
310960545d0dSAl Viro 	error = open_check_o_direct(file);
3110f4e0c30cSAl Viro 	if (error) {
311160545d0dSAl Viro 		fput(file);
3112f4e0c30cSAl Viro 	} else if (!(op->open_flag & O_EXCL)) {
3113f4e0c30cSAl Viro 		struct inode *inode = file_inode(file);
3114f4e0c30cSAl Viro 		spin_lock(&inode->i_lock);
3115f4e0c30cSAl Viro 		inode->i_state |= I_LINKABLE;
3116f4e0c30cSAl Viro 		spin_unlock(&inode->i_lock);
3117f4e0c30cSAl Viro 	}
311860545d0dSAl Viro out2:
311960545d0dSAl Viro 	mnt_drop_write(nd->path.mnt);
312060545d0dSAl Viro out:
312160545d0dSAl Viro 	path_put(&nd->path);
312260545d0dSAl Viro 	return error;
312360545d0dSAl Viro }
312460545d0dSAl Viro 
3125669abf4eSJeff Layton static struct file *path_openat(int dfd, struct filename *pathname,
312673d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
31271da177e4SLinus Torvalds {
3128fe2d35ffSAl Viro 	struct file *base = NULL;
312930d90494SAl Viro 	struct file *file;
31309850c056SAl Viro 	struct path path;
313147237687SAl Viro 	int opened = 0;
313213aab428SAl Viro 	int error;
313331e6b01fSNick Piggin 
313430d90494SAl Viro 	file = get_empty_filp();
31351afc99beSAl Viro 	if (IS_ERR(file))
31361afc99beSAl Viro 		return file;
313731e6b01fSNick Piggin 
313830d90494SAl Viro 	file->f_flags = op->open_flag;
313931e6b01fSNick Piggin 
3140bb458c64SAl Viro 	if (unlikely(file->f_flags & __O_TMPFILE)) {
314160545d0dSAl Viro 		error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
314260545d0dSAl Viro 		goto out;
314360545d0dSAl Viro 	}
314460545d0dSAl Viro 
3145669abf4eSJeff Layton 	error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
314631e6b01fSNick Piggin 	if (unlikely(error))
31472675a4ebSAl Viro 		goto out;
314831e6b01fSNick Piggin 
3149fe2d35ffSAl Viro 	current->total_link_count = 0;
3150669abf4eSJeff Layton 	error = link_path_walk(pathname->name, nd);
315131e6b01fSNick Piggin 	if (unlikely(error))
31522675a4ebSAl Viro 		goto out;
31531da177e4SLinus Torvalds 
31542675a4ebSAl Viro 	error = do_last(nd, &path, file, op, &opened, pathname);
31552675a4ebSAl Viro 	while (unlikely(error > 0)) { /* trailing symlink */
31567b9337aaSNick Piggin 		struct path link = path;
3157def4af30SAl Viro 		void *cookie;
3158574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
315973d049a4SAl Viro 			path_put_conditional(&path, nd);
316073d049a4SAl Viro 			path_put(&nd->path);
31612675a4ebSAl Viro 			error = -ELOOP;
316240b39136SAl Viro 			break;
316340b39136SAl Viro 		}
3164800179c9SKees Cook 		error = may_follow_link(&link, nd);
3165800179c9SKees Cook 		if (unlikely(error))
3166800179c9SKees Cook 			break;
316773d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
316873d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
3169574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
3170c3e380b0SAl Viro 		if (unlikely(error))
31712675a4ebSAl Viro 			break;
31722675a4ebSAl Viro 		error = do_last(nd, &path, file, op, &opened, pathname);
3173574197e0SAl Viro 		put_link(nd, &link, cookie);
3174806b681cSAl Viro 	}
317510fa8e62SAl Viro out:
317673d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
317773d049a4SAl Viro 		path_put(&nd->root);
3178fe2d35ffSAl Viro 	if (base)
3179fe2d35ffSAl Viro 		fput(base);
31802675a4ebSAl Viro 	if (!(opened & FILE_OPENED)) {
31812675a4ebSAl Viro 		BUG_ON(!error);
318230d90494SAl Viro 		put_filp(file);
3183015c3bbcSMiklos Szeredi 	}
31842675a4ebSAl Viro 	if (unlikely(error)) {
31852675a4ebSAl Viro 		if (error == -EOPENSTALE) {
31862675a4ebSAl Viro 			if (flags & LOOKUP_RCU)
31872675a4ebSAl Viro 				error = -ECHILD;
31882675a4ebSAl Viro 			else
31892675a4ebSAl Viro 				error = -ESTALE;
31902675a4ebSAl Viro 		}
31912675a4ebSAl Viro 		file = ERR_PTR(error);
31922675a4ebSAl Viro 	}
31932675a4ebSAl Viro 	return file;
3194de459215SKirill Korotaev }
31951da177e4SLinus Torvalds 
3196669abf4eSJeff Layton struct file *do_filp_open(int dfd, struct filename *pathname,
3197f9652e10SAl Viro 		const struct open_flags *op)
319813aab428SAl Viro {
319973d049a4SAl Viro 	struct nameidata nd;
3200f9652e10SAl Viro 	int flags = op->lookup_flags;
320113aab428SAl Viro 	struct file *filp;
320213aab428SAl Viro 
320373d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
320413aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
320573d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
320613aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
320773d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
320813aab428SAl Viro 	return filp;
320913aab428SAl Viro }
321013aab428SAl Viro 
321173d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3212f9652e10SAl Viro 		const char *name, const struct open_flags *op)
321373d049a4SAl Viro {
321473d049a4SAl Viro 	struct nameidata nd;
321573d049a4SAl Viro 	struct file *file;
3216669abf4eSJeff Layton 	struct filename filename = { .name = name };
3217f9652e10SAl Viro 	int flags = op->lookup_flags | LOOKUP_ROOT;
321873d049a4SAl Viro 
321973d049a4SAl Viro 	nd.root.mnt = mnt;
322073d049a4SAl Viro 	nd.root.dentry = dentry;
322173d049a4SAl Viro 
3222b18825a7SDavid Howells 	if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
322373d049a4SAl Viro 		return ERR_PTR(-ELOOP);
322473d049a4SAl Viro 
3225669abf4eSJeff Layton 	file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
322673d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
3227669abf4eSJeff Layton 		file = path_openat(-1, &filename, &nd, op, flags);
322873d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
3229669abf4eSJeff Layton 		file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
323073d049a4SAl Viro 	return file;
323173d049a4SAl Viro }
323273d049a4SAl Viro 
32331ac12b4bSJeff Layton struct dentry *kern_path_create(int dfd, const char *pathname,
32341ac12b4bSJeff Layton 				struct path *path, unsigned int lookup_flags)
32351da177e4SLinus Torvalds {
3236c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
3237ed75e95dSAl Viro 	struct nameidata nd;
3238c30dabfeSJan Kara 	int err2;
32391ac12b4bSJeff Layton 	int error;
32401ac12b4bSJeff Layton 	bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
32411ac12b4bSJeff Layton 
32421ac12b4bSJeff Layton 	/*
32431ac12b4bSJeff Layton 	 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
32441ac12b4bSJeff Layton 	 * other flags passed in are ignored!
32451ac12b4bSJeff Layton 	 */
32461ac12b4bSJeff Layton 	lookup_flags &= LOOKUP_REVAL;
32471ac12b4bSJeff Layton 
32481ac12b4bSJeff Layton 	error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
3249ed75e95dSAl Viro 	if (error)
3250ed75e95dSAl Viro 		return ERR_PTR(error);
32511da177e4SLinus Torvalds 
3252c663e5d8SChristoph Hellwig 	/*
3253c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
3254c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
3255c663e5d8SChristoph Hellwig 	 */
3256ed75e95dSAl Viro 	if (nd.last_type != LAST_NORM)
3257ed75e95dSAl Viro 		goto out;
3258ed75e95dSAl Viro 	nd.flags &= ~LOOKUP_PARENT;
3259ed75e95dSAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3260c663e5d8SChristoph Hellwig 
3261c30dabfeSJan Kara 	/* don't fail immediately if it's r/o, at least try to report other errors */
3262c30dabfeSJan Kara 	err2 = mnt_want_write(nd.path.mnt);
3263c663e5d8SChristoph Hellwig 	/*
3264c663e5d8SChristoph Hellwig 	 * Do the final lookup.
3265c663e5d8SChristoph Hellwig 	 */
3266ed75e95dSAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3267ed75e95dSAl Viro 	dentry = lookup_hash(&nd);
32681da177e4SLinus Torvalds 	if (IS_ERR(dentry))
3269a8104a9fSAl Viro 		goto unlock;
3270c663e5d8SChristoph Hellwig 
3271a8104a9fSAl Viro 	error = -EEXIST;
3272b18825a7SDavid Howells 	if (d_is_positive(dentry))
3273a8104a9fSAl Viro 		goto fail;
3274b18825a7SDavid Howells 
3275c663e5d8SChristoph Hellwig 	/*
3276c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
3277c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
3278c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
3279c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
3280c663e5d8SChristoph Hellwig 	 */
3281ed75e95dSAl Viro 	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
3282a8104a9fSAl Viro 		error = -ENOENT;
3283ed75e95dSAl Viro 		goto fail;
3284e9baf6e5SAl Viro 	}
3285c30dabfeSJan Kara 	if (unlikely(err2)) {
3286c30dabfeSJan Kara 		error = err2;
3287a8104a9fSAl Viro 		goto fail;
3288c30dabfeSJan Kara 	}
3289ed75e95dSAl Viro 	*path = nd.path;
3290e9baf6e5SAl Viro 	return dentry;
32911da177e4SLinus Torvalds fail:
3292a8104a9fSAl Viro 	dput(dentry);
3293a8104a9fSAl Viro 	dentry = ERR_PTR(error);
3294a8104a9fSAl Viro unlock:
3295dae6ad8fSAl Viro 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3296c30dabfeSJan Kara 	if (!err2)
3297c30dabfeSJan Kara 		mnt_drop_write(nd.path.mnt);
3298ed75e95dSAl Viro out:
3299dae6ad8fSAl Viro 	path_put(&nd.path);
3300ed75e95dSAl Viro 	return dentry;
3301dae6ad8fSAl Viro }
3302dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
3303dae6ad8fSAl Viro 
3304921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry)
3305921a1650SAl Viro {
3306921a1650SAl Viro 	dput(dentry);
3307921a1650SAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
3308a8104a9fSAl Viro 	mnt_drop_write(path->mnt);
3309921a1650SAl Viro 	path_put(path);
3310921a1650SAl Viro }
3311921a1650SAl Viro EXPORT_SYMBOL(done_path_create);
3312921a1650SAl Viro 
33131ac12b4bSJeff Layton struct dentry *user_path_create(int dfd, const char __user *pathname,
33141ac12b4bSJeff Layton 				struct path *path, unsigned int lookup_flags)
3315dae6ad8fSAl Viro {
331691a27b2aSJeff Layton 	struct filename *tmp = getname(pathname);
3317dae6ad8fSAl Viro 	struct dentry *res;
3318dae6ad8fSAl Viro 	if (IS_ERR(tmp))
3319dae6ad8fSAl Viro 		return ERR_CAST(tmp);
33201ac12b4bSJeff Layton 	res = kern_path_create(dfd, tmp->name, path, lookup_flags);
3321dae6ad8fSAl Viro 	putname(tmp);
3322dae6ad8fSAl Viro 	return res;
3323dae6ad8fSAl Viro }
3324dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
3325dae6ad8fSAl Viro 
33261a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
33271da177e4SLinus Torvalds {
3328a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
33291da177e4SLinus Torvalds 
33301da177e4SLinus Torvalds 	if (error)
33311da177e4SLinus Torvalds 		return error;
33321da177e4SLinus Torvalds 
3333975d6b39SEric W. Biederman 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
33341da177e4SLinus Torvalds 		return -EPERM;
33351da177e4SLinus Torvalds 
3336acfa4380SAl Viro 	if (!dir->i_op->mknod)
33371da177e4SLinus Torvalds 		return -EPERM;
33381da177e4SLinus Torvalds 
333908ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
334008ce5f16SSerge E. Hallyn 	if (error)
334108ce5f16SSerge E. Hallyn 		return error;
334208ce5f16SSerge E. Hallyn 
33431da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
33441da177e4SLinus Torvalds 	if (error)
33451da177e4SLinus Torvalds 		return error;
33461da177e4SLinus Torvalds 
33471da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
3348a74574aaSStephen Smalley 	if (!error)
3349f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
33501da177e4SLinus Torvalds 	return error;
33511da177e4SLinus Torvalds }
33521da177e4SLinus Torvalds 
3353f69aac00SAl Viro static int may_mknod(umode_t mode)
3354463c3197SDave Hansen {
3355463c3197SDave Hansen 	switch (mode & S_IFMT) {
3356463c3197SDave Hansen 	case S_IFREG:
3357463c3197SDave Hansen 	case S_IFCHR:
3358463c3197SDave Hansen 	case S_IFBLK:
3359463c3197SDave Hansen 	case S_IFIFO:
3360463c3197SDave Hansen 	case S_IFSOCK:
3361463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
3362463c3197SDave Hansen 		return 0;
3363463c3197SDave Hansen 	case S_IFDIR:
3364463c3197SDave Hansen 		return -EPERM;
3365463c3197SDave Hansen 	default:
3366463c3197SDave Hansen 		return -EINVAL;
3367463c3197SDave Hansen 	}
3368463c3197SDave Hansen }
3369463c3197SDave Hansen 
33708208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
33712e4d0924SHeiko Carstens 		unsigned, dev)
33721da177e4SLinus Torvalds {
33731da177e4SLinus Torvalds 	struct dentry *dentry;
3374dae6ad8fSAl Viro 	struct path path;
3375dae6ad8fSAl Viro 	int error;
3376972567f1SJeff Layton 	unsigned int lookup_flags = 0;
33771da177e4SLinus Torvalds 
33788e4bfca1SAl Viro 	error = may_mknod(mode);
33798e4bfca1SAl Viro 	if (error)
33808e4bfca1SAl Viro 		return error;
3381972567f1SJeff Layton retry:
3382972567f1SJeff Layton 	dentry = user_path_create(dfd, filename, &path, lookup_flags);
3383dae6ad8fSAl Viro 	if (IS_ERR(dentry))
3384dae6ad8fSAl Viro 		return PTR_ERR(dentry);
33852ad94ae6SAl Viro 
3386dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3387ce3b0f8dSAl Viro 		mode &= ~current_umask();
3388dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
3389be6d3e56SKentaro Takeda 	if (error)
3390a8104a9fSAl Viro 		goto out;
33911da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
33921da177e4SLinus Torvalds 		case 0: case S_IFREG:
3393312b63fbSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,true);
33941da177e4SLinus Torvalds 			break;
33951da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
3396dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
33971da177e4SLinus Torvalds 					new_decode_dev(dev));
33981da177e4SLinus Torvalds 			break;
33991da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
3400dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
34011da177e4SLinus Torvalds 			break;
34021da177e4SLinus Torvalds 	}
3403a8104a9fSAl Viro out:
3404921a1650SAl Viro 	done_path_create(&path, dentry);
3405972567f1SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3406972567f1SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3407972567f1SJeff Layton 		goto retry;
3408972567f1SJeff Layton 	}
34091da177e4SLinus Torvalds 	return error;
34101da177e4SLinus Torvalds }
34111da177e4SLinus Torvalds 
34128208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
34135590ff0dSUlrich Drepper {
34145590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
34155590ff0dSUlrich Drepper }
34165590ff0dSUlrich Drepper 
341718bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
34181da177e4SLinus Torvalds {
3419a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
34208de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
34211da177e4SLinus Torvalds 
34221da177e4SLinus Torvalds 	if (error)
34231da177e4SLinus Torvalds 		return error;
34241da177e4SLinus Torvalds 
3425acfa4380SAl Viro 	if (!dir->i_op->mkdir)
34261da177e4SLinus Torvalds 		return -EPERM;
34271da177e4SLinus Torvalds 
34281da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
34291da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
34301da177e4SLinus Torvalds 	if (error)
34311da177e4SLinus Torvalds 		return error;
34321da177e4SLinus Torvalds 
34338de52778SAl Viro 	if (max_links && dir->i_nlink >= max_links)
34348de52778SAl Viro 		return -EMLINK;
34358de52778SAl Viro 
34361da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
3437a74574aaSStephen Smalley 	if (!error)
3438f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
34391da177e4SLinus Torvalds 	return error;
34401da177e4SLinus Torvalds }
34411da177e4SLinus Torvalds 
3442a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
34431da177e4SLinus Torvalds {
34446902d925SDave Hansen 	struct dentry *dentry;
3445dae6ad8fSAl Viro 	struct path path;
3446dae6ad8fSAl Viro 	int error;
3447b76d8b82SJeff Layton 	unsigned int lookup_flags = LOOKUP_DIRECTORY;
34481da177e4SLinus Torvalds 
3449b76d8b82SJeff Layton retry:
3450b76d8b82SJeff Layton 	dentry = user_path_create(dfd, pathname, &path, lookup_flags);
34516902d925SDave Hansen 	if (IS_ERR(dentry))
3452dae6ad8fSAl Viro 		return PTR_ERR(dentry);
34536902d925SDave Hansen 
3454dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3455ce3b0f8dSAl Viro 		mode &= ~current_umask();
3456dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
3457a8104a9fSAl Viro 	if (!error)
3458dae6ad8fSAl Viro 		error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3459921a1650SAl Viro 	done_path_create(&path, dentry);
3460b76d8b82SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3461b76d8b82SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3462b76d8b82SJeff Layton 		goto retry;
3463b76d8b82SJeff Layton 	}
34641da177e4SLinus Torvalds 	return error;
34651da177e4SLinus Torvalds }
34661da177e4SLinus Torvalds 
3467a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
34685590ff0dSUlrich Drepper {
34695590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
34705590ff0dSUlrich Drepper }
34715590ff0dSUlrich Drepper 
34721da177e4SLinus Torvalds /*
3473a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
3474c0d02594SJ. Bruce Fields  * should have a usage count of 1 if we're the only user of this
3475a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
3476a71905f0SSage Weil  * then we drop the dentry now.
34771da177e4SLinus Torvalds  *
34781da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
34791da177e4SLinus Torvalds  * do a
34801da177e4SLinus Torvalds  *
34811da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
34821da177e4SLinus Torvalds  *		return -EBUSY;
34831da177e4SLinus Torvalds  *
34841da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
34851da177e4SLinus Torvalds  * that is still in use by something else..
34861da177e4SLinus Torvalds  */
34871da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
34881da177e4SLinus Torvalds {
34891da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
34901da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
349198474236SWaiman Long 	if (dentry->d_lockref.count == 1)
34921da177e4SLinus Torvalds 		__d_drop(dentry);
34931da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
34941da177e4SLinus Torvalds }
34951da177e4SLinus Torvalds 
34961da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
34971da177e4SLinus Torvalds {
34981da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
34991da177e4SLinus Torvalds 
35001da177e4SLinus Torvalds 	if (error)
35011da177e4SLinus Torvalds 		return error;
35021da177e4SLinus Torvalds 
3503acfa4380SAl Viro 	if (!dir->i_op->rmdir)
35041da177e4SLinus Torvalds 		return -EPERM;
35051da177e4SLinus Torvalds 
35061d2ef590SAl Viro 	dget(dentry);
35071b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
3508912dbc15SSage Weil 
35091da177e4SLinus Torvalds 	error = -EBUSY;
3510912dbc15SSage Weil 	if (d_mountpoint(dentry))
3511912dbc15SSage Weil 		goto out;
3512912dbc15SSage Weil 
35131da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
3514912dbc15SSage Weil 	if (error)
3515912dbc15SSage Weil 		goto out;
3516912dbc15SSage Weil 
35173cebde24SSage Weil 	shrink_dcache_parent(dentry);
35181da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
3519912dbc15SSage Weil 	if (error)
3520912dbc15SSage Weil 		goto out;
3521912dbc15SSage Weil 
35221da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
3523d83c49f3SAl Viro 	dont_mount(dentry);
35241da177e4SLinus Torvalds 
3525912dbc15SSage Weil out:
3526912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
35271d2ef590SAl Viro 	dput(dentry);
3528912dbc15SSage Weil 	if (!error)
3529912dbc15SSage Weil 		d_delete(dentry);
35301da177e4SLinus Torvalds 	return error;
35311da177e4SLinus Torvalds }
35321da177e4SLinus Torvalds 
35335590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
35341da177e4SLinus Torvalds {
35351da177e4SLinus Torvalds 	int error = 0;
353691a27b2aSJeff Layton 	struct filename *name;
35371da177e4SLinus Torvalds 	struct dentry *dentry;
35381da177e4SLinus Torvalds 	struct nameidata nd;
3539c6ee9206SJeff Layton 	unsigned int lookup_flags = 0;
3540c6ee9206SJeff Layton retry:
3541c6ee9206SJeff Layton 	name = user_path_parent(dfd, pathname, &nd, lookup_flags);
354291a27b2aSJeff Layton 	if (IS_ERR(name))
354391a27b2aSJeff Layton 		return PTR_ERR(name);
35441da177e4SLinus Torvalds 
35451da177e4SLinus Torvalds 	switch(nd.last_type) {
35461da177e4SLinus Torvalds 	case LAST_DOTDOT:
35471da177e4SLinus Torvalds 		error = -ENOTEMPTY;
35481da177e4SLinus Torvalds 		goto exit1;
35491da177e4SLinus Torvalds 	case LAST_DOT:
35501da177e4SLinus Torvalds 		error = -EINVAL;
35511da177e4SLinus Torvalds 		goto exit1;
35521da177e4SLinus Torvalds 	case LAST_ROOT:
35531da177e4SLinus Torvalds 		error = -EBUSY;
35541da177e4SLinus Torvalds 		goto exit1;
35551da177e4SLinus Torvalds 	}
35560612d9fbSOGAWA Hirofumi 
35570612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
3558c30dabfeSJan Kara 	error = mnt_want_write(nd.path.mnt);
3559c30dabfeSJan Kara 	if (error)
3560c30dabfeSJan Kara 		goto exit1;
35610612d9fbSOGAWA Hirofumi 
35624ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
356349705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
35641da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
35656902d925SDave Hansen 	if (IS_ERR(dentry))
35666902d925SDave Hansen 		goto exit2;
3567e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
3568e6bc45d6STheodore Ts'o 		error = -ENOENT;
3569e6bc45d6STheodore Ts'o 		goto exit3;
3570e6bc45d6STheodore Ts'o 	}
3571be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
3572be6d3e56SKentaro Takeda 	if (error)
3573c30dabfeSJan Kara 		goto exit3;
35744ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
35750622753bSDave Hansen exit3:
35761da177e4SLinus Torvalds 	dput(dentry);
35776902d925SDave Hansen exit2:
35784ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3579c30dabfeSJan Kara 	mnt_drop_write(nd.path.mnt);
35801da177e4SLinus Torvalds exit1:
35811d957f9bSJan Blunck 	path_put(&nd.path);
35821da177e4SLinus Torvalds 	putname(name);
3583c6ee9206SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3584c6ee9206SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3585c6ee9206SJeff Layton 		goto retry;
3586c6ee9206SJeff Layton 	}
35871da177e4SLinus Torvalds 	return error;
35881da177e4SLinus Torvalds }
35891da177e4SLinus Torvalds 
35903cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
35915590ff0dSUlrich Drepper {
35925590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
35935590ff0dSUlrich Drepper }
35945590ff0dSUlrich Drepper 
3595b21996e3SJ. Bruce Fields /**
3596b21996e3SJ. Bruce Fields  * vfs_unlink - unlink a filesystem object
3597b21996e3SJ. Bruce Fields  * @dir:	parent directory
3598b21996e3SJ. Bruce Fields  * @dentry:	victim
3599b21996e3SJ. Bruce Fields  * @delegated_inode: returns victim inode, if the inode is delegated.
3600b21996e3SJ. Bruce Fields  *
3601b21996e3SJ. Bruce Fields  * The caller must hold dir->i_mutex.
3602b21996e3SJ. Bruce Fields  *
3603b21996e3SJ. Bruce Fields  * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3604b21996e3SJ. Bruce Fields  * return a reference to the inode in delegated_inode.  The caller
3605b21996e3SJ. Bruce Fields  * should then break the delegation on that inode and retry.  Because
3606b21996e3SJ. Bruce Fields  * breaking a delegation may take a long time, the caller should drop
3607b21996e3SJ. Bruce Fields  * dir->i_mutex before doing so.
3608b21996e3SJ. Bruce Fields  *
3609b21996e3SJ. Bruce Fields  * Alternatively, a caller may pass NULL for delegated_inode.  This may
3610b21996e3SJ. Bruce Fields  * be appropriate for callers that expect the underlying filesystem not
3611b21996e3SJ. Bruce Fields  * to be NFS exported.
3612b21996e3SJ. Bruce Fields  */
3613b21996e3SJ. Bruce Fields int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
36141da177e4SLinus Torvalds {
36159accbb97SJ. Bruce Fields 	struct inode *target = dentry->d_inode;
36161da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
36171da177e4SLinus Torvalds 
36181da177e4SLinus Torvalds 	if (error)
36191da177e4SLinus Torvalds 		return error;
36201da177e4SLinus Torvalds 
3621acfa4380SAl Viro 	if (!dir->i_op->unlink)
36221da177e4SLinus Torvalds 		return -EPERM;
36231da177e4SLinus Torvalds 
36249accbb97SJ. Bruce Fields 	mutex_lock(&target->i_mutex);
36251da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
36261da177e4SLinus Torvalds 		error = -EBUSY;
36271da177e4SLinus Torvalds 	else {
36281da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
3629bec1052eSAl Viro 		if (!error) {
36305a14696cSJ. Bruce Fields 			error = try_break_deleg(target, delegated_inode);
36315a14696cSJ. Bruce Fields 			if (error)
3632b21996e3SJ. Bruce Fields 				goto out;
36331da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
3634bec1052eSAl Viro 			if (!error)
3635d83c49f3SAl Viro 				dont_mount(dentry);
3636bec1052eSAl Viro 		}
36371da177e4SLinus Torvalds 	}
3638b21996e3SJ. Bruce Fields out:
36399accbb97SJ. Bruce Fields 	mutex_unlock(&target->i_mutex);
36401da177e4SLinus Torvalds 
36411da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
36421da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
36439accbb97SJ. Bruce Fields 		fsnotify_link_count(target);
36441da177e4SLinus Torvalds 		d_delete(dentry);
36451da177e4SLinus Torvalds 	}
36460eeca283SRobert Love 
36471da177e4SLinus Torvalds 	return error;
36481da177e4SLinus Torvalds }
36491da177e4SLinus Torvalds 
36501da177e4SLinus Torvalds /*
36511da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
36521b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
36531da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
36541da177e4SLinus Torvalds  * while waiting on the I/O.
36551da177e4SLinus Torvalds  */
36565590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
36571da177e4SLinus Torvalds {
36582ad94ae6SAl Viro 	int error;
365991a27b2aSJeff Layton 	struct filename *name;
36601da177e4SLinus Torvalds 	struct dentry *dentry;
36611da177e4SLinus Torvalds 	struct nameidata nd;
36621da177e4SLinus Torvalds 	struct inode *inode = NULL;
3663b21996e3SJ. Bruce Fields 	struct inode *delegated_inode = NULL;
36645d18f813SJeff Layton 	unsigned int lookup_flags = 0;
36655d18f813SJeff Layton retry:
36665d18f813SJeff Layton 	name = user_path_parent(dfd, pathname, &nd, lookup_flags);
366791a27b2aSJeff Layton 	if (IS_ERR(name))
366891a27b2aSJeff Layton 		return PTR_ERR(name);
36692ad94ae6SAl Viro 
36701da177e4SLinus Torvalds 	error = -EISDIR;
36711da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
36721da177e4SLinus Torvalds 		goto exit1;
36730612d9fbSOGAWA Hirofumi 
36740612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
3675c30dabfeSJan Kara 	error = mnt_want_write(nd.path.mnt);
3676c30dabfeSJan Kara 	if (error)
3677c30dabfeSJan Kara 		goto exit1;
3678b21996e3SJ. Bruce Fields retry_deleg:
36794ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
368049705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
36811da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
36821da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
36831da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
368450338b88STörök Edwin 		if (nd.last.name[nd.last.len])
368550338b88STörök Edwin 			goto slashes;
36861da177e4SLinus Torvalds 		inode = dentry->d_inode;
3687b18825a7SDavid Howells 		if (d_is_negative(dentry))
3688e6bc45d6STheodore Ts'o 			goto slashes;
36897de9c6eeSAl Viro 		ihold(inode);
3690be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
3691be6d3e56SKentaro Takeda 		if (error)
3692c30dabfeSJan Kara 			goto exit2;
3693b21996e3SJ. Bruce Fields 		error = vfs_unlink(nd.path.dentry->d_inode, dentry, &delegated_inode);
36941da177e4SLinus Torvalds exit2:
36951da177e4SLinus Torvalds 		dput(dentry);
36961da177e4SLinus Torvalds 	}
36974ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
36981da177e4SLinus Torvalds 	if (inode)
36991da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
3700b21996e3SJ. Bruce Fields 	inode = NULL;
3701b21996e3SJ. Bruce Fields 	if (delegated_inode) {
37025a14696cSJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
3703b21996e3SJ. Bruce Fields 		if (!error)
3704b21996e3SJ. Bruce Fields 			goto retry_deleg;
3705b21996e3SJ. Bruce Fields 	}
3706c30dabfeSJan Kara 	mnt_drop_write(nd.path.mnt);
37071da177e4SLinus Torvalds exit1:
37081d957f9bSJan Blunck 	path_put(&nd.path);
37091da177e4SLinus Torvalds 	putname(name);
37105d18f813SJeff Layton 	if (retry_estale(error, lookup_flags)) {
37115d18f813SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
37125d18f813SJeff Layton 		inode = NULL;
37135d18f813SJeff Layton 		goto retry;
37145d18f813SJeff Layton 	}
37151da177e4SLinus Torvalds 	return error;
37161da177e4SLinus Torvalds 
37171da177e4SLinus Torvalds slashes:
3718b18825a7SDavid Howells 	if (d_is_negative(dentry))
3719b18825a7SDavid Howells 		error = -ENOENT;
3720b18825a7SDavid Howells 	else if (d_is_directory(dentry) || d_is_autodir(dentry))
3721b18825a7SDavid Howells 		error = -EISDIR;
3722b18825a7SDavid Howells 	else
3723b18825a7SDavid Howells 		error = -ENOTDIR;
37241da177e4SLinus Torvalds 	goto exit2;
37251da177e4SLinus Torvalds }
37261da177e4SLinus Torvalds 
37272e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
37285590ff0dSUlrich Drepper {
37295590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
37305590ff0dSUlrich Drepper 		return -EINVAL;
37315590ff0dSUlrich Drepper 
37325590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
37335590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
37345590ff0dSUlrich Drepper 
37355590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
37365590ff0dSUlrich Drepper }
37375590ff0dSUlrich Drepper 
37383480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
37395590ff0dSUlrich Drepper {
37405590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
37415590ff0dSUlrich Drepper }
37425590ff0dSUlrich Drepper 
3743db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
37441da177e4SLinus Torvalds {
3745a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
37461da177e4SLinus Torvalds 
37471da177e4SLinus Torvalds 	if (error)
37481da177e4SLinus Torvalds 		return error;
37491da177e4SLinus Torvalds 
3750acfa4380SAl Viro 	if (!dir->i_op->symlink)
37511da177e4SLinus Torvalds 		return -EPERM;
37521da177e4SLinus Torvalds 
37531da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
37541da177e4SLinus Torvalds 	if (error)
37551da177e4SLinus Torvalds 		return error;
37561da177e4SLinus Torvalds 
37571da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
3758a74574aaSStephen Smalley 	if (!error)
3759f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
37601da177e4SLinus Torvalds 	return error;
37611da177e4SLinus Torvalds }
37621da177e4SLinus Torvalds 
37632e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
37642e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
37651da177e4SLinus Torvalds {
37662ad94ae6SAl Viro 	int error;
376791a27b2aSJeff Layton 	struct filename *from;
37686902d925SDave Hansen 	struct dentry *dentry;
3769dae6ad8fSAl Viro 	struct path path;
3770f46d3567SJeff Layton 	unsigned int lookup_flags = 0;
37711da177e4SLinus Torvalds 
37721da177e4SLinus Torvalds 	from = getname(oldname);
37731da177e4SLinus Torvalds 	if (IS_ERR(from))
37741da177e4SLinus Torvalds 		return PTR_ERR(from);
3775f46d3567SJeff Layton retry:
3776f46d3567SJeff Layton 	dentry = user_path_create(newdfd, newname, &path, lookup_flags);
37771da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
37786902d925SDave Hansen 	if (IS_ERR(dentry))
3779dae6ad8fSAl Viro 		goto out_putname;
37806902d925SDave Hansen 
378191a27b2aSJeff Layton 	error = security_path_symlink(&path, dentry, from->name);
3782a8104a9fSAl Viro 	if (!error)
378391a27b2aSJeff Layton 		error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
3784921a1650SAl Viro 	done_path_create(&path, dentry);
3785f46d3567SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3786f46d3567SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3787f46d3567SJeff Layton 		goto retry;
3788f46d3567SJeff Layton 	}
37896902d925SDave Hansen out_putname:
37901da177e4SLinus Torvalds 	putname(from);
37911da177e4SLinus Torvalds 	return error;
37921da177e4SLinus Torvalds }
37931da177e4SLinus Torvalds 
37943480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
37955590ff0dSUlrich Drepper {
37965590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
37975590ff0dSUlrich Drepper }
37985590ff0dSUlrich Drepper 
3799146a8595SJ. Bruce Fields /**
3800146a8595SJ. Bruce Fields  * vfs_link - create a new link
3801146a8595SJ. Bruce Fields  * @old_dentry:	object to be linked
3802146a8595SJ. Bruce Fields  * @dir:	new parent
3803146a8595SJ. Bruce Fields  * @new_dentry:	where to create the new link
3804146a8595SJ. Bruce Fields  * @delegated_inode: returns inode needing a delegation break
3805146a8595SJ. Bruce Fields  *
3806146a8595SJ. Bruce Fields  * The caller must hold dir->i_mutex
3807146a8595SJ. Bruce Fields  *
3808146a8595SJ. Bruce Fields  * If vfs_link discovers a delegation on the to-be-linked file in need
3809146a8595SJ. Bruce Fields  * of breaking, it will return -EWOULDBLOCK and return a reference to the
3810146a8595SJ. Bruce Fields  * inode in delegated_inode.  The caller should then break the delegation
3811146a8595SJ. Bruce Fields  * and retry.  Because breaking a delegation may take a long time, the
3812146a8595SJ. Bruce Fields  * caller should drop the i_mutex before doing so.
3813146a8595SJ. Bruce Fields  *
3814146a8595SJ. Bruce Fields  * Alternatively, a caller may pass NULL for delegated_inode.  This may
3815146a8595SJ. Bruce Fields  * be appropriate for callers that expect the underlying filesystem not
3816146a8595SJ. Bruce Fields  * to be NFS exported.
3817146a8595SJ. Bruce Fields  */
3818146a8595SJ. Bruce Fields int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
38191da177e4SLinus Torvalds {
38201da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
38218de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
38221da177e4SLinus Torvalds 	int error;
38231da177e4SLinus Torvalds 
38241da177e4SLinus Torvalds 	if (!inode)
38251da177e4SLinus Torvalds 		return -ENOENT;
38261da177e4SLinus Torvalds 
3827a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
38281da177e4SLinus Torvalds 	if (error)
38291da177e4SLinus Torvalds 		return error;
38301da177e4SLinus Torvalds 
38311da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
38321da177e4SLinus Torvalds 		return -EXDEV;
38331da177e4SLinus Torvalds 
38341da177e4SLinus Torvalds 	/*
38351da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
38361da177e4SLinus Torvalds 	 */
38371da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
38381da177e4SLinus Torvalds 		return -EPERM;
3839acfa4380SAl Viro 	if (!dir->i_op->link)
38401da177e4SLinus Torvalds 		return -EPERM;
38417e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
38421da177e4SLinus Torvalds 		return -EPERM;
38431da177e4SLinus Torvalds 
38441da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
38451da177e4SLinus Torvalds 	if (error)
38461da177e4SLinus Torvalds 		return error;
38471da177e4SLinus Torvalds 
38487e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
3849aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
3850f4e0c30cSAl Viro 	if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
3851aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
38528de52778SAl Viro 	else if (max_links && inode->i_nlink >= max_links)
38538de52778SAl Viro 		error = -EMLINK;
3854146a8595SJ. Bruce Fields 	else {
3855146a8595SJ. Bruce Fields 		error = try_break_deleg(inode, delegated_inode);
3856146a8595SJ. Bruce Fields 		if (!error)
38571da177e4SLinus Torvalds 			error = dir->i_op->link(old_dentry, dir, new_dentry);
3858146a8595SJ. Bruce Fields 	}
3859f4e0c30cSAl Viro 
3860f4e0c30cSAl Viro 	if (!error && (inode->i_state & I_LINKABLE)) {
3861f4e0c30cSAl Viro 		spin_lock(&inode->i_lock);
3862f4e0c30cSAl Viro 		inode->i_state &= ~I_LINKABLE;
3863f4e0c30cSAl Viro 		spin_unlock(&inode->i_lock);
3864f4e0c30cSAl Viro 	}
38657e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
3866e31e14ecSStephen Smalley 	if (!error)
38677e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
38681da177e4SLinus Torvalds 	return error;
38691da177e4SLinus Torvalds }
38701da177e4SLinus Torvalds 
38711da177e4SLinus Torvalds /*
38721da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
38731da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
38741da177e4SLinus Torvalds  * newname.  --KAB
38751da177e4SLinus Torvalds  *
38761da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
38771da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
38781da177e4SLinus Torvalds  * and other special files.  --ADM
38791da177e4SLinus Torvalds  */
38802e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
38812e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
38821da177e4SLinus Torvalds {
38831da177e4SLinus Torvalds 	struct dentry *new_dentry;
3884dae6ad8fSAl Viro 	struct path old_path, new_path;
3885146a8595SJ. Bruce Fields 	struct inode *delegated_inode = NULL;
388611a7b371SAneesh Kumar K.V 	int how = 0;
38871da177e4SLinus Torvalds 	int error;
38881da177e4SLinus Torvalds 
388911a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3890c04030e1SUlrich Drepper 		return -EINVAL;
389111a7b371SAneesh Kumar K.V 	/*
3892f0cc6ffbSLinus Torvalds 	 * To use null names we require CAP_DAC_READ_SEARCH
3893f0cc6ffbSLinus Torvalds 	 * This ensures that not everyone will be able to create
3894f0cc6ffbSLinus Torvalds 	 * handlink using the passed filedescriptor.
389511a7b371SAneesh Kumar K.V 	 */
3896f0cc6ffbSLinus Torvalds 	if (flags & AT_EMPTY_PATH) {
3897f0cc6ffbSLinus Torvalds 		if (!capable(CAP_DAC_READ_SEARCH))
3898f0cc6ffbSLinus Torvalds 			return -ENOENT;
389911a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
3900f0cc6ffbSLinus Torvalds 	}
3901c04030e1SUlrich Drepper 
390211a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
390311a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
3904442e31caSJeff Layton retry:
390511a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
39061da177e4SLinus Torvalds 	if (error)
39072ad94ae6SAl Viro 		return error;
39082ad94ae6SAl Viro 
3909442e31caSJeff Layton 	new_dentry = user_path_create(newdfd, newname, &new_path,
3910442e31caSJeff Layton 					(how & LOOKUP_REVAL));
39111da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
39126902d925SDave Hansen 	if (IS_ERR(new_dentry))
3913dae6ad8fSAl Viro 		goto out;
3914dae6ad8fSAl Viro 
3915dae6ad8fSAl Viro 	error = -EXDEV;
3916dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
3917dae6ad8fSAl Viro 		goto out_dput;
3918800179c9SKees Cook 	error = may_linkat(&old_path);
3919800179c9SKees Cook 	if (unlikely(error))
3920800179c9SKees Cook 		goto out_dput;
3921dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
3922be6d3e56SKentaro Takeda 	if (error)
3923a8104a9fSAl Viro 		goto out_dput;
3924146a8595SJ. Bruce Fields 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
392575c3f29dSDave Hansen out_dput:
3926921a1650SAl Viro 	done_path_create(&new_path, new_dentry);
3927146a8595SJ. Bruce Fields 	if (delegated_inode) {
3928146a8595SJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
3929d22e6338SOleg Drokin 		if (!error) {
3930d22e6338SOleg Drokin 			path_put(&old_path);
3931146a8595SJ. Bruce Fields 			goto retry;
3932146a8595SJ. Bruce Fields 		}
3933d22e6338SOleg Drokin 	}
3934442e31caSJeff Layton 	if (retry_estale(error, how)) {
3935d22e6338SOleg Drokin 		path_put(&old_path);
3936442e31caSJeff Layton 		how |= LOOKUP_REVAL;
3937442e31caSJeff Layton 		goto retry;
3938442e31caSJeff Layton 	}
39391da177e4SLinus Torvalds out:
39402d8f3038SAl Viro 	path_put(&old_path);
39411da177e4SLinus Torvalds 
39421da177e4SLinus Torvalds 	return error;
39431da177e4SLinus Torvalds }
39441da177e4SLinus Torvalds 
39453480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
39465590ff0dSUlrich Drepper {
3947c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
39485590ff0dSUlrich Drepper }
39495590ff0dSUlrich Drepper 
39501da177e4SLinus Torvalds /*
39511da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
39521da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
39531da177e4SLinus Torvalds  * Problems:
39541da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
39551da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
39561da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
3957a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
39581da177e4SLinus Torvalds  *	   story.
39596cedba89SJ. Bruce Fields  *	c) we have to lock _four_ objects - parents and victim (if it exists),
39606cedba89SJ. Bruce Fields  *	   and source (if it is not a directory).
39611b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
39621da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
39631da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
3964a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
39651da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
39661da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
39671da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
3968a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
39691da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
39701da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
39711da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
3972e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
39731b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
39741da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3975c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
39761da177e4SLinus Torvalds  *	   locking].
39771da177e4SLinus Torvalds  */
397875c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
39791da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
39801da177e4SLinus Torvalds {
39811da177e4SLinus Torvalds 	int error = 0;
39829055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
39838de52778SAl Viro 	unsigned max_links = new_dir->i_sb->s_max_links;
39841da177e4SLinus Torvalds 
39851da177e4SLinus Torvalds 	/*
39861da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
39871da177e4SLinus Torvalds 	 * we'll need to flip '..'.
39881da177e4SLinus Torvalds 	 */
39891da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3990f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
39911da177e4SLinus Torvalds 		if (error)
39921da177e4SLinus Torvalds 			return error;
39931da177e4SLinus Torvalds 	}
39941da177e4SLinus Torvalds 
39951da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
39961da177e4SLinus Torvalds 	if (error)
39971da177e4SLinus Torvalds 		return error;
39981da177e4SLinus Torvalds 
39991d2ef590SAl Viro 	dget(new_dentry);
4000d83c49f3SAl Viro 	if (target)
40011b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
40029055cba7SSage Weil 
40031da177e4SLinus Torvalds 	error = -EBUSY;
40049055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
40059055cba7SSage Weil 		goto out;
40069055cba7SSage Weil 
40078de52778SAl Viro 	error = -EMLINK;
40088de52778SAl Viro 	if (max_links && !target && new_dir != old_dir &&
40098de52778SAl Viro 	    new_dir->i_nlink >= max_links)
40108de52778SAl Viro 		goto out;
40118de52778SAl Viro 
40123cebde24SSage Weil 	if (target)
40133cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
40141da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
40159055cba7SSage Weil 	if (error)
40169055cba7SSage Weil 		goto out;
40179055cba7SSage Weil 
40181da177e4SLinus Torvalds 	if (target) {
40191da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
4020d83c49f3SAl Viro 		dont_mount(new_dentry);
4021d83c49f3SAl Viro 	}
40229055cba7SSage Weil out:
40239055cba7SSage Weil 	if (target)
40241b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
40251d2ef590SAl Viro 	dput(new_dentry);
4026e31e14ecSStephen Smalley 	if (!error)
4027349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
40281da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
40291da177e4SLinus Torvalds 	return error;
40301da177e4SLinus Torvalds }
40311da177e4SLinus Torvalds 
403275c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
40338e6d782cSJ. Bruce Fields 			    struct inode *new_dir, struct dentry *new_dentry,
40348e6d782cSJ. Bruce Fields 			    struct inode **delegated_inode)
40351da177e4SLinus Torvalds {
403651892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
40376cedba89SJ. Bruce Fields 	struct inode *source = old_dentry->d_inode;
40381da177e4SLinus Torvalds 	int error;
40391da177e4SLinus Torvalds 
40401da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
40411da177e4SLinus Torvalds 	if (error)
40421da177e4SLinus Torvalds 		return error;
40431da177e4SLinus Torvalds 
40441da177e4SLinus Torvalds 	dget(new_dentry);
40456cedba89SJ. Bruce Fields 	lock_two_nondirectories(source, target);
404651892bbbSSage Weil 
40471da177e4SLinus Torvalds 	error = -EBUSY;
404851892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
404951892bbbSSage Weil 		goto out;
405051892bbbSSage Weil 
40518e6d782cSJ. Bruce Fields 	error = try_break_deleg(source, delegated_inode);
40528e6d782cSJ. Bruce Fields 	if (error)
40538e6d782cSJ. Bruce Fields 		goto out;
40548e6d782cSJ. Bruce Fields 	if (target) {
40558e6d782cSJ. Bruce Fields 		error = try_break_deleg(target, delegated_inode);
40568e6d782cSJ. Bruce Fields 		if (error)
40578e6d782cSJ. Bruce Fields 			goto out;
40588e6d782cSJ. Bruce Fields 	}
40591da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
406051892bbbSSage Weil 	if (error)
406151892bbbSSage Weil 		goto out;
406251892bbbSSage Weil 
4063bec1052eSAl Viro 	if (target)
4064d83c49f3SAl Viro 		dont_mount(new_dentry);
4065349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
40661da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
406751892bbbSSage Weil out:
40686cedba89SJ. Bruce Fields 	unlock_two_nondirectories(source, target);
40691da177e4SLinus Torvalds 	dput(new_dentry);
40701da177e4SLinus Torvalds 	return error;
40711da177e4SLinus Torvalds }
40721da177e4SLinus Torvalds 
40738e6d782cSJ. Bruce Fields /**
40748e6d782cSJ. Bruce Fields  * vfs_rename - rename a filesystem object
40758e6d782cSJ. Bruce Fields  * @old_dir:	parent of source
40768e6d782cSJ. Bruce Fields  * @old_dentry:	source
40778e6d782cSJ. Bruce Fields  * @new_dir:	parent of destination
40788e6d782cSJ. Bruce Fields  * @new_dentry:	destination
40798e6d782cSJ. Bruce Fields  * @delegated_inode: returns an inode needing a delegation break
40808e6d782cSJ. Bruce Fields  *
40818e6d782cSJ. Bruce Fields  * The caller must hold multiple mutexes--see lock_rename()).
40828e6d782cSJ. Bruce Fields  *
40838e6d782cSJ. Bruce Fields  * If vfs_rename discovers a delegation in need of breaking at either
40848e6d782cSJ. Bruce Fields  * the source or destination, it will return -EWOULDBLOCK and return a
40858e6d782cSJ. Bruce Fields  * reference to the inode in delegated_inode.  The caller should then
40868e6d782cSJ. Bruce Fields  * break the delegation and retry.  Because breaking a delegation may
40878e6d782cSJ. Bruce Fields  * take a long time, the caller should drop all locks before doing
40888e6d782cSJ. Bruce Fields  * so.
40898e6d782cSJ. Bruce Fields  *
40908e6d782cSJ. Bruce Fields  * Alternatively, a caller may pass NULL for delegated_inode.  This may
40918e6d782cSJ. Bruce Fields  * be appropriate for callers that expect the underlying filesystem not
40928e6d782cSJ. Bruce Fields  * to be NFS exported.
40938e6d782cSJ. Bruce Fields  */
40941da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
40958e6d782cSJ. Bruce Fields 	       struct inode *new_dir, struct dentry *new_dentry,
40968e6d782cSJ. Bruce Fields 	       struct inode **delegated_inode)
40971da177e4SLinus Torvalds {
40981da177e4SLinus Torvalds 	int error;
4099b18825a7SDavid Howells 	int is_dir = d_is_directory(old_dentry) || d_is_autodir(old_dentry);
410059b0df21SEric Paris 	const unsigned char *old_name;
41011da177e4SLinus Torvalds 
41021da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
41031da177e4SLinus Torvalds  		return 0;
41041da177e4SLinus Torvalds 
41051da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
41061da177e4SLinus Torvalds 	if (error)
41071da177e4SLinus Torvalds 		return error;
41081da177e4SLinus Torvalds 
41091da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
4110a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
41111da177e4SLinus Torvalds 	else
41121da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
41131da177e4SLinus Torvalds 	if (error)
41141da177e4SLinus Torvalds 		return error;
41151da177e4SLinus Torvalds 
4116acfa4380SAl Viro 	if (!old_dir->i_op->rename)
41171da177e4SLinus Torvalds 		return -EPERM;
41181da177e4SLinus Torvalds 
41190eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
41200eeca283SRobert Love 
41211da177e4SLinus Torvalds 	if (is_dir)
41221da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
41231da177e4SLinus Torvalds 	else
41248e6d782cSJ. Bruce Fields 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry,delegated_inode);
4125123df294SAl Viro 	if (!error)
4126123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
41275a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
41280eeca283SRobert Love 	fsnotify_oldname_free(old_name);
41290eeca283SRobert Love 
41301da177e4SLinus Torvalds 	return error;
41311da177e4SLinus Torvalds }
41321da177e4SLinus Torvalds 
41332e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
41342e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
41351da177e4SLinus Torvalds {
41361da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
41371da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
41381da177e4SLinus Torvalds 	struct dentry *trap;
41391da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
41408e6d782cSJ. Bruce Fields 	struct inode *delegated_inode = NULL;
414191a27b2aSJeff Layton 	struct filename *from;
414291a27b2aSJeff Layton 	struct filename *to;
4143c6a94284SJeff Layton 	unsigned int lookup_flags = 0;
4144c6a94284SJeff Layton 	bool should_retry = false;
41452ad94ae6SAl Viro 	int error;
4146c6a94284SJeff Layton retry:
4147c6a94284SJeff Layton 	from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
414891a27b2aSJeff Layton 	if (IS_ERR(from)) {
414991a27b2aSJeff Layton 		error = PTR_ERR(from);
41501da177e4SLinus Torvalds 		goto exit;
415191a27b2aSJeff Layton 	}
41521da177e4SLinus Torvalds 
4153c6a94284SJeff Layton 	to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
415491a27b2aSJeff Layton 	if (IS_ERR(to)) {
415591a27b2aSJeff Layton 		error = PTR_ERR(to);
41561da177e4SLinus Torvalds 		goto exit1;
415791a27b2aSJeff Layton 	}
41581da177e4SLinus Torvalds 
41591da177e4SLinus Torvalds 	error = -EXDEV;
41604ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
41611da177e4SLinus Torvalds 		goto exit2;
41621da177e4SLinus Torvalds 
41634ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
41641da177e4SLinus Torvalds 	error = -EBUSY;
41651da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
41661da177e4SLinus Torvalds 		goto exit2;
41671da177e4SLinus Torvalds 
41684ac91378SJan Blunck 	new_dir = newnd.path.dentry;
41691da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
41701da177e4SLinus Torvalds 		goto exit2;
41711da177e4SLinus Torvalds 
4172c30dabfeSJan Kara 	error = mnt_want_write(oldnd.path.mnt);
4173c30dabfeSJan Kara 	if (error)
4174c30dabfeSJan Kara 		goto exit2;
4175c30dabfeSJan Kara 
41760612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
41770612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
41784e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
41790612d9fbSOGAWA Hirofumi 
41808e6d782cSJ. Bruce Fields retry_deleg:
41811da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
41821da177e4SLinus Torvalds 
418349705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
41841da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
41851da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
41861da177e4SLinus Torvalds 		goto exit3;
41871da177e4SLinus Torvalds 	/* source must exist */
41881da177e4SLinus Torvalds 	error = -ENOENT;
4189b18825a7SDavid Howells 	if (d_is_negative(old_dentry))
41901da177e4SLinus Torvalds 		goto exit4;
41911da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
4192b18825a7SDavid Howells 	if (!d_is_directory(old_dentry) && !d_is_autodir(old_dentry)) {
41931da177e4SLinus Torvalds 		error = -ENOTDIR;
41941da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
41951da177e4SLinus Torvalds 			goto exit4;
41961da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
41971da177e4SLinus Torvalds 			goto exit4;
41981da177e4SLinus Torvalds 	}
41991da177e4SLinus Torvalds 	/* source should not be ancestor of target */
42001da177e4SLinus Torvalds 	error = -EINVAL;
42011da177e4SLinus Torvalds 	if (old_dentry == trap)
42021da177e4SLinus Torvalds 		goto exit4;
420349705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
42041da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
42051da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
42061da177e4SLinus Torvalds 		goto exit4;
42071da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
42081da177e4SLinus Torvalds 	error = -ENOTEMPTY;
42091da177e4SLinus Torvalds 	if (new_dentry == trap)
42101da177e4SLinus Torvalds 		goto exit5;
42111da177e4SLinus Torvalds 
4212be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
4213be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
4214be6d3e56SKentaro Takeda 	if (error)
4215c30dabfeSJan Kara 		goto exit5;
42161da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
42178e6d782cSJ. Bruce Fields 				   new_dir->d_inode, new_dentry,
42188e6d782cSJ. Bruce Fields 				   &delegated_inode);
42191da177e4SLinus Torvalds exit5:
42201da177e4SLinus Torvalds 	dput(new_dentry);
42211da177e4SLinus Torvalds exit4:
42221da177e4SLinus Torvalds 	dput(old_dentry);
42231da177e4SLinus Torvalds exit3:
42241da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
42258e6d782cSJ. Bruce Fields 	if (delegated_inode) {
42268e6d782cSJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
42278e6d782cSJ. Bruce Fields 		if (!error)
42288e6d782cSJ. Bruce Fields 			goto retry_deleg;
42298e6d782cSJ. Bruce Fields 	}
4230c30dabfeSJan Kara 	mnt_drop_write(oldnd.path.mnt);
42311da177e4SLinus Torvalds exit2:
4232c6a94284SJeff Layton 	if (retry_estale(error, lookup_flags))
4233c6a94284SJeff Layton 		should_retry = true;
42341d957f9bSJan Blunck 	path_put(&newnd.path);
42352ad94ae6SAl Viro 	putname(to);
42361da177e4SLinus Torvalds exit1:
42371d957f9bSJan Blunck 	path_put(&oldnd.path);
42381da177e4SLinus Torvalds 	putname(from);
4239c6a94284SJeff Layton 	if (should_retry) {
4240c6a94284SJeff Layton 		should_retry = false;
4241c6a94284SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
4242c6a94284SJeff Layton 		goto retry;
4243c6a94284SJeff Layton 	}
42442ad94ae6SAl Viro exit:
42451da177e4SLinus Torvalds 	return error;
42461da177e4SLinus Torvalds }
42471da177e4SLinus Torvalds 
4248a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
42495590ff0dSUlrich Drepper {
42505590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
42515590ff0dSUlrich Drepper }
42525590ff0dSUlrich Drepper 
42531da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
42541da177e4SLinus Torvalds {
42551da177e4SLinus Torvalds 	int len;
42561da177e4SLinus Torvalds 
42571da177e4SLinus Torvalds 	len = PTR_ERR(link);
42581da177e4SLinus Torvalds 	if (IS_ERR(link))
42591da177e4SLinus Torvalds 		goto out;
42601da177e4SLinus Torvalds 
42611da177e4SLinus Torvalds 	len = strlen(link);
42621da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
42631da177e4SLinus Torvalds 		len = buflen;
42641da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
42651da177e4SLinus Torvalds 		len = -EFAULT;
42661da177e4SLinus Torvalds out:
42671da177e4SLinus Torvalds 	return len;
42681da177e4SLinus Torvalds }
42691da177e4SLinus Torvalds 
42701da177e4SLinus Torvalds /*
42711da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
42721da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
42731da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
42741da177e4SLinus Torvalds  */
42751da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
42761da177e4SLinus Torvalds {
42771da177e4SLinus Torvalds 	struct nameidata nd;
4278cc314eefSLinus Torvalds 	void *cookie;
4279694a1764SMarcin Slusarz 	int res;
4280cc314eefSLinus Torvalds 
42811da177e4SLinus Torvalds 	nd.depth = 0;
4282cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
4283694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
4284694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
4285694a1764SMarcin Slusarz 
4286694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
42871da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
4288cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4289694a1764SMarcin Slusarz 	return res;
42901da177e4SLinus Torvalds }
42911da177e4SLinus Torvalds 
42921da177e4SLinus Torvalds /* get the link contents into pagecache */
42931da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
42941da177e4SLinus Torvalds {
4295ebd09abbSDuane Griffin 	char *kaddr;
42961da177e4SLinus Torvalds 	struct page *page;
42971da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
4298090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
42991da177e4SLinus Torvalds 	if (IS_ERR(page))
43006fe6900eSNick Piggin 		return (char*)page;
43011da177e4SLinus Torvalds 	*ppage = page;
4302ebd09abbSDuane Griffin 	kaddr = kmap(page);
4303ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4304ebd09abbSDuane Griffin 	return kaddr;
43051da177e4SLinus Torvalds }
43061da177e4SLinus Torvalds 
43071da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
43081da177e4SLinus Torvalds {
43091da177e4SLinus Torvalds 	struct page *page = NULL;
43101da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
43111da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
43121da177e4SLinus Torvalds 	if (page) {
43131da177e4SLinus Torvalds 		kunmap(page);
43141da177e4SLinus Torvalds 		page_cache_release(page);
43151da177e4SLinus Torvalds 	}
43161da177e4SLinus Torvalds 	return res;
43171da177e4SLinus Torvalds }
43181da177e4SLinus Torvalds 
4319cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
43201da177e4SLinus Torvalds {
4321cc314eefSLinus Torvalds 	struct page *page = NULL;
43221da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
4323cc314eefSLinus Torvalds 	return page;
43241da177e4SLinus Torvalds }
43251da177e4SLinus Torvalds 
4326cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
43271da177e4SLinus Torvalds {
4328cc314eefSLinus Torvalds 	struct page *page = cookie;
4329cc314eefSLinus Torvalds 
4330cc314eefSLinus Torvalds 	if (page) {
43311da177e4SLinus Torvalds 		kunmap(page);
43321da177e4SLinus Torvalds 		page_cache_release(page);
43331da177e4SLinus Torvalds 	}
43341da177e4SLinus Torvalds }
43351da177e4SLinus Torvalds 
433654566b2cSNick Piggin /*
433754566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
433854566b2cSNick Piggin  */
433954566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
43401da177e4SLinus Torvalds {
43411da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
43420adb25d2SKirill Korotaev 	struct page *page;
4343afddba49SNick Piggin 	void *fsdata;
4344beb497abSDmitriy Monakhov 	int err;
43451da177e4SLinus Torvalds 	char *kaddr;
434654566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
434754566b2cSNick Piggin 	if (nofs)
434854566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
43491da177e4SLinus Torvalds 
43507e53cac4SNeilBrown retry:
4351afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
435254566b2cSNick Piggin 				flags, &page, &fsdata);
43531da177e4SLinus Torvalds 	if (err)
4354afddba49SNick Piggin 		goto fail;
4355afddba49SNick Piggin 
4356e8e3c3d6SCong Wang 	kaddr = kmap_atomic(page);
43571da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
4358e8e3c3d6SCong Wang 	kunmap_atomic(kaddr);
4359afddba49SNick Piggin 
4360afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4361afddba49SNick Piggin 							page, fsdata);
43621da177e4SLinus Torvalds 	if (err < 0)
43631da177e4SLinus Torvalds 		goto fail;
4364afddba49SNick Piggin 	if (err < len-1)
4365afddba49SNick Piggin 		goto retry;
4366afddba49SNick Piggin 
43671da177e4SLinus Torvalds 	mark_inode_dirty(inode);
43681da177e4SLinus Torvalds 	return 0;
43691da177e4SLinus Torvalds fail:
43701da177e4SLinus Torvalds 	return err;
43711da177e4SLinus Torvalds }
43721da177e4SLinus Torvalds 
43730adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
43740adb25d2SKirill Korotaev {
43750adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
437654566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
43770adb25d2SKirill Korotaev }
43780adb25d2SKirill Korotaev 
437992e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
43801da177e4SLinus Torvalds 	.readlink	= generic_readlink,
43811da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
43821da177e4SLinus Torvalds 	.put_link	= page_put_link,
43831da177e4SLinus Torvalds };
43841da177e4SLinus Torvalds 
43852d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
4386cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
43871da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
43881da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
4389f6d2ac5cSAl Viro EXPORT_SYMBOL(get_write_access); /* nfsd */
43901da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
43911da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
43921da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
43931da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
43941da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
43950adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
43961da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
43971da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
4398d1811465SAl Viro EXPORT_SYMBOL(kern_path);
439916f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
4400f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
44011da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
44021da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
44031da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
44041da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
44051da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
44061da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
44071da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
44081da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
44091da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
44101da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
44111da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
44121da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
44131da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
4414