xref: /openbmc/linux/fs/namei.c (revision e5c832d5)
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);
577e5c832d5SLinus Torvalds 	return -ECHILD;
578e5c832d5SLinus Torvalds out:
579e5c832d5SLinus Torvalds 	unlock_rcu_walk();
58031e6b01fSNick Piggin 	return -ECHILD;
58131e6b01fSNick Piggin }
58231e6b01fSNick Piggin 
5834ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
58434286d66SNick Piggin {
5854ce16ef3SAl Viro 	return dentry->d_op->d_revalidate(dentry, flags);
58634286d66SNick Piggin }
58734286d66SNick Piggin 
5889f1fafeeSAl Viro /**
5899f1fafeeSAl Viro  * complete_walk - successful completion of path walk
5909f1fafeeSAl Viro  * @nd:  pointer nameidata
59139159de2SJeff Layton  *
5929f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
5939f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
5949f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
5959f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
5969f1fafeeSAl Viro  * need to drop nd->path.
59739159de2SJeff Layton  */
5989f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
59939159de2SJeff Layton {
60016c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
60139159de2SJeff Layton 	int status;
60239159de2SJeff Layton 
6039f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
6049f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
6059f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
6069f1fafeeSAl Viro 			nd->root.mnt = NULL;
60715570086SLinus Torvalds 
608e5c832d5SLinus Torvalds 		if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) {
60932a7991bSAl Viro 			unlock_rcu_walk();
6109f1fafeeSAl Viro 			return -ECHILD;
6119f1fafeeSAl Viro 		}
612e5c832d5SLinus Torvalds 		if (read_seqcount_retry(&dentry->d_seq, nd->seq)) {
613e5c832d5SLinus Torvalds 			unlock_rcu_walk();
614e5c832d5SLinus Torvalds 			dput(dentry);
615e5c832d5SLinus Torvalds 			return -ECHILD;
616e5c832d5SLinus Torvalds 		}
6179f1fafeeSAl Viro 		mntget(nd->path.mnt);
61832a7991bSAl Viro 		unlock_rcu_walk();
6199f1fafeeSAl Viro 	}
6209f1fafeeSAl Viro 
62116c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
62239159de2SJeff Layton 		return 0;
62339159de2SJeff Layton 
624ecf3d1f1SJeff Layton 	if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
62516c2cd71SAl Viro 		return 0;
62616c2cd71SAl Viro 
627ecf3d1f1SJeff Layton 	status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
62839159de2SJeff Layton 	if (status > 0)
62939159de2SJeff Layton 		return 0;
63039159de2SJeff Layton 
63116c2cd71SAl Viro 	if (!status)
63239159de2SJeff Layton 		status = -ESTALE;
63316c2cd71SAl Viro 
6349f1fafeeSAl Viro 	path_put(&nd->path);
63539159de2SJeff Layton 	return status;
63639159de2SJeff Layton }
63739159de2SJeff Layton 
6382a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
6392a737871SAl Viro {
640f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
641f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
6422a737871SAl Viro }
6432a737871SAl Viro 
6446de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
6456de88d72SAl Viro 
64631e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
64731e6b01fSNick Piggin {
64831e6b01fSNick Piggin 	if (!nd->root.mnt) {
64931e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
650c28cc364SNick Piggin 		unsigned seq;
651c28cc364SNick Piggin 
652c28cc364SNick Piggin 		do {
653c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
65431e6b01fSNick Piggin 			nd->root = fs->root;
655c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
656c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
65731e6b01fSNick Piggin 	}
65831e6b01fSNick Piggin }
65931e6b01fSNick Piggin 
660f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
6611da177e4SLinus Torvalds {
66231e6b01fSNick Piggin 	int ret;
66331e6b01fSNick Piggin 
6641da177e4SLinus Torvalds 	if (IS_ERR(link))
6651da177e4SLinus Torvalds 		goto fail;
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 	if (*link == '/') {
6682a737871SAl Viro 		set_root(nd);
6691d957f9bSJan Blunck 		path_put(&nd->path);
6702a737871SAl Viro 		nd->path = nd->root;
6712a737871SAl Viro 		path_get(&nd->root);
67216c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
6731da177e4SLinus Torvalds 	}
67431e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
675b4091d5fSChristoph Hellwig 
67631e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
67731e6b01fSNick Piggin 	return ret;
6781da177e4SLinus Torvalds fail:
6791d957f9bSJan Blunck 	path_put(&nd->path);
6801da177e4SLinus Torvalds 	return PTR_ERR(link);
6811da177e4SLinus Torvalds }
6821da177e4SLinus Torvalds 
6831d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
684051d3812SIan Kent {
685051d3812SIan Kent 	dput(path->dentry);
6864ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
687051d3812SIan Kent 		mntput(path->mnt);
688051d3812SIan Kent }
689051d3812SIan Kent 
6907b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
6917b9337aaSNick Piggin 					struct nameidata *nd)
692051d3812SIan Kent {
69331e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
6944ac91378SJan Blunck 		dput(nd->path.dentry);
69531e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
6964ac91378SJan Blunck 			mntput(nd->path.mnt);
6979a229683SHuang Shijie 	}
69831e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
6994ac91378SJan Blunck 	nd->path.dentry = path->dentry;
700051d3812SIan Kent }
701051d3812SIan Kent 
702b5fb63c1SChristoph Hellwig /*
703b5fb63c1SChristoph Hellwig  * Helper to directly jump to a known parsed path from ->follow_link,
704b5fb63c1SChristoph Hellwig  * caller must have taken a reference to path beforehand.
705b5fb63c1SChristoph Hellwig  */
706b5fb63c1SChristoph Hellwig void nd_jump_link(struct nameidata *nd, struct path *path)
707b5fb63c1SChristoph Hellwig {
708b5fb63c1SChristoph Hellwig 	path_put(&nd->path);
709b5fb63c1SChristoph Hellwig 
710b5fb63c1SChristoph Hellwig 	nd->path = *path;
711b5fb63c1SChristoph Hellwig 	nd->inode = nd->path.dentry->d_inode;
712b5fb63c1SChristoph Hellwig 	nd->flags |= LOOKUP_JUMPED;
713b5fb63c1SChristoph Hellwig }
714b5fb63c1SChristoph Hellwig 
715574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
716574197e0SAl Viro {
717574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
7186d7b5aaeSAl Viro 	if (inode->i_op->put_link)
719574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
720574197e0SAl Viro 	path_put(link);
721574197e0SAl Viro }
722574197e0SAl Viro 
723561ec64aSLinus Torvalds int sysctl_protected_symlinks __read_mostly = 0;
724561ec64aSLinus Torvalds int sysctl_protected_hardlinks __read_mostly = 0;
725800179c9SKees Cook 
726800179c9SKees Cook /**
727800179c9SKees Cook  * may_follow_link - Check symlink following for unsafe situations
728800179c9SKees Cook  * @link: The path of the symlink
72955852635SRandy Dunlap  * @nd: nameidata pathwalk data
730800179c9SKees Cook  *
731800179c9SKees Cook  * In the case of the sysctl_protected_symlinks sysctl being enabled,
732800179c9SKees Cook  * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
733800179c9SKees Cook  * in a sticky world-writable directory. This is to protect privileged
734800179c9SKees Cook  * processes from failing races against path names that may change out
735800179c9SKees Cook  * from under them by way of other users creating malicious symlinks.
736800179c9SKees Cook  * It will permit symlinks to be followed only when outside a sticky
737800179c9SKees Cook  * world-writable directory, or when the uid of the symlink and follower
738800179c9SKees Cook  * match, or when the directory owner matches the symlink's owner.
739800179c9SKees Cook  *
740800179c9SKees Cook  * Returns 0 if following the symlink is allowed, -ve on error.
741800179c9SKees Cook  */
742800179c9SKees Cook static inline int may_follow_link(struct path *link, struct nameidata *nd)
743800179c9SKees Cook {
744800179c9SKees Cook 	const struct inode *inode;
745800179c9SKees Cook 	const struct inode *parent;
746800179c9SKees Cook 
747800179c9SKees Cook 	if (!sysctl_protected_symlinks)
748800179c9SKees Cook 		return 0;
749800179c9SKees Cook 
750800179c9SKees Cook 	/* Allowed if owner and follower match. */
751800179c9SKees Cook 	inode = link->dentry->d_inode;
75281abe27bSEric W. Biederman 	if (uid_eq(current_cred()->fsuid, inode->i_uid))
753800179c9SKees Cook 		return 0;
754800179c9SKees Cook 
755800179c9SKees Cook 	/* Allowed if parent directory not sticky and world-writable. */
756800179c9SKees Cook 	parent = nd->path.dentry->d_inode;
757800179c9SKees Cook 	if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
758800179c9SKees Cook 		return 0;
759800179c9SKees Cook 
760800179c9SKees Cook 	/* Allowed if parent directory and link owner match. */
76181abe27bSEric W. Biederman 	if (uid_eq(parent->i_uid, inode->i_uid))
762800179c9SKees Cook 		return 0;
763800179c9SKees Cook 
764ffd8d101SSasha Levin 	audit_log_link_denied("follow_link", link);
765800179c9SKees Cook 	path_put_conditional(link, nd);
766800179c9SKees Cook 	path_put(&nd->path);
767800179c9SKees Cook 	return -EACCES;
768800179c9SKees Cook }
769800179c9SKees Cook 
770800179c9SKees Cook /**
771800179c9SKees Cook  * safe_hardlink_source - Check for safe hardlink conditions
772800179c9SKees Cook  * @inode: the source inode to hardlink from
773800179c9SKees Cook  *
774800179c9SKees Cook  * Return false if at least one of the following conditions:
775800179c9SKees Cook  *    - inode is not a regular file
776800179c9SKees Cook  *    - inode is setuid
777800179c9SKees Cook  *    - inode is setgid and group-exec
778800179c9SKees Cook  *    - access failure for read and write
779800179c9SKees Cook  *
780800179c9SKees Cook  * Otherwise returns true.
781800179c9SKees Cook  */
782800179c9SKees Cook static bool safe_hardlink_source(struct inode *inode)
783800179c9SKees Cook {
784800179c9SKees Cook 	umode_t mode = inode->i_mode;
785800179c9SKees Cook 
786800179c9SKees Cook 	/* Special files should not get pinned to the filesystem. */
787800179c9SKees Cook 	if (!S_ISREG(mode))
788800179c9SKees Cook 		return false;
789800179c9SKees Cook 
790800179c9SKees Cook 	/* Setuid files should not get pinned to the filesystem. */
791800179c9SKees Cook 	if (mode & S_ISUID)
792800179c9SKees Cook 		return false;
793800179c9SKees Cook 
794800179c9SKees Cook 	/* Executable setgid files should not get pinned to the filesystem. */
795800179c9SKees Cook 	if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
796800179c9SKees Cook 		return false;
797800179c9SKees Cook 
798800179c9SKees Cook 	/* Hardlinking to unreadable or unwritable sources is dangerous. */
799800179c9SKees Cook 	if (inode_permission(inode, MAY_READ | MAY_WRITE))
800800179c9SKees Cook 		return false;
801800179c9SKees Cook 
802800179c9SKees Cook 	return true;
803800179c9SKees Cook }
804800179c9SKees Cook 
805800179c9SKees Cook /**
806800179c9SKees Cook  * may_linkat - Check permissions for creating a hardlink
807800179c9SKees Cook  * @link: the source to hardlink from
808800179c9SKees Cook  *
809800179c9SKees Cook  * Block hardlink when all of:
810800179c9SKees Cook  *  - sysctl_protected_hardlinks enabled
811800179c9SKees Cook  *  - fsuid does not match inode
812800179c9SKees Cook  *  - hardlink source is unsafe (see safe_hardlink_source() above)
813800179c9SKees Cook  *  - not CAP_FOWNER
814800179c9SKees Cook  *
815800179c9SKees Cook  * Returns 0 if successful, -ve on error.
816800179c9SKees Cook  */
817800179c9SKees Cook static int may_linkat(struct path *link)
818800179c9SKees Cook {
819800179c9SKees Cook 	const struct cred *cred;
820800179c9SKees Cook 	struct inode *inode;
821800179c9SKees Cook 
822800179c9SKees Cook 	if (!sysctl_protected_hardlinks)
823800179c9SKees Cook 		return 0;
824800179c9SKees Cook 
825800179c9SKees Cook 	cred = current_cred();
826800179c9SKees Cook 	inode = link->dentry->d_inode;
827800179c9SKees Cook 
828800179c9SKees Cook 	/* Source inode owner (or CAP_FOWNER) can hardlink all they like,
829800179c9SKees Cook 	 * otherwise, it must be a safe source.
830800179c9SKees Cook 	 */
83181abe27bSEric W. Biederman 	if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
832800179c9SKees Cook 	    capable(CAP_FOWNER))
833800179c9SKees Cook 		return 0;
834800179c9SKees Cook 
835a51d9eaaSKees Cook 	audit_log_link_denied("linkat", link);
836800179c9SKees Cook 	return -EPERM;
837800179c9SKees Cook }
838800179c9SKees Cook 
839def4af30SAl Viro static __always_inline int
840574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
8411da177e4SLinus Torvalds {
8427b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
8436d7b5aaeSAl Viro 	int error;
8446d7b5aaeSAl Viro 	char *s;
8451da177e4SLinus Torvalds 
846844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
847844a3917SAl Viro 
8480e794589SAl Viro 	if (link->mnt == nd->path.mnt)
8490e794589SAl Viro 		mntget(link->mnt);
8500e794589SAl Viro 
8516d7b5aaeSAl Viro 	error = -ELOOP;
8526d7b5aaeSAl Viro 	if (unlikely(current->total_link_count >= 40))
8536d7b5aaeSAl Viro 		goto out_put_nd_path;
8546d7b5aaeSAl Viro 
855574197e0SAl Viro 	cond_resched();
856574197e0SAl Viro 	current->total_link_count++;
857574197e0SAl Viro 
85868ac1234SAl Viro 	touch_atime(link);
8591da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
860cd4e91d3SAl Viro 
86136f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
8626d7b5aaeSAl Viro 	if (error)
8636d7b5aaeSAl Viro 		goto out_put_nd_path;
86436f3b4f6SAl Viro 
86586acdca1SAl Viro 	nd->last_type = LAST_BIND;
866def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
867def4af30SAl Viro 	error = PTR_ERR(*p);
8686d7b5aaeSAl Viro 	if (IS_ERR(*p))
869408ef013SChristoph Hellwig 		goto out_put_nd_path;
8706d7b5aaeSAl Viro 
871cc314eefSLinus Torvalds 	error = 0;
8726d7b5aaeSAl Viro 	s = nd_get_link(nd);
8736d7b5aaeSAl Viro 	if (s) {
8741da177e4SLinus Torvalds 		error = __vfs_follow_link(nd, s);
8756d7b5aaeSAl Viro 		if (unlikely(error))
8766d7b5aaeSAl Viro 			put_link(nd, link, *p);
877b5fb63c1SChristoph Hellwig 	}
8786d7b5aaeSAl Viro 
8796d7b5aaeSAl Viro 	return error;
8806d7b5aaeSAl Viro 
8816d7b5aaeSAl Viro out_put_nd_path:
88298f6ef64SArnd Bergmann 	*p = NULL;
8836d7b5aaeSAl Viro 	path_put(&nd->path);
8846d7b5aaeSAl Viro 	path_put(link);
8851da177e4SLinus Torvalds 	return error;
8861da177e4SLinus Torvalds }
8871da177e4SLinus Torvalds 
88831e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
88931e6b01fSNick Piggin {
8900714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
8910714a533SAl Viro 	struct mount *parent;
89231e6b01fSNick Piggin 	struct dentry *mountpoint;
89331e6b01fSNick Piggin 
8940714a533SAl Viro 	parent = mnt->mnt_parent;
8950714a533SAl Viro 	if (&parent->mnt == path->mnt)
89631e6b01fSNick Piggin 		return 0;
897a73324daSAl Viro 	mountpoint = mnt->mnt_mountpoint;
89831e6b01fSNick Piggin 	path->dentry = mountpoint;
8990714a533SAl Viro 	path->mnt = &parent->mnt;
90031e6b01fSNick Piggin 	return 1;
90131e6b01fSNick Piggin }
90231e6b01fSNick Piggin 
903f015f126SDavid Howells /*
904f015f126SDavid Howells  * follow_up - Find the mountpoint of path's vfsmount
905f015f126SDavid Howells  *
906f015f126SDavid Howells  * Given a path, find the mountpoint of its source file system.
907f015f126SDavid Howells  * Replace @path with the path of the mountpoint in the parent mount.
908f015f126SDavid Howells  * Up is towards /.
909f015f126SDavid Howells  *
910f015f126SDavid Howells  * Return 1 if we went up a level and 0 if we were already at the
911f015f126SDavid Howells  * root.
912f015f126SDavid Howells  */
913bab77ebfSAl Viro int follow_up(struct path *path)
9141da177e4SLinus Torvalds {
9150714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
9160714a533SAl Viro 	struct mount *parent;
9171da177e4SLinus Torvalds 	struct dentry *mountpoint;
91899b7db7bSNick Piggin 
919962830dfSAndi Kleen 	br_read_lock(&vfsmount_lock);
9200714a533SAl Viro 	parent = mnt->mnt_parent;
9213c0a6163SAl Viro 	if (parent == mnt) {
922962830dfSAndi Kleen 		br_read_unlock(&vfsmount_lock);
9231da177e4SLinus Torvalds 		return 0;
9241da177e4SLinus Torvalds 	}
9250714a533SAl Viro 	mntget(&parent->mnt);
926a73324daSAl Viro 	mountpoint = dget(mnt->mnt_mountpoint);
927962830dfSAndi Kleen 	br_read_unlock(&vfsmount_lock);
928bab77ebfSAl Viro 	dput(path->dentry);
929bab77ebfSAl Viro 	path->dentry = mountpoint;
930bab77ebfSAl Viro 	mntput(path->mnt);
9310714a533SAl Viro 	path->mnt = &parent->mnt;
9321da177e4SLinus Torvalds 	return 1;
9331da177e4SLinus Torvalds }
9341da177e4SLinus Torvalds 
935b5c84bf6SNick Piggin /*
9369875cf80SDavid Howells  * Perform an automount
9379875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
9389875cf80SDavid Howells  *   were called with.
9391da177e4SLinus Torvalds  */
9409875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
9419875cf80SDavid Howells 			    bool *need_mntput)
94231e6b01fSNick Piggin {
9439875cf80SDavid Howells 	struct vfsmount *mnt;
944ea5b778aSDavid Howells 	int err;
9459875cf80SDavid Howells 
9469875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
9479875cf80SDavid Howells 		return -EREMOTE;
9489875cf80SDavid Howells 
9490ec26fd0SMiklos Szeredi 	/* We don't want to mount if someone's just doing a stat -
9500ec26fd0SMiklos Szeredi 	 * unless they're stat'ing a directory and appended a '/' to
9510ec26fd0SMiklos Szeredi 	 * the name.
9520ec26fd0SMiklos Szeredi 	 *
9530ec26fd0SMiklos Szeredi 	 * We do, however, want to mount if someone wants to open or
9545a30d8a2SDavid Howells 	 * create a file of any type under the mountpoint, wants to
9550ec26fd0SMiklos Szeredi 	 * traverse through the mountpoint or wants to open the
9560ec26fd0SMiklos Szeredi 	 * mounted directory.  Also, autofs may mark negative dentries
9570ec26fd0SMiklos Szeredi 	 * as being automount points.  These will need the attentions
9580ec26fd0SMiklos Szeredi 	 * of the daemon to instantiate them before they can be used.
9595a30d8a2SDavid Howells 	 */
9605a30d8a2SDavid Howells 	if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
961d94c177bSLinus Torvalds 		     LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
9625a30d8a2SDavid Howells 	    path->dentry->d_inode)
9639875cf80SDavid Howells 		return -EISDIR;
9640ec26fd0SMiklos Szeredi 
9659875cf80SDavid Howells 	current->total_link_count++;
9669875cf80SDavid Howells 	if (current->total_link_count >= 40)
9679875cf80SDavid Howells 		return -ELOOP;
9689875cf80SDavid Howells 
9699875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
9709875cf80SDavid Howells 	if (IS_ERR(mnt)) {
9719875cf80SDavid Howells 		/*
9729875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
9739875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
9749875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
9759875cf80SDavid Howells 		 *
9769875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
9779875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
9789875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
9799875cf80SDavid Howells 		 */
98049084c3bSAl Viro 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
9819875cf80SDavid Howells 			return -EREMOTE;
9829875cf80SDavid Howells 		return PTR_ERR(mnt);
98331e6b01fSNick Piggin 	}
984ea5b778aSDavid Howells 
9859875cf80SDavid Howells 	if (!mnt) /* mount collision */
9869875cf80SDavid Howells 		return 0;
9879875cf80SDavid Howells 
9888aef1884SAl Viro 	if (!*need_mntput) {
9898aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
9908aef1884SAl Viro 		mntget(path->mnt);
9918aef1884SAl Viro 		*need_mntput = true;
9928aef1884SAl Viro 	}
99319a167afSAl Viro 	err = finish_automount(mnt, path);
994ea5b778aSDavid Howells 
995ea5b778aSDavid Howells 	switch (err) {
996ea5b778aSDavid Howells 	case -EBUSY:
997ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
99819a167afSAl Viro 		return 0;
999ea5b778aSDavid Howells 	case 0:
10008aef1884SAl Viro 		path_put(path);
10019875cf80SDavid Howells 		path->mnt = mnt;
10029875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
10039875cf80SDavid Howells 		return 0;
100419a167afSAl Viro 	default:
100519a167afSAl Viro 		return err;
10069875cf80SDavid Howells 	}
100719a167afSAl Viro 
1008ea5b778aSDavid Howells }
10099875cf80SDavid Howells 
10109875cf80SDavid Howells /*
10119875cf80SDavid Howells  * Handle a dentry that is managed in some way.
1012cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
10139875cf80SDavid Howells  * - Flagged as mountpoint
10149875cf80SDavid Howells  * - Flagged as automount point
10159875cf80SDavid Howells  *
10169875cf80SDavid Howells  * This may only be called in refwalk mode.
10179875cf80SDavid Howells  *
10189875cf80SDavid Howells  * Serialization is taken care of in namespace.c
10199875cf80SDavid Howells  */
10209875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
10219875cf80SDavid Howells {
10228aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
10239875cf80SDavid Howells 	unsigned managed;
10249875cf80SDavid Howells 	bool need_mntput = false;
10258aef1884SAl Viro 	int ret = 0;
10269875cf80SDavid Howells 
10279875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
10289875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
10299875cf80SDavid Howells 	 * the components of that value change under us */
10309875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
10319875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
10329875cf80SDavid Howells 	       unlikely(managed != 0)) {
1033cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1034cc53ce53SDavid Howells 		 * being held. */
1035cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1036cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1037cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
10381aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
1039cc53ce53SDavid Howells 			if (ret < 0)
10408aef1884SAl Viro 				break;
1041cc53ce53SDavid Howells 		}
1042cc53ce53SDavid Howells 
10439875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
10449875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
10459875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
10469875cf80SDavid Howells 			if (mounted) {
10479875cf80SDavid Howells 				dput(path->dentry);
10489875cf80SDavid Howells 				if (need_mntput)
1049463ffb2eSAl Viro 					mntput(path->mnt);
1050463ffb2eSAl Viro 				path->mnt = mounted;
1051463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
10529875cf80SDavid Howells 				need_mntput = true;
10539875cf80SDavid Howells 				continue;
1054463ffb2eSAl Viro 			}
1055463ffb2eSAl Viro 
10569875cf80SDavid Howells 			/* Something is mounted on this dentry in another
10579875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
10589875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
10599875cf80SDavid Howells 			 * vfsmount_lock */
10601da177e4SLinus Torvalds 		}
10619875cf80SDavid Howells 
10629875cf80SDavid Howells 		/* Handle an automount point */
10639875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
10649875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
10659875cf80SDavid Howells 			if (ret < 0)
10668aef1884SAl Viro 				break;
10679875cf80SDavid Howells 			continue;
10689875cf80SDavid Howells 		}
10699875cf80SDavid Howells 
10709875cf80SDavid Howells 		/* We didn't change the current path point */
10719875cf80SDavid Howells 		break;
10729875cf80SDavid Howells 	}
10738aef1884SAl Viro 
10748aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
10758aef1884SAl Viro 		mntput(path->mnt);
10768aef1884SAl Viro 	if (ret == -EISDIR)
10778aef1884SAl Viro 		ret = 0;
1078a3fbbde7SAl Viro 	return ret < 0 ? ret : need_mntput;
10791da177e4SLinus Torvalds }
10801da177e4SLinus Torvalds 
1081cc53ce53SDavid Howells int follow_down_one(struct path *path)
10821da177e4SLinus Torvalds {
10831da177e4SLinus Torvalds 	struct vfsmount *mounted;
10841da177e4SLinus Torvalds 
10851c755af4SAl Viro 	mounted = lookup_mnt(path);
10861da177e4SLinus Torvalds 	if (mounted) {
10879393bd07SAl Viro 		dput(path->dentry);
10889393bd07SAl Viro 		mntput(path->mnt);
10899393bd07SAl Viro 		path->mnt = mounted;
10909393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
10911da177e4SLinus Torvalds 		return 1;
10921da177e4SLinus Torvalds 	}
10931da177e4SLinus Torvalds 	return 0;
10941da177e4SLinus Torvalds }
10951da177e4SLinus Torvalds 
109662a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
109762a7375eSIan Kent {
109862a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
109962a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
110062a7375eSIan Kent }
110162a7375eSIan Kent 
11029875cf80SDavid Howells /*
1103287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
1104287548e4SAl Viro  * we meet a managed dentry that would need blocking.
11059875cf80SDavid Howells  */
11069875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1107287548e4SAl Viro 			       struct inode **inode)
11089875cf80SDavid Howells {
110962a7375eSIan Kent 	for (;;) {
1110c7105365SAl Viro 		struct mount *mounted;
111162a7375eSIan Kent 		/*
111262a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
111362a7375eSIan Kent 		 * that wants to block transit.
111462a7375eSIan Kent 		 */
1115287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
1116ab90911fSDavid Howells 			return false;
111762a7375eSIan Kent 
111862a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
111962a7375eSIan Kent 			break;
112062a7375eSIan Kent 
11219875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
11229875cf80SDavid Howells 		if (!mounted)
11239875cf80SDavid Howells 			break;
1124c7105365SAl Viro 		path->mnt = &mounted->mnt;
1125c7105365SAl Viro 		path->dentry = mounted->mnt.mnt_root;
1126a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
11279875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
112859430262SLinus Torvalds 		/*
112959430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
113059430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
113159430262SLinus Torvalds 		 * because a mount-point is always pinned.
113259430262SLinus Torvalds 		 */
113359430262SLinus Torvalds 		*inode = path->dentry->d_inode;
11349875cf80SDavid Howells 	}
11359875cf80SDavid Howells 	return true;
11369875cf80SDavid Howells }
11379875cf80SDavid Howells 
1138dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
1139287548e4SAl Viro {
1140dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
1141c7105365SAl Viro 		struct mount *mounted;
1142dea39376SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
1143287548e4SAl Viro 		if (!mounted)
1144287548e4SAl Viro 			break;
1145c7105365SAl Viro 		nd->path.mnt = &mounted->mnt;
1146c7105365SAl Viro 		nd->path.dentry = mounted->mnt.mnt_root;
1147dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1148287548e4SAl Viro 	}
1149287548e4SAl Viro }
1150287548e4SAl Viro 
115131e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
115231e6b01fSNick Piggin {
115331e6b01fSNick Piggin 	set_root_rcu(nd);
115431e6b01fSNick Piggin 
115531e6b01fSNick Piggin 	while (1) {
115631e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
115731e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
115831e6b01fSNick Piggin 			break;
115931e6b01fSNick Piggin 		}
116031e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
116131e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
116231e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
116331e6b01fSNick Piggin 			unsigned seq;
116431e6b01fSNick Piggin 
116531e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
116631e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
1167ef7562d5SAl Viro 				goto failed;
116831e6b01fSNick Piggin 			nd->path.dentry = parent;
116931e6b01fSNick Piggin 			nd->seq = seq;
117031e6b01fSNick Piggin 			break;
117131e6b01fSNick Piggin 		}
117231e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
117331e6b01fSNick Piggin 			break;
117431e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
117531e6b01fSNick Piggin 	}
1176dea39376SAl Viro 	follow_mount_rcu(nd);
1177dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
117831e6b01fSNick Piggin 	return 0;
1179ef7562d5SAl Viro 
1180ef7562d5SAl Viro failed:
1181ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
11825b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
1183ef7562d5SAl Viro 		nd->root.mnt = NULL;
118432a7991bSAl Viro 	unlock_rcu_walk();
1185ef7562d5SAl Viro 	return -ECHILD;
118631e6b01fSNick Piggin }
118731e6b01fSNick Piggin 
11889875cf80SDavid Howells /*
1189cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
1190cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
1191cc53ce53SDavid Howells  * caller is permitted to proceed or not.
1192cc53ce53SDavid Howells  */
11937cc90cc3SAl Viro int follow_down(struct path *path)
1194cc53ce53SDavid Howells {
1195cc53ce53SDavid Howells 	unsigned managed;
1196cc53ce53SDavid Howells 	int ret;
1197cc53ce53SDavid Howells 
1198cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
1199cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1200cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1201cc53ce53SDavid Howells 		 * being held.
1202cc53ce53SDavid Howells 		 *
1203cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
1204cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
1205cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
1206cc53ce53SDavid Howells 		 * superstructure.
1207cc53ce53SDavid Howells 		 *
1208cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
1209cc53ce53SDavid Howells 		 */
1210cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1211cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1212cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
1213ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
12141aed3e42SAl Viro 				path->dentry, false);
1215cc53ce53SDavid Howells 			if (ret < 0)
1216cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
1217cc53ce53SDavid Howells 		}
1218cc53ce53SDavid Howells 
1219cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
1220cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
1221cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
1222cc53ce53SDavid Howells 			if (!mounted)
1223cc53ce53SDavid Howells 				break;
1224cc53ce53SDavid Howells 			dput(path->dentry);
1225cc53ce53SDavid Howells 			mntput(path->mnt);
1226cc53ce53SDavid Howells 			path->mnt = mounted;
1227cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1228cc53ce53SDavid Howells 			continue;
1229cc53ce53SDavid Howells 		}
1230cc53ce53SDavid Howells 
1231cc53ce53SDavid Howells 		/* Don't handle automount points here */
1232cc53ce53SDavid Howells 		break;
1233cc53ce53SDavid Howells 	}
1234cc53ce53SDavid Howells 	return 0;
1235cc53ce53SDavid Howells }
1236cc53ce53SDavid Howells 
1237cc53ce53SDavid Howells /*
12389875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
12399875cf80SDavid Howells  */
12409875cf80SDavid Howells static void follow_mount(struct path *path)
12419875cf80SDavid Howells {
12429875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
12439875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
12449875cf80SDavid Howells 		if (!mounted)
12459875cf80SDavid Howells 			break;
12469875cf80SDavid Howells 		dput(path->dentry);
12479875cf80SDavid Howells 		mntput(path->mnt);
12489875cf80SDavid Howells 		path->mnt = mounted;
12499875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
12509875cf80SDavid Howells 	}
12519875cf80SDavid Howells }
12529875cf80SDavid Howells 
125331e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
12541da177e4SLinus Torvalds {
12552a737871SAl Viro 	set_root(nd);
1256e518ddb7SAndreas Mohr 
12571da177e4SLinus Torvalds 	while(1) {
12584ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
12591da177e4SLinus Torvalds 
12602a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
12612a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
12621da177e4SLinus Torvalds 			break;
12631da177e4SLinus Torvalds 		}
12644ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
12653088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
12663088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
12671da177e4SLinus Torvalds 			dput(old);
12681da177e4SLinus Torvalds 			break;
12691da177e4SLinus Torvalds 		}
12703088dd70SAl Viro 		if (!follow_up(&nd->path))
12711da177e4SLinus Torvalds 			break;
12721da177e4SLinus Torvalds 	}
127379ed0226SAl Viro 	follow_mount(&nd->path);
127431e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
12751da177e4SLinus Torvalds }
12761da177e4SLinus Torvalds 
12771da177e4SLinus Torvalds /*
1278bad61189SMiklos Szeredi  * This looks up the name in dcache, possibly revalidates the old dentry and
1279bad61189SMiklos Szeredi  * allocates a new one if not found or not valid.  In the need_lookup argument
1280bad61189SMiklos Szeredi  * returns whether i_op->lookup is necessary.
1281bad61189SMiklos Szeredi  *
1282bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
1283baa03890SNick Piggin  */
1284bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1285201f956eSAl Viro 				    unsigned int flags, bool *need_lookup)
1286baa03890SNick Piggin {
1287baa03890SNick Piggin 	struct dentry *dentry;
1288bad61189SMiklos Szeredi 	int error;
1289baa03890SNick Piggin 
1290bad61189SMiklos Szeredi 	*need_lookup = false;
1291bad61189SMiklos Szeredi 	dentry = d_lookup(dir, name);
1292bad61189SMiklos Szeredi 	if (dentry) {
129339e3c955SJeff Layton 		if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1294201f956eSAl Viro 			error = d_revalidate(dentry, flags);
1295bad61189SMiklos Szeredi 			if (unlikely(error <= 0)) {
1296bad61189SMiklos Szeredi 				if (error < 0) {
1297bad61189SMiklos Szeredi 					dput(dentry);
1298bad61189SMiklos Szeredi 					return ERR_PTR(error);
1299bad61189SMiklos Szeredi 				} else if (!d_invalidate(dentry)) {
1300bad61189SMiklos Szeredi 					dput(dentry);
1301bad61189SMiklos Szeredi 					dentry = NULL;
1302bad61189SMiklos Szeredi 				}
1303bad61189SMiklos Szeredi 			}
1304bad61189SMiklos Szeredi 		}
1305bad61189SMiklos Szeredi 	}
1306baa03890SNick Piggin 
1307bad61189SMiklos Szeredi 	if (!dentry) {
1308bad61189SMiklos Szeredi 		dentry = d_alloc(dir, name);
1309baa03890SNick Piggin 		if (unlikely(!dentry))
1310baa03890SNick Piggin 			return ERR_PTR(-ENOMEM);
1311baa03890SNick Piggin 
1312bad61189SMiklos Szeredi 		*need_lookup = true;
1313baa03890SNick Piggin 	}
1314baa03890SNick Piggin 	return dentry;
1315baa03890SNick Piggin }
1316baa03890SNick Piggin 
1317baa03890SNick Piggin /*
1318bad61189SMiklos Szeredi  * Call i_op->lookup on the dentry.  The dentry must be negative but may be
1319bad61189SMiklos Szeredi  * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1320bad61189SMiklos Szeredi  *
1321bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
132244396f4bSJosef Bacik  */
1323bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
132472bd866aSAl Viro 				  unsigned int flags)
132544396f4bSJosef Bacik {
132644396f4bSJosef Bacik 	struct dentry *old;
132744396f4bSJosef Bacik 
132844396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
1329bad61189SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
1330e188dc02SMiklos Szeredi 		dput(dentry);
133144396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
1332e188dc02SMiklos Szeredi 	}
133344396f4bSJosef Bacik 
133472bd866aSAl Viro 	old = dir->i_op->lookup(dir, dentry, flags);
133544396f4bSJosef Bacik 	if (unlikely(old)) {
133644396f4bSJosef Bacik 		dput(dentry);
133744396f4bSJosef Bacik 		dentry = old;
133844396f4bSJosef Bacik 	}
133944396f4bSJosef Bacik 	return dentry;
134044396f4bSJosef Bacik }
134144396f4bSJosef Bacik 
1342a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name,
134372bd866aSAl Viro 		struct dentry *base, unsigned int flags)
1344a3255546SAl Viro {
1345bad61189SMiklos Szeredi 	bool need_lookup;
1346a3255546SAl Viro 	struct dentry *dentry;
1347a3255546SAl Viro 
134872bd866aSAl Viro 	dentry = lookup_dcache(name, base, flags, &need_lookup);
1349bad61189SMiklos Szeredi 	if (!need_lookup)
1350a3255546SAl Viro 		return dentry;
1351bad61189SMiklos Szeredi 
135272bd866aSAl Viro 	return lookup_real(base->d_inode, dentry, flags);
1353a3255546SAl Viro }
1354a3255546SAl Viro 
135544396f4bSJosef Bacik /*
13561da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
13571da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
13581da177e4SLinus Torvalds  *  It _is_ time-critical.
13591da177e4SLinus Torvalds  */
1360e97cdc87SAl Viro static int lookup_fast(struct nameidata *nd,
136131e6b01fSNick Piggin 		       struct path *path, struct inode **inode)
13621da177e4SLinus Torvalds {
13634ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
136431e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
13655a18fff2SAl Viro 	int need_reval = 1;
13665a18fff2SAl Viro 	int status = 1;
13679875cf80SDavid Howells 	int err;
13689875cf80SDavid Howells 
13693cac260aSAl Viro 	/*
1370b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1371b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1372b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1373b04f784eSNick Piggin 	 */
137431e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
137531e6b01fSNick Piggin 		unsigned seq;
1376da53be12SLinus Torvalds 		dentry = __d_lookup_rcu(parent, &nd->last, &seq);
13775a18fff2SAl Viro 		if (!dentry)
13785a18fff2SAl Viro 			goto unlazy;
13795a18fff2SAl Viro 
138012f8ad4bSLinus Torvalds 		/*
138112f8ad4bSLinus Torvalds 		 * This sequence count validates that the inode matches
138212f8ad4bSLinus Torvalds 		 * the dentry name information from lookup.
138312f8ad4bSLinus Torvalds 		 */
138412f8ad4bSLinus Torvalds 		*inode = dentry->d_inode;
138512f8ad4bSLinus Torvalds 		if (read_seqcount_retry(&dentry->d_seq, seq))
138612f8ad4bSLinus Torvalds 			return -ECHILD;
138712f8ad4bSLinus Torvalds 
138812f8ad4bSLinus Torvalds 		/*
138912f8ad4bSLinus Torvalds 		 * This sequence count validates that the parent had no
139012f8ad4bSLinus Torvalds 		 * changes while we did the lookup of the dentry above.
139112f8ad4bSLinus Torvalds 		 *
139212f8ad4bSLinus Torvalds 		 * The memory barrier in read_seqcount_begin of child is
139312f8ad4bSLinus Torvalds 		 *  enough, we can use __read_seqcount_retry here.
139412f8ad4bSLinus Torvalds 		 */
139531e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
139631e6b01fSNick Piggin 			return -ECHILD;
139731e6b01fSNick Piggin 		nd->seq = seq;
13985a18fff2SAl Viro 
139924643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
14004ce16ef3SAl Viro 			status = d_revalidate(dentry, nd->flags);
14015a18fff2SAl Viro 			if (unlikely(status <= 0)) {
14025a18fff2SAl Viro 				if (status != -ECHILD)
14035a18fff2SAl Viro 					need_reval = 0;
14045a18fff2SAl Viro 				goto unlazy;
14055a18fff2SAl Viro 			}
140624643087SAl Viro 		}
140731e6b01fSNick Piggin 		path->mnt = mnt;
140831e6b01fSNick Piggin 		path->dentry = dentry;
1409d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1410d6e9bd25SAl Viro 			goto unlazy;
1411d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1412d6e9bd25SAl Viro 			goto unlazy;
14139875cf80SDavid Howells 		return 0;
14145a18fff2SAl Viro unlazy:
141519660af7SAl Viro 		if (unlazy_walk(nd, dentry))
14165a18fff2SAl Viro 			return -ECHILD;
14175a18fff2SAl Viro 	} else {
1418e97cdc87SAl Viro 		dentry = __d_lookup(parent, &nd->last);
141924643087SAl Viro 	}
14205a18fff2SAl Viro 
142181e6f520SAl Viro 	if (unlikely(!dentry))
142281e6f520SAl Viro 		goto need_lookup;
14235a18fff2SAl Viro 
14245a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
14254ce16ef3SAl Viro 		status = d_revalidate(dentry, nd->flags);
14265a18fff2SAl Viro 	if (unlikely(status <= 0)) {
14275a18fff2SAl Viro 		if (status < 0) {
14285a18fff2SAl Viro 			dput(dentry);
14295a18fff2SAl Viro 			return status;
14305a18fff2SAl Viro 		}
14315a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
14325a18fff2SAl Viro 			dput(dentry);
143381e6f520SAl Viro 			goto need_lookup;
14345a18fff2SAl Viro 		}
14355a18fff2SAl Viro 	}
1436697f514dSMiklos Szeredi 
14371da177e4SLinus Torvalds 	path->mnt = mnt;
14381da177e4SLinus Torvalds 	path->dentry = dentry;
14399875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
144089312214SIan Kent 	if (unlikely(err < 0)) {
144189312214SIan Kent 		path_put_conditional(path, nd);
14429875cf80SDavid Howells 		return err;
144389312214SIan Kent 	}
1444a3fbbde7SAl Viro 	if (err)
1445a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
144631e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
14471da177e4SLinus Torvalds 	return 0;
144881e6f520SAl Viro 
144981e6f520SAl Viro need_lookup:
1450697f514dSMiklos Szeredi 	return 1;
1451697f514dSMiklos Szeredi }
1452697f514dSMiklos Szeredi 
1453697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */
1454cc2a5271SAl Viro static int lookup_slow(struct nameidata *nd, struct path *path)
1455697f514dSMiklos Szeredi {
1456697f514dSMiklos Szeredi 	struct dentry *dentry, *parent;
1457697f514dSMiklos Szeredi 	int err;
1458697f514dSMiklos Szeredi 
1459697f514dSMiklos Szeredi 	parent = nd->path.dentry;
146081e6f520SAl Viro 	BUG_ON(nd->inode != parent->d_inode);
146181e6f520SAl Viro 
146281e6f520SAl Viro 	mutex_lock(&parent->d_inode->i_mutex);
1463cc2a5271SAl Viro 	dentry = __lookup_hash(&nd->last, parent, nd->flags);
146481e6f520SAl Viro 	mutex_unlock(&parent->d_inode->i_mutex);
146581e6f520SAl Viro 	if (IS_ERR(dentry))
146681e6f520SAl Viro 		return PTR_ERR(dentry);
1467697f514dSMiklos Szeredi 	path->mnt = nd->path.mnt;
1468697f514dSMiklos Szeredi 	path->dentry = dentry;
1469697f514dSMiklos Szeredi 	err = follow_managed(path, nd->flags);
1470697f514dSMiklos Szeredi 	if (unlikely(err < 0)) {
1471697f514dSMiklos Szeredi 		path_put_conditional(path, nd);
1472697f514dSMiklos Szeredi 		return err;
1473697f514dSMiklos Szeredi 	}
1474697f514dSMiklos Szeredi 	if (err)
1475697f514dSMiklos Szeredi 		nd->flags |= LOOKUP_JUMPED;
1476697f514dSMiklos Szeredi 	return 0;
14771da177e4SLinus Torvalds }
14781da177e4SLinus Torvalds 
147952094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
148052094c8aSAl Viro {
148152094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
14824ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
148352094c8aSAl Viro 		if (err != -ECHILD)
148452094c8aSAl Viro 			return err;
148519660af7SAl Viro 		if (unlazy_walk(nd, NULL))
148652094c8aSAl Viro 			return -ECHILD;
148752094c8aSAl Viro 	}
14884ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
148952094c8aSAl Viro }
149052094c8aSAl Viro 
14919856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
14929856fa1bSAl Viro {
14939856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
14949856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
14959856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
14969856fa1bSAl Viro 				return -ECHILD;
14979856fa1bSAl Viro 		} else
14989856fa1bSAl Viro 			follow_dotdot(nd);
14999856fa1bSAl Viro 	}
15009856fa1bSAl Viro 	return 0;
15019856fa1bSAl Viro }
15029856fa1bSAl Viro 
1503951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1504951361f9SAl Viro {
1505951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1506951361f9SAl Viro 		path_put(&nd->path);
1507951361f9SAl Viro 	} else {
1508951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
15095b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1510951361f9SAl Viro 			nd->root.mnt = NULL;
151132a7991bSAl Viro 		unlock_rcu_walk();
1512951361f9SAl Viro 	}
1513951361f9SAl Viro }
1514951361f9SAl Viro 
15153ddcd056SLinus Torvalds /*
15163ddcd056SLinus Torvalds  * Do we need to follow links? We _really_ want to be able
15173ddcd056SLinus Torvalds  * to do this check without having to look at inode->i_op,
15183ddcd056SLinus Torvalds  * so we keep a cache of "no, this doesn't need follow_link"
15193ddcd056SLinus Torvalds  * for the common case.
15203ddcd056SLinus Torvalds  */
15217813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow)
15223ddcd056SLinus Torvalds {
15233ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
15243ddcd056SLinus Torvalds 		if (likely(inode->i_op->follow_link))
15253ddcd056SLinus Torvalds 			return follow;
15263ddcd056SLinus Torvalds 
15273ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
15283ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
15293ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_NOFOLLOW;
15303ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
15313ddcd056SLinus Torvalds 	}
15323ddcd056SLinus Torvalds 	return 0;
15333ddcd056SLinus Torvalds }
15343ddcd056SLinus Torvalds 
1535ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
153621b9b073SAl Viro 		int follow)
1537ce57dfc1SAl Viro {
1538ce57dfc1SAl Viro 	struct inode *inode;
1539ce57dfc1SAl Viro 	int err;
1540ce57dfc1SAl Viro 	/*
1541ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1542ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1543ce57dfc1SAl Viro 	 * parent relationships.
1544ce57dfc1SAl Viro 	 */
154521b9b073SAl Viro 	if (unlikely(nd->last_type != LAST_NORM))
154621b9b073SAl Viro 		return handle_dots(nd, nd->last_type);
1547e97cdc87SAl Viro 	err = lookup_fast(nd, path, &inode);
1548ce57dfc1SAl Viro 	if (unlikely(err)) {
1549697f514dSMiklos Szeredi 		if (err < 0)
1550697f514dSMiklos Szeredi 			goto out_err;
1551697f514dSMiklos Szeredi 
1552cc2a5271SAl Viro 		err = lookup_slow(nd, path);
1553697f514dSMiklos Szeredi 		if (err < 0)
1554697f514dSMiklos Szeredi 			goto out_err;
1555697f514dSMiklos Szeredi 
1556697f514dSMiklos Szeredi 		inode = path->dentry->d_inode;
1557ce57dfc1SAl Viro 	}
1558697f514dSMiklos Szeredi 	err = -ENOENT;
1559697f514dSMiklos Szeredi 	if (!inode)
1560697f514dSMiklos Szeredi 		goto out_path_put;
1561697f514dSMiklos Szeredi 
15627813b94aSLinus Torvalds 	if (should_follow_link(inode, follow)) {
156319660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
156419660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
1565697f514dSMiklos Szeredi 				err = -ECHILD;
1566697f514dSMiklos Szeredi 				goto out_err;
156719660af7SAl Viro 			}
156819660af7SAl Viro 		}
1569ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1570ce57dfc1SAl Viro 		return 1;
1571ce57dfc1SAl Viro 	}
1572ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1573ce57dfc1SAl Viro 	nd->inode = inode;
1574ce57dfc1SAl Viro 	return 0;
1575697f514dSMiklos Szeredi 
1576697f514dSMiklos Szeredi out_path_put:
1577697f514dSMiklos Szeredi 	path_to_nameidata(path, nd);
1578697f514dSMiklos Szeredi out_err:
1579697f514dSMiklos Szeredi 	terminate_walk(nd);
1580697f514dSMiklos Szeredi 	return err;
1581ce57dfc1SAl Viro }
1582ce57dfc1SAl Viro 
15831da177e4SLinus Torvalds /*
1584b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1585b356379aSAl Viro  * limiting consecutive symlinks to 40.
1586b356379aSAl Viro  *
1587b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1588b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1589b356379aSAl Viro  */
1590b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1591b356379aSAl Viro {
1592b356379aSAl Viro 	int res;
1593b356379aSAl Viro 
1594b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1595b356379aSAl Viro 		path_put_conditional(path, nd);
1596b356379aSAl Viro 		path_put(&nd->path);
1597b356379aSAl Viro 		return -ELOOP;
1598b356379aSAl Viro 	}
15991a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1600b356379aSAl Viro 
1601b356379aSAl Viro 	nd->depth++;
1602b356379aSAl Viro 	current->link_count++;
1603b356379aSAl Viro 
1604b356379aSAl Viro 	do {
1605b356379aSAl Viro 		struct path link = *path;
1606b356379aSAl Viro 		void *cookie;
1607574197e0SAl Viro 
1608574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
16096d7b5aaeSAl Viro 		if (res)
16106d7b5aaeSAl Viro 			break;
161121b9b073SAl Viro 		res = walk_component(nd, path, LOOKUP_FOLLOW);
1612574197e0SAl Viro 		put_link(nd, &link, cookie);
1613b356379aSAl Viro 	} while (res > 0);
1614b356379aSAl Viro 
1615b356379aSAl Viro 	current->link_count--;
1616b356379aSAl Viro 	nd->depth--;
1617b356379aSAl Viro 	return res;
1618b356379aSAl Viro }
1619b356379aSAl Viro 
1620b356379aSAl Viro /*
16213ddcd056SLinus Torvalds  * We really don't want to look at inode->i_op->lookup
16223ddcd056SLinus Torvalds  * when we don't have to. So we keep a cache bit in
16233ddcd056SLinus Torvalds  * the inode ->i_opflags field that says "yes, we can
16243ddcd056SLinus Torvalds  * do lookup on this inode".
16253ddcd056SLinus Torvalds  */
16263ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode)
16273ddcd056SLinus Torvalds {
16283ddcd056SLinus Torvalds 	if (likely(inode->i_opflags & IOP_LOOKUP))
16293ddcd056SLinus Torvalds 		return 1;
16303ddcd056SLinus Torvalds 	if (likely(!inode->i_op->lookup))
16313ddcd056SLinus Torvalds 		return 0;
16323ddcd056SLinus Torvalds 
16333ddcd056SLinus Torvalds 	/* We do this once for the lifetime of the inode */
16343ddcd056SLinus Torvalds 	spin_lock(&inode->i_lock);
16353ddcd056SLinus Torvalds 	inode->i_opflags |= IOP_LOOKUP;
16363ddcd056SLinus Torvalds 	spin_unlock(&inode->i_lock);
16373ddcd056SLinus Torvalds 	return 1;
16383ddcd056SLinus Torvalds }
16393ddcd056SLinus Torvalds 
1640bfcfaa77SLinus Torvalds /*
1641bfcfaa77SLinus Torvalds  * We can do the critical dentry name comparison and hashing
1642bfcfaa77SLinus Torvalds  * operations one word at a time, but we are limited to:
1643bfcfaa77SLinus Torvalds  *
1644bfcfaa77SLinus Torvalds  * - Architectures with fast unaligned word accesses. We could
1645bfcfaa77SLinus Torvalds  *   do a "get_unaligned()" if this helps and is sufficiently
1646bfcfaa77SLinus Torvalds  *   fast.
1647bfcfaa77SLinus Torvalds  *
1648bfcfaa77SLinus Torvalds  * - Little-endian machines (so that we can generate the mask
1649bfcfaa77SLinus Torvalds  *   of low bytes efficiently). Again, we *could* do a byte
1650bfcfaa77SLinus Torvalds  *   swapping load on big-endian architectures if that is not
1651bfcfaa77SLinus Torvalds  *   expensive enough to make the optimization worthless.
1652bfcfaa77SLinus Torvalds  *
1653bfcfaa77SLinus Torvalds  * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1654bfcfaa77SLinus Torvalds  *   do not trap on the (extremely unlikely) case of a page
1655bfcfaa77SLinus Torvalds  *   crossing operation.
1656bfcfaa77SLinus Torvalds  *
1657bfcfaa77SLinus Torvalds  * - Furthermore, we need an efficient 64-bit compile for the
1658bfcfaa77SLinus Torvalds  *   64-bit case in order to generate the "number of bytes in
1659bfcfaa77SLinus Torvalds  *   the final mask". Again, that could be replaced with a
1660bfcfaa77SLinus Torvalds  *   efficient population count instruction or similar.
1661bfcfaa77SLinus Torvalds  */
1662bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS
1663bfcfaa77SLinus Torvalds 
1664f68e556eSLinus Torvalds #include <asm/word-at-a-time.h>
1665bfcfaa77SLinus Torvalds 
1666f68e556eSLinus Torvalds #ifdef CONFIG_64BIT
1667bfcfaa77SLinus Torvalds 
1668bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash)
1669bfcfaa77SLinus Torvalds {
1670bfcfaa77SLinus Torvalds 	hash += hash >> (8*sizeof(int));
1671bfcfaa77SLinus Torvalds 	return hash;
1672bfcfaa77SLinus Torvalds }
1673bfcfaa77SLinus Torvalds 
1674bfcfaa77SLinus Torvalds #else	/* 32-bit case */
1675bfcfaa77SLinus Torvalds 
1676bfcfaa77SLinus Torvalds #define fold_hash(x) (x)
1677bfcfaa77SLinus Torvalds 
1678bfcfaa77SLinus Torvalds #endif
1679bfcfaa77SLinus Torvalds 
1680bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1681bfcfaa77SLinus Torvalds {
1682bfcfaa77SLinus Torvalds 	unsigned long a, mask;
1683bfcfaa77SLinus Torvalds 	unsigned long hash = 0;
1684bfcfaa77SLinus Torvalds 
1685bfcfaa77SLinus Torvalds 	for (;;) {
1686e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name);
1687bfcfaa77SLinus Torvalds 		if (len < sizeof(unsigned long))
1688bfcfaa77SLinus Torvalds 			break;
1689bfcfaa77SLinus Torvalds 		hash += a;
1690f132c5beSAl Viro 		hash *= 9;
1691bfcfaa77SLinus Torvalds 		name += sizeof(unsigned long);
1692bfcfaa77SLinus Torvalds 		len -= sizeof(unsigned long);
1693bfcfaa77SLinus Torvalds 		if (!len)
1694bfcfaa77SLinus Torvalds 			goto done;
1695bfcfaa77SLinus Torvalds 	}
1696bfcfaa77SLinus Torvalds 	mask = ~(~0ul << len*8);
1697bfcfaa77SLinus Torvalds 	hash += mask & a;
1698bfcfaa77SLinus Torvalds done:
1699bfcfaa77SLinus Torvalds 	return fold_hash(hash);
1700bfcfaa77SLinus Torvalds }
1701bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
1702bfcfaa77SLinus Torvalds 
1703bfcfaa77SLinus Torvalds /*
1704bfcfaa77SLinus Torvalds  * Calculate the length and hash of the path component, and
1705bfcfaa77SLinus Torvalds  * return the length of the component;
1706bfcfaa77SLinus Torvalds  */
1707bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1708bfcfaa77SLinus Torvalds {
170936126f8fSLinus Torvalds 	unsigned long a, b, adata, bdata, mask, hash, len;
171036126f8fSLinus Torvalds 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1711bfcfaa77SLinus Torvalds 
1712bfcfaa77SLinus Torvalds 	hash = a = 0;
1713bfcfaa77SLinus Torvalds 	len = -sizeof(unsigned long);
1714bfcfaa77SLinus Torvalds 	do {
1715bfcfaa77SLinus Torvalds 		hash = (hash + a) * 9;
1716bfcfaa77SLinus Torvalds 		len += sizeof(unsigned long);
1717e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name+len);
171836126f8fSLinus Torvalds 		b = a ^ REPEAT_BYTE('/');
171936126f8fSLinus Torvalds 	} while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1720bfcfaa77SLinus Torvalds 
172136126f8fSLinus Torvalds 	adata = prep_zero_mask(a, adata, &constants);
172236126f8fSLinus Torvalds 	bdata = prep_zero_mask(b, bdata, &constants);
172336126f8fSLinus Torvalds 
172436126f8fSLinus Torvalds 	mask = create_zero_mask(adata | bdata);
172536126f8fSLinus Torvalds 
172636126f8fSLinus Torvalds 	hash += a & zero_bytemask(mask);
1727bfcfaa77SLinus Torvalds 	*hashp = fold_hash(hash);
1728bfcfaa77SLinus Torvalds 
172936126f8fSLinus Torvalds 	return len + find_zero(mask);
1730bfcfaa77SLinus Torvalds }
1731bfcfaa77SLinus Torvalds 
1732bfcfaa77SLinus Torvalds #else
1733bfcfaa77SLinus Torvalds 
17340145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
17350145acc2SLinus Torvalds {
17360145acc2SLinus Torvalds 	unsigned long hash = init_name_hash();
17370145acc2SLinus Torvalds 	while (len--)
17380145acc2SLinus Torvalds 		hash = partial_name_hash(*name++, hash);
17390145acc2SLinus Torvalds 	return end_name_hash(hash);
17400145acc2SLinus Torvalds }
1741ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
17420145acc2SLinus Torvalds 
17433ddcd056SLinus Torvalds /*
1744200e9ef7SLinus Torvalds  * We know there's a real path component here of at least
1745200e9ef7SLinus Torvalds  * one character.
1746200e9ef7SLinus Torvalds  */
1747200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1748200e9ef7SLinus Torvalds {
1749200e9ef7SLinus Torvalds 	unsigned long hash = init_name_hash();
1750200e9ef7SLinus Torvalds 	unsigned long len = 0, c;
1751200e9ef7SLinus Torvalds 
1752200e9ef7SLinus Torvalds 	c = (unsigned char)*name;
1753200e9ef7SLinus Torvalds 	do {
1754200e9ef7SLinus Torvalds 		len++;
1755200e9ef7SLinus Torvalds 		hash = partial_name_hash(c, hash);
1756200e9ef7SLinus Torvalds 		c = (unsigned char)name[len];
1757200e9ef7SLinus Torvalds 	} while (c && c != '/');
1758200e9ef7SLinus Torvalds 	*hashp = end_name_hash(hash);
1759200e9ef7SLinus Torvalds 	return len;
1760200e9ef7SLinus Torvalds }
1761200e9ef7SLinus Torvalds 
1762bfcfaa77SLinus Torvalds #endif
1763bfcfaa77SLinus Torvalds 
1764200e9ef7SLinus Torvalds /*
17651da177e4SLinus Torvalds  * Name resolution.
1766ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1767ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
17681da177e4SLinus Torvalds  *
1769ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1770ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
17711da177e4SLinus Torvalds  */
17726de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
17731da177e4SLinus Torvalds {
17741da177e4SLinus Torvalds 	struct path next;
17751da177e4SLinus Torvalds 	int err;
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds 	while (*name=='/')
17781da177e4SLinus Torvalds 		name++;
17791da177e4SLinus Torvalds 	if (!*name)
1780086e183aSAl Viro 		return 0;
17811da177e4SLinus Torvalds 
17821da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
17831da177e4SLinus Torvalds 	for(;;) {
17841da177e4SLinus Torvalds 		struct qstr this;
1785200e9ef7SLinus Torvalds 		long len;
1786fe479a58SAl Viro 		int type;
17871da177e4SLinus Torvalds 
178852094c8aSAl Viro 		err = may_lookup(nd);
17891da177e4SLinus Torvalds  		if (err)
17901da177e4SLinus Torvalds 			break;
17911da177e4SLinus Torvalds 
1792200e9ef7SLinus Torvalds 		len = hash_name(name, &this.hash);
17931da177e4SLinus Torvalds 		this.name = name;
1794200e9ef7SLinus Torvalds 		this.len = len;
17951da177e4SLinus Torvalds 
1796fe479a58SAl Viro 		type = LAST_NORM;
1797200e9ef7SLinus Torvalds 		if (name[0] == '.') switch (len) {
1798fe479a58SAl Viro 			case 2:
1799200e9ef7SLinus Torvalds 				if (name[1] == '.') {
1800fe479a58SAl Viro 					type = LAST_DOTDOT;
180116c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
180216c2cd71SAl Viro 				}
1803fe479a58SAl Viro 				break;
1804fe479a58SAl Viro 			case 1:
1805fe479a58SAl Viro 				type = LAST_DOT;
1806fe479a58SAl Viro 		}
18075a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
18085a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
180916c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
18105a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1811da53be12SLinus Torvalds 				err = parent->d_op->d_hash(parent, &this);
18125a202bcdSAl Viro 				if (err < 0)
18135a202bcdSAl Viro 					break;
18145a202bcdSAl Viro 			}
18155a202bcdSAl Viro 		}
1816fe479a58SAl Viro 
18175f4a6a69SAl Viro 		nd->last = this;
18185f4a6a69SAl Viro 		nd->last_type = type;
18195f4a6a69SAl Viro 
1820200e9ef7SLinus Torvalds 		if (!name[len])
18215f4a6a69SAl Viro 			return 0;
1822200e9ef7SLinus Torvalds 		/*
1823200e9ef7SLinus Torvalds 		 * If it wasn't NUL, we know it was '/'. Skip that
1824200e9ef7SLinus Torvalds 		 * slash, and continue until no more slashes.
1825200e9ef7SLinus Torvalds 		 */
1826200e9ef7SLinus Torvalds 		do {
1827200e9ef7SLinus Torvalds 			len++;
1828200e9ef7SLinus Torvalds 		} while (unlikely(name[len] == '/'));
1829200e9ef7SLinus Torvalds 		if (!name[len])
18305f4a6a69SAl Viro 			return 0;
18315f4a6a69SAl Viro 
1832200e9ef7SLinus Torvalds 		name += len;
18331da177e4SLinus Torvalds 
183421b9b073SAl Viro 		err = walk_component(nd, &next, LOOKUP_FOLLOW);
1835ce57dfc1SAl Viro 		if (err < 0)
1836ce57dfc1SAl Viro 			return err;
1837fe479a58SAl Viro 
1838ce57dfc1SAl Viro 		if (err) {
1839b356379aSAl Viro 			err = nested_symlink(&next, nd);
18401da177e4SLinus Torvalds 			if (err)
1841a7472babSAl Viro 				return err;
184231e6b01fSNick Piggin 		}
18435f4a6a69SAl Viro 		if (!can_lookup(nd->inode)) {
18443ddcd056SLinus Torvalds 			err = -ENOTDIR;
18453ddcd056SLinus Torvalds 			break;
18465f4a6a69SAl Viro 		}
1847ce57dfc1SAl Viro 	}
1848951361f9SAl Viro 	terminate_walk(nd);
18491da177e4SLinus Torvalds 	return err;
18501da177e4SLinus Torvalds }
18511da177e4SLinus Torvalds 
185270e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
185370e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
185431e6b01fSNick Piggin {
185531e6b01fSNick Piggin 	int retval = 0;
185631e6b01fSNick Piggin 
185731e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
185816c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
185931e6b01fSNick Piggin 	nd->depth = 0;
18605b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
18615b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
186273d049a4SAl Viro 		if (*name) {
1863741b7c3fSAl Viro 			if (!can_lookup(inode))
18645b6ca027SAl Viro 				return -ENOTDIR;
18655b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
18665b6ca027SAl Viro 			if (retval)
18675b6ca027SAl Viro 				return retval;
186873d049a4SAl Viro 		}
18695b6ca027SAl Viro 		nd->path = nd->root;
18705b6ca027SAl Viro 		nd->inode = inode;
18715b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
187232a7991bSAl Viro 			lock_rcu_walk();
18735b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
18745b6ca027SAl Viro 		} else {
18755b6ca027SAl Viro 			path_get(&nd->path);
18765b6ca027SAl Viro 		}
18775b6ca027SAl Viro 		return 0;
18785b6ca027SAl Viro 	}
18795b6ca027SAl Viro 
188031e6b01fSNick Piggin 	nd->root.mnt = NULL;
188131e6b01fSNick Piggin 
188231e6b01fSNick Piggin 	if (*name=='/') {
1883e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
188432a7991bSAl Viro 			lock_rcu_walk();
1885e41f7d4eSAl Viro 			set_root_rcu(nd);
1886e41f7d4eSAl Viro 		} else {
1887e41f7d4eSAl Viro 			set_root(nd);
1888e41f7d4eSAl Viro 			path_get(&nd->root);
1889e41f7d4eSAl Viro 		}
189031e6b01fSNick Piggin 		nd->path = nd->root;
189131e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1892e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
189331e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1894c28cc364SNick Piggin 			unsigned seq;
189531e6b01fSNick Piggin 
189632a7991bSAl Viro 			lock_rcu_walk();
189731e6b01fSNick Piggin 
1898c28cc364SNick Piggin 			do {
1899c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
190031e6b01fSNick Piggin 				nd->path = fs->pwd;
1901c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1902c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1903e41f7d4eSAl Viro 		} else {
1904e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1905e41f7d4eSAl Viro 		}
190631e6b01fSNick Piggin 	} else {
1907582aa64aSJeff Layton 		/* Caller must check execute permissions on the starting path component */
19082903ff01SAl Viro 		struct fd f = fdget_raw(dfd);
190931e6b01fSNick Piggin 		struct dentry *dentry;
191031e6b01fSNick Piggin 
19112903ff01SAl Viro 		if (!f.file)
19122903ff01SAl Viro 			return -EBADF;
191331e6b01fSNick Piggin 
19142903ff01SAl Viro 		dentry = f.file->f_path.dentry;
191531e6b01fSNick Piggin 
1916f52e0c11SAl Viro 		if (*name) {
1917741b7c3fSAl Viro 			if (!can_lookup(dentry->d_inode)) {
19182903ff01SAl Viro 				fdput(f);
19192903ff01SAl Viro 				return -ENOTDIR;
1920f52e0c11SAl Viro 			}
19212903ff01SAl Viro 		}
19222903ff01SAl Viro 
19232903ff01SAl Viro 		nd->path = f.file->f_path;
1924e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
19252903ff01SAl Viro 			if (f.need_put)
19262903ff01SAl Viro 				*fp = f.file;
1927c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
192832a7991bSAl Viro 			lock_rcu_walk();
19295590ff0dSUlrich Drepper 		} else {
19302903ff01SAl Viro 			path_get(&nd->path);
19312903ff01SAl Viro 			fdput(f);
19321da177e4SLinus Torvalds 		}
1933e41f7d4eSAl Viro 	}
1934e41f7d4eSAl Viro 
193531e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
19369b4a9b14SAl Viro 	return 0;
19379b4a9b14SAl Viro }
19389b4a9b14SAl Viro 
1939bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1940bd92d7feSAl Viro {
1941bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1942bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1943bd92d7feSAl Viro 
1944bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
194521b9b073SAl Viro 	return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
1946bd92d7feSAl Viro }
1947bd92d7feSAl Viro 
19489b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1949ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
19509b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
19519b4a9b14SAl Viro {
195270e9b357SAl Viro 	struct file *base = NULL;
1953bd92d7feSAl Viro 	struct path path;
1954bd92d7feSAl Viro 	int err;
195531e6b01fSNick Piggin 
195631e6b01fSNick Piggin 	/*
195731e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
195831e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
195931e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
196031e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
196131e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
196231e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
196331e6b01fSNick Piggin 	 * analogue, foo_rcu().
196431e6b01fSNick Piggin 	 *
196531e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
196631e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
196731e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
196831e6b01fSNick Piggin 	 * be able to complete).
196931e6b01fSNick Piggin 	 */
1970bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1971ee0827cdSAl Viro 
1972bd92d7feSAl Viro 	if (unlikely(err))
1973bd92d7feSAl Viro 		return err;
1974ee0827cdSAl Viro 
1975ee0827cdSAl Viro 	current->total_link_count = 0;
1976bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1977bd92d7feSAl Viro 
1978bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1979bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1980bd92d7feSAl Viro 		while (err > 0) {
1981bd92d7feSAl Viro 			void *cookie;
1982bd92d7feSAl Viro 			struct path link = path;
1983800179c9SKees Cook 			err = may_follow_link(&link, nd);
1984800179c9SKees Cook 			if (unlikely(err))
1985800179c9SKees Cook 				break;
1986bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1987574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
19886d7b5aaeSAl Viro 			if (err)
19896d7b5aaeSAl Viro 				break;
1990bd92d7feSAl Viro 			err = lookup_last(nd, &path);
1991574197e0SAl Viro 			put_link(nd, &link, cookie);
1992bd92d7feSAl Viro 		}
1993bd92d7feSAl Viro 	}
1994ee0827cdSAl Viro 
19959f1fafeeSAl Viro 	if (!err)
19969f1fafeeSAl Viro 		err = complete_walk(nd);
1997bd92d7feSAl Viro 
1998bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
199905252901SAl Viro 		if (!can_lookup(nd->inode)) {
2000bd92d7feSAl Viro 			path_put(&nd->path);
2001bd23a539SAl Viro 			err = -ENOTDIR;
2002bd92d7feSAl Viro 		}
2003bd92d7feSAl Viro 	}
200416c2cd71SAl Viro 
200570e9b357SAl Viro 	if (base)
200670e9b357SAl Viro 		fput(base);
2007ee0827cdSAl Viro 
20085b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
200931e6b01fSNick Piggin 		path_put(&nd->root);
201031e6b01fSNick Piggin 		nd->root.mnt = NULL;
201131e6b01fSNick Piggin 	}
2012bd92d7feSAl Viro 	return err;
201331e6b01fSNick Piggin }
201431e6b01fSNick Piggin 
2015873f1eedSJeff Layton static int filename_lookup(int dfd, struct filename *name,
2016873f1eedSJeff Layton 				unsigned int flags, struct nameidata *nd)
2017873f1eedSJeff Layton {
2018873f1eedSJeff Layton 	int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
2019873f1eedSJeff Layton 	if (unlikely(retval == -ECHILD))
2020873f1eedSJeff Layton 		retval = path_lookupat(dfd, name->name, flags, nd);
2021873f1eedSJeff Layton 	if (unlikely(retval == -ESTALE))
2022873f1eedSJeff Layton 		retval = path_lookupat(dfd, name->name,
2023873f1eedSJeff Layton 						flags | LOOKUP_REVAL, nd);
2024873f1eedSJeff Layton 
2025873f1eedSJeff Layton 	if (likely(!retval))
2026adb5c247SJeff Layton 		audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
2027873f1eedSJeff Layton 	return retval;
2028873f1eedSJeff Layton }
2029873f1eedSJeff Layton 
2030ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
2031ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
2032ee0827cdSAl Viro {
2033873f1eedSJeff Layton 	struct filename filename = { .name = name };
2034ee0827cdSAl Viro 
2035873f1eedSJeff Layton 	return filename_lookup(dfd, &filename, flags, nd);
20361da177e4SLinus Torvalds }
20371da177e4SLinus Torvalds 
203879714f72SAl Viro /* does lookup, returns the object with parent locked */
203979714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path)
20405590ff0dSUlrich Drepper {
204179714f72SAl Viro 	struct nameidata nd;
204279714f72SAl Viro 	struct dentry *d;
204379714f72SAl Viro 	int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
204479714f72SAl Viro 	if (err)
204579714f72SAl Viro 		return ERR_PTR(err);
204679714f72SAl Viro 	if (nd.last_type != LAST_NORM) {
204779714f72SAl Viro 		path_put(&nd.path);
204879714f72SAl Viro 		return ERR_PTR(-EINVAL);
204979714f72SAl Viro 	}
205079714f72SAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
20511e0ea001SAl Viro 	d = __lookup_hash(&nd.last, nd.path.dentry, 0);
205279714f72SAl Viro 	if (IS_ERR(d)) {
205379714f72SAl Viro 		mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
205479714f72SAl Viro 		path_put(&nd.path);
205579714f72SAl Viro 		return d;
205679714f72SAl Viro 	}
205779714f72SAl Viro 	*path = nd.path;
205879714f72SAl Viro 	return d;
20595590ff0dSUlrich Drepper }
20605590ff0dSUlrich Drepper 
2061d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
2062d1811465SAl Viro {
2063d1811465SAl Viro 	struct nameidata nd;
2064d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2065d1811465SAl Viro 	if (!res)
2066d1811465SAl Viro 		*path = nd.path;
2067d1811465SAl Viro 	return res;
2068d1811465SAl Viro }
2069d1811465SAl Viro 
207016f18200SJosef 'Jeff' Sipek /**
207116f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
207216f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
207316f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
207416f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
207516f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
2076e0a01249SAl Viro  * @path: pointer to struct path to fill
207716f18200SJosef 'Jeff' Sipek  */
207816f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
207916f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
2080e0a01249SAl Viro 		    struct path *path)
208116f18200SJosef 'Jeff' Sipek {
2082e0a01249SAl Viro 	struct nameidata nd;
2083e0a01249SAl Viro 	int err;
2084e0a01249SAl Viro 	nd.root.dentry = dentry;
2085e0a01249SAl Viro 	nd.root.mnt = mnt;
2086e0a01249SAl Viro 	BUG_ON(flags & LOOKUP_PARENT);
20875b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
2088e0a01249SAl Viro 	err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2089e0a01249SAl Viro 	if (!err)
2090e0a01249SAl Viro 		*path = nd.path;
2091e0a01249SAl Viro 	return err;
209216f18200SJosef 'Jeff' Sipek }
209316f18200SJosef 'Jeff' Sipek 
2094057f6c01SJames Morris /*
2095057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
2096057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
2097057f6c01SJames Morris  * SMP-safe.
2098057f6c01SJames Morris  */
2099a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
21001da177e4SLinus Torvalds {
210172bd866aSAl Viro 	return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
21021da177e4SLinus Torvalds }
21031da177e4SLinus Torvalds 
2104eead1911SChristoph Hellwig /**
2105a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
2106eead1911SChristoph Hellwig  * @name:	pathname component to lookup
2107eead1911SChristoph Hellwig  * @base:	base directory to lookup from
2108eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
2109eead1911SChristoph Hellwig  *
2110a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
2111a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
2112eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
2113eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
2114eead1911SChristoph Hellwig  */
2115057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2116057f6c01SJames Morris {
2117057f6c01SJames Morris 	struct qstr this;
21186a96ba54SAl Viro 	unsigned int c;
2119cda309deSMiklos Szeredi 	int err;
2120057f6c01SJames Morris 
21212f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
21222f9092e1SDavid Woodhouse 
21236a96ba54SAl Viro 	this.name = name;
21246a96ba54SAl Viro 	this.len = len;
21250145acc2SLinus Torvalds 	this.hash = full_name_hash(name, len);
21266a96ba54SAl Viro 	if (!len)
21276a96ba54SAl Viro 		return ERR_PTR(-EACCES);
21286a96ba54SAl Viro 
212921d8a15aSAl Viro 	if (unlikely(name[0] == '.')) {
213021d8a15aSAl Viro 		if (len < 2 || (len == 2 && name[1] == '.'))
213121d8a15aSAl Viro 			return ERR_PTR(-EACCES);
213221d8a15aSAl Viro 	}
213321d8a15aSAl Viro 
21346a96ba54SAl Viro 	while (len--) {
21356a96ba54SAl Viro 		c = *(const unsigned char *)name++;
21366a96ba54SAl Viro 		if (c == '/' || c == '\0')
21376a96ba54SAl Viro 			return ERR_PTR(-EACCES);
21386a96ba54SAl Viro 	}
21395a202bcdSAl Viro 	/*
21405a202bcdSAl Viro 	 * See if the low-level filesystem might want
21415a202bcdSAl Viro 	 * to use its own hash..
21425a202bcdSAl Viro 	 */
21435a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
2144da53be12SLinus Torvalds 		int err = base->d_op->d_hash(base, &this);
21455a202bcdSAl Viro 		if (err < 0)
21465a202bcdSAl Viro 			return ERR_PTR(err);
21475a202bcdSAl Viro 	}
2148eead1911SChristoph Hellwig 
2149cda309deSMiklos Szeredi 	err = inode_permission(base->d_inode, MAY_EXEC);
2150cda309deSMiklos Szeredi 	if (err)
2151cda309deSMiklos Szeredi 		return ERR_PTR(err);
2152cda309deSMiklos Szeredi 
215372bd866aSAl Viro 	return __lookup_hash(&this, base, 0);
2154057f6c01SJames Morris }
2155057f6c01SJames Morris 
21561fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
21571fa1e7f6SAndy Whitcroft 		 struct path *path, int *empty)
21581da177e4SLinus Torvalds {
21592d8f3038SAl Viro 	struct nameidata nd;
216091a27b2aSJeff Layton 	struct filename *tmp = getname_flags(name, flags, empty);
21611da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
21621da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
21632d8f3038SAl Viro 
21642d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
21652d8f3038SAl Viro 
2166873f1eedSJeff Layton 		err = filename_lookup(dfd, tmp, flags, &nd);
21671da177e4SLinus Torvalds 		putname(tmp);
21682d8f3038SAl Viro 		if (!err)
21692d8f3038SAl Viro 			*path = nd.path;
21701da177e4SLinus Torvalds 	}
21711da177e4SLinus Torvalds 	return err;
21721da177e4SLinus Torvalds }
21731da177e4SLinus Torvalds 
21741fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags,
21751fa1e7f6SAndy Whitcroft 		 struct path *path)
21761fa1e7f6SAndy Whitcroft {
2177f7493e5dSLinus Torvalds 	return user_path_at_empty(dfd, name, flags, path, NULL);
21781fa1e7f6SAndy Whitcroft }
21791fa1e7f6SAndy Whitcroft 
2180873f1eedSJeff Layton /*
2181873f1eedSJeff Layton  * NB: most callers don't do anything directly with the reference to the
2182873f1eedSJeff Layton  *     to struct filename, but the nd->last pointer points into the name string
2183873f1eedSJeff Layton  *     allocated by getname. So we must hold the reference to it until all
2184873f1eedSJeff Layton  *     path-walking is complete.
2185873f1eedSJeff Layton  */
218691a27b2aSJeff Layton static struct filename *
21879e790bd6SJeff Layton user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
21889e790bd6SJeff Layton 		 unsigned int flags)
21892ad94ae6SAl Viro {
219091a27b2aSJeff Layton 	struct filename *s = getname(path);
21912ad94ae6SAl Viro 	int error;
21922ad94ae6SAl Viro 
21939e790bd6SJeff Layton 	/* only LOOKUP_REVAL is allowed in extra flags */
21949e790bd6SJeff Layton 	flags &= LOOKUP_REVAL;
21959e790bd6SJeff Layton 
21962ad94ae6SAl Viro 	if (IS_ERR(s))
219791a27b2aSJeff Layton 		return s;
21982ad94ae6SAl Viro 
21999e790bd6SJeff Layton 	error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
220091a27b2aSJeff Layton 	if (error) {
22012ad94ae6SAl Viro 		putname(s);
220291a27b2aSJeff Layton 		return ERR_PTR(error);
220391a27b2aSJeff Layton 	}
22042ad94ae6SAl Viro 
220591a27b2aSJeff Layton 	return s;
22062ad94ae6SAl Viro }
22072ad94ae6SAl Viro 
22088033426eSJeff Layton /**
22098033426eSJeff Layton  * umount_lookup_last - look up last component for umount
22108033426eSJeff Layton  * @nd:   pathwalk nameidata - currently pointing at parent directory of "last"
22118033426eSJeff Layton  * @path: pointer to container for result
22128033426eSJeff Layton  *
22138033426eSJeff Layton  * This is a special lookup_last function just for umount. In this case, we
22148033426eSJeff Layton  * need to resolve the path without doing any revalidation.
22158033426eSJeff Layton  *
22168033426eSJeff Layton  * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
22178033426eSJeff Layton  * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
22188033426eSJeff Layton  * in almost all cases, this lookup will be served out of the dcache. The only
22198033426eSJeff Layton  * cases where it won't are if nd->last refers to a symlink or the path is
22208033426eSJeff Layton  * bogus and it doesn't exist.
22218033426eSJeff Layton  *
22228033426eSJeff Layton  * Returns:
22238033426eSJeff Layton  * -error: if there was an error during lookup. This includes -ENOENT if the
22248033426eSJeff Layton  *         lookup found a negative dentry. The nd->path reference will also be
22258033426eSJeff Layton  *         put in this case.
22268033426eSJeff Layton  *
22278033426eSJeff Layton  * 0:      if we successfully resolved nd->path and found it to not to be a
22288033426eSJeff Layton  *         symlink that needs to be followed. "path" will also be populated.
22298033426eSJeff Layton  *         The nd->path reference will also be put.
22308033426eSJeff Layton  *
22318033426eSJeff Layton  * 1:      if we successfully resolved nd->last and found it to be a symlink
22328033426eSJeff Layton  *         that needs to be followed. "path" will be populated with the path
22338033426eSJeff Layton  *         to the link, and nd->path will *not* be put.
22348033426eSJeff Layton  */
22358033426eSJeff Layton static int
22368033426eSJeff Layton umount_lookup_last(struct nameidata *nd, struct path *path)
22378033426eSJeff Layton {
22388033426eSJeff Layton 	int error = 0;
22398033426eSJeff Layton 	struct dentry *dentry;
22408033426eSJeff Layton 	struct dentry *dir = nd->path.dentry;
22418033426eSJeff Layton 
22428033426eSJeff Layton 	if (unlikely(nd->flags & LOOKUP_RCU)) {
22438033426eSJeff Layton 		WARN_ON_ONCE(1);
22448033426eSJeff Layton 		error = -ECHILD;
22458033426eSJeff Layton 		goto error_check;
22468033426eSJeff Layton 	}
22478033426eSJeff Layton 
22488033426eSJeff Layton 	nd->flags &= ~LOOKUP_PARENT;
22498033426eSJeff Layton 
22508033426eSJeff Layton 	if (unlikely(nd->last_type != LAST_NORM)) {
22518033426eSJeff Layton 		error = handle_dots(nd, nd->last_type);
22528033426eSJeff Layton 		if (!error)
22538033426eSJeff Layton 			dentry = dget(nd->path.dentry);
22548033426eSJeff Layton 		goto error_check;
22558033426eSJeff Layton 	}
22568033426eSJeff Layton 
22578033426eSJeff Layton 	mutex_lock(&dir->d_inode->i_mutex);
22588033426eSJeff Layton 	dentry = d_lookup(dir, &nd->last);
22598033426eSJeff Layton 	if (!dentry) {
22608033426eSJeff Layton 		/*
22618033426eSJeff Layton 		 * No cached dentry. Mounted dentries are pinned in the cache,
22628033426eSJeff Layton 		 * so that means that this dentry is probably a symlink or the
22638033426eSJeff Layton 		 * path doesn't actually point to a mounted dentry.
22648033426eSJeff Layton 		 */
22658033426eSJeff Layton 		dentry = d_alloc(dir, &nd->last);
22668033426eSJeff Layton 		if (!dentry) {
22678033426eSJeff Layton 			error = -ENOMEM;
22688033426eSJeff Layton 		} else {
22698033426eSJeff Layton 			dentry = lookup_real(dir->d_inode, dentry, nd->flags);
22708033426eSJeff Layton 			if (IS_ERR(dentry))
22718033426eSJeff Layton 				error = PTR_ERR(dentry);
22728033426eSJeff Layton 		}
22738033426eSJeff Layton 	}
22748033426eSJeff Layton 	mutex_unlock(&dir->d_inode->i_mutex);
22758033426eSJeff Layton 
22768033426eSJeff Layton error_check:
22778033426eSJeff Layton 	if (!error) {
22788033426eSJeff Layton 		if (!dentry->d_inode) {
22798033426eSJeff Layton 			error = -ENOENT;
22808033426eSJeff Layton 			dput(dentry);
22818033426eSJeff Layton 		} else {
22828033426eSJeff Layton 			path->dentry = dentry;
22838033426eSJeff Layton 			path->mnt = mntget(nd->path.mnt);
22848033426eSJeff Layton 			if (should_follow_link(dentry->d_inode,
22858033426eSJeff Layton 						nd->flags & LOOKUP_FOLLOW))
22868033426eSJeff Layton 				return 1;
22878033426eSJeff Layton 			follow_mount(path);
22888033426eSJeff Layton 		}
22898033426eSJeff Layton 	}
22908033426eSJeff Layton 	terminate_walk(nd);
22918033426eSJeff Layton 	return error;
22928033426eSJeff Layton }
22938033426eSJeff Layton 
22948033426eSJeff Layton /**
22958033426eSJeff Layton  * path_umountat - look up a path to be umounted
22968033426eSJeff Layton  * @dfd:	directory file descriptor to start walk from
22978033426eSJeff Layton  * @name:	full pathname to walk
22988033426eSJeff Layton  * @flags:	lookup flags
22998033426eSJeff Layton  * @nd:		pathwalk nameidata
23008033426eSJeff Layton  *
23018033426eSJeff Layton  * Look up the given name, but don't attempt to revalidate the last component.
23028033426eSJeff Layton  * Returns 0 and "path" will be valid on success; Retuns error otherwise.
23038033426eSJeff Layton  */
23048033426eSJeff Layton static int
23058033426eSJeff Layton path_umountat(int dfd, const char *name, struct path *path, unsigned int flags)
23068033426eSJeff Layton {
23078033426eSJeff Layton 	struct file *base = NULL;
23088033426eSJeff Layton 	struct nameidata nd;
23098033426eSJeff Layton 	int err;
23108033426eSJeff Layton 
23118033426eSJeff Layton 	err = path_init(dfd, name, flags | LOOKUP_PARENT, &nd, &base);
23128033426eSJeff Layton 	if (unlikely(err))
23138033426eSJeff Layton 		return err;
23148033426eSJeff Layton 
23158033426eSJeff Layton 	current->total_link_count = 0;
23168033426eSJeff Layton 	err = link_path_walk(name, &nd);
23178033426eSJeff Layton 	if (err)
23188033426eSJeff Layton 		goto out;
23198033426eSJeff Layton 
23208033426eSJeff Layton 	/* If we're in rcuwalk, drop out of it to handle last component */
23218033426eSJeff Layton 	if (nd.flags & LOOKUP_RCU) {
23228033426eSJeff Layton 		err = unlazy_walk(&nd, NULL);
23238033426eSJeff Layton 		if (err) {
23248033426eSJeff Layton 			terminate_walk(&nd);
23258033426eSJeff Layton 			goto out;
23268033426eSJeff Layton 		}
23278033426eSJeff Layton 	}
23288033426eSJeff Layton 
23298033426eSJeff Layton 	err = umount_lookup_last(&nd, path);
23308033426eSJeff Layton 	while (err > 0) {
23318033426eSJeff Layton 		void *cookie;
23328033426eSJeff Layton 		struct path link = *path;
23338033426eSJeff Layton 		err = may_follow_link(&link, &nd);
23348033426eSJeff Layton 		if (unlikely(err))
23358033426eSJeff Layton 			break;
23368033426eSJeff Layton 		nd.flags |= LOOKUP_PARENT;
23378033426eSJeff Layton 		err = follow_link(&link, &nd, &cookie);
23388033426eSJeff Layton 		if (err)
23398033426eSJeff Layton 			break;
23408033426eSJeff Layton 		err = umount_lookup_last(&nd, path);
23418033426eSJeff Layton 		put_link(&nd, &link, cookie);
23428033426eSJeff Layton 	}
23438033426eSJeff Layton out:
23448033426eSJeff Layton 	if (base)
23458033426eSJeff Layton 		fput(base);
23468033426eSJeff Layton 
23478033426eSJeff Layton 	if (nd.root.mnt && !(nd.flags & LOOKUP_ROOT))
23488033426eSJeff Layton 		path_put(&nd.root);
23498033426eSJeff Layton 
23508033426eSJeff Layton 	return err;
23518033426eSJeff Layton }
23528033426eSJeff Layton 
23538033426eSJeff Layton /**
23548033426eSJeff Layton  * user_path_umountat - lookup a path from userland in order to umount it
23558033426eSJeff Layton  * @dfd:	directory file descriptor
23568033426eSJeff Layton  * @name:	pathname from userland
23578033426eSJeff Layton  * @flags:	lookup flags
23588033426eSJeff Layton  * @path:	pointer to container to hold result
23598033426eSJeff Layton  *
23608033426eSJeff Layton  * A umount is a special case for path walking. We're not actually interested
23618033426eSJeff Layton  * in the inode in this situation, and ESTALE errors can be a problem. We
23628033426eSJeff Layton  * simply want track down the dentry and vfsmount attached at the mountpoint
23638033426eSJeff Layton  * and avoid revalidating the last component.
23648033426eSJeff Layton  *
23658033426eSJeff Layton  * Returns 0 and populates "path" on success.
23668033426eSJeff Layton  */
23678033426eSJeff Layton int
23688033426eSJeff Layton user_path_umountat(int dfd, const char __user *name, unsigned int flags,
23698033426eSJeff Layton 			struct path *path)
23708033426eSJeff Layton {
23718033426eSJeff Layton 	struct filename *s = getname(name);
23728033426eSJeff Layton 	int error;
23738033426eSJeff Layton 
23748033426eSJeff Layton 	if (IS_ERR(s))
23758033426eSJeff Layton 		return PTR_ERR(s);
23768033426eSJeff Layton 
23778033426eSJeff Layton 	error = path_umountat(dfd, s->name, path, flags | LOOKUP_RCU);
23788033426eSJeff Layton 	if (unlikely(error == -ECHILD))
23798033426eSJeff Layton 		error = path_umountat(dfd, s->name, path, flags);
23808033426eSJeff Layton 	if (unlikely(error == -ESTALE))
23818033426eSJeff Layton 		error = path_umountat(dfd, s->name, path, flags | LOOKUP_REVAL);
23828033426eSJeff Layton 
23838033426eSJeff Layton 	if (likely(!error))
23848033426eSJeff Layton 		audit_inode(s, path->dentry, 0);
23858033426eSJeff Layton 
23868033426eSJeff Layton 	putname(s);
23878033426eSJeff Layton 	return error;
23888033426eSJeff Layton }
23898033426eSJeff Layton 
23901da177e4SLinus Torvalds /*
23911da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
23921da177e4SLinus Torvalds  * minimal.
23931da177e4SLinus Torvalds  */
23941da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
23951da177e4SLinus Torvalds {
23968e96e3b7SEric W. Biederman 	kuid_t fsuid = current_fsuid();
2397da9592edSDavid Howells 
23981da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
23991da177e4SLinus Torvalds 		return 0;
24008e96e3b7SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
24011da177e4SLinus Torvalds 		return 0;
24028e96e3b7SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
24031da177e4SLinus Torvalds 		return 0;
24041a48e2acSEric W. Biederman 	return !inode_capable(inode, CAP_FOWNER);
24051da177e4SLinus Torvalds }
24061da177e4SLinus Torvalds 
24071da177e4SLinus Torvalds /*
24081da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
24091da177e4SLinus Torvalds  *  whether the type of victim is right.
24101da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
24111da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
24121da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
24131da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
24141da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
24151da177e4SLinus Torvalds  *	a. be owner of dir, or
24161da177e4SLinus Torvalds  *	b. be owner of victim, or
24171da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
24181da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
24191da177e4SLinus Torvalds  *     links pointing to it.
24201da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
24211da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
24221da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
24231da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
24241da177e4SLinus Torvalds  *     nfs_async_unlink().
24251da177e4SLinus Torvalds  */
2426858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
24271da177e4SLinus Torvalds {
24281da177e4SLinus Torvalds 	int error;
24291da177e4SLinus Torvalds 
24301da177e4SLinus Torvalds 	if (!victim->d_inode)
24311da177e4SLinus Torvalds 		return -ENOENT;
24321da177e4SLinus Torvalds 
24331da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
24344fa6b5ecSJeff Layton 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
24351da177e4SLinus Torvalds 
2436f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
24371da177e4SLinus Torvalds 	if (error)
24381da177e4SLinus Torvalds 		return error;
24391da177e4SLinus Torvalds 	if (IS_APPEND(dir))
24401da177e4SLinus Torvalds 		return -EPERM;
24411da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
2442f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
24431da177e4SLinus Torvalds 		return -EPERM;
24441da177e4SLinus Torvalds 	if (isdir) {
24451da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
24461da177e4SLinus Torvalds 			return -ENOTDIR;
24471da177e4SLinus Torvalds 		if (IS_ROOT(victim))
24481da177e4SLinus Torvalds 			return -EBUSY;
24491da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
24501da177e4SLinus Torvalds 		return -EISDIR;
24511da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
24521da177e4SLinus Torvalds 		return -ENOENT;
24531da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
24541da177e4SLinus Torvalds 		return -EBUSY;
24551da177e4SLinus Torvalds 	return 0;
24561da177e4SLinus Torvalds }
24571da177e4SLinus Torvalds 
24581da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
24591da177e4SLinus Torvalds  *  dir.
24601da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
24611da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
24621da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
24631da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
24641da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
24651da177e4SLinus Torvalds  */
2466a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
24671da177e4SLinus Torvalds {
24681da177e4SLinus Torvalds 	if (child->d_inode)
24691da177e4SLinus Torvalds 		return -EEXIST;
24701da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
24711da177e4SLinus Torvalds 		return -ENOENT;
2472f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
24731da177e4SLinus Torvalds }
24741da177e4SLinus Torvalds 
24751da177e4SLinus Torvalds /*
24761da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
24771da177e4SLinus Torvalds  */
24781da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
24791da177e4SLinus Torvalds {
24801da177e4SLinus Torvalds 	struct dentry *p;
24811da177e4SLinus Torvalds 
24821da177e4SLinus Torvalds 	if (p1 == p2) {
2483f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
24841da177e4SLinus Torvalds 		return NULL;
24851da177e4SLinus Torvalds 	}
24861da177e4SLinus Torvalds 
2487a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
24881da177e4SLinus Torvalds 
2489e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
2490e2761a11SOGAWA Hirofumi 	if (p) {
2491f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2492f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
24931da177e4SLinus Torvalds 		return p;
24941da177e4SLinus Torvalds 	}
24951da177e4SLinus Torvalds 
2496e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
2497e2761a11SOGAWA Hirofumi 	if (p) {
2498f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2499f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
25001da177e4SLinus Torvalds 		return p;
25011da177e4SLinus Torvalds 	}
25021da177e4SLinus Torvalds 
2503f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2504f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
25051da177e4SLinus Torvalds 	return NULL;
25061da177e4SLinus Torvalds }
25071da177e4SLinus Torvalds 
25081da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
25091da177e4SLinus Torvalds {
25101b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
25111da177e4SLinus Torvalds 	if (p1 != p2) {
25121b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
2513a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
25141da177e4SLinus Torvalds 	}
25151da177e4SLinus Torvalds }
25161da177e4SLinus Torvalds 
25174acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2518312b63fbSAl Viro 		bool want_excl)
25191da177e4SLinus Torvalds {
2520a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
25211da177e4SLinus Torvalds 	if (error)
25221da177e4SLinus Torvalds 		return error;
25231da177e4SLinus Torvalds 
2524acfa4380SAl Viro 	if (!dir->i_op->create)
25251da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
25261da177e4SLinus Torvalds 	mode &= S_IALLUGO;
25271da177e4SLinus Torvalds 	mode |= S_IFREG;
25281da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
25291da177e4SLinus Torvalds 	if (error)
25301da177e4SLinus Torvalds 		return error;
2531312b63fbSAl Viro 	error = dir->i_op->create(dir, dentry, mode, want_excl);
2532a74574aaSStephen Smalley 	if (!error)
2533f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
25341da177e4SLinus Torvalds 	return error;
25351da177e4SLinus Torvalds }
25361da177e4SLinus Torvalds 
253773d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
25381da177e4SLinus Torvalds {
25393fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
25401da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
25411da177e4SLinus Torvalds 	int error;
25421da177e4SLinus Torvalds 
2543bcda7652SAl Viro 	/* O_PATH? */
2544bcda7652SAl Viro 	if (!acc_mode)
2545bcda7652SAl Viro 		return 0;
2546bcda7652SAl Viro 
25471da177e4SLinus Torvalds 	if (!inode)
25481da177e4SLinus Torvalds 		return -ENOENT;
25491da177e4SLinus Torvalds 
2550c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2551c8fe8f30SChristoph Hellwig 	case S_IFLNK:
25521da177e4SLinus Torvalds 		return -ELOOP;
2553c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2554c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
25551da177e4SLinus Torvalds 			return -EISDIR;
2556c8fe8f30SChristoph Hellwig 		break;
2557c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2558c8fe8f30SChristoph Hellwig 	case S_IFCHR:
25593fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
25601da177e4SLinus Torvalds 			return -EACCES;
2561c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
2562c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2563c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
25641da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2565c8fe8f30SChristoph Hellwig 		break;
25664a3fd211SDave Hansen 	}
2567b41572e9SDave Hansen 
25683fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2569b41572e9SDave Hansen 	if (error)
2570b41572e9SDave Hansen 		return error;
25716146f0d5SMimi Zohar 
25721da177e4SLinus Torvalds 	/*
25731da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
25741da177e4SLinus Torvalds 	 */
25751da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
25768737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
25777715b521SAl Viro 			return -EPERM;
25781da177e4SLinus Torvalds 		if (flag & O_TRUNC)
25797715b521SAl Viro 			return -EPERM;
25801da177e4SLinus Torvalds 	}
25811da177e4SLinus Torvalds 
25821da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
25832e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
25847715b521SAl Viro 		return -EPERM;
25851da177e4SLinus Torvalds 
2586f3c7691eSJ. Bruce Fields 	return 0;
25877715b521SAl Viro }
25887715b521SAl Viro 
2589e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
25907715b521SAl Viro {
2591e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
25927715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
25937715b521SAl Viro 	int error = get_write_access(inode);
25941da177e4SLinus Torvalds 	if (error)
25957715b521SAl Viro 		return error;
25961da177e4SLinus Torvalds 	/*
25971da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
25981da177e4SLinus Torvalds 	 */
25991da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2600be6d3e56SKentaro Takeda 	if (!error)
2601ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
26021da177e4SLinus Torvalds 	if (!error) {
26037715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2604d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2605e1181ee6SJeff Layton 				    filp);
26061da177e4SLinus Torvalds 	}
26071da177e4SLinus Torvalds 	put_write_access(inode);
2608acd0c935SMimi Zohar 	return error;
26091da177e4SLinus Torvalds }
26101da177e4SLinus Torvalds 
2611d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2612d57999e1SDave Hansen {
26138a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
26148a5e929dSAl Viro 		flag--;
2615d57999e1SDave Hansen 	return flag;
2616d57999e1SDave Hansen }
2617d57999e1SDave Hansen 
2618d18e9008SMiklos Szeredi static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2619d18e9008SMiklos Szeredi {
2620d18e9008SMiklos Szeredi 	int error = security_path_mknod(dir, dentry, mode, 0);
2621d18e9008SMiklos Szeredi 	if (error)
2622d18e9008SMiklos Szeredi 		return error;
2623d18e9008SMiklos Szeredi 
2624d18e9008SMiklos Szeredi 	error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2625d18e9008SMiklos Szeredi 	if (error)
2626d18e9008SMiklos Szeredi 		return error;
2627d18e9008SMiklos Szeredi 
2628d18e9008SMiklos Szeredi 	return security_inode_create(dir->dentry->d_inode, dentry, mode);
2629d18e9008SMiklos Szeredi }
2630d18e9008SMiklos Szeredi 
26311acf0af9SDavid Howells /*
26321acf0af9SDavid Howells  * Attempt to atomically look up, create and open a file from a negative
26331acf0af9SDavid Howells  * dentry.
26341acf0af9SDavid Howells  *
26351acf0af9SDavid Howells  * Returns 0 if successful.  The file will have been created and attached to
26361acf0af9SDavid Howells  * @file by the filesystem calling finish_open().
26371acf0af9SDavid Howells  *
26381acf0af9SDavid Howells  * Returns 1 if the file was looked up only or didn't need creating.  The
26391acf0af9SDavid Howells  * caller will need to perform the open themselves.  @path will have been
26401acf0af9SDavid Howells  * updated to point to the new dentry.  This may be negative.
26411acf0af9SDavid Howells  *
26421acf0af9SDavid Howells  * Returns an error code otherwise.
26431acf0af9SDavid Howells  */
26442675a4ebSAl Viro static int atomic_open(struct nameidata *nd, struct dentry *dentry,
264530d90494SAl Viro 			struct path *path, struct file *file,
2646015c3bbcSMiklos Szeredi 			const struct open_flags *op,
264764894cf8SAl Viro 			bool got_write, bool need_lookup,
264847237687SAl Viro 			int *opened)
2649d18e9008SMiklos Szeredi {
2650d18e9008SMiklos Szeredi 	struct inode *dir =  nd->path.dentry->d_inode;
2651d18e9008SMiklos Szeredi 	unsigned open_flag = open_to_namei_flags(op->open_flag);
2652d18e9008SMiklos Szeredi 	umode_t mode;
2653d18e9008SMiklos Szeredi 	int error;
2654d18e9008SMiklos Szeredi 	int acc_mode;
2655d18e9008SMiklos Szeredi 	int create_error = 0;
2656d18e9008SMiklos Szeredi 	struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2657d18e9008SMiklos Szeredi 
2658d18e9008SMiklos Szeredi 	BUG_ON(dentry->d_inode);
2659d18e9008SMiklos Szeredi 
2660d18e9008SMiklos Szeredi 	/* Don't create child dentry for a dead directory. */
2661d18e9008SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
26622675a4ebSAl Viro 		error = -ENOENT;
2663d18e9008SMiklos Szeredi 		goto out;
2664d18e9008SMiklos Szeredi 	}
2665d18e9008SMiklos Szeredi 
266662b259d8SMiklos Szeredi 	mode = op->mode;
2667d18e9008SMiklos Szeredi 	if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2668d18e9008SMiklos Szeredi 		mode &= ~current_umask();
2669d18e9008SMiklos Szeredi 
2670f8310c59SAl Viro 	if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) {
2671d18e9008SMiklos Szeredi 		open_flag &= ~O_TRUNC;
267247237687SAl Viro 		*opened |= FILE_CREATED;
2673d18e9008SMiklos Szeredi 	}
2674d18e9008SMiklos Szeredi 
2675d18e9008SMiklos Szeredi 	/*
2676d18e9008SMiklos Szeredi 	 * Checking write permission is tricky, bacuse we don't know if we are
2677d18e9008SMiklos Szeredi 	 * going to actually need it: O_CREAT opens should work as long as the
2678d18e9008SMiklos Szeredi 	 * file exists.  But checking existence breaks atomicity.  The trick is
2679d18e9008SMiklos Szeredi 	 * to check access and if not granted clear O_CREAT from the flags.
2680d18e9008SMiklos Szeredi 	 *
2681d18e9008SMiklos Szeredi 	 * Another problem is returing the "right" error value (e.g. for an
2682d18e9008SMiklos Szeredi 	 * O_EXCL open we want to return EEXIST not EROFS).
2683d18e9008SMiklos Szeredi 	 */
268464894cf8SAl Viro 	if (((open_flag & (O_CREAT | O_TRUNC)) ||
268564894cf8SAl Viro 	    (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
268664894cf8SAl Viro 		if (!(open_flag & O_CREAT)) {
2687d18e9008SMiklos Szeredi 			/*
2688d18e9008SMiklos Szeredi 			 * No O_CREATE -> atomicity not a requirement -> fall
2689d18e9008SMiklos Szeredi 			 * back to lookup + open
2690d18e9008SMiklos Szeredi 			 */
2691d18e9008SMiklos Szeredi 			goto no_open;
2692d18e9008SMiklos Szeredi 		} else if (open_flag & (O_EXCL | O_TRUNC)) {
2693d18e9008SMiklos Szeredi 			/* Fall back and fail with the right error */
269464894cf8SAl Viro 			create_error = -EROFS;
2695d18e9008SMiklos Szeredi 			goto no_open;
2696d18e9008SMiklos Szeredi 		} else {
2697d18e9008SMiklos Szeredi 			/* No side effects, safe to clear O_CREAT */
269864894cf8SAl Viro 			create_error = -EROFS;
2699d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2700d18e9008SMiklos Szeredi 		}
2701d18e9008SMiklos Szeredi 	}
2702d18e9008SMiklos Szeredi 
2703d18e9008SMiklos Szeredi 	if (open_flag & O_CREAT) {
270438227f78SMiklos Szeredi 		error = may_o_create(&nd->path, dentry, mode);
2705d18e9008SMiklos Szeredi 		if (error) {
2706d18e9008SMiklos Szeredi 			create_error = error;
2707d18e9008SMiklos Szeredi 			if (open_flag & O_EXCL)
2708d18e9008SMiklos Szeredi 				goto no_open;
2709d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2710d18e9008SMiklos Szeredi 		}
2711d18e9008SMiklos Szeredi 	}
2712d18e9008SMiklos Szeredi 
2713d18e9008SMiklos Szeredi 	if (nd->flags & LOOKUP_DIRECTORY)
2714d18e9008SMiklos Szeredi 		open_flag |= O_DIRECTORY;
2715d18e9008SMiklos Szeredi 
271630d90494SAl Viro 	file->f_path.dentry = DENTRY_NOT_SET;
271730d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
271830d90494SAl Viro 	error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
271947237687SAl Viro 				      opened);
2720d9585277SAl Viro 	if (error < 0) {
2721d9585277SAl Viro 		if (create_error && error == -ENOENT)
2722d9585277SAl Viro 			error = create_error;
2723d18e9008SMiklos Szeredi 		goto out;
2724d18e9008SMiklos Szeredi 	}
2725d18e9008SMiklos Szeredi 
2726d18e9008SMiklos Szeredi 	acc_mode = op->acc_mode;
272747237687SAl Viro 	if (*opened & FILE_CREATED) {
2728d18e9008SMiklos Szeredi 		fsnotify_create(dir, dentry);
2729d18e9008SMiklos Szeredi 		acc_mode = MAY_OPEN;
2730d18e9008SMiklos Szeredi 	}
2731d18e9008SMiklos Szeredi 
2732d9585277SAl Viro 	if (error) {	/* returned 1, that is */
273330d90494SAl Viro 		if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
27342675a4ebSAl Viro 			error = -EIO;
2735d18e9008SMiklos Szeredi 			goto out;
2736d18e9008SMiklos Szeredi 		}
273730d90494SAl Viro 		if (file->f_path.dentry) {
2738d18e9008SMiklos Szeredi 			dput(dentry);
273930d90494SAl Viro 			dentry = file->f_path.dentry;
2740d18e9008SMiklos Szeredi 		}
274162b2ce96SSage Weil 		if (create_error && dentry->d_inode == NULL) {
274262b2ce96SSage Weil 			error = create_error;
274362b2ce96SSage Weil 			goto out;
274462b2ce96SSage Weil 		}
2745d18e9008SMiklos Szeredi 		goto looked_up;
2746d18e9008SMiklos Szeredi 	}
2747d18e9008SMiklos Szeredi 
2748d18e9008SMiklos Szeredi 	/*
2749d18e9008SMiklos Szeredi 	 * We didn't have the inode before the open, so check open permission
2750d18e9008SMiklos Szeredi 	 * here.
2751d18e9008SMiklos Szeredi 	 */
27522675a4ebSAl Viro 	error = may_open(&file->f_path, acc_mode, open_flag);
27532675a4ebSAl Viro 	if (error)
27542675a4ebSAl Viro 		fput(file);
2755d18e9008SMiklos Szeredi 
2756d18e9008SMiklos Szeredi out:
2757d18e9008SMiklos Szeredi 	dput(dentry);
27582675a4ebSAl Viro 	return error;
2759d18e9008SMiklos Szeredi 
2760d18e9008SMiklos Szeredi no_open:
2761d18e9008SMiklos Szeredi 	if (need_lookup) {
276272bd866aSAl Viro 		dentry = lookup_real(dir, dentry, nd->flags);
2763d18e9008SMiklos Szeredi 		if (IS_ERR(dentry))
27642675a4ebSAl Viro 			return PTR_ERR(dentry);
2765d18e9008SMiklos Szeredi 
2766d18e9008SMiklos Szeredi 		if (create_error) {
2767d18e9008SMiklos Szeredi 			int open_flag = op->open_flag;
2768d18e9008SMiklos Szeredi 
27692675a4ebSAl Viro 			error = create_error;
2770d18e9008SMiklos Szeredi 			if ((open_flag & O_EXCL)) {
2771d18e9008SMiklos Szeredi 				if (!dentry->d_inode)
2772d18e9008SMiklos Szeredi 					goto out;
2773d18e9008SMiklos Szeredi 			} else if (!dentry->d_inode) {
2774d18e9008SMiklos Szeredi 				goto out;
2775d18e9008SMiklos Szeredi 			} else if ((open_flag & O_TRUNC) &&
2776d18e9008SMiklos Szeredi 				   S_ISREG(dentry->d_inode->i_mode)) {
2777d18e9008SMiklos Szeredi 				goto out;
2778d18e9008SMiklos Szeredi 			}
2779d18e9008SMiklos Szeredi 			/* will fail later, go on to get the right error */
2780d18e9008SMiklos Szeredi 		}
2781d18e9008SMiklos Szeredi 	}
2782d18e9008SMiklos Szeredi looked_up:
2783d18e9008SMiklos Szeredi 	path->dentry = dentry;
2784d18e9008SMiklos Szeredi 	path->mnt = nd->path.mnt;
27852675a4ebSAl Viro 	return 1;
2786d18e9008SMiklos Szeredi }
2787d18e9008SMiklos Szeredi 
278831e6b01fSNick Piggin /*
27891acf0af9SDavid Howells  * Look up and maybe create and open the last component.
2790d58ffd35SMiklos Szeredi  *
2791d58ffd35SMiklos Szeredi  * Must be called with i_mutex held on parent.
2792d58ffd35SMiklos Szeredi  *
27931acf0af9SDavid Howells  * Returns 0 if the file was successfully atomically created (if necessary) and
27941acf0af9SDavid Howells  * opened.  In this case the file will be returned attached to @file.
27951acf0af9SDavid Howells  *
27961acf0af9SDavid Howells  * Returns 1 if the file was not completely opened at this time, though lookups
27971acf0af9SDavid Howells  * and creations will have been performed and the dentry returned in @path will
27981acf0af9SDavid Howells  * be positive upon return if O_CREAT was specified.  If O_CREAT wasn't
27991acf0af9SDavid Howells  * specified then a negative dentry may be returned.
28001acf0af9SDavid Howells  *
28011acf0af9SDavid Howells  * An error code is returned otherwise.
28021acf0af9SDavid Howells  *
28031acf0af9SDavid Howells  * FILE_CREATE will be set in @*opened if the dentry was created and will be
28041acf0af9SDavid Howells  * cleared otherwise prior to returning.
2805d58ffd35SMiklos Szeredi  */
28062675a4ebSAl Viro static int lookup_open(struct nameidata *nd, struct path *path,
280730d90494SAl Viro 			struct file *file,
2808d58ffd35SMiklos Szeredi 			const struct open_flags *op,
280964894cf8SAl Viro 			bool got_write, int *opened)
2810d58ffd35SMiklos Szeredi {
2811d58ffd35SMiklos Szeredi 	struct dentry *dir = nd->path.dentry;
281254ef4872SMiklos Szeredi 	struct inode *dir_inode = dir->d_inode;
2813d58ffd35SMiklos Szeredi 	struct dentry *dentry;
2814d58ffd35SMiklos Szeredi 	int error;
281554ef4872SMiklos Szeredi 	bool need_lookup;
2816d58ffd35SMiklos Szeredi 
281747237687SAl Viro 	*opened &= ~FILE_CREATED;
2818201f956eSAl Viro 	dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
2819d58ffd35SMiklos Szeredi 	if (IS_ERR(dentry))
28202675a4ebSAl Viro 		return PTR_ERR(dentry);
2821d58ffd35SMiklos Szeredi 
2822d18e9008SMiklos Szeredi 	/* Cached positive dentry: will open in f_op->open */
2823d18e9008SMiklos Szeredi 	if (!need_lookup && dentry->d_inode)
2824d18e9008SMiklos Szeredi 		goto out_no_open;
2825d18e9008SMiklos Szeredi 
2826d18e9008SMiklos Szeredi 	if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
282764894cf8SAl Viro 		return atomic_open(nd, dentry, path, file, op, got_write,
282847237687SAl Viro 				   need_lookup, opened);
2829d18e9008SMiklos Szeredi 	}
2830d18e9008SMiklos Szeredi 
283154ef4872SMiklos Szeredi 	if (need_lookup) {
283254ef4872SMiklos Szeredi 		BUG_ON(dentry->d_inode);
283354ef4872SMiklos Szeredi 
283472bd866aSAl Viro 		dentry = lookup_real(dir_inode, dentry, nd->flags);
283554ef4872SMiklos Szeredi 		if (IS_ERR(dentry))
28362675a4ebSAl Viro 			return PTR_ERR(dentry);
283754ef4872SMiklos Szeredi 	}
283854ef4872SMiklos Szeredi 
2839d58ffd35SMiklos Szeredi 	/* Negative dentry, just create the file */
2840d58ffd35SMiklos Szeredi 	if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2841d58ffd35SMiklos Szeredi 		umode_t mode = op->mode;
2842d58ffd35SMiklos Szeredi 		if (!IS_POSIXACL(dir->d_inode))
2843d58ffd35SMiklos Szeredi 			mode &= ~current_umask();
2844d58ffd35SMiklos Szeredi 		/*
2845d58ffd35SMiklos Szeredi 		 * This write is needed to ensure that a
2846d58ffd35SMiklos Szeredi 		 * rw->ro transition does not occur between
2847d58ffd35SMiklos Szeredi 		 * the time when the file is created and when
2848d58ffd35SMiklos Szeredi 		 * a permanent write count is taken through
2849015c3bbcSMiklos Szeredi 		 * the 'struct file' in finish_open().
2850d58ffd35SMiklos Szeredi 		 */
285164894cf8SAl Viro 		if (!got_write) {
285264894cf8SAl Viro 			error = -EROFS;
2853d58ffd35SMiklos Szeredi 			goto out_dput;
285464894cf8SAl Viro 		}
285547237687SAl Viro 		*opened |= FILE_CREATED;
2856d58ffd35SMiklos Szeredi 		error = security_path_mknod(&nd->path, dentry, mode, 0);
2857d58ffd35SMiklos Szeredi 		if (error)
2858d58ffd35SMiklos Szeredi 			goto out_dput;
2859312b63fbSAl Viro 		error = vfs_create(dir->d_inode, dentry, mode,
2860312b63fbSAl Viro 				   nd->flags & LOOKUP_EXCL);
2861d58ffd35SMiklos Szeredi 		if (error)
2862d58ffd35SMiklos Szeredi 			goto out_dput;
2863d58ffd35SMiklos Szeredi 	}
2864d18e9008SMiklos Szeredi out_no_open:
2865d58ffd35SMiklos Szeredi 	path->dentry = dentry;
2866d58ffd35SMiklos Szeredi 	path->mnt = nd->path.mnt;
28672675a4ebSAl Viro 	return 1;
2868d58ffd35SMiklos Szeredi 
2869d58ffd35SMiklos Szeredi out_dput:
2870d58ffd35SMiklos Szeredi 	dput(dentry);
28712675a4ebSAl Viro 	return error;
2872d58ffd35SMiklos Szeredi }
2873d58ffd35SMiklos Szeredi 
2874d58ffd35SMiklos Szeredi /*
2875fe2d35ffSAl Viro  * Handle the last step of open()
287631e6b01fSNick Piggin  */
28772675a4ebSAl Viro static int do_last(struct nameidata *nd, struct path *path,
287830d90494SAl Viro 		   struct file *file, const struct open_flags *op,
2879669abf4eSJeff Layton 		   int *opened, struct filename *name)
2880fb1cc555SAl Viro {
2881a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
2882ca344a89SAl Viro 	int open_flag = op->open_flag;
288377d660a8SMiklos Szeredi 	bool will_truncate = (open_flag & O_TRUNC) != 0;
288464894cf8SAl Viro 	bool got_write = false;
2885bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2886a1eb3315SMiklos Szeredi 	struct inode *inode;
288777d660a8SMiklos Szeredi 	bool symlink_ok = false;
288816b1c1cdSMiklos Szeredi 	struct path save_parent = { .dentry = NULL, .mnt = NULL };
288916b1c1cdSMiklos Szeredi 	bool retried = false;
289016c2cd71SAl Viro 	int error;
2891fb1cc555SAl Viro 
2892c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2893c3e380b0SAl Viro 	nd->flags |= op->intent;
2894c3e380b0SAl Viro 
2895bc77daa7SAl Viro 	if (nd->last_type != LAST_NORM) {
2896fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2897fe2d35ffSAl Viro 		if (error)
28982675a4ebSAl Viro 			return error;
2899e83db167SMiklos Szeredi 		goto finish_open;
29001f36f774SAl Viro 	}
2901a2c36b45SAl Viro 
2902ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2903fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2904fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2905bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
290677d660a8SMiklos Szeredi 			symlink_ok = true;
2907fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2908e97cdc87SAl Viro 		error = lookup_fast(nd, path, &inode);
290971574865SMiklos Szeredi 		if (likely(!error))
291071574865SMiklos Szeredi 			goto finish_lookup;
291171574865SMiklos Szeredi 
2912ce57dfc1SAl Viro 		if (error < 0)
29132675a4ebSAl Viro 			goto out;
2914a1eb3315SMiklos Szeredi 
291537d7fffcSMiklos Szeredi 		BUG_ON(nd->inode != dir->d_inode);
2916b6183df7SMiklos Szeredi 	} else {
2917fe2d35ffSAl Viro 		/* create side of things */
2918a3fbbde7SAl Viro 		/*
2919b6183df7SMiklos Szeredi 		 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2920b6183df7SMiklos Szeredi 		 * has been cleared when we got to the last component we are
2921b6183df7SMiklos Szeredi 		 * about to look up
2922a3fbbde7SAl Viro 		 */
29239f1fafeeSAl Viro 		error = complete_walk(nd);
29249f1fafeeSAl Viro 		if (error)
29252675a4ebSAl Viro 			return error;
2926fe2d35ffSAl Viro 
292733e2208aSJeff Layton 		audit_inode(name, dir, LOOKUP_PARENT);
292816c2cd71SAl Viro 		error = -EISDIR;
29291f36f774SAl Viro 		/* trailing slashes? */
293031e6b01fSNick Piggin 		if (nd->last.name[nd->last.len])
29312675a4ebSAl Viro 			goto out;
2932b6183df7SMiklos Szeredi 	}
29331f36f774SAl Viro 
293416b1c1cdSMiklos Szeredi retry_lookup:
293564894cf8SAl Viro 	if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
293664894cf8SAl Viro 		error = mnt_want_write(nd->path.mnt);
293764894cf8SAl Viro 		if (!error)
293864894cf8SAl Viro 			got_write = true;
293964894cf8SAl Viro 		/*
294064894cf8SAl Viro 		 * do _not_ fail yet - we might not need that or fail with
294164894cf8SAl Viro 		 * a different error; let lookup_open() decide; we'll be
294264894cf8SAl Viro 		 * dropping this one anyway.
294364894cf8SAl Viro 		 */
294464894cf8SAl Viro 	}
2945a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
294664894cf8SAl Viro 	error = lookup_open(nd, path, file, op, got_write, opened);
2947fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2948fb1cc555SAl Viro 
29492675a4ebSAl Viro 	if (error <= 0) {
29502675a4ebSAl Viro 		if (error)
2951d58ffd35SMiklos Szeredi 			goto out;
29526c0d46c4SAl Viro 
295347237687SAl Viro 		if ((*opened & FILE_CREATED) ||
2954496ad9aaSAl Viro 		    !S_ISREG(file_inode(file)->i_mode))
295577d660a8SMiklos Szeredi 			will_truncate = false;
2956d18e9008SMiklos Szeredi 
2957adb5c247SJeff Layton 		audit_inode(name, file->f_path.dentry, 0);
2958d18e9008SMiklos Szeredi 		goto opened;
2959d18e9008SMiklos Szeredi 	}
2960d18e9008SMiklos Szeredi 
296147237687SAl Viro 	if (*opened & FILE_CREATED) {
29629b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2963ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
296477d660a8SMiklos Szeredi 		will_truncate = false;
2965bcda7652SAl Viro 		acc_mode = MAY_OPEN;
2966d58ffd35SMiklos Szeredi 		path_to_nameidata(path, nd);
2967e83db167SMiklos Szeredi 		goto finish_open_created;
2968fb1cc555SAl Viro 	}
2969fb1cc555SAl Viro 
2970fb1cc555SAl Viro 	/*
29713134f37eSJeff Layton 	 * create/update audit record if it already exists.
2972fb1cc555SAl Viro 	 */
29733134f37eSJeff Layton 	if (path->dentry->d_inode)
2974adb5c247SJeff Layton 		audit_inode(name, path->dentry, 0);
2975fb1cc555SAl Viro 
2976d18e9008SMiklos Szeredi 	/*
2977d18e9008SMiklos Szeredi 	 * If atomic_open() acquired write access it is dropped now due to
2978d18e9008SMiklos Szeredi 	 * possible mount and symlink following (this might be optimized away if
2979d18e9008SMiklos Szeredi 	 * necessary...)
2980d18e9008SMiklos Szeredi 	 */
298164894cf8SAl Viro 	if (got_write) {
2982d18e9008SMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
298364894cf8SAl Viro 		got_write = false;
2984d18e9008SMiklos Szeredi 	}
2985d18e9008SMiklos Szeredi 
2986fb1cc555SAl Viro 	error = -EEXIST;
2987f8310c59SAl Viro 	if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
2988fb1cc555SAl Viro 		goto exit_dput;
2989fb1cc555SAl Viro 
29909875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
29919875cf80SDavid Howells 	if (error < 0)
2992fb1cc555SAl Viro 		goto exit_dput;
2993fb1cc555SAl Viro 
2994a3fbbde7SAl Viro 	if (error)
2995a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
2996a3fbbde7SAl Viro 
2997decf3400SMiklos Szeredi 	BUG_ON(nd->flags & LOOKUP_RCU);
2998decf3400SMiklos Szeredi 	inode = path->dentry->d_inode;
29995f5daac1SMiklos Szeredi finish_lookup:
30005f5daac1SMiklos Szeredi 	/* we _can_ be in RCU mode here */
3001fb1cc555SAl Viro 	error = -ENOENT;
300254c33e7fSMiklos Szeredi 	if (!inode) {
300354c33e7fSMiklos Szeredi 		path_to_nameidata(path, nd);
30042675a4ebSAl Viro 		goto out;
300554c33e7fSMiklos Szeredi 	}
30069e67f361SAl Viro 
3007d45ea867SMiklos Szeredi 	if (should_follow_link(inode, !symlink_ok)) {
3008d45ea867SMiklos Szeredi 		if (nd->flags & LOOKUP_RCU) {
3009d45ea867SMiklos Szeredi 			if (unlikely(unlazy_walk(nd, path->dentry))) {
3010d45ea867SMiklos Szeredi 				error = -ECHILD;
30112675a4ebSAl Viro 				goto out;
3012d45ea867SMiklos Szeredi 			}
3013d45ea867SMiklos Szeredi 		}
3014d45ea867SMiklos Szeredi 		BUG_ON(inode != path->dentry->d_inode);
30152675a4ebSAl Viro 		return 1;
3016d45ea867SMiklos Szeredi 	}
3017fb1cc555SAl Viro 
301816b1c1cdSMiklos Szeredi 	if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
3019fb1cc555SAl Viro 		path_to_nameidata(path, nd);
302016b1c1cdSMiklos Szeredi 	} else {
302116b1c1cdSMiklos Szeredi 		save_parent.dentry = nd->path.dentry;
302216b1c1cdSMiklos Szeredi 		save_parent.mnt = mntget(path->mnt);
302316b1c1cdSMiklos Szeredi 		nd->path.dentry = path->dentry;
302416b1c1cdSMiklos Szeredi 
302516b1c1cdSMiklos Szeredi 	}
3026decf3400SMiklos Szeredi 	nd->inode = inode;
3027a3fbbde7SAl Viro 	/* Why this, you ask?  _Now_ we might have grown LOOKUP_JUMPED... */
3028bc77daa7SAl Viro finish_open:
3029a3fbbde7SAl Viro 	error = complete_walk(nd);
303016b1c1cdSMiklos Szeredi 	if (error) {
303116b1c1cdSMiklos Szeredi 		path_put(&save_parent);
30322675a4ebSAl Viro 		return error;
303316b1c1cdSMiklos Szeredi 	}
3034bc77daa7SAl Viro 	audit_inode(name, nd->path.dentry, 0);
3035fb1cc555SAl Viro 	error = -EISDIR;
3036050ac841SMiklos Szeredi 	if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
30372675a4ebSAl Viro 		goto out;
3038af2f5542SMiklos Szeredi 	error = -ENOTDIR;
303905252901SAl Viro 	if ((nd->flags & LOOKUP_DIRECTORY) && !can_lookup(nd->inode))
30402675a4ebSAl Viro 		goto out;
30416c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
304277d660a8SMiklos Szeredi 		will_truncate = false;
30436c0d46c4SAl Viro 
30440f9d1a10SAl Viro 	if (will_truncate) {
30450f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
30460f9d1a10SAl Viro 		if (error)
30472675a4ebSAl Viro 			goto out;
304864894cf8SAl Viro 		got_write = true;
30490f9d1a10SAl Viro 	}
3050e83db167SMiklos Szeredi finish_open_created:
3051bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
3052ca344a89SAl Viro 	if (error)
30532675a4ebSAl Viro 		goto out;
305430d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
305530d90494SAl Viro 	error = finish_open(file, nd->path.dentry, NULL, opened);
305630d90494SAl Viro 	if (error) {
305730d90494SAl Viro 		if (error == -EOPENSTALE)
3058f60dc3dbSMiklos Szeredi 			goto stale_open;
3059015c3bbcSMiklos Szeredi 		goto out;
3060f60dc3dbSMiklos Szeredi 	}
3061a8277b9bSMiklos Szeredi opened:
30622675a4ebSAl Viro 	error = open_check_o_direct(file);
3063015c3bbcSMiklos Szeredi 	if (error)
3064015c3bbcSMiklos Szeredi 		goto exit_fput;
30652675a4ebSAl Viro 	error = ima_file_check(file, op->acc_mode);
3066aa4caadbSMiklos Szeredi 	if (error)
3067aa4caadbSMiklos Szeredi 		goto exit_fput;
3068aa4caadbSMiklos Szeredi 
30690f9d1a10SAl Viro 	if (will_truncate) {
30702675a4ebSAl Viro 		error = handle_truncate(file);
3071aa4caadbSMiklos Szeredi 		if (error)
3072aa4caadbSMiklos Szeredi 			goto exit_fput;
30730f9d1a10SAl Viro 	}
3074ca344a89SAl Viro out:
307564894cf8SAl Viro 	if (got_write)
30760f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
307716b1c1cdSMiklos Szeredi 	path_put(&save_parent);
3078e276ae67SMiklos Szeredi 	terminate_walk(nd);
30792675a4ebSAl Viro 	return error;
3080fb1cc555SAl Viro 
3081fb1cc555SAl Viro exit_dput:
3082fb1cc555SAl Viro 	path_put_conditional(path, nd);
3083ca344a89SAl Viro 	goto out;
3084015c3bbcSMiklos Szeredi exit_fput:
30852675a4ebSAl Viro 	fput(file);
30862675a4ebSAl Viro 	goto out;
3087015c3bbcSMiklos Szeredi 
3088f60dc3dbSMiklos Szeredi stale_open:
3089f60dc3dbSMiklos Szeredi 	/* If no saved parent or already retried then can't retry */
3090f60dc3dbSMiklos Szeredi 	if (!save_parent.dentry || retried)
3091f60dc3dbSMiklos Szeredi 		goto out;
3092f60dc3dbSMiklos Szeredi 
3093f60dc3dbSMiklos Szeredi 	BUG_ON(save_parent.dentry != dir);
3094f60dc3dbSMiklos Szeredi 	path_put(&nd->path);
3095f60dc3dbSMiklos Szeredi 	nd->path = save_parent;
3096f60dc3dbSMiklos Szeredi 	nd->inode = dir->d_inode;
3097f60dc3dbSMiklos Szeredi 	save_parent.mnt = NULL;
3098f60dc3dbSMiklos Szeredi 	save_parent.dentry = NULL;
309964894cf8SAl Viro 	if (got_write) {
3100f60dc3dbSMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
310164894cf8SAl Viro 		got_write = false;
3102f60dc3dbSMiklos Szeredi 	}
3103f60dc3dbSMiklos Szeredi 	retried = true;
3104f60dc3dbSMiklos Szeredi 	goto retry_lookup;
3105fb1cc555SAl Viro }
3106fb1cc555SAl Viro 
310760545d0dSAl Viro static int do_tmpfile(int dfd, struct filename *pathname,
310860545d0dSAl Viro 		struct nameidata *nd, int flags,
310960545d0dSAl Viro 		const struct open_flags *op,
311060545d0dSAl Viro 		struct file *file, int *opened)
311160545d0dSAl Viro {
311260545d0dSAl Viro 	static const struct qstr name = QSTR_INIT("/", 1);
311360545d0dSAl Viro 	struct dentry *dentry, *child;
311460545d0dSAl Viro 	struct inode *dir;
311560545d0dSAl Viro 	int error = path_lookupat(dfd, pathname->name,
311660545d0dSAl Viro 				  flags | LOOKUP_DIRECTORY, nd);
311760545d0dSAl Viro 	if (unlikely(error))
311860545d0dSAl Viro 		return error;
311960545d0dSAl Viro 	error = mnt_want_write(nd->path.mnt);
312060545d0dSAl Viro 	if (unlikely(error))
312160545d0dSAl Viro 		goto out;
312260545d0dSAl Viro 	/* we want directory to be writable */
312360545d0dSAl Viro 	error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
312460545d0dSAl Viro 	if (error)
312560545d0dSAl Viro 		goto out2;
312660545d0dSAl Viro 	dentry = nd->path.dentry;
312760545d0dSAl Viro 	dir = dentry->d_inode;
312860545d0dSAl Viro 	if (!dir->i_op->tmpfile) {
312960545d0dSAl Viro 		error = -EOPNOTSUPP;
313060545d0dSAl Viro 		goto out2;
313160545d0dSAl Viro 	}
313260545d0dSAl Viro 	child = d_alloc(dentry, &name);
313360545d0dSAl Viro 	if (unlikely(!child)) {
313460545d0dSAl Viro 		error = -ENOMEM;
313560545d0dSAl Viro 		goto out2;
313660545d0dSAl Viro 	}
313760545d0dSAl Viro 	nd->flags &= ~LOOKUP_DIRECTORY;
313860545d0dSAl Viro 	nd->flags |= op->intent;
313960545d0dSAl Viro 	dput(nd->path.dentry);
314060545d0dSAl Viro 	nd->path.dentry = child;
314160545d0dSAl Viro 	error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
314260545d0dSAl Viro 	if (error)
314360545d0dSAl Viro 		goto out2;
314460545d0dSAl Viro 	audit_inode(pathname, nd->path.dentry, 0);
314560545d0dSAl Viro 	error = may_open(&nd->path, op->acc_mode, op->open_flag);
314660545d0dSAl Viro 	if (error)
314760545d0dSAl Viro 		goto out2;
314860545d0dSAl Viro 	file->f_path.mnt = nd->path.mnt;
314960545d0dSAl Viro 	error = finish_open(file, nd->path.dentry, NULL, opened);
315060545d0dSAl Viro 	if (error)
315160545d0dSAl Viro 		goto out2;
315260545d0dSAl Viro 	error = open_check_o_direct(file);
3153f4e0c30cSAl Viro 	if (error) {
315460545d0dSAl Viro 		fput(file);
3155f4e0c30cSAl Viro 	} else if (!(op->open_flag & O_EXCL)) {
3156f4e0c30cSAl Viro 		struct inode *inode = file_inode(file);
3157f4e0c30cSAl Viro 		spin_lock(&inode->i_lock);
3158f4e0c30cSAl Viro 		inode->i_state |= I_LINKABLE;
3159f4e0c30cSAl Viro 		spin_unlock(&inode->i_lock);
3160f4e0c30cSAl Viro 	}
316160545d0dSAl Viro out2:
316260545d0dSAl Viro 	mnt_drop_write(nd->path.mnt);
316360545d0dSAl Viro out:
316460545d0dSAl Viro 	path_put(&nd->path);
316560545d0dSAl Viro 	return error;
316660545d0dSAl Viro }
316760545d0dSAl Viro 
3168669abf4eSJeff Layton static struct file *path_openat(int dfd, struct filename *pathname,
316973d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
31701da177e4SLinus Torvalds {
3171fe2d35ffSAl Viro 	struct file *base = NULL;
317230d90494SAl Viro 	struct file *file;
31739850c056SAl Viro 	struct path path;
317447237687SAl Viro 	int opened = 0;
317513aab428SAl Viro 	int error;
317631e6b01fSNick Piggin 
317730d90494SAl Viro 	file = get_empty_filp();
31781afc99beSAl Viro 	if (IS_ERR(file))
31791afc99beSAl Viro 		return file;
318031e6b01fSNick Piggin 
318130d90494SAl Viro 	file->f_flags = op->open_flag;
318231e6b01fSNick Piggin 
3183bb458c64SAl Viro 	if (unlikely(file->f_flags & __O_TMPFILE)) {
318460545d0dSAl Viro 		error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
318560545d0dSAl Viro 		goto out;
318660545d0dSAl Viro 	}
318760545d0dSAl Viro 
3188669abf4eSJeff Layton 	error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
318931e6b01fSNick Piggin 	if (unlikely(error))
31902675a4ebSAl Viro 		goto out;
319131e6b01fSNick Piggin 
3192fe2d35ffSAl Viro 	current->total_link_count = 0;
3193669abf4eSJeff Layton 	error = link_path_walk(pathname->name, nd);
319431e6b01fSNick Piggin 	if (unlikely(error))
31952675a4ebSAl Viro 		goto out;
31961da177e4SLinus Torvalds 
31972675a4ebSAl Viro 	error = do_last(nd, &path, file, op, &opened, pathname);
31982675a4ebSAl Viro 	while (unlikely(error > 0)) { /* trailing symlink */
31997b9337aaSNick Piggin 		struct path link = path;
3200def4af30SAl Viro 		void *cookie;
3201574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
320273d049a4SAl Viro 			path_put_conditional(&path, nd);
320373d049a4SAl Viro 			path_put(&nd->path);
32042675a4ebSAl Viro 			error = -ELOOP;
320540b39136SAl Viro 			break;
320640b39136SAl Viro 		}
3207800179c9SKees Cook 		error = may_follow_link(&link, nd);
3208800179c9SKees Cook 		if (unlikely(error))
3209800179c9SKees Cook 			break;
321073d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
321173d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
3212574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
3213c3e380b0SAl Viro 		if (unlikely(error))
32142675a4ebSAl Viro 			break;
32152675a4ebSAl Viro 		error = do_last(nd, &path, file, op, &opened, pathname);
3216574197e0SAl Viro 		put_link(nd, &link, cookie);
3217806b681cSAl Viro 	}
321810fa8e62SAl Viro out:
321973d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
322073d049a4SAl Viro 		path_put(&nd->root);
3221fe2d35ffSAl Viro 	if (base)
3222fe2d35ffSAl Viro 		fput(base);
32232675a4ebSAl Viro 	if (!(opened & FILE_OPENED)) {
32242675a4ebSAl Viro 		BUG_ON(!error);
322530d90494SAl Viro 		put_filp(file);
3226015c3bbcSMiklos Szeredi 	}
32272675a4ebSAl Viro 	if (unlikely(error)) {
32282675a4ebSAl Viro 		if (error == -EOPENSTALE) {
32292675a4ebSAl Viro 			if (flags & LOOKUP_RCU)
32302675a4ebSAl Viro 				error = -ECHILD;
32312675a4ebSAl Viro 			else
32322675a4ebSAl Viro 				error = -ESTALE;
32332675a4ebSAl Viro 		}
32342675a4ebSAl Viro 		file = ERR_PTR(error);
32352675a4ebSAl Viro 	}
32362675a4ebSAl Viro 	return file;
3237de459215SKirill Korotaev }
32381da177e4SLinus Torvalds 
3239669abf4eSJeff Layton struct file *do_filp_open(int dfd, struct filename *pathname,
3240f9652e10SAl Viro 		const struct open_flags *op)
324113aab428SAl Viro {
324273d049a4SAl Viro 	struct nameidata nd;
3243f9652e10SAl Viro 	int flags = op->lookup_flags;
324413aab428SAl Viro 	struct file *filp;
324513aab428SAl Viro 
324673d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
324713aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
324873d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
324913aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
325073d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
325113aab428SAl Viro 	return filp;
325213aab428SAl Viro }
325313aab428SAl Viro 
325473d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3255f9652e10SAl Viro 		const char *name, const struct open_flags *op)
325673d049a4SAl Viro {
325773d049a4SAl Viro 	struct nameidata nd;
325873d049a4SAl Viro 	struct file *file;
3259669abf4eSJeff Layton 	struct filename filename = { .name = name };
3260f9652e10SAl Viro 	int flags = op->lookup_flags | LOOKUP_ROOT;
326173d049a4SAl Viro 
326273d049a4SAl Viro 	nd.root.mnt = mnt;
326373d049a4SAl Viro 	nd.root.dentry = dentry;
326473d049a4SAl Viro 
3265bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
326673d049a4SAl Viro 		return ERR_PTR(-ELOOP);
326773d049a4SAl Viro 
3268669abf4eSJeff Layton 	file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
326973d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
3270669abf4eSJeff Layton 		file = path_openat(-1, &filename, &nd, op, flags);
327173d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
3272669abf4eSJeff Layton 		file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
327373d049a4SAl Viro 	return file;
327473d049a4SAl Viro }
327573d049a4SAl Viro 
32761ac12b4bSJeff Layton struct dentry *kern_path_create(int dfd, const char *pathname,
32771ac12b4bSJeff Layton 				struct path *path, unsigned int lookup_flags)
32781da177e4SLinus Torvalds {
3279c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
3280ed75e95dSAl Viro 	struct nameidata nd;
3281c30dabfeSJan Kara 	int err2;
32821ac12b4bSJeff Layton 	int error;
32831ac12b4bSJeff Layton 	bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
32841ac12b4bSJeff Layton 
32851ac12b4bSJeff Layton 	/*
32861ac12b4bSJeff Layton 	 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
32871ac12b4bSJeff Layton 	 * other flags passed in are ignored!
32881ac12b4bSJeff Layton 	 */
32891ac12b4bSJeff Layton 	lookup_flags &= LOOKUP_REVAL;
32901ac12b4bSJeff Layton 
32911ac12b4bSJeff Layton 	error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
3292ed75e95dSAl Viro 	if (error)
3293ed75e95dSAl Viro 		return ERR_PTR(error);
32941da177e4SLinus Torvalds 
3295c663e5d8SChristoph Hellwig 	/*
3296c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
3297c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
3298c663e5d8SChristoph Hellwig 	 */
3299ed75e95dSAl Viro 	if (nd.last_type != LAST_NORM)
3300ed75e95dSAl Viro 		goto out;
3301ed75e95dSAl Viro 	nd.flags &= ~LOOKUP_PARENT;
3302ed75e95dSAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3303c663e5d8SChristoph Hellwig 
3304c30dabfeSJan Kara 	/* don't fail immediately if it's r/o, at least try to report other errors */
3305c30dabfeSJan Kara 	err2 = mnt_want_write(nd.path.mnt);
3306c663e5d8SChristoph Hellwig 	/*
3307c663e5d8SChristoph Hellwig 	 * Do the final lookup.
3308c663e5d8SChristoph Hellwig 	 */
3309ed75e95dSAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3310ed75e95dSAl Viro 	dentry = lookup_hash(&nd);
33111da177e4SLinus Torvalds 	if (IS_ERR(dentry))
3312a8104a9fSAl Viro 		goto unlock;
3313c663e5d8SChristoph Hellwig 
3314a8104a9fSAl Viro 	error = -EEXIST;
3315e9baf6e5SAl Viro 	if (dentry->d_inode)
3316a8104a9fSAl Viro 		goto fail;
3317c663e5d8SChristoph Hellwig 	/*
3318c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
3319c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
3320c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
3321c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
3322c663e5d8SChristoph Hellwig 	 */
3323ed75e95dSAl Viro 	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
3324a8104a9fSAl Viro 		error = -ENOENT;
3325ed75e95dSAl Viro 		goto fail;
3326e9baf6e5SAl Viro 	}
3327c30dabfeSJan Kara 	if (unlikely(err2)) {
3328c30dabfeSJan Kara 		error = err2;
3329a8104a9fSAl Viro 		goto fail;
3330c30dabfeSJan Kara 	}
3331ed75e95dSAl Viro 	*path = nd.path;
3332e9baf6e5SAl Viro 	return dentry;
33331da177e4SLinus Torvalds fail:
3334a8104a9fSAl Viro 	dput(dentry);
3335a8104a9fSAl Viro 	dentry = ERR_PTR(error);
3336a8104a9fSAl Viro unlock:
3337dae6ad8fSAl Viro 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3338c30dabfeSJan Kara 	if (!err2)
3339c30dabfeSJan Kara 		mnt_drop_write(nd.path.mnt);
3340ed75e95dSAl Viro out:
3341dae6ad8fSAl Viro 	path_put(&nd.path);
3342ed75e95dSAl Viro 	return dentry;
3343dae6ad8fSAl Viro }
3344dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
3345dae6ad8fSAl Viro 
3346921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry)
3347921a1650SAl Viro {
3348921a1650SAl Viro 	dput(dentry);
3349921a1650SAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
3350a8104a9fSAl Viro 	mnt_drop_write(path->mnt);
3351921a1650SAl Viro 	path_put(path);
3352921a1650SAl Viro }
3353921a1650SAl Viro EXPORT_SYMBOL(done_path_create);
3354921a1650SAl Viro 
33551ac12b4bSJeff Layton struct dentry *user_path_create(int dfd, const char __user *pathname,
33561ac12b4bSJeff Layton 				struct path *path, unsigned int lookup_flags)
3357dae6ad8fSAl Viro {
335891a27b2aSJeff Layton 	struct filename *tmp = getname(pathname);
3359dae6ad8fSAl Viro 	struct dentry *res;
3360dae6ad8fSAl Viro 	if (IS_ERR(tmp))
3361dae6ad8fSAl Viro 		return ERR_CAST(tmp);
33621ac12b4bSJeff Layton 	res = kern_path_create(dfd, tmp->name, path, lookup_flags);
3363dae6ad8fSAl Viro 	putname(tmp);
3364dae6ad8fSAl Viro 	return res;
3365dae6ad8fSAl Viro }
3366dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
3367dae6ad8fSAl Viro 
33681a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
33691da177e4SLinus Torvalds {
3370a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
33711da177e4SLinus Torvalds 
33721da177e4SLinus Torvalds 	if (error)
33731da177e4SLinus Torvalds 		return error;
33741da177e4SLinus Torvalds 
3375975d6b39SEric W. Biederman 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
33761da177e4SLinus Torvalds 		return -EPERM;
33771da177e4SLinus Torvalds 
3378acfa4380SAl Viro 	if (!dir->i_op->mknod)
33791da177e4SLinus Torvalds 		return -EPERM;
33801da177e4SLinus Torvalds 
338108ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
338208ce5f16SSerge E. Hallyn 	if (error)
338308ce5f16SSerge E. Hallyn 		return error;
338408ce5f16SSerge E. Hallyn 
33851da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
33861da177e4SLinus Torvalds 	if (error)
33871da177e4SLinus Torvalds 		return error;
33881da177e4SLinus Torvalds 
33891da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
3390a74574aaSStephen Smalley 	if (!error)
3391f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
33921da177e4SLinus Torvalds 	return error;
33931da177e4SLinus Torvalds }
33941da177e4SLinus Torvalds 
3395f69aac00SAl Viro static int may_mknod(umode_t mode)
3396463c3197SDave Hansen {
3397463c3197SDave Hansen 	switch (mode & S_IFMT) {
3398463c3197SDave Hansen 	case S_IFREG:
3399463c3197SDave Hansen 	case S_IFCHR:
3400463c3197SDave Hansen 	case S_IFBLK:
3401463c3197SDave Hansen 	case S_IFIFO:
3402463c3197SDave Hansen 	case S_IFSOCK:
3403463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
3404463c3197SDave Hansen 		return 0;
3405463c3197SDave Hansen 	case S_IFDIR:
3406463c3197SDave Hansen 		return -EPERM;
3407463c3197SDave Hansen 	default:
3408463c3197SDave Hansen 		return -EINVAL;
3409463c3197SDave Hansen 	}
3410463c3197SDave Hansen }
3411463c3197SDave Hansen 
34128208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
34132e4d0924SHeiko Carstens 		unsigned, dev)
34141da177e4SLinus Torvalds {
34151da177e4SLinus Torvalds 	struct dentry *dentry;
3416dae6ad8fSAl Viro 	struct path path;
3417dae6ad8fSAl Viro 	int error;
3418972567f1SJeff Layton 	unsigned int lookup_flags = 0;
34191da177e4SLinus Torvalds 
34208e4bfca1SAl Viro 	error = may_mknod(mode);
34218e4bfca1SAl Viro 	if (error)
34228e4bfca1SAl Viro 		return error;
3423972567f1SJeff Layton retry:
3424972567f1SJeff Layton 	dentry = user_path_create(dfd, filename, &path, lookup_flags);
3425dae6ad8fSAl Viro 	if (IS_ERR(dentry))
3426dae6ad8fSAl Viro 		return PTR_ERR(dentry);
34272ad94ae6SAl Viro 
3428dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3429ce3b0f8dSAl Viro 		mode &= ~current_umask();
3430dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
3431be6d3e56SKentaro Takeda 	if (error)
3432a8104a9fSAl Viro 		goto out;
34331da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
34341da177e4SLinus Torvalds 		case 0: case S_IFREG:
3435312b63fbSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,true);
34361da177e4SLinus Torvalds 			break;
34371da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
3438dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
34391da177e4SLinus Torvalds 					new_decode_dev(dev));
34401da177e4SLinus Torvalds 			break;
34411da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
3442dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
34431da177e4SLinus Torvalds 			break;
34441da177e4SLinus Torvalds 	}
3445a8104a9fSAl Viro out:
3446921a1650SAl Viro 	done_path_create(&path, dentry);
3447972567f1SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3448972567f1SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3449972567f1SJeff Layton 		goto retry;
3450972567f1SJeff Layton 	}
34511da177e4SLinus Torvalds 	return error;
34521da177e4SLinus Torvalds }
34531da177e4SLinus Torvalds 
34548208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
34555590ff0dSUlrich Drepper {
34565590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
34575590ff0dSUlrich Drepper }
34585590ff0dSUlrich Drepper 
345918bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
34601da177e4SLinus Torvalds {
3461a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
34628de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
34631da177e4SLinus Torvalds 
34641da177e4SLinus Torvalds 	if (error)
34651da177e4SLinus Torvalds 		return error;
34661da177e4SLinus Torvalds 
3467acfa4380SAl Viro 	if (!dir->i_op->mkdir)
34681da177e4SLinus Torvalds 		return -EPERM;
34691da177e4SLinus Torvalds 
34701da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
34711da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
34721da177e4SLinus Torvalds 	if (error)
34731da177e4SLinus Torvalds 		return error;
34741da177e4SLinus Torvalds 
34758de52778SAl Viro 	if (max_links && dir->i_nlink >= max_links)
34768de52778SAl Viro 		return -EMLINK;
34778de52778SAl Viro 
34781da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
3479a74574aaSStephen Smalley 	if (!error)
3480f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
34811da177e4SLinus Torvalds 	return error;
34821da177e4SLinus Torvalds }
34831da177e4SLinus Torvalds 
3484a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
34851da177e4SLinus Torvalds {
34866902d925SDave Hansen 	struct dentry *dentry;
3487dae6ad8fSAl Viro 	struct path path;
3488dae6ad8fSAl Viro 	int error;
3489b76d8b82SJeff Layton 	unsigned int lookup_flags = LOOKUP_DIRECTORY;
34901da177e4SLinus Torvalds 
3491b76d8b82SJeff Layton retry:
3492b76d8b82SJeff Layton 	dentry = user_path_create(dfd, pathname, &path, lookup_flags);
34936902d925SDave Hansen 	if (IS_ERR(dentry))
3494dae6ad8fSAl Viro 		return PTR_ERR(dentry);
34956902d925SDave Hansen 
3496dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3497ce3b0f8dSAl Viro 		mode &= ~current_umask();
3498dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
3499a8104a9fSAl Viro 	if (!error)
3500dae6ad8fSAl Viro 		error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3501921a1650SAl Viro 	done_path_create(&path, dentry);
3502b76d8b82SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3503b76d8b82SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3504b76d8b82SJeff Layton 		goto retry;
3505b76d8b82SJeff Layton 	}
35061da177e4SLinus Torvalds 	return error;
35071da177e4SLinus Torvalds }
35081da177e4SLinus Torvalds 
3509a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
35105590ff0dSUlrich Drepper {
35115590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
35125590ff0dSUlrich Drepper }
35135590ff0dSUlrich Drepper 
35141da177e4SLinus Torvalds /*
3515a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
3516c0d02594SJ. Bruce Fields  * should have a usage count of 1 if we're the only user of this
3517a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
3518a71905f0SSage Weil  * then we drop the dentry now.
35191da177e4SLinus Torvalds  *
35201da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
35211da177e4SLinus Torvalds  * do a
35221da177e4SLinus Torvalds  *
35231da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
35241da177e4SLinus Torvalds  *		return -EBUSY;
35251da177e4SLinus Torvalds  *
35261da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
35271da177e4SLinus Torvalds  * that is still in use by something else..
35281da177e4SLinus Torvalds  */
35291da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
35301da177e4SLinus Torvalds {
35311da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
35321da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
353398474236SWaiman Long 	if (dentry->d_lockref.count == 1)
35341da177e4SLinus Torvalds 		__d_drop(dentry);
35351da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
35361da177e4SLinus Torvalds }
35371da177e4SLinus Torvalds 
35381da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
35391da177e4SLinus Torvalds {
35401da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
35411da177e4SLinus Torvalds 
35421da177e4SLinus Torvalds 	if (error)
35431da177e4SLinus Torvalds 		return error;
35441da177e4SLinus Torvalds 
3545acfa4380SAl Viro 	if (!dir->i_op->rmdir)
35461da177e4SLinus Torvalds 		return -EPERM;
35471da177e4SLinus Torvalds 
35481d2ef590SAl Viro 	dget(dentry);
35491b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
3550912dbc15SSage Weil 
35511da177e4SLinus Torvalds 	error = -EBUSY;
3552912dbc15SSage Weil 	if (d_mountpoint(dentry))
3553912dbc15SSage Weil 		goto out;
3554912dbc15SSage Weil 
35551da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
3556912dbc15SSage Weil 	if (error)
3557912dbc15SSage Weil 		goto out;
3558912dbc15SSage Weil 
35593cebde24SSage Weil 	shrink_dcache_parent(dentry);
35601da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
3561912dbc15SSage Weil 	if (error)
3562912dbc15SSage Weil 		goto out;
3563912dbc15SSage Weil 
35641da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
3565d83c49f3SAl Viro 	dont_mount(dentry);
35661da177e4SLinus Torvalds 
3567912dbc15SSage Weil out:
3568912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
35691d2ef590SAl Viro 	dput(dentry);
3570912dbc15SSage Weil 	if (!error)
3571912dbc15SSage Weil 		d_delete(dentry);
35721da177e4SLinus Torvalds 	return error;
35731da177e4SLinus Torvalds }
35741da177e4SLinus Torvalds 
35755590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
35761da177e4SLinus Torvalds {
35771da177e4SLinus Torvalds 	int error = 0;
357891a27b2aSJeff Layton 	struct filename *name;
35791da177e4SLinus Torvalds 	struct dentry *dentry;
35801da177e4SLinus Torvalds 	struct nameidata nd;
3581c6ee9206SJeff Layton 	unsigned int lookup_flags = 0;
3582c6ee9206SJeff Layton retry:
3583c6ee9206SJeff Layton 	name = user_path_parent(dfd, pathname, &nd, lookup_flags);
358491a27b2aSJeff Layton 	if (IS_ERR(name))
358591a27b2aSJeff Layton 		return PTR_ERR(name);
35861da177e4SLinus Torvalds 
35871da177e4SLinus Torvalds 	switch(nd.last_type) {
35881da177e4SLinus Torvalds 	case LAST_DOTDOT:
35891da177e4SLinus Torvalds 		error = -ENOTEMPTY;
35901da177e4SLinus Torvalds 		goto exit1;
35911da177e4SLinus Torvalds 	case LAST_DOT:
35921da177e4SLinus Torvalds 		error = -EINVAL;
35931da177e4SLinus Torvalds 		goto exit1;
35941da177e4SLinus Torvalds 	case LAST_ROOT:
35951da177e4SLinus Torvalds 		error = -EBUSY;
35961da177e4SLinus Torvalds 		goto exit1;
35971da177e4SLinus Torvalds 	}
35980612d9fbSOGAWA Hirofumi 
35990612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
3600c30dabfeSJan Kara 	error = mnt_want_write(nd.path.mnt);
3601c30dabfeSJan Kara 	if (error)
3602c30dabfeSJan Kara 		goto exit1;
36030612d9fbSOGAWA Hirofumi 
36044ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
360549705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
36061da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
36076902d925SDave Hansen 	if (IS_ERR(dentry))
36086902d925SDave Hansen 		goto exit2;
3609e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
3610e6bc45d6STheodore Ts'o 		error = -ENOENT;
3611e6bc45d6STheodore Ts'o 		goto exit3;
3612e6bc45d6STheodore Ts'o 	}
3613be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
3614be6d3e56SKentaro Takeda 	if (error)
3615c30dabfeSJan Kara 		goto exit3;
36164ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
36170622753bSDave Hansen exit3:
36181da177e4SLinus Torvalds 	dput(dentry);
36196902d925SDave Hansen exit2:
36204ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3621c30dabfeSJan Kara 	mnt_drop_write(nd.path.mnt);
36221da177e4SLinus Torvalds exit1:
36231d957f9bSJan Blunck 	path_put(&nd.path);
36241da177e4SLinus Torvalds 	putname(name);
3625c6ee9206SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3626c6ee9206SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3627c6ee9206SJeff Layton 		goto retry;
3628c6ee9206SJeff Layton 	}
36291da177e4SLinus Torvalds 	return error;
36301da177e4SLinus Torvalds }
36311da177e4SLinus Torvalds 
36323cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
36335590ff0dSUlrich Drepper {
36345590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
36355590ff0dSUlrich Drepper }
36365590ff0dSUlrich Drepper 
36371da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
36381da177e4SLinus Torvalds {
36391da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
36401da177e4SLinus Torvalds 
36411da177e4SLinus Torvalds 	if (error)
36421da177e4SLinus Torvalds 		return error;
36431da177e4SLinus Torvalds 
3644acfa4380SAl Viro 	if (!dir->i_op->unlink)
36451da177e4SLinus Torvalds 		return -EPERM;
36461da177e4SLinus Torvalds 
36471b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
36481da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
36491da177e4SLinus Torvalds 		error = -EBUSY;
36501da177e4SLinus Torvalds 	else {
36511da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
3652bec1052eSAl Viro 		if (!error) {
36531da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
3654bec1052eSAl Viro 			if (!error)
3655d83c49f3SAl Viro 				dont_mount(dentry);
3656bec1052eSAl Viro 		}
36571da177e4SLinus Torvalds 	}
36581b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
36591da177e4SLinus Torvalds 
36601da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
36611da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
3662ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
36631da177e4SLinus Torvalds 		d_delete(dentry);
36641da177e4SLinus Torvalds 	}
36650eeca283SRobert Love 
36661da177e4SLinus Torvalds 	return error;
36671da177e4SLinus Torvalds }
36681da177e4SLinus Torvalds 
36691da177e4SLinus Torvalds /*
36701da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
36711b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
36721da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
36731da177e4SLinus Torvalds  * while waiting on the I/O.
36741da177e4SLinus Torvalds  */
36755590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
36761da177e4SLinus Torvalds {
36772ad94ae6SAl Viro 	int error;
367891a27b2aSJeff Layton 	struct filename *name;
36791da177e4SLinus Torvalds 	struct dentry *dentry;
36801da177e4SLinus Torvalds 	struct nameidata nd;
36811da177e4SLinus Torvalds 	struct inode *inode = NULL;
36825d18f813SJeff Layton 	unsigned int lookup_flags = 0;
36835d18f813SJeff Layton retry:
36845d18f813SJeff Layton 	name = user_path_parent(dfd, pathname, &nd, lookup_flags);
368591a27b2aSJeff Layton 	if (IS_ERR(name))
368691a27b2aSJeff Layton 		return PTR_ERR(name);
36872ad94ae6SAl Viro 
36881da177e4SLinus Torvalds 	error = -EISDIR;
36891da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
36901da177e4SLinus Torvalds 		goto exit1;
36910612d9fbSOGAWA Hirofumi 
36920612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
3693c30dabfeSJan Kara 	error = mnt_want_write(nd.path.mnt);
3694c30dabfeSJan Kara 	if (error)
3695c30dabfeSJan Kara 		goto exit1;
36960612d9fbSOGAWA Hirofumi 
36974ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
369849705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
36991da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
37001da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
37011da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
370250338b88STörök Edwin 		if (nd.last.name[nd.last.len])
370350338b88STörök Edwin 			goto slashes;
37041da177e4SLinus Torvalds 		inode = dentry->d_inode;
370550338b88STörök Edwin 		if (!inode)
3706e6bc45d6STheodore Ts'o 			goto slashes;
37077de9c6eeSAl Viro 		ihold(inode);
3708be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
3709be6d3e56SKentaro Takeda 		if (error)
3710c30dabfeSJan Kara 			goto exit2;
37114ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
37121da177e4SLinus Torvalds exit2:
37131da177e4SLinus Torvalds 		dput(dentry);
37141da177e4SLinus Torvalds 	}
37154ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
37161da177e4SLinus Torvalds 	if (inode)
37171da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
3718c30dabfeSJan Kara 	mnt_drop_write(nd.path.mnt);
37191da177e4SLinus Torvalds exit1:
37201d957f9bSJan Blunck 	path_put(&nd.path);
37211da177e4SLinus Torvalds 	putname(name);
37225d18f813SJeff Layton 	if (retry_estale(error, lookup_flags)) {
37235d18f813SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
37245d18f813SJeff Layton 		inode = NULL;
37255d18f813SJeff Layton 		goto retry;
37265d18f813SJeff Layton 	}
37271da177e4SLinus Torvalds 	return error;
37281da177e4SLinus Torvalds 
37291da177e4SLinus Torvalds slashes:
37301da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
37311da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
37321da177e4SLinus Torvalds 	goto exit2;
37331da177e4SLinus Torvalds }
37341da177e4SLinus Torvalds 
37352e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
37365590ff0dSUlrich Drepper {
37375590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
37385590ff0dSUlrich Drepper 		return -EINVAL;
37395590ff0dSUlrich Drepper 
37405590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
37415590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
37425590ff0dSUlrich Drepper 
37435590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
37445590ff0dSUlrich Drepper }
37455590ff0dSUlrich Drepper 
37463480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
37475590ff0dSUlrich Drepper {
37485590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
37495590ff0dSUlrich Drepper }
37505590ff0dSUlrich Drepper 
3751db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
37521da177e4SLinus Torvalds {
3753a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
37541da177e4SLinus Torvalds 
37551da177e4SLinus Torvalds 	if (error)
37561da177e4SLinus Torvalds 		return error;
37571da177e4SLinus Torvalds 
3758acfa4380SAl Viro 	if (!dir->i_op->symlink)
37591da177e4SLinus Torvalds 		return -EPERM;
37601da177e4SLinus Torvalds 
37611da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
37621da177e4SLinus Torvalds 	if (error)
37631da177e4SLinus Torvalds 		return error;
37641da177e4SLinus Torvalds 
37651da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
3766a74574aaSStephen Smalley 	if (!error)
3767f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
37681da177e4SLinus Torvalds 	return error;
37691da177e4SLinus Torvalds }
37701da177e4SLinus Torvalds 
37712e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
37722e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
37731da177e4SLinus Torvalds {
37742ad94ae6SAl Viro 	int error;
377591a27b2aSJeff Layton 	struct filename *from;
37766902d925SDave Hansen 	struct dentry *dentry;
3777dae6ad8fSAl Viro 	struct path path;
3778f46d3567SJeff Layton 	unsigned int lookup_flags = 0;
37791da177e4SLinus Torvalds 
37801da177e4SLinus Torvalds 	from = getname(oldname);
37811da177e4SLinus Torvalds 	if (IS_ERR(from))
37821da177e4SLinus Torvalds 		return PTR_ERR(from);
3783f46d3567SJeff Layton retry:
3784f46d3567SJeff Layton 	dentry = user_path_create(newdfd, newname, &path, lookup_flags);
37851da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
37866902d925SDave Hansen 	if (IS_ERR(dentry))
3787dae6ad8fSAl Viro 		goto out_putname;
37886902d925SDave Hansen 
378991a27b2aSJeff Layton 	error = security_path_symlink(&path, dentry, from->name);
3790a8104a9fSAl Viro 	if (!error)
379191a27b2aSJeff Layton 		error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
3792921a1650SAl Viro 	done_path_create(&path, dentry);
3793f46d3567SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3794f46d3567SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3795f46d3567SJeff Layton 		goto retry;
3796f46d3567SJeff Layton 	}
37976902d925SDave Hansen out_putname:
37981da177e4SLinus Torvalds 	putname(from);
37991da177e4SLinus Torvalds 	return error;
38001da177e4SLinus Torvalds }
38011da177e4SLinus Torvalds 
38023480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
38035590ff0dSUlrich Drepper {
38045590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
38055590ff0dSUlrich Drepper }
38065590ff0dSUlrich Drepper 
38071da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
38081da177e4SLinus Torvalds {
38091da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
38108de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
38111da177e4SLinus Torvalds 	int error;
38121da177e4SLinus Torvalds 
38131da177e4SLinus Torvalds 	if (!inode)
38141da177e4SLinus Torvalds 		return -ENOENT;
38151da177e4SLinus Torvalds 
3816a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
38171da177e4SLinus Torvalds 	if (error)
38181da177e4SLinus Torvalds 		return error;
38191da177e4SLinus Torvalds 
38201da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
38211da177e4SLinus Torvalds 		return -EXDEV;
38221da177e4SLinus Torvalds 
38231da177e4SLinus Torvalds 	/*
38241da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
38251da177e4SLinus Torvalds 	 */
38261da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
38271da177e4SLinus Torvalds 		return -EPERM;
3828acfa4380SAl Viro 	if (!dir->i_op->link)
38291da177e4SLinus Torvalds 		return -EPERM;
38307e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
38311da177e4SLinus Torvalds 		return -EPERM;
38321da177e4SLinus Torvalds 
38331da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
38341da177e4SLinus Torvalds 	if (error)
38351da177e4SLinus Torvalds 		return error;
38361da177e4SLinus Torvalds 
38377e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
3838aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
3839f4e0c30cSAl Viro 	if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
3840aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
38418de52778SAl Viro 	else if (max_links && inode->i_nlink >= max_links)
38428de52778SAl Viro 		error = -EMLINK;
3843aae8a97dSAneesh Kumar K.V 	else
38441da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
3845f4e0c30cSAl Viro 
3846f4e0c30cSAl Viro 	if (!error && (inode->i_state & I_LINKABLE)) {
3847f4e0c30cSAl Viro 		spin_lock(&inode->i_lock);
3848f4e0c30cSAl Viro 		inode->i_state &= ~I_LINKABLE;
3849f4e0c30cSAl Viro 		spin_unlock(&inode->i_lock);
3850f4e0c30cSAl Viro 	}
38517e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
3852e31e14ecSStephen Smalley 	if (!error)
38537e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
38541da177e4SLinus Torvalds 	return error;
38551da177e4SLinus Torvalds }
38561da177e4SLinus Torvalds 
38571da177e4SLinus Torvalds /*
38581da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
38591da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
38601da177e4SLinus Torvalds  * newname.  --KAB
38611da177e4SLinus Torvalds  *
38621da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
38631da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
38641da177e4SLinus Torvalds  * and other special files.  --ADM
38651da177e4SLinus Torvalds  */
38662e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
38672e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
38681da177e4SLinus Torvalds {
38691da177e4SLinus Torvalds 	struct dentry *new_dentry;
3870dae6ad8fSAl Viro 	struct path old_path, new_path;
387111a7b371SAneesh Kumar K.V 	int how = 0;
38721da177e4SLinus Torvalds 	int error;
38731da177e4SLinus Torvalds 
387411a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3875c04030e1SUlrich Drepper 		return -EINVAL;
387611a7b371SAneesh Kumar K.V 	/*
3877f0cc6ffbSLinus Torvalds 	 * To use null names we require CAP_DAC_READ_SEARCH
3878f0cc6ffbSLinus Torvalds 	 * This ensures that not everyone will be able to create
3879f0cc6ffbSLinus Torvalds 	 * handlink using the passed filedescriptor.
388011a7b371SAneesh Kumar K.V 	 */
3881f0cc6ffbSLinus Torvalds 	if (flags & AT_EMPTY_PATH) {
3882f0cc6ffbSLinus Torvalds 		if (!capable(CAP_DAC_READ_SEARCH))
3883f0cc6ffbSLinus Torvalds 			return -ENOENT;
388411a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
3885f0cc6ffbSLinus Torvalds 	}
3886c04030e1SUlrich Drepper 
388711a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
388811a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
3889442e31caSJeff Layton retry:
389011a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
38911da177e4SLinus Torvalds 	if (error)
38922ad94ae6SAl Viro 		return error;
38932ad94ae6SAl Viro 
3894442e31caSJeff Layton 	new_dentry = user_path_create(newdfd, newname, &new_path,
3895442e31caSJeff Layton 					(how & LOOKUP_REVAL));
38961da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
38976902d925SDave Hansen 	if (IS_ERR(new_dentry))
3898dae6ad8fSAl Viro 		goto out;
3899dae6ad8fSAl Viro 
3900dae6ad8fSAl Viro 	error = -EXDEV;
3901dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
3902dae6ad8fSAl Viro 		goto out_dput;
3903800179c9SKees Cook 	error = may_linkat(&old_path);
3904800179c9SKees Cook 	if (unlikely(error))
3905800179c9SKees Cook 		goto out_dput;
3906dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
3907be6d3e56SKentaro Takeda 	if (error)
3908a8104a9fSAl Viro 		goto out_dput;
3909dae6ad8fSAl Viro 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
391075c3f29dSDave Hansen out_dput:
3911921a1650SAl Viro 	done_path_create(&new_path, new_dentry);
3912442e31caSJeff Layton 	if (retry_estale(error, how)) {
3913442e31caSJeff Layton 		how |= LOOKUP_REVAL;
3914442e31caSJeff Layton 		goto retry;
3915442e31caSJeff Layton 	}
39161da177e4SLinus Torvalds out:
39172d8f3038SAl Viro 	path_put(&old_path);
39181da177e4SLinus Torvalds 
39191da177e4SLinus Torvalds 	return error;
39201da177e4SLinus Torvalds }
39211da177e4SLinus Torvalds 
39223480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
39235590ff0dSUlrich Drepper {
3924c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
39255590ff0dSUlrich Drepper }
39265590ff0dSUlrich Drepper 
39271da177e4SLinus Torvalds /*
39281da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
39291da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
39301da177e4SLinus Torvalds  * Problems:
39311da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
39321da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
39331da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
3934a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
39351da177e4SLinus Torvalds  *	   story.
39361da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
39371b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
39381da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
39391da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
3940a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
39411da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
39421da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
39431da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
3944a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
39451da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
39461da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
39471da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
3948e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
39491b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
39501da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3951c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
39521da177e4SLinus Torvalds  *	   locking].
39531da177e4SLinus Torvalds  */
395475c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
39551da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
39561da177e4SLinus Torvalds {
39571da177e4SLinus Torvalds 	int error = 0;
39589055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
39598de52778SAl Viro 	unsigned max_links = new_dir->i_sb->s_max_links;
39601da177e4SLinus Torvalds 
39611da177e4SLinus Torvalds 	/*
39621da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
39631da177e4SLinus Torvalds 	 * we'll need to flip '..'.
39641da177e4SLinus Torvalds 	 */
39651da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3966f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
39671da177e4SLinus Torvalds 		if (error)
39681da177e4SLinus Torvalds 			return error;
39691da177e4SLinus Torvalds 	}
39701da177e4SLinus Torvalds 
39711da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
39721da177e4SLinus Torvalds 	if (error)
39731da177e4SLinus Torvalds 		return error;
39741da177e4SLinus Torvalds 
39751d2ef590SAl Viro 	dget(new_dentry);
3976d83c49f3SAl Viro 	if (target)
39771b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
39789055cba7SSage Weil 
39791da177e4SLinus Torvalds 	error = -EBUSY;
39809055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
39819055cba7SSage Weil 		goto out;
39829055cba7SSage Weil 
39838de52778SAl Viro 	error = -EMLINK;
39848de52778SAl Viro 	if (max_links && !target && new_dir != old_dir &&
39858de52778SAl Viro 	    new_dir->i_nlink >= max_links)
39868de52778SAl Viro 		goto out;
39878de52778SAl Viro 
39883cebde24SSage Weil 	if (target)
39893cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
39901da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
39919055cba7SSage Weil 	if (error)
39929055cba7SSage Weil 		goto out;
39939055cba7SSage Weil 
39941da177e4SLinus Torvalds 	if (target) {
39951da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
3996d83c49f3SAl Viro 		dont_mount(new_dentry);
3997d83c49f3SAl Viro 	}
39989055cba7SSage Weil out:
39999055cba7SSage Weil 	if (target)
40001b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
40011d2ef590SAl Viro 	dput(new_dentry);
4002e31e14ecSStephen Smalley 	if (!error)
4003349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
40041da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
40051da177e4SLinus Torvalds 	return error;
40061da177e4SLinus Torvalds }
40071da177e4SLinus Torvalds 
400875c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
40091da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
40101da177e4SLinus Torvalds {
401151892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
40121da177e4SLinus Torvalds 	int error;
40131da177e4SLinus Torvalds 
40141da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
40151da177e4SLinus Torvalds 	if (error)
40161da177e4SLinus Torvalds 		return error;
40171da177e4SLinus Torvalds 
40181da177e4SLinus Torvalds 	dget(new_dentry);
40191da177e4SLinus Torvalds 	if (target)
40201b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
402151892bbbSSage Weil 
40221da177e4SLinus Torvalds 	error = -EBUSY;
402351892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
402451892bbbSSage Weil 		goto out;
402551892bbbSSage Weil 
40261da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
402751892bbbSSage Weil 	if (error)
402851892bbbSSage Weil 		goto out;
402951892bbbSSage Weil 
4030bec1052eSAl Viro 	if (target)
4031d83c49f3SAl Viro 		dont_mount(new_dentry);
4032349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
40331da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
403451892bbbSSage Weil out:
40351da177e4SLinus Torvalds 	if (target)
40361b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
40371da177e4SLinus Torvalds 	dput(new_dentry);
40381da177e4SLinus Torvalds 	return error;
40391da177e4SLinus Torvalds }
40401da177e4SLinus Torvalds 
40411da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
40421da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
40431da177e4SLinus Torvalds {
40441da177e4SLinus Torvalds 	int error;
40451da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
404659b0df21SEric Paris 	const unsigned char *old_name;
40471da177e4SLinus Torvalds 
40481da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
40491da177e4SLinus Torvalds  		return 0;
40501da177e4SLinus Torvalds 
40511da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
40521da177e4SLinus Torvalds 	if (error)
40531da177e4SLinus Torvalds 		return error;
40541da177e4SLinus Torvalds 
40551da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
4056a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
40571da177e4SLinus Torvalds 	else
40581da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
40591da177e4SLinus Torvalds 	if (error)
40601da177e4SLinus Torvalds 		return error;
40611da177e4SLinus Torvalds 
4062acfa4380SAl Viro 	if (!old_dir->i_op->rename)
40631da177e4SLinus Torvalds 		return -EPERM;
40641da177e4SLinus Torvalds 
40650eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
40660eeca283SRobert Love 
40671da177e4SLinus Torvalds 	if (is_dir)
40681da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
40691da177e4SLinus Torvalds 	else
40701da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
4071123df294SAl Viro 	if (!error)
4072123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
40735a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
40740eeca283SRobert Love 	fsnotify_oldname_free(old_name);
40750eeca283SRobert Love 
40761da177e4SLinus Torvalds 	return error;
40771da177e4SLinus Torvalds }
40781da177e4SLinus Torvalds 
40792e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
40802e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
40811da177e4SLinus Torvalds {
40821da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
40831da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
40841da177e4SLinus Torvalds 	struct dentry *trap;
40851da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
408691a27b2aSJeff Layton 	struct filename *from;
408791a27b2aSJeff Layton 	struct filename *to;
4088c6a94284SJeff Layton 	unsigned int lookup_flags = 0;
4089c6a94284SJeff Layton 	bool should_retry = false;
40902ad94ae6SAl Viro 	int error;
4091c6a94284SJeff Layton retry:
4092c6a94284SJeff Layton 	from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
409391a27b2aSJeff Layton 	if (IS_ERR(from)) {
409491a27b2aSJeff Layton 		error = PTR_ERR(from);
40951da177e4SLinus Torvalds 		goto exit;
409691a27b2aSJeff Layton 	}
40971da177e4SLinus Torvalds 
4098c6a94284SJeff Layton 	to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
409991a27b2aSJeff Layton 	if (IS_ERR(to)) {
410091a27b2aSJeff Layton 		error = PTR_ERR(to);
41011da177e4SLinus Torvalds 		goto exit1;
410291a27b2aSJeff Layton 	}
41031da177e4SLinus Torvalds 
41041da177e4SLinus Torvalds 	error = -EXDEV;
41054ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
41061da177e4SLinus Torvalds 		goto exit2;
41071da177e4SLinus Torvalds 
41084ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
41091da177e4SLinus Torvalds 	error = -EBUSY;
41101da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
41111da177e4SLinus Torvalds 		goto exit2;
41121da177e4SLinus Torvalds 
41134ac91378SJan Blunck 	new_dir = newnd.path.dentry;
41141da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
41151da177e4SLinus Torvalds 		goto exit2;
41161da177e4SLinus Torvalds 
4117c30dabfeSJan Kara 	error = mnt_want_write(oldnd.path.mnt);
4118c30dabfeSJan Kara 	if (error)
4119c30dabfeSJan Kara 		goto exit2;
4120c30dabfeSJan Kara 
41210612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
41220612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
41234e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
41240612d9fbSOGAWA Hirofumi 
41251da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
41261da177e4SLinus Torvalds 
412749705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
41281da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
41291da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
41301da177e4SLinus Torvalds 		goto exit3;
41311da177e4SLinus Torvalds 	/* source must exist */
41321da177e4SLinus Torvalds 	error = -ENOENT;
41331da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
41341da177e4SLinus Torvalds 		goto exit4;
41351da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
41361da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
41371da177e4SLinus Torvalds 		error = -ENOTDIR;
41381da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
41391da177e4SLinus Torvalds 			goto exit4;
41401da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
41411da177e4SLinus Torvalds 			goto exit4;
41421da177e4SLinus Torvalds 	}
41431da177e4SLinus Torvalds 	/* source should not be ancestor of target */
41441da177e4SLinus Torvalds 	error = -EINVAL;
41451da177e4SLinus Torvalds 	if (old_dentry == trap)
41461da177e4SLinus Torvalds 		goto exit4;
414749705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
41481da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
41491da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
41501da177e4SLinus Torvalds 		goto exit4;
41511da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
41521da177e4SLinus Torvalds 	error = -ENOTEMPTY;
41531da177e4SLinus Torvalds 	if (new_dentry == trap)
41541da177e4SLinus Torvalds 		goto exit5;
41551da177e4SLinus Torvalds 
4156be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
4157be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
4158be6d3e56SKentaro Takeda 	if (error)
4159c30dabfeSJan Kara 		goto exit5;
41601da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
41611da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
41621da177e4SLinus Torvalds exit5:
41631da177e4SLinus Torvalds 	dput(new_dentry);
41641da177e4SLinus Torvalds exit4:
41651da177e4SLinus Torvalds 	dput(old_dentry);
41661da177e4SLinus Torvalds exit3:
41671da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
4168c30dabfeSJan Kara 	mnt_drop_write(oldnd.path.mnt);
41691da177e4SLinus Torvalds exit2:
4170c6a94284SJeff Layton 	if (retry_estale(error, lookup_flags))
4171c6a94284SJeff Layton 		should_retry = true;
41721d957f9bSJan Blunck 	path_put(&newnd.path);
41732ad94ae6SAl Viro 	putname(to);
41741da177e4SLinus Torvalds exit1:
41751d957f9bSJan Blunck 	path_put(&oldnd.path);
41761da177e4SLinus Torvalds 	putname(from);
4177c6a94284SJeff Layton 	if (should_retry) {
4178c6a94284SJeff Layton 		should_retry = false;
4179c6a94284SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
4180c6a94284SJeff Layton 		goto retry;
4181c6a94284SJeff Layton 	}
41822ad94ae6SAl Viro exit:
41831da177e4SLinus Torvalds 	return error;
41841da177e4SLinus Torvalds }
41851da177e4SLinus Torvalds 
4186a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
41875590ff0dSUlrich Drepper {
41885590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
41895590ff0dSUlrich Drepper }
41905590ff0dSUlrich Drepper 
41911da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
41921da177e4SLinus Torvalds {
41931da177e4SLinus Torvalds 	int len;
41941da177e4SLinus Torvalds 
41951da177e4SLinus Torvalds 	len = PTR_ERR(link);
41961da177e4SLinus Torvalds 	if (IS_ERR(link))
41971da177e4SLinus Torvalds 		goto out;
41981da177e4SLinus Torvalds 
41991da177e4SLinus Torvalds 	len = strlen(link);
42001da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
42011da177e4SLinus Torvalds 		len = buflen;
42021da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
42031da177e4SLinus Torvalds 		len = -EFAULT;
42041da177e4SLinus Torvalds out:
42051da177e4SLinus Torvalds 	return len;
42061da177e4SLinus Torvalds }
42071da177e4SLinus Torvalds 
42081da177e4SLinus Torvalds /*
42091da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
42101da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
42111da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
42121da177e4SLinus Torvalds  */
42131da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
42141da177e4SLinus Torvalds {
42151da177e4SLinus Torvalds 	struct nameidata nd;
4216cc314eefSLinus Torvalds 	void *cookie;
4217694a1764SMarcin Slusarz 	int res;
4218cc314eefSLinus Torvalds 
42191da177e4SLinus Torvalds 	nd.depth = 0;
4220cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
4221694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
4222694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
4223694a1764SMarcin Slusarz 
4224694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
42251da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
4226cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4227694a1764SMarcin Slusarz 	return res;
42281da177e4SLinus Torvalds }
42291da177e4SLinus Torvalds 
42301da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
42311da177e4SLinus Torvalds {
42321da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
42331da177e4SLinus Torvalds }
42341da177e4SLinus Torvalds 
42351da177e4SLinus Torvalds /* get the link contents into pagecache */
42361da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
42371da177e4SLinus Torvalds {
4238ebd09abbSDuane Griffin 	char *kaddr;
42391da177e4SLinus Torvalds 	struct page *page;
42401da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
4241090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
42421da177e4SLinus Torvalds 	if (IS_ERR(page))
42436fe6900eSNick Piggin 		return (char*)page;
42441da177e4SLinus Torvalds 	*ppage = page;
4245ebd09abbSDuane Griffin 	kaddr = kmap(page);
4246ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4247ebd09abbSDuane Griffin 	return kaddr;
42481da177e4SLinus Torvalds }
42491da177e4SLinus Torvalds 
42501da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
42511da177e4SLinus Torvalds {
42521da177e4SLinus Torvalds 	struct page *page = NULL;
42531da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
42541da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
42551da177e4SLinus Torvalds 	if (page) {
42561da177e4SLinus Torvalds 		kunmap(page);
42571da177e4SLinus Torvalds 		page_cache_release(page);
42581da177e4SLinus Torvalds 	}
42591da177e4SLinus Torvalds 	return res;
42601da177e4SLinus Torvalds }
42611da177e4SLinus Torvalds 
4262cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
42631da177e4SLinus Torvalds {
4264cc314eefSLinus Torvalds 	struct page *page = NULL;
42651da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
4266cc314eefSLinus Torvalds 	return page;
42671da177e4SLinus Torvalds }
42681da177e4SLinus Torvalds 
4269cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
42701da177e4SLinus Torvalds {
4271cc314eefSLinus Torvalds 	struct page *page = cookie;
4272cc314eefSLinus Torvalds 
4273cc314eefSLinus Torvalds 	if (page) {
42741da177e4SLinus Torvalds 		kunmap(page);
42751da177e4SLinus Torvalds 		page_cache_release(page);
42761da177e4SLinus Torvalds 	}
42771da177e4SLinus Torvalds }
42781da177e4SLinus Torvalds 
427954566b2cSNick Piggin /*
428054566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
428154566b2cSNick Piggin  */
428254566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
42831da177e4SLinus Torvalds {
42841da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
42850adb25d2SKirill Korotaev 	struct page *page;
4286afddba49SNick Piggin 	void *fsdata;
4287beb497abSDmitriy Monakhov 	int err;
42881da177e4SLinus Torvalds 	char *kaddr;
428954566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
429054566b2cSNick Piggin 	if (nofs)
429154566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
42921da177e4SLinus Torvalds 
42937e53cac4SNeilBrown retry:
4294afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
429554566b2cSNick Piggin 				flags, &page, &fsdata);
42961da177e4SLinus Torvalds 	if (err)
4297afddba49SNick Piggin 		goto fail;
4298afddba49SNick Piggin 
4299e8e3c3d6SCong Wang 	kaddr = kmap_atomic(page);
43001da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
4301e8e3c3d6SCong Wang 	kunmap_atomic(kaddr);
4302afddba49SNick Piggin 
4303afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4304afddba49SNick Piggin 							page, fsdata);
43051da177e4SLinus Torvalds 	if (err < 0)
43061da177e4SLinus Torvalds 		goto fail;
4307afddba49SNick Piggin 	if (err < len-1)
4308afddba49SNick Piggin 		goto retry;
4309afddba49SNick Piggin 
43101da177e4SLinus Torvalds 	mark_inode_dirty(inode);
43111da177e4SLinus Torvalds 	return 0;
43121da177e4SLinus Torvalds fail:
43131da177e4SLinus Torvalds 	return err;
43141da177e4SLinus Torvalds }
43151da177e4SLinus Torvalds 
43160adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
43170adb25d2SKirill Korotaev {
43180adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
431954566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
43200adb25d2SKirill Korotaev }
43210adb25d2SKirill Korotaev 
432292e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
43231da177e4SLinus Torvalds 	.readlink	= generic_readlink,
43241da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
43251da177e4SLinus Torvalds 	.put_link	= page_put_link,
43261da177e4SLinus Torvalds };
43271da177e4SLinus Torvalds 
43282d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
4329cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
43301da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
43311da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
4332f6d2ac5cSAl Viro EXPORT_SYMBOL(get_write_access); /* nfsd */
43331da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
43341da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
43351da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
43361da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
43371da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
43380adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
43391da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
43401da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
4341d1811465SAl Viro EXPORT_SYMBOL(kern_path);
434216f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
4343f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
43441da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
43451da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
43461da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
43471da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
43481da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
43491da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
43501da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
43511da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
43521da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
43531da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
43541da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
43551da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
43561da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
43571da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
4358