xref: /openbmc/linux/fs/namei.c (revision bc77daa7)
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;
5115b6ca027SAl Viro 	int want_root = 0;
51231e6b01fSNick Piggin 
51331e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
5145b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
5155b6ca027SAl Viro 		want_root = 1;
51631e6b01fSNick Piggin 		spin_lock(&fs->lock);
51731e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
51831e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
51931e6b01fSNick Piggin 			goto err_root;
52031e6b01fSNick Piggin 	}
52131e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
52219660af7SAl Viro 	if (!dentry) {
52319660af7SAl Viro 		if (!__d_rcu_to_refcount(parent, nd->seq))
52419660af7SAl Viro 			goto err_parent;
52519660af7SAl Viro 		BUG_ON(nd->inode != parent->d_inode);
52619660af7SAl Viro 	} else {
52794c0d4ecSAl Viro 		if (dentry->d_parent != parent)
52894c0d4ecSAl Viro 			goto err_parent;
52931e6b01fSNick Piggin 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
53031e6b01fSNick Piggin 		if (!__d_rcu_to_refcount(dentry, nd->seq))
53119660af7SAl Viro 			goto err_child;
53231e6b01fSNick Piggin 		/*
53319660af7SAl Viro 		 * If the sequence check on the child dentry passed, then
53419660af7SAl Viro 		 * the child has not been removed from its parent. This
53519660af7SAl Viro 		 * means the parent dentry must be valid and able to take
53619660af7SAl Viro 		 * a reference at this point.
53731e6b01fSNick Piggin 		 */
53831e6b01fSNick Piggin 		BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
53931e6b01fSNick Piggin 		BUG_ON(!parent->d_count);
54031e6b01fSNick Piggin 		parent->d_count++;
54131e6b01fSNick Piggin 		spin_unlock(&dentry->d_lock);
54219660af7SAl Viro 	}
54331e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
5445b6ca027SAl Viro 	if (want_root) {
54531e6b01fSNick Piggin 		path_get(&nd->root);
54631e6b01fSNick Piggin 		spin_unlock(&fs->lock);
54731e6b01fSNick Piggin 	}
54831e6b01fSNick Piggin 	mntget(nd->path.mnt);
54931e6b01fSNick Piggin 
55032a7991bSAl Viro 	unlock_rcu_walk();
55131e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
55231e6b01fSNick Piggin 	return 0;
55319660af7SAl Viro 
55419660af7SAl Viro err_child:
55531e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
55619660af7SAl Viro err_parent:
55731e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
55831e6b01fSNick Piggin err_root:
5595b6ca027SAl Viro 	if (want_root)
56031e6b01fSNick Piggin 		spin_unlock(&fs->lock);
56131e6b01fSNick Piggin 	return -ECHILD;
56231e6b01fSNick Piggin }
56331e6b01fSNick Piggin 
5644ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
56534286d66SNick Piggin {
5664ce16ef3SAl Viro 	return dentry->d_op->d_revalidate(dentry, flags);
56734286d66SNick Piggin }
56834286d66SNick Piggin 
5699f1fafeeSAl Viro /**
5709f1fafeeSAl Viro  * complete_walk - successful completion of path walk
5719f1fafeeSAl Viro  * @nd:  pointer nameidata
57239159de2SJeff Layton  *
5739f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
5749f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
5759f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
5769f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
5779f1fafeeSAl Viro  * need to drop nd->path.
57839159de2SJeff Layton  */
5799f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
58039159de2SJeff Layton {
58116c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
58239159de2SJeff Layton 	int status;
58339159de2SJeff Layton 
5849f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
5859f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
5869f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
5879f1fafeeSAl Viro 			nd->root.mnt = NULL;
5889f1fafeeSAl Viro 		spin_lock(&dentry->d_lock);
5899f1fafeeSAl Viro 		if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
5909f1fafeeSAl Viro 			spin_unlock(&dentry->d_lock);
59132a7991bSAl Viro 			unlock_rcu_walk();
5929f1fafeeSAl Viro 			return -ECHILD;
5939f1fafeeSAl Viro 		}
5949f1fafeeSAl Viro 		BUG_ON(nd->inode != dentry->d_inode);
5959f1fafeeSAl Viro 		spin_unlock(&dentry->d_lock);
5969f1fafeeSAl Viro 		mntget(nd->path.mnt);
59732a7991bSAl Viro 		unlock_rcu_walk();
5989f1fafeeSAl Viro 	}
5999f1fafeeSAl Viro 
60016c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
60139159de2SJeff Layton 		return 0;
60239159de2SJeff Layton 
603ecf3d1f1SJeff Layton 	if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
60416c2cd71SAl Viro 		return 0;
60516c2cd71SAl Viro 
606ecf3d1f1SJeff Layton 	status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
60739159de2SJeff Layton 	if (status > 0)
60839159de2SJeff Layton 		return 0;
60939159de2SJeff Layton 
61016c2cd71SAl Viro 	if (!status)
61139159de2SJeff Layton 		status = -ESTALE;
61216c2cd71SAl Viro 
6139f1fafeeSAl Viro 	path_put(&nd->path);
61439159de2SJeff Layton 	return status;
61539159de2SJeff Layton }
61639159de2SJeff Layton 
6172a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
6182a737871SAl Viro {
619f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
620f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
6212a737871SAl Viro }
6222a737871SAl Viro 
6236de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
6246de88d72SAl Viro 
62531e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
62631e6b01fSNick Piggin {
62731e6b01fSNick Piggin 	if (!nd->root.mnt) {
62831e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
629c28cc364SNick Piggin 		unsigned seq;
630c28cc364SNick Piggin 
631c28cc364SNick Piggin 		do {
632c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
63331e6b01fSNick Piggin 			nd->root = fs->root;
634c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
635c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
63631e6b01fSNick Piggin 	}
63731e6b01fSNick Piggin }
63831e6b01fSNick Piggin 
639f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
6401da177e4SLinus Torvalds {
64131e6b01fSNick Piggin 	int ret;
64231e6b01fSNick Piggin 
6431da177e4SLinus Torvalds 	if (IS_ERR(link))
6441da177e4SLinus Torvalds 		goto fail;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	if (*link == '/') {
6472a737871SAl Viro 		set_root(nd);
6481d957f9bSJan Blunck 		path_put(&nd->path);
6492a737871SAl Viro 		nd->path = nd->root;
6502a737871SAl Viro 		path_get(&nd->root);
65116c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
6521da177e4SLinus Torvalds 	}
65331e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
654b4091d5fSChristoph Hellwig 
65531e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
65631e6b01fSNick Piggin 	return ret;
6571da177e4SLinus Torvalds fail:
6581d957f9bSJan Blunck 	path_put(&nd->path);
6591da177e4SLinus Torvalds 	return PTR_ERR(link);
6601da177e4SLinus Torvalds }
6611da177e4SLinus Torvalds 
6621d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
663051d3812SIan Kent {
664051d3812SIan Kent 	dput(path->dentry);
6654ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
666051d3812SIan Kent 		mntput(path->mnt);
667051d3812SIan Kent }
668051d3812SIan Kent 
6697b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
6707b9337aaSNick Piggin 					struct nameidata *nd)
671051d3812SIan Kent {
67231e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
6734ac91378SJan Blunck 		dput(nd->path.dentry);
67431e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
6754ac91378SJan Blunck 			mntput(nd->path.mnt);
6769a229683SHuang Shijie 	}
67731e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
6784ac91378SJan Blunck 	nd->path.dentry = path->dentry;
679051d3812SIan Kent }
680051d3812SIan Kent 
681b5fb63c1SChristoph Hellwig /*
682b5fb63c1SChristoph Hellwig  * Helper to directly jump to a known parsed path from ->follow_link,
683b5fb63c1SChristoph Hellwig  * caller must have taken a reference to path beforehand.
684b5fb63c1SChristoph Hellwig  */
685b5fb63c1SChristoph Hellwig void nd_jump_link(struct nameidata *nd, struct path *path)
686b5fb63c1SChristoph Hellwig {
687b5fb63c1SChristoph Hellwig 	path_put(&nd->path);
688b5fb63c1SChristoph Hellwig 
689b5fb63c1SChristoph Hellwig 	nd->path = *path;
690b5fb63c1SChristoph Hellwig 	nd->inode = nd->path.dentry->d_inode;
691b5fb63c1SChristoph Hellwig 	nd->flags |= LOOKUP_JUMPED;
692b5fb63c1SChristoph Hellwig }
693b5fb63c1SChristoph Hellwig 
694574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
695574197e0SAl Viro {
696574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
6976d7b5aaeSAl Viro 	if (inode->i_op->put_link)
698574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
699574197e0SAl Viro 	path_put(link);
700574197e0SAl Viro }
701574197e0SAl Viro 
702561ec64aSLinus Torvalds int sysctl_protected_symlinks __read_mostly = 0;
703561ec64aSLinus Torvalds int sysctl_protected_hardlinks __read_mostly = 0;
704800179c9SKees Cook 
705800179c9SKees Cook /**
706800179c9SKees Cook  * may_follow_link - Check symlink following for unsafe situations
707800179c9SKees Cook  * @link: The path of the symlink
70855852635SRandy Dunlap  * @nd: nameidata pathwalk data
709800179c9SKees Cook  *
710800179c9SKees Cook  * In the case of the sysctl_protected_symlinks sysctl being enabled,
711800179c9SKees Cook  * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
712800179c9SKees Cook  * in a sticky world-writable directory. This is to protect privileged
713800179c9SKees Cook  * processes from failing races against path names that may change out
714800179c9SKees Cook  * from under them by way of other users creating malicious symlinks.
715800179c9SKees Cook  * It will permit symlinks to be followed only when outside a sticky
716800179c9SKees Cook  * world-writable directory, or when the uid of the symlink and follower
717800179c9SKees Cook  * match, or when the directory owner matches the symlink's owner.
718800179c9SKees Cook  *
719800179c9SKees Cook  * Returns 0 if following the symlink is allowed, -ve on error.
720800179c9SKees Cook  */
721800179c9SKees Cook static inline int may_follow_link(struct path *link, struct nameidata *nd)
722800179c9SKees Cook {
723800179c9SKees Cook 	const struct inode *inode;
724800179c9SKees Cook 	const struct inode *parent;
725800179c9SKees Cook 
726800179c9SKees Cook 	if (!sysctl_protected_symlinks)
727800179c9SKees Cook 		return 0;
728800179c9SKees Cook 
729800179c9SKees Cook 	/* Allowed if owner and follower match. */
730800179c9SKees Cook 	inode = link->dentry->d_inode;
73181abe27bSEric W. Biederman 	if (uid_eq(current_cred()->fsuid, inode->i_uid))
732800179c9SKees Cook 		return 0;
733800179c9SKees Cook 
734800179c9SKees Cook 	/* Allowed if parent directory not sticky and world-writable. */
735800179c9SKees Cook 	parent = nd->path.dentry->d_inode;
736800179c9SKees Cook 	if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
737800179c9SKees Cook 		return 0;
738800179c9SKees Cook 
739800179c9SKees Cook 	/* Allowed if parent directory and link owner match. */
74081abe27bSEric W. Biederman 	if (uid_eq(parent->i_uid, inode->i_uid))
741800179c9SKees Cook 		return 0;
742800179c9SKees Cook 
743ffd8d101SSasha Levin 	audit_log_link_denied("follow_link", link);
744800179c9SKees Cook 	path_put_conditional(link, nd);
745800179c9SKees Cook 	path_put(&nd->path);
746800179c9SKees Cook 	return -EACCES;
747800179c9SKees Cook }
748800179c9SKees Cook 
749800179c9SKees Cook /**
750800179c9SKees Cook  * safe_hardlink_source - Check for safe hardlink conditions
751800179c9SKees Cook  * @inode: the source inode to hardlink from
752800179c9SKees Cook  *
753800179c9SKees Cook  * Return false if at least one of the following conditions:
754800179c9SKees Cook  *    - inode is not a regular file
755800179c9SKees Cook  *    - inode is setuid
756800179c9SKees Cook  *    - inode is setgid and group-exec
757800179c9SKees Cook  *    - access failure for read and write
758800179c9SKees Cook  *
759800179c9SKees Cook  * Otherwise returns true.
760800179c9SKees Cook  */
761800179c9SKees Cook static bool safe_hardlink_source(struct inode *inode)
762800179c9SKees Cook {
763800179c9SKees Cook 	umode_t mode = inode->i_mode;
764800179c9SKees Cook 
765800179c9SKees Cook 	/* Special files should not get pinned to the filesystem. */
766800179c9SKees Cook 	if (!S_ISREG(mode))
767800179c9SKees Cook 		return false;
768800179c9SKees Cook 
769800179c9SKees Cook 	/* Setuid files should not get pinned to the filesystem. */
770800179c9SKees Cook 	if (mode & S_ISUID)
771800179c9SKees Cook 		return false;
772800179c9SKees Cook 
773800179c9SKees Cook 	/* Executable setgid files should not get pinned to the filesystem. */
774800179c9SKees Cook 	if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
775800179c9SKees Cook 		return false;
776800179c9SKees Cook 
777800179c9SKees Cook 	/* Hardlinking to unreadable or unwritable sources is dangerous. */
778800179c9SKees Cook 	if (inode_permission(inode, MAY_READ | MAY_WRITE))
779800179c9SKees Cook 		return false;
780800179c9SKees Cook 
781800179c9SKees Cook 	return true;
782800179c9SKees Cook }
783800179c9SKees Cook 
784800179c9SKees Cook /**
785800179c9SKees Cook  * may_linkat - Check permissions for creating a hardlink
786800179c9SKees Cook  * @link: the source to hardlink from
787800179c9SKees Cook  *
788800179c9SKees Cook  * Block hardlink when all of:
789800179c9SKees Cook  *  - sysctl_protected_hardlinks enabled
790800179c9SKees Cook  *  - fsuid does not match inode
791800179c9SKees Cook  *  - hardlink source is unsafe (see safe_hardlink_source() above)
792800179c9SKees Cook  *  - not CAP_FOWNER
793800179c9SKees Cook  *
794800179c9SKees Cook  * Returns 0 if successful, -ve on error.
795800179c9SKees Cook  */
796800179c9SKees Cook static int may_linkat(struct path *link)
797800179c9SKees Cook {
798800179c9SKees Cook 	const struct cred *cred;
799800179c9SKees Cook 	struct inode *inode;
800800179c9SKees Cook 
801800179c9SKees Cook 	if (!sysctl_protected_hardlinks)
802800179c9SKees Cook 		return 0;
803800179c9SKees Cook 
804800179c9SKees Cook 	cred = current_cred();
805800179c9SKees Cook 	inode = link->dentry->d_inode;
806800179c9SKees Cook 
807800179c9SKees Cook 	/* Source inode owner (or CAP_FOWNER) can hardlink all they like,
808800179c9SKees Cook 	 * otherwise, it must be a safe source.
809800179c9SKees Cook 	 */
81081abe27bSEric W. Biederman 	if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
811800179c9SKees Cook 	    capable(CAP_FOWNER))
812800179c9SKees Cook 		return 0;
813800179c9SKees Cook 
814a51d9eaaSKees Cook 	audit_log_link_denied("linkat", link);
815800179c9SKees Cook 	return -EPERM;
816800179c9SKees Cook }
817800179c9SKees Cook 
818def4af30SAl Viro static __always_inline int
819574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
8201da177e4SLinus Torvalds {
8217b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
8226d7b5aaeSAl Viro 	int error;
8236d7b5aaeSAl Viro 	char *s;
8241da177e4SLinus Torvalds 
825844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
826844a3917SAl Viro 
8270e794589SAl Viro 	if (link->mnt == nd->path.mnt)
8280e794589SAl Viro 		mntget(link->mnt);
8290e794589SAl Viro 
8306d7b5aaeSAl Viro 	error = -ELOOP;
8316d7b5aaeSAl Viro 	if (unlikely(current->total_link_count >= 40))
8326d7b5aaeSAl Viro 		goto out_put_nd_path;
8336d7b5aaeSAl Viro 
834574197e0SAl Viro 	cond_resched();
835574197e0SAl Viro 	current->total_link_count++;
836574197e0SAl Viro 
83768ac1234SAl Viro 	touch_atime(link);
8381da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
839cd4e91d3SAl Viro 
84036f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
8416d7b5aaeSAl Viro 	if (error)
8426d7b5aaeSAl Viro 		goto out_put_nd_path;
84336f3b4f6SAl Viro 
84486acdca1SAl Viro 	nd->last_type = LAST_BIND;
845def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
846def4af30SAl Viro 	error = PTR_ERR(*p);
8476d7b5aaeSAl Viro 	if (IS_ERR(*p))
848408ef013SChristoph Hellwig 		goto out_put_nd_path;
8496d7b5aaeSAl Viro 
850cc314eefSLinus Torvalds 	error = 0;
8516d7b5aaeSAl Viro 	s = nd_get_link(nd);
8526d7b5aaeSAl Viro 	if (s) {
8531da177e4SLinus Torvalds 		error = __vfs_follow_link(nd, s);
8546d7b5aaeSAl Viro 		if (unlikely(error))
8556d7b5aaeSAl Viro 			put_link(nd, link, *p);
856b5fb63c1SChristoph Hellwig 	}
8576d7b5aaeSAl Viro 
8586d7b5aaeSAl Viro 	return error;
8596d7b5aaeSAl Viro 
8606d7b5aaeSAl Viro out_put_nd_path:
86198f6ef64SArnd Bergmann 	*p = NULL;
8626d7b5aaeSAl Viro 	path_put(&nd->path);
8636d7b5aaeSAl Viro 	path_put(link);
8641da177e4SLinus Torvalds 	return error;
8651da177e4SLinus Torvalds }
8661da177e4SLinus Torvalds 
86731e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
86831e6b01fSNick Piggin {
8690714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
8700714a533SAl Viro 	struct mount *parent;
87131e6b01fSNick Piggin 	struct dentry *mountpoint;
87231e6b01fSNick Piggin 
8730714a533SAl Viro 	parent = mnt->mnt_parent;
8740714a533SAl Viro 	if (&parent->mnt == path->mnt)
87531e6b01fSNick Piggin 		return 0;
876a73324daSAl Viro 	mountpoint = mnt->mnt_mountpoint;
87731e6b01fSNick Piggin 	path->dentry = mountpoint;
8780714a533SAl Viro 	path->mnt = &parent->mnt;
87931e6b01fSNick Piggin 	return 1;
88031e6b01fSNick Piggin }
88131e6b01fSNick Piggin 
882f015f126SDavid Howells /*
883f015f126SDavid Howells  * follow_up - Find the mountpoint of path's vfsmount
884f015f126SDavid Howells  *
885f015f126SDavid Howells  * Given a path, find the mountpoint of its source file system.
886f015f126SDavid Howells  * Replace @path with the path of the mountpoint in the parent mount.
887f015f126SDavid Howells  * Up is towards /.
888f015f126SDavid Howells  *
889f015f126SDavid Howells  * Return 1 if we went up a level and 0 if we were already at the
890f015f126SDavid Howells  * root.
891f015f126SDavid Howells  */
892bab77ebfSAl Viro int follow_up(struct path *path)
8931da177e4SLinus Torvalds {
8940714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
8950714a533SAl Viro 	struct mount *parent;
8961da177e4SLinus Torvalds 	struct dentry *mountpoint;
89799b7db7bSNick Piggin 
898962830dfSAndi Kleen 	br_read_lock(&vfsmount_lock);
8990714a533SAl Viro 	parent = mnt->mnt_parent;
9003c0a6163SAl Viro 	if (parent == mnt) {
901962830dfSAndi Kleen 		br_read_unlock(&vfsmount_lock);
9021da177e4SLinus Torvalds 		return 0;
9031da177e4SLinus Torvalds 	}
9040714a533SAl Viro 	mntget(&parent->mnt);
905a73324daSAl Viro 	mountpoint = dget(mnt->mnt_mountpoint);
906962830dfSAndi Kleen 	br_read_unlock(&vfsmount_lock);
907bab77ebfSAl Viro 	dput(path->dentry);
908bab77ebfSAl Viro 	path->dentry = mountpoint;
909bab77ebfSAl Viro 	mntput(path->mnt);
9100714a533SAl Viro 	path->mnt = &parent->mnt;
9111da177e4SLinus Torvalds 	return 1;
9121da177e4SLinus Torvalds }
9131da177e4SLinus Torvalds 
914b5c84bf6SNick Piggin /*
9159875cf80SDavid Howells  * Perform an automount
9169875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
9179875cf80SDavid Howells  *   were called with.
9181da177e4SLinus Torvalds  */
9199875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
9209875cf80SDavid Howells 			    bool *need_mntput)
92131e6b01fSNick Piggin {
9229875cf80SDavid Howells 	struct vfsmount *mnt;
923ea5b778aSDavid Howells 	int err;
9249875cf80SDavid Howells 
9259875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
9269875cf80SDavid Howells 		return -EREMOTE;
9279875cf80SDavid Howells 
9280ec26fd0SMiklos Szeredi 	/* We don't want to mount if someone's just doing a stat -
9290ec26fd0SMiklos Szeredi 	 * unless they're stat'ing a directory and appended a '/' to
9300ec26fd0SMiklos Szeredi 	 * the name.
9310ec26fd0SMiklos Szeredi 	 *
9320ec26fd0SMiklos Szeredi 	 * We do, however, want to mount if someone wants to open or
9335a30d8a2SDavid Howells 	 * create a file of any type under the mountpoint, wants to
9340ec26fd0SMiklos Szeredi 	 * traverse through the mountpoint or wants to open the
9350ec26fd0SMiklos Szeredi 	 * mounted directory.  Also, autofs may mark negative dentries
9360ec26fd0SMiklos Szeredi 	 * as being automount points.  These will need the attentions
9370ec26fd0SMiklos Szeredi 	 * of the daemon to instantiate them before they can be used.
9385a30d8a2SDavid Howells 	 */
9395a30d8a2SDavid Howells 	if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
940d94c177bSLinus Torvalds 		     LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
9415a30d8a2SDavid Howells 	    path->dentry->d_inode)
9429875cf80SDavid Howells 		return -EISDIR;
9430ec26fd0SMiklos Szeredi 
9449875cf80SDavid Howells 	current->total_link_count++;
9459875cf80SDavid Howells 	if (current->total_link_count >= 40)
9469875cf80SDavid Howells 		return -ELOOP;
9479875cf80SDavid Howells 
9489875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
9499875cf80SDavid Howells 	if (IS_ERR(mnt)) {
9509875cf80SDavid Howells 		/*
9519875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
9529875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
9539875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
9549875cf80SDavid Howells 		 *
9559875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
9569875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
9579875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
9589875cf80SDavid Howells 		 */
95949084c3bSAl Viro 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
9609875cf80SDavid Howells 			return -EREMOTE;
9619875cf80SDavid Howells 		return PTR_ERR(mnt);
96231e6b01fSNick Piggin 	}
963ea5b778aSDavid Howells 
9649875cf80SDavid Howells 	if (!mnt) /* mount collision */
9659875cf80SDavid Howells 		return 0;
9669875cf80SDavid Howells 
9678aef1884SAl Viro 	if (!*need_mntput) {
9688aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
9698aef1884SAl Viro 		mntget(path->mnt);
9708aef1884SAl Viro 		*need_mntput = true;
9718aef1884SAl Viro 	}
97219a167afSAl Viro 	err = finish_automount(mnt, path);
973ea5b778aSDavid Howells 
974ea5b778aSDavid Howells 	switch (err) {
975ea5b778aSDavid Howells 	case -EBUSY:
976ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
97719a167afSAl Viro 		return 0;
978ea5b778aSDavid Howells 	case 0:
9798aef1884SAl Viro 		path_put(path);
9809875cf80SDavid Howells 		path->mnt = mnt;
9819875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
9829875cf80SDavid Howells 		return 0;
98319a167afSAl Viro 	default:
98419a167afSAl Viro 		return err;
9859875cf80SDavid Howells 	}
98619a167afSAl Viro 
987ea5b778aSDavid Howells }
9889875cf80SDavid Howells 
9899875cf80SDavid Howells /*
9909875cf80SDavid Howells  * Handle a dentry that is managed in some way.
991cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
9929875cf80SDavid Howells  * - Flagged as mountpoint
9939875cf80SDavid Howells  * - Flagged as automount point
9949875cf80SDavid Howells  *
9959875cf80SDavid Howells  * This may only be called in refwalk mode.
9969875cf80SDavid Howells  *
9979875cf80SDavid Howells  * Serialization is taken care of in namespace.c
9989875cf80SDavid Howells  */
9999875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
10009875cf80SDavid Howells {
10018aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
10029875cf80SDavid Howells 	unsigned managed;
10039875cf80SDavid Howells 	bool need_mntput = false;
10048aef1884SAl Viro 	int ret = 0;
10059875cf80SDavid Howells 
10069875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
10079875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
10089875cf80SDavid Howells 	 * the components of that value change under us */
10099875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
10109875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
10119875cf80SDavid Howells 	       unlikely(managed != 0)) {
1012cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1013cc53ce53SDavid Howells 		 * being held. */
1014cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1015cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1016cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
10171aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
1018cc53ce53SDavid Howells 			if (ret < 0)
10198aef1884SAl Viro 				break;
1020cc53ce53SDavid Howells 		}
1021cc53ce53SDavid Howells 
10229875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
10239875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
10249875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
10259875cf80SDavid Howells 			if (mounted) {
10269875cf80SDavid Howells 				dput(path->dentry);
10279875cf80SDavid Howells 				if (need_mntput)
1028463ffb2eSAl Viro 					mntput(path->mnt);
1029463ffb2eSAl Viro 				path->mnt = mounted;
1030463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
10319875cf80SDavid Howells 				need_mntput = true;
10329875cf80SDavid Howells 				continue;
1033463ffb2eSAl Viro 			}
1034463ffb2eSAl Viro 
10359875cf80SDavid Howells 			/* Something is mounted on this dentry in another
10369875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
10379875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
10389875cf80SDavid Howells 			 * vfsmount_lock */
10391da177e4SLinus Torvalds 		}
10409875cf80SDavid Howells 
10419875cf80SDavid Howells 		/* Handle an automount point */
10429875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
10439875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
10449875cf80SDavid Howells 			if (ret < 0)
10458aef1884SAl Viro 				break;
10469875cf80SDavid Howells 			continue;
10479875cf80SDavid Howells 		}
10489875cf80SDavid Howells 
10499875cf80SDavid Howells 		/* We didn't change the current path point */
10509875cf80SDavid Howells 		break;
10519875cf80SDavid Howells 	}
10528aef1884SAl Viro 
10538aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
10548aef1884SAl Viro 		mntput(path->mnt);
10558aef1884SAl Viro 	if (ret == -EISDIR)
10568aef1884SAl Viro 		ret = 0;
1057a3fbbde7SAl Viro 	return ret < 0 ? ret : need_mntput;
10581da177e4SLinus Torvalds }
10591da177e4SLinus Torvalds 
1060cc53ce53SDavid Howells int follow_down_one(struct path *path)
10611da177e4SLinus Torvalds {
10621da177e4SLinus Torvalds 	struct vfsmount *mounted;
10631da177e4SLinus Torvalds 
10641c755af4SAl Viro 	mounted = lookup_mnt(path);
10651da177e4SLinus Torvalds 	if (mounted) {
10669393bd07SAl Viro 		dput(path->dentry);
10679393bd07SAl Viro 		mntput(path->mnt);
10689393bd07SAl Viro 		path->mnt = mounted;
10699393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
10701da177e4SLinus Torvalds 		return 1;
10711da177e4SLinus Torvalds 	}
10721da177e4SLinus Torvalds 	return 0;
10731da177e4SLinus Torvalds }
10741da177e4SLinus Torvalds 
107562a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
107662a7375eSIan Kent {
107762a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
107862a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
107962a7375eSIan Kent }
108062a7375eSIan Kent 
10819875cf80SDavid Howells /*
1082287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
1083287548e4SAl Viro  * we meet a managed dentry that would need blocking.
10849875cf80SDavid Howells  */
10859875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1086287548e4SAl Viro 			       struct inode **inode)
10879875cf80SDavid Howells {
108862a7375eSIan Kent 	for (;;) {
1089c7105365SAl Viro 		struct mount *mounted;
109062a7375eSIan Kent 		/*
109162a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
109262a7375eSIan Kent 		 * that wants to block transit.
109362a7375eSIan Kent 		 */
1094287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
1095ab90911fSDavid Howells 			return false;
109662a7375eSIan Kent 
109762a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
109862a7375eSIan Kent 			break;
109962a7375eSIan Kent 
11009875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
11019875cf80SDavid Howells 		if (!mounted)
11029875cf80SDavid Howells 			break;
1103c7105365SAl Viro 		path->mnt = &mounted->mnt;
1104c7105365SAl Viro 		path->dentry = mounted->mnt.mnt_root;
1105a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
11069875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
110759430262SLinus Torvalds 		/*
110859430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
110959430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
111059430262SLinus Torvalds 		 * because a mount-point is always pinned.
111159430262SLinus Torvalds 		 */
111259430262SLinus Torvalds 		*inode = path->dentry->d_inode;
11139875cf80SDavid Howells 	}
11149875cf80SDavid Howells 	return true;
11159875cf80SDavid Howells }
11169875cf80SDavid Howells 
1117dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
1118287548e4SAl Viro {
1119dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
1120c7105365SAl Viro 		struct mount *mounted;
1121dea39376SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
1122287548e4SAl Viro 		if (!mounted)
1123287548e4SAl Viro 			break;
1124c7105365SAl Viro 		nd->path.mnt = &mounted->mnt;
1125c7105365SAl Viro 		nd->path.dentry = mounted->mnt.mnt_root;
1126dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1127287548e4SAl Viro 	}
1128287548e4SAl Viro }
1129287548e4SAl Viro 
113031e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
113131e6b01fSNick Piggin {
113231e6b01fSNick Piggin 	set_root_rcu(nd);
113331e6b01fSNick Piggin 
113431e6b01fSNick Piggin 	while (1) {
113531e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
113631e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
113731e6b01fSNick Piggin 			break;
113831e6b01fSNick Piggin 		}
113931e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
114031e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
114131e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
114231e6b01fSNick Piggin 			unsigned seq;
114331e6b01fSNick Piggin 
114431e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
114531e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
1146ef7562d5SAl Viro 				goto failed;
114731e6b01fSNick Piggin 			nd->path.dentry = parent;
114831e6b01fSNick Piggin 			nd->seq = seq;
114931e6b01fSNick Piggin 			break;
115031e6b01fSNick Piggin 		}
115131e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
115231e6b01fSNick Piggin 			break;
115331e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
115431e6b01fSNick Piggin 	}
1155dea39376SAl Viro 	follow_mount_rcu(nd);
1156dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
115731e6b01fSNick Piggin 	return 0;
1158ef7562d5SAl Viro 
1159ef7562d5SAl Viro failed:
1160ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
11615b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
1162ef7562d5SAl Viro 		nd->root.mnt = NULL;
116332a7991bSAl Viro 	unlock_rcu_walk();
1164ef7562d5SAl Viro 	return -ECHILD;
116531e6b01fSNick Piggin }
116631e6b01fSNick Piggin 
11679875cf80SDavid Howells /*
1168cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
1169cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
1170cc53ce53SDavid Howells  * caller is permitted to proceed or not.
1171cc53ce53SDavid Howells  */
11727cc90cc3SAl Viro int follow_down(struct path *path)
1173cc53ce53SDavid Howells {
1174cc53ce53SDavid Howells 	unsigned managed;
1175cc53ce53SDavid Howells 	int ret;
1176cc53ce53SDavid Howells 
1177cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
1178cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1179cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1180cc53ce53SDavid Howells 		 * being held.
1181cc53ce53SDavid Howells 		 *
1182cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
1183cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
1184cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
1185cc53ce53SDavid Howells 		 * superstructure.
1186cc53ce53SDavid Howells 		 *
1187cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
1188cc53ce53SDavid Howells 		 */
1189cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1190cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1191cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
1192ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
11931aed3e42SAl Viro 				path->dentry, false);
1194cc53ce53SDavid Howells 			if (ret < 0)
1195cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
1196cc53ce53SDavid Howells 		}
1197cc53ce53SDavid Howells 
1198cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
1199cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
1200cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
1201cc53ce53SDavid Howells 			if (!mounted)
1202cc53ce53SDavid Howells 				break;
1203cc53ce53SDavid Howells 			dput(path->dentry);
1204cc53ce53SDavid Howells 			mntput(path->mnt);
1205cc53ce53SDavid Howells 			path->mnt = mounted;
1206cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1207cc53ce53SDavid Howells 			continue;
1208cc53ce53SDavid Howells 		}
1209cc53ce53SDavid Howells 
1210cc53ce53SDavid Howells 		/* Don't handle automount points here */
1211cc53ce53SDavid Howells 		break;
1212cc53ce53SDavid Howells 	}
1213cc53ce53SDavid Howells 	return 0;
1214cc53ce53SDavid Howells }
1215cc53ce53SDavid Howells 
1216cc53ce53SDavid Howells /*
12179875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
12189875cf80SDavid Howells  */
12199875cf80SDavid Howells static void follow_mount(struct path *path)
12209875cf80SDavid Howells {
12219875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
12229875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
12239875cf80SDavid Howells 		if (!mounted)
12249875cf80SDavid Howells 			break;
12259875cf80SDavid Howells 		dput(path->dentry);
12269875cf80SDavid Howells 		mntput(path->mnt);
12279875cf80SDavid Howells 		path->mnt = mounted;
12289875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
12299875cf80SDavid Howells 	}
12309875cf80SDavid Howells }
12319875cf80SDavid Howells 
123231e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
12331da177e4SLinus Torvalds {
12342a737871SAl Viro 	set_root(nd);
1235e518ddb7SAndreas Mohr 
12361da177e4SLinus Torvalds 	while(1) {
12374ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
12381da177e4SLinus Torvalds 
12392a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
12402a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
12411da177e4SLinus Torvalds 			break;
12421da177e4SLinus Torvalds 		}
12434ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
12443088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
12453088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
12461da177e4SLinus Torvalds 			dput(old);
12471da177e4SLinus Torvalds 			break;
12481da177e4SLinus Torvalds 		}
12493088dd70SAl Viro 		if (!follow_up(&nd->path))
12501da177e4SLinus Torvalds 			break;
12511da177e4SLinus Torvalds 	}
125279ed0226SAl Viro 	follow_mount(&nd->path);
125331e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
12541da177e4SLinus Torvalds }
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds /*
1257bad61189SMiklos Szeredi  * This looks up the name in dcache, possibly revalidates the old dentry and
1258bad61189SMiklos Szeredi  * allocates a new one if not found or not valid.  In the need_lookup argument
1259bad61189SMiklos Szeredi  * returns whether i_op->lookup is necessary.
1260bad61189SMiklos Szeredi  *
1261bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
1262baa03890SNick Piggin  */
1263bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1264201f956eSAl Viro 				    unsigned int flags, bool *need_lookup)
1265baa03890SNick Piggin {
1266baa03890SNick Piggin 	struct dentry *dentry;
1267bad61189SMiklos Szeredi 	int error;
1268baa03890SNick Piggin 
1269bad61189SMiklos Szeredi 	*need_lookup = false;
1270bad61189SMiklos Szeredi 	dentry = d_lookup(dir, name);
1271bad61189SMiklos Szeredi 	if (dentry) {
127239e3c955SJeff Layton 		if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1273201f956eSAl Viro 			error = d_revalidate(dentry, flags);
1274bad61189SMiklos Szeredi 			if (unlikely(error <= 0)) {
1275bad61189SMiklos Szeredi 				if (error < 0) {
1276bad61189SMiklos Szeredi 					dput(dentry);
1277bad61189SMiklos Szeredi 					return ERR_PTR(error);
1278bad61189SMiklos Szeredi 				} else if (!d_invalidate(dentry)) {
1279bad61189SMiklos Szeredi 					dput(dentry);
1280bad61189SMiklos Szeredi 					dentry = NULL;
1281bad61189SMiklos Szeredi 				}
1282bad61189SMiklos Szeredi 			}
1283bad61189SMiklos Szeredi 		}
1284bad61189SMiklos Szeredi 	}
1285baa03890SNick Piggin 
1286bad61189SMiklos Szeredi 	if (!dentry) {
1287bad61189SMiklos Szeredi 		dentry = d_alloc(dir, name);
1288baa03890SNick Piggin 		if (unlikely(!dentry))
1289baa03890SNick Piggin 			return ERR_PTR(-ENOMEM);
1290baa03890SNick Piggin 
1291bad61189SMiklos Szeredi 		*need_lookup = true;
1292baa03890SNick Piggin 	}
1293baa03890SNick Piggin 	return dentry;
1294baa03890SNick Piggin }
1295baa03890SNick Piggin 
1296baa03890SNick Piggin /*
1297bad61189SMiklos Szeredi  * Call i_op->lookup on the dentry.  The dentry must be negative but may be
1298bad61189SMiklos Szeredi  * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1299bad61189SMiklos Szeredi  *
1300bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
130144396f4bSJosef Bacik  */
1302bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
130372bd866aSAl Viro 				  unsigned int flags)
130444396f4bSJosef Bacik {
130544396f4bSJosef Bacik 	struct dentry *old;
130644396f4bSJosef Bacik 
130744396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
1308bad61189SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
1309e188dc02SMiklos Szeredi 		dput(dentry);
131044396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
1311e188dc02SMiklos Szeredi 	}
131244396f4bSJosef Bacik 
131372bd866aSAl Viro 	old = dir->i_op->lookup(dir, dentry, flags);
131444396f4bSJosef Bacik 	if (unlikely(old)) {
131544396f4bSJosef Bacik 		dput(dentry);
131644396f4bSJosef Bacik 		dentry = old;
131744396f4bSJosef Bacik 	}
131844396f4bSJosef Bacik 	return dentry;
131944396f4bSJosef Bacik }
132044396f4bSJosef Bacik 
1321a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name,
132272bd866aSAl Viro 		struct dentry *base, unsigned int flags)
1323a3255546SAl Viro {
1324bad61189SMiklos Szeredi 	bool need_lookup;
1325a3255546SAl Viro 	struct dentry *dentry;
1326a3255546SAl Viro 
132772bd866aSAl Viro 	dentry = lookup_dcache(name, base, flags, &need_lookup);
1328bad61189SMiklos Szeredi 	if (!need_lookup)
1329a3255546SAl Viro 		return dentry;
1330bad61189SMiklos Szeredi 
133172bd866aSAl Viro 	return lookup_real(base->d_inode, dentry, flags);
1332a3255546SAl Viro }
1333a3255546SAl Viro 
133444396f4bSJosef Bacik /*
13351da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
13361da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
13371da177e4SLinus Torvalds  *  It _is_ time-critical.
13381da177e4SLinus Torvalds  */
1339e97cdc87SAl Viro static int lookup_fast(struct nameidata *nd,
134031e6b01fSNick Piggin 		       struct path *path, struct inode **inode)
13411da177e4SLinus Torvalds {
13424ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
134331e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
13445a18fff2SAl Viro 	int need_reval = 1;
13455a18fff2SAl Viro 	int status = 1;
13469875cf80SDavid Howells 	int err;
13479875cf80SDavid Howells 
13483cac260aSAl Viro 	/*
1349b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1350b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1351b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1352b04f784eSNick Piggin 	 */
135331e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
135431e6b01fSNick Piggin 		unsigned seq;
1355e97cdc87SAl Viro 		dentry = __d_lookup_rcu(parent, &nd->last, &seq, nd->inode);
13565a18fff2SAl Viro 		if (!dentry)
13575a18fff2SAl Viro 			goto unlazy;
13585a18fff2SAl Viro 
135912f8ad4bSLinus Torvalds 		/*
136012f8ad4bSLinus Torvalds 		 * This sequence count validates that the inode matches
136112f8ad4bSLinus Torvalds 		 * the dentry name information from lookup.
136212f8ad4bSLinus Torvalds 		 */
136312f8ad4bSLinus Torvalds 		*inode = dentry->d_inode;
136412f8ad4bSLinus Torvalds 		if (read_seqcount_retry(&dentry->d_seq, seq))
136512f8ad4bSLinus Torvalds 			return -ECHILD;
136612f8ad4bSLinus Torvalds 
136712f8ad4bSLinus Torvalds 		/*
136812f8ad4bSLinus Torvalds 		 * This sequence count validates that the parent had no
136912f8ad4bSLinus Torvalds 		 * changes while we did the lookup of the dentry above.
137012f8ad4bSLinus Torvalds 		 *
137112f8ad4bSLinus Torvalds 		 * The memory barrier in read_seqcount_begin of child is
137212f8ad4bSLinus Torvalds 		 *  enough, we can use __read_seqcount_retry here.
137312f8ad4bSLinus Torvalds 		 */
137431e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
137531e6b01fSNick Piggin 			return -ECHILD;
137631e6b01fSNick Piggin 		nd->seq = seq;
13775a18fff2SAl Viro 
137824643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
13794ce16ef3SAl Viro 			status = d_revalidate(dentry, nd->flags);
13805a18fff2SAl Viro 			if (unlikely(status <= 0)) {
13815a18fff2SAl Viro 				if (status != -ECHILD)
13825a18fff2SAl Viro 					need_reval = 0;
13835a18fff2SAl Viro 				goto unlazy;
13845a18fff2SAl Viro 			}
138524643087SAl Viro 		}
138631e6b01fSNick Piggin 		path->mnt = mnt;
138731e6b01fSNick Piggin 		path->dentry = dentry;
1388d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1389d6e9bd25SAl Viro 			goto unlazy;
1390d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1391d6e9bd25SAl Viro 			goto unlazy;
13929875cf80SDavid Howells 		return 0;
13935a18fff2SAl Viro unlazy:
139419660af7SAl Viro 		if (unlazy_walk(nd, dentry))
13955a18fff2SAl Viro 			return -ECHILD;
13965a18fff2SAl Viro 	} else {
1397e97cdc87SAl Viro 		dentry = __d_lookup(parent, &nd->last);
139824643087SAl Viro 	}
13995a18fff2SAl Viro 
140081e6f520SAl Viro 	if (unlikely(!dentry))
140181e6f520SAl Viro 		goto need_lookup;
14025a18fff2SAl Viro 
14035a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
14044ce16ef3SAl Viro 		status = d_revalidate(dentry, nd->flags);
14055a18fff2SAl Viro 	if (unlikely(status <= 0)) {
14065a18fff2SAl Viro 		if (status < 0) {
14075a18fff2SAl Viro 			dput(dentry);
14085a18fff2SAl Viro 			return status;
14095a18fff2SAl Viro 		}
14105a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
14115a18fff2SAl Viro 			dput(dentry);
141281e6f520SAl Viro 			goto need_lookup;
14135a18fff2SAl Viro 		}
14145a18fff2SAl Viro 	}
1415697f514dSMiklos Szeredi 
14161da177e4SLinus Torvalds 	path->mnt = mnt;
14171da177e4SLinus Torvalds 	path->dentry = dentry;
14189875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
141989312214SIan Kent 	if (unlikely(err < 0)) {
142089312214SIan Kent 		path_put_conditional(path, nd);
14219875cf80SDavid Howells 		return err;
142289312214SIan Kent 	}
1423a3fbbde7SAl Viro 	if (err)
1424a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
142531e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
14261da177e4SLinus Torvalds 	return 0;
142781e6f520SAl Viro 
142881e6f520SAl Viro need_lookup:
1429697f514dSMiklos Szeredi 	return 1;
1430697f514dSMiklos Szeredi }
1431697f514dSMiklos Szeredi 
1432697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */
1433cc2a5271SAl Viro static int lookup_slow(struct nameidata *nd, struct path *path)
1434697f514dSMiklos Szeredi {
1435697f514dSMiklos Szeredi 	struct dentry *dentry, *parent;
1436697f514dSMiklos Szeredi 	int err;
1437697f514dSMiklos Szeredi 
1438697f514dSMiklos Szeredi 	parent = nd->path.dentry;
143981e6f520SAl Viro 	BUG_ON(nd->inode != parent->d_inode);
144081e6f520SAl Viro 
144181e6f520SAl Viro 	mutex_lock(&parent->d_inode->i_mutex);
1442cc2a5271SAl Viro 	dentry = __lookup_hash(&nd->last, parent, nd->flags);
144381e6f520SAl Viro 	mutex_unlock(&parent->d_inode->i_mutex);
144481e6f520SAl Viro 	if (IS_ERR(dentry))
144581e6f520SAl Viro 		return PTR_ERR(dentry);
1446697f514dSMiklos Szeredi 	path->mnt = nd->path.mnt;
1447697f514dSMiklos Szeredi 	path->dentry = dentry;
1448697f514dSMiklos Szeredi 	err = follow_managed(path, nd->flags);
1449697f514dSMiklos Szeredi 	if (unlikely(err < 0)) {
1450697f514dSMiklos Szeredi 		path_put_conditional(path, nd);
1451697f514dSMiklos Szeredi 		return err;
1452697f514dSMiklos Szeredi 	}
1453697f514dSMiklos Szeredi 	if (err)
1454697f514dSMiklos Szeredi 		nd->flags |= LOOKUP_JUMPED;
1455697f514dSMiklos Szeredi 	return 0;
14561da177e4SLinus Torvalds }
14571da177e4SLinus Torvalds 
145852094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
145952094c8aSAl Viro {
146052094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
14614ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
146252094c8aSAl Viro 		if (err != -ECHILD)
146352094c8aSAl Viro 			return err;
146419660af7SAl Viro 		if (unlazy_walk(nd, NULL))
146552094c8aSAl Viro 			return -ECHILD;
146652094c8aSAl Viro 	}
14674ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
146852094c8aSAl Viro }
146952094c8aSAl Viro 
14709856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
14719856fa1bSAl Viro {
14729856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
14739856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
14749856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
14759856fa1bSAl Viro 				return -ECHILD;
14769856fa1bSAl Viro 		} else
14779856fa1bSAl Viro 			follow_dotdot(nd);
14789856fa1bSAl Viro 	}
14799856fa1bSAl Viro 	return 0;
14809856fa1bSAl Viro }
14819856fa1bSAl Viro 
1482951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1483951361f9SAl Viro {
1484951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1485951361f9SAl Viro 		path_put(&nd->path);
1486951361f9SAl Viro 	} else {
1487951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
14885b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1489951361f9SAl Viro 			nd->root.mnt = NULL;
149032a7991bSAl Viro 		unlock_rcu_walk();
1491951361f9SAl Viro 	}
1492951361f9SAl Viro }
1493951361f9SAl Viro 
14943ddcd056SLinus Torvalds /*
14953ddcd056SLinus Torvalds  * Do we need to follow links? We _really_ want to be able
14963ddcd056SLinus Torvalds  * to do this check without having to look at inode->i_op,
14973ddcd056SLinus Torvalds  * so we keep a cache of "no, this doesn't need follow_link"
14983ddcd056SLinus Torvalds  * for the common case.
14993ddcd056SLinus Torvalds  */
15007813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow)
15013ddcd056SLinus Torvalds {
15023ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
15033ddcd056SLinus Torvalds 		if (likely(inode->i_op->follow_link))
15043ddcd056SLinus Torvalds 			return follow;
15053ddcd056SLinus Torvalds 
15063ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
15073ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
15083ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_NOFOLLOW;
15093ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
15103ddcd056SLinus Torvalds 	}
15113ddcd056SLinus Torvalds 	return 0;
15123ddcd056SLinus Torvalds }
15133ddcd056SLinus Torvalds 
1514ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
151521b9b073SAl Viro 		int follow)
1516ce57dfc1SAl Viro {
1517ce57dfc1SAl Viro 	struct inode *inode;
1518ce57dfc1SAl Viro 	int err;
1519ce57dfc1SAl Viro 	/*
1520ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1521ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1522ce57dfc1SAl Viro 	 * parent relationships.
1523ce57dfc1SAl Viro 	 */
152421b9b073SAl Viro 	if (unlikely(nd->last_type != LAST_NORM))
152521b9b073SAl Viro 		return handle_dots(nd, nd->last_type);
1526e97cdc87SAl Viro 	err = lookup_fast(nd, path, &inode);
1527ce57dfc1SAl Viro 	if (unlikely(err)) {
1528697f514dSMiklos Szeredi 		if (err < 0)
1529697f514dSMiklos Szeredi 			goto out_err;
1530697f514dSMiklos Szeredi 
1531cc2a5271SAl Viro 		err = lookup_slow(nd, path);
1532697f514dSMiklos Szeredi 		if (err < 0)
1533697f514dSMiklos Szeredi 			goto out_err;
1534697f514dSMiklos Szeredi 
1535697f514dSMiklos Szeredi 		inode = path->dentry->d_inode;
1536ce57dfc1SAl Viro 	}
1537697f514dSMiklos Szeredi 	err = -ENOENT;
1538697f514dSMiklos Szeredi 	if (!inode)
1539697f514dSMiklos Szeredi 		goto out_path_put;
1540697f514dSMiklos Szeredi 
15417813b94aSLinus Torvalds 	if (should_follow_link(inode, follow)) {
154219660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
154319660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
1544697f514dSMiklos Szeredi 				err = -ECHILD;
1545697f514dSMiklos Szeredi 				goto out_err;
154619660af7SAl Viro 			}
154719660af7SAl Viro 		}
1548ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1549ce57dfc1SAl Viro 		return 1;
1550ce57dfc1SAl Viro 	}
1551ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1552ce57dfc1SAl Viro 	nd->inode = inode;
1553ce57dfc1SAl Viro 	return 0;
1554697f514dSMiklos Szeredi 
1555697f514dSMiklos Szeredi out_path_put:
1556697f514dSMiklos Szeredi 	path_to_nameidata(path, nd);
1557697f514dSMiklos Szeredi out_err:
1558697f514dSMiklos Szeredi 	terminate_walk(nd);
1559697f514dSMiklos Szeredi 	return err;
1560ce57dfc1SAl Viro }
1561ce57dfc1SAl Viro 
15621da177e4SLinus Torvalds /*
1563b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1564b356379aSAl Viro  * limiting consecutive symlinks to 40.
1565b356379aSAl Viro  *
1566b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1567b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1568b356379aSAl Viro  */
1569b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1570b356379aSAl Viro {
1571b356379aSAl Viro 	int res;
1572b356379aSAl Viro 
1573b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1574b356379aSAl Viro 		path_put_conditional(path, nd);
1575b356379aSAl Viro 		path_put(&nd->path);
1576b356379aSAl Viro 		return -ELOOP;
1577b356379aSAl Viro 	}
15781a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1579b356379aSAl Viro 
1580b356379aSAl Viro 	nd->depth++;
1581b356379aSAl Viro 	current->link_count++;
1582b356379aSAl Viro 
1583b356379aSAl Viro 	do {
1584b356379aSAl Viro 		struct path link = *path;
1585b356379aSAl Viro 		void *cookie;
1586574197e0SAl Viro 
1587574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
15886d7b5aaeSAl Viro 		if (res)
15896d7b5aaeSAl Viro 			break;
159021b9b073SAl Viro 		res = walk_component(nd, path, LOOKUP_FOLLOW);
1591574197e0SAl Viro 		put_link(nd, &link, cookie);
1592b356379aSAl Viro 	} while (res > 0);
1593b356379aSAl Viro 
1594b356379aSAl Viro 	current->link_count--;
1595b356379aSAl Viro 	nd->depth--;
1596b356379aSAl Viro 	return res;
1597b356379aSAl Viro }
1598b356379aSAl Viro 
1599b356379aSAl Viro /*
16003ddcd056SLinus Torvalds  * We really don't want to look at inode->i_op->lookup
16013ddcd056SLinus Torvalds  * when we don't have to. So we keep a cache bit in
16023ddcd056SLinus Torvalds  * the inode ->i_opflags field that says "yes, we can
16033ddcd056SLinus Torvalds  * do lookup on this inode".
16043ddcd056SLinus Torvalds  */
16053ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode)
16063ddcd056SLinus Torvalds {
16073ddcd056SLinus Torvalds 	if (likely(inode->i_opflags & IOP_LOOKUP))
16083ddcd056SLinus Torvalds 		return 1;
16093ddcd056SLinus Torvalds 	if (likely(!inode->i_op->lookup))
16103ddcd056SLinus Torvalds 		return 0;
16113ddcd056SLinus Torvalds 
16123ddcd056SLinus Torvalds 	/* We do this once for the lifetime of the inode */
16133ddcd056SLinus Torvalds 	spin_lock(&inode->i_lock);
16143ddcd056SLinus Torvalds 	inode->i_opflags |= IOP_LOOKUP;
16153ddcd056SLinus Torvalds 	spin_unlock(&inode->i_lock);
16163ddcd056SLinus Torvalds 	return 1;
16173ddcd056SLinus Torvalds }
16183ddcd056SLinus Torvalds 
1619bfcfaa77SLinus Torvalds /*
1620bfcfaa77SLinus Torvalds  * We can do the critical dentry name comparison and hashing
1621bfcfaa77SLinus Torvalds  * operations one word at a time, but we are limited to:
1622bfcfaa77SLinus Torvalds  *
1623bfcfaa77SLinus Torvalds  * - Architectures with fast unaligned word accesses. We could
1624bfcfaa77SLinus Torvalds  *   do a "get_unaligned()" if this helps and is sufficiently
1625bfcfaa77SLinus Torvalds  *   fast.
1626bfcfaa77SLinus Torvalds  *
1627bfcfaa77SLinus Torvalds  * - Little-endian machines (so that we can generate the mask
1628bfcfaa77SLinus Torvalds  *   of low bytes efficiently). Again, we *could* do a byte
1629bfcfaa77SLinus Torvalds  *   swapping load on big-endian architectures if that is not
1630bfcfaa77SLinus Torvalds  *   expensive enough to make the optimization worthless.
1631bfcfaa77SLinus Torvalds  *
1632bfcfaa77SLinus Torvalds  * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1633bfcfaa77SLinus Torvalds  *   do not trap on the (extremely unlikely) case of a page
1634bfcfaa77SLinus Torvalds  *   crossing operation.
1635bfcfaa77SLinus Torvalds  *
1636bfcfaa77SLinus Torvalds  * - Furthermore, we need an efficient 64-bit compile for the
1637bfcfaa77SLinus Torvalds  *   64-bit case in order to generate the "number of bytes in
1638bfcfaa77SLinus Torvalds  *   the final mask". Again, that could be replaced with a
1639bfcfaa77SLinus Torvalds  *   efficient population count instruction or similar.
1640bfcfaa77SLinus Torvalds  */
1641bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS
1642bfcfaa77SLinus Torvalds 
1643f68e556eSLinus Torvalds #include <asm/word-at-a-time.h>
1644bfcfaa77SLinus Torvalds 
1645f68e556eSLinus Torvalds #ifdef CONFIG_64BIT
1646bfcfaa77SLinus Torvalds 
1647bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash)
1648bfcfaa77SLinus Torvalds {
1649bfcfaa77SLinus Torvalds 	hash += hash >> (8*sizeof(int));
1650bfcfaa77SLinus Torvalds 	return hash;
1651bfcfaa77SLinus Torvalds }
1652bfcfaa77SLinus Torvalds 
1653bfcfaa77SLinus Torvalds #else	/* 32-bit case */
1654bfcfaa77SLinus Torvalds 
1655bfcfaa77SLinus Torvalds #define fold_hash(x) (x)
1656bfcfaa77SLinus Torvalds 
1657bfcfaa77SLinus Torvalds #endif
1658bfcfaa77SLinus Torvalds 
1659bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1660bfcfaa77SLinus Torvalds {
1661bfcfaa77SLinus Torvalds 	unsigned long a, mask;
1662bfcfaa77SLinus Torvalds 	unsigned long hash = 0;
1663bfcfaa77SLinus Torvalds 
1664bfcfaa77SLinus Torvalds 	for (;;) {
1665e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name);
1666bfcfaa77SLinus Torvalds 		if (len < sizeof(unsigned long))
1667bfcfaa77SLinus Torvalds 			break;
1668bfcfaa77SLinus Torvalds 		hash += a;
1669f132c5beSAl Viro 		hash *= 9;
1670bfcfaa77SLinus Torvalds 		name += sizeof(unsigned long);
1671bfcfaa77SLinus Torvalds 		len -= sizeof(unsigned long);
1672bfcfaa77SLinus Torvalds 		if (!len)
1673bfcfaa77SLinus Torvalds 			goto done;
1674bfcfaa77SLinus Torvalds 	}
1675bfcfaa77SLinus Torvalds 	mask = ~(~0ul << len*8);
1676bfcfaa77SLinus Torvalds 	hash += mask & a;
1677bfcfaa77SLinus Torvalds done:
1678bfcfaa77SLinus Torvalds 	return fold_hash(hash);
1679bfcfaa77SLinus Torvalds }
1680bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
1681bfcfaa77SLinus Torvalds 
1682bfcfaa77SLinus Torvalds /*
1683bfcfaa77SLinus Torvalds  * Calculate the length and hash of the path component, and
1684bfcfaa77SLinus Torvalds  * return the length of the component;
1685bfcfaa77SLinus Torvalds  */
1686bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1687bfcfaa77SLinus Torvalds {
168836126f8fSLinus Torvalds 	unsigned long a, b, adata, bdata, mask, hash, len;
168936126f8fSLinus Torvalds 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1690bfcfaa77SLinus Torvalds 
1691bfcfaa77SLinus Torvalds 	hash = a = 0;
1692bfcfaa77SLinus Torvalds 	len = -sizeof(unsigned long);
1693bfcfaa77SLinus Torvalds 	do {
1694bfcfaa77SLinus Torvalds 		hash = (hash + a) * 9;
1695bfcfaa77SLinus Torvalds 		len += sizeof(unsigned long);
1696e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name+len);
169736126f8fSLinus Torvalds 		b = a ^ REPEAT_BYTE('/');
169836126f8fSLinus Torvalds 	} while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1699bfcfaa77SLinus Torvalds 
170036126f8fSLinus Torvalds 	adata = prep_zero_mask(a, adata, &constants);
170136126f8fSLinus Torvalds 	bdata = prep_zero_mask(b, bdata, &constants);
170236126f8fSLinus Torvalds 
170336126f8fSLinus Torvalds 	mask = create_zero_mask(adata | bdata);
170436126f8fSLinus Torvalds 
170536126f8fSLinus Torvalds 	hash += a & zero_bytemask(mask);
1706bfcfaa77SLinus Torvalds 	*hashp = fold_hash(hash);
1707bfcfaa77SLinus Torvalds 
170836126f8fSLinus Torvalds 	return len + find_zero(mask);
1709bfcfaa77SLinus Torvalds }
1710bfcfaa77SLinus Torvalds 
1711bfcfaa77SLinus Torvalds #else
1712bfcfaa77SLinus Torvalds 
17130145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
17140145acc2SLinus Torvalds {
17150145acc2SLinus Torvalds 	unsigned long hash = init_name_hash();
17160145acc2SLinus Torvalds 	while (len--)
17170145acc2SLinus Torvalds 		hash = partial_name_hash(*name++, hash);
17180145acc2SLinus Torvalds 	return end_name_hash(hash);
17190145acc2SLinus Torvalds }
1720ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
17210145acc2SLinus Torvalds 
17223ddcd056SLinus Torvalds /*
1723200e9ef7SLinus Torvalds  * We know there's a real path component here of at least
1724200e9ef7SLinus Torvalds  * one character.
1725200e9ef7SLinus Torvalds  */
1726200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1727200e9ef7SLinus Torvalds {
1728200e9ef7SLinus Torvalds 	unsigned long hash = init_name_hash();
1729200e9ef7SLinus Torvalds 	unsigned long len = 0, c;
1730200e9ef7SLinus Torvalds 
1731200e9ef7SLinus Torvalds 	c = (unsigned char)*name;
1732200e9ef7SLinus Torvalds 	do {
1733200e9ef7SLinus Torvalds 		len++;
1734200e9ef7SLinus Torvalds 		hash = partial_name_hash(c, hash);
1735200e9ef7SLinus Torvalds 		c = (unsigned char)name[len];
1736200e9ef7SLinus Torvalds 	} while (c && c != '/');
1737200e9ef7SLinus Torvalds 	*hashp = end_name_hash(hash);
1738200e9ef7SLinus Torvalds 	return len;
1739200e9ef7SLinus Torvalds }
1740200e9ef7SLinus Torvalds 
1741bfcfaa77SLinus Torvalds #endif
1742bfcfaa77SLinus Torvalds 
1743200e9ef7SLinus Torvalds /*
17441da177e4SLinus Torvalds  * Name resolution.
1745ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1746ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
17471da177e4SLinus Torvalds  *
1748ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1749ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
17501da177e4SLinus Torvalds  */
17516de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
17521da177e4SLinus Torvalds {
17531da177e4SLinus Torvalds 	struct path next;
17541da177e4SLinus Torvalds 	int err;
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 	while (*name=='/')
17571da177e4SLinus Torvalds 		name++;
17581da177e4SLinus Torvalds 	if (!*name)
1759086e183aSAl Viro 		return 0;
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
17621da177e4SLinus Torvalds 	for(;;) {
17631da177e4SLinus Torvalds 		struct qstr this;
1764200e9ef7SLinus Torvalds 		long len;
1765fe479a58SAl Viro 		int type;
17661da177e4SLinus Torvalds 
176752094c8aSAl Viro 		err = may_lookup(nd);
17681da177e4SLinus Torvalds  		if (err)
17691da177e4SLinus Torvalds 			break;
17701da177e4SLinus Torvalds 
1771200e9ef7SLinus Torvalds 		len = hash_name(name, &this.hash);
17721da177e4SLinus Torvalds 		this.name = name;
1773200e9ef7SLinus Torvalds 		this.len = len;
17741da177e4SLinus Torvalds 
1775fe479a58SAl Viro 		type = LAST_NORM;
1776200e9ef7SLinus Torvalds 		if (name[0] == '.') switch (len) {
1777fe479a58SAl Viro 			case 2:
1778200e9ef7SLinus Torvalds 				if (name[1] == '.') {
1779fe479a58SAl Viro 					type = LAST_DOTDOT;
178016c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
178116c2cd71SAl Viro 				}
1782fe479a58SAl Viro 				break;
1783fe479a58SAl Viro 			case 1:
1784fe479a58SAl Viro 				type = LAST_DOT;
1785fe479a58SAl Viro 		}
17865a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
17875a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
178816c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
17895a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
17905a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
17915a202bcdSAl Viro 							   &this);
17925a202bcdSAl Viro 				if (err < 0)
17935a202bcdSAl Viro 					break;
17945a202bcdSAl Viro 			}
17955a202bcdSAl Viro 		}
1796fe479a58SAl Viro 
17975f4a6a69SAl Viro 		nd->last = this;
17985f4a6a69SAl Viro 		nd->last_type = type;
17995f4a6a69SAl Viro 
1800200e9ef7SLinus Torvalds 		if (!name[len])
18015f4a6a69SAl Viro 			return 0;
1802200e9ef7SLinus Torvalds 		/*
1803200e9ef7SLinus Torvalds 		 * If it wasn't NUL, we know it was '/'. Skip that
1804200e9ef7SLinus Torvalds 		 * slash, and continue until no more slashes.
1805200e9ef7SLinus Torvalds 		 */
1806200e9ef7SLinus Torvalds 		do {
1807200e9ef7SLinus Torvalds 			len++;
1808200e9ef7SLinus Torvalds 		} while (unlikely(name[len] == '/'));
1809200e9ef7SLinus Torvalds 		if (!name[len])
18105f4a6a69SAl Viro 			return 0;
18115f4a6a69SAl Viro 
1812200e9ef7SLinus Torvalds 		name += len;
18131da177e4SLinus Torvalds 
181421b9b073SAl Viro 		err = walk_component(nd, &next, LOOKUP_FOLLOW);
1815ce57dfc1SAl Viro 		if (err < 0)
1816ce57dfc1SAl Viro 			return err;
1817fe479a58SAl Viro 
1818ce57dfc1SAl Viro 		if (err) {
1819b356379aSAl Viro 			err = nested_symlink(&next, nd);
18201da177e4SLinus Torvalds 			if (err)
1821a7472babSAl Viro 				return err;
182231e6b01fSNick Piggin 		}
18235f4a6a69SAl Viro 		if (!can_lookup(nd->inode)) {
18243ddcd056SLinus Torvalds 			err = -ENOTDIR;
18253ddcd056SLinus Torvalds 			break;
18265f4a6a69SAl Viro 		}
1827ce57dfc1SAl Viro 	}
1828951361f9SAl Viro 	terminate_walk(nd);
18291da177e4SLinus Torvalds 	return err;
18301da177e4SLinus Torvalds }
18311da177e4SLinus Torvalds 
183270e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
183370e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
183431e6b01fSNick Piggin {
183531e6b01fSNick Piggin 	int retval = 0;
183631e6b01fSNick Piggin 
183731e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
183816c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
183931e6b01fSNick Piggin 	nd->depth = 0;
18405b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
18415b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
184273d049a4SAl Viro 		if (*name) {
1843741b7c3fSAl Viro 			if (!can_lookup(inode))
18445b6ca027SAl Viro 				return -ENOTDIR;
18455b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
18465b6ca027SAl Viro 			if (retval)
18475b6ca027SAl Viro 				return retval;
184873d049a4SAl Viro 		}
18495b6ca027SAl Viro 		nd->path = nd->root;
18505b6ca027SAl Viro 		nd->inode = inode;
18515b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
185232a7991bSAl Viro 			lock_rcu_walk();
18535b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
18545b6ca027SAl Viro 		} else {
18555b6ca027SAl Viro 			path_get(&nd->path);
18565b6ca027SAl Viro 		}
18575b6ca027SAl Viro 		return 0;
18585b6ca027SAl Viro 	}
18595b6ca027SAl Viro 
186031e6b01fSNick Piggin 	nd->root.mnt = NULL;
186131e6b01fSNick Piggin 
186231e6b01fSNick Piggin 	if (*name=='/') {
1863e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
186432a7991bSAl Viro 			lock_rcu_walk();
1865e41f7d4eSAl Viro 			set_root_rcu(nd);
1866e41f7d4eSAl Viro 		} else {
1867e41f7d4eSAl Viro 			set_root(nd);
1868e41f7d4eSAl Viro 			path_get(&nd->root);
1869e41f7d4eSAl Viro 		}
187031e6b01fSNick Piggin 		nd->path = nd->root;
187131e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1872e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
187331e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1874c28cc364SNick Piggin 			unsigned seq;
187531e6b01fSNick Piggin 
187632a7991bSAl Viro 			lock_rcu_walk();
187731e6b01fSNick Piggin 
1878c28cc364SNick Piggin 			do {
1879c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
188031e6b01fSNick Piggin 				nd->path = fs->pwd;
1881c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1882c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1883e41f7d4eSAl Viro 		} else {
1884e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1885e41f7d4eSAl Viro 		}
188631e6b01fSNick Piggin 	} else {
1887582aa64aSJeff Layton 		/* Caller must check execute permissions on the starting path component */
18882903ff01SAl Viro 		struct fd f = fdget_raw(dfd);
188931e6b01fSNick Piggin 		struct dentry *dentry;
189031e6b01fSNick Piggin 
18912903ff01SAl Viro 		if (!f.file)
18922903ff01SAl Viro 			return -EBADF;
189331e6b01fSNick Piggin 
18942903ff01SAl Viro 		dentry = f.file->f_path.dentry;
189531e6b01fSNick Piggin 
1896f52e0c11SAl Viro 		if (*name) {
1897741b7c3fSAl Viro 			if (!can_lookup(dentry->d_inode)) {
18982903ff01SAl Viro 				fdput(f);
18992903ff01SAl Viro 				return -ENOTDIR;
1900f52e0c11SAl Viro 			}
19012903ff01SAl Viro 		}
19022903ff01SAl Viro 
19032903ff01SAl Viro 		nd->path = f.file->f_path;
1904e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
19052903ff01SAl Viro 			if (f.need_put)
19062903ff01SAl Viro 				*fp = f.file;
1907c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
190832a7991bSAl Viro 			lock_rcu_walk();
19095590ff0dSUlrich Drepper 		} else {
19102903ff01SAl Viro 			path_get(&nd->path);
19112903ff01SAl Viro 			fdput(f);
19121da177e4SLinus Torvalds 		}
1913e41f7d4eSAl Viro 	}
1914e41f7d4eSAl Viro 
191531e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
19169b4a9b14SAl Viro 	return 0;
19179b4a9b14SAl Viro }
19189b4a9b14SAl Viro 
1919bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1920bd92d7feSAl Viro {
1921bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1922bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1923bd92d7feSAl Viro 
1924bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
192521b9b073SAl Viro 	return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
1926bd92d7feSAl Viro }
1927bd92d7feSAl Viro 
19289b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1929ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
19309b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
19319b4a9b14SAl Viro {
193270e9b357SAl Viro 	struct file *base = NULL;
1933bd92d7feSAl Viro 	struct path path;
1934bd92d7feSAl Viro 	int err;
193531e6b01fSNick Piggin 
193631e6b01fSNick Piggin 	/*
193731e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
193831e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
193931e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
194031e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
194131e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
194231e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
194331e6b01fSNick Piggin 	 * analogue, foo_rcu().
194431e6b01fSNick Piggin 	 *
194531e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
194631e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
194731e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
194831e6b01fSNick Piggin 	 * be able to complete).
194931e6b01fSNick Piggin 	 */
1950bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1951ee0827cdSAl Viro 
1952bd92d7feSAl Viro 	if (unlikely(err))
1953bd92d7feSAl Viro 		return err;
1954ee0827cdSAl Viro 
1955ee0827cdSAl Viro 	current->total_link_count = 0;
1956bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1957bd92d7feSAl Viro 
1958bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1959bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1960bd92d7feSAl Viro 		while (err > 0) {
1961bd92d7feSAl Viro 			void *cookie;
1962bd92d7feSAl Viro 			struct path link = path;
1963800179c9SKees Cook 			err = may_follow_link(&link, nd);
1964800179c9SKees Cook 			if (unlikely(err))
1965800179c9SKees Cook 				break;
1966bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1967574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
19686d7b5aaeSAl Viro 			if (err)
19696d7b5aaeSAl Viro 				break;
1970bd92d7feSAl Viro 			err = lookup_last(nd, &path);
1971574197e0SAl Viro 			put_link(nd, &link, cookie);
1972bd92d7feSAl Viro 		}
1973bd92d7feSAl Viro 	}
1974ee0827cdSAl Viro 
19759f1fafeeSAl Viro 	if (!err)
19769f1fafeeSAl Viro 		err = complete_walk(nd);
1977bd92d7feSAl Viro 
1978bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
197905252901SAl Viro 		if (!can_lookup(nd->inode)) {
1980bd92d7feSAl Viro 			path_put(&nd->path);
1981bd23a539SAl Viro 			err = -ENOTDIR;
1982bd92d7feSAl Viro 		}
1983bd92d7feSAl Viro 	}
198416c2cd71SAl Viro 
198570e9b357SAl Viro 	if (base)
198670e9b357SAl Viro 		fput(base);
1987ee0827cdSAl Viro 
19885b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
198931e6b01fSNick Piggin 		path_put(&nd->root);
199031e6b01fSNick Piggin 		nd->root.mnt = NULL;
199131e6b01fSNick Piggin 	}
1992bd92d7feSAl Viro 	return err;
199331e6b01fSNick Piggin }
199431e6b01fSNick Piggin 
1995873f1eedSJeff Layton static int filename_lookup(int dfd, struct filename *name,
1996873f1eedSJeff Layton 				unsigned int flags, struct nameidata *nd)
1997873f1eedSJeff Layton {
1998873f1eedSJeff Layton 	int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
1999873f1eedSJeff Layton 	if (unlikely(retval == -ECHILD))
2000873f1eedSJeff Layton 		retval = path_lookupat(dfd, name->name, flags, nd);
2001873f1eedSJeff Layton 	if (unlikely(retval == -ESTALE))
2002873f1eedSJeff Layton 		retval = path_lookupat(dfd, name->name,
2003873f1eedSJeff Layton 						flags | LOOKUP_REVAL, nd);
2004873f1eedSJeff Layton 
2005873f1eedSJeff Layton 	if (likely(!retval))
2006adb5c247SJeff Layton 		audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
2007873f1eedSJeff Layton 	return retval;
2008873f1eedSJeff Layton }
2009873f1eedSJeff Layton 
2010ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
2011ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
2012ee0827cdSAl Viro {
2013873f1eedSJeff Layton 	struct filename filename = { .name = name };
2014ee0827cdSAl Viro 
2015873f1eedSJeff Layton 	return filename_lookup(dfd, &filename, flags, nd);
20161da177e4SLinus Torvalds }
20171da177e4SLinus Torvalds 
201879714f72SAl Viro /* does lookup, returns the object with parent locked */
201979714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path)
20205590ff0dSUlrich Drepper {
202179714f72SAl Viro 	struct nameidata nd;
202279714f72SAl Viro 	struct dentry *d;
202379714f72SAl Viro 	int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
202479714f72SAl Viro 	if (err)
202579714f72SAl Viro 		return ERR_PTR(err);
202679714f72SAl Viro 	if (nd.last_type != LAST_NORM) {
202779714f72SAl Viro 		path_put(&nd.path);
202879714f72SAl Viro 		return ERR_PTR(-EINVAL);
202979714f72SAl Viro 	}
203079714f72SAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
20311e0ea001SAl Viro 	d = __lookup_hash(&nd.last, nd.path.dentry, 0);
203279714f72SAl Viro 	if (IS_ERR(d)) {
203379714f72SAl Viro 		mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
203479714f72SAl Viro 		path_put(&nd.path);
203579714f72SAl Viro 		return d;
203679714f72SAl Viro 	}
203779714f72SAl Viro 	*path = nd.path;
203879714f72SAl Viro 	return d;
20395590ff0dSUlrich Drepper }
20405590ff0dSUlrich Drepper 
2041d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
2042d1811465SAl Viro {
2043d1811465SAl Viro 	struct nameidata nd;
2044d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2045d1811465SAl Viro 	if (!res)
2046d1811465SAl Viro 		*path = nd.path;
2047d1811465SAl Viro 	return res;
2048d1811465SAl Viro }
2049d1811465SAl Viro 
205016f18200SJosef 'Jeff' Sipek /**
205116f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
205216f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
205316f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
205416f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
205516f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
2056e0a01249SAl Viro  * @path: pointer to struct path to fill
205716f18200SJosef 'Jeff' Sipek  */
205816f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
205916f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
2060e0a01249SAl Viro 		    struct path *path)
206116f18200SJosef 'Jeff' Sipek {
2062e0a01249SAl Viro 	struct nameidata nd;
2063e0a01249SAl Viro 	int err;
2064e0a01249SAl Viro 	nd.root.dentry = dentry;
2065e0a01249SAl Viro 	nd.root.mnt = mnt;
2066e0a01249SAl Viro 	BUG_ON(flags & LOOKUP_PARENT);
20675b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
2068e0a01249SAl Viro 	err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2069e0a01249SAl Viro 	if (!err)
2070e0a01249SAl Viro 		*path = nd.path;
2071e0a01249SAl Viro 	return err;
207216f18200SJosef 'Jeff' Sipek }
207316f18200SJosef 'Jeff' Sipek 
2074057f6c01SJames Morris /*
2075057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
2076057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
2077057f6c01SJames Morris  * SMP-safe.
2078057f6c01SJames Morris  */
2079a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
20801da177e4SLinus Torvalds {
208172bd866aSAl Viro 	return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
20821da177e4SLinus Torvalds }
20831da177e4SLinus Torvalds 
2084eead1911SChristoph Hellwig /**
2085a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
2086eead1911SChristoph Hellwig  * @name:	pathname component to lookup
2087eead1911SChristoph Hellwig  * @base:	base directory to lookup from
2088eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
2089eead1911SChristoph Hellwig  *
2090a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
2091a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
2092eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
2093eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
2094eead1911SChristoph Hellwig  */
2095057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2096057f6c01SJames Morris {
2097057f6c01SJames Morris 	struct qstr this;
20986a96ba54SAl Viro 	unsigned int c;
2099cda309deSMiklos Szeredi 	int err;
2100057f6c01SJames Morris 
21012f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
21022f9092e1SDavid Woodhouse 
21036a96ba54SAl Viro 	this.name = name;
21046a96ba54SAl Viro 	this.len = len;
21050145acc2SLinus Torvalds 	this.hash = full_name_hash(name, len);
21066a96ba54SAl Viro 	if (!len)
21076a96ba54SAl Viro 		return ERR_PTR(-EACCES);
21086a96ba54SAl Viro 
210921d8a15aSAl Viro 	if (unlikely(name[0] == '.')) {
211021d8a15aSAl Viro 		if (len < 2 || (len == 2 && name[1] == '.'))
211121d8a15aSAl Viro 			return ERR_PTR(-EACCES);
211221d8a15aSAl Viro 	}
211321d8a15aSAl Viro 
21146a96ba54SAl Viro 	while (len--) {
21156a96ba54SAl Viro 		c = *(const unsigned char *)name++;
21166a96ba54SAl Viro 		if (c == '/' || c == '\0')
21176a96ba54SAl Viro 			return ERR_PTR(-EACCES);
21186a96ba54SAl Viro 	}
21195a202bcdSAl Viro 	/*
21205a202bcdSAl Viro 	 * See if the low-level filesystem might want
21215a202bcdSAl Viro 	 * to use its own hash..
21225a202bcdSAl Viro 	 */
21235a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
21245a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
21255a202bcdSAl Viro 		if (err < 0)
21265a202bcdSAl Viro 			return ERR_PTR(err);
21275a202bcdSAl Viro 	}
2128eead1911SChristoph Hellwig 
2129cda309deSMiklos Szeredi 	err = inode_permission(base->d_inode, MAY_EXEC);
2130cda309deSMiklos Szeredi 	if (err)
2131cda309deSMiklos Szeredi 		return ERR_PTR(err);
2132cda309deSMiklos Szeredi 
213372bd866aSAl Viro 	return __lookup_hash(&this, base, 0);
2134057f6c01SJames Morris }
2135057f6c01SJames Morris 
21361fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
21371fa1e7f6SAndy Whitcroft 		 struct path *path, int *empty)
21381da177e4SLinus Torvalds {
21392d8f3038SAl Viro 	struct nameidata nd;
214091a27b2aSJeff Layton 	struct filename *tmp = getname_flags(name, flags, empty);
21411da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
21421da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
21432d8f3038SAl Viro 
21442d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
21452d8f3038SAl Viro 
2146873f1eedSJeff Layton 		err = filename_lookup(dfd, tmp, flags, &nd);
21471da177e4SLinus Torvalds 		putname(tmp);
21482d8f3038SAl Viro 		if (!err)
21492d8f3038SAl Viro 			*path = nd.path;
21501da177e4SLinus Torvalds 	}
21511da177e4SLinus Torvalds 	return err;
21521da177e4SLinus Torvalds }
21531da177e4SLinus Torvalds 
21541fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags,
21551fa1e7f6SAndy Whitcroft 		 struct path *path)
21561fa1e7f6SAndy Whitcroft {
2157f7493e5dSLinus Torvalds 	return user_path_at_empty(dfd, name, flags, path, NULL);
21581fa1e7f6SAndy Whitcroft }
21591fa1e7f6SAndy Whitcroft 
2160873f1eedSJeff Layton /*
2161873f1eedSJeff Layton  * NB: most callers don't do anything directly with the reference to the
2162873f1eedSJeff Layton  *     to struct filename, but the nd->last pointer points into the name string
2163873f1eedSJeff Layton  *     allocated by getname. So we must hold the reference to it until all
2164873f1eedSJeff Layton  *     path-walking is complete.
2165873f1eedSJeff Layton  */
216691a27b2aSJeff Layton static struct filename *
21679e790bd6SJeff Layton user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
21689e790bd6SJeff Layton 		 unsigned int flags)
21692ad94ae6SAl Viro {
217091a27b2aSJeff Layton 	struct filename *s = getname(path);
21712ad94ae6SAl Viro 	int error;
21722ad94ae6SAl Viro 
21739e790bd6SJeff Layton 	/* only LOOKUP_REVAL is allowed in extra flags */
21749e790bd6SJeff Layton 	flags &= LOOKUP_REVAL;
21759e790bd6SJeff Layton 
21762ad94ae6SAl Viro 	if (IS_ERR(s))
217791a27b2aSJeff Layton 		return s;
21782ad94ae6SAl Viro 
21799e790bd6SJeff Layton 	error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
218091a27b2aSJeff Layton 	if (error) {
21812ad94ae6SAl Viro 		putname(s);
218291a27b2aSJeff Layton 		return ERR_PTR(error);
218391a27b2aSJeff Layton 	}
21842ad94ae6SAl Viro 
218591a27b2aSJeff Layton 	return s;
21862ad94ae6SAl Viro }
21872ad94ae6SAl Viro 
21881da177e4SLinus Torvalds /*
21891da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
21901da177e4SLinus Torvalds  * minimal.
21911da177e4SLinus Torvalds  */
21921da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
21931da177e4SLinus Torvalds {
21948e96e3b7SEric W. Biederman 	kuid_t fsuid = current_fsuid();
2195da9592edSDavid Howells 
21961da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
21971da177e4SLinus Torvalds 		return 0;
21988e96e3b7SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
21991da177e4SLinus Torvalds 		return 0;
22008e96e3b7SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
22011da177e4SLinus Torvalds 		return 0;
22021a48e2acSEric W. Biederman 	return !inode_capable(inode, CAP_FOWNER);
22031da177e4SLinus Torvalds }
22041da177e4SLinus Torvalds 
22051da177e4SLinus Torvalds /*
22061da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
22071da177e4SLinus Torvalds  *  whether the type of victim is right.
22081da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
22091da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
22101da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
22111da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
22121da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
22131da177e4SLinus Torvalds  *	a. be owner of dir, or
22141da177e4SLinus Torvalds  *	b. be owner of victim, or
22151da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
22161da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
22171da177e4SLinus Torvalds  *     links pointing to it.
22181da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
22191da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
22201da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
22211da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
22221da177e4SLinus Torvalds  *     nfs_async_unlink().
22231da177e4SLinus Torvalds  */
2224858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
22251da177e4SLinus Torvalds {
22261da177e4SLinus Torvalds 	int error;
22271da177e4SLinus Torvalds 
22281da177e4SLinus Torvalds 	if (!victim->d_inode)
22291da177e4SLinus Torvalds 		return -ENOENT;
22301da177e4SLinus Torvalds 
22311da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
22324fa6b5ecSJeff Layton 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
22331da177e4SLinus Torvalds 
2234f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
22351da177e4SLinus Torvalds 	if (error)
22361da177e4SLinus Torvalds 		return error;
22371da177e4SLinus Torvalds 	if (IS_APPEND(dir))
22381da177e4SLinus Torvalds 		return -EPERM;
22391da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
2240f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
22411da177e4SLinus Torvalds 		return -EPERM;
22421da177e4SLinus Torvalds 	if (isdir) {
22431da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
22441da177e4SLinus Torvalds 			return -ENOTDIR;
22451da177e4SLinus Torvalds 		if (IS_ROOT(victim))
22461da177e4SLinus Torvalds 			return -EBUSY;
22471da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
22481da177e4SLinus Torvalds 		return -EISDIR;
22491da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
22501da177e4SLinus Torvalds 		return -ENOENT;
22511da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
22521da177e4SLinus Torvalds 		return -EBUSY;
22531da177e4SLinus Torvalds 	return 0;
22541da177e4SLinus Torvalds }
22551da177e4SLinus Torvalds 
22561da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
22571da177e4SLinus Torvalds  *  dir.
22581da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
22591da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
22601da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
22611da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
22621da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
22631da177e4SLinus Torvalds  */
2264a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
22651da177e4SLinus Torvalds {
22661da177e4SLinus Torvalds 	if (child->d_inode)
22671da177e4SLinus Torvalds 		return -EEXIST;
22681da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
22691da177e4SLinus Torvalds 		return -ENOENT;
2270f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
22711da177e4SLinus Torvalds }
22721da177e4SLinus Torvalds 
22731da177e4SLinus Torvalds /*
22741da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
22751da177e4SLinus Torvalds  */
22761da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
22771da177e4SLinus Torvalds {
22781da177e4SLinus Torvalds 	struct dentry *p;
22791da177e4SLinus Torvalds 
22801da177e4SLinus Torvalds 	if (p1 == p2) {
2281f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
22821da177e4SLinus Torvalds 		return NULL;
22831da177e4SLinus Torvalds 	}
22841da177e4SLinus Torvalds 
2285a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
22861da177e4SLinus Torvalds 
2287e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
2288e2761a11SOGAWA Hirofumi 	if (p) {
2289f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2290f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
22911da177e4SLinus Torvalds 		return p;
22921da177e4SLinus Torvalds 	}
22931da177e4SLinus Torvalds 
2294e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
2295e2761a11SOGAWA Hirofumi 	if (p) {
2296f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2297f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
22981da177e4SLinus Torvalds 		return p;
22991da177e4SLinus Torvalds 	}
23001da177e4SLinus Torvalds 
2301f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2302f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
23031da177e4SLinus Torvalds 	return NULL;
23041da177e4SLinus Torvalds }
23051da177e4SLinus Torvalds 
23061da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
23071da177e4SLinus Torvalds {
23081b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
23091da177e4SLinus Torvalds 	if (p1 != p2) {
23101b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
2311a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
23121da177e4SLinus Torvalds 	}
23131da177e4SLinus Torvalds }
23141da177e4SLinus Torvalds 
23154acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2316312b63fbSAl Viro 		bool want_excl)
23171da177e4SLinus Torvalds {
2318a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
23191da177e4SLinus Torvalds 	if (error)
23201da177e4SLinus Torvalds 		return error;
23211da177e4SLinus Torvalds 
2322acfa4380SAl Viro 	if (!dir->i_op->create)
23231da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
23241da177e4SLinus Torvalds 	mode &= S_IALLUGO;
23251da177e4SLinus Torvalds 	mode |= S_IFREG;
23261da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
23271da177e4SLinus Torvalds 	if (error)
23281da177e4SLinus Torvalds 		return error;
2329312b63fbSAl Viro 	error = dir->i_op->create(dir, dentry, mode, want_excl);
2330a74574aaSStephen Smalley 	if (!error)
2331f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
23321da177e4SLinus Torvalds 	return error;
23331da177e4SLinus Torvalds }
23341da177e4SLinus Torvalds 
233573d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
23361da177e4SLinus Torvalds {
23373fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
23381da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
23391da177e4SLinus Torvalds 	int error;
23401da177e4SLinus Torvalds 
2341bcda7652SAl Viro 	/* O_PATH? */
2342bcda7652SAl Viro 	if (!acc_mode)
2343bcda7652SAl Viro 		return 0;
2344bcda7652SAl Viro 
23451da177e4SLinus Torvalds 	if (!inode)
23461da177e4SLinus Torvalds 		return -ENOENT;
23471da177e4SLinus Torvalds 
2348c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2349c8fe8f30SChristoph Hellwig 	case S_IFLNK:
23501da177e4SLinus Torvalds 		return -ELOOP;
2351c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2352c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
23531da177e4SLinus Torvalds 			return -EISDIR;
2354c8fe8f30SChristoph Hellwig 		break;
2355c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2356c8fe8f30SChristoph Hellwig 	case S_IFCHR:
23573fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
23581da177e4SLinus Torvalds 			return -EACCES;
2359c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
2360c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2361c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
23621da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2363c8fe8f30SChristoph Hellwig 		break;
23644a3fd211SDave Hansen 	}
2365b41572e9SDave Hansen 
23663fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2367b41572e9SDave Hansen 	if (error)
2368b41572e9SDave Hansen 		return error;
23696146f0d5SMimi Zohar 
23701da177e4SLinus Torvalds 	/*
23711da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
23721da177e4SLinus Torvalds 	 */
23731da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
23748737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
23757715b521SAl Viro 			return -EPERM;
23761da177e4SLinus Torvalds 		if (flag & O_TRUNC)
23777715b521SAl Viro 			return -EPERM;
23781da177e4SLinus Torvalds 	}
23791da177e4SLinus Torvalds 
23801da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
23812e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
23827715b521SAl Viro 		return -EPERM;
23831da177e4SLinus Torvalds 
2384f3c7691eSJ. Bruce Fields 	return 0;
23857715b521SAl Viro }
23867715b521SAl Viro 
2387e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
23887715b521SAl Viro {
2389e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
23907715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
23917715b521SAl Viro 	int error = get_write_access(inode);
23921da177e4SLinus Torvalds 	if (error)
23937715b521SAl Viro 		return error;
23941da177e4SLinus Torvalds 	/*
23951da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
23961da177e4SLinus Torvalds 	 */
23971da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2398be6d3e56SKentaro Takeda 	if (!error)
2399ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
24001da177e4SLinus Torvalds 	if (!error) {
24017715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2402d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2403e1181ee6SJeff Layton 				    filp);
24041da177e4SLinus Torvalds 	}
24051da177e4SLinus Torvalds 	put_write_access(inode);
2406acd0c935SMimi Zohar 	return error;
24071da177e4SLinus Torvalds }
24081da177e4SLinus Torvalds 
2409d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2410d57999e1SDave Hansen {
24118a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
24128a5e929dSAl Viro 		flag--;
2413d57999e1SDave Hansen 	return flag;
2414d57999e1SDave Hansen }
2415d57999e1SDave Hansen 
2416d18e9008SMiklos Szeredi static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2417d18e9008SMiklos Szeredi {
2418d18e9008SMiklos Szeredi 	int error = security_path_mknod(dir, dentry, mode, 0);
2419d18e9008SMiklos Szeredi 	if (error)
2420d18e9008SMiklos Szeredi 		return error;
2421d18e9008SMiklos Szeredi 
2422d18e9008SMiklos Szeredi 	error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2423d18e9008SMiklos Szeredi 	if (error)
2424d18e9008SMiklos Szeredi 		return error;
2425d18e9008SMiklos Szeredi 
2426d18e9008SMiklos Szeredi 	return security_inode_create(dir->dentry->d_inode, dentry, mode);
2427d18e9008SMiklos Szeredi }
2428d18e9008SMiklos Szeredi 
24291acf0af9SDavid Howells /*
24301acf0af9SDavid Howells  * Attempt to atomically look up, create and open a file from a negative
24311acf0af9SDavid Howells  * dentry.
24321acf0af9SDavid Howells  *
24331acf0af9SDavid Howells  * Returns 0 if successful.  The file will have been created and attached to
24341acf0af9SDavid Howells  * @file by the filesystem calling finish_open().
24351acf0af9SDavid Howells  *
24361acf0af9SDavid Howells  * Returns 1 if the file was looked up only or didn't need creating.  The
24371acf0af9SDavid Howells  * caller will need to perform the open themselves.  @path will have been
24381acf0af9SDavid Howells  * updated to point to the new dentry.  This may be negative.
24391acf0af9SDavid Howells  *
24401acf0af9SDavid Howells  * Returns an error code otherwise.
24411acf0af9SDavid Howells  */
24422675a4ebSAl Viro static int atomic_open(struct nameidata *nd, struct dentry *dentry,
244330d90494SAl Viro 			struct path *path, struct file *file,
2444015c3bbcSMiklos Szeredi 			const struct open_flags *op,
244564894cf8SAl Viro 			bool got_write, bool need_lookup,
244647237687SAl Viro 			int *opened)
2447d18e9008SMiklos Szeredi {
2448d18e9008SMiklos Szeredi 	struct inode *dir =  nd->path.dentry->d_inode;
2449d18e9008SMiklos Szeredi 	unsigned open_flag = open_to_namei_flags(op->open_flag);
2450d18e9008SMiklos Szeredi 	umode_t mode;
2451d18e9008SMiklos Szeredi 	int error;
2452d18e9008SMiklos Szeredi 	int acc_mode;
2453d18e9008SMiklos Szeredi 	int create_error = 0;
2454d18e9008SMiklos Szeredi 	struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2455d18e9008SMiklos Szeredi 
2456d18e9008SMiklos Szeredi 	BUG_ON(dentry->d_inode);
2457d18e9008SMiklos Szeredi 
2458d18e9008SMiklos Szeredi 	/* Don't create child dentry for a dead directory. */
2459d18e9008SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
24602675a4ebSAl Viro 		error = -ENOENT;
2461d18e9008SMiklos Szeredi 		goto out;
2462d18e9008SMiklos Szeredi 	}
2463d18e9008SMiklos Szeredi 
246462b259d8SMiklos Szeredi 	mode = op->mode;
2465d18e9008SMiklos Szeredi 	if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2466d18e9008SMiklos Szeredi 		mode &= ~current_umask();
2467d18e9008SMiklos Szeredi 
2468f8310c59SAl Viro 	if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) {
2469d18e9008SMiklos Szeredi 		open_flag &= ~O_TRUNC;
247047237687SAl Viro 		*opened |= FILE_CREATED;
2471d18e9008SMiklos Szeredi 	}
2472d18e9008SMiklos Szeredi 
2473d18e9008SMiklos Szeredi 	/*
2474d18e9008SMiklos Szeredi 	 * Checking write permission is tricky, bacuse we don't know if we are
2475d18e9008SMiklos Szeredi 	 * going to actually need it: O_CREAT opens should work as long as the
2476d18e9008SMiklos Szeredi 	 * file exists.  But checking existence breaks atomicity.  The trick is
2477d18e9008SMiklos Szeredi 	 * to check access and if not granted clear O_CREAT from the flags.
2478d18e9008SMiklos Szeredi 	 *
2479d18e9008SMiklos Szeredi 	 * Another problem is returing the "right" error value (e.g. for an
2480d18e9008SMiklos Szeredi 	 * O_EXCL open we want to return EEXIST not EROFS).
2481d18e9008SMiklos Szeredi 	 */
248264894cf8SAl Viro 	if (((open_flag & (O_CREAT | O_TRUNC)) ||
248364894cf8SAl Viro 	    (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
248464894cf8SAl Viro 		if (!(open_flag & O_CREAT)) {
2485d18e9008SMiklos Szeredi 			/*
2486d18e9008SMiklos Szeredi 			 * No O_CREATE -> atomicity not a requirement -> fall
2487d18e9008SMiklos Szeredi 			 * back to lookup + open
2488d18e9008SMiklos Szeredi 			 */
2489d18e9008SMiklos Szeredi 			goto no_open;
2490d18e9008SMiklos Szeredi 		} else if (open_flag & (O_EXCL | O_TRUNC)) {
2491d18e9008SMiklos Szeredi 			/* Fall back and fail with the right error */
249264894cf8SAl Viro 			create_error = -EROFS;
2493d18e9008SMiklos Szeredi 			goto no_open;
2494d18e9008SMiklos Szeredi 		} else {
2495d18e9008SMiklos Szeredi 			/* No side effects, safe to clear O_CREAT */
249664894cf8SAl Viro 			create_error = -EROFS;
2497d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2498d18e9008SMiklos Szeredi 		}
2499d18e9008SMiklos Szeredi 	}
2500d18e9008SMiklos Szeredi 
2501d18e9008SMiklos Szeredi 	if (open_flag & O_CREAT) {
250238227f78SMiklos Szeredi 		error = may_o_create(&nd->path, dentry, mode);
2503d18e9008SMiklos Szeredi 		if (error) {
2504d18e9008SMiklos Szeredi 			create_error = error;
2505d18e9008SMiklos Szeredi 			if (open_flag & O_EXCL)
2506d18e9008SMiklos Szeredi 				goto no_open;
2507d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2508d18e9008SMiklos Szeredi 		}
2509d18e9008SMiklos Szeredi 	}
2510d18e9008SMiklos Szeredi 
2511d18e9008SMiklos Szeredi 	if (nd->flags & LOOKUP_DIRECTORY)
2512d18e9008SMiklos Szeredi 		open_flag |= O_DIRECTORY;
2513d18e9008SMiklos Szeredi 
251430d90494SAl Viro 	file->f_path.dentry = DENTRY_NOT_SET;
251530d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
251630d90494SAl Viro 	error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
251747237687SAl Viro 				      opened);
2518d9585277SAl Viro 	if (error < 0) {
2519d9585277SAl Viro 		if (create_error && error == -ENOENT)
2520d9585277SAl Viro 			error = create_error;
2521d18e9008SMiklos Szeredi 		goto out;
2522d18e9008SMiklos Szeredi 	}
2523d18e9008SMiklos Szeredi 
2524d18e9008SMiklos Szeredi 	acc_mode = op->acc_mode;
252547237687SAl Viro 	if (*opened & FILE_CREATED) {
2526d18e9008SMiklos Szeredi 		fsnotify_create(dir, dentry);
2527d18e9008SMiklos Szeredi 		acc_mode = MAY_OPEN;
2528d18e9008SMiklos Szeredi 	}
2529d18e9008SMiklos Szeredi 
2530d9585277SAl Viro 	if (error) {	/* returned 1, that is */
253130d90494SAl Viro 		if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
25322675a4ebSAl Viro 			error = -EIO;
2533d18e9008SMiklos Szeredi 			goto out;
2534d18e9008SMiklos Szeredi 		}
253530d90494SAl Viro 		if (file->f_path.dentry) {
2536d18e9008SMiklos Szeredi 			dput(dentry);
253730d90494SAl Viro 			dentry = file->f_path.dentry;
2538d18e9008SMiklos Szeredi 		}
253962b2ce96SSage Weil 		if (create_error && dentry->d_inode == NULL) {
254062b2ce96SSage Weil 			error = create_error;
254162b2ce96SSage Weil 			goto out;
254262b2ce96SSage Weil 		}
2543d18e9008SMiklos Szeredi 		goto looked_up;
2544d18e9008SMiklos Szeredi 	}
2545d18e9008SMiklos Szeredi 
2546d18e9008SMiklos Szeredi 	/*
2547d18e9008SMiklos Szeredi 	 * We didn't have the inode before the open, so check open permission
2548d18e9008SMiklos Szeredi 	 * here.
2549d18e9008SMiklos Szeredi 	 */
25502675a4ebSAl Viro 	error = may_open(&file->f_path, acc_mode, open_flag);
25512675a4ebSAl Viro 	if (error)
25522675a4ebSAl Viro 		fput(file);
2553d18e9008SMiklos Szeredi 
2554d18e9008SMiklos Szeredi out:
2555d18e9008SMiklos Szeredi 	dput(dentry);
25562675a4ebSAl Viro 	return error;
2557d18e9008SMiklos Szeredi 
2558d18e9008SMiklos Szeredi no_open:
2559d18e9008SMiklos Szeredi 	if (need_lookup) {
256072bd866aSAl Viro 		dentry = lookup_real(dir, dentry, nd->flags);
2561d18e9008SMiklos Szeredi 		if (IS_ERR(dentry))
25622675a4ebSAl Viro 			return PTR_ERR(dentry);
2563d18e9008SMiklos Szeredi 
2564d18e9008SMiklos Szeredi 		if (create_error) {
2565d18e9008SMiklos Szeredi 			int open_flag = op->open_flag;
2566d18e9008SMiklos Szeredi 
25672675a4ebSAl Viro 			error = create_error;
2568d18e9008SMiklos Szeredi 			if ((open_flag & O_EXCL)) {
2569d18e9008SMiklos Szeredi 				if (!dentry->d_inode)
2570d18e9008SMiklos Szeredi 					goto out;
2571d18e9008SMiklos Szeredi 			} else if (!dentry->d_inode) {
2572d18e9008SMiklos Szeredi 				goto out;
2573d18e9008SMiklos Szeredi 			} else if ((open_flag & O_TRUNC) &&
2574d18e9008SMiklos Szeredi 				   S_ISREG(dentry->d_inode->i_mode)) {
2575d18e9008SMiklos Szeredi 				goto out;
2576d18e9008SMiklos Szeredi 			}
2577d18e9008SMiklos Szeredi 			/* will fail later, go on to get the right error */
2578d18e9008SMiklos Szeredi 		}
2579d18e9008SMiklos Szeredi 	}
2580d18e9008SMiklos Szeredi looked_up:
2581d18e9008SMiklos Szeredi 	path->dentry = dentry;
2582d18e9008SMiklos Szeredi 	path->mnt = nd->path.mnt;
25832675a4ebSAl Viro 	return 1;
2584d18e9008SMiklos Szeredi }
2585d18e9008SMiklos Szeredi 
258631e6b01fSNick Piggin /*
25871acf0af9SDavid Howells  * Look up and maybe create and open the last component.
2588d58ffd35SMiklos Szeredi  *
2589d58ffd35SMiklos Szeredi  * Must be called with i_mutex held on parent.
2590d58ffd35SMiklos Szeredi  *
25911acf0af9SDavid Howells  * Returns 0 if the file was successfully atomically created (if necessary) and
25921acf0af9SDavid Howells  * opened.  In this case the file will be returned attached to @file.
25931acf0af9SDavid Howells  *
25941acf0af9SDavid Howells  * Returns 1 if the file was not completely opened at this time, though lookups
25951acf0af9SDavid Howells  * and creations will have been performed and the dentry returned in @path will
25961acf0af9SDavid Howells  * be positive upon return if O_CREAT was specified.  If O_CREAT wasn't
25971acf0af9SDavid Howells  * specified then a negative dentry may be returned.
25981acf0af9SDavid Howells  *
25991acf0af9SDavid Howells  * An error code is returned otherwise.
26001acf0af9SDavid Howells  *
26011acf0af9SDavid Howells  * FILE_CREATE will be set in @*opened if the dentry was created and will be
26021acf0af9SDavid Howells  * cleared otherwise prior to returning.
2603d58ffd35SMiklos Szeredi  */
26042675a4ebSAl Viro static int lookup_open(struct nameidata *nd, struct path *path,
260530d90494SAl Viro 			struct file *file,
2606d58ffd35SMiklos Szeredi 			const struct open_flags *op,
260764894cf8SAl Viro 			bool got_write, int *opened)
2608d58ffd35SMiklos Szeredi {
2609d58ffd35SMiklos Szeredi 	struct dentry *dir = nd->path.dentry;
261054ef4872SMiklos Szeredi 	struct inode *dir_inode = dir->d_inode;
2611d58ffd35SMiklos Szeredi 	struct dentry *dentry;
2612d58ffd35SMiklos Szeredi 	int error;
261354ef4872SMiklos Szeredi 	bool need_lookup;
2614d58ffd35SMiklos Szeredi 
261547237687SAl Viro 	*opened &= ~FILE_CREATED;
2616201f956eSAl Viro 	dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
2617d58ffd35SMiklos Szeredi 	if (IS_ERR(dentry))
26182675a4ebSAl Viro 		return PTR_ERR(dentry);
2619d58ffd35SMiklos Szeredi 
2620d18e9008SMiklos Szeredi 	/* Cached positive dentry: will open in f_op->open */
2621d18e9008SMiklos Szeredi 	if (!need_lookup && dentry->d_inode)
2622d18e9008SMiklos Szeredi 		goto out_no_open;
2623d18e9008SMiklos Szeredi 
2624d18e9008SMiklos Szeredi 	if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
262564894cf8SAl Viro 		return atomic_open(nd, dentry, path, file, op, got_write,
262647237687SAl Viro 				   need_lookup, opened);
2627d18e9008SMiklos Szeredi 	}
2628d18e9008SMiklos Szeredi 
262954ef4872SMiklos Szeredi 	if (need_lookup) {
263054ef4872SMiklos Szeredi 		BUG_ON(dentry->d_inode);
263154ef4872SMiklos Szeredi 
263272bd866aSAl Viro 		dentry = lookup_real(dir_inode, dentry, nd->flags);
263354ef4872SMiklos Szeredi 		if (IS_ERR(dentry))
26342675a4ebSAl Viro 			return PTR_ERR(dentry);
263554ef4872SMiklos Szeredi 	}
263654ef4872SMiklos Szeredi 
2637d58ffd35SMiklos Szeredi 	/* Negative dentry, just create the file */
2638d58ffd35SMiklos Szeredi 	if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2639d58ffd35SMiklos Szeredi 		umode_t mode = op->mode;
2640d58ffd35SMiklos Szeredi 		if (!IS_POSIXACL(dir->d_inode))
2641d58ffd35SMiklos Szeredi 			mode &= ~current_umask();
2642d58ffd35SMiklos Szeredi 		/*
2643d58ffd35SMiklos Szeredi 		 * This write is needed to ensure that a
2644d58ffd35SMiklos Szeredi 		 * rw->ro transition does not occur between
2645d58ffd35SMiklos Szeredi 		 * the time when the file is created and when
2646d58ffd35SMiklos Szeredi 		 * a permanent write count is taken through
2647015c3bbcSMiklos Szeredi 		 * the 'struct file' in finish_open().
2648d58ffd35SMiklos Szeredi 		 */
264964894cf8SAl Viro 		if (!got_write) {
265064894cf8SAl Viro 			error = -EROFS;
2651d58ffd35SMiklos Szeredi 			goto out_dput;
265264894cf8SAl Viro 		}
265347237687SAl Viro 		*opened |= FILE_CREATED;
2654d58ffd35SMiklos Szeredi 		error = security_path_mknod(&nd->path, dentry, mode, 0);
2655d58ffd35SMiklos Szeredi 		if (error)
2656d58ffd35SMiklos Szeredi 			goto out_dput;
2657312b63fbSAl Viro 		error = vfs_create(dir->d_inode, dentry, mode,
2658312b63fbSAl Viro 				   nd->flags & LOOKUP_EXCL);
2659d58ffd35SMiklos Szeredi 		if (error)
2660d58ffd35SMiklos Szeredi 			goto out_dput;
2661d58ffd35SMiklos Szeredi 	}
2662d18e9008SMiklos Szeredi out_no_open:
2663d58ffd35SMiklos Szeredi 	path->dentry = dentry;
2664d58ffd35SMiklos Szeredi 	path->mnt = nd->path.mnt;
26652675a4ebSAl Viro 	return 1;
2666d58ffd35SMiklos Szeredi 
2667d58ffd35SMiklos Szeredi out_dput:
2668d58ffd35SMiklos Szeredi 	dput(dentry);
26692675a4ebSAl Viro 	return error;
2670d58ffd35SMiklos Szeredi }
2671d58ffd35SMiklos Szeredi 
2672d58ffd35SMiklos Szeredi /*
2673fe2d35ffSAl Viro  * Handle the last step of open()
267431e6b01fSNick Piggin  */
26752675a4ebSAl Viro static int do_last(struct nameidata *nd, struct path *path,
267630d90494SAl Viro 		   struct file *file, const struct open_flags *op,
2677669abf4eSJeff Layton 		   int *opened, struct filename *name)
2678fb1cc555SAl Viro {
2679a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
2680ca344a89SAl Viro 	int open_flag = op->open_flag;
268177d660a8SMiklos Szeredi 	bool will_truncate = (open_flag & O_TRUNC) != 0;
268264894cf8SAl Viro 	bool got_write = false;
2683bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2684a1eb3315SMiklos Szeredi 	struct inode *inode;
268577d660a8SMiklos Szeredi 	bool symlink_ok = false;
268616b1c1cdSMiklos Szeredi 	struct path save_parent = { .dentry = NULL, .mnt = NULL };
268716b1c1cdSMiklos Szeredi 	bool retried = false;
268816c2cd71SAl Viro 	int error;
2689fb1cc555SAl Viro 
2690c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2691c3e380b0SAl Viro 	nd->flags |= op->intent;
2692c3e380b0SAl Viro 
2693bc77daa7SAl Viro 	if (nd->last_type != LAST_NORM) {
2694fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2695fe2d35ffSAl Viro 		if (error)
26962675a4ebSAl Viro 			return error;
2697e83db167SMiklos Szeredi 		goto finish_open;
26981f36f774SAl Viro 	}
2699a2c36b45SAl Viro 
2700ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2701fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2702fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2703bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
270477d660a8SMiklos Szeredi 			symlink_ok = true;
2705fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2706e97cdc87SAl Viro 		error = lookup_fast(nd, path, &inode);
270771574865SMiklos Szeredi 		if (likely(!error))
270871574865SMiklos Szeredi 			goto finish_lookup;
270971574865SMiklos Szeredi 
2710ce57dfc1SAl Viro 		if (error < 0)
27112675a4ebSAl Viro 			goto out;
2712a1eb3315SMiklos Szeredi 
271337d7fffcSMiklos Szeredi 		BUG_ON(nd->inode != dir->d_inode);
2714b6183df7SMiklos Szeredi 	} else {
2715fe2d35ffSAl Viro 		/* create side of things */
2716a3fbbde7SAl Viro 		/*
2717b6183df7SMiklos Szeredi 		 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2718b6183df7SMiklos Szeredi 		 * has been cleared when we got to the last component we are
2719b6183df7SMiklos Szeredi 		 * about to look up
2720a3fbbde7SAl Viro 		 */
27219f1fafeeSAl Viro 		error = complete_walk(nd);
27229f1fafeeSAl Viro 		if (error)
27232675a4ebSAl Viro 			return error;
2724fe2d35ffSAl Viro 
272533e2208aSJeff Layton 		audit_inode(name, dir, LOOKUP_PARENT);
272616c2cd71SAl Viro 		error = -EISDIR;
27271f36f774SAl Viro 		/* trailing slashes? */
272831e6b01fSNick Piggin 		if (nd->last.name[nd->last.len])
27292675a4ebSAl Viro 			goto out;
2730b6183df7SMiklos Szeredi 	}
27311f36f774SAl Viro 
273216b1c1cdSMiklos Szeredi retry_lookup:
273364894cf8SAl Viro 	if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
273464894cf8SAl Viro 		error = mnt_want_write(nd->path.mnt);
273564894cf8SAl Viro 		if (!error)
273664894cf8SAl Viro 			got_write = true;
273764894cf8SAl Viro 		/*
273864894cf8SAl Viro 		 * do _not_ fail yet - we might not need that or fail with
273964894cf8SAl Viro 		 * a different error; let lookup_open() decide; we'll be
274064894cf8SAl Viro 		 * dropping this one anyway.
274164894cf8SAl Viro 		 */
274264894cf8SAl Viro 	}
2743a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
274464894cf8SAl Viro 	error = lookup_open(nd, path, file, op, got_write, opened);
2745fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2746fb1cc555SAl Viro 
27472675a4ebSAl Viro 	if (error <= 0) {
27482675a4ebSAl Viro 		if (error)
2749d58ffd35SMiklos Szeredi 			goto out;
27506c0d46c4SAl Viro 
275147237687SAl Viro 		if ((*opened & FILE_CREATED) ||
2752496ad9aaSAl Viro 		    !S_ISREG(file_inode(file)->i_mode))
275377d660a8SMiklos Szeredi 			will_truncate = false;
2754d18e9008SMiklos Szeredi 
2755adb5c247SJeff Layton 		audit_inode(name, file->f_path.dentry, 0);
2756d18e9008SMiklos Szeredi 		goto opened;
2757d18e9008SMiklos Szeredi 	}
2758d18e9008SMiklos Szeredi 
275947237687SAl Viro 	if (*opened & FILE_CREATED) {
27609b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2761ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
276277d660a8SMiklos Szeredi 		will_truncate = false;
2763bcda7652SAl Viro 		acc_mode = MAY_OPEN;
2764d58ffd35SMiklos Szeredi 		path_to_nameidata(path, nd);
2765e83db167SMiklos Szeredi 		goto finish_open_created;
2766fb1cc555SAl Viro 	}
2767fb1cc555SAl Viro 
2768fb1cc555SAl Viro 	/*
27693134f37eSJeff Layton 	 * create/update audit record if it already exists.
2770fb1cc555SAl Viro 	 */
27713134f37eSJeff Layton 	if (path->dentry->d_inode)
2772adb5c247SJeff Layton 		audit_inode(name, path->dentry, 0);
2773fb1cc555SAl Viro 
2774d18e9008SMiklos Szeredi 	/*
2775d18e9008SMiklos Szeredi 	 * If atomic_open() acquired write access it is dropped now due to
2776d18e9008SMiklos Szeredi 	 * possible mount and symlink following (this might be optimized away if
2777d18e9008SMiklos Szeredi 	 * necessary...)
2778d18e9008SMiklos Szeredi 	 */
277964894cf8SAl Viro 	if (got_write) {
2780d18e9008SMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
278164894cf8SAl Viro 		got_write = false;
2782d18e9008SMiklos Szeredi 	}
2783d18e9008SMiklos Szeredi 
2784fb1cc555SAl Viro 	error = -EEXIST;
2785f8310c59SAl Viro 	if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
2786fb1cc555SAl Viro 		goto exit_dput;
2787fb1cc555SAl Viro 
27889875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
27899875cf80SDavid Howells 	if (error < 0)
2790fb1cc555SAl Viro 		goto exit_dput;
2791fb1cc555SAl Viro 
2792a3fbbde7SAl Viro 	if (error)
2793a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
2794a3fbbde7SAl Viro 
2795decf3400SMiklos Szeredi 	BUG_ON(nd->flags & LOOKUP_RCU);
2796decf3400SMiklos Szeredi 	inode = path->dentry->d_inode;
27975f5daac1SMiklos Szeredi finish_lookup:
27985f5daac1SMiklos Szeredi 	/* we _can_ be in RCU mode here */
2799fb1cc555SAl Viro 	error = -ENOENT;
280054c33e7fSMiklos Szeredi 	if (!inode) {
280154c33e7fSMiklos Szeredi 		path_to_nameidata(path, nd);
28022675a4ebSAl Viro 		goto out;
280354c33e7fSMiklos Szeredi 	}
28049e67f361SAl Viro 
2805d45ea867SMiklos Szeredi 	if (should_follow_link(inode, !symlink_ok)) {
2806d45ea867SMiklos Szeredi 		if (nd->flags & LOOKUP_RCU) {
2807d45ea867SMiklos Szeredi 			if (unlikely(unlazy_walk(nd, path->dentry))) {
2808d45ea867SMiklos Szeredi 				error = -ECHILD;
28092675a4ebSAl Viro 				goto out;
2810d45ea867SMiklos Szeredi 			}
2811d45ea867SMiklos Szeredi 		}
2812d45ea867SMiklos Szeredi 		BUG_ON(inode != path->dentry->d_inode);
28132675a4ebSAl Viro 		return 1;
2814d45ea867SMiklos Szeredi 	}
2815fb1cc555SAl Viro 
281616b1c1cdSMiklos Szeredi 	if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
2817fb1cc555SAl Viro 		path_to_nameidata(path, nd);
281816b1c1cdSMiklos Szeredi 	} else {
281916b1c1cdSMiklos Szeredi 		save_parent.dentry = nd->path.dentry;
282016b1c1cdSMiklos Szeredi 		save_parent.mnt = mntget(path->mnt);
282116b1c1cdSMiklos Szeredi 		nd->path.dentry = path->dentry;
282216b1c1cdSMiklos Szeredi 
282316b1c1cdSMiklos Szeredi 	}
2824decf3400SMiklos Szeredi 	nd->inode = inode;
2825a3fbbde7SAl Viro 	/* Why this, you ask?  _Now_ we might have grown LOOKUP_JUMPED... */
2826bc77daa7SAl Viro finish_open:
2827a3fbbde7SAl Viro 	error = complete_walk(nd);
282816b1c1cdSMiklos Szeredi 	if (error) {
282916b1c1cdSMiklos Szeredi 		path_put(&save_parent);
28302675a4ebSAl Viro 		return error;
283116b1c1cdSMiklos Szeredi 	}
2832bc77daa7SAl Viro 	audit_inode(name, nd->path.dentry, 0);
2833fb1cc555SAl Viro 	error = -EISDIR;
2834050ac841SMiklos Szeredi 	if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
28352675a4ebSAl Viro 		goto out;
2836af2f5542SMiklos Szeredi 	error = -ENOTDIR;
283705252901SAl Viro 	if ((nd->flags & LOOKUP_DIRECTORY) && !can_lookup(nd->inode))
28382675a4ebSAl Viro 		goto out;
28396c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
284077d660a8SMiklos Szeredi 		will_truncate = false;
28416c0d46c4SAl Viro 
28420f9d1a10SAl Viro 	if (will_truncate) {
28430f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
28440f9d1a10SAl Viro 		if (error)
28452675a4ebSAl Viro 			goto out;
284664894cf8SAl Viro 		got_write = true;
28470f9d1a10SAl Viro 	}
2848e83db167SMiklos Szeredi finish_open_created:
2849bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2850ca344a89SAl Viro 	if (error)
28512675a4ebSAl Viro 		goto out;
285230d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
285330d90494SAl Viro 	error = finish_open(file, nd->path.dentry, NULL, opened);
285430d90494SAl Viro 	if (error) {
285530d90494SAl Viro 		if (error == -EOPENSTALE)
2856f60dc3dbSMiklos Szeredi 			goto stale_open;
2857015c3bbcSMiklos Szeredi 		goto out;
2858f60dc3dbSMiklos Szeredi 	}
2859a8277b9bSMiklos Szeredi opened:
28602675a4ebSAl Viro 	error = open_check_o_direct(file);
2861015c3bbcSMiklos Szeredi 	if (error)
2862015c3bbcSMiklos Szeredi 		goto exit_fput;
28632675a4ebSAl Viro 	error = ima_file_check(file, op->acc_mode);
2864aa4caadbSMiklos Szeredi 	if (error)
2865aa4caadbSMiklos Szeredi 		goto exit_fput;
2866aa4caadbSMiklos Szeredi 
28670f9d1a10SAl Viro 	if (will_truncate) {
28682675a4ebSAl Viro 		error = handle_truncate(file);
2869aa4caadbSMiklos Szeredi 		if (error)
2870aa4caadbSMiklos Szeredi 			goto exit_fput;
28710f9d1a10SAl Viro 	}
2872ca344a89SAl Viro out:
287364894cf8SAl Viro 	if (got_write)
28740f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
287516b1c1cdSMiklos Szeredi 	path_put(&save_parent);
2876e276ae67SMiklos Szeredi 	terminate_walk(nd);
28772675a4ebSAl Viro 	return error;
2878fb1cc555SAl Viro 
2879fb1cc555SAl Viro exit_dput:
2880fb1cc555SAl Viro 	path_put_conditional(path, nd);
2881ca344a89SAl Viro 	goto out;
2882015c3bbcSMiklos Szeredi exit_fput:
28832675a4ebSAl Viro 	fput(file);
28842675a4ebSAl Viro 	goto out;
2885015c3bbcSMiklos Szeredi 
2886f60dc3dbSMiklos Szeredi stale_open:
2887f60dc3dbSMiklos Szeredi 	/* If no saved parent or already retried then can't retry */
2888f60dc3dbSMiklos Szeredi 	if (!save_parent.dentry || retried)
2889f60dc3dbSMiklos Szeredi 		goto out;
2890f60dc3dbSMiklos Szeredi 
2891f60dc3dbSMiklos Szeredi 	BUG_ON(save_parent.dentry != dir);
2892f60dc3dbSMiklos Szeredi 	path_put(&nd->path);
2893f60dc3dbSMiklos Szeredi 	nd->path = save_parent;
2894f60dc3dbSMiklos Szeredi 	nd->inode = dir->d_inode;
2895f60dc3dbSMiklos Szeredi 	save_parent.mnt = NULL;
2896f60dc3dbSMiklos Szeredi 	save_parent.dentry = NULL;
289764894cf8SAl Viro 	if (got_write) {
2898f60dc3dbSMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
289964894cf8SAl Viro 		got_write = false;
2900f60dc3dbSMiklos Szeredi 	}
2901f60dc3dbSMiklos Szeredi 	retried = true;
2902f60dc3dbSMiklos Szeredi 	goto retry_lookup;
2903fb1cc555SAl Viro }
2904fb1cc555SAl Viro 
2905669abf4eSJeff Layton static struct file *path_openat(int dfd, struct filename *pathname,
290673d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
29071da177e4SLinus Torvalds {
2908fe2d35ffSAl Viro 	struct file *base = NULL;
290930d90494SAl Viro 	struct file *file;
29109850c056SAl Viro 	struct path path;
291147237687SAl Viro 	int opened = 0;
291213aab428SAl Viro 	int error;
291331e6b01fSNick Piggin 
291430d90494SAl Viro 	file = get_empty_filp();
29151afc99beSAl Viro 	if (IS_ERR(file))
29161afc99beSAl Viro 		return file;
291731e6b01fSNick Piggin 
291830d90494SAl Viro 	file->f_flags = op->open_flag;
291931e6b01fSNick Piggin 
2920669abf4eSJeff Layton 	error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
292131e6b01fSNick Piggin 	if (unlikely(error))
29222675a4ebSAl Viro 		goto out;
292331e6b01fSNick Piggin 
2924fe2d35ffSAl Viro 	current->total_link_count = 0;
2925669abf4eSJeff Layton 	error = link_path_walk(pathname->name, nd);
292631e6b01fSNick Piggin 	if (unlikely(error))
29272675a4ebSAl Viro 		goto out;
29281da177e4SLinus Torvalds 
29292675a4ebSAl Viro 	error = do_last(nd, &path, file, op, &opened, pathname);
29302675a4ebSAl Viro 	while (unlikely(error > 0)) { /* trailing symlink */
29317b9337aaSNick Piggin 		struct path link = path;
2932def4af30SAl Viro 		void *cookie;
2933574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
293473d049a4SAl Viro 			path_put_conditional(&path, nd);
293573d049a4SAl Viro 			path_put(&nd->path);
29362675a4ebSAl Viro 			error = -ELOOP;
293740b39136SAl Viro 			break;
293840b39136SAl Viro 		}
2939800179c9SKees Cook 		error = may_follow_link(&link, nd);
2940800179c9SKees Cook 		if (unlikely(error))
2941800179c9SKees Cook 			break;
294273d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
294373d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2944574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
2945c3e380b0SAl Viro 		if (unlikely(error))
29462675a4ebSAl Viro 			break;
29472675a4ebSAl Viro 		error = do_last(nd, &path, file, op, &opened, pathname);
2948574197e0SAl Viro 		put_link(nd, &link, cookie);
2949806b681cSAl Viro 	}
295010fa8e62SAl Viro out:
295173d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
295273d049a4SAl Viro 		path_put(&nd->root);
2953fe2d35ffSAl Viro 	if (base)
2954fe2d35ffSAl Viro 		fput(base);
29552675a4ebSAl Viro 	if (!(opened & FILE_OPENED)) {
29562675a4ebSAl Viro 		BUG_ON(!error);
295730d90494SAl Viro 		put_filp(file);
2958015c3bbcSMiklos Szeredi 	}
29592675a4ebSAl Viro 	if (unlikely(error)) {
29602675a4ebSAl Viro 		if (error == -EOPENSTALE) {
29612675a4ebSAl Viro 			if (flags & LOOKUP_RCU)
29622675a4ebSAl Viro 				error = -ECHILD;
29632675a4ebSAl Viro 			else
29642675a4ebSAl Viro 				error = -ESTALE;
29652675a4ebSAl Viro 		}
29662675a4ebSAl Viro 		file = ERR_PTR(error);
29672675a4ebSAl Viro 	}
29682675a4ebSAl Viro 	return file;
2969de459215SKirill Korotaev }
29701da177e4SLinus Torvalds 
2971669abf4eSJeff Layton struct file *do_filp_open(int dfd, struct filename *pathname,
297213aab428SAl Viro 		const struct open_flags *op, int flags)
297313aab428SAl Viro {
297473d049a4SAl Viro 	struct nameidata nd;
297513aab428SAl Viro 	struct file *filp;
297613aab428SAl Viro 
297773d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
297813aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
297973d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
298013aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
298173d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
298213aab428SAl Viro 	return filp;
298313aab428SAl Viro }
298413aab428SAl Viro 
298573d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
298673d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
298773d049a4SAl Viro {
298873d049a4SAl Viro 	struct nameidata nd;
298973d049a4SAl Viro 	struct file *file;
2990669abf4eSJeff Layton 	struct filename filename = { .name = name };
299173d049a4SAl Viro 
299273d049a4SAl Viro 	nd.root.mnt = mnt;
299373d049a4SAl Viro 	nd.root.dentry = dentry;
299473d049a4SAl Viro 
299573d049a4SAl Viro 	flags |= LOOKUP_ROOT;
299673d049a4SAl Viro 
2997bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
299873d049a4SAl Viro 		return ERR_PTR(-ELOOP);
299973d049a4SAl Viro 
3000669abf4eSJeff Layton 	file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
300173d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
3002669abf4eSJeff Layton 		file = path_openat(-1, &filename, &nd, op, flags);
300373d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
3004669abf4eSJeff Layton 		file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
300573d049a4SAl Viro 	return file;
300673d049a4SAl Viro }
300773d049a4SAl Viro 
30081ac12b4bSJeff Layton struct dentry *kern_path_create(int dfd, const char *pathname,
30091ac12b4bSJeff Layton 				struct path *path, unsigned int lookup_flags)
30101da177e4SLinus Torvalds {
3011c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
3012ed75e95dSAl Viro 	struct nameidata nd;
3013c30dabfeSJan Kara 	int err2;
30141ac12b4bSJeff Layton 	int error;
30151ac12b4bSJeff Layton 	bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
30161ac12b4bSJeff Layton 
30171ac12b4bSJeff Layton 	/*
30181ac12b4bSJeff Layton 	 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
30191ac12b4bSJeff Layton 	 * other flags passed in are ignored!
30201ac12b4bSJeff Layton 	 */
30211ac12b4bSJeff Layton 	lookup_flags &= LOOKUP_REVAL;
30221ac12b4bSJeff Layton 
30231ac12b4bSJeff Layton 	error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
3024ed75e95dSAl Viro 	if (error)
3025ed75e95dSAl Viro 		return ERR_PTR(error);
30261da177e4SLinus Torvalds 
3027c663e5d8SChristoph Hellwig 	/*
3028c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
3029c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
3030c663e5d8SChristoph Hellwig 	 */
3031ed75e95dSAl Viro 	if (nd.last_type != LAST_NORM)
3032ed75e95dSAl Viro 		goto out;
3033ed75e95dSAl Viro 	nd.flags &= ~LOOKUP_PARENT;
3034ed75e95dSAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3035c663e5d8SChristoph Hellwig 
3036c30dabfeSJan Kara 	/* don't fail immediately if it's r/o, at least try to report other errors */
3037c30dabfeSJan Kara 	err2 = mnt_want_write(nd.path.mnt);
3038c663e5d8SChristoph Hellwig 	/*
3039c663e5d8SChristoph Hellwig 	 * Do the final lookup.
3040c663e5d8SChristoph Hellwig 	 */
3041ed75e95dSAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3042ed75e95dSAl Viro 	dentry = lookup_hash(&nd);
30431da177e4SLinus Torvalds 	if (IS_ERR(dentry))
3044a8104a9fSAl Viro 		goto unlock;
3045c663e5d8SChristoph Hellwig 
3046a8104a9fSAl Viro 	error = -EEXIST;
3047e9baf6e5SAl Viro 	if (dentry->d_inode)
3048a8104a9fSAl Viro 		goto fail;
3049c663e5d8SChristoph Hellwig 	/*
3050c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
3051c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
3052c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
3053c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
3054c663e5d8SChristoph Hellwig 	 */
3055ed75e95dSAl Viro 	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
3056a8104a9fSAl Viro 		error = -ENOENT;
3057ed75e95dSAl Viro 		goto fail;
3058e9baf6e5SAl Viro 	}
3059c30dabfeSJan Kara 	if (unlikely(err2)) {
3060c30dabfeSJan Kara 		error = err2;
3061a8104a9fSAl Viro 		goto fail;
3062c30dabfeSJan Kara 	}
3063ed75e95dSAl Viro 	*path = nd.path;
3064e9baf6e5SAl Viro 	return dentry;
30651da177e4SLinus Torvalds fail:
3066a8104a9fSAl Viro 	dput(dentry);
3067a8104a9fSAl Viro 	dentry = ERR_PTR(error);
3068a8104a9fSAl Viro unlock:
3069dae6ad8fSAl Viro 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3070c30dabfeSJan Kara 	if (!err2)
3071c30dabfeSJan Kara 		mnt_drop_write(nd.path.mnt);
3072ed75e95dSAl Viro out:
3073dae6ad8fSAl Viro 	path_put(&nd.path);
3074ed75e95dSAl Viro 	return dentry;
3075dae6ad8fSAl Viro }
3076dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
3077dae6ad8fSAl Viro 
3078921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry)
3079921a1650SAl Viro {
3080921a1650SAl Viro 	dput(dentry);
3081921a1650SAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
3082a8104a9fSAl Viro 	mnt_drop_write(path->mnt);
3083921a1650SAl Viro 	path_put(path);
3084921a1650SAl Viro }
3085921a1650SAl Viro EXPORT_SYMBOL(done_path_create);
3086921a1650SAl Viro 
30871ac12b4bSJeff Layton struct dentry *user_path_create(int dfd, const char __user *pathname,
30881ac12b4bSJeff Layton 				struct path *path, unsigned int lookup_flags)
3089dae6ad8fSAl Viro {
309091a27b2aSJeff Layton 	struct filename *tmp = getname(pathname);
3091dae6ad8fSAl Viro 	struct dentry *res;
3092dae6ad8fSAl Viro 	if (IS_ERR(tmp))
3093dae6ad8fSAl Viro 		return ERR_CAST(tmp);
30941ac12b4bSJeff Layton 	res = kern_path_create(dfd, tmp->name, path, lookup_flags);
3095dae6ad8fSAl Viro 	putname(tmp);
3096dae6ad8fSAl Viro 	return res;
3097dae6ad8fSAl Viro }
3098dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
3099dae6ad8fSAl Viro 
31001a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
31011da177e4SLinus Torvalds {
3102a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
31031da177e4SLinus Torvalds 
31041da177e4SLinus Torvalds 	if (error)
31051da177e4SLinus Torvalds 		return error;
31061da177e4SLinus Torvalds 
3107975d6b39SEric W. Biederman 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
31081da177e4SLinus Torvalds 		return -EPERM;
31091da177e4SLinus Torvalds 
3110acfa4380SAl Viro 	if (!dir->i_op->mknod)
31111da177e4SLinus Torvalds 		return -EPERM;
31121da177e4SLinus Torvalds 
311308ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
311408ce5f16SSerge E. Hallyn 	if (error)
311508ce5f16SSerge E. Hallyn 		return error;
311608ce5f16SSerge E. Hallyn 
31171da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
31181da177e4SLinus Torvalds 	if (error)
31191da177e4SLinus Torvalds 		return error;
31201da177e4SLinus Torvalds 
31211da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
3122a74574aaSStephen Smalley 	if (!error)
3123f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
31241da177e4SLinus Torvalds 	return error;
31251da177e4SLinus Torvalds }
31261da177e4SLinus Torvalds 
3127f69aac00SAl Viro static int may_mknod(umode_t mode)
3128463c3197SDave Hansen {
3129463c3197SDave Hansen 	switch (mode & S_IFMT) {
3130463c3197SDave Hansen 	case S_IFREG:
3131463c3197SDave Hansen 	case S_IFCHR:
3132463c3197SDave Hansen 	case S_IFBLK:
3133463c3197SDave Hansen 	case S_IFIFO:
3134463c3197SDave Hansen 	case S_IFSOCK:
3135463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
3136463c3197SDave Hansen 		return 0;
3137463c3197SDave Hansen 	case S_IFDIR:
3138463c3197SDave Hansen 		return -EPERM;
3139463c3197SDave Hansen 	default:
3140463c3197SDave Hansen 		return -EINVAL;
3141463c3197SDave Hansen 	}
3142463c3197SDave Hansen }
3143463c3197SDave Hansen 
31448208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
31452e4d0924SHeiko Carstens 		unsigned, dev)
31461da177e4SLinus Torvalds {
31471da177e4SLinus Torvalds 	struct dentry *dentry;
3148dae6ad8fSAl Viro 	struct path path;
3149dae6ad8fSAl Viro 	int error;
3150972567f1SJeff Layton 	unsigned int lookup_flags = 0;
31511da177e4SLinus Torvalds 
31528e4bfca1SAl Viro 	error = may_mknod(mode);
31538e4bfca1SAl Viro 	if (error)
31548e4bfca1SAl Viro 		return error;
3155972567f1SJeff Layton retry:
3156972567f1SJeff Layton 	dentry = user_path_create(dfd, filename, &path, lookup_flags);
3157dae6ad8fSAl Viro 	if (IS_ERR(dentry))
3158dae6ad8fSAl Viro 		return PTR_ERR(dentry);
31592ad94ae6SAl Viro 
3160dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3161ce3b0f8dSAl Viro 		mode &= ~current_umask();
3162dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
3163be6d3e56SKentaro Takeda 	if (error)
3164a8104a9fSAl Viro 		goto out;
31651da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
31661da177e4SLinus Torvalds 		case 0: case S_IFREG:
3167312b63fbSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,true);
31681da177e4SLinus Torvalds 			break;
31691da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
3170dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
31711da177e4SLinus Torvalds 					new_decode_dev(dev));
31721da177e4SLinus Torvalds 			break;
31731da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
3174dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
31751da177e4SLinus Torvalds 			break;
31761da177e4SLinus Torvalds 	}
3177a8104a9fSAl Viro out:
3178921a1650SAl Viro 	done_path_create(&path, dentry);
3179972567f1SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3180972567f1SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3181972567f1SJeff Layton 		goto retry;
3182972567f1SJeff Layton 	}
31831da177e4SLinus Torvalds 	return error;
31841da177e4SLinus Torvalds }
31851da177e4SLinus Torvalds 
31868208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
31875590ff0dSUlrich Drepper {
31885590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
31895590ff0dSUlrich Drepper }
31905590ff0dSUlrich Drepper 
319118bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
31921da177e4SLinus Torvalds {
3193a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
31948de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
31951da177e4SLinus Torvalds 
31961da177e4SLinus Torvalds 	if (error)
31971da177e4SLinus Torvalds 		return error;
31981da177e4SLinus Torvalds 
3199acfa4380SAl Viro 	if (!dir->i_op->mkdir)
32001da177e4SLinus Torvalds 		return -EPERM;
32011da177e4SLinus Torvalds 
32021da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
32031da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
32041da177e4SLinus Torvalds 	if (error)
32051da177e4SLinus Torvalds 		return error;
32061da177e4SLinus Torvalds 
32078de52778SAl Viro 	if (max_links && dir->i_nlink >= max_links)
32088de52778SAl Viro 		return -EMLINK;
32098de52778SAl Viro 
32101da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
3211a74574aaSStephen Smalley 	if (!error)
3212f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
32131da177e4SLinus Torvalds 	return error;
32141da177e4SLinus Torvalds }
32151da177e4SLinus Torvalds 
3216a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
32171da177e4SLinus Torvalds {
32186902d925SDave Hansen 	struct dentry *dentry;
3219dae6ad8fSAl Viro 	struct path path;
3220dae6ad8fSAl Viro 	int error;
3221b76d8b82SJeff Layton 	unsigned int lookup_flags = LOOKUP_DIRECTORY;
32221da177e4SLinus Torvalds 
3223b76d8b82SJeff Layton retry:
3224b76d8b82SJeff Layton 	dentry = user_path_create(dfd, pathname, &path, lookup_flags);
32256902d925SDave Hansen 	if (IS_ERR(dentry))
3226dae6ad8fSAl Viro 		return PTR_ERR(dentry);
32276902d925SDave Hansen 
3228dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3229ce3b0f8dSAl Viro 		mode &= ~current_umask();
3230dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
3231a8104a9fSAl Viro 	if (!error)
3232dae6ad8fSAl Viro 		error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3233921a1650SAl Viro 	done_path_create(&path, dentry);
3234b76d8b82SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3235b76d8b82SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3236b76d8b82SJeff Layton 		goto retry;
3237b76d8b82SJeff Layton 	}
32381da177e4SLinus Torvalds 	return error;
32391da177e4SLinus Torvalds }
32401da177e4SLinus Torvalds 
3241a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
32425590ff0dSUlrich Drepper {
32435590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
32445590ff0dSUlrich Drepper }
32455590ff0dSUlrich Drepper 
32461da177e4SLinus Torvalds /*
3247a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
3248c0d02594SJ. Bruce Fields  * should have a usage count of 1 if we're the only user of this
3249a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
3250a71905f0SSage Weil  * then we drop the dentry now.
32511da177e4SLinus Torvalds  *
32521da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
32531da177e4SLinus Torvalds  * do a
32541da177e4SLinus Torvalds  *
32551da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
32561da177e4SLinus Torvalds  *		return -EBUSY;
32571da177e4SLinus Torvalds  *
32581da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
32591da177e4SLinus Torvalds  * that is still in use by something else..
32601da177e4SLinus Torvalds  */
32611da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
32621da177e4SLinus Torvalds {
32631da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
32641da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
326564252c75SSage Weil 	if (dentry->d_count == 1)
32661da177e4SLinus Torvalds 		__d_drop(dentry);
32671da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
32681da177e4SLinus Torvalds }
32691da177e4SLinus Torvalds 
32701da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
32711da177e4SLinus Torvalds {
32721da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
32731da177e4SLinus Torvalds 
32741da177e4SLinus Torvalds 	if (error)
32751da177e4SLinus Torvalds 		return error;
32761da177e4SLinus Torvalds 
3277acfa4380SAl Viro 	if (!dir->i_op->rmdir)
32781da177e4SLinus Torvalds 		return -EPERM;
32791da177e4SLinus Torvalds 
32801d2ef590SAl Viro 	dget(dentry);
32811b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
3282912dbc15SSage Weil 
32831da177e4SLinus Torvalds 	error = -EBUSY;
3284912dbc15SSage Weil 	if (d_mountpoint(dentry))
3285912dbc15SSage Weil 		goto out;
3286912dbc15SSage Weil 
32871da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
3288912dbc15SSage Weil 	if (error)
3289912dbc15SSage Weil 		goto out;
3290912dbc15SSage Weil 
32913cebde24SSage Weil 	shrink_dcache_parent(dentry);
32921da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
3293912dbc15SSage Weil 	if (error)
3294912dbc15SSage Weil 		goto out;
3295912dbc15SSage Weil 
32961da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
3297d83c49f3SAl Viro 	dont_mount(dentry);
32981da177e4SLinus Torvalds 
3299912dbc15SSage Weil out:
3300912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
33011d2ef590SAl Viro 	dput(dentry);
3302912dbc15SSage Weil 	if (!error)
3303912dbc15SSage Weil 		d_delete(dentry);
33041da177e4SLinus Torvalds 	return error;
33051da177e4SLinus Torvalds }
33061da177e4SLinus Torvalds 
33075590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
33081da177e4SLinus Torvalds {
33091da177e4SLinus Torvalds 	int error = 0;
331091a27b2aSJeff Layton 	struct filename *name;
33111da177e4SLinus Torvalds 	struct dentry *dentry;
33121da177e4SLinus Torvalds 	struct nameidata nd;
3313c6ee9206SJeff Layton 	unsigned int lookup_flags = 0;
3314c6ee9206SJeff Layton retry:
3315c6ee9206SJeff Layton 	name = user_path_parent(dfd, pathname, &nd, lookup_flags);
331691a27b2aSJeff Layton 	if (IS_ERR(name))
331791a27b2aSJeff Layton 		return PTR_ERR(name);
33181da177e4SLinus Torvalds 
33191da177e4SLinus Torvalds 	switch(nd.last_type) {
33201da177e4SLinus Torvalds 	case LAST_DOTDOT:
33211da177e4SLinus Torvalds 		error = -ENOTEMPTY;
33221da177e4SLinus Torvalds 		goto exit1;
33231da177e4SLinus Torvalds 	case LAST_DOT:
33241da177e4SLinus Torvalds 		error = -EINVAL;
33251da177e4SLinus Torvalds 		goto exit1;
33261da177e4SLinus Torvalds 	case LAST_ROOT:
33271da177e4SLinus Torvalds 		error = -EBUSY;
33281da177e4SLinus Torvalds 		goto exit1;
33291da177e4SLinus Torvalds 	}
33300612d9fbSOGAWA Hirofumi 
33310612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
3332c30dabfeSJan Kara 	error = mnt_want_write(nd.path.mnt);
3333c30dabfeSJan Kara 	if (error)
3334c30dabfeSJan Kara 		goto exit1;
33350612d9fbSOGAWA Hirofumi 
33364ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
333749705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
33381da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
33396902d925SDave Hansen 	if (IS_ERR(dentry))
33406902d925SDave Hansen 		goto exit2;
3341e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
3342e6bc45d6STheodore Ts'o 		error = -ENOENT;
3343e6bc45d6STheodore Ts'o 		goto exit3;
3344e6bc45d6STheodore Ts'o 	}
3345be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
3346be6d3e56SKentaro Takeda 	if (error)
3347c30dabfeSJan Kara 		goto exit3;
33484ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
33490622753bSDave Hansen exit3:
33501da177e4SLinus Torvalds 	dput(dentry);
33516902d925SDave Hansen exit2:
33524ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3353c30dabfeSJan Kara 	mnt_drop_write(nd.path.mnt);
33541da177e4SLinus Torvalds exit1:
33551d957f9bSJan Blunck 	path_put(&nd.path);
33561da177e4SLinus Torvalds 	putname(name);
3357c6ee9206SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3358c6ee9206SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3359c6ee9206SJeff Layton 		goto retry;
3360c6ee9206SJeff Layton 	}
33611da177e4SLinus Torvalds 	return error;
33621da177e4SLinus Torvalds }
33631da177e4SLinus Torvalds 
33643cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
33655590ff0dSUlrich Drepper {
33665590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
33675590ff0dSUlrich Drepper }
33685590ff0dSUlrich Drepper 
33691da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
33701da177e4SLinus Torvalds {
33711da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
33721da177e4SLinus Torvalds 
33731da177e4SLinus Torvalds 	if (error)
33741da177e4SLinus Torvalds 		return error;
33751da177e4SLinus Torvalds 
3376acfa4380SAl Viro 	if (!dir->i_op->unlink)
33771da177e4SLinus Torvalds 		return -EPERM;
33781da177e4SLinus Torvalds 
33791b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
33801da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
33811da177e4SLinus Torvalds 		error = -EBUSY;
33821da177e4SLinus Torvalds 	else {
33831da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
3384bec1052eSAl Viro 		if (!error) {
33851da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
3386bec1052eSAl Viro 			if (!error)
3387d83c49f3SAl Viro 				dont_mount(dentry);
3388bec1052eSAl Viro 		}
33891da177e4SLinus Torvalds 	}
33901b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
33911da177e4SLinus Torvalds 
33921da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
33931da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
3394ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
33951da177e4SLinus Torvalds 		d_delete(dentry);
33961da177e4SLinus Torvalds 	}
33970eeca283SRobert Love 
33981da177e4SLinus Torvalds 	return error;
33991da177e4SLinus Torvalds }
34001da177e4SLinus Torvalds 
34011da177e4SLinus Torvalds /*
34021da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
34031b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
34041da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
34051da177e4SLinus Torvalds  * while waiting on the I/O.
34061da177e4SLinus Torvalds  */
34075590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
34081da177e4SLinus Torvalds {
34092ad94ae6SAl Viro 	int error;
341091a27b2aSJeff Layton 	struct filename *name;
34111da177e4SLinus Torvalds 	struct dentry *dentry;
34121da177e4SLinus Torvalds 	struct nameidata nd;
34131da177e4SLinus Torvalds 	struct inode *inode = NULL;
34145d18f813SJeff Layton 	unsigned int lookup_flags = 0;
34155d18f813SJeff Layton retry:
34165d18f813SJeff Layton 	name = user_path_parent(dfd, pathname, &nd, lookup_flags);
341791a27b2aSJeff Layton 	if (IS_ERR(name))
341891a27b2aSJeff Layton 		return PTR_ERR(name);
34192ad94ae6SAl Viro 
34201da177e4SLinus Torvalds 	error = -EISDIR;
34211da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
34221da177e4SLinus Torvalds 		goto exit1;
34230612d9fbSOGAWA Hirofumi 
34240612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
3425c30dabfeSJan Kara 	error = mnt_want_write(nd.path.mnt);
3426c30dabfeSJan Kara 	if (error)
3427c30dabfeSJan Kara 		goto exit1;
34280612d9fbSOGAWA Hirofumi 
34294ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
343049705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
34311da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
34321da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
34331da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
343450338b88STörök Edwin 		if (nd.last.name[nd.last.len])
343550338b88STörök Edwin 			goto slashes;
34361da177e4SLinus Torvalds 		inode = dentry->d_inode;
343750338b88STörök Edwin 		if (!inode)
3438e6bc45d6STheodore Ts'o 			goto slashes;
34397de9c6eeSAl Viro 		ihold(inode);
3440be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
3441be6d3e56SKentaro Takeda 		if (error)
3442c30dabfeSJan Kara 			goto exit2;
34434ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
34441da177e4SLinus Torvalds exit2:
34451da177e4SLinus Torvalds 		dput(dentry);
34461da177e4SLinus Torvalds 	}
34474ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
34481da177e4SLinus Torvalds 	if (inode)
34491da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
3450c30dabfeSJan Kara 	mnt_drop_write(nd.path.mnt);
34511da177e4SLinus Torvalds exit1:
34521d957f9bSJan Blunck 	path_put(&nd.path);
34531da177e4SLinus Torvalds 	putname(name);
34545d18f813SJeff Layton 	if (retry_estale(error, lookup_flags)) {
34555d18f813SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
34565d18f813SJeff Layton 		inode = NULL;
34575d18f813SJeff Layton 		goto retry;
34585d18f813SJeff Layton 	}
34591da177e4SLinus Torvalds 	return error;
34601da177e4SLinus Torvalds 
34611da177e4SLinus Torvalds slashes:
34621da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
34631da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
34641da177e4SLinus Torvalds 	goto exit2;
34651da177e4SLinus Torvalds }
34661da177e4SLinus Torvalds 
34672e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
34685590ff0dSUlrich Drepper {
34695590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
34705590ff0dSUlrich Drepper 		return -EINVAL;
34715590ff0dSUlrich Drepper 
34725590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
34735590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
34745590ff0dSUlrich Drepper 
34755590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
34765590ff0dSUlrich Drepper }
34775590ff0dSUlrich Drepper 
34783480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
34795590ff0dSUlrich Drepper {
34805590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
34815590ff0dSUlrich Drepper }
34825590ff0dSUlrich Drepper 
3483db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
34841da177e4SLinus Torvalds {
3485a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
34861da177e4SLinus Torvalds 
34871da177e4SLinus Torvalds 	if (error)
34881da177e4SLinus Torvalds 		return error;
34891da177e4SLinus Torvalds 
3490acfa4380SAl Viro 	if (!dir->i_op->symlink)
34911da177e4SLinus Torvalds 		return -EPERM;
34921da177e4SLinus Torvalds 
34931da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
34941da177e4SLinus Torvalds 	if (error)
34951da177e4SLinus Torvalds 		return error;
34961da177e4SLinus Torvalds 
34971da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
3498a74574aaSStephen Smalley 	if (!error)
3499f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
35001da177e4SLinus Torvalds 	return error;
35011da177e4SLinus Torvalds }
35021da177e4SLinus Torvalds 
35032e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
35042e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
35051da177e4SLinus Torvalds {
35062ad94ae6SAl Viro 	int error;
350791a27b2aSJeff Layton 	struct filename *from;
35086902d925SDave Hansen 	struct dentry *dentry;
3509dae6ad8fSAl Viro 	struct path path;
3510f46d3567SJeff Layton 	unsigned int lookup_flags = 0;
35111da177e4SLinus Torvalds 
35121da177e4SLinus Torvalds 	from = getname(oldname);
35131da177e4SLinus Torvalds 	if (IS_ERR(from))
35141da177e4SLinus Torvalds 		return PTR_ERR(from);
3515f46d3567SJeff Layton retry:
3516f46d3567SJeff Layton 	dentry = user_path_create(newdfd, newname, &path, lookup_flags);
35171da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
35186902d925SDave Hansen 	if (IS_ERR(dentry))
3519dae6ad8fSAl Viro 		goto out_putname;
35206902d925SDave Hansen 
352191a27b2aSJeff Layton 	error = security_path_symlink(&path, dentry, from->name);
3522a8104a9fSAl Viro 	if (!error)
352391a27b2aSJeff Layton 		error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
3524921a1650SAl Viro 	done_path_create(&path, dentry);
3525f46d3567SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3526f46d3567SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3527f46d3567SJeff Layton 		goto retry;
3528f46d3567SJeff Layton 	}
35296902d925SDave Hansen out_putname:
35301da177e4SLinus Torvalds 	putname(from);
35311da177e4SLinus Torvalds 	return error;
35321da177e4SLinus Torvalds }
35331da177e4SLinus Torvalds 
35343480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
35355590ff0dSUlrich Drepper {
35365590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
35375590ff0dSUlrich Drepper }
35385590ff0dSUlrich Drepper 
35391da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
35401da177e4SLinus Torvalds {
35411da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
35428de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
35431da177e4SLinus Torvalds 	int error;
35441da177e4SLinus Torvalds 
35451da177e4SLinus Torvalds 	if (!inode)
35461da177e4SLinus Torvalds 		return -ENOENT;
35471da177e4SLinus Torvalds 
3548a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
35491da177e4SLinus Torvalds 	if (error)
35501da177e4SLinus Torvalds 		return error;
35511da177e4SLinus Torvalds 
35521da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
35531da177e4SLinus Torvalds 		return -EXDEV;
35541da177e4SLinus Torvalds 
35551da177e4SLinus Torvalds 	/*
35561da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
35571da177e4SLinus Torvalds 	 */
35581da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
35591da177e4SLinus Torvalds 		return -EPERM;
3560acfa4380SAl Viro 	if (!dir->i_op->link)
35611da177e4SLinus Torvalds 		return -EPERM;
35627e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
35631da177e4SLinus Torvalds 		return -EPERM;
35641da177e4SLinus Torvalds 
35651da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
35661da177e4SLinus Torvalds 	if (error)
35671da177e4SLinus Torvalds 		return error;
35681da177e4SLinus Torvalds 
35697e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
3570aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
3571aae8a97dSAneesh Kumar K.V 	if (inode->i_nlink == 0)
3572aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
35738de52778SAl Viro 	else if (max_links && inode->i_nlink >= max_links)
35748de52778SAl Viro 		error = -EMLINK;
3575aae8a97dSAneesh Kumar K.V 	else
35761da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
35777e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
3578e31e14ecSStephen Smalley 	if (!error)
35797e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
35801da177e4SLinus Torvalds 	return error;
35811da177e4SLinus Torvalds }
35821da177e4SLinus Torvalds 
35831da177e4SLinus Torvalds /*
35841da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
35851da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
35861da177e4SLinus Torvalds  * newname.  --KAB
35871da177e4SLinus Torvalds  *
35881da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
35891da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
35901da177e4SLinus Torvalds  * and other special files.  --ADM
35911da177e4SLinus Torvalds  */
35922e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
35932e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
35941da177e4SLinus Torvalds {
35951da177e4SLinus Torvalds 	struct dentry *new_dentry;
3596dae6ad8fSAl Viro 	struct path old_path, new_path;
359711a7b371SAneesh Kumar K.V 	int how = 0;
35981da177e4SLinus Torvalds 	int error;
35991da177e4SLinus Torvalds 
360011a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3601c04030e1SUlrich Drepper 		return -EINVAL;
360211a7b371SAneesh Kumar K.V 	/*
360311a7b371SAneesh Kumar K.V 	 * To use null names we require CAP_DAC_READ_SEARCH
360411a7b371SAneesh Kumar K.V 	 * This ensures that not everyone will be able to create
360511a7b371SAneesh Kumar K.V 	 * handlink using the passed filedescriptor.
360611a7b371SAneesh Kumar K.V 	 */
360711a7b371SAneesh Kumar K.V 	if (flags & AT_EMPTY_PATH) {
360811a7b371SAneesh Kumar K.V 		if (!capable(CAP_DAC_READ_SEARCH))
360911a7b371SAneesh Kumar K.V 			return -ENOENT;
361011a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
361111a7b371SAneesh Kumar K.V 	}
3612c04030e1SUlrich Drepper 
361311a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
361411a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
3615442e31caSJeff Layton retry:
361611a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
36171da177e4SLinus Torvalds 	if (error)
36182ad94ae6SAl Viro 		return error;
36192ad94ae6SAl Viro 
3620442e31caSJeff Layton 	new_dentry = user_path_create(newdfd, newname, &new_path,
3621442e31caSJeff Layton 					(how & LOOKUP_REVAL));
36221da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
36236902d925SDave Hansen 	if (IS_ERR(new_dentry))
3624dae6ad8fSAl Viro 		goto out;
3625dae6ad8fSAl Viro 
3626dae6ad8fSAl Viro 	error = -EXDEV;
3627dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
3628dae6ad8fSAl Viro 		goto out_dput;
3629800179c9SKees Cook 	error = may_linkat(&old_path);
3630800179c9SKees Cook 	if (unlikely(error))
3631800179c9SKees Cook 		goto out_dput;
3632dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
3633be6d3e56SKentaro Takeda 	if (error)
3634a8104a9fSAl Viro 		goto out_dput;
3635dae6ad8fSAl Viro 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
363675c3f29dSDave Hansen out_dput:
3637921a1650SAl Viro 	done_path_create(&new_path, new_dentry);
3638442e31caSJeff Layton 	if (retry_estale(error, how)) {
3639442e31caSJeff Layton 		how |= LOOKUP_REVAL;
3640442e31caSJeff Layton 		goto retry;
3641442e31caSJeff Layton 	}
36421da177e4SLinus Torvalds out:
36432d8f3038SAl Viro 	path_put(&old_path);
36441da177e4SLinus Torvalds 
36451da177e4SLinus Torvalds 	return error;
36461da177e4SLinus Torvalds }
36471da177e4SLinus Torvalds 
36483480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
36495590ff0dSUlrich Drepper {
3650c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
36515590ff0dSUlrich Drepper }
36525590ff0dSUlrich Drepper 
36531da177e4SLinus Torvalds /*
36541da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
36551da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
36561da177e4SLinus Torvalds  * Problems:
36571da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
36581da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
36591da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
3660a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
36611da177e4SLinus Torvalds  *	   story.
36621da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
36631b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
36641da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
36651da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
3666a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
36671da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
36681da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
36691da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
3670a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
36711da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
36721da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
36731da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
3674e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
36751b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
36761da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3677c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
36781da177e4SLinus Torvalds  *	   locking].
36791da177e4SLinus Torvalds  */
368075c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
36811da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
36821da177e4SLinus Torvalds {
36831da177e4SLinus Torvalds 	int error = 0;
36849055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
36858de52778SAl Viro 	unsigned max_links = new_dir->i_sb->s_max_links;
36861da177e4SLinus Torvalds 
36871da177e4SLinus Torvalds 	/*
36881da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
36891da177e4SLinus Torvalds 	 * we'll need to flip '..'.
36901da177e4SLinus Torvalds 	 */
36911da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3692f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
36931da177e4SLinus Torvalds 		if (error)
36941da177e4SLinus Torvalds 			return error;
36951da177e4SLinus Torvalds 	}
36961da177e4SLinus Torvalds 
36971da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
36981da177e4SLinus Torvalds 	if (error)
36991da177e4SLinus Torvalds 		return error;
37001da177e4SLinus Torvalds 
37011d2ef590SAl Viro 	dget(new_dentry);
3702d83c49f3SAl Viro 	if (target)
37031b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
37049055cba7SSage Weil 
37051da177e4SLinus Torvalds 	error = -EBUSY;
37069055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
37079055cba7SSage Weil 		goto out;
37089055cba7SSage Weil 
37098de52778SAl Viro 	error = -EMLINK;
37108de52778SAl Viro 	if (max_links && !target && new_dir != old_dir &&
37118de52778SAl Viro 	    new_dir->i_nlink >= max_links)
37128de52778SAl Viro 		goto out;
37138de52778SAl Viro 
37143cebde24SSage Weil 	if (target)
37153cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
37161da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
37179055cba7SSage Weil 	if (error)
37189055cba7SSage Weil 		goto out;
37199055cba7SSage Weil 
37201da177e4SLinus Torvalds 	if (target) {
37211da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
3722d83c49f3SAl Viro 		dont_mount(new_dentry);
3723d83c49f3SAl Viro 	}
37249055cba7SSage Weil out:
37259055cba7SSage Weil 	if (target)
37261b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
37271d2ef590SAl Viro 	dput(new_dentry);
3728e31e14ecSStephen Smalley 	if (!error)
3729349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
37301da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
37311da177e4SLinus Torvalds 	return error;
37321da177e4SLinus Torvalds }
37331da177e4SLinus Torvalds 
373475c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
37351da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
37361da177e4SLinus Torvalds {
373751892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
37381da177e4SLinus Torvalds 	int error;
37391da177e4SLinus Torvalds 
37401da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
37411da177e4SLinus Torvalds 	if (error)
37421da177e4SLinus Torvalds 		return error;
37431da177e4SLinus Torvalds 
37441da177e4SLinus Torvalds 	dget(new_dentry);
37451da177e4SLinus Torvalds 	if (target)
37461b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
374751892bbbSSage Weil 
37481da177e4SLinus Torvalds 	error = -EBUSY;
374951892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
375051892bbbSSage Weil 		goto out;
375151892bbbSSage Weil 
37521da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
375351892bbbSSage Weil 	if (error)
375451892bbbSSage Weil 		goto out;
375551892bbbSSage Weil 
3756bec1052eSAl Viro 	if (target)
3757d83c49f3SAl Viro 		dont_mount(new_dentry);
3758349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
37591da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
376051892bbbSSage Weil out:
37611da177e4SLinus Torvalds 	if (target)
37621b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
37631da177e4SLinus Torvalds 	dput(new_dentry);
37641da177e4SLinus Torvalds 	return error;
37651da177e4SLinus Torvalds }
37661da177e4SLinus Torvalds 
37671da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
37681da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
37691da177e4SLinus Torvalds {
37701da177e4SLinus Torvalds 	int error;
37711da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
377259b0df21SEric Paris 	const unsigned char *old_name;
37731da177e4SLinus Torvalds 
37741da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
37751da177e4SLinus Torvalds  		return 0;
37761da177e4SLinus Torvalds 
37771da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
37781da177e4SLinus Torvalds 	if (error)
37791da177e4SLinus Torvalds 		return error;
37801da177e4SLinus Torvalds 
37811da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3782a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
37831da177e4SLinus Torvalds 	else
37841da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
37851da177e4SLinus Torvalds 	if (error)
37861da177e4SLinus Torvalds 		return error;
37871da177e4SLinus Torvalds 
3788acfa4380SAl Viro 	if (!old_dir->i_op->rename)
37891da177e4SLinus Torvalds 		return -EPERM;
37901da177e4SLinus Torvalds 
37910eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
37920eeca283SRobert Love 
37931da177e4SLinus Torvalds 	if (is_dir)
37941da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
37951da177e4SLinus Torvalds 	else
37961da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3797123df294SAl Viro 	if (!error)
3798123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
37995a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
38000eeca283SRobert Love 	fsnotify_oldname_free(old_name);
38010eeca283SRobert Love 
38021da177e4SLinus Torvalds 	return error;
38031da177e4SLinus Torvalds }
38041da177e4SLinus Torvalds 
38052e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
38062e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
38071da177e4SLinus Torvalds {
38081da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
38091da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
38101da177e4SLinus Torvalds 	struct dentry *trap;
38111da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
381291a27b2aSJeff Layton 	struct filename *from;
381391a27b2aSJeff Layton 	struct filename *to;
3814c6a94284SJeff Layton 	unsigned int lookup_flags = 0;
3815c6a94284SJeff Layton 	bool should_retry = false;
38162ad94ae6SAl Viro 	int error;
3817c6a94284SJeff Layton retry:
3818c6a94284SJeff Layton 	from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
381991a27b2aSJeff Layton 	if (IS_ERR(from)) {
382091a27b2aSJeff Layton 		error = PTR_ERR(from);
38211da177e4SLinus Torvalds 		goto exit;
382291a27b2aSJeff Layton 	}
38231da177e4SLinus Torvalds 
3824c6a94284SJeff Layton 	to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
382591a27b2aSJeff Layton 	if (IS_ERR(to)) {
382691a27b2aSJeff Layton 		error = PTR_ERR(to);
38271da177e4SLinus Torvalds 		goto exit1;
382891a27b2aSJeff Layton 	}
38291da177e4SLinus Torvalds 
38301da177e4SLinus Torvalds 	error = -EXDEV;
38314ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
38321da177e4SLinus Torvalds 		goto exit2;
38331da177e4SLinus Torvalds 
38344ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
38351da177e4SLinus Torvalds 	error = -EBUSY;
38361da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
38371da177e4SLinus Torvalds 		goto exit2;
38381da177e4SLinus Torvalds 
38394ac91378SJan Blunck 	new_dir = newnd.path.dentry;
38401da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
38411da177e4SLinus Torvalds 		goto exit2;
38421da177e4SLinus Torvalds 
3843c30dabfeSJan Kara 	error = mnt_want_write(oldnd.path.mnt);
3844c30dabfeSJan Kara 	if (error)
3845c30dabfeSJan Kara 		goto exit2;
3846c30dabfeSJan Kara 
38470612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
38480612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
38494e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
38500612d9fbSOGAWA Hirofumi 
38511da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
38521da177e4SLinus Torvalds 
385349705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
38541da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
38551da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
38561da177e4SLinus Torvalds 		goto exit3;
38571da177e4SLinus Torvalds 	/* source must exist */
38581da177e4SLinus Torvalds 	error = -ENOENT;
38591da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
38601da177e4SLinus Torvalds 		goto exit4;
38611da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
38621da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
38631da177e4SLinus Torvalds 		error = -ENOTDIR;
38641da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
38651da177e4SLinus Torvalds 			goto exit4;
38661da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
38671da177e4SLinus Torvalds 			goto exit4;
38681da177e4SLinus Torvalds 	}
38691da177e4SLinus Torvalds 	/* source should not be ancestor of target */
38701da177e4SLinus Torvalds 	error = -EINVAL;
38711da177e4SLinus Torvalds 	if (old_dentry == trap)
38721da177e4SLinus Torvalds 		goto exit4;
387349705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
38741da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
38751da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
38761da177e4SLinus Torvalds 		goto exit4;
38771da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
38781da177e4SLinus Torvalds 	error = -ENOTEMPTY;
38791da177e4SLinus Torvalds 	if (new_dentry == trap)
38801da177e4SLinus Torvalds 		goto exit5;
38811da177e4SLinus Torvalds 
3882be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3883be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3884be6d3e56SKentaro Takeda 	if (error)
3885c30dabfeSJan Kara 		goto exit5;
38861da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
38871da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
38881da177e4SLinus Torvalds exit5:
38891da177e4SLinus Torvalds 	dput(new_dentry);
38901da177e4SLinus Torvalds exit4:
38911da177e4SLinus Torvalds 	dput(old_dentry);
38921da177e4SLinus Torvalds exit3:
38931da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
3894c30dabfeSJan Kara 	mnt_drop_write(oldnd.path.mnt);
38951da177e4SLinus Torvalds exit2:
3896c6a94284SJeff Layton 	if (retry_estale(error, lookup_flags))
3897c6a94284SJeff Layton 		should_retry = true;
38981d957f9bSJan Blunck 	path_put(&newnd.path);
38992ad94ae6SAl Viro 	putname(to);
39001da177e4SLinus Torvalds exit1:
39011d957f9bSJan Blunck 	path_put(&oldnd.path);
39021da177e4SLinus Torvalds 	putname(from);
3903c6a94284SJeff Layton 	if (should_retry) {
3904c6a94284SJeff Layton 		should_retry = false;
3905c6a94284SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3906c6a94284SJeff Layton 		goto retry;
3907c6a94284SJeff Layton 	}
39082ad94ae6SAl Viro exit:
39091da177e4SLinus Torvalds 	return error;
39101da177e4SLinus Torvalds }
39111da177e4SLinus Torvalds 
3912a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
39135590ff0dSUlrich Drepper {
39145590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
39155590ff0dSUlrich Drepper }
39165590ff0dSUlrich Drepper 
39171da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
39181da177e4SLinus Torvalds {
39191da177e4SLinus Torvalds 	int len;
39201da177e4SLinus Torvalds 
39211da177e4SLinus Torvalds 	len = PTR_ERR(link);
39221da177e4SLinus Torvalds 	if (IS_ERR(link))
39231da177e4SLinus Torvalds 		goto out;
39241da177e4SLinus Torvalds 
39251da177e4SLinus Torvalds 	len = strlen(link);
39261da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
39271da177e4SLinus Torvalds 		len = buflen;
39281da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
39291da177e4SLinus Torvalds 		len = -EFAULT;
39301da177e4SLinus Torvalds out:
39311da177e4SLinus Torvalds 	return len;
39321da177e4SLinus Torvalds }
39331da177e4SLinus Torvalds 
39341da177e4SLinus Torvalds /*
39351da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
39361da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
39371da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
39381da177e4SLinus Torvalds  */
39391da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
39401da177e4SLinus Torvalds {
39411da177e4SLinus Torvalds 	struct nameidata nd;
3942cc314eefSLinus Torvalds 	void *cookie;
3943694a1764SMarcin Slusarz 	int res;
3944cc314eefSLinus Torvalds 
39451da177e4SLinus Torvalds 	nd.depth = 0;
3946cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3947694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3948694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3949694a1764SMarcin Slusarz 
3950694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
39511da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3952cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3953694a1764SMarcin Slusarz 	return res;
39541da177e4SLinus Torvalds }
39551da177e4SLinus Torvalds 
39561da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
39571da177e4SLinus Torvalds {
39581da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
39591da177e4SLinus Torvalds }
39601da177e4SLinus Torvalds 
39611da177e4SLinus Torvalds /* get the link contents into pagecache */
39621da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
39631da177e4SLinus Torvalds {
3964ebd09abbSDuane Griffin 	char *kaddr;
39651da177e4SLinus Torvalds 	struct page *page;
39661da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3967090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
39681da177e4SLinus Torvalds 	if (IS_ERR(page))
39696fe6900eSNick Piggin 		return (char*)page;
39701da177e4SLinus Torvalds 	*ppage = page;
3971ebd09abbSDuane Griffin 	kaddr = kmap(page);
3972ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3973ebd09abbSDuane Griffin 	return kaddr;
39741da177e4SLinus Torvalds }
39751da177e4SLinus Torvalds 
39761da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
39771da177e4SLinus Torvalds {
39781da177e4SLinus Torvalds 	struct page *page = NULL;
39791da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
39801da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
39811da177e4SLinus Torvalds 	if (page) {
39821da177e4SLinus Torvalds 		kunmap(page);
39831da177e4SLinus Torvalds 		page_cache_release(page);
39841da177e4SLinus Torvalds 	}
39851da177e4SLinus Torvalds 	return res;
39861da177e4SLinus Torvalds }
39871da177e4SLinus Torvalds 
3988cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
39891da177e4SLinus Torvalds {
3990cc314eefSLinus Torvalds 	struct page *page = NULL;
39911da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3992cc314eefSLinus Torvalds 	return page;
39931da177e4SLinus Torvalds }
39941da177e4SLinus Torvalds 
3995cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
39961da177e4SLinus Torvalds {
3997cc314eefSLinus Torvalds 	struct page *page = cookie;
3998cc314eefSLinus Torvalds 
3999cc314eefSLinus Torvalds 	if (page) {
40001da177e4SLinus Torvalds 		kunmap(page);
40011da177e4SLinus Torvalds 		page_cache_release(page);
40021da177e4SLinus Torvalds 	}
40031da177e4SLinus Torvalds }
40041da177e4SLinus Torvalds 
400554566b2cSNick Piggin /*
400654566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
400754566b2cSNick Piggin  */
400854566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
40091da177e4SLinus Torvalds {
40101da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
40110adb25d2SKirill Korotaev 	struct page *page;
4012afddba49SNick Piggin 	void *fsdata;
4013beb497abSDmitriy Monakhov 	int err;
40141da177e4SLinus Torvalds 	char *kaddr;
401554566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
401654566b2cSNick Piggin 	if (nofs)
401754566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
40181da177e4SLinus Torvalds 
40197e53cac4SNeilBrown retry:
4020afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
402154566b2cSNick Piggin 				flags, &page, &fsdata);
40221da177e4SLinus Torvalds 	if (err)
4023afddba49SNick Piggin 		goto fail;
4024afddba49SNick Piggin 
4025e8e3c3d6SCong Wang 	kaddr = kmap_atomic(page);
40261da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
4027e8e3c3d6SCong Wang 	kunmap_atomic(kaddr);
4028afddba49SNick Piggin 
4029afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4030afddba49SNick Piggin 							page, fsdata);
40311da177e4SLinus Torvalds 	if (err < 0)
40321da177e4SLinus Torvalds 		goto fail;
4033afddba49SNick Piggin 	if (err < len-1)
4034afddba49SNick Piggin 		goto retry;
4035afddba49SNick Piggin 
40361da177e4SLinus Torvalds 	mark_inode_dirty(inode);
40371da177e4SLinus Torvalds 	return 0;
40381da177e4SLinus Torvalds fail:
40391da177e4SLinus Torvalds 	return err;
40401da177e4SLinus Torvalds }
40411da177e4SLinus Torvalds 
40420adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
40430adb25d2SKirill Korotaev {
40440adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
404554566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
40460adb25d2SKirill Korotaev }
40470adb25d2SKirill Korotaev 
404892e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
40491da177e4SLinus Torvalds 	.readlink	= generic_readlink,
40501da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
40511da177e4SLinus Torvalds 	.put_link	= page_put_link,
40521da177e4SLinus Torvalds };
40531da177e4SLinus Torvalds 
40542d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
4055cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
40561da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
40571da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
4058f6d2ac5cSAl Viro EXPORT_SYMBOL(get_write_access); /* nfsd */
40591da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
40601da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
40611da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
40621da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
40631da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
40640adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
40651da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
40661da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
4067d1811465SAl Viro EXPORT_SYMBOL(kern_path);
406816f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
4069f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
40701da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
40711da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
40721da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
40731da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
40741da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
40751da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
40761da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
40771da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
40781da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
40791da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
40801da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
40811da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
40821da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
40831da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
4084