xref: /openbmc/linux/fs/namei.c (revision d0d27277)
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 }
21291a27b2aSJeff Layton EXPORT_SYMBOL(getname);
213f52e0c11SAl Viro 
2141da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
21591a27b2aSJeff Layton void putname(struct filename *name)
2161da177e4SLinus Torvalds {
2175ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
21891a27b2aSJeff Layton 		return audit_putname(name);
21991a27b2aSJeff Layton 	final_putname(name);
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds #endif
2221da177e4SLinus Torvalds 
223e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask)
224e77819e5SLinus Torvalds {
22584635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL
226e77819e5SLinus Torvalds 	struct posix_acl *acl;
227e77819e5SLinus Torvalds 
228e77819e5SLinus Torvalds 	if (mask & MAY_NOT_BLOCK) {
2293567866bSAl Viro 		acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
2303567866bSAl Viro 	        if (!acl)
231e77819e5SLinus Torvalds 	                return -EAGAIN;
2323567866bSAl Viro 		/* no ->get_acl() calls in RCU mode... */
2333567866bSAl Viro 		if (acl == ACL_NOT_CACHED)
234e77819e5SLinus Torvalds 			return -ECHILD;
235206b1d09SAri Savolainen 	        return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
236e77819e5SLinus Torvalds 	}
237e77819e5SLinus Torvalds 
238e77819e5SLinus Torvalds 	acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
239e77819e5SLinus Torvalds 
240e77819e5SLinus Torvalds 	/*
2414e34e719SChristoph Hellwig 	 * A filesystem can force a ACL callback by just never filling the
2424e34e719SChristoph Hellwig 	 * ACL cache. But normally you'd fill the cache either at inode
2434e34e719SChristoph Hellwig 	 * instantiation time, or on the first ->get_acl call.
244e77819e5SLinus Torvalds 	 *
2454e34e719SChristoph Hellwig 	 * If the filesystem doesn't have a get_acl() function at all, we'll
2464e34e719SChristoph Hellwig 	 * just create the negative cache entry.
247e77819e5SLinus Torvalds 	 */
248e77819e5SLinus Torvalds 	if (acl == ACL_NOT_CACHED) {
2494e34e719SChristoph Hellwig 	        if (inode->i_op->get_acl) {
2504e34e719SChristoph Hellwig 			acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
2514e34e719SChristoph Hellwig 			if (IS_ERR(acl))
2524e34e719SChristoph Hellwig 				return PTR_ERR(acl);
2534e34e719SChristoph Hellwig 		} else {
254e77819e5SLinus Torvalds 		        set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
255e77819e5SLinus Torvalds 		        return -EAGAIN;
256e77819e5SLinus Torvalds 		}
2574e34e719SChristoph Hellwig 	}
258e77819e5SLinus Torvalds 
259e77819e5SLinus Torvalds 	if (acl) {
260e77819e5SLinus Torvalds 	        int error = posix_acl_permission(inode, acl, mask);
261e77819e5SLinus Torvalds 	        posix_acl_release(acl);
262e77819e5SLinus Torvalds 	        return error;
263e77819e5SLinus Torvalds 	}
26484635d68SLinus Torvalds #endif
265e77819e5SLinus Torvalds 
266e77819e5SLinus Torvalds 	return -EAGAIN;
267e77819e5SLinus Torvalds }
268e77819e5SLinus Torvalds 
2695909ccaaSLinus Torvalds /*
270948409c7SAndreas Gruenbacher  * This does the basic permission checking
2715909ccaaSLinus Torvalds  */
2727e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask)
2735909ccaaSLinus Torvalds {
27426cf46beSLinus Torvalds 	unsigned int mode = inode->i_mode;
2755909ccaaSLinus Torvalds 
2768e96e3b7SEric W. Biederman 	if (likely(uid_eq(current_fsuid(), inode->i_uid)))
2775909ccaaSLinus Torvalds 		mode >>= 6;
2785909ccaaSLinus Torvalds 	else {
279e77819e5SLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
2807e40145eSAl Viro 			int error = check_acl(inode, mask);
2815909ccaaSLinus Torvalds 			if (error != -EAGAIN)
2825909ccaaSLinus Torvalds 				return error;
2835909ccaaSLinus Torvalds 		}
2845909ccaaSLinus Torvalds 
2855909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
2865909ccaaSLinus Torvalds 			mode >>= 3;
2875909ccaaSLinus Torvalds 	}
2885909ccaaSLinus Torvalds 
2895909ccaaSLinus Torvalds 	/*
2905909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
2915909ccaaSLinus Torvalds 	 */
2929c2c7039SAl Viro 	if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
2935909ccaaSLinus Torvalds 		return 0;
2945909ccaaSLinus Torvalds 	return -EACCES;
2955909ccaaSLinus Torvalds }
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds /**
2981da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
2991da177e4SLinus Torvalds  * @inode:	inode to check access rights for
3008fd90c8dSAndreas Gruenbacher  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
3011da177e4SLinus Torvalds  *
3021da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
3031da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
3041da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
305b74c79e9SNick Piggin  * are used for other things.
306b74c79e9SNick Piggin  *
307b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
308b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
309b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
3101da177e4SLinus Torvalds  */
3112830ba7fSAl Viro int generic_permission(struct inode *inode, int mask)
3121da177e4SLinus Torvalds {
3135909ccaaSLinus Torvalds 	int ret;
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	/*
316948409c7SAndreas Gruenbacher 	 * Do the basic permission checks.
3171da177e4SLinus Torvalds 	 */
3187e40145eSAl Viro 	ret = acl_permission_check(inode, mask);
3195909ccaaSLinus Torvalds 	if (ret != -EACCES)
3205909ccaaSLinus Torvalds 		return ret;
3211da177e4SLinus Torvalds 
322d594e7ecSAl Viro 	if (S_ISDIR(inode->i_mode)) {
323d594e7ecSAl Viro 		/* DACs are overridable for directories */
3241a48e2acSEric W. Biederman 		if (inode_capable(inode, CAP_DAC_OVERRIDE))
325d594e7ecSAl Viro 			return 0;
326d594e7ecSAl Viro 		if (!(mask & MAY_WRITE))
3271a48e2acSEric W. Biederman 			if (inode_capable(inode, CAP_DAC_READ_SEARCH))
328d594e7ecSAl Viro 				return 0;
329d594e7ecSAl Viro 		return -EACCES;
330d594e7ecSAl Viro 	}
3311da177e4SLinus Torvalds 	/*
3321da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
333d594e7ecSAl Viro 	 * Executable DACs are overridable when there is
334d594e7ecSAl Viro 	 * at least one exec bit set.
3351da177e4SLinus Torvalds 	 */
336d594e7ecSAl Viro 	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
3371a48e2acSEric W. Biederman 		if (inode_capable(inode, CAP_DAC_OVERRIDE))
3381da177e4SLinus Torvalds 			return 0;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	/*
3411da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
3421da177e4SLinus Torvalds 	 */
3437ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
344d594e7ecSAl Viro 	if (mask == MAY_READ)
3451a48e2acSEric W. Biederman 		if (inode_capable(inode, CAP_DAC_READ_SEARCH))
3461da177e4SLinus Torvalds 			return 0;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	return -EACCES;
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds 
3513ddcd056SLinus Torvalds /*
3523ddcd056SLinus Torvalds  * We _really_ want to just do "generic_permission()" without
3533ddcd056SLinus Torvalds  * even looking at the inode->i_op values. So we keep a cache
3543ddcd056SLinus Torvalds  * flag in inode->i_opflags, that says "this has not special
3553ddcd056SLinus Torvalds  * permission function, use the fast case".
3563ddcd056SLinus Torvalds  */
3573ddcd056SLinus Torvalds static inline int do_inode_permission(struct inode *inode, int mask)
3583ddcd056SLinus Torvalds {
3593ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
3603ddcd056SLinus Torvalds 		if (likely(inode->i_op->permission))
3613ddcd056SLinus Torvalds 			return inode->i_op->permission(inode, mask);
3623ddcd056SLinus Torvalds 
3633ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
3643ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
3653ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_FASTPERM;
3663ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
3673ddcd056SLinus Torvalds 	}
3683ddcd056SLinus Torvalds 	return generic_permission(inode, mask);
3693ddcd056SLinus Torvalds }
3703ddcd056SLinus Torvalds 
371cb23beb5SChristoph Hellwig /**
3720bdaea90SDavid Howells  * __inode_permission - Check for access rights to a given inode
3730bdaea90SDavid Howells  * @inode: Inode to check permission on
3740bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
375cb23beb5SChristoph Hellwig  *
3760bdaea90SDavid Howells  * Check for read/write/execute permissions on an inode.
377948409c7SAndreas Gruenbacher  *
378948409c7SAndreas Gruenbacher  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
3790bdaea90SDavid Howells  *
3800bdaea90SDavid Howells  * This does not check for a read-only file system.  You probably want
3810bdaea90SDavid Howells  * inode_permission().
382cb23beb5SChristoph Hellwig  */
3830bdaea90SDavid Howells int __inode_permission(struct inode *inode, int mask)
3841da177e4SLinus Torvalds {
385e6305c43SAl Viro 	int retval;
3861da177e4SLinus Torvalds 
3873ddcd056SLinus Torvalds 	if (unlikely(mask & MAY_WRITE)) {
3881da177e4SLinus Torvalds 		/*
3891da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
3901da177e4SLinus Torvalds 		 */
3911da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
3921da177e4SLinus Torvalds 			return -EACCES;
3931da177e4SLinus Torvalds 	}
3941da177e4SLinus Torvalds 
3953ddcd056SLinus Torvalds 	retval = do_inode_permission(inode, mask);
3961da177e4SLinus Torvalds 	if (retval)
3971da177e4SLinus Torvalds 		return retval;
3981da177e4SLinus Torvalds 
39908ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
40008ce5f16SSerge E. Hallyn 	if (retval)
40108ce5f16SSerge E. Hallyn 		return retval;
40208ce5f16SSerge E. Hallyn 
403d09ca739SEric Paris 	return security_inode_permission(inode, mask);
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
406f4d6ff89SAl Viro /**
4070bdaea90SDavid Howells  * sb_permission - Check superblock-level permissions
4080bdaea90SDavid Howells  * @sb: Superblock of inode to check permission on
40955852635SRandy Dunlap  * @inode: Inode to check permission on
4100bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
4110bdaea90SDavid Howells  *
4120bdaea90SDavid Howells  * Separate out file-system wide checks from inode-specific permission checks.
4130bdaea90SDavid Howells  */
4140bdaea90SDavid Howells static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
4150bdaea90SDavid Howells {
4160bdaea90SDavid Howells 	if (unlikely(mask & MAY_WRITE)) {
4170bdaea90SDavid Howells 		umode_t mode = inode->i_mode;
4180bdaea90SDavid Howells 
4190bdaea90SDavid Howells 		/* Nobody gets write access to a read-only fs. */
4200bdaea90SDavid Howells 		if ((sb->s_flags & MS_RDONLY) &&
4210bdaea90SDavid Howells 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
4220bdaea90SDavid Howells 			return -EROFS;
4230bdaea90SDavid Howells 	}
4240bdaea90SDavid Howells 	return 0;
4250bdaea90SDavid Howells }
4260bdaea90SDavid Howells 
4270bdaea90SDavid Howells /**
4280bdaea90SDavid Howells  * inode_permission - Check for access rights to a given inode
4290bdaea90SDavid Howells  * @inode: Inode to check permission on
4300bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
4310bdaea90SDavid Howells  *
4320bdaea90SDavid Howells  * Check for read/write/execute permissions on an inode.  We use fs[ug]id for
4330bdaea90SDavid Howells  * this, letting us set arbitrary permissions for filesystem access without
4340bdaea90SDavid Howells  * changing the "normal" UIDs which are used for other things.
4350bdaea90SDavid Howells  *
4360bdaea90SDavid Howells  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
4370bdaea90SDavid Howells  */
4380bdaea90SDavid Howells int inode_permission(struct inode *inode, int mask)
4390bdaea90SDavid Howells {
4400bdaea90SDavid Howells 	int retval;
4410bdaea90SDavid Howells 
4420bdaea90SDavid Howells 	retval = sb_permission(inode->i_sb, inode, mask);
4430bdaea90SDavid Howells 	if (retval)
4440bdaea90SDavid Howells 		return retval;
4450bdaea90SDavid Howells 	return __inode_permission(inode, mask);
4460bdaea90SDavid Howells }
4470bdaea90SDavid Howells 
4480bdaea90SDavid Howells /**
4495dd784d0SJan Blunck  * path_get - get a reference to a path
4505dd784d0SJan Blunck  * @path: path to get the reference to
4515dd784d0SJan Blunck  *
4525dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
4535dd784d0SJan Blunck  */
454dcf787f3SAl Viro void path_get(const struct path *path)
4555dd784d0SJan Blunck {
4565dd784d0SJan Blunck 	mntget(path->mnt);
4575dd784d0SJan Blunck 	dget(path->dentry);
4585dd784d0SJan Blunck }
4595dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
4605dd784d0SJan Blunck 
4615dd784d0SJan Blunck /**
4621d957f9bSJan Blunck  * path_put - put a reference to a path
4631d957f9bSJan Blunck  * @path: path to put the reference to
4641d957f9bSJan Blunck  *
4651d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
4661d957f9bSJan Blunck  */
467dcf787f3SAl Viro void path_put(const struct path *path)
4681da177e4SLinus Torvalds {
4691d957f9bSJan Blunck 	dput(path->dentry);
4701d957f9bSJan Blunck 	mntput(path->mnt);
4711da177e4SLinus Torvalds }
4721d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
4731da177e4SLinus Torvalds 
47419660af7SAl Viro /*
47531e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
47619660af7SAl Viro  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
47719660af7SAl Viro  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
47819660af7SAl Viro  * normal reference counts on dentries and vfsmounts to transition to rcu-walk
47919660af7SAl Viro  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
48019660af7SAl Viro  * got stuck, so ref-walk may continue from there. If this is not successful
48119660af7SAl Viro  * (eg. a seqcount has changed), then failure is returned and it's up to caller
48219660af7SAl Viro  * to restart the path walk from the beginning in ref-walk mode.
48331e6b01fSNick Piggin  */
48431e6b01fSNick Piggin 
48532a7991bSAl Viro static inline void lock_rcu_walk(void)
48632a7991bSAl Viro {
48732a7991bSAl Viro 	br_read_lock(&vfsmount_lock);
48832a7991bSAl Viro 	rcu_read_lock();
48932a7991bSAl Viro }
49032a7991bSAl Viro 
49132a7991bSAl Viro static inline void unlock_rcu_walk(void)
49232a7991bSAl Viro {
49332a7991bSAl Viro 	rcu_read_unlock();
49432a7991bSAl Viro 	br_read_unlock(&vfsmount_lock);
49532a7991bSAl Viro }
49632a7991bSAl Viro 
49731e6b01fSNick Piggin /**
49819660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
49919660af7SAl Viro  * @nd: nameidata pathwalk data
50019660af7SAl Viro  * @dentry: child of nd->path.dentry or NULL
50139191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
50231e6b01fSNick Piggin  *
50319660af7SAl Viro  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
50419660af7SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
50519660af7SAl Viro  * @nd or NULL.  Must be called from rcu-walk context.
50631e6b01fSNick Piggin  */
50719660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
50831e6b01fSNick Piggin {
50931e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
51031e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
51131e6b01fSNick Piggin 
51231e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
513e5c832d5SLinus Torvalds 
514e5c832d5SLinus Torvalds 	/*
515e5c832d5SLinus Torvalds 	 * Get a reference to the parent first: we're
516e5c832d5SLinus Torvalds 	 * going to make "path_put(nd->path)" valid in
517e5c832d5SLinus Torvalds 	 * non-RCU context for "terminate_walk()".
518e5c832d5SLinus Torvalds 	 *
519e5c832d5SLinus Torvalds 	 * If this doesn't work, return immediately with
520e5c832d5SLinus Torvalds 	 * RCU walking still active (and then we will do
521e5c832d5SLinus Torvalds 	 * the RCU walk cleanup in terminate_walk()).
522e5c832d5SLinus Torvalds 	 */
523e5c832d5SLinus Torvalds 	if (!lockref_get_not_dead(&parent->d_lockref))
524e5c832d5SLinus Torvalds 		return -ECHILD;
525e5c832d5SLinus Torvalds 
526e5c832d5SLinus Torvalds 	/*
527e5c832d5SLinus Torvalds 	 * After the mntget(), we terminate_walk() will do
528e5c832d5SLinus Torvalds 	 * the right thing for non-RCU mode, and all our
529e5c832d5SLinus Torvalds 	 * subsequent exit cases should unlock_rcu_walk()
530e5c832d5SLinus Torvalds 	 * before returning.
531e5c832d5SLinus Torvalds 	 */
532e5c832d5SLinus Torvalds 	mntget(nd->path.mnt);
533e5c832d5SLinus Torvalds 	nd->flags &= ~LOOKUP_RCU;
53415570086SLinus Torvalds 
53515570086SLinus Torvalds 	/*
53615570086SLinus Torvalds 	 * For a negative lookup, the lookup sequence point is the parents
53715570086SLinus Torvalds 	 * sequence point, and it only needs to revalidate the parent dentry.
53815570086SLinus Torvalds 	 *
53915570086SLinus Torvalds 	 * For a positive lookup, we need to move both the parent and the
54015570086SLinus Torvalds 	 * dentry from the RCU domain to be properly refcounted. And the
54115570086SLinus Torvalds 	 * sequence number in the dentry validates *both* dentry counters,
54215570086SLinus Torvalds 	 * since we checked the sequence number of the parent after we got
54315570086SLinus Torvalds 	 * the child sequence number. So we know the parent must still
54415570086SLinus Torvalds 	 * be valid if the child sequence number is still valid.
54515570086SLinus Torvalds 	 */
54619660af7SAl Viro 	if (!dentry) {
547e5c832d5SLinus Torvalds 		if (read_seqcount_retry(&parent->d_seq, nd->seq))
548e5c832d5SLinus Torvalds 			goto out;
54919660af7SAl Viro 		BUG_ON(nd->inode != parent->d_inode);
55019660af7SAl Viro 	} else {
551e5c832d5SLinus Torvalds 		if (!lockref_get_not_dead(&dentry->d_lockref))
552e5c832d5SLinus Torvalds 			goto out;
553e5c832d5SLinus Torvalds 		if (read_seqcount_retry(&dentry->d_seq, nd->seq))
554e5c832d5SLinus Torvalds 			goto drop_dentry;
55519660af7SAl Viro 	}
556e5c832d5SLinus Torvalds 
557e5c832d5SLinus Torvalds 	/*
558e5c832d5SLinus Torvalds 	 * Sequence counts matched. Now make sure that the root is
559e5c832d5SLinus Torvalds 	 * still valid and get it if required.
560e5c832d5SLinus Torvalds 	 */
561e5c832d5SLinus Torvalds 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
562e5c832d5SLinus Torvalds 		spin_lock(&fs->lock);
563e5c832d5SLinus Torvalds 		if (nd->root.mnt != fs->root.mnt || nd->root.dentry != fs->root.dentry)
564e5c832d5SLinus Torvalds 			goto unlock_and_drop_dentry;
56531e6b01fSNick Piggin 		path_get(&nd->root);
56631e6b01fSNick Piggin 		spin_unlock(&fs->lock);
56731e6b01fSNick Piggin 	}
56831e6b01fSNick Piggin 
56932a7991bSAl Viro 	unlock_rcu_walk();
57031e6b01fSNick Piggin 	return 0;
57119660af7SAl Viro 
572e5c832d5SLinus Torvalds unlock_and_drop_dentry:
57331e6b01fSNick Piggin 	spin_unlock(&fs->lock);
574e5c832d5SLinus Torvalds drop_dentry:
575e5c832d5SLinus Torvalds 	unlock_rcu_walk();
576e5c832d5SLinus Torvalds 	dput(dentry);
577d0d27277SLinus Torvalds 	goto drop_root_mnt;
578e5c832d5SLinus Torvalds out:
579e5c832d5SLinus Torvalds 	unlock_rcu_walk();
580d0d27277SLinus Torvalds drop_root_mnt:
581d0d27277SLinus Torvalds 	if (!(nd->flags & LOOKUP_ROOT))
582d0d27277SLinus Torvalds 		nd->root.mnt = NULL;
58331e6b01fSNick Piggin 	return -ECHILD;
58431e6b01fSNick Piggin }
58531e6b01fSNick Piggin 
5864ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
58734286d66SNick Piggin {
5884ce16ef3SAl Viro 	return dentry->d_op->d_revalidate(dentry, flags);
58934286d66SNick Piggin }
59034286d66SNick Piggin 
5919f1fafeeSAl Viro /**
5929f1fafeeSAl Viro  * complete_walk - successful completion of path walk
5939f1fafeeSAl Viro  * @nd:  pointer nameidata
59439159de2SJeff Layton  *
5959f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
5969f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
5979f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
5989f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
5999f1fafeeSAl Viro  * need to drop nd->path.
60039159de2SJeff Layton  */
6019f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
60239159de2SJeff Layton {
60316c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
60439159de2SJeff Layton 	int status;
60539159de2SJeff Layton 
6069f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
6079f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
6089f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
6099f1fafeeSAl Viro 			nd->root.mnt = NULL;
61015570086SLinus Torvalds 
611e5c832d5SLinus Torvalds 		if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) {
61232a7991bSAl Viro 			unlock_rcu_walk();
6139f1fafeeSAl Viro 			return -ECHILD;
6149f1fafeeSAl Viro 		}
615e5c832d5SLinus Torvalds 		if (read_seqcount_retry(&dentry->d_seq, nd->seq)) {
616e5c832d5SLinus Torvalds 			unlock_rcu_walk();
617e5c832d5SLinus Torvalds 			dput(dentry);
618e5c832d5SLinus Torvalds 			return -ECHILD;
619e5c832d5SLinus Torvalds 		}
6209f1fafeeSAl Viro 		mntget(nd->path.mnt);
62132a7991bSAl Viro 		unlock_rcu_walk();
6229f1fafeeSAl Viro 	}
6239f1fafeeSAl Viro 
62416c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
62539159de2SJeff Layton 		return 0;
62639159de2SJeff Layton 
627ecf3d1f1SJeff Layton 	if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
62816c2cd71SAl Viro 		return 0;
62916c2cd71SAl Viro 
630ecf3d1f1SJeff Layton 	status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
63139159de2SJeff Layton 	if (status > 0)
63239159de2SJeff Layton 		return 0;
63339159de2SJeff Layton 
63416c2cd71SAl Viro 	if (!status)
63539159de2SJeff Layton 		status = -ESTALE;
63616c2cd71SAl Viro 
6379f1fafeeSAl Viro 	path_put(&nd->path);
63839159de2SJeff Layton 	return status;
63939159de2SJeff Layton }
64039159de2SJeff Layton 
6412a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
6422a737871SAl Viro {
643f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
644f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
6452a737871SAl Viro }
6462a737871SAl Viro 
6476de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
6486de88d72SAl Viro 
64931e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
65031e6b01fSNick Piggin {
65131e6b01fSNick Piggin 	if (!nd->root.mnt) {
65231e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
653c28cc364SNick Piggin 		unsigned seq;
654c28cc364SNick Piggin 
655c28cc364SNick Piggin 		do {
656c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
65731e6b01fSNick Piggin 			nd->root = fs->root;
658c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
659c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
66031e6b01fSNick Piggin 	}
66131e6b01fSNick Piggin }
66231e6b01fSNick Piggin 
663f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
6641da177e4SLinus Torvalds {
66531e6b01fSNick Piggin 	int ret;
66631e6b01fSNick Piggin 
6671da177e4SLinus Torvalds 	if (IS_ERR(link))
6681da177e4SLinus Torvalds 		goto fail;
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 	if (*link == '/') {
6712a737871SAl Viro 		set_root(nd);
6721d957f9bSJan Blunck 		path_put(&nd->path);
6732a737871SAl Viro 		nd->path = nd->root;
6742a737871SAl Viro 		path_get(&nd->root);
67516c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
6761da177e4SLinus Torvalds 	}
67731e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
678b4091d5fSChristoph Hellwig 
67931e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
68031e6b01fSNick Piggin 	return ret;
6811da177e4SLinus Torvalds fail:
6821d957f9bSJan Blunck 	path_put(&nd->path);
6831da177e4SLinus Torvalds 	return PTR_ERR(link);
6841da177e4SLinus Torvalds }
6851da177e4SLinus Torvalds 
6861d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
687051d3812SIan Kent {
688051d3812SIan Kent 	dput(path->dentry);
6894ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
690051d3812SIan Kent 		mntput(path->mnt);
691051d3812SIan Kent }
692051d3812SIan Kent 
6937b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
6947b9337aaSNick Piggin 					struct nameidata *nd)
695051d3812SIan Kent {
69631e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
6974ac91378SJan Blunck 		dput(nd->path.dentry);
69831e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
6994ac91378SJan Blunck 			mntput(nd->path.mnt);
7009a229683SHuang Shijie 	}
70131e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
7024ac91378SJan Blunck 	nd->path.dentry = path->dentry;
703051d3812SIan Kent }
704051d3812SIan Kent 
705b5fb63c1SChristoph Hellwig /*
706b5fb63c1SChristoph Hellwig  * Helper to directly jump to a known parsed path from ->follow_link,
707b5fb63c1SChristoph Hellwig  * caller must have taken a reference to path beforehand.
708b5fb63c1SChristoph Hellwig  */
709b5fb63c1SChristoph Hellwig void nd_jump_link(struct nameidata *nd, struct path *path)
710b5fb63c1SChristoph Hellwig {
711b5fb63c1SChristoph Hellwig 	path_put(&nd->path);
712b5fb63c1SChristoph Hellwig 
713b5fb63c1SChristoph Hellwig 	nd->path = *path;
714b5fb63c1SChristoph Hellwig 	nd->inode = nd->path.dentry->d_inode;
715b5fb63c1SChristoph Hellwig 	nd->flags |= LOOKUP_JUMPED;
716b5fb63c1SChristoph Hellwig }
717b5fb63c1SChristoph Hellwig 
718574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
719574197e0SAl Viro {
720574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
7216d7b5aaeSAl Viro 	if (inode->i_op->put_link)
722574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
723574197e0SAl Viro 	path_put(link);
724574197e0SAl Viro }
725574197e0SAl Viro 
726561ec64aSLinus Torvalds int sysctl_protected_symlinks __read_mostly = 0;
727561ec64aSLinus Torvalds int sysctl_protected_hardlinks __read_mostly = 0;
728800179c9SKees Cook 
729800179c9SKees Cook /**
730800179c9SKees Cook  * may_follow_link - Check symlink following for unsafe situations
731800179c9SKees Cook  * @link: The path of the symlink
73255852635SRandy Dunlap  * @nd: nameidata pathwalk data
733800179c9SKees Cook  *
734800179c9SKees Cook  * In the case of the sysctl_protected_symlinks sysctl being enabled,
735800179c9SKees Cook  * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
736800179c9SKees Cook  * in a sticky world-writable directory. This is to protect privileged
737800179c9SKees Cook  * processes from failing races against path names that may change out
738800179c9SKees Cook  * from under them by way of other users creating malicious symlinks.
739800179c9SKees Cook  * It will permit symlinks to be followed only when outside a sticky
740800179c9SKees Cook  * world-writable directory, or when the uid of the symlink and follower
741800179c9SKees Cook  * match, or when the directory owner matches the symlink's owner.
742800179c9SKees Cook  *
743800179c9SKees Cook  * Returns 0 if following the symlink is allowed, -ve on error.
744800179c9SKees Cook  */
745800179c9SKees Cook static inline int may_follow_link(struct path *link, struct nameidata *nd)
746800179c9SKees Cook {
747800179c9SKees Cook 	const struct inode *inode;
748800179c9SKees Cook 	const struct inode *parent;
749800179c9SKees Cook 
750800179c9SKees Cook 	if (!sysctl_protected_symlinks)
751800179c9SKees Cook 		return 0;
752800179c9SKees Cook 
753800179c9SKees Cook 	/* Allowed if owner and follower match. */
754800179c9SKees Cook 	inode = link->dentry->d_inode;
75581abe27bSEric W. Biederman 	if (uid_eq(current_cred()->fsuid, inode->i_uid))
756800179c9SKees Cook 		return 0;
757800179c9SKees Cook 
758800179c9SKees Cook 	/* Allowed if parent directory not sticky and world-writable. */
759800179c9SKees Cook 	parent = nd->path.dentry->d_inode;
760800179c9SKees Cook 	if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
761800179c9SKees Cook 		return 0;
762800179c9SKees Cook 
763800179c9SKees Cook 	/* Allowed if parent directory and link owner match. */
76481abe27bSEric W. Biederman 	if (uid_eq(parent->i_uid, inode->i_uid))
765800179c9SKees Cook 		return 0;
766800179c9SKees Cook 
767ffd8d101SSasha Levin 	audit_log_link_denied("follow_link", link);
768800179c9SKees Cook 	path_put_conditional(link, nd);
769800179c9SKees Cook 	path_put(&nd->path);
770800179c9SKees Cook 	return -EACCES;
771800179c9SKees Cook }
772800179c9SKees Cook 
773800179c9SKees Cook /**
774800179c9SKees Cook  * safe_hardlink_source - Check for safe hardlink conditions
775800179c9SKees Cook  * @inode: the source inode to hardlink from
776800179c9SKees Cook  *
777800179c9SKees Cook  * Return false if at least one of the following conditions:
778800179c9SKees Cook  *    - inode is not a regular file
779800179c9SKees Cook  *    - inode is setuid
780800179c9SKees Cook  *    - inode is setgid and group-exec
781800179c9SKees Cook  *    - access failure for read and write
782800179c9SKees Cook  *
783800179c9SKees Cook  * Otherwise returns true.
784800179c9SKees Cook  */
785800179c9SKees Cook static bool safe_hardlink_source(struct inode *inode)
786800179c9SKees Cook {
787800179c9SKees Cook 	umode_t mode = inode->i_mode;
788800179c9SKees Cook 
789800179c9SKees Cook 	/* Special files should not get pinned to the filesystem. */
790800179c9SKees Cook 	if (!S_ISREG(mode))
791800179c9SKees Cook 		return false;
792800179c9SKees Cook 
793800179c9SKees Cook 	/* Setuid files should not get pinned to the filesystem. */
794800179c9SKees Cook 	if (mode & S_ISUID)
795800179c9SKees Cook 		return false;
796800179c9SKees Cook 
797800179c9SKees Cook 	/* Executable setgid files should not get pinned to the filesystem. */
798800179c9SKees Cook 	if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
799800179c9SKees Cook 		return false;
800800179c9SKees Cook 
801800179c9SKees Cook 	/* Hardlinking to unreadable or unwritable sources is dangerous. */
802800179c9SKees Cook 	if (inode_permission(inode, MAY_READ | MAY_WRITE))
803800179c9SKees Cook 		return false;
804800179c9SKees Cook 
805800179c9SKees Cook 	return true;
806800179c9SKees Cook }
807800179c9SKees Cook 
808800179c9SKees Cook /**
809800179c9SKees Cook  * may_linkat - Check permissions for creating a hardlink
810800179c9SKees Cook  * @link: the source to hardlink from
811800179c9SKees Cook  *
812800179c9SKees Cook  * Block hardlink when all of:
813800179c9SKees Cook  *  - sysctl_protected_hardlinks enabled
814800179c9SKees Cook  *  - fsuid does not match inode
815800179c9SKees Cook  *  - hardlink source is unsafe (see safe_hardlink_source() above)
816800179c9SKees Cook  *  - not CAP_FOWNER
817800179c9SKees Cook  *
818800179c9SKees Cook  * Returns 0 if successful, -ve on error.
819800179c9SKees Cook  */
820800179c9SKees Cook static int may_linkat(struct path *link)
821800179c9SKees Cook {
822800179c9SKees Cook 	const struct cred *cred;
823800179c9SKees Cook 	struct inode *inode;
824800179c9SKees Cook 
825800179c9SKees Cook 	if (!sysctl_protected_hardlinks)
826800179c9SKees Cook 		return 0;
827800179c9SKees Cook 
828800179c9SKees Cook 	cred = current_cred();
829800179c9SKees Cook 	inode = link->dentry->d_inode;
830800179c9SKees Cook 
831800179c9SKees Cook 	/* Source inode owner (or CAP_FOWNER) can hardlink all they like,
832800179c9SKees Cook 	 * otherwise, it must be a safe source.
833800179c9SKees Cook 	 */
83481abe27bSEric W. Biederman 	if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
835800179c9SKees Cook 	    capable(CAP_FOWNER))
836800179c9SKees Cook 		return 0;
837800179c9SKees Cook 
838a51d9eaaSKees Cook 	audit_log_link_denied("linkat", link);
839800179c9SKees Cook 	return -EPERM;
840800179c9SKees Cook }
841800179c9SKees Cook 
842def4af30SAl Viro static __always_inline int
843574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
8441da177e4SLinus Torvalds {
8457b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
8466d7b5aaeSAl Viro 	int error;
8476d7b5aaeSAl Viro 	char *s;
8481da177e4SLinus Torvalds 
849844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
850844a3917SAl Viro 
8510e794589SAl Viro 	if (link->mnt == nd->path.mnt)
8520e794589SAl Viro 		mntget(link->mnt);
8530e794589SAl Viro 
8546d7b5aaeSAl Viro 	error = -ELOOP;
8556d7b5aaeSAl Viro 	if (unlikely(current->total_link_count >= 40))
8566d7b5aaeSAl Viro 		goto out_put_nd_path;
8576d7b5aaeSAl Viro 
858574197e0SAl Viro 	cond_resched();
859574197e0SAl Viro 	current->total_link_count++;
860574197e0SAl Viro 
86168ac1234SAl Viro 	touch_atime(link);
8621da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
863cd4e91d3SAl Viro 
86436f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
8656d7b5aaeSAl Viro 	if (error)
8666d7b5aaeSAl Viro 		goto out_put_nd_path;
86736f3b4f6SAl Viro 
86886acdca1SAl Viro 	nd->last_type = LAST_BIND;
869def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
870def4af30SAl Viro 	error = PTR_ERR(*p);
8716d7b5aaeSAl Viro 	if (IS_ERR(*p))
872408ef013SChristoph Hellwig 		goto out_put_nd_path;
8736d7b5aaeSAl Viro 
874cc314eefSLinus Torvalds 	error = 0;
8756d7b5aaeSAl Viro 	s = nd_get_link(nd);
8766d7b5aaeSAl Viro 	if (s) {
8771da177e4SLinus Torvalds 		error = __vfs_follow_link(nd, s);
8786d7b5aaeSAl Viro 		if (unlikely(error))
8796d7b5aaeSAl Viro 			put_link(nd, link, *p);
880b5fb63c1SChristoph Hellwig 	}
8816d7b5aaeSAl Viro 
8826d7b5aaeSAl Viro 	return error;
8836d7b5aaeSAl Viro 
8846d7b5aaeSAl Viro out_put_nd_path:
88598f6ef64SArnd Bergmann 	*p = NULL;
8866d7b5aaeSAl Viro 	path_put(&nd->path);
8876d7b5aaeSAl Viro 	path_put(link);
8881da177e4SLinus Torvalds 	return error;
8891da177e4SLinus Torvalds }
8901da177e4SLinus Torvalds 
89131e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
89231e6b01fSNick Piggin {
8930714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
8940714a533SAl Viro 	struct mount *parent;
89531e6b01fSNick Piggin 	struct dentry *mountpoint;
89631e6b01fSNick Piggin 
8970714a533SAl Viro 	parent = mnt->mnt_parent;
8980714a533SAl Viro 	if (&parent->mnt == path->mnt)
89931e6b01fSNick Piggin 		return 0;
900a73324daSAl Viro 	mountpoint = mnt->mnt_mountpoint;
90131e6b01fSNick Piggin 	path->dentry = mountpoint;
9020714a533SAl Viro 	path->mnt = &parent->mnt;
90331e6b01fSNick Piggin 	return 1;
90431e6b01fSNick Piggin }
90531e6b01fSNick Piggin 
906f015f126SDavid Howells /*
907f015f126SDavid Howells  * follow_up - Find the mountpoint of path's vfsmount
908f015f126SDavid Howells  *
909f015f126SDavid Howells  * Given a path, find the mountpoint of its source file system.
910f015f126SDavid Howells  * Replace @path with the path of the mountpoint in the parent mount.
911f015f126SDavid Howells  * Up is towards /.
912f015f126SDavid Howells  *
913f015f126SDavid Howells  * Return 1 if we went up a level and 0 if we were already at the
914f015f126SDavid Howells  * root.
915f015f126SDavid Howells  */
916bab77ebfSAl Viro int follow_up(struct path *path)
9171da177e4SLinus Torvalds {
9180714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
9190714a533SAl Viro 	struct mount *parent;
9201da177e4SLinus Torvalds 	struct dentry *mountpoint;
92199b7db7bSNick Piggin 
922962830dfSAndi Kleen 	br_read_lock(&vfsmount_lock);
9230714a533SAl Viro 	parent = mnt->mnt_parent;
9243c0a6163SAl Viro 	if (parent == mnt) {
925962830dfSAndi Kleen 		br_read_unlock(&vfsmount_lock);
9261da177e4SLinus Torvalds 		return 0;
9271da177e4SLinus Torvalds 	}
9280714a533SAl Viro 	mntget(&parent->mnt);
929a73324daSAl Viro 	mountpoint = dget(mnt->mnt_mountpoint);
930962830dfSAndi Kleen 	br_read_unlock(&vfsmount_lock);
931bab77ebfSAl Viro 	dput(path->dentry);
932bab77ebfSAl Viro 	path->dentry = mountpoint;
933bab77ebfSAl Viro 	mntput(path->mnt);
9340714a533SAl Viro 	path->mnt = &parent->mnt;
9351da177e4SLinus Torvalds 	return 1;
9361da177e4SLinus Torvalds }
9371da177e4SLinus Torvalds 
938b5c84bf6SNick Piggin /*
9399875cf80SDavid Howells  * Perform an automount
9409875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
9419875cf80SDavid Howells  *   were called with.
9421da177e4SLinus Torvalds  */
9439875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
9449875cf80SDavid Howells 			    bool *need_mntput)
94531e6b01fSNick Piggin {
9469875cf80SDavid Howells 	struct vfsmount *mnt;
947ea5b778aSDavid Howells 	int err;
9489875cf80SDavid Howells 
9499875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
9509875cf80SDavid Howells 		return -EREMOTE;
9519875cf80SDavid Howells 
9520ec26fd0SMiklos Szeredi 	/* We don't want to mount if someone's just doing a stat -
9530ec26fd0SMiklos Szeredi 	 * unless they're stat'ing a directory and appended a '/' to
9540ec26fd0SMiklos Szeredi 	 * the name.
9550ec26fd0SMiklos Szeredi 	 *
9560ec26fd0SMiklos Szeredi 	 * We do, however, want to mount if someone wants to open or
9575a30d8a2SDavid Howells 	 * create a file of any type under the mountpoint, wants to
9580ec26fd0SMiklos Szeredi 	 * traverse through the mountpoint or wants to open the
9590ec26fd0SMiklos Szeredi 	 * mounted directory.  Also, autofs may mark negative dentries
9600ec26fd0SMiklos Szeredi 	 * as being automount points.  These will need the attentions
9610ec26fd0SMiklos Szeredi 	 * of the daemon to instantiate them before they can be used.
9625a30d8a2SDavid Howells 	 */
9635a30d8a2SDavid Howells 	if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
964d94c177bSLinus Torvalds 		     LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
9655a30d8a2SDavid Howells 	    path->dentry->d_inode)
9669875cf80SDavid Howells 		return -EISDIR;
9670ec26fd0SMiklos Szeredi 
9689875cf80SDavid Howells 	current->total_link_count++;
9699875cf80SDavid Howells 	if (current->total_link_count >= 40)
9709875cf80SDavid Howells 		return -ELOOP;
9719875cf80SDavid Howells 
9729875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
9739875cf80SDavid Howells 	if (IS_ERR(mnt)) {
9749875cf80SDavid Howells 		/*
9759875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
9769875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
9779875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
9789875cf80SDavid Howells 		 *
9799875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
9809875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
9819875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
9829875cf80SDavid Howells 		 */
98349084c3bSAl Viro 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
9849875cf80SDavid Howells 			return -EREMOTE;
9859875cf80SDavid Howells 		return PTR_ERR(mnt);
98631e6b01fSNick Piggin 	}
987ea5b778aSDavid Howells 
9889875cf80SDavid Howells 	if (!mnt) /* mount collision */
9899875cf80SDavid Howells 		return 0;
9909875cf80SDavid Howells 
9918aef1884SAl Viro 	if (!*need_mntput) {
9928aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
9938aef1884SAl Viro 		mntget(path->mnt);
9948aef1884SAl Viro 		*need_mntput = true;
9958aef1884SAl Viro 	}
99619a167afSAl Viro 	err = finish_automount(mnt, path);
997ea5b778aSDavid Howells 
998ea5b778aSDavid Howells 	switch (err) {
999ea5b778aSDavid Howells 	case -EBUSY:
1000ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
100119a167afSAl Viro 		return 0;
1002ea5b778aSDavid Howells 	case 0:
10038aef1884SAl Viro 		path_put(path);
10049875cf80SDavid Howells 		path->mnt = mnt;
10059875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
10069875cf80SDavid Howells 		return 0;
100719a167afSAl Viro 	default:
100819a167afSAl Viro 		return err;
10099875cf80SDavid Howells 	}
101019a167afSAl Viro 
1011ea5b778aSDavid Howells }
10129875cf80SDavid Howells 
10139875cf80SDavid Howells /*
10149875cf80SDavid Howells  * Handle a dentry that is managed in some way.
1015cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
10169875cf80SDavid Howells  * - Flagged as mountpoint
10179875cf80SDavid Howells  * - Flagged as automount point
10189875cf80SDavid Howells  *
10199875cf80SDavid Howells  * This may only be called in refwalk mode.
10209875cf80SDavid Howells  *
10219875cf80SDavid Howells  * Serialization is taken care of in namespace.c
10229875cf80SDavid Howells  */
10239875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
10249875cf80SDavid Howells {
10258aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
10269875cf80SDavid Howells 	unsigned managed;
10279875cf80SDavid Howells 	bool need_mntput = false;
10288aef1884SAl Viro 	int ret = 0;
10299875cf80SDavid Howells 
10309875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
10319875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
10329875cf80SDavid Howells 	 * the components of that value change under us */
10339875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
10349875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
10359875cf80SDavid Howells 	       unlikely(managed != 0)) {
1036cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1037cc53ce53SDavid Howells 		 * being held. */
1038cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1039cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1040cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
10411aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
1042cc53ce53SDavid Howells 			if (ret < 0)
10438aef1884SAl Viro 				break;
1044cc53ce53SDavid Howells 		}
1045cc53ce53SDavid Howells 
10469875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
10479875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
10489875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
10499875cf80SDavid Howells 			if (mounted) {
10509875cf80SDavid Howells 				dput(path->dentry);
10519875cf80SDavid Howells 				if (need_mntput)
1052463ffb2eSAl Viro 					mntput(path->mnt);
1053463ffb2eSAl Viro 				path->mnt = mounted;
1054463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
10559875cf80SDavid Howells 				need_mntput = true;
10569875cf80SDavid Howells 				continue;
1057463ffb2eSAl Viro 			}
1058463ffb2eSAl Viro 
10599875cf80SDavid Howells 			/* Something is mounted on this dentry in another
10609875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
10619875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
10629875cf80SDavid Howells 			 * vfsmount_lock */
10631da177e4SLinus Torvalds 		}
10649875cf80SDavid Howells 
10659875cf80SDavid Howells 		/* Handle an automount point */
10669875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
10679875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
10689875cf80SDavid Howells 			if (ret < 0)
10698aef1884SAl Viro 				break;
10709875cf80SDavid Howells 			continue;
10719875cf80SDavid Howells 		}
10729875cf80SDavid Howells 
10739875cf80SDavid Howells 		/* We didn't change the current path point */
10749875cf80SDavid Howells 		break;
10759875cf80SDavid Howells 	}
10768aef1884SAl Viro 
10778aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
10788aef1884SAl Viro 		mntput(path->mnt);
10798aef1884SAl Viro 	if (ret == -EISDIR)
10808aef1884SAl Viro 		ret = 0;
1081a3fbbde7SAl Viro 	return ret < 0 ? ret : need_mntput;
10821da177e4SLinus Torvalds }
10831da177e4SLinus Torvalds 
1084cc53ce53SDavid Howells int follow_down_one(struct path *path)
10851da177e4SLinus Torvalds {
10861da177e4SLinus Torvalds 	struct vfsmount *mounted;
10871da177e4SLinus Torvalds 
10881c755af4SAl Viro 	mounted = lookup_mnt(path);
10891da177e4SLinus Torvalds 	if (mounted) {
10909393bd07SAl Viro 		dput(path->dentry);
10919393bd07SAl Viro 		mntput(path->mnt);
10929393bd07SAl Viro 		path->mnt = mounted;
10939393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
10941da177e4SLinus Torvalds 		return 1;
10951da177e4SLinus Torvalds 	}
10961da177e4SLinus Torvalds 	return 0;
10971da177e4SLinus Torvalds }
10981da177e4SLinus Torvalds 
109962a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
110062a7375eSIan Kent {
110162a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
110262a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
110362a7375eSIan Kent }
110462a7375eSIan Kent 
11059875cf80SDavid Howells /*
1106287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
1107287548e4SAl Viro  * we meet a managed dentry that would need blocking.
11089875cf80SDavid Howells  */
11099875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1110287548e4SAl Viro 			       struct inode **inode)
11119875cf80SDavid Howells {
111262a7375eSIan Kent 	for (;;) {
1113c7105365SAl Viro 		struct mount *mounted;
111462a7375eSIan Kent 		/*
111562a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
111662a7375eSIan Kent 		 * that wants to block transit.
111762a7375eSIan Kent 		 */
1118287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
1119ab90911fSDavid Howells 			return false;
112062a7375eSIan Kent 
112162a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
112262a7375eSIan Kent 			break;
112362a7375eSIan Kent 
11249875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
11259875cf80SDavid Howells 		if (!mounted)
11269875cf80SDavid Howells 			break;
1127c7105365SAl Viro 		path->mnt = &mounted->mnt;
1128c7105365SAl Viro 		path->dentry = mounted->mnt.mnt_root;
1129a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
11309875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
113159430262SLinus Torvalds 		/*
113259430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
113359430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
113459430262SLinus Torvalds 		 * because a mount-point is always pinned.
113559430262SLinus Torvalds 		 */
113659430262SLinus Torvalds 		*inode = path->dentry->d_inode;
11379875cf80SDavid Howells 	}
11389875cf80SDavid Howells 	return true;
11399875cf80SDavid Howells }
11409875cf80SDavid Howells 
1141dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
1142287548e4SAl Viro {
1143dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
1144c7105365SAl Viro 		struct mount *mounted;
1145dea39376SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
1146287548e4SAl Viro 		if (!mounted)
1147287548e4SAl Viro 			break;
1148c7105365SAl Viro 		nd->path.mnt = &mounted->mnt;
1149c7105365SAl Viro 		nd->path.dentry = mounted->mnt.mnt_root;
1150dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1151287548e4SAl Viro 	}
1152287548e4SAl Viro }
1153287548e4SAl Viro 
115431e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
115531e6b01fSNick Piggin {
115631e6b01fSNick Piggin 	set_root_rcu(nd);
115731e6b01fSNick Piggin 
115831e6b01fSNick Piggin 	while (1) {
115931e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
116031e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
116131e6b01fSNick Piggin 			break;
116231e6b01fSNick Piggin 		}
116331e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
116431e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
116531e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
116631e6b01fSNick Piggin 			unsigned seq;
116731e6b01fSNick Piggin 
116831e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
116931e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
1170ef7562d5SAl Viro 				goto failed;
117131e6b01fSNick Piggin 			nd->path.dentry = parent;
117231e6b01fSNick Piggin 			nd->seq = seq;
117331e6b01fSNick Piggin 			break;
117431e6b01fSNick Piggin 		}
117531e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
117631e6b01fSNick Piggin 			break;
117731e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
117831e6b01fSNick Piggin 	}
1179dea39376SAl Viro 	follow_mount_rcu(nd);
1180dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
118131e6b01fSNick Piggin 	return 0;
1182ef7562d5SAl Viro 
1183ef7562d5SAl Viro failed:
1184ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
11855b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
1186ef7562d5SAl Viro 		nd->root.mnt = NULL;
118732a7991bSAl Viro 	unlock_rcu_walk();
1188ef7562d5SAl Viro 	return -ECHILD;
118931e6b01fSNick Piggin }
119031e6b01fSNick Piggin 
11919875cf80SDavid Howells /*
1192cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
1193cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
1194cc53ce53SDavid Howells  * caller is permitted to proceed or not.
1195cc53ce53SDavid Howells  */
11967cc90cc3SAl Viro int follow_down(struct path *path)
1197cc53ce53SDavid Howells {
1198cc53ce53SDavid Howells 	unsigned managed;
1199cc53ce53SDavid Howells 	int ret;
1200cc53ce53SDavid Howells 
1201cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
1202cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1203cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1204cc53ce53SDavid Howells 		 * being held.
1205cc53ce53SDavid Howells 		 *
1206cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
1207cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
1208cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
1209cc53ce53SDavid Howells 		 * superstructure.
1210cc53ce53SDavid Howells 		 *
1211cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
1212cc53ce53SDavid Howells 		 */
1213cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1214cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1215cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
1216ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
12171aed3e42SAl Viro 				path->dentry, false);
1218cc53ce53SDavid Howells 			if (ret < 0)
1219cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
1220cc53ce53SDavid Howells 		}
1221cc53ce53SDavid Howells 
1222cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
1223cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
1224cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
1225cc53ce53SDavid Howells 			if (!mounted)
1226cc53ce53SDavid Howells 				break;
1227cc53ce53SDavid Howells 			dput(path->dentry);
1228cc53ce53SDavid Howells 			mntput(path->mnt);
1229cc53ce53SDavid Howells 			path->mnt = mounted;
1230cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1231cc53ce53SDavid Howells 			continue;
1232cc53ce53SDavid Howells 		}
1233cc53ce53SDavid Howells 
1234cc53ce53SDavid Howells 		/* Don't handle automount points here */
1235cc53ce53SDavid Howells 		break;
1236cc53ce53SDavid Howells 	}
1237cc53ce53SDavid Howells 	return 0;
1238cc53ce53SDavid Howells }
1239cc53ce53SDavid Howells 
1240cc53ce53SDavid Howells /*
12419875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
12429875cf80SDavid Howells  */
12439875cf80SDavid Howells static void follow_mount(struct path *path)
12449875cf80SDavid Howells {
12459875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
12469875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
12479875cf80SDavid Howells 		if (!mounted)
12489875cf80SDavid Howells 			break;
12499875cf80SDavid Howells 		dput(path->dentry);
12509875cf80SDavid Howells 		mntput(path->mnt);
12519875cf80SDavid Howells 		path->mnt = mounted;
12529875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
12539875cf80SDavid Howells 	}
12549875cf80SDavid Howells }
12559875cf80SDavid Howells 
125631e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
12571da177e4SLinus Torvalds {
12582a737871SAl Viro 	set_root(nd);
1259e518ddb7SAndreas Mohr 
12601da177e4SLinus Torvalds 	while(1) {
12614ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
12621da177e4SLinus Torvalds 
12632a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
12642a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
12651da177e4SLinus Torvalds 			break;
12661da177e4SLinus Torvalds 		}
12674ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
12683088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
12693088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
12701da177e4SLinus Torvalds 			dput(old);
12711da177e4SLinus Torvalds 			break;
12721da177e4SLinus Torvalds 		}
12733088dd70SAl Viro 		if (!follow_up(&nd->path))
12741da177e4SLinus Torvalds 			break;
12751da177e4SLinus Torvalds 	}
127679ed0226SAl Viro 	follow_mount(&nd->path);
127731e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
12781da177e4SLinus Torvalds }
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds /*
1281bad61189SMiklos Szeredi  * This looks up the name in dcache, possibly revalidates the old dentry and
1282bad61189SMiklos Szeredi  * allocates a new one if not found or not valid.  In the need_lookup argument
1283bad61189SMiklos Szeredi  * returns whether i_op->lookup is necessary.
1284bad61189SMiklos Szeredi  *
1285bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
1286baa03890SNick Piggin  */
1287bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1288201f956eSAl Viro 				    unsigned int flags, bool *need_lookup)
1289baa03890SNick Piggin {
1290baa03890SNick Piggin 	struct dentry *dentry;
1291bad61189SMiklos Szeredi 	int error;
1292baa03890SNick Piggin 
1293bad61189SMiklos Szeredi 	*need_lookup = false;
1294bad61189SMiklos Szeredi 	dentry = d_lookup(dir, name);
1295bad61189SMiklos Szeredi 	if (dentry) {
129639e3c955SJeff Layton 		if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1297201f956eSAl Viro 			error = d_revalidate(dentry, flags);
1298bad61189SMiklos Szeredi 			if (unlikely(error <= 0)) {
1299bad61189SMiklos Szeredi 				if (error < 0) {
1300bad61189SMiklos Szeredi 					dput(dentry);
1301bad61189SMiklos Szeredi 					return ERR_PTR(error);
1302bad61189SMiklos Szeredi 				} else if (!d_invalidate(dentry)) {
1303bad61189SMiklos Szeredi 					dput(dentry);
1304bad61189SMiklos Szeredi 					dentry = NULL;
1305bad61189SMiklos Szeredi 				}
1306bad61189SMiklos Szeredi 			}
1307bad61189SMiklos Szeredi 		}
1308bad61189SMiklos Szeredi 	}
1309baa03890SNick Piggin 
1310bad61189SMiklos Szeredi 	if (!dentry) {
1311bad61189SMiklos Szeredi 		dentry = d_alloc(dir, name);
1312baa03890SNick Piggin 		if (unlikely(!dentry))
1313baa03890SNick Piggin 			return ERR_PTR(-ENOMEM);
1314baa03890SNick Piggin 
1315bad61189SMiklos Szeredi 		*need_lookup = true;
1316baa03890SNick Piggin 	}
1317baa03890SNick Piggin 	return dentry;
1318baa03890SNick Piggin }
1319baa03890SNick Piggin 
1320baa03890SNick Piggin /*
1321bad61189SMiklos Szeredi  * Call i_op->lookup on the dentry.  The dentry must be negative but may be
1322bad61189SMiklos Szeredi  * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1323bad61189SMiklos Szeredi  *
1324bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
132544396f4bSJosef Bacik  */
1326bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
132772bd866aSAl Viro 				  unsigned int flags)
132844396f4bSJosef Bacik {
132944396f4bSJosef Bacik 	struct dentry *old;
133044396f4bSJosef Bacik 
133144396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
1332bad61189SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
1333e188dc02SMiklos Szeredi 		dput(dentry);
133444396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
1335e188dc02SMiklos Szeredi 	}
133644396f4bSJosef Bacik 
133772bd866aSAl Viro 	old = dir->i_op->lookup(dir, dentry, flags);
133844396f4bSJosef Bacik 	if (unlikely(old)) {
133944396f4bSJosef Bacik 		dput(dentry);
134044396f4bSJosef Bacik 		dentry = old;
134144396f4bSJosef Bacik 	}
134244396f4bSJosef Bacik 	return dentry;
134344396f4bSJosef Bacik }
134444396f4bSJosef Bacik 
1345a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name,
134672bd866aSAl Viro 		struct dentry *base, unsigned int flags)
1347a3255546SAl Viro {
1348bad61189SMiklos Szeredi 	bool need_lookup;
1349a3255546SAl Viro 	struct dentry *dentry;
1350a3255546SAl Viro 
135172bd866aSAl Viro 	dentry = lookup_dcache(name, base, flags, &need_lookup);
1352bad61189SMiklos Szeredi 	if (!need_lookup)
1353a3255546SAl Viro 		return dentry;
1354bad61189SMiklos Szeredi 
135572bd866aSAl Viro 	return lookup_real(base->d_inode, dentry, flags);
1356a3255546SAl Viro }
1357a3255546SAl Viro 
135844396f4bSJosef Bacik /*
13591da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
13601da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
13611da177e4SLinus Torvalds  *  It _is_ time-critical.
13621da177e4SLinus Torvalds  */
1363e97cdc87SAl Viro static int lookup_fast(struct nameidata *nd,
136431e6b01fSNick Piggin 		       struct path *path, struct inode **inode)
13651da177e4SLinus Torvalds {
13664ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
136731e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
13685a18fff2SAl Viro 	int need_reval = 1;
13695a18fff2SAl Viro 	int status = 1;
13709875cf80SDavid Howells 	int err;
13719875cf80SDavid Howells 
13723cac260aSAl Viro 	/*
1373b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1374b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1375b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1376b04f784eSNick Piggin 	 */
137731e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
137831e6b01fSNick Piggin 		unsigned seq;
1379da53be12SLinus Torvalds 		dentry = __d_lookup_rcu(parent, &nd->last, &seq);
13805a18fff2SAl Viro 		if (!dentry)
13815a18fff2SAl Viro 			goto unlazy;
13825a18fff2SAl Viro 
138312f8ad4bSLinus Torvalds 		/*
138412f8ad4bSLinus Torvalds 		 * This sequence count validates that the inode matches
138512f8ad4bSLinus Torvalds 		 * the dentry name information from lookup.
138612f8ad4bSLinus Torvalds 		 */
138712f8ad4bSLinus Torvalds 		*inode = dentry->d_inode;
138812f8ad4bSLinus Torvalds 		if (read_seqcount_retry(&dentry->d_seq, seq))
138912f8ad4bSLinus Torvalds 			return -ECHILD;
139012f8ad4bSLinus Torvalds 
139112f8ad4bSLinus Torvalds 		/*
139212f8ad4bSLinus Torvalds 		 * This sequence count validates that the parent had no
139312f8ad4bSLinus Torvalds 		 * changes while we did the lookup of the dentry above.
139412f8ad4bSLinus Torvalds 		 *
139512f8ad4bSLinus Torvalds 		 * The memory barrier in read_seqcount_begin of child is
139612f8ad4bSLinus Torvalds 		 *  enough, we can use __read_seqcount_retry here.
139712f8ad4bSLinus Torvalds 		 */
139831e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
139931e6b01fSNick Piggin 			return -ECHILD;
140031e6b01fSNick Piggin 		nd->seq = seq;
14015a18fff2SAl Viro 
140224643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
14034ce16ef3SAl Viro 			status = d_revalidate(dentry, nd->flags);
14045a18fff2SAl Viro 			if (unlikely(status <= 0)) {
14055a18fff2SAl Viro 				if (status != -ECHILD)
14065a18fff2SAl Viro 					need_reval = 0;
14075a18fff2SAl Viro 				goto unlazy;
14085a18fff2SAl Viro 			}
140924643087SAl Viro 		}
141031e6b01fSNick Piggin 		path->mnt = mnt;
141131e6b01fSNick Piggin 		path->dentry = dentry;
1412d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1413d6e9bd25SAl Viro 			goto unlazy;
1414d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1415d6e9bd25SAl Viro 			goto unlazy;
14169875cf80SDavid Howells 		return 0;
14175a18fff2SAl Viro unlazy:
141819660af7SAl Viro 		if (unlazy_walk(nd, dentry))
14195a18fff2SAl Viro 			return -ECHILD;
14205a18fff2SAl Viro 	} else {
1421e97cdc87SAl Viro 		dentry = __d_lookup(parent, &nd->last);
142224643087SAl Viro 	}
14235a18fff2SAl Viro 
142481e6f520SAl Viro 	if (unlikely(!dentry))
142581e6f520SAl Viro 		goto need_lookup;
14265a18fff2SAl Viro 
14275a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
14284ce16ef3SAl Viro 		status = d_revalidate(dentry, nd->flags);
14295a18fff2SAl Viro 	if (unlikely(status <= 0)) {
14305a18fff2SAl Viro 		if (status < 0) {
14315a18fff2SAl Viro 			dput(dentry);
14325a18fff2SAl Viro 			return status;
14335a18fff2SAl Viro 		}
14345a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
14355a18fff2SAl Viro 			dput(dentry);
143681e6f520SAl Viro 			goto need_lookup;
14375a18fff2SAl Viro 		}
14385a18fff2SAl Viro 	}
1439697f514dSMiklos Szeredi 
14401da177e4SLinus Torvalds 	path->mnt = mnt;
14411da177e4SLinus Torvalds 	path->dentry = dentry;
14429875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
144389312214SIan Kent 	if (unlikely(err < 0)) {
144489312214SIan Kent 		path_put_conditional(path, nd);
14459875cf80SDavid Howells 		return err;
144689312214SIan Kent 	}
1447a3fbbde7SAl Viro 	if (err)
1448a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
144931e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
14501da177e4SLinus Torvalds 	return 0;
145181e6f520SAl Viro 
145281e6f520SAl Viro need_lookup:
1453697f514dSMiklos Szeredi 	return 1;
1454697f514dSMiklos Szeredi }
1455697f514dSMiklos Szeredi 
1456697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */
1457cc2a5271SAl Viro static int lookup_slow(struct nameidata *nd, struct path *path)
1458697f514dSMiklos Szeredi {
1459697f514dSMiklos Szeredi 	struct dentry *dentry, *parent;
1460697f514dSMiklos Szeredi 	int err;
1461697f514dSMiklos Szeredi 
1462697f514dSMiklos Szeredi 	parent = nd->path.dentry;
146381e6f520SAl Viro 	BUG_ON(nd->inode != parent->d_inode);
146481e6f520SAl Viro 
146581e6f520SAl Viro 	mutex_lock(&parent->d_inode->i_mutex);
1466cc2a5271SAl Viro 	dentry = __lookup_hash(&nd->last, parent, nd->flags);
146781e6f520SAl Viro 	mutex_unlock(&parent->d_inode->i_mutex);
146881e6f520SAl Viro 	if (IS_ERR(dentry))
146981e6f520SAl Viro 		return PTR_ERR(dentry);
1470697f514dSMiklos Szeredi 	path->mnt = nd->path.mnt;
1471697f514dSMiklos Szeredi 	path->dentry = dentry;
1472697f514dSMiklos Szeredi 	err = follow_managed(path, nd->flags);
1473697f514dSMiklos Szeredi 	if (unlikely(err < 0)) {
1474697f514dSMiklos Szeredi 		path_put_conditional(path, nd);
1475697f514dSMiklos Szeredi 		return err;
1476697f514dSMiklos Szeredi 	}
1477697f514dSMiklos Szeredi 	if (err)
1478697f514dSMiklos Szeredi 		nd->flags |= LOOKUP_JUMPED;
1479697f514dSMiklos Szeredi 	return 0;
14801da177e4SLinus Torvalds }
14811da177e4SLinus Torvalds 
148252094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
148352094c8aSAl Viro {
148452094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
14854ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
148652094c8aSAl Viro 		if (err != -ECHILD)
148752094c8aSAl Viro 			return err;
148819660af7SAl Viro 		if (unlazy_walk(nd, NULL))
148952094c8aSAl Viro 			return -ECHILD;
149052094c8aSAl Viro 	}
14914ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
149252094c8aSAl Viro }
149352094c8aSAl Viro 
14949856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
14959856fa1bSAl Viro {
14969856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
14979856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
14989856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
14999856fa1bSAl Viro 				return -ECHILD;
15009856fa1bSAl Viro 		} else
15019856fa1bSAl Viro 			follow_dotdot(nd);
15029856fa1bSAl Viro 	}
15039856fa1bSAl Viro 	return 0;
15049856fa1bSAl Viro }
15059856fa1bSAl Viro 
1506951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1507951361f9SAl Viro {
1508951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1509951361f9SAl Viro 		path_put(&nd->path);
1510951361f9SAl Viro 	} else {
1511951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
15125b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1513951361f9SAl Viro 			nd->root.mnt = NULL;
151432a7991bSAl Viro 		unlock_rcu_walk();
1515951361f9SAl Viro 	}
1516951361f9SAl Viro }
1517951361f9SAl Viro 
15183ddcd056SLinus Torvalds /*
15193ddcd056SLinus Torvalds  * Do we need to follow links? We _really_ want to be able
15203ddcd056SLinus Torvalds  * to do this check without having to look at inode->i_op,
15213ddcd056SLinus Torvalds  * so we keep a cache of "no, this doesn't need follow_link"
15223ddcd056SLinus Torvalds  * for the common case.
15233ddcd056SLinus Torvalds  */
15247813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow)
15253ddcd056SLinus Torvalds {
15263ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
15273ddcd056SLinus Torvalds 		if (likely(inode->i_op->follow_link))
15283ddcd056SLinus Torvalds 			return follow;
15293ddcd056SLinus Torvalds 
15303ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
15313ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
15323ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_NOFOLLOW;
15333ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
15343ddcd056SLinus Torvalds 	}
15353ddcd056SLinus Torvalds 	return 0;
15363ddcd056SLinus Torvalds }
15373ddcd056SLinus Torvalds 
1538ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
153921b9b073SAl Viro 		int follow)
1540ce57dfc1SAl Viro {
1541ce57dfc1SAl Viro 	struct inode *inode;
1542ce57dfc1SAl Viro 	int err;
1543ce57dfc1SAl Viro 	/*
1544ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1545ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1546ce57dfc1SAl Viro 	 * parent relationships.
1547ce57dfc1SAl Viro 	 */
154821b9b073SAl Viro 	if (unlikely(nd->last_type != LAST_NORM))
154921b9b073SAl Viro 		return handle_dots(nd, nd->last_type);
1550e97cdc87SAl Viro 	err = lookup_fast(nd, path, &inode);
1551ce57dfc1SAl Viro 	if (unlikely(err)) {
1552697f514dSMiklos Szeredi 		if (err < 0)
1553697f514dSMiklos Szeredi 			goto out_err;
1554697f514dSMiklos Szeredi 
1555cc2a5271SAl Viro 		err = lookup_slow(nd, path);
1556697f514dSMiklos Szeredi 		if (err < 0)
1557697f514dSMiklos Szeredi 			goto out_err;
1558697f514dSMiklos Szeredi 
1559697f514dSMiklos Szeredi 		inode = path->dentry->d_inode;
1560ce57dfc1SAl Viro 	}
1561697f514dSMiklos Szeredi 	err = -ENOENT;
1562697f514dSMiklos Szeredi 	if (!inode)
1563697f514dSMiklos Szeredi 		goto out_path_put;
1564697f514dSMiklos Szeredi 
15657813b94aSLinus Torvalds 	if (should_follow_link(inode, follow)) {
156619660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
156719660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
1568697f514dSMiklos Szeredi 				err = -ECHILD;
1569697f514dSMiklos Szeredi 				goto out_err;
157019660af7SAl Viro 			}
157119660af7SAl Viro 		}
1572ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1573ce57dfc1SAl Viro 		return 1;
1574ce57dfc1SAl Viro 	}
1575ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1576ce57dfc1SAl Viro 	nd->inode = inode;
1577ce57dfc1SAl Viro 	return 0;
1578697f514dSMiklos Szeredi 
1579697f514dSMiklos Szeredi out_path_put:
1580697f514dSMiklos Szeredi 	path_to_nameidata(path, nd);
1581697f514dSMiklos Szeredi out_err:
1582697f514dSMiklos Szeredi 	terminate_walk(nd);
1583697f514dSMiklos Szeredi 	return err;
1584ce57dfc1SAl Viro }
1585ce57dfc1SAl Viro 
15861da177e4SLinus Torvalds /*
1587b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1588b356379aSAl Viro  * limiting consecutive symlinks to 40.
1589b356379aSAl Viro  *
1590b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1591b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1592b356379aSAl Viro  */
1593b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1594b356379aSAl Viro {
1595b356379aSAl Viro 	int res;
1596b356379aSAl Viro 
1597b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1598b356379aSAl Viro 		path_put_conditional(path, nd);
1599b356379aSAl Viro 		path_put(&nd->path);
1600b356379aSAl Viro 		return -ELOOP;
1601b356379aSAl Viro 	}
16021a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1603b356379aSAl Viro 
1604b356379aSAl Viro 	nd->depth++;
1605b356379aSAl Viro 	current->link_count++;
1606b356379aSAl Viro 
1607b356379aSAl Viro 	do {
1608b356379aSAl Viro 		struct path link = *path;
1609b356379aSAl Viro 		void *cookie;
1610574197e0SAl Viro 
1611574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
16126d7b5aaeSAl Viro 		if (res)
16136d7b5aaeSAl Viro 			break;
161421b9b073SAl Viro 		res = walk_component(nd, path, LOOKUP_FOLLOW);
1615574197e0SAl Viro 		put_link(nd, &link, cookie);
1616b356379aSAl Viro 	} while (res > 0);
1617b356379aSAl Viro 
1618b356379aSAl Viro 	current->link_count--;
1619b356379aSAl Viro 	nd->depth--;
1620b356379aSAl Viro 	return res;
1621b356379aSAl Viro }
1622b356379aSAl Viro 
1623b356379aSAl Viro /*
16243ddcd056SLinus Torvalds  * We really don't want to look at inode->i_op->lookup
16253ddcd056SLinus Torvalds  * when we don't have to. So we keep a cache bit in
16263ddcd056SLinus Torvalds  * the inode ->i_opflags field that says "yes, we can
16273ddcd056SLinus Torvalds  * do lookup on this inode".
16283ddcd056SLinus Torvalds  */
16293ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode)
16303ddcd056SLinus Torvalds {
16313ddcd056SLinus Torvalds 	if (likely(inode->i_opflags & IOP_LOOKUP))
16323ddcd056SLinus Torvalds 		return 1;
16333ddcd056SLinus Torvalds 	if (likely(!inode->i_op->lookup))
16343ddcd056SLinus Torvalds 		return 0;
16353ddcd056SLinus Torvalds 
16363ddcd056SLinus Torvalds 	/* We do this once for the lifetime of the inode */
16373ddcd056SLinus Torvalds 	spin_lock(&inode->i_lock);
16383ddcd056SLinus Torvalds 	inode->i_opflags |= IOP_LOOKUP;
16393ddcd056SLinus Torvalds 	spin_unlock(&inode->i_lock);
16403ddcd056SLinus Torvalds 	return 1;
16413ddcd056SLinus Torvalds }
16423ddcd056SLinus Torvalds 
1643bfcfaa77SLinus Torvalds /*
1644bfcfaa77SLinus Torvalds  * We can do the critical dentry name comparison and hashing
1645bfcfaa77SLinus Torvalds  * operations one word at a time, but we are limited to:
1646bfcfaa77SLinus Torvalds  *
1647bfcfaa77SLinus Torvalds  * - Architectures with fast unaligned word accesses. We could
1648bfcfaa77SLinus Torvalds  *   do a "get_unaligned()" if this helps and is sufficiently
1649bfcfaa77SLinus Torvalds  *   fast.
1650bfcfaa77SLinus Torvalds  *
1651bfcfaa77SLinus Torvalds  * - Little-endian machines (so that we can generate the mask
1652bfcfaa77SLinus Torvalds  *   of low bytes efficiently). Again, we *could* do a byte
1653bfcfaa77SLinus Torvalds  *   swapping load on big-endian architectures if that is not
1654bfcfaa77SLinus Torvalds  *   expensive enough to make the optimization worthless.
1655bfcfaa77SLinus Torvalds  *
1656bfcfaa77SLinus Torvalds  * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1657bfcfaa77SLinus Torvalds  *   do not trap on the (extremely unlikely) case of a page
1658bfcfaa77SLinus Torvalds  *   crossing operation.
1659bfcfaa77SLinus Torvalds  *
1660bfcfaa77SLinus Torvalds  * - Furthermore, we need an efficient 64-bit compile for the
1661bfcfaa77SLinus Torvalds  *   64-bit case in order to generate the "number of bytes in
1662bfcfaa77SLinus Torvalds  *   the final mask". Again, that could be replaced with a
1663bfcfaa77SLinus Torvalds  *   efficient population count instruction or similar.
1664bfcfaa77SLinus Torvalds  */
1665bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS
1666bfcfaa77SLinus Torvalds 
1667f68e556eSLinus Torvalds #include <asm/word-at-a-time.h>
1668bfcfaa77SLinus Torvalds 
1669f68e556eSLinus Torvalds #ifdef CONFIG_64BIT
1670bfcfaa77SLinus Torvalds 
1671bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash)
1672bfcfaa77SLinus Torvalds {
1673bfcfaa77SLinus Torvalds 	hash += hash >> (8*sizeof(int));
1674bfcfaa77SLinus Torvalds 	return hash;
1675bfcfaa77SLinus Torvalds }
1676bfcfaa77SLinus Torvalds 
1677bfcfaa77SLinus Torvalds #else	/* 32-bit case */
1678bfcfaa77SLinus Torvalds 
1679bfcfaa77SLinus Torvalds #define fold_hash(x) (x)
1680bfcfaa77SLinus Torvalds 
1681bfcfaa77SLinus Torvalds #endif
1682bfcfaa77SLinus Torvalds 
1683bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1684bfcfaa77SLinus Torvalds {
1685bfcfaa77SLinus Torvalds 	unsigned long a, mask;
1686bfcfaa77SLinus Torvalds 	unsigned long hash = 0;
1687bfcfaa77SLinus Torvalds 
1688bfcfaa77SLinus Torvalds 	for (;;) {
1689e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name);
1690bfcfaa77SLinus Torvalds 		if (len < sizeof(unsigned long))
1691bfcfaa77SLinus Torvalds 			break;
1692bfcfaa77SLinus Torvalds 		hash += a;
1693f132c5beSAl Viro 		hash *= 9;
1694bfcfaa77SLinus Torvalds 		name += sizeof(unsigned long);
1695bfcfaa77SLinus Torvalds 		len -= sizeof(unsigned long);
1696bfcfaa77SLinus Torvalds 		if (!len)
1697bfcfaa77SLinus Torvalds 			goto done;
1698bfcfaa77SLinus Torvalds 	}
1699bfcfaa77SLinus Torvalds 	mask = ~(~0ul << len*8);
1700bfcfaa77SLinus Torvalds 	hash += mask & a;
1701bfcfaa77SLinus Torvalds done:
1702bfcfaa77SLinus Torvalds 	return fold_hash(hash);
1703bfcfaa77SLinus Torvalds }
1704bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
1705bfcfaa77SLinus Torvalds 
1706bfcfaa77SLinus Torvalds /*
1707bfcfaa77SLinus Torvalds  * Calculate the length and hash of the path component, and
1708bfcfaa77SLinus Torvalds  * return the length of the component;
1709bfcfaa77SLinus Torvalds  */
1710bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1711bfcfaa77SLinus Torvalds {
171236126f8fSLinus Torvalds 	unsigned long a, b, adata, bdata, mask, hash, len;
171336126f8fSLinus Torvalds 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1714bfcfaa77SLinus Torvalds 
1715bfcfaa77SLinus Torvalds 	hash = a = 0;
1716bfcfaa77SLinus Torvalds 	len = -sizeof(unsigned long);
1717bfcfaa77SLinus Torvalds 	do {
1718bfcfaa77SLinus Torvalds 		hash = (hash + a) * 9;
1719bfcfaa77SLinus Torvalds 		len += sizeof(unsigned long);
1720e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name+len);
172136126f8fSLinus Torvalds 		b = a ^ REPEAT_BYTE('/');
172236126f8fSLinus Torvalds 	} while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1723bfcfaa77SLinus Torvalds 
172436126f8fSLinus Torvalds 	adata = prep_zero_mask(a, adata, &constants);
172536126f8fSLinus Torvalds 	bdata = prep_zero_mask(b, bdata, &constants);
172636126f8fSLinus Torvalds 
172736126f8fSLinus Torvalds 	mask = create_zero_mask(adata | bdata);
172836126f8fSLinus Torvalds 
172936126f8fSLinus Torvalds 	hash += a & zero_bytemask(mask);
1730bfcfaa77SLinus Torvalds 	*hashp = fold_hash(hash);
1731bfcfaa77SLinus Torvalds 
173236126f8fSLinus Torvalds 	return len + find_zero(mask);
1733bfcfaa77SLinus Torvalds }
1734bfcfaa77SLinus Torvalds 
1735bfcfaa77SLinus Torvalds #else
1736bfcfaa77SLinus Torvalds 
17370145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
17380145acc2SLinus Torvalds {
17390145acc2SLinus Torvalds 	unsigned long hash = init_name_hash();
17400145acc2SLinus Torvalds 	while (len--)
17410145acc2SLinus Torvalds 		hash = partial_name_hash(*name++, hash);
17420145acc2SLinus Torvalds 	return end_name_hash(hash);
17430145acc2SLinus Torvalds }
1744ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
17450145acc2SLinus Torvalds 
17463ddcd056SLinus Torvalds /*
1747200e9ef7SLinus Torvalds  * We know there's a real path component here of at least
1748200e9ef7SLinus Torvalds  * one character.
1749200e9ef7SLinus Torvalds  */
1750200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1751200e9ef7SLinus Torvalds {
1752200e9ef7SLinus Torvalds 	unsigned long hash = init_name_hash();
1753200e9ef7SLinus Torvalds 	unsigned long len = 0, c;
1754200e9ef7SLinus Torvalds 
1755200e9ef7SLinus Torvalds 	c = (unsigned char)*name;
1756200e9ef7SLinus Torvalds 	do {
1757200e9ef7SLinus Torvalds 		len++;
1758200e9ef7SLinus Torvalds 		hash = partial_name_hash(c, hash);
1759200e9ef7SLinus Torvalds 		c = (unsigned char)name[len];
1760200e9ef7SLinus Torvalds 	} while (c && c != '/');
1761200e9ef7SLinus Torvalds 	*hashp = end_name_hash(hash);
1762200e9ef7SLinus Torvalds 	return len;
1763200e9ef7SLinus Torvalds }
1764200e9ef7SLinus Torvalds 
1765bfcfaa77SLinus Torvalds #endif
1766bfcfaa77SLinus Torvalds 
1767200e9ef7SLinus Torvalds /*
17681da177e4SLinus Torvalds  * Name resolution.
1769ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1770ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
17711da177e4SLinus Torvalds  *
1772ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1773ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
17741da177e4SLinus Torvalds  */
17756de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
17761da177e4SLinus Torvalds {
17771da177e4SLinus Torvalds 	struct path next;
17781da177e4SLinus Torvalds 	int err;
17791da177e4SLinus Torvalds 
17801da177e4SLinus Torvalds 	while (*name=='/')
17811da177e4SLinus Torvalds 		name++;
17821da177e4SLinus Torvalds 	if (!*name)
1783086e183aSAl Viro 		return 0;
17841da177e4SLinus Torvalds 
17851da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
17861da177e4SLinus Torvalds 	for(;;) {
17871da177e4SLinus Torvalds 		struct qstr this;
1788200e9ef7SLinus Torvalds 		long len;
1789fe479a58SAl Viro 		int type;
17901da177e4SLinus Torvalds 
179152094c8aSAl Viro 		err = may_lookup(nd);
17921da177e4SLinus Torvalds  		if (err)
17931da177e4SLinus Torvalds 			break;
17941da177e4SLinus Torvalds 
1795200e9ef7SLinus Torvalds 		len = hash_name(name, &this.hash);
17961da177e4SLinus Torvalds 		this.name = name;
1797200e9ef7SLinus Torvalds 		this.len = len;
17981da177e4SLinus Torvalds 
1799fe479a58SAl Viro 		type = LAST_NORM;
1800200e9ef7SLinus Torvalds 		if (name[0] == '.') switch (len) {
1801fe479a58SAl Viro 			case 2:
1802200e9ef7SLinus Torvalds 				if (name[1] == '.') {
1803fe479a58SAl Viro 					type = LAST_DOTDOT;
180416c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
180516c2cd71SAl Viro 				}
1806fe479a58SAl Viro 				break;
1807fe479a58SAl Viro 			case 1:
1808fe479a58SAl Viro 				type = LAST_DOT;
1809fe479a58SAl Viro 		}
18105a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
18115a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
181216c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
18135a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1814da53be12SLinus Torvalds 				err = parent->d_op->d_hash(parent, &this);
18155a202bcdSAl Viro 				if (err < 0)
18165a202bcdSAl Viro 					break;
18175a202bcdSAl Viro 			}
18185a202bcdSAl Viro 		}
1819fe479a58SAl Viro 
18205f4a6a69SAl Viro 		nd->last = this;
18215f4a6a69SAl Viro 		nd->last_type = type;
18225f4a6a69SAl Viro 
1823200e9ef7SLinus Torvalds 		if (!name[len])
18245f4a6a69SAl Viro 			return 0;
1825200e9ef7SLinus Torvalds 		/*
1826200e9ef7SLinus Torvalds 		 * If it wasn't NUL, we know it was '/'. Skip that
1827200e9ef7SLinus Torvalds 		 * slash, and continue until no more slashes.
1828200e9ef7SLinus Torvalds 		 */
1829200e9ef7SLinus Torvalds 		do {
1830200e9ef7SLinus Torvalds 			len++;
1831200e9ef7SLinus Torvalds 		} while (unlikely(name[len] == '/'));
1832200e9ef7SLinus Torvalds 		if (!name[len])
18335f4a6a69SAl Viro 			return 0;
18345f4a6a69SAl Viro 
1835200e9ef7SLinus Torvalds 		name += len;
18361da177e4SLinus Torvalds 
183721b9b073SAl Viro 		err = walk_component(nd, &next, LOOKUP_FOLLOW);
1838ce57dfc1SAl Viro 		if (err < 0)
1839ce57dfc1SAl Viro 			return err;
1840fe479a58SAl Viro 
1841ce57dfc1SAl Viro 		if (err) {
1842b356379aSAl Viro 			err = nested_symlink(&next, nd);
18431da177e4SLinus Torvalds 			if (err)
1844a7472babSAl Viro 				return err;
184531e6b01fSNick Piggin 		}
18465f4a6a69SAl Viro 		if (!can_lookup(nd->inode)) {
18473ddcd056SLinus Torvalds 			err = -ENOTDIR;
18483ddcd056SLinus Torvalds 			break;
18495f4a6a69SAl Viro 		}
1850ce57dfc1SAl Viro 	}
1851951361f9SAl Viro 	terminate_walk(nd);
18521da177e4SLinus Torvalds 	return err;
18531da177e4SLinus Torvalds }
18541da177e4SLinus Torvalds 
185570e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
185670e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
185731e6b01fSNick Piggin {
185831e6b01fSNick Piggin 	int retval = 0;
185931e6b01fSNick Piggin 
186031e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
186116c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
186231e6b01fSNick Piggin 	nd->depth = 0;
18635b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
18645b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
186573d049a4SAl Viro 		if (*name) {
1866741b7c3fSAl Viro 			if (!can_lookup(inode))
18675b6ca027SAl Viro 				return -ENOTDIR;
18685b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
18695b6ca027SAl Viro 			if (retval)
18705b6ca027SAl Viro 				return retval;
187173d049a4SAl Viro 		}
18725b6ca027SAl Viro 		nd->path = nd->root;
18735b6ca027SAl Viro 		nd->inode = inode;
18745b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
187532a7991bSAl Viro 			lock_rcu_walk();
18765b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
18775b6ca027SAl Viro 		} else {
18785b6ca027SAl Viro 			path_get(&nd->path);
18795b6ca027SAl Viro 		}
18805b6ca027SAl Viro 		return 0;
18815b6ca027SAl Viro 	}
18825b6ca027SAl Viro 
188331e6b01fSNick Piggin 	nd->root.mnt = NULL;
188431e6b01fSNick Piggin 
188531e6b01fSNick Piggin 	if (*name=='/') {
1886e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
188732a7991bSAl Viro 			lock_rcu_walk();
1888e41f7d4eSAl Viro 			set_root_rcu(nd);
1889e41f7d4eSAl Viro 		} else {
1890e41f7d4eSAl Viro 			set_root(nd);
1891e41f7d4eSAl Viro 			path_get(&nd->root);
1892e41f7d4eSAl Viro 		}
189331e6b01fSNick Piggin 		nd->path = nd->root;
189431e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1895e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
189631e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1897c28cc364SNick Piggin 			unsigned seq;
189831e6b01fSNick Piggin 
189932a7991bSAl Viro 			lock_rcu_walk();
190031e6b01fSNick Piggin 
1901c28cc364SNick Piggin 			do {
1902c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
190331e6b01fSNick Piggin 				nd->path = fs->pwd;
1904c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1905c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1906e41f7d4eSAl Viro 		} else {
1907e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1908e41f7d4eSAl Viro 		}
190931e6b01fSNick Piggin 	} else {
1910582aa64aSJeff Layton 		/* Caller must check execute permissions on the starting path component */
19112903ff01SAl Viro 		struct fd f = fdget_raw(dfd);
191231e6b01fSNick Piggin 		struct dentry *dentry;
191331e6b01fSNick Piggin 
19142903ff01SAl Viro 		if (!f.file)
19152903ff01SAl Viro 			return -EBADF;
191631e6b01fSNick Piggin 
19172903ff01SAl Viro 		dentry = f.file->f_path.dentry;
191831e6b01fSNick Piggin 
1919f52e0c11SAl Viro 		if (*name) {
1920741b7c3fSAl Viro 			if (!can_lookup(dentry->d_inode)) {
19212903ff01SAl Viro 				fdput(f);
19222903ff01SAl Viro 				return -ENOTDIR;
1923f52e0c11SAl Viro 			}
19242903ff01SAl Viro 		}
19252903ff01SAl Viro 
19262903ff01SAl Viro 		nd->path = f.file->f_path;
1927e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
19282903ff01SAl Viro 			if (f.need_put)
19292903ff01SAl Viro 				*fp = f.file;
1930c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
193132a7991bSAl Viro 			lock_rcu_walk();
19325590ff0dSUlrich Drepper 		} else {
19332903ff01SAl Viro 			path_get(&nd->path);
19342903ff01SAl Viro 			fdput(f);
19351da177e4SLinus Torvalds 		}
1936e41f7d4eSAl Viro 	}
1937e41f7d4eSAl Viro 
193831e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
19399b4a9b14SAl Viro 	return 0;
19409b4a9b14SAl Viro }
19419b4a9b14SAl Viro 
1942bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1943bd92d7feSAl Viro {
1944bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1945bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1946bd92d7feSAl Viro 
1947bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
194821b9b073SAl Viro 	return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
1949bd92d7feSAl Viro }
1950bd92d7feSAl Viro 
19519b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1952ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
19539b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
19549b4a9b14SAl Viro {
195570e9b357SAl Viro 	struct file *base = NULL;
1956bd92d7feSAl Viro 	struct path path;
1957bd92d7feSAl Viro 	int err;
195831e6b01fSNick Piggin 
195931e6b01fSNick Piggin 	/*
196031e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
196131e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
196231e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
196331e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
196431e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
196531e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
196631e6b01fSNick Piggin 	 * analogue, foo_rcu().
196731e6b01fSNick Piggin 	 *
196831e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
196931e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
197031e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
197131e6b01fSNick Piggin 	 * be able to complete).
197231e6b01fSNick Piggin 	 */
1973bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1974ee0827cdSAl Viro 
1975bd92d7feSAl Viro 	if (unlikely(err))
1976bd92d7feSAl Viro 		return err;
1977ee0827cdSAl Viro 
1978ee0827cdSAl Viro 	current->total_link_count = 0;
1979bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1980bd92d7feSAl Viro 
1981bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1982bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1983bd92d7feSAl Viro 		while (err > 0) {
1984bd92d7feSAl Viro 			void *cookie;
1985bd92d7feSAl Viro 			struct path link = path;
1986800179c9SKees Cook 			err = may_follow_link(&link, nd);
1987800179c9SKees Cook 			if (unlikely(err))
1988800179c9SKees Cook 				break;
1989bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1990574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
19916d7b5aaeSAl Viro 			if (err)
19926d7b5aaeSAl Viro 				break;
1993bd92d7feSAl Viro 			err = lookup_last(nd, &path);
1994574197e0SAl Viro 			put_link(nd, &link, cookie);
1995bd92d7feSAl Viro 		}
1996bd92d7feSAl Viro 	}
1997ee0827cdSAl Viro 
19989f1fafeeSAl Viro 	if (!err)
19999f1fafeeSAl Viro 		err = complete_walk(nd);
2000bd92d7feSAl Viro 
2001bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
200205252901SAl Viro 		if (!can_lookup(nd->inode)) {
2003bd92d7feSAl Viro 			path_put(&nd->path);
2004bd23a539SAl Viro 			err = -ENOTDIR;
2005bd92d7feSAl Viro 		}
2006bd92d7feSAl Viro 	}
200716c2cd71SAl Viro 
200870e9b357SAl Viro 	if (base)
200970e9b357SAl Viro 		fput(base);
2010ee0827cdSAl Viro 
20115b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
201231e6b01fSNick Piggin 		path_put(&nd->root);
201331e6b01fSNick Piggin 		nd->root.mnt = NULL;
201431e6b01fSNick Piggin 	}
2015bd92d7feSAl Viro 	return err;
201631e6b01fSNick Piggin }
201731e6b01fSNick Piggin 
2018873f1eedSJeff Layton static int filename_lookup(int dfd, struct filename *name,
2019873f1eedSJeff Layton 				unsigned int flags, struct nameidata *nd)
2020873f1eedSJeff Layton {
2021873f1eedSJeff Layton 	int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
2022873f1eedSJeff Layton 	if (unlikely(retval == -ECHILD))
2023873f1eedSJeff Layton 		retval = path_lookupat(dfd, name->name, flags, nd);
2024873f1eedSJeff Layton 	if (unlikely(retval == -ESTALE))
2025873f1eedSJeff Layton 		retval = path_lookupat(dfd, name->name,
2026873f1eedSJeff Layton 						flags | LOOKUP_REVAL, nd);
2027873f1eedSJeff Layton 
2028873f1eedSJeff Layton 	if (likely(!retval))
2029adb5c247SJeff Layton 		audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
2030873f1eedSJeff Layton 	return retval;
2031873f1eedSJeff Layton }
2032873f1eedSJeff Layton 
2033ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
2034ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
2035ee0827cdSAl Viro {
2036873f1eedSJeff Layton 	struct filename filename = { .name = name };
2037ee0827cdSAl Viro 
2038873f1eedSJeff Layton 	return filename_lookup(dfd, &filename, flags, nd);
20391da177e4SLinus Torvalds }
20401da177e4SLinus Torvalds 
204179714f72SAl Viro /* does lookup, returns the object with parent locked */
204279714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path)
20435590ff0dSUlrich Drepper {
204479714f72SAl Viro 	struct nameidata nd;
204579714f72SAl Viro 	struct dentry *d;
204679714f72SAl Viro 	int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
204779714f72SAl Viro 	if (err)
204879714f72SAl Viro 		return ERR_PTR(err);
204979714f72SAl Viro 	if (nd.last_type != LAST_NORM) {
205079714f72SAl Viro 		path_put(&nd.path);
205179714f72SAl Viro 		return ERR_PTR(-EINVAL);
205279714f72SAl Viro 	}
205379714f72SAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
20541e0ea001SAl Viro 	d = __lookup_hash(&nd.last, nd.path.dentry, 0);
205579714f72SAl Viro 	if (IS_ERR(d)) {
205679714f72SAl Viro 		mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
205779714f72SAl Viro 		path_put(&nd.path);
205879714f72SAl Viro 		return d;
205979714f72SAl Viro 	}
206079714f72SAl Viro 	*path = nd.path;
206179714f72SAl Viro 	return d;
20625590ff0dSUlrich Drepper }
20635590ff0dSUlrich Drepper 
2064d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
2065d1811465SAl Viro {
2066d1811465SAl Viro 	struct nameidata nd;
2067d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2068d1811465SAl Viro 	if (!res)
2069d1811465SAl Viro 		*path = nd.path;
2070d1811465SAl Viro 	return res;
2071d1811465SAl Viro }
2072d1811465SAl Viro 
207316f18200SJosef 'Jeff' Sipek /**
207416f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
207516f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
207616f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
207716f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
207816f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
2079e0a01249SAl Viro  * @path: pointer to struct path to fill
208016f18200SJosef 'Jeff' Sipek  */
208116f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
208216f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
2083e0a01249SAl Viro 		    struct path *path)
208416f18200SJosef 'Jeff' Sipek {
2085e0a01249SAl Viro 	struct nameidata nd;
2086e0a01249SAl Viro 	int err;
2087e0a01249SAl Viro 	nd.root.dentry = dentry;
2088e0a01249SAl Viro 	nd.root.mnt = mnt;
2089e0a01249SAl Viro 	BUG_ON(flags & LOOKUP_PARENT);
20905b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
2091e0a01249SAl Viro 	err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2092e0a01249SAl Viro 	if (!err)
2093e0a01249SAl Viro 		*path = nd.path;
2094e0a01249SAl Viro 	return err;
209516f18200SJosef 'Jeff' Sipek }
209616f18200SJosef 'Jeff' Sipek 
2097057f6c01SJames Morris /*
2098057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
2099057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
2100057f6c01SJames Morris  * SMP-safe.
2101057f6c01SJames Morris  */
2102a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
21031da177e4SLinus Torvalds {
210472bd866aSAl Viro 	return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
21051da177e4SLinus Torvalds }
21061da177e4SLinus Torvalds 
2107eead1911SChristoph Hellwig /**
2108a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
2109eead1911SChristoph Hellwig  * @name:	pathname component to lookup
2110eead1911SChristoph Hellwig  * @base:	base directory to lookup from
2111eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
2112eead1911SChristoph Hellwig  *
2113a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
2114a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
2115eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
2116eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
2117eead1911SChristoph Hellwig  */
2118057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2119057f6c01SJames Morris {
2120057f6c01SJames Morris 	struct qstr this;
21216a96ba54SAl Viro 	unsigned int c;
2122cda309deSMiklos Szeredi 	int err;
2123057f6c01SJames Morris 
21242f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
21252f9092e1SDavid Woodhouse 
21266a96ba54SAl Viro 	this.name = name;
21276a96ba54SAl Viro 	this.len = len;
21280145acc2SLinus Torvalds 	this.hash = full_name_hash(name, len);
21296a96ba54SAl Viro 	if (!len)
21306a96ba54SAl Viro 		return ERR_PTR(-EACCES);
21316a96ba54SAl Viro 
213221d8a15aSAl Viro 	if (unlikely(name[0] == '.')) {
213321d8a15aSAl Viro 		if (len < 2 || (len == 2 && name[1] == '.'))
213421d8a15aSAl Viro 			return ERR_PTR(-EACCES);
213521d8a15aSAl Viro 	}
213621d8a15aSAl Viro 
21376a96ba54SAl Viro 	while (len--) {
21386a96ba54SAl Viro 		c = *(const unsigned char *)name++;
21396a96ba54SAl Viro 		if (c == '/' || c == '\0')
21406a96ba54SAl Viro 			return ERR_PTR(-EACCES);
21416a96ba54SAl Viro 	}
21425a202bcdSAl Viro 	/*
21435a202bcdSAl Viro 	 * See if the low-level filesystem might want
21445a202bcdSAl Viro 	 * to use its own hash..
21455a202bcdSAl Viro 	 */
21465a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
2147da53be12SLinus Torvalds 		int err = base->d_op->d_hash(base, &this);
21485a202bcdSAl Viro 		if (err < 0)
21495a202bcdSAl Viro 			return ERR_PTR(err);
21505a202bcdSAl Viro 	}
2151eead1911SChristoph Hellwig 
2152cda309deSMiklos Szeredi 	err = inode_permission(base->d_inode, MAY_EXEC);
2153cda309deSMiklos Szeredi 	if (err)
2154cda309deSMiklos Szeredi 		return ERR_PTR(err);
2155cda309deSMiklos Szeredi 
215672bd866aSAl Viro 	return __lookup_hash(&this, base, 0);
2157057f6c01SJames Morris }
2158057f6c01SJames Morris 
21591fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
21601fa1e7f6SAndy Whitcroft 		 struct path *path, int *empty)
21611da177e4SLinus Torvalds {
21622d8f3038SAl Viro 	struct nameidata nd;
216391a27b2aSJeff Layton 	struct filename *tmp = getname_flags(name, flags, empty);
21641da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
21651da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
21662d8f3038SAl Viro 
21672d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
21682d8f3038SAl Viro 
2169873f1eedSJeff Layton 		err = filename_lookup(dfd, tmp, flags, &nd);
21701da177e4SLinus Torvalds 		putname(tmp);
21712d8f3038SAl Viro 		if (!err)
21722d8f3038SAl Viro 			*path = nd.path;
21731da177e4SLinus Torvalds 	}
21741da177e4SLinus Torvalds 	return err;
21751da177e4SLinus Torvalds }
21761da177e4SLinus Torvalds 
21771fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags,
21781fa1e7f6SAndy Whitcroft 		 struct path *path)
21791fa1e7f6SAndy Whitcroft {
2180f7493e5dSLinus Torvalds 	return user_path_at_empty(dfd, name, flags, path, NULL);
21811fa1e7f6SAndy Whitcroft }
21821fa1e7f6SAndy Whitcroft 
2183873f1eedSJeff Layton /*
2184873f1eedSJeff Layton  * NB: most callers don't do anything directly with the reference to the
2185873f1eedSJeff Layton  *     to struct filename, but the nd->last pointer points into the name string
2186873f1eedSJeff Layton  *     allocated by getname. So we must hold the reference to it until all
2187873f1eedSJeff Layton  *     path-walking is complete.
2188873f1eedSJeff Layton  */
218991a27b2aSJeff Layton static struct filename *
21909e790bd6SJeff Layton user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
21919e790bd6SJeff Layton 		 unsigned int flags)
21922ad94ae6SAl Viro {
219391a27b2aSJeff Layton 	struct filename *s = getname(path);
21942ad94ae6SAl Viro 	int error;
21952ad94ae6SAl Viro 
21969e790bd6SJeff Layton 	/* only LOOKUP_REVAL is allowed in extra flags */
21979e790bd6SJeff Layton 	flags &= LOOKUP_REVAL;
21989e790bd6SJeff Layton 
21992ad94ae6SAl Viro 	if (IS_ERR(s))
220091a27b2aSJeff Layton 		return s;
22012ad94ae6SAl Viro 
22029e790bd6SJeff Layton 	error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
220391a27b2aSJeff Layton 	if (error) {
22042ad94ae6SAl Viro 		putname(s);
220591a27b2aSJeff Layton 		return ERR_PTR(error);
220691a27b2aSJeff Layton 	}
22072ad94ae6SAl Viro 
220891a27b2aSJeff Layton 	return s;
22092ad94ae6SAl Viro }
22102ad94ae6SAl Viro 
22118033426eSJeff Layton /**
22128033426eSJeff Layton  * umount_lookup_last - look up last component for umount
22138033426eSJeff Layton  * @nd:   pathwalk nameidata - currently pointing at parent directory of "last"
22148033426eSJeff Layton  * @path: pointer to container for result
22158033426eSJeff Layton  *
22168033426eSJeff Layton  * This is a special lookup_last function just for umount. In this case, we
22178033426eSJeff Layton  * need to resolve the path without doing any revalidation.
22188033426eSJeff Layton  *
22198033426eSJeff Layton  * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
22208033426eSJeff Layton  * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
22218033426eSJeff Layton  * in almost all cases, this lookup will be served out of the dcache. The only
22228033426eSJeff Layton  * cases where it won't are if nd->last refers to a symlink or the path is
22238033426eSJeff Layton  * bogus and it doesn't exist.
22248033426eSJeff Layton  *
22258033426eSJeff Layton  * Returns:
22268033426eSJeff Layton  * -error: if there was an error during lookup. This includes -ENOENT if the
22278033426eSJeff Layton  *         lookup found a negative dentry. The nd->path reference will also be
22288033426eSJeff Layton  *         put in this case.
22298033426eSJeff Layton  *
22308033426eSJeff Layton  * 0:      if we successfully resolved nd->path and found it to not to be a
22318033426eSJeff Layton  *         symlink that needs to be followed. "path" will also be populated.
22328033426eSJeff Layton  *         The nd->path reference will also be put.
22338033426eSJeff Layton  *
22348033426eSJeff Layton  * 1:      if we successfully resolved nd->last and found it to be a symlink
22358033426eSJeff Layton  *         that needs to be followed. "path" will be populated with the path
22368033426eSJeff Layton  *         to the link, and nd->path will *not* be put.
22378033426eSJeff Layton  */
22388033426eSJeff Layton static int
22398033426eSJeff Layton umount_lookup_last(struct nameidata *nd, struct path *path)
22408033426eSJeff Layton {
22418033426eSJeff Layton 	int error = 0;
22428033426eSJeff Layton 	struct dentry *dentry;
22438033426eSJeff Layton 	struct dentry *dir = nd->path.dentry;
22448033426eSJeff Layton 
22458033426eSJeff Layton 	if (unlikely(nd->flags & LOOKUP_RCU)) {
22468033426eSJeff Layton 		WARN_ON_ONCE(1);
22478033426eSJeff Layton 		error = -ECHILD;
22488033426eSJeff Layton 		goto error_check;
22498033426eSJeff Layton 	}
22508033426eSJeff Layton 
22518033426eSJeff Layton 	nd->flags &= ~LOOKUP_PARENT;
22528033426eSJeff Layton 
22538033426eSJeff Layton 	if (unlikely(nd->last_type != LAST_NORM)) {
22548033426eSJeff Layton 		error = handle_dots(nd, nd->last_type);
22558033426eSJeff Layton 		if (!error)
22568033426eSJeff Layton 			dentry = dget(nd->path.dentry);
22578033426eSJeff Layton 		goto error_check;
22588033426eSJeff Layton 	}
22598033426eSJeff Layton 
22608033426eSJeff Layton 	mutex_lock(&dir->d_inode->i_mutex);
22618033426eSJeff Layton 	dentry = d_lookup(dir, &nd->last);
22628033426eSJeff Layton 	if (!dentry) {
22638033426eSJeff Layton 		/*
22648033426eSJeff Layton 		 * No cached dentry. Mounted dentries are pinned in the cache,
22658033426eSJeff Layton 		 * so that means that this dentry is probably a symlink or the
22668033426eSJeff Layton 		 * path doesn't actually point to a mounted dentry.
22678033426eSJeff Layton 		 */
22688033426eSJeff Layton 		dentry = d_alloc(dir, &nd->last);
22698033426eSJeff Layton 		if (!dentry) {
22708033426eSJeff Layton 			error = -ENOMEM;
22718033426eSJeff Layton 		} else {
22728033426eSJeff Layton 			dentry = lookup_real(dir->d_inode, dentry, nd->flags);
22738033426eSJeff Layton 			if (IS_ERR(dentry))
22748033426eSJeff Layton 				error = PTR_ERR(dentry);
22758033426eSJeff Layton 		}
22768033426eSJeff Layton 	}
22778033426eSJeff Layton 	mutex_unlock(&dir->d_inode->i_mutex);
22788033426eSJeff Layton 
22798033426eSJeff Layton error_check:
22808033426eSJeff Layton 	if (!error) {
22818033426eSJeff Layton 		if (!dentry->d_inode) {
22828033426eSJeff Layton 			error = -ENOENT;
22838033426eSJeff Layton 			dput(dentry);
22848033426eSJeff Layton 		} else {
22858033426eSJeff Layton 			path->dentry = dentry;
22868033426eSJeff Layton 			path->mnt = mntget(nd->path.mnt);
22878033426eSJeff Layton 			if (should_follow_link(dentry->d_inode,
22888033426eSJeff Layton 						nd->flags & LOOKUP_FOLLOW))
22898033426eSJeff Layton 				return 1;
22908033426eSJeff Layton 			follow_mount(path);
22918033426eSJeff Layton 		}
22928033426eSJeff Layton 	}
22938033426eSJeff Layton 	terminate_walk(nd);
22948033426eSJeff Layton 	return error;
22958033426eSJeff Layton }
22968033426eSJeff Layton 
22978033426eSJeff Layton /**
22988033426eSJeff Layton  * path_umountat - look up a path to be umounted
22998033426eSJeff Layton  * @dfd:	directory file descriptor to start walk from
23008033426eSJeff Layton  * @name:	full pathname to walk
23018033426eSJeff Layton  * @flags:	lookup flags
23028033426eSJeff Layton  * @nd:		pathwalk nameidata
23038033426eSJeff Layton  *
23048033426eSJeff Layton  * Look up the given name, but don't attempt to revalidate the last component.
23058033426eSJeff Layton  * Returns 0 and "path" will be valid on success; Retuns error otherwise.
23068033426eSJeff Layton  */
23078033426eSJeff Layton static int
23088033426eSJeff Layton path_umountat(int dfd, const char *name, struct path *path, unsigned int flags)
23098033426eSJeff Layton {
23108033426eSJeff Layton 	struct file *base = NULL;
23118033426eSJeff Layton 	struct nameidata nd;
23128033426eSJeff Layton 	int err;
23138033426eSJeff Layton 
23148033426eSJeff Layton 	err = path_init(dfd, name, flags | LOOKUP_PARENT, &nd, &base);
23158033426eSJeff Layton 	if (unlikely(err))
23168033426eSJeff Layton 		return err;
23178033426eSJeff Layton 
23188033426eSJeff Layton 	current->total_link_count = 0;
23198033426eSJeff Layton 	err = link_path_walk(name, &nd);
23208033426eSJeff Layton 	if (err)
23218033426eSJeff Layton 		goto out;
23228033426eSJeff Layton 
23238033426eSJeff Layton 	/* If we're in rcuwalk, drop out of it to handle last component */
23248033426eSJeff Layton 	if (nd.flags & LOOKUP_RCU) {
23258033426eSJeff Layton 		err = unlazy_walk(&nd, NULL);
23268033426eSJeff Layton 		if (err) {
23278033426eSJeff Layton 			terminate_walk(&nd);
23288033426eSJeff Layton 			goto out;
23298033426eSJeff Layton 		}
23308033426eSJeff Layton 	}
23318033426eSJeff Layton 
23328033426eSJeff Layton 	err = umount_lookup_last(&nd, path);
23338033426eSJeff Layton 	while (err > 0) {
23348033426eSJeff Layton 		void *cookie;
23358033426eSJeff Layton 		struct path link = *path;
23368033426eSJeff Layton 		err = may_follow_link(&link, &nd);
23378033426eSJeff Layton 		if (unlikely(err))
23388033426eSJeff Layton 			break;
23398033426eSJeff Layton 		nd.flags |= LOOKUP_PARENT;
23408033426eSJeff Layton 		err = follow_link(&link, &nd, &cookie);
23418033426eSJeff Layton 		if (err)
23428033426eSJeff Layton 			break;
23438033426eSJeff Layton 		err = umount_lookup_last(&nd, path);
23448033426eSJeff Layton 		put_link(&nd, &link, cookie);
23458033426eSJeff Layton 	}
23468033426eSJeff Layton out:
23478033426eSJeff Layton 	if (base)
23488033426eSJeff Layton 		fput(base);
23498033426eSJeff Layton 
23508033426eSJeff Layton 	if (nd.root.mnt && !(nd.flags & LOOKUP_ROOT))
23518033426eSJeff Layton 		path_put(&nd.root);
23528033426eSJeff Layton 
23538033426eSJeff Layton 	return err;
23548033426eSJeff Layton }
23558033426eSJeff Layton 
23568033426eSJeff Layton /**
23578033426eSJeff Layton  * user_path_umountat - lookup a path from userland in order to umount it
23588033426eSJeff Layton  * @dfd:	directory file descriptor
23598033426eSJeff Layton  * @name:	pathname from userland
23608033426eSJeff Layton  * @flags:	lookup flags
23618033426eSJeff Layton  * @path:	pointer to container to hold result
23628033426eSJeff Layton  *
23638033426eSJeff Layton  * A umount is a special case for path walking. We're not actually interested
23648033426eSJeff Layton  * in the inode in this situation, and ESTALE errors can be a problem. We
23658033426eSJeff Layton  * simply want track down the dentry and vfsmount attached at the mountpoint
23668033426eSJeff Layton  * and avoid revalidating the last component.
23678033426eSJeff Layton  *
23688033426eSJeff Layton  * Returns 0 and populates "path" on success.
23698033426eSJeff Layton  */
23708033426eSJeff Layton int
23718033426eSJeff Layton user_path_umountat(int dfd, const char __user *name, unsigned int flags,
23728033426eSJeff Layton 			struct path *path)
23738033426eSJeff Layton {
23748033426eSJeff Layton 	struct filename *s = getname(name);
23758033426eSJeff Layton 	int error;
23768033426eSJeff Layton 
23778033426eSJeff Layton 	if (IS_ERR(s))
23788033426eSJeff Layton 		return PTR_ERR(s);
23798033426eSJeff Layton 
23808033426eSJeff Layton 	error = path_umountat(dfd, s->name, path, flags | LOOKUP_RCU);
23818033426eSJeff Layton 	if (unlikely(error == -ECHILD))
23828033426eSJeff Layton 		error = path_umountat(dfd, s->name, path, flags);
23838033426eSJeff Layton 	if (unlikely(error == -ESTALE))
23848033426eSJeff Layton 		error = path_umountat(dfd, s->name, path, flags | LOOKUP_REVAL);
23858033426eSJeff Layton 
23868033426eSJeff Layton 	if (likely(!error))
23878033426eSJeff Layton 		audit_inode(s, path->dentry, 0);
23888033426eSJeff Layton 
23898033426eSJeff Layton 	putname(s);
23908033426eSJeff Layton 	return error;
23918033426eSJeff Layton }
23928033426eSJeff Layton 
23931da177e4SLinus Torvalds /*
23941da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
23951da177e4SLinus Torvalds  * minimal.
23961da177e4SLinus Torvalds  */
23971da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
23981da177e4SLinus Torvalds {
23998e96e3b7SEric W. Biederman 	kuid_t fsuid = current_fsuid();
2400da9592edSDavid Howells 
24011da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
24021da177e4SLinus Torvalds 		return 0;
24038e96e3b7SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
24041da177e4SLinus Torvalds 		return 0;
24058e96e3b7SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
24061da177e4SLinus Torvalds 		return 0;
24071a48e2acSEric W. Biederman 	return !inode_capable(inode, CAP_FOWNER);
24081da177e4SLinus Torvalds }
24091da177e4SLinus Torvalds 
24101da177e4SLinus Torvalds /*
24111da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
24121da177e4SLinus Torvalds  *  whether the type of victim is right.
24131da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
24141da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
24151da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
24161da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
24171da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
24181da177e4SLinus Torvalds  *	a. be owner of dir, or
24191da177e4SLinus Torvalds  *	b. be owner of victim, or
24201da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
24211da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
24221da177e4SLinus Torvalds  *     links pointing to it.
24231da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
24241da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
24251da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
24261da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
24271da177e4SLinus Torvalds  *     nfs_async_unlink().
24281da177e4SLinus Torvalds  */
2429858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
24301da177e4SLinus Torvalds {
24311da177e4SLinus Torvalds 	int error;
24321da177e4SLinus Torvalds 
24331da177e4SLinus Torvalds 	if (!victim->d_inode)
24341da177e4SLinus Torvalds 		return -ENOENT;
24351da177e4SLinus Torvalds 
24361da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
24374fa6b5ecSJeff Layton 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
24381da177e4SLinus Torvalds 
2439f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
24401da177e4SLinus Torvalds 	if (error)
24411da177e4SLinus Torvalds 		return error;
24421da177e4SLinus Torvalds 	if (IS_APPEND(dir))
24431da177e4SLinus Torvalds 		return -EPERM;
24441da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
2445f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
24461da177e4SLinus Torvalds 		return -EPERM;
24471da177e4SLinus Torvalds 	if (isdir) {
24481da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
24491da177e4SLinus Torvalds 			return -ENOTDIR;
24501da177e4SLinus Torvalds 		if (IS_ROOT(victim))
24511da177e4SLinus Torvalds 			return -EBUSY;
24521da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
24531da177e4SLinus Torvalds 		return -EISDIR;
24541da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
24551da177e4SLinus Torvalds 		return -ENOENT;
24561da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
24571da177e4SLinus Torvalds 		return -EBUSY;
24581da177e4SLinus Torvalds 	return 0;
24591da177e4SLinus Torvalds }
24601da177e4SLinus Torvalds 
24611da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
24621da177e4SLinus Torvalds  *  dir.
24631da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
24641da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
24651da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
24661da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
24671da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
24681da177e4SLinus Torvalds  */
2469a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
24701da177e4SLinus Torvalds {
24711da177e4SLinus Torvalds 	if (child->d_inode)
24721da177e4SLinus Torvalds 		return -EEXIST;
24731da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
24741da177e4SLinus Torvalds 		return -ENOENT;
2475f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
24761da177e4SLinus Torvalds }
24771da177e4SLinus Torvalds 
24781da177e4SLinus Torvalds /*
24791da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
24801da177e4SLinus Torvalds  */
24811da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
24821da177e4SLinus Torvalds {
24831da177e4SLinus Torvalds 	struct dentry *p;
24841da177e4SLinus Torvalds 
24851da177e4SLinus Torvalds 	if (p1 == p2) {
2486f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
24871da177e4SLinus Torvalds 		return NULL;
24881da177e4SLinus Torvalds 	}
24891da177e4SLinus Torvalds 
2490a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
24911da177e4SLinus Torvalds 
2492e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
2493e2761a11SOGAWA Hirofumi 	if (p) {
2494f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2495f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
24961da177e4SLinus Torvalds 		return p;
24971da177e4SLinus Torvalds 	}
24981da177e4SLinus Torvalds 
2499e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
2500e2761a11SOGAWA Hirofumi 	if (p) {
2501f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2502f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
25031da177e4SLinus Torvalds 		return p;
25041da177e4SLinus Torvalds 	}
25051da177e4SLinus Torvalds 
2506f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2507f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
25081da177e4SLinus Torvalds 	return NULL;
25091da177e4SLinus Torvalds }
25101da177e4SLinus Torvalds 
25111da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
25121da177e4SLinus Torvalds {
25131b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
25141da177e4SLinus Torvalds 	if (p1 != p2) {
25151b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
2516a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
25171da177e4SLinus Torvalds 	}
25181da177e4SLinus Torvalds }
25191da177e4SLinus Torvalds 
25204acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2521312b63fbSAl Viro 		bool want_excl)
25221da177e4SLinus Torvalds {
2523a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
25241da177e4SLinus Torvalds 	if (error)
25251da177e4SLinus Torvalds 		return error;
25261da177e4SLinus Torvalds 
2527acfa4380SAl Viro 	if (!dir->i_op->create)
25281da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
25291da177e4SLinus Torvalds 	mode &= S_IALLUGO;
25301da177e4SLinus Torvalds 	mode |= S_IFREG;
25311da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
25321da177e4SLinus Torvalds 	if (error)
25331da177e4SLinus Torvalds 		return error;
2534312b63fbSAl Viro 	error = dir->i_op->create(dir, dentry, mode, want_excl);
2535a74574aaSStephen Smalley 	if (!error)
2536f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
25371da177e4SLinus Torvalds 	return error;
25381da177e4SLinus Torvalds }
25391da177e4SLinus Torvalds 
254073d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
25411da177e4SLinus Torvalds {
25423fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
25431da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
25441da177e4SLinus Torvalds 	int error;
25451da177e4SLinus Torvalds 
2546bcda7652SAl Viro 	/* O_PATH? */
2547bcda7652SAl Viro 	if (!acc_mode)
2548bcda7652SAl Viro 		return 0;
2549bcda7652SAl Viro 
25501da177e4SLinus Torvalds 	if (!inode)
25511da177e4SLinus Torvalds 		return -ENOENT;
25521da177e4SLinus Torvalds 
2553c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2554c8fe8f30SChristoph Hellwig 	case S_IFLNK:
25551da177e4SLinus Torvalds 		return -ELOOP;
2556c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2557c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
25581da177e4SLinus Torvalds 			return -EISDIR;
2559c8fe8f30SChristoph Hellwig 		break;
2560c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2561c8fe8f30SChristoph Hellwig 	case S_IFCHR:
25623fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
25631da177e4SLinus Torvalds 			return -EACCES;
2564c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
2565c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2566c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
25671da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2568c8fe8f30SChristoph Hellwig 		break;
25694a3fd211SDave Hansen 	}
2570b41572e9SDave Hansen 
25713fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2572b41572e9SDave Hansen 	if (error)
2573b41572e9SDave Hansen 		return error;
25746146f0d5SMimi Zohar 
25751da177e4SLinus Torvalds 	/*
25761da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
25771da177e4SLinus Torvalds 	 */
25781da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
25798737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
25807715b521SAl Viro 			return -EPERM;
25811da177e4SLinus Torvalds 		if (flag & O_TRUNC)
25827715b521SAl Viro 			return -EPERM;
25831da177e4SLinus Torvalds 	}
25841da177e4SLinus Torvalds 
25851da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
25862e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
25877715b521SAl Viro 		return -EPERM;
25881da177e4SLinus Torvalds 
2589f3c7691eSJ. Bruce Fields 	return 0;
25907715b521SAl Viro }
25917715b521SAl Viro 
2592e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
25937715b521SAl Viro {
2594e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
25957715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
25967715b521SAl Viro 	int error = get_write_access(inode);
25971da177e4SLinus Torvalds 	if (error)
25987715b521SAl Viro 		return error;
25991da177e4SLinus Torvalds 	/*
26001da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
26011da177e4SLinus Torvalds 	 */
26021da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2603be6d3e56SKentaro Takeda 	if (!error)
2604ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
26051da177e4SLinus Torvalds 	if (!error) {
26067715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2607d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2608e1181ee6SJeff Layton 				    filp);
26091da177e4SLinus Torvalds 	}
26101da177e4SLinus Torvalds 	put_write_access(inode);
2611acd0c935SMimi Zohar 	return error;
26121da177e4SLinus Torvalds }
26131da177e4SLinus Torvalds 
2614d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2615d57999e1SDave Hansen {
26168a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
26178a5e929dSAl Viro 		flag--;
2618d57999e1SDave Hansen 	return flag;
2619d57999e1SDave Hansen }
2620d57999e1SDave Hansen 
2621d18e9008SMiklos Szeredi static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2622d18e9008SMiklos Szeredi {
2623d18e9008SMiklos Szeredi 	int error = security_path_mknod(dir, dentry, mode, 0);
2624d18e9008SMiklos Szeredi 	if (error)
2625d18e9008SMiklos Szeredi 		return error;
2626d18e9008SMiklos Szeredi 
2627d18e9008SMiklos Szeredi 	error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2628d18e9008SMiklos Szeredi 	if (error)
2629d18e9008SMiklos Szeredi 		return error;
2630d18e9008SMiklos Szeredi 
2631d18e9008SMiklos Szeredi 	return security_inode_create(dir->dentry->d_inode, dentry, mode);
2632d18e9008SMiklos Szeredi }
2633d18e9008SMiklos Szeredi 
26341acf0af9SDavid Howells /*
26351acf0af9SDavid Howells  * Attempt to atomically look up, create and open a file from a negative
26361acf0af9SDavid Howells  * dentry.
26371acf0af9SDavid Howells  *
26381acf0af9SDavid Howells  * Returns 0 if successful.  The file will have been created and attached to
26391acf0af9SDavid Howells  * @file by the filesystem calling finish_open().
26401acf0af9SDavid Howells  *
26411acf0af9SDavid Howells  * Returns 1 if the file was looked up only or didn't need creating.  The
26421acf0af9SDavid Howells  * caller will need to perform the open themselves.  @path will have been
26431acf0af9SDavid Howells  * updated to point to the new dentry.  This may be negative.
26441acf0af9SDavid Howells  *
26451acf0af9SDavid Howells  * Returns an error code otherwise.
26461acf0af9SDavid Howells  */
26472675a4ebSAl Viro static int atomic_open(struct nameidata *nd, struct dentry *dentry,
264830d90494SAl Viro 			struct path *path, struct file *file,
2649015c3bbcSMiklos Szeredi 			const struct open_flags *op,
265064894cf8SAl Viro 			bool got_write, bool need_lookup,
265147237687SAl Viro 			int *opened)
2652d18e9008SMiklos Szeredi {
2653d18e9008SMiklos Szeredi 	struct inode *dir =  nd->path.dentry->d_inode;
2654d18e9008SMiklos Szeredi 	unsigned open_flag = open_to_namei_flags(op->open_flag);
2655d18e9008SMiklos Szeredi 	umode_t mode;
2656d18e9008SMiklos Szeredi 	int error;
2657d18e9008SMiklos Szeredi 	int acc_mode;
2658d18e9008SMiklos Szeredi 	int create_error = 0;
2659d18e9008SMiklos Szeredi 	struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2660d18e9008SMiklos Szeredi 
2661d18e9008SMiklos Szeredi 	BUG_ON(dentry->d_inode);
2662d18e9008SMiklos Szeredi 
2663d18e9008SMiklos Szeredi 	/* Don't create child dentry for a dead directory. */
2664d18e9008SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
26652675a4ebSAl Viro 		error = -ENOENT;
2666d18e9008SMiklos Szeredi 		goto out;
2667d18e9008SMiklos Szeredi 	}
2668d18e9008SMiklos Szeredi 
266962b259d8SMiklos Szeredi 	mode = op->mode;
2670d18e9008SMiklos Szeredi 	if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2671d18e9008SMiklos Szeredi 		mode &= ~current_umask();
2672d18e9008SMiklos Szeredi 
2673f8310c59SAl Viro 	if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) {
2674d18e9008SMiklos Szeredi 		open_flag &= ~O_TRUNC;
267547237687SAl Viro 		*opened |= FILE_CREATED;
2676d18e9008SMiklos Szeredi 	}
2677d18e9008SMiklos Szeredi 
2678d18e9008SMiklos Szeredi 	/*
2679d18e9008SMiklos Szeredi 	 * Checking write permission is tricky, bacuse we don't know if we are
2680d18e9008SMiklos Szeredi 	 * going to actually need it: O_CREAT opens should work as long as the
2681d18e9008SMiklos Szeredi 	 * file exists.  But checking existence breaks atomicity.  The trick is
2682d18e9008SMiklos Szeredi 	 * to check access and if not granted clear O_CREAT from the flags.
2683d18e9008SMiklos Szeredi 	 *
2684d18e9008SMiklos Szeredi 	 * Another problem is returing the "right" error value (e.g. for an
2685d18e9008SMiklos Szeredi 	 * O_EXCL open we want to return EEXIST not EROFS).
2686d18e9008SMiklos Szeredi 	 */
268764894cf8SAl Viro 	if (((open_flag & (O_CREAT | O_TRUNC)) ||
268864894cf8SAl Viro 	    (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
268964894cf8SAl Viro 		if (!(open_flag & O_CREAT)) {
2690d18e9008SMiklos Szeredi 			/*
2691d18e9008SMiklos Szeredi 			 * No O_CREATE -> atomicity not a requirement -> fall
2692d18e9008SMiklos Szeredi 			 * back to lookup + open
2693d18e9008SMiklos Szeredi 			 */
2694d18e9008SMiklos Szeredi 			goto no_open;
2695d18e9008SMiklos Szeredi 		} else if (open_flag & (O_EXCL | O_TRUNC)) {
2696d18e9008SMiklos Szeredi 			/* Fall back and fail with the right error */
269764894cf8SAl Viro 			create_error = -EROFS;
2698d18e9008SMiklos Szeredi 			goto no_open;
2699d18e9008SMiklos Szeredi 		} else {
2700d18e9008SMiklos Szeredi 			/* No side effects, safe to clear O_CREAT */
270164894cf8SAl Viro 			create_error = -EROFS;
2702d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2703d18e9008SMiklos Szeredi 		}
2704d18e9008SMiklos Szeredi 	}
2705d18e9008SMiklos Szeredi 
2706d18e9008SMiklos Szeredi 	if (open_flag & O_CREAT) {
270738227f78SMiklos Szeredi 		error = may_o_create(&nd->path, dentry, mode);
2708d18e9008SMiklos Szeredi 		if (error) {
2709d18e9008SMiklos Szeredi 			create_error = error;
2710d18e9008SMiklos Szeredi 			if (open_flag & O_EXCL)
2711d18e9008SMiklos Szeredi 				goto no_open;
2712d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2713d18e9008SMiklos Szeredi 		}
2714d18e9008SMiklos Szeredi 	}
2715d18e9008SMiklos Szeredi 
2716d18e9008SMiklos Szeredi 	if (nd->flags & LOOKUP_DIRECTORY)
2717d18e9008SMiklos Szeredi 		open_flag |= O_DIRECTORY;
2718d18e9008SMiklos Szeredi 
271930d90494SAl Viro 	file->f_path.dentry = DENTRY_NOT_SET;
272030d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
272130d90494SAl Viro 	error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
272247237687SAl Viro 				      opened);
2723d9585277SAl Viro 	if (error < 0) {
2724d9585277SAl Viro 		if (create_error && error == -ENOENT)
2725d9585277SAl Viro 			error = create_error;
2726d18e9008SMiklos Szeredi 		goto out;
2727d18e9008SMiklos Szeredi 	}
2728d18e9008SMiklos Szeredi 
2729d18e9008SMiklos Szeredi 	acc_mode = op->acc_mode;
273047237687SAl Viro 	if (*opened & FILE_CREATED) {
2731d18e9008SMiklos Szeredi 		fsnotify_create(dir, dentry);
2732d18e9008SMiklos Szeredi 		acc_mode = MAY_OPEN;
2733d18e9008SMiklos Szeredi 	}
2734d18e9008SMiklos Szeredi 
2735d9585277SAl Viro 	if (error) {	/* returned 1, that is */
273630d90494SAl Viro 		if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
27372675a4ebSAl Viro 			error = -EIO;
2738d18e9008SMiklos Szeredi 			goto out;
2739d18e9008SMiklos Szeredi 		}
274030d90494SAl Viro 		if (file->f_path.dentry) {
2741d18e9008SMiklos Szeredi 			dput(dentry);
274230d90494SAl Viro 			dentry = file->f_path.dentry;
2743d18e9008SMiklos Szeredi 		}
274462b2ce96SSage Weil 		if (create_error && dentry->d_inode == NULL) {
274562b2ce96SSage Weil 			error = create_error;
274662b2ce96SSage Weil 			goto out;
274762b2ce96SSage Weil 		}
2748d18e9008SMiklos Szeredi 		goto looked_up;
2749d18e9008SMiklos Szeredi 	}
2750d18e9008SMiklos Szeredi 
2751d18e9008SMiklos Szeredi 	/*
2752d18e9008SMiklos Szeredi 	 * We didn't have the inode before the open, so check open permission
2753d18e9008SMiklos Szeredi 	 * here.
2754d18e9008SMiklos Szeredi 	 */
27552675a4ebSAl Viro 	error = may_open(&file->f_path, acc_mode, open_flag);
27562675a4ebSAl Viro 	if (error)
27572675a4ebSAl Viro 		fput(file);
2758d18e9008SMiklos Szeredi 
2759d18e9008SMiklos Szeredi out:
2760d18e9008SMiklos Szeredi 	dput(dentry);
27612675a4ebSAl Viro 	return error;
2762d18e9008SMiklos Szeredi 
2763d18e9008SMiklos Szeredi no_open:
2764d18e9008SMiklos Szeredi 	if (need_lookup) {
276572bd866aSAl Viro 		dentry = lookup_real(dir, dentry, nd->flags);
2766d18e9008SMiklos Szeredi 		if (IS_ERR(dentry))
27672675a4ebSAl Viro 			return PTR_ERR(dentry);
2768d18e9008SMiklos Szeredi 
2769d18e9008SMiklos Szeredi 		if (create_error) {
2770d18e9008SMiklos Szeredi 			int open_flag = op->open_flag;
2771d18e9008SMiklos Szeredi 
27722675a4ebSAl Viro 			error = create_error;
2773d18e9008SMiklos Szeredi 			if ((open_flag & O_EXCL)) {
2774d18e9008SMiklos Szeredi 				if (!dentry->d_inode)
2775d18e9008SMiklos Szeredi 					goto out;
2776d18e9008SMiklos Szeredi 			} else if (!dentry->d_inode) {
2777d18e9008SMiklos Szeredi 				goto out;
2778d18e9008SMiklos Szeredi 			} else if ((open_flag & O_TRUNC) &&
2779d18e9008SMiklos Szeredi 				   S_ISREG(dentry->d_inode->i_mode)) {
2780d18e9008SMiklos Szeredi 				goto out;
2781d18e9008SMiklos Szeredi 			}
2782d18e9008SMiklos Szeredi 			/* will fail later, go on to get the right error */
2783d18e9008SMiklos Szeredi 		}
2784d18e9008SMiklos Szeredi 	}
2785d18e9008SMiklos Szeredi looked_up:
2786d18e9008SMiklos Szeredi 	path->dentry = dentry;
2787d18e9008SMiklos Szeredi 	path->mnt = nd->path.mnt;
27882675a4ebSAl Viro 	return 1;
2789d18e9008SMiklos Szeredi }
2790d18e9008SMiklos Szeredi 
279131e6b01fSNick Piggin /*
27921acf0af9SDavid Howells  * Look up and maybe create and open the last component.
2793d58ffd35SMiklos Szeredi  *
2794d58ffd35SMiklos Szeredi  * Must be called with i_mutex held on parent.
2795d58ffd35SMiklos Szeredi  *
27961acf0af9SDavid Howells  * Returns 0 if the file was successfully atomically created (if necessary) and
27971acf0af9SDavid Howells  * opened.  In this case the file will be returned attached to @file.
27981acf0af9SDavid Howells  *
27991acf0af9SDavid Howells  * Returns 1 if the file was not completely opened at this time, though lookups
28001acf0af9SDavid Howells  * and creations will have been performed and the dentry returned in @path will
28011acf0af9SDavid Howells  * be positive upon return if O_CREAT was specified.  If O_CREAT wasn't
28021acf0af9SDavid Howells  * specified then a negative dentry may be returned.
28031acf0af9SDavid Howells  *
28041acf0af9SDavid Howells  * An error code is returned otherwise.
28051acf0af9SDavid Howells  *
28061acf0af9SDavid Howells  * FILE_CREATE will be set in @*opened if the dentry was created and will be
28071acf0af9SDavid Howells  * cleared otherwise prior to returning.
2808d58ffd35SMiklos Szeredi  */
28092675a4ebSAl Viro static int lookup_open(struct nameidata *nd, struct path *path,
281030d90494SAl Viro 			struct file *file,
2811d58ffd35SMiklos Szeredi 			const struct open_flags *op,
281264894cf8SAl Viro 			bool got_write, int *opened)
2813d58ffd35SMiklos Szeredi {
2814d58ffd35SMiklos Szeredi 	struct dentry *dir = nd->path.dentry;
281554ef4872SMiklos Szeredi 	struct inode *dir_inode = dir->d_inode;
2816d58ffd35SMiklos Szeredi 	struct dentry *dentry;
2817d58ffd35SMiklos Szeredi 	int error;
281854ef4872SMiklos Szeredi 	bool need_lookup;
2819d58ffd35SMiklos Szeredi 
282047237687SAl Viro 	*opened &= ~FILE_CREATED;
2821201f956eSAl Viro 	dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
2822d58ffd35SMiklos Szeredi 	if (IS_ERR(dentry))
28232675a4ebSAl Viro 		return PTR_ERR(dentry);
2824d58ffd35SMiklos Szeredi 
2825d18e9008SMiklos Szeredi 	/* Cached positive dentry: will open in f_op->open */
2826d18e9008SMiklos Szeredi 	if (!need_lookup && dentry->d_inode)
2827d18e9008SMiklos Szeredi 		goto out_no_open;
2828d18e9008SMiklos Szeredi 
2829d18e9008SMiklos Szeredi 	if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
283064894cf8SAl Viro 		return atomic_open(nd, dentry, path, file, op, got_write,
283147237687SAl Viro 				   need_lookup, opened);
2832d18e9008SMiklos Szeredi 	}
2833d18e9008SMiklos Szeredi 
283454ef4872SMiklos Szeredi 	if (need_lookup) {
283554ef4872SMiklos Szeredi 		BUG_ON(dentry->d_inode);
283654ef4872SMiklos Szeredi 
283772bd866aSAl Viro 		dentry = lookup_real(dir_inode, dentry, nd->flags);
283854ef4872SMiklos Szeredi 		if (IS_ERR(dentry))
28392675a4ebSAl Viro 			return PTR_ERR(dentry);
284054ef4872SMiklos Szeredi 	}
284154ef4872SMiklos Szeredi 
2842d58ffd35SMiklos Szeredi 	/* Negative dentry, just create the file */
2843d58ffd35SMiklos Szeredi 	if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2844d58ffd35SMiklos Szeredi 		umode_t mode = op->mode;
2845d58ffd35SMiklos Szeredi 		if (!IS_POSIXACL(dir->d_inode))
2846d58ffd35SMiklos Szeredi 			mode &= ~current_umask();
2847d58ffd35SMiklos Szeredi 		/*
2848d58ffd35SMiklos Szeredi 		 * This write is needed to ensure that a
2849d58ffd35SMiklos Szeredi 		 * rw->ro transition does not occur between
2850d58ffd35SMiklos Szeredi 		 * the time when the file is created and when
2851d58ffd35SMiklos Szeredi 		 * a permanent write count is taken through
2852015c3bbcSMiklos Szeredi 		 * the 'struct file' in finish_open().
2853d58ffd35SMiklos Szeredi 		 */
285464894cf8SAl Viro 		if (!got_write) {
285564894cf8SAl Viro 			error = -EROFS;
2856d58ffd35SMiklos Szeredi 			goto out_dput;
285764894cf8SAl Viro 		}
285847237687SAl Viro 		*opened |= FILE_CREATED;
2859d58ffd35SMiklos Szeredi 		error = security_path_mknod(&nd->path, dentry, mode, 0);
2860d58ffd35SMiklos Szeredi 		if (error)
2861d58ffd35SMiklos Szeredi 			goto out_dput;
2862312b63fbSAl Viro 		error = vfs_create(dir->d_inode, dentry, mode,
2863312b63fbSAl Viro 				   nd->flags & LOOKUP_EXCL);
2864d58ffd35SMiklos Szeredi 		if (error)
2865d58ffd35SMiklos Szeredi 			goto out_dput;
2866d58ffd35SMiklos Szeredi 	}
2867d18e9008SMiklos Szeredi out_no_open:
2868d58ffd35SMiklos Szeredi 	path->dentry = dentry;
2869d58ffd35SMiklos Szeredi 	path->mnt = nd->path.mnt;
28702675a4ebSAl Viro 	return 1;
2871d58ffd35SMiklos Szeredi 
2872d58ffd35SMiklos Szeredi out_dput:
2873d58ffd35SMiklos Szeredi 	dput(dentry);
28742675a4ebSAl Viro 	return error;
2875d58ffd35SMiklos Szeredi }
2876d58ffd35SMiklos Szeredi 
2877d58ffd35SMiklos Szeredi /*
2878fe2d35ffSAl Viro  * Handle the last step of open()
287931e6b01fSNick Piggin  */
28802675a4ebSAl Viro static int do_last(struct nameidata *nd, struct path *path,
288130d90494SAl Viro 		   struct file *file, const struct open_flags *op,
2882669abf4eSJeff Layton 		   int *opened, struct filename *name)
2883fb1cc555SAl Viro {
2884a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
2885ca344a89SAl Viro 	int open_flag = op->open_flag;
288677d660a8SMiklos Szeredi 	bool will_truncate = (open_flag & O_TRUNC) != 0;
288764894cf8SAl Viro 	bool got_write = false;
2888bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2889a1eb3315SMiklos Szeredi 	struct inode *inode;
289077d660a8SMiklos Szeredi 	bool symlink_ok = false;
289116b1c1cdSMiklos Szeredi 	struct path save_parent = { .dentry = NULL, .mnt = NULL };
289216b1c1cdSMiklos Szeredi 	bool retried = false;
289316c2cd71SAl Viro 	int error;
2894fb1cc555SAl Viro 
2895c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2896c3e380b0SAl Viro 	nd->flags |= op->intent;
2897c3e380b0SAl Viro 
2898bc77daa7SAl Viro 	if (nd->last_type != LAST_NORM) {
2899fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2900fe2d35ffSAl Viro 		if (error)
29012675a4ebSAl Viro 			return error;
2902e83db167SMiklos Szeredi 		goto finish_open;
29031f36f774SAl Viro 	}
2904a2c36b45SAl Viro 
2905ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2906fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2907fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2908bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
290977d660a8SMiklos Szeredi 			symlink_ok = true;
2910fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2911e97cdc87SAl Viro 		error = lookup_fast(nd, path, &inode);
291271574865SMiklos Szeredi 		if (likely(!error))
291371574865SMiklos Szeredi 			goto finish_lookup;
291471574865SMiklos Szeredi 
2915ce57dfc1SAl Viro 		if (error < 0)
29162675a4ebSAl Viro 			goto out;
2917a1eb3315SMiklos Szeredi 
291837d7fffcSMiklos Szeredi 		BUG_ON(nd->inode != dir->d_inode);
2919b6183df7SMiklos Szeredi 	} else {
2920fe2d35ffSAl Viro 		/* create side of things */
2921a3fbbde7SAl Viro 		/*
2922b6183df7SMiklos Szeredi 		 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2923b6183df7SMiklos Szeredi 		 * has been cleared when we got to the last component we are
2924b6183df7SMiklos Szeredi 		 * about to look up
2925a3fbbde7SAl Viro 		 */
29269f1fafeeSAl Viro 		error = complete_walk(nd);
29279f1fafeeSAl Viro 		if (error)
29282675a4ebSAl Viro 			return error;
2929fe2d35ffSAl Viro 
293033e2208aSJeff Layton 		audit_inode(name, dir, LOOKUP_PARENT);
293116c2cd71SAl Viro 		error = -EISDIR;
29321f36f774SAl Viro 		/* trailing slashes? */
293331e6b01fSNick Piggin 		if (nd->last.name[nd->last.len])
29342675a4ebSAl Viro 			goto out;
2935b6183df7SMiklos Szeredi 	}
29361f36f774SAl Viro 
293716b1c1cdSMiklos Szeredi retry_lookup:
293864894cf8SAl Viro 	if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
293964894cf8SAl Viro 		error = mnt_want_write(nd->path.mnt);
294064894cf8SAl Viro 		if (!error)
294164894cf8SAl Viro 			got_write = true;
294264894cf8SAl Viro 		/*
294364894cf8SAl Viro 		 * do _not_ fail yet - we might not need that or fail with
294464894cf8SAl Viro 		 * a different error; let lookup_open() decide; we'll be
294564894cf8SAl Viro 		 * dropping this one anyway.
294664894cf8SAl Viro 		 */
294764894cf8SAl Viro 	}
2948a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
294964894cf8SAl Viro 	error = lookup_open(nd, path, file, op, got_write, opened);
2950fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2951fb1cc555SAl Viro 
29522675a4ebSAl Viro 	if (error <= 0) {
29532675a4ebSAl Viro 		if (error)
2954d58ffd35SMiklos Szeredi 			goto out;
29556c0d46c4SAl Viro 
295647237687SAl Viro 		if ((*opened & FILE_CREATED) ||
2957496ad9aaSAl Viro 		    !S_ISREG(file_inode(file)->i_mode))
295877d660a8SMiklos Szeredi 			will_truncate = false;
2959d18e9008SMiklos Szeredi 
2960adb5c247SJeff Layton 		audit_inode(name, file->f_path.dentry, 0);
2961d18e9008SMiklos Szeredi 		goto opened;
2962d18e9008SMiklos Szeredi 	}
2963d18e9008SMiklos Szeredi 
296447237687SAl Viro 	if (*opened & FILE_CREATED) {
29659b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2966ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
296777d660a8SMiklos Szeredi 		will_truncate = false;
2968bcda7652SAl Viro 		acc_mode = MAY_OPEN;
2969d58ffd35SMiklos Szeredi 		path_to_nameidata(path, nd);
2970e83db167SMiklos Szeredi 		goto finish_open_created;
2971fb1cc555SAl Viro 	}
2972fb1cc555SAl Viro 
2973fb1cc555SAl Viro 	/*
29743134f37eSJeff Layton 	 * create/update audit record if it already exists.
2975fb1cc555SAl Viro 	 */
29763134f37eSJeff Layton 	if (path->dentry->d_inode)
2977adb5c247SJeff Layton 		audit_inode(name, path->dentry, 0);
2978fb1cc555SAl Viro 
2979d18e9008SMiklos Szeredi 	/*
2980d18e9008SMiklos Szeredi 	 * If atomic_open() acquired write access it is dropped now due to
2981d18e9008SMiklos Szeredi 	 * possible mount and symlink following (this might be optimized away if
2982d18e9008SMiklos Szeredi 	 * necessary...)
2983d18e9008SMiklos Szeredi 	 */
298464894cf8SAl Viro 	if (got_write) {
2985d18e9008SMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
298664894cf8SAl Viro 		got_write = false;
2987d18e9008SMiklos Szeredi 	}
2988d18e9008SMiklos Szeredi 
2989fb1cc555SAl Viro 	error = -EEXIST;
2990f8310c59SAl Viro 	if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
2991fb1cc555SAl Viro 		goto exit_dput;
2992fb1cc555SAl Viro 
29939875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
29949875cf80SDavid Howells 	if (error < 0)
2995fb1cc555SAl Viro 		goto exit_dput;
2996fb1cc555SAl Viro 
2997a3fbbde7SAl Viro 	if (error)
2998a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
2999a3fbbde7SAl Viro 
3000decf3400SMiklos Szeredi 	BUG_ON(nd->flags & LOOKUP_RCU);
3001decf3400SMiklos Szeredi 	inode = path->dentry->d_inode;
30025f5daac1SMiklos Szeredi finish_lookup:
30035f5daac1SMiklos Szeredi 	/* we _can_ be in RCU mode here */
3004fb1cc555SAl Viro 	error = -ENOENT;
300554c33e7fSMiklos Szeredi 	if (!inode) {
300654c33e7fSMiklos Szeredi 		path_to_nameidata(path, nd);
30072675a4ebSAl Viro 		goto out;
300854c33e7fSMiklos Szeredi 	}
30099e67f361SAl Viro 
3010d45ea867SMiklos Szeredi 	if (should_follow_link(inode, !symlink_ok)) {
3011d45ea867SMiklos Szeredi 		if (nd->flags & LOOKUP_RCU) {
3012d45ea867SMiklos Szeredi 			if (unlikely(unlazy_walk(nd, path->dentry))) {
3013d45ea867SMiklos Szeredi 				error = -ECHILD;
30142675a4ebSAl Viro 				goto out;
3015d45ea867SMiklos Szeredi 			}
3016d45ea867SMiklos Szeredi 		}
3017d45ea867SMiklos Szeredi 		BUG_ON(inode != path->dentry->d_inode);
30182675a4ebSAl Viro 		return 1;
3019d45ea867SMiklos Szeredi 	}
3020fb1cc555SAl Viro 
302116b1c1cdSMiklos Szeredi 	if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
3022fb1cc555SAl Viro 		path_to_nameidata(path, nd);
302316b1c1cdSMiklos Szeredi 	} else {
302416b1c1cdSMiklos Szeredi 		save_parent.dentry = nd->path.dentry;
302516b1c1cdSMiklos Szeredi 		save_parent.mnt = mntget(path->mnt);
302616b1c1cdSMiklos Szeredi 		nd->path.dentry = path->dentry;
302716b1c1cdSMiklos Szeredi 
302816b1c1cdSMiklos Szeredi 	}
3029decf3400SMiklos Szeredi 	nd->inode = inode;
3030a3fbbde7SAl Viro 	/* Why this, you ask?  _Now_ we might have grown LOOKUP_JUMPED... */
3031bc77daa7SAl Viro finish_open:
3032a3fbbde7SAl Viro 	error = complete_walk(nd);
303316b1c1cdSMiklos Szeredi 	if (error) {
303416b1c1cdSMiklos Szeredi 		path_put(&save_parent);
30352675a4ebSAl Viro 		return error;
303616b1c1cdSMiklos Szeredi 	}
3037bc77daa7SAl Viro 	audit_inode(name, nd->path.dentry, 0);
3038fb1cc555SAl Viro 	error = -EISDIR;
3039050ac841SMiklos Szeredi 	if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
30402675a4ebSAl Viro 		goto out;
3041af2f5542SMiklos Szeredi 	error = -ENOTDIR;
304205252901SAl Viro 	if ((nd->flags & LOOKUP_DIRECTORY) && !can_lookup(nd->inode))
30432675a4ebSAl Viro 		goto out;
30446c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
304577d660a8SMiklos Szeredi 		will_truncate = false;
30466c0d46c4SAl Viro 
30470f9d1a10SAl Viro 	if (will_truncate) {
30480f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
30490f9d1a10SAl Viro 		if (error)
30502675a4ebSAl Viro 			goto out;
305164894cf8SAl Viro 		got_write = true;
30520f9d1a10SAl Viro 	}
3053e83db167SMiklos Szeredi finish_open_created:
3054bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
3055ca344a89SAl Viro 	if (error)
30562675a4ebSAl Viro 		goto out;
305730d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
305830d90494SAl Viro 	error = finish_open(file, nd->path.dentry, NULL, opened);
305930d90494SAl Viro 	if (error) {
306030d90494SAl Viro 		if (error == -EOPENSTALE)
3061f60dc3dbSMiklos Szeredi 			goto stale_open;
3062015c3bbcSMiklos Szeredi 		goto out;
3063f60dc3dbSMiklos Szeredi 	}
3064a8277b9bSMiklos Szeredi opened:
30652675a4ebSAl Viro 	error = open_check_o_direct(file);
3066015c3bbcSMiklos Szeredi 	if (error)
3067015c3bbcSMiklos Szeredi 		goto exit_fput;
30682675a4ebSAl Viro 	error = ima_file_check(file, op->acc_mode);
3069aa4caadbSMiklos Szeredi 	if (error)
3070aa4caadbSMiklos Szeredi 		goto exit_fput;
3071aa4caadbSMiklos Szeredi 
30720f9d1a10SAl Viro 	if (will_truncate) {
30732675a4ebSAl Viro 		error = handle_truncate(file);
3074aa4caadbSMiklos Szeredi 		if (error)
3075aa4caadbSMiklos Szeredi 			goto exit_fput;
30760f9d1a10SAl Viro 	}
3077ca344a89SAl Viro out:
307864894cf8SAl Viro 	if (got_write)
30790f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
308016b1c1cdSMiklos Szeredi 	path_put(&save_parent);
3081e276ae67SMiklos Szeredi 	terminate_walk(nd);
30822675a4ebSAl Viro 	return error;
3083fb1cc555SAl Viro 
3084fb1cc555SAl Viro exit_dput:
3085fb1cc555SAl Viro 	path_put_conditional(path, nd);
3086ca344a89SAl Viro 	goto out;
3087015c3bbcSMiklos Szeredi exit_fput:
30882675a4ebSAl Viro 	fput(file);
30892675a4ebSAl Viro 	goto out;
3090015c3bbcSMiklos Szeredi 
3091f60dc3dbSMiklos Szeredi stale_open:
3092f60dc3dbSMiklos Szeredi 	/* If no saved parent or already retried then can't retry */
3093f60dc3dbSMiklos Szeredi 	if (!save_parent.dentry || retried)
3094f60dc3dbSMiklos Szeredi 		goto out;
3095f60dc3dbSMiklos Szeredi 
3096f60dc3dbSMiklos Szeredi 	BUG_ON(save_parent.dentry != dir);
3097f60dc3dbSMiklos Szeredi 	path_put(&nd->path);
3098f60dc3dbSMiklos Szeredi 	nd->path = save_parent;
3099f60dc3dbSMiklos Szeredi 	nd->inode = dir->d_inode;
3100f60dc3dbSMiklos Szeredi 	save_parent.mnt = NULL;
3101f60dc3dbSMiklos Szeredi 	save_parent.dentry = NULL;
310264894cf8SAl Viro 	if (got_write) {
3103f60dc3dbSMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
310464894cf8SAl Viro 		got_write = false;
3105f60dc3dbSMiklos Szeredi 	}
3106f60dc3dbSMiklos Szeredi 	retried = true;
3107f60dc3dbSMiklos Szeredi 	goto retry_lookup;
3108fb1cc555SAl Viro }
3109fb1cc555SAl Viro 
311060545d0dSAl Viro static int do_tmpfile(int dfd, struct filename *pathname,
311160545d0dSAl Viro 		struct nameidata *nd, int flags,
311260545d0dSAl Viro 		const struct open_flags *op,
311360545d0dSAl Viro 		struct file *file, int *opened)
311460545d0dSAl Viro {
311560545d0dSAl Viro 	static const struct qstr name = QSTR_INIT("/", 1);
311660545d0dSAl Viro 	struct dentry *dentry, *child;
311760545d0dSAl Viro 	struct inode *dir;
311860545d0dSAl Viro 	int error = path_lookupat(dfd, pathname->name,
311960545d0dSAl Viro 				  flags | LOOKUP_DIRECTORY, nd);
312060545d0dSAl Viro 	if (unlikely(error))
312160545d0dSAl Viro 		return error;
312260545d0dSAl Viro 	error = mnt_want_write(nd->path.mnt);
312360545d0dSAl Viro 	if (unlikely(error))
312460545d0dSAl Viro 		goto out;
312560545d0dSAl Viro 	/* we want directory to be writable */
312660545d0dSAl Viro 	error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
312760545d0dSAl Viro 	if (error)
312860545d0dSAl Viro 		goto out2;
312960545d0dSAl Viro 	dentry = nd->path.dentry;
313060545d0dSAl Viro 	dir = dentry->d_inode;
313160545d0dSAl Viro 	if (!dir->i_op->tmpfile) {
313260545d0dSAl Viro 		error = -EOPNOTSUPP;
313360545d0dSAl Viro 		goto out2;
313460545d0dSAl Viro 	}
313560545d0dSAl Viro 	child = d_alloc(dentry, &name);
313660545d0dSAl Viro 	if (unlikely(!child)) {
313760545d0dSAl Viro 		error = -ENOMEM;
313860545d0dSAl Viro 		goto out2;
313960545d0dSAl Viro 	}
314060545d0dSAl Viro 	nd->flags &= ~LOOKUP_DIRECTORY;
314160545d0dSAl Viro 	nd->flags |= op->intent;
314260545d0dSAl Viro 	dput(nd->path.dentry);
314360545d0dSAl Viro 	nd->path.dentry = child;
314460545d0dSAl Viro 	error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
314560545d0dSAl Viro 	if (error)
314660545d0dSAl Viro 		goto out2;
314760545d0dSAl Viro 	audit_inode(pathname, nd->path.dentry, 0);
314860545d0dSAl Viro 	error = may_open(&nd->path, op->acc_mode, op->open_flag);
314960545d0dSAl Viro 	if (error)
315060545d0dSAl Viro 		goto out2;
315160545d0dSAl Viro 	file->f_path.mnt = nd->path.mnt;
315260545d0dSAl Viro 	error = finish_open(file, nd->path.dentry, NULL, opened);
315360545d0dSAl Viro 	if (error)
315460545d0dSAl Viro 		goto out2;
315560545d0dSAl Viro 	error = open_check_o_direct(file);
3156f4e0c30cSAl Viro 	if (error) {
315760545d0dSAl Viro 		fput(file);
3158f4e0c30cSAl Viro 	} else if (!(op->open_flag & O_EXCL)) {
3159f4e0c30cSAl Viro 		struct inode *inode = file_inode(file);
3160f4e0c30cSAl Viro 		spin_lock(&inode->i_lock);
3161f4e0c30cSAl Viro 		inode->i_state |= I_LINKABLE;
3162f4e0c30cSAl Viro 		spin_unlock(&inode->i_lock);
3163f4e0c30cSAl Viro 	}
316460545d0dSAl Viro out2:
316560545d0dSAl Viro 	mnt_drop_write(nd->path.mnt);
316660545d0dSAl Viro out:
316760545d0dSAl Viro 	path_put(&nd->path);
316860545d0dSAl Viro 	return error;
316960545d0dSAl Viro }
317060545d0dSAl Viro 
3171669abf4eSJeff Layton static struct file *path_openat(int dfd, struct filename *pathname,
317273d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
31731da177e4SLinus Torvalds {
3174fe2d35ffSAl Viro 	struct file *base = NULL;
317530d90494SAl Viro 	struct file *file;
31769850c056SAl Viro 	struct path path;
317747237687SAl Viro 	int opened = 0;
317813aab428SAl Viro 	int error;
317931e6b01fSNick Piggin 
318030d90494SAl Viro 	file = get_empty_filp();
31811afc99beSAl Viro 	if (IS_ERR(file))
31821afc99beSAl Viro 		return file;
318331e6b01fSNick Piggin 
318430d90494SAl Viro 	file->f_flags = op->open_flag;
318531e6b01fSNick Piggin 
3186bb458c64SAl Viro 	if (unlikely(file->f_flags & __O_TMPFILE)) {
318760545d0dSAl Viro 		error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
318860545d0dSAl Viro 		goto out;
318960545d0dSAl Viro 	}
319060545d0dSAl Viro 
3191669abf4eSJeff Layton 	error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
319231e6b01fSNick Piggin 	if (unlikely(error))
31932675a4ebSAl Viro 		goto out;
319431e6b01fSNick Piggin 
3195fe2d35ffSAl Viro 	current->total_link_count = 0;
3196669abf4eSJeff Layton 	error = link_path_walk(pathname->name, nd);
319731e6b01fSNick Piggin 	if (unlikely(error))
31982675a4ebSAl Viro 		goto out;
31991da177e4SLinus Torvalds 
32002675a4ebSAl Viro 	error = do_last(nd, &path, file, op, &opened, pathname);
32012675a4ebSAl Viro 	while (unlikely(error > 0)) { /* trailing symlink */
32027b9337aaSNick Piggin 		struct path link = path;
3203def4af30SAl Viro 		void *cookie;
3204574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
320573d049a4SAl Viro 			path_put_conditional(&path, nd);
320673d049a4SAl Viro 			path_put(&nd->path);
32072675a4ebSAl Viro 			error = -ELOOP;
320840b39136SAl Viro 			break;
320940b39136SAl Viro 		}
3210800179c9SKees Cook 		error = may_follow_link(&link, nd);
3211800179c9SKees Cook 		if (unlikely(error))
3212800179c9SKees Cook 			break;
321373d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
321473d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
3215574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
3216c3e380b0SAl Viro 		if (unlikely(error))
32172675a4ebSAl Viro 			break;
32182675a4ebSAl Viro 		error = do_last(nd, &path, file, op, &opened, pathname);
3219574197e0SAl Viro 		put_link(nd, &link, cookie);
3220806b681cSAl Viro 	}
322110fa8e62SAl Viro out:
322273d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
322373d049a4SAl Viro 		path_put(&nd->root);
3224fe2d35ffSAl Viro 	if (base)
3225fe2d35ffSAl Viro 		fput(base);
32262675a4ebSAl Viro 	if (!(opened & FILE_OPENED)) {
32272675a4ebSAl Viro 		BUG_ON(!error);
322830d90494SAl Viro 		put_filp(file);
3229015c3bbcSMiklos Szeredi 	}
32302675a4ebSAl Viro 	if (unlikely(error)) {
32312675a4ebSAl Viro 		if (error == -EOPENSTALE) {
32322675a4ebSAl Viro 			if (flags & LOOKUP_RCU)
32332675a4ebSAl Viro 				error = -ECHILD;
32342675a4ebSAl Viro 			else
32352675a4ebSAl Viro 				error = -ESTALE;
32362675a4ebSAl Viro 		}
32372675a4ebSAl Viro 		file = ERR_PTR(error);
32382675a4ebSAl Viro 	}
32392675a4ebSAl Viro 	return file;
3240de459215SKirill Korotaev }
32411da177e4SLinus Torvalds 
3242669abf4eSJeff Layton struct file *do_filp_open(int dfd, struct filename *pathname,
3243f9652e10SAl Viro 		const struct open_flags *op)
324413aab428SAl Viro {
324573d049a4SAl Viro 	struct nameidata nd;
3246f9652e10SAl Viro 	int flags = op->lookup_flags;
324713aab428SAl Viro 	struct file *filp;
324813aab428SAl Viro 
324973d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
325013aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
325173d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
325213aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
325373d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
325413aab428SAl Viro 	return filp;
325513aab428SAl Viro }
325613aab428SAl Viro 
325773d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3258f9652e10SAl Viro 		const char *name, const struct open_flags *op)
325973d049a4SAl Viro {
326073d049a4SAl Viro 	struct nameidata nd;
326173d049a4SAl Viro 	struct file *file;
3262669abf4eSJeff Layton 	struct filename filename = { .name = name };
3263f9652e10SAl Viro 	int flags = op->lookup_flags | LOOKUP_ROOT;
326473d049a4SAl Viro 
326573d049a4SAl Viro 	nd.root.mnt = mnt;
326673d049a4SAl Viro 	nd.root.dentry = dentry;
326773d049a4SAl Viro 
3268bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
326973d049a4SAl Viro 		return ERR_PTR(-ELOOP);
327073d049a4SAl Viro 
3271669abf4eSJeff Layton 	file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
327273d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
3273669abf4eSJeff Layton 		file = path_openat(-1, &filename, &nd, op, flags);
327473d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
3275669abf4eSJeff Layton 		file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
327673d049a4SAl Viro 	return file;
327773d049a4SAl Viro }
327873d049a4SAl Viro 
32791ac12b4bSJeff Layton struct dentry *kern_path_create(int dfd, const char *pathname,
32801ac12b4bSJeff Layton 				struct path *path, unsigned int lookup_flags)
32811da177e4SLinus Torvalds {
3282c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
3283ed75e95dSAl Viro 	struct nameidata nd;
3284c30dabfeSJan Kara 	int err2;
32851ac12b4bSJeff Layton 	int error;
32861ac12b4bSJeff Layton 	bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
32871ac12b4bSJeff Layton 
32881ac12b4bSJeff Layton 	/*
32891ac12b4bSJeff Layton 	 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
32901ac12b4bSJeff Layton 	 * other flags passed in are ignored!
32911ac12b4bSJeff Layton 	 */
32921ac12b4bSJeff Layton 	lookup_flags &= LOOKUP_REVAL;
32931ac12b4bSJeff Layton 
32941ac12b4bSJeff Layton 	error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
3295ed75e95dSAl Viro 	if (error)
3296ed75e95dSAl Viro 		return ERR_PTR(error);
32971da177e4SLinus Torvalds 
3298c663e5d8SChristoph Hellwig 	/*
3299c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
3300c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
3301c663e5d8SChristoph Hellwig 	 */
3302ed75e95dSAl Viro 	if (nd.last_type != LAST_NORM)
3303ed75e95dSAl Viro 		goto out;
3304ed75e95dSAl Viro 	nd.flags &= ~LOOKUP_PARENT;
3305ed75e95dSAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3306c663e5d8SChristoph Hellwig 
3307c30dabfeSJan Kara 	/* don't fail immediately if it's r/o, at least try to report other errors */
3308c30dabfeSJan Kara 	err2 = mnt_want_write(nd.path.mnt);
3309c663e5d8SChristoph Hellwig 	/*
3310c663e5d8SChristoph Hellwig 	 * Do the final lookup.
3311c663e5d8SChristoph Hellwig 	 */
3312ed75e95dSAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3313ed75e95dSAl Viro 	dentry = lookup_hash(&nd);
33141da177e4SLinus Torvalds 	if (IS_ERR(dentry))
3315a8104a9fSAl Viro 		goto unlock;
3316c663e5d8SChristoph Hellwig 
3317a8104a9fSAl Viro 	error = -EEXIST;
3318e9baf6e5SAl Viro 	if (dentry->d_inode)
3319a8104a9fSAl Viro 		goto fail;
3320c663e5d8SChristoph Hellwig 	/*
3321c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
3322c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
3323c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
3324c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
3325c663e5d8SChristoph Hellwig 	 */
3326ed75e95dSAl Viro 	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
3327a8104a9fSAl Viro 		error = -ENOENT;
3328ed75e95dSAl Viro 		goto fail;
3329e9baf6e5SAl Viro 	}
3330c30dabfeSJan Kara 	if (unlikely(err2)) {
3331c30dabfeSJan Kara 		error = err2;
3332a8104a9fSAl Viro 		goto fail;
3333c30dabfeSJan Kara 	}
3334ed75e95dSAl Viro 	*path = nd.path;
3335e9baf6e5SAl Viro 	return dentry;
33361da177e4SLinus Torvalds fail:
3337a8104a9fSAl Viro 	dput(dentry);
3338a8104a9fSAl Viro 	dentry = ERR_PTR(error);
3339a8104a9fSAl Viro unlock:
3340dae6ad8fSAl Viro 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3341c30dabfeSJan Kara 	if (!err2)
3342c30dabfeSJan Kara 		mnt_drop_write(nd.path.mnt);
3343ed75e95dSAl Viro out:
3344dae6ad8fSAl Viro 	path_put(&nd.path);
3345ed75e95dSAl Viro 	return dentry;
3346dae6ad8fSAl Viro }
3347dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
3348dae6ad8fSAl Viro 
3349921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry)
3350921a1650SAl Viro {
3351921a1650SAl Viro 	dput(dentry);
3352921a1650SAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
3353a8104a9fSAl Viro 	mnt_drop_write(path->mnt);
3354921a1650SAl Viro 	path_put(path);
3355921a1650SAl Viro }
3356921a1650SAl Viro EXPORT_SYMBOL(done_path_create);
3357921a1650SAl Viro 
33581ac12b4bSJeff Layton struct dentry *user_path_create(int dfd, const char __user *pathname,
33591ac12b4bSJeff Layton 				struct path *path, unsigned int lookup_flags)
3360dae6ad8fSAl Viro {
336191a27b2aSJeff Layton 	struct filename *tmp = getname(pathname);
3362dae6ad8fSAl Viro 	struct dentry *res;
3363dae6ad8fSAl Viro 	if (IS_ERR(tmp))
3364dae6ad8fSAl Viro 		return ERR_CAST(tmp);
33651ac12b4bSJeff Layton 	res = kern_path_create(dfd, tmp->name, path, lookup_flags);
3366dae6ad8fSAl Viro 	putname(tmp);
3367dae6ad8fSAl Viro 	return res;
3368dae6ad8fSAl Viro }
3369dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
3370dae6ad8fSAl Viro 
33711a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
33721da177e4SLinus Torvalds {
3373a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
33741da177e4SLinus Torvalds 
33751da177e4SLinus Torvalds 	if (error)
33761da177e4SLinus Torvalds 		return error;
33771da177e4SLinus Torvalds 
3378975d6b39SEric W. Biederman 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
33791da177e4SLinus Torvalds 		return -EPERM;
33801da177e4SLinus Torvalds 
3381acfa4380SAl Viro 	if (!dir->i_op->mknod)
33821da177e4SLinus Torvalds 		return -EPERM;
33831da177e4SLinus Torvalds 
338408ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
338508ce5f16SSerge E. Hallyn 	if (error)
338608ce5f16SSerge E. Hallyn 		return error;
338708ce5f16SSerge E. Hallyn 
33881da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
33891da177e4SLinus Torvalds 	if (error)
33901da177e4SLinus Torvalds 		return error;
33911da177e4SLinus Torvalds 
33921da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
3393a74574aaSStephen Smalley 	if (!error)
3394f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
33951da177e4SLinus Torvalds 	return error;
33961da177e4SLinus Torvalds }
33971da177e4SLinus Torvalds 
3398f69aac00SAl Viro static int may_mknod(umode_t mode)
3399463c3197SDave Hansen {
3400463c3197SDave Hansen 	switch (mode & S_IFMT) {
3401463c3197SDave Hansen 	case S_IFREG:
3402463c3197SDave Hansen 	case S_IFCHR:
3403463c3197SDave Hansen 	case S_IFBLK:
3404463c3197SDave Hansen 	case S_IFIFO:
3405463c3197SDave Hansen 	case S_IFSOCK:
3406463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
3407463c3197SDave Hansen 		return 0;
3408463c3197SDave Hansen 	case S_IFDIR:
3409463c3197SDave Hansen 		return -EPERM;
3410463c3197SDave Hansen 	default:
3411463c3197SDave Hansen 		return -EINVAL;
3412463c3197SDave Hansen 	}
3413463c3197SDave Hansen }
3414463c3197SDave Hansen 
34158208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
34162e4d0924SHeiko Carstens 		unsigned, dev)
34171da177e4SLinus Torvalds {
34181da177e4SLinus Torvalds 	struct dentry *dentry;
3419dae6ad8fSAl Viro 	struct path path;
3420dae6ad8fSAl Viro 	int error;
3421972567f1SJeff Layton 	unsigned int lookup_flags = 0;
34221da177e4SLinus Torvalds 
34238e4bfca1SAl Viro 	error = may_mknod(mode);
34248e4bfca1SAl Viro 	if (error)
34258e4bfca1SAl Viro 		return error;
3426972567f1SJeff Layton retry:
3427972567f1SJeff Layton 	dentry = user_path_create(dfd, filename, &path, lookup_flags);
3428dae6ad8fSAl Viro 	if (IS_ERR(dentry))
3429dae6ad8fSAl Viro 		return PTR_ERR(dentry);
34302ad94ae6SAl Viro 
3431dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3432ce3b0f8dSAl Viro 		mode &= ~current_umask();
3433dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
3434be6d3e56SKentaro Takeda 	if (error)
3435a8104a9fSAl Viro 		goto out;
34361da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
34371da177e4SLinus Torvalds 		case 0: case S_IFREG:
3438312b63fbSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,true);
34391da177e4SLinus Torvalds 			break;
34401da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
3441dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
34421da177e4SLinus Torvalds 					new_decode_dev(dev));
34431da177e4SLinus Torvalds 			break;
34441da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
3445dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
34461da177e4SLinus Torvalds 			break;
34471da177e4SLinus Torvalds 	}
3448a8104a9fSAl Viro out:
3449921a1650SAl Viro 	done_path_create(&path, dentry);
3450972567f1SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3451972567f1SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3452972567f1SJeff Layton 		goto retry;
3453972567f1SJeff Layton 	}
34541da177e4SLinus Torvalds 	return error;
34551da177e4SLinus Torvalds }
34561da177e4SLinus Torvalds 
34578208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
34585590ff0dSUlrich Drepper {
34595590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
34605590ff0dSUlrich Drepper }
34615590ff0dSUlrich Drepper 
346218bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
34631da177e4SLinus Torvalds {
3464a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
34658de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
34661da177e4SLinus Torvalds 
34671da177e4SLinus Torvalds 	if (error)
34681da177e4SLinus Torvalds 		return error;
34691da177e4SLinus Torvalds 
3470acfa4380SAl Viro 	if (!dir->i_op->mkdir)
34711da177e4SLinus Torvalds 		return -EPERM;
34721da177e4SLinus Torvalds 
34731da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
34741da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
34751da177e4SLinus Torvalds 	if (error)
34761da177e4SLinus Torvalds 		return error;
34771da177e4SLinus Torvalds 
34788de52778SAl Viro 	if (max_links && dir->i_nlink >= max_links)
34798de52778SAl Viro 		return -EMLINK;
34808de52778SAl Viro 
34811da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
3482a74574aaSStephen Smalley 	if (!error)
3483f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
34841da177e4SLinus Torvalds 	return error;
34851da177e4SLinus Torvalds }
34861da177e4SLinus Torvalds 
3487a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
34881da177e4SLinus Torvalds {
34896902d925SDave Hansen 	struct dentry *dentry;
3490dae6ad8fSAl Viro 	struct path path;
3491dae6ad8fSAl Viro 	int error;
3492b76d8b82SJeff Layton 	unsigned int lookup_flags = LOOKUP_DIRECTORY;
34931da177e4SLinus Torvalds 
3494b76d8b82SJeff Layton retry:
3495b76d8b82SJeff Layton 	dentry = user_path_create(dfd, pathname, &path, lookup_flags);
34966902d925SDave Hansen 	if (IS_ERR(dentry))
3497dae6ad8fSAl Viro 		return PTR_ERR(dentry);
34986902d925SDave Hansen 
3499dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3500ce3b0f8dSAl Viro 		mode &= ~current_umask();
3501dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
3502a8104a9fSAl Viro 	if (!error)
3503dae6ad8fSAl Viro 		error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3504921a1650SAl Viro 	done_path_create(&path, dentry);
3505b76d8b82SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3506b76d8b82SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3507b76d8b82SJeff Layton 		goto retry;
3508b76d8b82SJeff Layton 	}
35091da177e4SLinus Torvalds 	return error;
35101da177e4SLinus Torvalds }
35111da177e4SLinus Torvalds 
3512a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
35135590ff0dSUlrich Drepper {
35145590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
35155590ff0dSUlrich Drepper }
35165590ff0dSUlrich Drepper 
35171da177e4SLinus Torvalds /*
3518a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
3519c0d02594SJ. Bruce Fields  * should have a usage count of 1 if we're the only user of this
3520a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
3521a71905f0SSage Weil  * then we drop the dentry now.
35221da177e4SLinus Torvalds  *
35231da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
35241da177e4SLinus Torvalds  * do a
35251da177e4SLinus Torvalds  *
35261da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
35271da177e4SLinus Torvalds  *		return -EBUSY;
35281da177e4SLinus Torvalds  *
35291da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
35301da177e4SLinus Torvalds  * that is still in use by something else..
35311da177e4SLinus Torvalds  */
35321da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
35331da177e4SLinus Torvalds {
35341da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
35351da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
353698474236SWaiman Long 	if (dentry->d_lockref.count == 1)
35371da177e4SLinus Torvalds 		__d_drop(dentry);
35381da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
35391da177e4SLinus Torvalds }
35401da177e4SLinus Torvalds 
35411da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
35421da177e4SLinus Torvalds {
35431da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
35441da177e4SLinus Torvalds 
35451da177e4SLinus Torvalds 	if (error)
35461da177e4SLinus Torvalds 		return error;
35471da177e4SLinus Torvalds 
3548acfa4380SAl Viro 	if (!dir->i_op->rmdir)
35491da177e4SLinus Torvalds 		return -EPERM;
35501da177e4SLinus Torvalds 
35511d2ef590SAl Viro 	dget(dentry);
35521b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
3553912dbc15SSage Weil 
35541da177e4SLinus Torvalds 	error = -EBUSY;
3555912dbc15SSage Weil 	if (d_mountpoint(dentry))
3556912dbc15SSage Weil 		goto out;
3557912dbc15SSage Weil 
35581da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
3559912dbc15SSage Weil 	if (error)
3560912dbc15SSage Weil 		goto out;
3561912dbc15SSage Weil 
35623cebde24SSage Weil 	shrink_dcache_parent(dentry);
35631da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
3564912dbc15SSage Weil 	if (error)
3565912dbc15SSage Weil 		goto out;
3566912dbc15SSage Weil 
35671da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
3568d83c49f3SAl Viro 	dont_mount(dentry);
35691da177e4SLinus Torvalds 
3570912dbc15SSage Weil out:
3571912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
35721d2ef590SAl Viro 	dput(dentry);
3573912dbc15SSage Weil 	if (!error)
3574912dbc15SSage Weil 		d_delete(dentry);
35751da177e4SLinus Torvalds 	return error;
35761da177e4SLinus Torvalds }
35771da177e4SLinus Torvalds 
35785590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
35791da177e4SLinus Torvalds {
35801da177e4SLinus Torvalds 	int error = 0;
358191a27b2aSJeff Layton 	struct filename *name;
35821da177e4SLinus Torvalds 	struct dentry *dentry;
35831da177e4SLinus Torvalds 	struct nameidata nd;
3584c6ee9206SJeff Layton 	unsigned int lookup_flags = 0;
3585c6ee9206SJeff Layton retry:
3586c6ee9206SJeff Layton 	name = user_path_parent(dfd, pathname, &nd, lookup_flags);
358791a27b2aSJeff Layton 	if (IS_ERR(name))
358891a27b2aSJeff Layton 		return PTR_ERR(name);
35891da177e4SLinus Torvalds 
35901da177e4SLinus Torvalds 	switch(nd.last_type) {
35911da177e4SLinus Torvalds 	case LAST_DOTDOT:
35921da177e4SLinus Torvalds 		error = -ENOTEMPTY;
35931da177e4SLinus Torvalds 		goto exit1;
35941da177e4SLinus Torvalds 	case LAST_DOT:
35951da177e4SLinus Torvalds 		error = -EINVAL;
35961da177e4SLinus Torvalds 		goto exit1;
35971da177e4SLinus Torvalds 	case LAST_ROOT:
35981da177e4SLinus Torvalds 		error = -EBUSY;
35991da177e4SLinus Torvalds 		goto exit1;
36001da177e4SLinus Torvalds 	}
36010612d9fbSOGAWA Hirofumi 
36020612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
3603c30dabfeSJan Kara 	error = mnt_want_write(nd.path.mnt);
3604c30dabfeSJan Kara 	if (error)
3605c30dabfeSJan Kara 		goto exit1;
36060612d9fbSOGAWA Hirofumi 
36074ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
360849705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
36091da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
36106902d925SDave Hansen 	if (IS_ERR(dentry))
36116902d925SDave Hansen 		goto exit2;
3612e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
3613e6bc45d6STheodore Ts'o 		error = -ENOENT;
3614e6bc45d6STheodore Ts'o 		goto exit3;
3615e6bc45d6STheodore Ts'o 	}
3616be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
3617be6d3e56SKentaro Takeda 	if (error)
3618c30dabfeSJan Kara 		goto exit3;
36194ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
36200622753bSDave Hansen exit3:
36211da177e4SLinus Torvalds 	dput(dentry);
36226902d925SDave Hansen exit2:
36234ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3624c30dabfeSJan Kara 	mnt_drop_write(nd.path.mnt);
36251da177e4SLinus Torvalds exit1:
36261d957f9bSJan Blunck 	path_put(&nd.path);
36271da177e4SLinus Torvalds 	putname(name);
3628c6ee9206SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3629c6ee9206SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3630c6ee9206SJeff Layton 		goto retry;
3631c6ee9206SJeff Layton 	}
36321da177e4SLinus Torvalds 	return error;
36331da177e4SLinus Torvalds }
36341da177e4SLinus Torvalds 
36353cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
36365590ff0dSUlrich Drepper {
36375590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
36385590ff0dSUlrich Drepper }
36395590ff0dSUlrich Drepper 
36401da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
36411da177e4SLinus Torvalds {
36421da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
36431da177e4SLinus Torvalds 
36441da177e4SLinus Torvalds 	if (error)
36451da177e4SLinus Torvalds 		return error;
36461da177e4SLinus Torvalds 
3647acfa4380SAl Viro 	if (!dir->i_op->unlink)
36481da177e4SLinus Torvalds 		return -EPERM;
36491da177e4SLinus Torvalds 
36501b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
36511da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
36521da177e4SLinus Torvalds 		error = -EBUSY;
36531da177e4SLinus Torvalds 	else {
36541da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
3655bec1052eSAl Viro 		if (!error) {
36561da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
3657bec1052eSAl Viro 			if (!error)
3658d83c49f3SAl Viro 				dont_mount(dentry);
3659bec1052eSAl Viro 		}
36601da177e4SLinus Torvalds 	}
36611b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
36621da177e4SLinus Torvalds 
36631da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
36641da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
3665ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
36661da177e4SLinus Torvalds 		d_delete(dentry);
36671da177e4SLinus Torvalds 	}
36680eeca283SRobert Love 
36691da177e4SLinus Torvalds 	return error;
36701da177e4SLinus Torvalds }
36711da177e4SLinus Torvalds 
36721da177e4SLinus Torvalds /*
36731da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
36741b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
36751da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
36761da177e4SLinus Torvalds  * while waiting on the I/O.
36771da177e4SLinus Torvalds  */
36785590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
36791da177e4SLinus Torvalds {
36802ad94ae6SAl Viro 	int error;
368191a27b2aSJeff Layton 	struct filename *name;
36821da177e4SLinus Torvalds 	struct dentry *dentry;
36831da177e4SLinus Torvalds 	struct nameidata nd;
36841da177e4SLinus Torvalds 	struct inode *inode = NULL;
36855d18f813SJeff Layton 	unsigned int lookup_flags = 0;
36865d18f813SJeff Layton retry:
36875d18f813SJeff Layton 	name = user_path_parent(dfd, pathname, &nd, lookup_flags);
368891a27b2aSJeff Layton 	if (IS_ERR(name))
368991a27b2aSJeff Layton 		return PTR_ERR(name);
36902ad94ae6SAl Viro 
36911da177e4SLinus Torvalds 	error = -EISDIR;
36921da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
36931da177e4SLinus Torvalds 		goto exit1;
36940612d9fbSOGAWA Hirofumi 
36950612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
3696c30dabfeSJan Kara 	error = mnt_want_write(nd.path.mnt);
3697c30dabfeSJan Kara 	if (error)
3698c30dabfeSJan Kara 		goto exit1;
36990612d9fbSOGAWA Hirofumi 
37004ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
370149705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
37021da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
37031da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
37041da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
370550338b88STörök Edwin 		if (nd.last.name[nd.last.len])
370650338b88STörök Edwin 			goto slashes;
37071da177e4SLinus Torvalds 		inode = dentry->d_inode;
370850338b88STörök Edwin 		if (!inode)
3709e6bc45d6STheodore Ts'o 			goto slashes;
37107de9c6eeSAl Viro 		ihold(inode);
3711be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
3712be6d3e56SKentaro Takeda 		if (error)
3713c30dabfeSJan Kara 			goto exit2;
37144ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
37151da177e4SLinus Torvalds exit2:
37161da177e4SLinus Torvalds 		dput(dentry);
37171da177e4SLinus Torvalds 	}
37184ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
37191da177e4SLinus Torvalds 	if (inode)
37201da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
3721c30dabfeSJan Kara 	mnt_drop_write(nd.path.mnt);
37221da177e4SLinus Torvalds exit1:
37231d957f9bSJan Blunck 	path_put(&nd.path);
37241da177e4SLinus Torvalds 	putname(name);
37255d18f813SJeff Layton 	if (retry_estale(error, lookup_flags)) {
37265d18f813SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
37275d18f813SJeff Layton 		inode = NULL;
37285d18f813SJeff Layton 		goto retry;
37295d18f813SJeff Layton 	}
37301da177e4SLinus Torvalds 	return error;
37311da177e4SLinus Torvalds 
37321da177e4SLinus Torvalds slashes:
37331da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
37341da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
37351da177e4SLinus Torvalds 	goto exit2;
37361da177e4SLinus Torvalds }
37371da177e4SLinus Torvalds 
37382e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
37395590ff0dSUlrich Drepper {
37405590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
37415590ff0dSUlrich Drepper 		return -EINVAL;
37425590ff0dSUlrich Drepper 
37435590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
37445590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
37455590ff0dSUlrich Drepper 
37465590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
37475590ff0dSUlrich Drepper }
37485590ff0dSUlrich Drepper 
37493480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
37505590ff0dSUlrich Drepper {
37515590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
37525590ff0dSUlrich Drepper }
37535590ff0dSUlrich Drepper 
3754db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
37551da177e4SLinus Torvalds {
3756a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
37571da177e4SLinus Torvalds 
37581da177e4SLinus Torvalds 	if (error)
37591da177e4SLinus Torvalds 		return error;
37601da177e4SLinus Torvalds 
3761acfa4380SAl Viro 	if (!dir->i_op->symlink)
37621da177e4SLinus Torvalds 		return -EPERM;
37631da177e4SLinus Torvalds 
37641da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
37651da177e4SLinus Torvalds 	if (error)
37661da177e4SLinus Torvalds 		return error;
37671da177e4SLinus Torvalds 
37681da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
3769a74574aaSStephen Smalley 	if (!error)
3770f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
37711da177e4SLinus Torvalds 	return error;
37721da177e4SLinus Torvalds }
37731da177e4SLinus Torvalds 
37742e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
37752e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
37761da177e4SLinus Torvalds {
37772ad94ae6SAl Viro 	int error;
377891a27b2aSJeff Layton 	struct filename *from;
37796902d925SDave Hansen 	struct dentry *dentry;
3780dae6ad8fSAl Viro 	struct path path;
3781f46d3567SJeff Layton 	unsigned int lookup_flags = 0;
37821da177e4SLinus Torvalds 
37831da177e4SLinus Torvalds 	from = getname(oldname);
37841da177e4SLinus Torvalds 	if (IS_ERR(from))
37851da177e4SLinus Torvalds 		return PTR_ERR(from);
3786f46d3567SJeff Layton retry:
3787f46d3567SJeff Layton 	dentry = user_path_create(newdfd, newname, &path, lookup_flags);
37881da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
37896902d925SDave Hansen 	if (IS_ERR(dentry))
3790dae6ad8fSAl Viro 		goto out_putname;
37916902d925SDave Hansen 
379291a27b2aSJeff Layton 	error = security_path_symlink(&path, dentry, from->name);
3793a8104a9fSAl Viro 	if (!error)
379491a27b2aSJeff Layton 		error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
3795921a1650SAl Viro 	done_path_create(&path, dentry);
3796f46d3567SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3797f46d3567SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3798f46d3567SJeff Layton 		goto retry;
3799f46d3567SJeff Layton 	}
38006902d925SDave Hansen out_putname:
38011da177e4SLinus Torvalds 	putname(from);
38021da177e4SLinus Torvalds 	return error;
38031da177e4SLinus Torvalds }
38041da177e4SLinus Torvalds 
38053480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
38065590ff0dSUlrich Drepper {
38075590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
38085590ff0dSUlrich Drepper }
38095590ff0dSUlrich Drepper 
38101da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
38111da177e4SLinus Torvalds {
38121da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
38138de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
38141da177e4SLinus Torvalds 	int error;
38151da177e4SLinus Torvalds 
38161da177e4SLinus Torvalds 	if (!inode)
38171da177e4SLinus Torvalds 		return -ENOENT;
38181da177e4SLinus Torvalds 
3819a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
38201da177e4SLinus Torvalds 	if (error)
38211da177e4SLinus Torvalds 		return error;
38221da177e4SLinus Torvalds 
38231da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
38241da177e4SLinus Torvalds 		return -EXDEV;
38251da177e4SLinus Torvalds 
38261da177e4SLinus Torvalds 	/*
38271da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
38281da177e4SLinus Torvalds 	 */
38291da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
38301da177e4SLinus Torvalds 		return -EPERM;
3831acfa4380SAl Viro 	if (!dir->i_op->link)
38321da177e4SLinus Torvalds 		return -EPERM;
38337e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
38341da177e4SLinus Torvalds 		return -EPERM;
38351da177e4SLinus Torvalds 
38361da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
38371da177e4SLinus Torvalds 	if (error)
38381da177e4SLinus Torvalds 		return error;
38391da177e4SLinus Torvalds 
38407e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
3841aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
3842f4e0c30cSAl Viro 	if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
3843aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
38448de52778SAl Viro 	else if (max_links && inode->i_nlink >= max_links)
38458de52778SAl Viro 		error = -EMLINK;
3846aae8a97dSAneesh Kumar K.V 	else
38471da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
3848f4e0c30cSAl Viro 
3849f4e0c30cSAl Viro 	if (!error && (inode->i_state & I_LINKABLE)) {
3850f4e0c30cSAl Viro 		spin_lock(&inode->i_lock);
3851f4e0c30cSAl Viro 		inode->i_state &= ~I_LINKABLE;
3852f4e0c30cSAl Viro 		spin_unlock(&inode->i_lock);
3853f4e0c30cSAl Viro 	}
38547e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
3855e31e14ecSStephen Smalley 	if (!error)
38567e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
38571da177e4SLinus Torvalds 	return error;
38581da177e4SLinus Torvalds }
38591da177e4SLinus Torvalds 
38601da177e4SLinus Torvalds /*
38611da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
38621da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
38631da177e4SLinus Torvalds  * newname.  --KAB
38641da177e4SLinus Torvalds  *
38651da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
38661da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
38671da177e4SLinus Torvalds  * and other special files.  --ADM
38681da177e4SLinus Torvalds  */
38692e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
38702e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
38711da177e4SLinus Torvalds {
38721da177e4SLinus Torvalds 	struct dentry *new_dentry;
3873dae6ad8fSAl Viro 	struct path old_path, new_path;
387411a7b371SAneesh Kumar K.V 	int how = 0;
38751da177e4SLinus Torvalds 	int error;
38761da177e4SLinus Torvalds 
387711a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3878c04030e1SUlrich Drepper 		return -EINVAL;
387911a7b371SAneesh Kumar K.V 	/*
3880f0cc6ffbSLinus Torvalds 	 * To use null names we require CAP_DAC_READ_SEARCH
3881f0cc6ffbSLinus Torvalds 	 * This ensures that not everyone will be able to create
3882f0cc6ffbSLinus Torvalds 	 * handlink using the passed filedescriptor.
388311a7b371SAneesh Kumar K.V 	 */
3884f0cc6ffbSLinus Torvalds 	if (flags & AT_EMPTY_PATH) {
3885f0cc6ffbSLinus Torvalds 		if (!capable(CAP_DAC_READ_SEARCH))
3886f0cc6ffbSLinus Torvalds 			return -ENOENT;
388711a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
3888f0cc6ffbSLinus Torvalds 	}
3889c04030e1SUlrich Drepper 
389011a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
389111a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
3892442e31caSJeff Layton retry:
389311a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
38941da177e4SLinus Torvalds 	if (error)
38952ad94ae6SAl Viro 		return error;
38962ad94ae6SAl Viro 
3897442e31caSJeff Layton 	new_dentry = user_path_create(newdfd, newname, &new_path,
3898442e31caSJeff Layton 					(how & LOOKUP_REVAL));
38991da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
39006902d925SDave Hansen 	if (IS_ERR(new_dentry))
3901dae6ad8fSAl Viro 		goto out;
3902dae6ad8fSAl Viro 
3903dae6ad8fSAl Viro 	error = -EXDEV;
3904dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
3905dae6ad8fSAl Viro 		goto out_dput;
3906800179c9SKees Cook 	error = may_linkat(&old_path);
3907800179c9SKees Cook 	if (unlikely(error))
3908800179c9SKees Cook 		goto out_dput;
3909dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
3910be6d3e56SKentaro Takeda 	if (error)
3911a8104a9fSAl Viro 		goto out_dput;
3912dae6ad8fSAl Viro 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
391375c3f29dSDave Hansen out_dput:
3914921a1650SAl Viro 	done_path_create(&new_path, new_dentry);
3915442e31caSJeff Layton 	if (retry_estale(error, how)) {
3916442e31caSJeff Layton 		how |= LOOKUP_REVAL;
3917442e31caSJeff Layton 		goto retry;
3918442e31caSJeff Layton 	}
39191da177e4SLinus Torvalds out:
39202d8f3038SAl Viro 	path_put(&old_path);
39211da177e4SLinus Torvalds 
39221da177e4SLinus Torvalds 	return error;
39231da177e4SLinus Torvalds }
39241da177e4SLinus Torvalds 
39253480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
39265590ff0dSUlrich Drepper {
3927c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
39285590ff0dSUlrich Drepper }
39295590ff0dSUlrich Drepper 
39301da177e4SLinus Torvalds /*
39311da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
39321da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
39331da177e4SLinus Torvalds  * Problems:
39341da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
39351da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
39361da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
3937a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
39381da177e4SLinus Torvalds  *	   story.
39391da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
39401b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
39411da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
39421da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
3943a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
39441da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
39451da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
39461da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
3947a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
39481da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
39491da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
39501da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
3951e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
39521b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
39531da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3954c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
39551da177e4SLinus Torvalds  *	   locking].
39561da177e4SLinus Torvalds  */
395775c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
39581da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
39591da177e4SLinus Torvalds {
39601da177e4SLinus Torvalds 	int error = 0;
39619055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
39628de52778SAl Viro 	unsigned max_links = new_dir->i_sb->s_max_links;
39631da177e4SLinus Torvalds 
39641da177e4SLinus Torvalds 	/*
39651da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
39661da177e4SLinus Torvalds 	 * we'll need to flip '..'.
39671da177e4SLinus Torvalds 	 */
39681da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3969f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
39701da177e4SLinus Torvalds 		if (error)
39711da177e4SLinus Torvalds 			return error;
39721da177e4SLinus Torvalds 	}
39731da177e4SLinus Torvalds 
39741da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
39751da177e4SLinus Torvalds 	if (error)
39761da177e4SLinus Torvalds 		return error;
39771da177e4SLinus Torvalds 
39781d2ef590SAl Viro 	dget(new_dentry);
3979d83c49f3SAl Viro 	if (target)
39801b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
39819055cba7SSage Weil 
39821da177e4SLinus Torvalds 	error = -EBUSY;
39839055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
39849055cba7SSage Weil 		goto out;
39859055cba7SSage Weil 
39868de52778SAl Viro 	error = -EMLINK;
39878de52778SAl Viro 	if (max_links && !target && new_dir != old_dir &&
39888de52778SAl Viro 	    new_dir->i_nlink >= max_links)
39898de52778SAl Viro 		goto out;
39908de52778SAl Viro 
39913cebde24SSage Weil 	if (target)
39923cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
39931da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
39949055cba7SSage Weil 	if (error)
39959055cba7SSage Weil 		goto out;
39969055cba7SSage Weil 
39971da177e4SLinus Torvalds 	if (target) {
39981da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
3999d83c49f3SAl Viro 		dont_mount(new_dentry);
4000d83c49f3SAl Viro 	}
40019055cba7SSage Weil out:
40029055cba7SSage Weil 	if (target)
40031b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
40041d2ef590SAl Viro 	dput(new_dentry);
4005e31e14ecSStephen Smalley 	if (!error)
4006349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
40071da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
40081da177e4SLinus Torvalds 	return error;
40091da177e4SLinus Torvalds }
40101da177e4SLinus Torvalds 
401175c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
40121da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
40131da177e4SLinus Torvalds {
401451892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
40151da177e4SLinus Torvalds 	int error;
40161da177e4SLinus Torvalds 
40171da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
40181da177e4SLinus Torvalds 	if (error)
40191da177e4SLinus Torvalds 		return error;
40201da177e4SLinus Torvalds 
40211da177e4SLinus Torvalds 	dget(new_dentry);
40221da177e4SLinus Torvalds 	if (target)
40231b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
402451892bbbSSage Weil 
40251da177e4SLinus Torvalds 	error = -EBUSY;
402651892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
402751892bbbSSage Weil 		goto out;
402851892bbbSSage Weil 
40291da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
403051892bbbSSage Weil 	if (error)
403151892bbbSSage Weil 		goto out;
403251892bbbSSage Weil 
4033bec1052eSAl Viro 	if (target)
4034d83c49f3SAl Viro 		dont_mount(new_dentry);
4035349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
40361da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
403751892bbbSSage Weil out:
40381da177e4SLinus Torvalds 	if (target)
40391b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
40401da177e4SLinus Torvalds 	dput(new_dentry);
40411da177e4SLinus Torvalds 	return error;
40421da177e4SLinus Torvalds }
40431da177e4SLinus Torvalds 
40441da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
40451da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
40461da177e4SLinus Torvalds {
40471da177e4SLinus Torvalds 	int error;
40481da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
404959b0df21SEric Paris 	const unsigned char *old_name;
40501da177e4SLinus Torvalds 
40511da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
40521da177e4SLinus Torvalds  		return 0;
40531da177e4SLinus Torvalds 
40541da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
40551da177e4SLinus Torvalds 	if (error)
40561da177e4SLinus Torvalds 		return error;
40571da177e4SLinus Torvalds 
40581da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
4059a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
40601da177e4SLinus Torvalds 	else
40611da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
40621da177e4SLinus Torvalds 	if (error)
40631da177e4SLinus Torvalds 		return error;
40641da177e4SLinus Torvalds 
4065acfa4380SAl Viro 	if (!old_dir->i_op->rename)
40661da177e4SLinus Torvalds 		return -EPERM;
40671da177e4SLinus Torvalds 
40680eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
40690eeca283SRobert Love 
40701da177e4SLinus Torvalds 	if (is_dir)
40711da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
40721da177e4SLinus Torvalds 	else
40731da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
4074123df294SAl Viro 	if (!error)
4075123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
40765a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
40770eeca283SRobert Love 	fsnotify_oldname_free(old_name);
40780eeca283SRobert Love 
40791da177e4SLinus Torvalds 	return error;
40801da177e4SLinus Torvalds }
40811da177e4SLinus Torvalds 
40822e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
40832e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
40841da177e4SLinus Torvalds {
40851da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
40861da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
40871da177e4SLinus Torvalds 	struct dentry *trap;
40881da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
408991a27b2aSJeff Layton 	struct filename *from;
409091a27b2aSJeff Layton 	struct filename *to;
4091c6a94284SJeff Layton 	unsigned int lookup_flags = 0;
4092c6a94284SJeff Layton 	bool should_retry = false;
40932ad94ae6SAl Viro 	int error;
4094c6a94284SJeff Layton retry:
4095c6a94284SJeff Layton 	from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
409691a27b2aSJeff Layton 	if (IS_ERR(from)) {
409791a27b2aSJeff Layton 		error = PTR_ERR(from);
40981da177e4SLinus Torvalds 		goto exit;
409991a27b2aSJeff Layton 	}
41001da177e4SLinus Torvalds 
4101c6a94284SJeff Layton 	to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
410291a27b2aSJeff Layton 	if (IS_ERR(to)) {
410391a27b2aSJeff Layton 		error = PTR_ERR(to);
41041da177e4SLinus Torvalds 		goto exit1;
410591a27b2aSJeff Layton 	}
41061da177e4SLinus Torvalds 
41071da177e4SLinus Torvalds 	error = -EXDEV;
41084ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
41091da177e4SLinus Torvalds 		goto exit2;
41101da177e4SLinus Torvalds 
41114ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
41121da177e4SLinus Torvalds 	error = -EBUSY;
41131da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
41141da177e4SLinus Torvalds 		goto exit2;
41151da177e4SLinus Torvalds 
41164ac91378SJan Blunck 	new_dir = newnd.path.dentry;
41171da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
41181da177e4SLinus Torvalds 		goto exit2;
41191da177e4SLinus Torvalds 
4120c30dabfeSJan Kara 	error = mnt_want_write(oldnd.path.mnt);
4121c30dabfeSJan Kara 	if (error)
4122c30dabfeSJan Kara 		goto exit2;
4123c30dabfeSJan Kara 
41240612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
41250612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
41264e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
41270612d9fbSOGAWA Hirofumi 
41281da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
41291da177e4SLinus Torvalds 
413049705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
41311da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
41321da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
41331da177e4SLinus Torvalds 		goto exit3;
41341da177e4SLinus Torvalds 	/* source must exist */
41351da177e4SLinus Torvalds 	error = -ENOENT;
41361da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
41371da177e4SLinus Torvalds 		goto exit4;
41381da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
41391da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
41401da177e4SLinus Torvalds 		error = -ENOTDIR;
41411da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
41421da177e4SLinus Torvalds 			goto exit4;
41431da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
41441da177e4SLinus Torvalds 			goto exit4;
41451da177e4SLinus Torvalds 	}
41461da177e4SLinus Torvalds 	/* source should not be ancestor of target */
41471da177e4SLinus Torvalds 	error = -EINVAL;
41481da177e4SLinus Torvalds 	if (old_dentry == trap)
41491da177e4SLinus Torvalds 		goto exit4;
415049705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
41511da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
41521da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
41531da177e4SLinus Torvalds 		goto exit4;
41541da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
41551da177e4SLinus Torvalds 	error = -ENOTEMPTY;
41561da177e4SLinus Torvalds 	if (new_dentry == trap)
41571da177e4SLinus Torvalds 		goto exit5;
41581da177e4SLinus Torvalds 
4159be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
4160be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
4161be6d3e56SKentaro Takeda 	if (error)
4162c30dabfeSJan Kara 		goto exit5;
41631da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
41641da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
41651da177e4SLinus Torvalds exit5:
41661da177e4SLinus Torvalds 	dput(new_dentry);
41671da177e4SLinus Torvalds exit4:
41681da177e4SLinus Torvalds 	dput(old_dentry);
41691da177e4SLinus Torvalds exit3:
41701da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
4171c30dabfeSJan Kara 	mnt_drop_write(oldnd.path.mnt);
41721da177e4SLinus Torvalds exit2:
4173c6a94284SJeff Layton 	if (retry_estale(error, lookup_flags))
4174c6a94284SJeff Layton 		should_retry = true;
41751d957f9bSJan Blunck 	path_put(&newnd.path);
41762ad94ae6SAl Viro 	putname(to);
41771da177e4SLinus Torvalds exit1:
41781d957f9bSJan Blunck 	path_put(&oldnd.path);
41791da177e4SLinus Torvalds 	putname(from);
4180c6a94284SJeff Layton 	if (should_retry) {
4181c6a94284SJeff Layton 		should_retry = false;
4182c6a94284SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
4183c6a94284SJeff Layton 		goto retry;
4184c6a94284SJeff Layton 	}
41852ad94ae6SAl Viro exit:
41861da177e4SLinus Torvalds 	return error;
41871da177e4SLinus Torvalds }
41881da177e4SLinus Torvalds 
4189a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
41905590ff0dSUlrich Drepper {
41915590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
41925590ff0dSUlrich Drepper }
41935590ff0dSUlrich Drepper 
41941da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
41951da177e4SLinus Torvalds {
41961da177e4SLinus Torvalds 	int len;
41971da177e4SLinus Torvalds 
41981da177e4SLinus Torvalds 	len = PTR_ERR(link);
41991da177e4SLinus Torvalds 	if (IS_ERR(link))
42001da177e4SLinus Torvalds 		goto out;
42011da177e4SLinus Torvalds 
42021da177e4SLinus Torvalds 	len = strlen(link);
42031da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
42041da177e4SLinus Torvalds 		len = buflen;
42051da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
42061da177e4SLinus Torvalds 		len = -EFAULT;
42071da177e4SLinus Torvalds out:
42081da177e4SLinus Torvalds 	return len;
42091da177e4SLinus Torvalds }
42101da177e4SLinus Torvalds 
42111da177e4SLinus Torvalds /*
42121da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
42131da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
42141da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
42151da177e4SLinus Torvalds  */
42161da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
42171da177e4SLinus Torvalds {
42181da177e4SLinus Torvalds 	struct nameidata nd;
4219cc314eefSLinus Torvalds 	void *cookie;
4220694a1764SMarcin Slusarz 	int res;
4221cc314eefSLinus Torvalds 
42221da177e4SLinus Torvalds 	nd.depth = 0;
4223cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
4224694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
4225694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
4226694a1764SMarcin Slusarz 
4227694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
42281da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
4229cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4230694a1764SMarcin Slusarz 	return res;
42311da177e4SLinus Torvalds }
42321da177e4SLinus Torvalds 
42331da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
42341da177e4SLinus Torvalds {
42351da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
42361da177e4SLinus Torvalds }
42371da177e4SLinus Torvalds 
42381da177e4SLinus Torvalds /* get the link contents into pagecache */
42391da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
42401da177e4SLinus Torvalds {
4241ebd09abbSDuane Griffin 	char *kaddr;
42421da177e4SLinus Torvalds 	struct page *page;
42431da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
4244090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
42451da177e4SLinus Torvalds 	if (IS_ERR(page))
42466fe6900eSNick Piggin 		return (char*)page;
42471da177e4SLinus Torvalds 	*ppage = page;
4248ebd09abbSDuane Griffin 	kaddr = kmap(page);
4249ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4250ebd09abbSDuane Griffin 	return kaddr;
42511da177e4SLinus Torvalds }
42521da177e4SLinus Torvalds 
42531da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
42541da177e4SLinus Torvalds {
42551da177e4SLinus Torvalds 	struct page *page = NULL;
42561da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
42571da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
42581da177e4SLinus Torvalds 	if (page) {
42591da177e4SLinus Torvalds 		kunmap(page);
42601da177e4SLinus Torvalds 		page_cache_release(page);
42611da177e4SLinus Torvalds 	}
42621da177e4SLinus Torvalds 	return res;
42631da177e4SLinus Torvalds }
42641da177e4SLinus Torvalds 
4265cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
42661da177e4SLinus Torvalds {
4267cc314eefSLinus Torvalds 	struct page *page = NULL;
42681da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
4269cc314eefSLinus Torvalds 	return page;
42701da177e4SLinus Torvalds }
42711da177e4SLinus Torvalds 
4272cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
42731da177e4SLinus Torvalds {
4274cc314eefSLinus Torvalds 	struct page *page = cookie;
4275cc314eefSLinus Torvalds 
4276cc314eefSLinus Torvalds 	if (page) {
42771da177e4SLinus Torvalds 		kunmap(page);
42781da177e4SLinus Torvalds 		page_cache_release(page);
42791da177e4SLinus Torvalds 	}
42801da177e4SLinus Torvalds }
42811da177e4SLinus Torvalds 
428254566b2cSNick Piggin /*
428354566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
428454566b2cSNick Piggin  */
428554566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
42861da177e4SLinus Torvalds {
42871da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
42880adb25d2SKirill Korotaev 	struct page *page;
4289afddba49SNick Piggin 	void *fsdata;
4290beb497abSDmitriy Monakhov 	int err;
42911da177e4SLinus Torvalds 	char *kaddr;
429254566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
429354566b2cSNick Piggin 	if (nofs)
429454566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
42951da177e4SLinus Torvalds 
42967e53cac4SNeilBrown retry:
4297afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
429854566b2cSNick Piggin 				flags, &page, &fsdata);
42991da177e4SLinus Torvalds 	if (err)
4300afddba49SNick Piggin 		goto fail;
4301afddba49SNick Piggin 
4302e8e3c3d6SCong Wang 	kaddr = kmap_atomic(page);
43031da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
4304e8e3c3d6SCong Wang 	kunmap_atomic(kaddr);
4305afddba49SNick Piggin 
4306afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4307afddba49SNick Piggin 							page, fsdata);
43081da177e4SLinus Torvalds 	if (err < 0)
43091da177e4SLinus Torvalds 		goto fail;
4310afddba49SNick Piggin 	if (err < len-1)
4311afddba49SNick Piggin 		goto retry;
4312afddba49SNick Piggin 
43131da177e4SLinus Torvalds 	mark_inode_dirty(inode);
43141da177e4SLinus Torvalds 	return 0;
43151da177e4SLinus Torvalds fail:
43161da177e4SLinus Torvalds 	return err;
43171da177e4SLinus Torvalds }
43181da177e4SLinus Torvalds 
43190adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
43200adb25d2SKirill Korotaev {
43210adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
432254566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
43230adb25d2SKirill Korotaev }
43240adb25d2SKirill Korotaev 
432592e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
43261da177e4SLinus Torvalds 	.readlink	= generic_readlink,
43271da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
43281da177e4SLinus Torvalds 	.put_link	= page_put_link,
43291da177e4SLinus Torvalds };
43301da177e4SLinus Torvalds 
43312d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
4332cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
43331da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
43341da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
4335f6d2ac5cSAl Viro EXPORT_SYMBOL(get_write_access); /* nfsd */
43361da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
43371da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
43381da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
43391da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
43401da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
43410adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
43421da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
43431da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
4344d1811465SAl Viro EXPORT_SYMBOL(kern_path);
434516f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
4346f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
43471da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
43481da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
43491da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
43501da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
43511da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
43521da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
43531da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
43541da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
43551da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
43561da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
43571da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
43581da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
43591da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
43601da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
4361