xref: /openbmc/linux/fs/namei.c (revision 84635d68)
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>
181da177e4SLinus Torvalds #include <linux/module.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/fs.h>
211da177e4SLinus Torvalds #include <linux/namei.h>
221da177e4SLinus Torvalds #include <linux/pagemap.h>
230eeca283SRobert Love #include <linux/fsnotify.h>
241da177e4SLinus Torvalds #include <linux/personality.h>
251da177e4SLinus Torvalds #include <linux/security.h>
266146f0d5SMimi Zohar #include <linux/ima.h>
271da177e4SLinus Torvalds #include <linux/syscalls.h>
281da177e4SLinus Torvalds #include <linux/mount.h>
291da177e4SLinus Torvalds #include <linux/audit.h>
3016f7e0feSRandy Dunlap #include <linux/capability.h>
31834f2a4aSTrond Myklebust #include <linux/file.h>
325590ff0dSUlrich Drepper #include <linux/fcntl.h>
3308ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h>
345ad4e53bSAl Viro #include <linux/fs_struct.h>
35e77819e5SLinus Torvalds #include <linux/posix_acl.h>
361da177e4SLinus Torvalds #include <asm/uaccess.h>
371da177e4SLinus Torvalds 
38e81e3f4dSEric Paris #include "internal.h"
39e81e3f4dSEric Paris 
401da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer]
411da177e4SLinus Torvalds  * Fundamental changes in the pathname lookup mechanisms (namei)
421da177e4SLinus Torvalds  * were necessary because of omirr.  The reason is that omirr needs
431da177e4SLinus Torvalds  * to know the _real_ pathname, not the user-supplied one, in case
441da177e4SLinus Torvalds  * of symlinks (and also when transname replacements occur).
451da177e4SLinus Torvalds  *
461da177e4SLinus Torvalds  * The new code replaces the old recursive symlink resolution with
471da177e4SLinus Torvalds  * an iterative one (in case of non-nested symlink chains).  It does
481da177e4SLinus Torvalds  * this with calls to <fs>_follow_link().
491da177e4SLinus Torvalds  * As a side effect, dir_namei(), _namei() and follow_link() are now
501da177e4SLinus Torvalds  * replaced with a single function lookup_dentry() that can handle all
511da177e4SLinus Torvalds  * the special cases of the former code.
521da177e4SLinus Torvalds  *
531da177e4SLinus Torvalds  * With the new dcache, the pathname is stored at each inode, at least as
541da177e4SLinus Torvalds  * long as the refcount of the inode is positive.  As a side effect, the
551da177e4SLinus Torvalds  * size of the dcache depends on the inode cache and thus is dynamic.
561da177e4SLinus Torvalds  *
571da177e4SLinus Torvalds  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
581da177e4SLinus Torvalds  * resolution to correspond with current state of the code.
591da177e4SLinus Torvalds  *
601da177e4SLinus Torvalds  * Note that the symlink resolution is not *completely* iterative.
611da177e4SLinus Torvalds  * There is still a significant amount of tail- and mid- recursion in
621da177e4SLinus Torvalds  * the algorithm.  Also, note that <fs>_readlink() is not used in
631da177e4SLinus Torvalds  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
641da177e4SLinus Torvalds  * may return different results than <fs>_follow_link().  Many virtual
651da177e4SLinus Torvalds  * filesystems (including /proc) exhibit this behavior.
661da177e4SLinus Torvalds  */
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
691da177e4SLinus Torvalds  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
701da177e4SLinus Torvalds  * and the name already exists in form of a symlink, try to create the new
711da177e4SLinus Torvalds  * name indicated by the symlink. The old code always complained that the
721da177e4SLinus Torvalds  * name already exists, due to not following the symlink even if its target
731da177e4SLinus Torvalds  * is nonexistent.  The new semantics affects also mknod() and link() when
7425985edcSLucas De Marchi  * the name is a symlink pointing to a non-existent name.
751da177e4SLinus Torvalds  *
761da177e4SLinus Torvalds  * I don't know which semantics is the right one, since I have no access
771da177e4SLinus Torvalds  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
781da177e4SLinus Torvalds  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
791da177e4SLinus Torvalds  * "old" one. Personally, I think the new semantics is much more logical.
801da177e4SLinus Torvalds  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
811da177e4SLinus Torvalds  * file does succeed in both HP-UX and SunOs, but not in Solaris
821da177e4SLinus Torvalds  * and in the old Linux semantics.
831da177e4SLinus Torvalds  */
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
861da177e4SLinus Torvalds  * semantics.  See the comments in "open_namei" and "do_link" below.
871da177e4SLinus Torvalds  *
881da177e4SLinus Torvalds  * [10-Sep-98 Alan Modra] Another symlink change.
891da177e4SLinus Torvalds  */
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
921da177e4SLinus Torvalds  *	inside the path - always follow.
931da177e4SLinus Torvalds  *	in the last component in creation/removal/renaming - never follow.
941da177e4SLinus Torvalds  *	if LOOKUP_FOLLOW passed - follow.
951da177e4SLinus Torvalds  *	if the pathname has trailing slashes - follow.
961da177e4SLinus Torvalds  *	otherwise - don't follow.
971da177e4SLinus Torvalds  * (applied in that order).
981da177e4SLinus Torvalds  *
991da177e4SLinus Torvalds  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
1001da177e4SLinus Torvalds  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
1011da177e4SLinus Torvalds  * During the 2.4 we need to fix the userland stuff depending on it -
1021da177e4SLinus Torvalds  * hopefully we will be able to get rid of that wart in 2.5. So far only
1031da177e4SLinus Torvalds  * XEmacs seems to be relying on it...
1041da177e4SLinus Torvalds  */
1051da177e4SLinus Torvalds /*
1061da177e4SLinus Torvalds  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
107a11f3a05SArjan van de Ven  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1081da177e4SLinus Torvalds  * any extra contention...
1091da177e4SLinus Torvalds  */
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional
1121da177e4SLinus Torvalds  * checking and hopefully speeding things up, we copy filenames to the
1131da177e4SLinus Torvalds  * kernel data space before using them..
1141da177e4SLinus Torvalds  *
1151da177e4SLinus Torvalds  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
1161da177e4SLinus Torvalds  * PATH_MAX includes the nul terminator --RR.
1171da177e4SLinus Torvalds  */
118858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page)
1191da177e4SLinus Torvalds {
1201da177e4SLinus Torvalds 	int retval;
1211da177e4SLinus Torvalds 	unsigned long len = PATH_MAX;
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	if (!segment_eq(get_fs(), KERNEL_DS)) {
1241da177e4SLinus Torvalds 		if ((unsigned long) filename >= TASK_SIZE)
1251da177e4SLinus Torvalds 			return -EFAULT;
1261da177e4SLinus Torvalds 		if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
1271da177e4SLinus Torvalds 			len = TASK_SIZE - (unsigned long) filename;
1281da177e4SLinus Torvalds 	}
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	retval = strncpy_from_user(page, filename, len);
1311da177e4SLinus Torvalds 	if (retval > 0) {
1321da177e4SLinus Torvalds 		if (retval < len)
1331da177e4SLinus Torvalds 			return 0;
1341da177e4SLinus Torvalds 		return -ENAMETOOLONG;
1351da177e4SLinus Torvalds 	} else if (!retval)
1361da177e4SLinus Torvalds 		retval = -ENOENT;
1371da177e4SLinus Torvalds 	return retval;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
140f52e0c11SAl Viro static char *getname_flags(const char __user * filename, int flags)
1411da177e4SLinus Torvalds {
1421da177e4SLinus Torvalds 	char *tmp, *result;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	result = ERR_PTR(-ENOMEM);
1451da177e4SLinus Torvalds 	tmp = __getname();
1461da177e4SLinus Torvalds 	if (tmp)  {
1471da177e4SLinus Torvalds 		int retval = do_getname(filename, tmp);
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 		result = tmp;
1501da177e4SLinus Torvalds 		if (retval < 0) {
151f52e0c11SAl Viro 			if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
1521da177e4SLinus Torvalds 				__putname(tmp);
1531da177e4SLinus Torvalds 				result = ERR_PTR(retval);
1541da177e4SLinus Torvalds 			}
1551da177e4SLinus Torvalds 		}
156f52e0c11SAl Viro 	}
1571da177e4SLinus Torvalds 	audit_getname(result);
1581da177e4SLinus Torvalds 	return result;
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds 
161f52e0c11SAl Viro char *getname(const char __user * filename)
162f52e0c11SAl Viro {
163f52e0c11SAl Viro 	return getname_flags(filename, 0);
164f52e0c11SAl Viro }
165f52e0c11SAl Viro 
1661da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
1671da177e4SLinus Torvalds void putname(const char *name)
1681da177e4SLinus Torvalds {
1695ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
1701da177e4SLinus Torvalds 		audit_putname(name);
1711da177e4SLinus Torvalds 	else
1721da177e4SLinus Torvalds 		__putname(name);
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds EXPORT_SYMBOL(putname);
1751da177e4SLinus Torvalds #endif
1761da177e4SLinus Torvalds 
177e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask)
178e77819e5SLinus Torvalds {
17984635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL
180e77819e5SLinus Torvalds 	struct posix_acl *acl;
181e77819e5SLinus Torvalds 
182e77819e5SLinus Torvalds 	/*
183e77819e5SLinus Torvalds 	 * Under RCU walk, we cannot even do a "get_cached_acl()",
184e77819e5SLinus Torvalds 	 * because that involves locking and getting a refcount on
185e77819e5SLinus Torvalds 	 * a cached ACL.
186e77819e5SLinus Torvalds 	 *
187e77819e5SLinus Torvalds 	 * So the only case we handle during RCU walking is the
188e77819e5SLinus Torvalds 	 * case of a cached "no ACL at all", which needs no locks
189e77819e5SLinus Torvalds 	 * or refcounts.
190e77819e5SLinus Torvalds 	 */
191e77819e5SLinus Torvalds 	if (mask & MAY_NOT_BLOCK) {
192e77819e5SLinus Torvalds 	        if (negative_cached_acl(inode, ACL_TYPE_ACCESS))
193e77819e5SLinus Torvalds 	                return -EAGAIN;
194e77819e5SLinus Torvalds 	        return -ECHILD;
195e77819e5SLinus Torvalds 	}
196e77819e5SLinus Torvalds 
197e77819e5SLinus Torvalds 	acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
198e77819e5SLinus Torvalds 
199e77819e5SLinus Torvalds 	/*
2004e34e719SChristoph Hellwig 	 * A filesystem can force a ACL callback by just never filling the
2014e34e719SChristoph Hellwig 	 * ACL cache. But normally you'd fill the cache either at inode
2024e34e719SChristoph Hellwig 	 * instantiation time, or on the first ->get_acl call.
203e77819e5SLinus Torvalds 	 *
2044e34e719SChristoph Hellwig 	 * If the filesystem doesn't have a get_acl() function at all, we'll
2054e34e719SChristoph Hellwig 	 * just create the negative cache entry.
206e77819e5SLinus Torvalds 	 */
207e77819e5SLinus Torvalds 	if (acl == ACL_NOT_CACHED) {
2084e34e719SChristoph Hellwig 	        if (inode->i_op->get_acl) {
2094e34e719SChristoph Hellwig 			acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
2104e34e719SChristoph Hellwig 			if (IS_ERR(acl))
2114e34e719SChristoph Hellwig 				return PTR_ERR(acl);
2124e34e719SChristoph Hellwig 		} else {
213e77819e5SLinus Torvalds 		        set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
214e77819e5SLinus Torvalds 		        return -EAGAIN;
215e77819e5SLinus Torvalds 		}
2164e34e719SChristoph Hellwig 	}
217e77819e5SLinus Torvalds 
218e77819e5SLinus Torvalds 	if (acl) {
219e77819e5SLinus Torvalds 	        int error = posix_acl_permission(inode, acl, mask);
220e77819e5SLinus Torvalds 	        posix_acl_release(acl);
221e77819e5SLinus Torvalds 	        return error;
222e77819e5SLinus Torvalds 	}
22384635d68SLinus Torvalds #endif
224e77819e5SLinus Torvalds 
225e77819e5SLinus Torvalds 	return -EAGAIN;
226e77819e5SLinus Torvalds }
227e77819e5SLinus Torvalds 
2285909ccaaSLinus Torvalds /*
2295909ccaaSLinus Torvalds  * This does basic POSIX ACL permission checking
2305909ccaaSLinus Torvalds  */
2317e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask)
2325909ccaaSLinus Torvalds {
23326cf46beSLinus Torvalds 	unsigned int mode = inode->i_mode;
2345909ccaaSLinus Torvalds 
2359c2c7039SAl Viro 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
2365909ccaaSLinus Torvalds 
237e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
238e795b717SSerge E. Hallyn 		goto other_perms;
239e795b717SSerge E. Hallyn 
24014067ff5SLinus Torvalds 	if (likely(current_fsuid() == inode->i_uid))
2415909ccaaSLinus Torvalds 		mode >>= 6;
2425909ccaaSLinus Torvalds 	else {
243e77819e5SLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
2447e40145eSAl Viro 			int error = check_acl(inode, mask);
2455909ccaaSLinus Torvalds 			if (error != -EAGAIN)
2465909ccaaSLinus Torvalds 				return error;
2475909ccaaSLinus Torvalds 		}
2485909ccaaSLinus Torvalds 
2495909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
2505909ccaaSLinus Torvalds 			mode >>= 3;
2515909ccaaSLinus Torvalds 	}
2525909ccaaSLinus Torvalds 
253e795b717SSerge E. Hallyn other_perms:
2545909ccaaSLinus Torvalds 	/*
2555909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
2565909ccaaSLinus Torvalds 	 */
2579c2c7039SAl Viro 	if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
2585909ccaaSLinus Torvalds 		return 0;
2595909ccaaSLinus Torvalds 	return -EACCES;
2605909ccaaSLinus Torvalds }
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds /**
2631da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
2641da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2651da177e4SLinus Torvalds  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
2661da177e4SLinus Torvalds  *
2671da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
2681da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
2691da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
270b74c79e9SNick Piggin  * are used for other things.
271b74c79e9SNick Piggin  *
272b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
273b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
274b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
2751da177e4SLinus Torvalds  */
2762830ba7fSAl Viro int generic_permission(struct inode *inode, int mask)
2771da177e4SLinus Torvalds {
2785909ccaaSLinus Torvalds 	int ret;
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	/*
2815909ccaaSLinus Torvalds 	 * Do the basic POSIX ACL permission checks.
2821da177e4SLinus Torvalds 	 */
2837e40145eSAl Viro 	ret = acl_permission_check(inode, mask);
2845909ccaaSLinus Torvalds 	if (ret != -EACCES)
2855909ccaaSLinus Torvalds 		return ret;
2861da177e4SLinus Torvalds 
287d594e7ecSAl Viro 	if (S_ISDIR(inode->i_mode)) {
288d594e7ecSAl Viro 		/* DACs are overridable for directories */
289d594e7ecSAl Viro 		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
290d594e7ecSAl Viro 			return 0;
291d594e7ecSAl Viro 		if (!(mask & MAY_WRITE))
292d594e7ecSAl Viro 			if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
293d594e7ecSAl Viro 				return 0;
294d594e7ecSAl Viro 		return -EACCES;
295d594e7ecSAl Viro 	}
2961da177e4SLinus Torvalds 	/*
2971da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
298d594e7ecSAl Viro 	 * Executable DACs are overridable when there is
299d594e7ecSAl Viro 	 * at least one exec bit set.
3001da177e4SLinus Torvalds 	 */
301d594e7ecSAl Viro 	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
302e795b717SSerge E. Hallyn 		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
3031da177e4SLinus Torvalds 			return 0;
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	/*
3061da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
3071da177e4SLinus Torvalds 	 */
3087ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
309d594e7ecSAl Viro 	if (mask == MAY_READ)
310e795b717SSerge E. Hallyn 		if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
3111da177e4SLinus Torvalds 			return 0;
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	return -EACCES;
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds 
316cb23beb5SChristoph Hellwig /**
317cb23beb5SChristoph Hellwig  * inode_permission  -  check for access rights to a given inode
318cb23beb5SChristoph Hellwig  * @inode:	inode to check permission on
319cb23beb5SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
320cb23beb5SChristoph Hellwig  *
321cb23beb5SChristoph Hellwig  * Used to check for read/write/execute permissions on an inode.
322cb23beb5SChristoph Hellwig  * We use "fsuid" for this, letting us set arbitrary permissions
323cb23beb5SChristoph Hellwig  * for filesystem access without changing the "normal" uids which
324cb23beb5SChristoph Hellwig  * are used for other things.
325cb23beb5SChristoph Hellwig  */
326f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask)
3271da177e4SLinus Torvalds {
328e6305c43SAl Viro 	int retval;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	if (mask & MAY_WRITE) {
33122590e41SMiklos Szeredi 		umode_t mode = inode->i_mode;
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 		/*
3341da177e4SLinus Torvalds 		 * Nobody gets write access to a read-only fs.
3351da177e4SLinus Torvalds 		 */
3361da177e4SLinus Torvalds 		if (IS_RDONLY(inode) &&
3371da177e4SLinus Torvalds 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
3381da177e4SLinus Torvalds 			return -EROFS;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 		/*
3411da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
3421da177e4SLinus Torvalds 		 */
3431da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
3441da177e4SLinus Torvalds 			return -EACCES;
3451da177e4SLinus Torvalds 	}
3461da177e4SLinus Torvalds 
347acfa4380SAl Viro 	if (inode->i_op->permission)
34810556cb2SAl Viro 		retval = inode->i_op->permission(inode, mask);
349f696a365SMiklos Szeredi 	else
3502830ba7fSAl Viro 		retval = generic_permission(inode, mask);
351f696a365SMiklos Szeredi 
3521da177e4SLinus Torvalds 	if (retval)
3531da177e4SLinus Torvalds 		return retval;
3541da177e4SLinus Torvalds 
35508ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
35608ce5f16SSerge E. Hallyn 	if (retval)
35708ce5f16SSerge E. Hallyn 		return retval;
35808ce5f16SSerge E. Hallyn 
359d09ca739SEric Paris 	return security_inode_permission(inode, mask);
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
362f4d6ff89SAl Viro /**
3635dd784d0SJan Blunck  * path_get - get a reference to a path
3645dd784d0SJan Blunck  * @path: path to get the reference to
3655dd784d0SJan Blunck  *
3665dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3675dd784d0SJan Blunck  */
3685dd784d0SJan Blunck void path_get(struct path *path)
3695dd784d0SJan Blunck {
3705dd784d0SJan Blunck 	mntget(path->mnt);
3715dd784d0SJan Blunck 	dget(path->dentry);
3725dd784d0SJan Blunck }
3735dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
3745dd784d0SJan Blunck 
3755dd784d0SJan Blunck /**
3761d957f9bSJan Blunck  * path_put - put a reference to a path
3771d957f9bSJan Blunck  * @path: path to put the reference to
3781d957f9bSJan Blunck  *
3791d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
3801d957f9bSJan Blunck  */
3811d957f9bSJan Blunck void path_put(struct path *path)
3821da177e4SLinus Torvalds {
3831d957f9bSJan Blunck 	dput(path->dentry);
3841d957f9bSJan Blunck 	mntput(path->mnt);
3851da177e4SLinus Torvalds }
3861d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
3871da177e4SLinus Torvalds 
38819660af7SAl Viro /*
38931e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
39019660af7SAl Viro  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
39119660af7SAl Viro  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
39219660af7SAl Viro  * normal reference counts on dentries and vfsmounts to transition to rcu-walk
39319660af7SAl Viro  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
39419660af7SAl Viro  * got stuck, so ref-walk may continue from there. If this is not successful
39519660af7SAl Viro  * (eg. a seqcount has changed), then failure is returned and it's up to caller
39619660af7SAl Viro  * to restart the path walk from the beginning in ref-walk mode.
39731e6b01fSNick Piggin  */
39831e6b01fSNick Piggin 
39931e6b01fSNick Piggin /**
40019660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
40119660af7SAl Viro  * @nd: nameidata pathwalk data
40219660af7SAl Viro  * @dentry: child of nd->path.dentry or NULL
40339191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
40431e6b01fSNick Piggin  *
40519660af7SAl Viro  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
40619660af7SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
40719660af7SAl Viro  * @nd or NULL.  Must be called from rcu-walk context.
40831e6b01fSNick Piggin  */
40919660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
41031e6b01fSNick Piggin {
41131e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
41231e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
4135b6ca027SAl Viro 	int want_root = 0;
41431e6b01fSNick Piggin 
41531e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
4165b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
4175b6ca027SAl Viro 		want_root = 1;
41831e6b01fSNick Piggin 		spin_lock(&fs->lock);
41931e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
42031e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
42131e6b01fSNick Piggin 			goto err_root;
42231e6b01fSNick Piggin 	}
42331e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
42419660af7SAl Viro 	if (!dentry) {
42519660af7SAl Viro 		if (!__d_rcu_to_refcount(parent, nd->seq))
42619660af7SAl Viro 			goto err_parent;
42719660af7SAl Viro 		BUG_ON(nd->inode != parent->d_inode);
42819660af7SAl Viro 	} else {
42994c0d4ecSAl Viro 		if (dentry->d_parent != parent)
43094c0d4ecSAl Viro 			goto err_parent;
43131e6b01fSNick Piggin 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
43231e6b01fSNick Piggin 		if (!__d_rcu_to_refcount(dentry, nd->seq))
43319660af7SAl Viro 			goto err_child;
43431e6b01fSNick Piggin 		/*
43519660af7SAl Viro 		 * If the sequence check on the child dentry passed, then
43619660af7SAl Viro 		 * the child has not been removed from its parent. This
43719660af7SAl Viro 		 * means the parent dentry must be valid and able to take
43819660af7SAl Viro 		 * a reference at this point.
43931e6b01fSNick Piggin 		 */
44031e6b01fSNick Piggin 		BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
44131e6b01fSNick Piggin 		BUG_ON(!parent->d_count);
44231e6b01fSNick Piggin 		parent->d_count++;
44331e6b01fSNick Piggin 		spin_unlock(&dentry->d_lock);
44419660af7SAl Viro 	}
44531e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
4465b6ca027SAl Viro 	if (want_root) {
44731e6b01fSNick Piggin 		path_get(&nd->root);
44831e6b01fSNick Piggin 		spin_unlock(&fs->lock);
44931e6b01fSNick Piggin 	}
45031e6b01fSNick Piggin 	mntget(nd->path.mnt);
45131e6b01fSNick Piggin 
45231e6b01fSNick Piggin 	rcu_read_unlock();
45331e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
45431e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
45531e6b01fSNick Piggin 	return 0;
45619660af7SAl Viro 
45719660af7SAl Viro err_child:
45831e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
45919660af7SAl Viro err_parent:
46031e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
46131e6b01fSNick Piggin err_root:
4625b6ca027SAl Viro 	if (want_root)
46331e6b01fSNick Piggin 		spin_unlock(&fs->lock);
46431e6b01fSNick Piggin 	return -ECHILD;
46531e6b01fSNick Piggin }
46631e6b01fSNick Piggin 
46731e6b01fSNick Piggin /**
468834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
469834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
470834f2a4aSTrond Myklebust  */
471834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
472834f2a4aSTrond Myklebust {
4732dab5974SLinus Torvalds 	struct file *file = nd->intent.open.file;
4742dab5974SLinus Torvalds 
4752dab5974SLinus Torvalds 	if (file && !IS_ERR(file)) {
4762dab5974SLinus Torvalds 		if (file->f_path.dentry == NULL)
4772dab5974SLinus Torvalds 			put_filp(file);
478834f2a4aSTrond Myklebust 		else
4792dab5974SLinus Torvalds 			fput(file);
4802dab5974SLinus Torvalds 	}
481834f2a4aSTrond Myklebust }
482834f2a4aSTrond Myklebust 
483f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
48434286d66SNick Piggin {
485f60aef7eSAl Viro 	return dentry->d_op->d_revalidate(dentry, nd);
48634286d66SNick Piggin }
48734286d66SNick Piggin 
4889f1fafeeSAl Viro /**
4899f1fafeeSAl Viro  * complete_walk - successful completion of path walk
4909f1fafeeSAl Viro  * @nd:  pointer nameidata
49139159de2SJeff Layton  *
4929f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
4939f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
4949f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
4959f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
4969f1fafeeSAl Viro  * need to drop nd->path.
49739159de2SJeff Layton  */
4989f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
49939159de2SJeff Layton {
50016c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
50139159de2SJeff Layton 	int status;
50239159de2SJeff Layton 
5039f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
5049f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
5059f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
5069f1fafeeSAl Viro 			nd->root.mnt = NULL;
5079f1fafeeSAl Viro 		spin_lock(&dentry->d_lock);
5089f1fafeeSAl Viro 		if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
5099f1fafeeSAl Viro 			spin_unlock(&dentry->d_lock);
5109f1fafeeSAl Viro 			rcu_read_unlock();
5119f1fafeeSAl Viro 			br_read_unlock(vfsmount_lock);
5129f1fafeeSAl Viro 			return -ECHILD;
5139f1fafeeSAl Viro 		}
5149f1fafeeSAl Viro 		BUG_ON(nd->inode != dentry->d_inode);
5159f1fafeeSAl Viro 		spin_unlock(&dentry->d_lock);
5169f1fafeeSAl Viro 		mntget(nd->path.mnt);
5179f1fafeeSAl Viro 		rcu_read_unlock();
5189f1fafeeSAl Viro 		br_read_unlock(vfsmount_lock);
5199f1fafeeSAl Viro 	}
5209f1fafeeSAl Viro 
52116c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
52239159de2SJeff Layton 		return 0;
52339159de2SJeff Layton 
52416c2cd71SAl Viro 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
52516c2cd71SAl Viro 		return 0;
52616c2cd71SAl Viro 
52716c2cd71SAl Viro 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
52816c2cd71SAl Viro 		return 0;
52916c2cd71SAl Viro 
53016c2cd71SAl Viro 	/* Note: we do not d_invalidate() */
53134286d66SNick Piggin 	status = d_revalidate(dentry, nd);
53239159de2SJeff Layton 	if (status > 0)
53339159de2SJeff Layton 		return 0;
53439159de2SJeff Layton 
53516c2cd71SAl Viro 	if (!status)
53639159de2SJeff Layton 		status = -ESTALE;
53716c2cd71SAl Viro 
5389f1fafeeSAl Viro 	path_put(&nd->path);
53939159de2SJeff Layton 	return status;
54039159de2SJeff Layton }
54139159de2SJeff Layton 
5422a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
5432a737871SAl Viro {
544f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
545f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
5462a737871SAl Viro }
5472a737871SAl Viro 
5486de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
5496de88d72SAl Viro 
55031e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
55131e6b01fSNick Piggin {
55231e6b01fSNick Piggin 	if (!nd->root.mnt) {
55331e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
554c28cc364SNick Piggin 		unsigned seq;
555c28cc364SNick Piggin 
556c28cc364SNick Piggin 		do {
557c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
55831e6b01fSNick Piggin 			nd->root = fs->root;
559c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
560c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
56131e6b01fSNick Piggin 	}
56231e6b01fSNick Piggin }
56331e6b01fSNick Piggin 
564f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
5651da177e4SLinus Torvalds {
56631e6b01fSNick Piggin 	int ret;
56731e6b01fSNick Piggin 
5681da177e4SLinus Torvalds 	if (IS_ERR(link))
5691da177e4SLinus Torvalds 		goto fail;
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	if (*link == '/') {
5722a737871SAl Viro 		set_root(nd);
5731d957f9bSJan Blunck 		path_put(&nd->path);
5742a737871SAl Viro 		nd->path = nd->root;
5752a737871SAl Viro 		path_get(&nd->root);
57616c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
5771da177e4SLinus Torvalds 	}
57831e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
579b4091d5fSChristoph Hellwig 
58031e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
58131e6b01fSNick Piggin 	return ret;
5821da177e4SLinus Torvalds fail:
5831d957f9bSJan Blunck 	path_put(&nd->path);
5841da177e4SLinus Torvalds 	return PTR_ERR(link);
5851da177e4SLinus Torvalds }
5861da177e4SLinus Torvalds 
5871d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
588051d3812SIan Kent {
589051d3812SIan Kent 	dput(path->dentry);
5904ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
591051d3812SIan Kent 		mntput(path->mnt);
592051d3812SIan Kent }
593051d3812SIan Kent 
5947b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
5957b9337aaSNick Piggin 					struct nameidata *nd)
596051d3812SIan Kent {
59731e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
5984ac91378SJan Blunck 		dput(nd->path.dentry);
59931e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
6004ac91378SJan Blunck 			mntput(nd->path.mnt);
6019a229683SHuang Shijie 	}
60231e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
6034ac91378SJan Blunck 	nd->path.dentry = path->dentry;
604051d3812SIan Kent }
605051d3812SIan Kent 
606574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
607574197e0SAl Viro {
608574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
609574197e0SAl Viro 	if (!IS_ERR(cookie) && inode->i_op->put_link)
610574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
611574197e0SAl Viro 	path_put(link);
612574197e0SAl Viro }
613574197e0SAl Viro 
614def4af30SAl Viro static __always_inline int
615574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
6161da177e4SLinus Torvalds {
6171da177e4SLinus Torvalds 	int error;
6187b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
6191da177e4SLinus Torvalds 
620844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
621844a3917SAl Viro 
6220e794589SAl Viro 	if (link->mnt == nd->path.mnt)
6230e794589SAl Viro 		mntget(link->mnt);
6240e794589SAl Viro 
625574197e0SAl Viro 	if (unlikely(current->total_link_count >= 40)) {
626574197e0SAl Viro 		*p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
627574197e0SAl Viro 		path_put(&nd->path);
628574197e0SAl Viro 		return -ELOOP;
629574197e0SAl Viro 	}
630574197e0SAl Viro 	cond_resched();
631574197e0SAl Viro 	current->total_link_count++;
632574197e0SAl Viro 
6337b9337aaSNick Piggin 	touch_atime(link->mnt, dentry);
6341da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
635cd4e91d3SAl Viro 
63636f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
63736f3b4f6SAl Viro 	if (error) {
63836f3b4f6SAl Viro 		*p = ERR_PTR(error); /* no ->put_link(), please */
63936f3b4f6SAl Viro 		path_put(&nd->path);
64036f3b4f6SAl Viro 		return error;
64136f3b4f6SAl Viro 	}
64236f3b4f6SAl Viro 
64386acdca1SAl Viro 	nd->last_type = LAST_BIND;
644def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
645def4af30SAl Viro 	error = PTR_ERR(*p);
646def4af30SAl Viro 	if (!IS_ERR(*p)) {
6471da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
648cc314eefSLinus Torvalds 		error = 0;
6491da177e4SLinus Torvalds 		if (s)
6501da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
651bcda7652SAl Viro 		else if (nd->last_type == LAST_BIND) {
65216c2cd71SAl Viro 			nd->flags |= LOOKUP_JUMPED;
653b21041d0SAl Viro 			nd->inode = nd->path.dentry->d_inode;
654b21041d0SAl Viro 			if (nd->inode->i_op->follow_link) {
655bcda7652SAl Viro 				/* stepped on a _really_ weird one */
656bcda7652SAl Viro 				path_put(&nd->path);
657bcda7652SAl Viro 				error = -ELOOP;
658bcda7652SAl Viro 			}
659bcda7652SAl Viro 		}
6601da177e4SLinus Torvalds 	}
6611da177e4SLinus Torvalds 	return error;
6621da177e4SLinus Torvalds }
6631da177e4SLinus Torvalds 
66431e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
66531e6b01fSNick Piggin {
66631e6b01fSNick Piggin 	struct vfsmount *parent;
66731e6b01fSNick Piggin 	struct dentry *mountpoint;
66831e6b01fSNick Piggin 
66931e6b01fSNick Piggin 	parent = path->mnt->mnt_parent;
67031e6b01fSNick Piggin 	if (parent == path->mnt)
67131e6b01fSNick Piggin 		return 0;
67231e6b01fSNick Piggin 	mountpoint = path->mnt->mnt_mountpoint;
67331e6b01fSNick Piggin 	path->dentry = mountpoint;
67431e6b01fSNick Piggin 	path->mnt = parent;
67531e6b01fSNick Piggin 	return 1;
67631e6b01fSNick Piggin }
67731e6b01fSNick Piggin 
678bab77ebfSAl Viro int follow_up(struct path *path)
6791da177e4SLinus Torvalds {
6801da177e4SLinus Torvalds 	struct vfsmount *parent;
6811da177e4SLinus Torvalds 	struct dentry *mountpoint;
68299b7db7bSNick Piggin 
68399b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
684bab77ebfSAl Viro 	parent = path->mnt->mnt_parent;
685bab77ebfSAl Viro 	if (parent == path->mnt) {
68699b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
6871da177e4SLinus Torvalds 		return 0;
6881da177e4SLinus Torvalds 	}
6891da177e4SLinus Torvalds 	mntget(parent);
690bab77ebfSAl Viro 	mountpoint = dget(path->mnt->mnt_mountpoint);
69199b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
692bab77ebfSAl Viro 	dput(path->dentry);
693bab77ebfSAl Viro 	path->dentry = mountpoint;
694bab77ebfSAl Viro 	mntput(path->mnt);
695bab77ebfSAl Viro 	path->mnt = parent;
6961da177e4SLinus Torvalds 	return 1;
6971da177e4SLinus Torvalds }
6981da177e4SLinus Torvalds 
699b5c84bf6SNick Piggin /*
7009875cf80SDavid Howells  * Perform an automount
7019875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
7029875cf80SDavid Howells  *   were called with.
7031da177e4SLinus Torvalds  */
7049875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
7059875cf80SDavid Howells 			    bool *need_mntput)
70631e6b01fSNick Piggin {
7079875cf80SDavid Howells 	struct vfsmount *mnt;
708ea5b778aSDavid Howells 	int err;
7099875cf80SDavid Howells 
7109875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
7119875cf80SDavid Howells 		return -EREMOTE;
7129875cf80SDavid Howells 
7136f45b656SDavid Howells 	/* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
7146f45b656SDavid Howells 	 * and this is the terminal part of the path.
7156f45b656SDavid Howells 	 */
71649084c3bSAl Viro 	if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_PARENT))
7176f45b656SDavid Howells 		return -EISDIR; /* we actually want to stop here */
7186f45b656SDavid Howells 
7199875cf80SDavid Howells 	/* We want to mount if someone is trying to open/create a file of any
7209875cf80SDavid Howells 	 * type under the mountpoint, wants to traverse through the mountpoint
7219875cf80SDavid Howells 	 * or wants to open the mounted directory.
7229875cf80SDavid Howells 	 *
7239875cf80SDavid Howells 	 * We don't want to mount if someone's just doing a stat and they've
7249875cf80SDavid Howells 	 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
7259875cf80SDavid Howells 	 * appended a '/' to the name.
7269875cf80SDavid Howells 	 */
7279875cf80SDavid Howells 	if (!(flags & LOOKUP_FOLLOW) &&
72849084c3bSAl Viro 	    !(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
7299875cf80SDavid Howells 		       LOOKUP_OPEN | LOOKUP_CREATE)))
7309875cf80SDavid Howells 		return -EISDIR;
7319875cf80SDavid Howells 
7329875cf80SDavid Howells 	current->total_link_count++;
7339875cf80SDavid Howells 	if (current->total_link_count >= 40)
7349875cf80SDavid Howells 		return -ELOOP;
7359875cf80SDavid Howells 
7369875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
7379875cf80SDavid Howells 	if (IS_ERR(mnt)) {
7389875cf80SDavid Howells 		/*
7399875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
7409875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
7419875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
7429875cf80SDavid Howells 		 *
7439875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
7449875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
7459875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
7469875cf80SDavid Howells 		 */
74749084c3bSAl Viro 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
7489875cf80SDavid Howells 			return -EREMOTE;
7499875cf80SDavid Howells 		return PTR_ERR(mnt);
75031e6b01fSNick Piggin 	}
751ea5b778aSDavid Howells 
7529875cf80SDavid Howells 	if (!mnt) /* mount collision */
7539875cf80SDavid Howells 		return 0;
7549875cf80SDavid Howells 
7558aef1884SAl Viro 	if (!*need_mntput) {
7568aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
7578aef1884SAl Viro 		mntget(path->mnt);
7588aef1884SAl Viro 		*need_mntput = true;
7598aef1884SAl Viro 	}
76019a167afSAl Viro 	err = finish_automount(mnt, path);
761ea5b778aSDavid Howells 
762ea5b778aSDavid Howells 	switch (err) {
763ea5b778aSDavid Howells 	case -EBUSY:
764ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
76519a167afSAl Viro 		return 0;
766ea5b778aSDavid Howells 	case 0:
7678aef1884SAl Viro 		path_put(path);
7689875cf80SDavid Howells 		path->mnt = mnt;
7699875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
7709875cf80SDavid Howells 		return 0;
77119a167afSAl Viro 	default:
77219a167afSAl Viro 		return err;
7739875cf80SDavid Howells 	}
77419a167afSAl Viro 
775ea5b778aSDavid Howells }
7769875cf80SDavid Howells 
7779875cf80SDavid Howells /*
7789875cf80SDavid Howells  * Handle a dentry that is managed in some way.
779cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
7809875cf80SDavid Howells  * - Flagged as mountpoint
7819875cf80SDavid Howells  * - Flagged as automount point
7829875cf80SDavid Howells  *
7839875cf80SDavid Howells  * This may only be called in refwalk mode.
7849875cf80SDavid Howells  *
7859875cf80SDavid Howells  * Serialization is taken care of in namespace.c
7869875cf80SDavid Howells  */
7879875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
7889875cf80SDavid Howells {
7898aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
7909875cf80SDavid Howells 	unsigned managed;
7919875cf80SDavid Howells 	bool need_mntput = false;
7928aef1884SAl Viro 	int ret = 0;
7939875cf80SDavid Howells 
7949875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
7959875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
7969875cf80SDavid Howells 	 * the components of that value change under us */
7979875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
7989875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
7999875cf80SDavid Howells 	       unlikely(managed != 0)) {
800cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
801cc53ce53SDavid Howells 		 * being held. */
802cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
803cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
804cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
8051aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
806cc53ce53SDavid Howells 			if (ret < 0)
8078aef1884SAl Viro 				break;
808cc53ce53SDavid Howells 		}
809cc53ce53SDavid Howells 
8109875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
8119875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
8129875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
8139875cf80SDavid Howells 			if (mounted) {
8149875cf80SDavid Howells 				dput(path->dentry);
8159875cf80SDavid Howells 				if (need_mntput)
816463ffb2eSAl Viro 					mntput(path->mnt);
817463ffb2eSAl Viro 				path->mnt = mounted;
818463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
8199875cf80SDavid Howells 				need_mntput = true;
8209875cf80SDavid Howells 				continue;
821463ffb2eSAl Viro 			}
822463ffb2eSAl Viro 
8239875cf80SDavid Howells 			/* Something is mounted on this dentry in another
8249875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
8259875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
8269875cf80SDavid Howells 			 * vfsmount_lock */
8271da177e4SLinus Torvalds 		}
8289875cf80SDavid Howells 
8299875cf80SDavid Howells 		/* Handle an automount point */
8309875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
8319875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
8329875cf80SDavid Howells 			if (ret < 0)
8338aef1884SAl Viro 				break;
8349875cf80SDavid Howells 			continue;
8359875cf80SDavid Howells 		}
8369875cf80SDavid Howells 
8379875cf80SDavid Howells 		/* We didn't change the current path point */
8389875cf80SDavid Howells 		break;
8399875cf80SDavid Howells 	}
8408aef1884SAl Viro 
8418aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
8428aef1884SAl Viro 		mntput(path->mnt);
8438aef1884SAl Viro 	if (ret == -EISDIR)
8448aef1884SAl Viro 		ret = 0;
8458aef1884SAl Viro 	return ret;
8461da177e4SLinus Torvalds }
8471da177e4SLinus Torvalds 
848cc53ce53SDavid Howells int follow_down_one(struct path *path)
8491da177e4SLinus Torvalds {
8501da177e4SLinus Torvalds 	struct vfsmount *mounted;
8511da177e4SLinus Torvalds 
8521c755af4SAl Viro 	mounted = lookup_mnt(path);
8531da177e4SLinus Torvalds 	if (mounted) {
8549393bd07SAl Viro 		dput(path->dentry);
8559393bd07SAl Viro 		mntput(path->mnt);
8569393bd07SAl Viro 		path->mnt = mounted;
8579393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
8581da177e4SLinus Torvalds 		return 1;
8591da177e4SLinus Torvalds 	}
8601da177e4SLinus Torvalds 	return 0;
8611da177e4SLinus Torvalds }
8621da177e4SLinus Torvalds 
86362a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
86462a7375eSIan Kent {
86562a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
86662a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
86762a7375eSIan Kent }
86862a7375eSIan Kent 
8699875cf80SDavid Howells /*
870287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
871287548e4SAl Viro  * we meet a managed dentry that would need blocking.
8729875cf80SDavid Howells  */
8739875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
874287548e4SAl Viro 			       struct inode **inode)
8759875cf80SDavid Howells {
87662a7375eSIan Kent 	for (;;) {
8779875cf80SDavid Howells 		struct vfsmount *mounted;
87862a7375eSIan Kent 		/*
87962a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
88062a7375eSIan Kent 		 * that wants to block transit.
88162a7375eSIan Kent 		 */
882287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
883ab90911fSDavid Howells 			return false;
88462a7375eSIan Kent 
88562a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
88662a7375eSIan Kent 			break;
88762a7375eSIan Kent 
8889875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
8899875cf80SDavid Howells 		if (!mounted)
8909875cf80SDavid Howells 			break;
8919875cf80SDavid Howells 		path->mnt = mounted;
8929875cf80SDavid Howells 		path->dentry = mounted->mnt_root;
8939875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
89459430262SLinus Torvalds 		/*
89559430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
89659430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
89759430262SLinus Torvalds 		 * because a mount-point is always pinned.
89859430262SLinus Torvalds 		 */
89959430262SLinus Torvalds 		*inode = path->dentry->d_inode;
9009875cf80SDavid Howells 	}
9019875cf80SDavid Howells 	return true;
9029875cf80SDavid Howells }
9039875cf80SDavid Howells 
904dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
905287548e4SAl Viro {
906dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
907287548e4SAl Viro 		struct vfsmount *mounted;
908dea39376SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
909287548e4SAl Viro 		if (!mounted)
910287548e4SAl Viro 			break;
911dea39376SAl Viro 		nd->path.mnt = mounted;
912dea39376SAl Viro 		nd->path.dentry = mounted->mnt_root;
913dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
914287548e4SAl Viro 	}
915287548e4SAl Viro }
916287548e4SAl Viro 
91731e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
91831e6b01fSNick Piggin {
91931e6b01fSNick Piggin 	set_root_rcu(nd);
92031e6b01fSNick Piggin 
92131e6b01fSNick Piggin 	while (1) {
92231e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
92331e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
92431e6b01fSNick Piggin 			break;
92531e6b01fSNick Piggin 		}
92631e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
92731e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
92831e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
92931e6b01fSNick Piggin 			unsigned seq;
93031e6b01fSNick Piggin 
93131e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
93231e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
933ef7562d5SAl Viro 				goto failed;
93431e6b01fSNick Piggin 			nd->path.dentry = parent;
93531e6b01fSNick Piggin 			nd->seq = seq;
93631e6b01fSNick Piggin 			break;
93731e6b01fSNick Piggin 		}
93831e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
93931e6b01fSNick Piggin 			break;
94031e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
94131e6b01fSNick Piggin 	}
942dea39376SAl Viro 	follow_mount_rcu(nd);
943dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
94431e6b01fSNick Piggin 	return 0;
945ef7562d5SAl Viro 
946ef7562d5SAl Viro failed:
947ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
9485b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
949ef7562d5SAl Viro 		nd->root.mnt = NULL;
950ef7562d5SAl Viro 	rcu_read_unlock();
951ef7562d5SAl Viro 	br_read_unlock(vfsmount_lock);
952ef7562d5SAl Viro 	return -ECHILD;
95331e6b01fSNick Piggin }
95431e6b01fSNick Piggin 
9559875cf80SDavid Howells /*
956cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
957cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
958cc53ce53SDavid Howells  * caller is permitted to proceed or not.
959cc53ce53SDavid Howells  */
9607cc90cc3SAl Viro int follow_down(struct path *path)
961cc53ce53SDavid Howells {
962cc53ce53SDavid Howells 	unsigned managed;
963cc53ce53SDavid Howells 	int ret;
964cc53ce53SDavid Howells 
965cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
966cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
967cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
968cc53ce53SDavid Howells 		 * being held.
969cc53ce53SDavid Howells 		 *
970cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
971cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
972cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
973cc53ce53SDavid Howells 		 * superstructure.
974cc53ce53SDavid Howells 		 *
975cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
976cc53ce53SDavid Howells 		 */
977cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
978cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
979cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
980ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
9811aed3e42SAl Viro 				path->dentry, false);
982cc53ce53SDavid Howells 			if (ret < 0)
983cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
984cc53ce53SDavid Howells 		}
985cc53ce53SDavid Howells 
986cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
987cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
988cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
989cc53ce53SDavid Howells 			if (!mounted)
990cc53ce53SDavid Howells 				break;
991cc53ce53SDavid Howells 			dput(path->dentry);
992cc53ce53SDavid Howells 			mntput(path->mnt);
993cc53ce53SDavid Howells 			path->mnt = mounted;
994cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
995cc53ce53SDavid Howells 			continue;
996cc53ce53SDavid Howells 		}
997cc53ce53SDavid Howells 
998cc53ce53SDavid Howells 		/* Don't handle automount points here */
999cc53ce53SDavid Howells 		break;
1000cc53ce53SDavid Howells 	}
1001cc53ce53SDavid Howells 	return 0;
1002cc53ce53SDavid Howells }
1003cc53ce53SDavid Howells 
1004cc53ce53SDavid Howells /*
10059875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
10069875cf80SDavid Howells  */
10079875cf80SDavid Howells static void follow_mount(struct path *path)
10089875cf80SDavid Howells {
10099875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
10109875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
10119875cf80SDavid Howells 		if (!mounted)
10129875cf80SDavid Howells 			break;
10139875cf80SDavid Howells 		dput(path->dentry);
10149875cf80SDavid Howells 		mntput(path->mnt);
10159875cf80SDavid Howells 		path->mnt = mounted;
10169875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
10179875cf80SDavid Howells 	}
10189875cf80SDavid Howells }
10199875cf80SDavid Howells 
102031e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
10211da177e4SLinus Torvalds {
10222a737871SAl Viro 	set_root(nd);
1023e518ddb7SAndreas Mohr 
10241da177e4SLinus Torvalds 	while(1) {
10254ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
10261da177e4SLinus Torvalds 
10272a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
10282a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
10291da177e4SLinus Torvalds 			break;
10301da177e4SLinus Torvalds 		}
10314ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
10323088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
10333088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
10341da177e4SLinus Torvalds 			dput(old);
10351da177e4SLinus Torvalds 			break;
10361da177e4SLinus Torvalds 		}
10373088dd70SAl Viro 		if (!follow_up(&nd->path))
10381da177e4SLinus Torvalds 			break;
10391da177e4SLinus Torvalds 	}
104079ed0226SAl Viro 	follow_mount(&nd->path);
104131e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
10421da177e4SLinus Torvalds }
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds /*
1045baa03890SNick Piggin  * Allocate a dentry with name and parent, and perform a parent
1046baa03890SNick Piggin  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1047baa03890SNick Piggin  * on error. parent->d_inode->i_mutex must be held. d_lookup must
1048baa03890SNick Piggin  * have verified that no child exists while under i_mutex.
1049baa03890SNick Piggin  */
1050baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1051baa03890SNick Piggin 				struct qstr *name, struct nameidata *nd)
1052baa03890SNick Piggin {
1053baa03890SNick Piggin 	struct inode *inode = parent->d_inode;
1054baa03890SNick Piggin 	struct dentry *dentry;
1055baa03890SNick Piggin 	struct dentry *old;
1056baa03890SNick Piggin 
1057baa03890SNick Piggin 	/* Don't create child dentry for a dead directory. */
1058baa03890SNick Piggin 	if (unlikely(IS_DEADDIR(inode)))
1059baa03890SNick Piggin 		return ERR_PTR(-ENOENT);
1060baa03890SNick Piggin 
1061baa03890SNick Piggin 	dentry = d_alloc(parent, name);
1062baa03890SNick Piggin 	if (unlikely(!dentry))
1063baa03890SNick Piggin 		return ERR_PTR(-ENOMEM);
1064baa03890SNick Piggin 
1065baa03890SNick Piggin 	old = inode->i_op->lookup(inode, dentry, nd);
1066baa03890SNick Piggin 	if (unlikely(old)) {
1067baa03890SNick Piggin 		dput(dentry);
1068baa03890SNick Piggin 		dentry = old;
1069baa03890SNick Piggin 	}
1070baa03890SNick Piggin 	return dentry;
1071baa03890SNick Piggin }
1072baa03890SNick Piggin 
1073baa03890SNick Piggin /*
107444396f4bSJosef Bacik  * We already have a dentry, but require a lookup to be performed on the parent
107544396f4bSJosef Bacik  * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
107644396f4bSJosef Bacik  * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
107744396f4bSJosef Bacik  * child exists while under i_mutex.
107844396f4bSJosef Bacik  */
107944396f4bSJosef Bacik static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
108044396f4bSJosef Bacik 				     struct nameidata *nd)
108144396f4bSJosef Bacik {
108244396f4bSJosef Bacik 	struct inode *inode = parent->d_inode;
108344396f4bSJosef Bacik 	struct dentry *old;
108444396f4bSJosef Bacik 
108544396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
108644396f4bSJosef Bacik 	if (unlikely(IS_DEADDIR(inode)))
108744396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
108844396f4bSJosef Bacik 
108944396f4bSJosef Bacik 	old = inode->i_op->lookup(inode, dentry, nd);
109044396f4bSJosef Bacik 	if (unlikely(old)) {
109144396f4bSJosef Bacik 		dput(dentry);
109244396f4bSJosef Bacik 		dentry = old;
109344396f4bSJosef Bacik 	}
109444396f4bSJosef Bacik 	return dentry;
109544396f4bSJosef Bacik }
109644396f4bSJosef Bacik 
109744396f4bSJosef Bacik /*
10981da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
10991da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
11001da177e4SLinus Torvalds  *  It _is_ time-critical.
11011da177e4SLinus Torvalds  */
11021da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
110331e6b01fSNick Piggin 			struct path *path, struct inode **inode)
11041da177e4SLinus Torvalds {
11054ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
110631e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
11075a18fff2SAl Viro 	int need_reval = 1;
11085a18fff2SAl Viro 	int status = 1;
11099875cf80SDavid Howells 	int err;
11109875cf80SDavid Howells 
11113cac260aSAl Viro 	/*
1112b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1113b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1114b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1115b04f784eSNick Piggin 	 */
111631e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
111731e6b01fSNick Piggin 		unsigned seq;
111831e6b01fSNick Piggin 		*inode = nd->inode;
111931e6b01fSNick Piggin 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
11205a18fff2SAl Viro 		if (!dentry)
11215a18fff2SAl Viro 			goto unlazy;
11225a18fff2SAl Viro 
112331e6b01fSNick Piggin 		/* Memory barrier in read_seqcount_begin of child is enough */
112431e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
112531e6b01fSNick Piggin 			return -ECHILD;
112631e6b01fSNick Piggin 		nd->seq = seq;
11275a18fff2SAl Viro 
112824643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
11295a18fff2SAl Viro 			status = d_revalidate(dentry, nd);
11305a18fff2SAl Viro 			if (unlikely(status <= 0)) {
11315a18fff2SAl Viro 				if (status != -ECHILD)
11325a18fff2SAl Viro 					need_reval = 0;
11335a18fff2SAl Viro 				goto unlazy;
11345a18fff2SAl Viro 			}
113524643087SAl Viro 		}
113644396f4bSJosef Bacik 		if (unlikely(d_need_lookup(dentry)))
113744396f4bSJosef Bacik 			goto unlazy;
113831e6b01fSNick Piggin 		path->mnt = mnt;
113931e6b01fSNick Piggin 		path->dentry = dentry;
1140d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1141d6e9bd25SAl Viro 			goto unlazy;
1142d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1143d6e9bd25SAl Viro 			goto unlazy;
11449875cf80SDavid Howells 		return 0;
11455a18fff2SAl Viro unlazy:
114619660af7SAl Viro 		if (unlazy_walk(nd, dentry))
11475a18fff2SAl Viro 			return -ECHILD;
11485a18fff2SAl Viro 	} else {
114931e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
115024643087SAl Viro 	}
11515a18fff2SAl Viro 
115244396f4bSJosef Bacik 	if (dentry && unlikely(d_need_lookup(dentry))) {
115344396f4bSJosef Bacik 		dput(dentry);
115444396f4bSJosef Bacik 		dentry = NULL;
115544396f4bSJosef Bacik 	}
11565a18fff2SAl Viro retry:
11575a18fff2SAl Viro 	if (unlikely(!dentry)) {
11585a18fff2SAl Viro 		struct inode *dir = parent->d_inode;
11595a18fff2SAl Viro 		BUG_ON(nd->inode != dir);
11605a18fff2SAl Viro 
11615a18fff2SAl Viro 		mutex_lock(&dir->i_mutex);
11625a18fff2SAl Viro 		dentry = d_lookup(parent, name);
11635a18fff2SAl Viro 		if (likely(!dentry)) {
11645a18fff2SAl Viro 			dentry = d_alloc_and_lookup(parent, name, nd);
11655a18fff2SAl Viro 			if (IS_ERR(dentry)) {
11665a18fff2SAl Viro 				mutex_unlock(&dir->i_mutex);
11675a18fff2SAl Viro 				return PTR_ERR(dentry);
11685a18fff2SAl Viro 			}
11695a18fff2SAl Viro 			/* known good */
11705a18fff2SAl Viro 			need_reval = 0;
11715a18fff2SAl Viro 			status = 1;
117244396f4bSJosef Bacik 		} else if (unlikely(d_need_lookup(dentry))) {
117344396f4bSJosef Bacik 			dentry = d_inode_lookup(parent, dentry, nd);
117444396f4bSJosef Bacik 			if (IS_ERR(dentry)) {
117544396f4bSJosef Bacik 				mutex_unlock(&dir->i_mutex);
117644396f4bSJosef Bacik 				return PTR_ERR(dentry);
117744396f4bSJosef Bacik 			}
117844396f4bSJosef Bacik 			/* known good */
117944396f4bSJosef Bacik 			need_reval = 0;
118044396f4bSJosef Bacik 			status = 1;
11815a18fff2SAl Viro 		}
11825a18fff2SAl Viro 		mutex_unlock(&dir->i_mutex);
11835a18fff2SAl Viro 	}
11845a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
11855a18fff2SAl Viro 		status = d_revalidate(dentry, nd);
11865a18fff2SAl Viro 	if (unlikely(status <= 0)) {
11875a18fff2SAl Viro 		if (status < 0) {
11885a18fff2SAl Viro 			dput(dentry);
11895a18fff2SAl Viro 			return status;
11905a18fff2SAl Viro 		}
11915a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
11925a18fff2SAl Viro 			dput(dentry);
11935a18fff2SAl Viro 			dentry = NULL;
11945a18fff2SAl Viro 			need_reval = 1;
11955a18fff2SAl Viro 			goto retry;
11965a18fff2SAl Viro 		}
11975a18fff2SAl Viro 	}
11985a18fff2SAl Viro 
11991da177e4SLinus Torvalds 	path->mnt = mnt;
12001da177e4SLinus Torvalds 	path->dentry = dentry;
12019875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
120289312214SIan Kent 	if (unlikely(err < 0)) {
120389312214SIan Kent 		path_put_conditional(path, nd);
12049875cf80SDavid Howells 		return err;
120589312214SIan Kent 	}
120631e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
12071da177e4SLinus Torvalds 	return 0;
12081da177e4SLinus Torvalds }
12091da177e4SLinus Torvalds 
121052094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
121152094c8aSAl Viro {
121252094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
12134ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
121452094c8aSAl Viro 		if (err != -ECHILD)
121552094c8aSAl Viro 			return err;
121619660af7SAl Viro 		if (unlazy_walk(nd, NULL))
121752094c8aSAl Viro 			return -ECHILD;
121852094c8aSAl Viro 	}
12194ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
122052094c8aSAl Viro }
122152094c8aSAl Viro 
12229856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
12239856fa1bSAl Viro {
12249856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
12259856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
12269856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
12279856fa1bSAl Viro 				return -ECHILD;
12289856fa1bSAl Viro 		} else
12299856fa1bSAl Viro 			follow_dotdot(nd);
12309856fa1bSAl Viro 	}
12319856fa1bSAl Viro 	return 0;
12329856fa1bSAl Viro }
12339856fa1bSAl Viro 
1234951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1235951361f9SAl Viro {
1236951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1237951361f9SAl Viro 		path_put(&nd->path);
1238951361f9SAl Viro 	} else {
1239951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
12405b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1241951361f9SAl Viro 			nd->root.mnt = NULL;
1242951361f9SAl Viro 		rcu_read_unlock();
1243951361f9SAl Viro 		br_read_unlock(vfsmount_lock);
1244951361f9SAl Viro 	}
1245951361f9SAl Viro }
1246951361f9SAl Viro 
1247ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
1248ce57dfc1SAl Viro 		struct qstr *name, int type, int follow)
1249ce57dfc1SAl Viro {
1250ce57dfc1SAl Viro 	struct inode *inode;
1251ce57dfc1SAl Viro 	int err;
1252ce57dfc1SAl Viro 	/*
1253ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1254ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1255ce57dfc1SAl Viro 	 * parent relationships.
1256ce57dfc1SAl Viro 	 */
1257ce57dfc1SAl Viro 	if (unlikely(type != LAST_NORM))
1258ce57dfc1SAl Viro 		return handle_dots(nd, type);
1259ce57dfc1SAl Viro 	err = do_lookup(nd, name, path, &inode);
1260ce57dfc1SAl Viro 	if (unlikely(err)) {
1261ce57dfc1SAl Viro 		terminate_walk(nd);
1262ce57dfc1SAl Viro 		return err;
1263ce57dfc1SAl Viro 	}
1264ce57dfc1SAl Viro 	if (!inode) {
1265ce57dfc1SAl Viro 		path_to_nameidata(path, nd);
1266ce57dfc1SAl Viro 		terminate_walk(nd);
1267ce57dfc1SAl Viro 		return -ENOENT;
1268ce57dfc1SAl Viro 	}
1269ce57dfc1SAl Viro 	if (unlikely(inode->i_op->follow_link) && follow) {
127019660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
127119660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
127219660af7SAl Viro 				terminate_walk(nd);
1273ce57dfc1SAl Viro 				return -ECHILD;
127419660af7SAl Viro 			}
127519660af7SAl Viro 		}
1276ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1277ce57dfc1SAl Viro 		return 1;
1278ce57dfc1SAl Viro 	}
1279ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1280ce57dfc1SAl Viro 	nd->inode = inode;
1281ce57dfc1SAl Viro 	return 0;
1282ce57dfc1SAl Viro }
1283ce57dfc1SAl Viro 
12841da177e4SLinus Torvalds /*
1285b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1286b356379aSAl Viro  * limiting consecutive symlinks to 40.
1287b356379aSAl Viro  *
1288b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1289b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1290b356379aSAl Viro  */
1291b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1292b356379aSAl Viro {
1293b356379aSAl Viro 	int res;
1294b356379aSAl Viro 
1295b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1296b356379aSAl Viro 		path_put_conditional(path, nd);
1297b356379aSAl Viro 		path_put(&nd->path);
1298b356379aSAl Viro 		return -ELOOP;
1299b356379aSAl Viro 	}
13001a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1301b356379aSAl Viro 
1302b356379aSAl Viro 	nd->depth++;
1303b356379aSAl Viro 	current->link_count++;
1304b356379aSAl Viro 
1305b356379aSAl Viro 	do {
1306b356379aSAl Viro 		struct path link = *path;
1307b356379aSAl Viro 		void *cookie;
1308574197e0SAl Viro 
1309574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
1310b356379aSAl Viro 		if (!res)
1311b356379aSAl Viro 			res = walk_component(nd, path, &nd->last,
1312b356379aSAl Viro 					     nd->last_type, LOOKUP_FOLLOW);
1313574197e0SAl Viro 		put_link(nd, &link, cookie);
1314b356379aSAl Viro 	} while (res > 0);
1315b356379aSAl Viro 
1316b356379aSAl Viro 	current->link_count--;
1317b356379aSAl Viro 	nd->depth--;
1318b356379aSAl Viro 	return res;
1319b356379aSAl Viro }
1320b356379aSAl Viro 
1321b356379aSAl Viro /*
13221da177e4SLinus Torvalds  * Name resolution.
1323ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1324ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
13251da177e4SLinus Torvalds  *
1326ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1327ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
13281da177e4SLinus Torvalds  */
13296de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
13301da177e4SLinus Torvalds {
13311da177e4SLinus Torvalds 	struct path next;
13321da177e4SLinus Torvalds 	int err;
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 	while (*name=='/')
13351da177e4SLinus Torvalds 		name++;
13361da177e4SLinus Torvalds 	if (!*name)
1337086e183aSAl Viro 		return 0;
13381da177e4SLinus Torvalds 
13391da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
13401da177e4SLinus Torvalds 	for(;;) {
13411da177e4SLinus Torvalds 		unsigned long hash;
13421da177e4SLinus Torvalds 		struct qstr this;
13431da177e4SLinus Torvalds 		unsigned int c;
1344fe479a58SAl Viro 		int type;
13451da177e4SLinus Torvalds 
134652094c8aSAl Viro 		err = may_lookup(nd);
13471da177e4SLinus Torvalds  		if (err)
13481da177e4SLinus Torvalds 			break;
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds 		this.name = name;
13511da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds 		hash = init_name_hash();
13541da177e4SLinus Torvalds 		do {
13551da177e4SLinus Torvalds 			name++;
13561da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
13571da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
13581da177e4SLinus Torvalds 		} while (c && (c != '/'));
13591da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
13601da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
13611da177e4SLinus Torvalds 
1362fe479a58SAl Viro 		type = LAST_NORM;
1363fe479a58SAl Viro 		if (this.name[0] == '.') switch (this.len) {
1364fe479a58SAl Viro 			case 2:
136516c2cd71SAl Viro 				if (this.name[1] == '.') {
1366fe479a58SAl Viro 					type = LAST_DOTDOT;
136716c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
136816c2cd71SAl Viro 				}
1369fe479a58SAl Viro 				break;
1370fe479a58SAl Viro 			case 1:
1371fe479a58SAl Viro 				type = LAST_DOT;
1372fe479a58SAl Viro 		}
13735a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
13745a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
137516c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
13765a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
13775a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
13785a202bcdSAl Viro 							   &this);
13795a202bcdSAl Viro 				if (err < 0)
13805a202bcdSAl Viro 					break;
13815a202bcdSAl Viro 			}
13825a202bcdSAl Viro 		}
1383fe479a58SAl Viro 
13841da177e4SLinus Torvalds 		/* remove trailing slashes? */
13851da177e4SLinus Torvalds 		if (!c)
13861da177e4SLinus Torvalds 			goto last_component;
13871da177e4SLinus Torvalds 		while (*++name == '/');
13881da177e4SLinus Torvalds 		if (!*name)
1389b356379aSAl Viro 			goto last_component;
13901da177e4SLinus Torvalds 
1391ce57dfc1SAl Viro 		err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1392ce57dfc1SAl Viro 		if (err < 0)
1393ce57dfc1SAl Viro 			return err;
1394fe479a58SAl Viro 
1395ce57dfc1SAl Viro 		if (err) {
1396b356379aSAl Viro 			err = nested_symlink(&next, nd);
13971da177e4SLinus Torvalds 			if (err)
1398a7472babSAl Viro 				return err;
139931e6b01fSNick Piggin 		}
14001da177e4SLinus Torvalds 		err = -ENOTDIR;
140131e6b01fSNick Piggin 		if (!nd->inode->i_op->lookup)
14021da177e4SLinus Torvalds 			break;
14031da177e4SLinus Torvalds 		continue;
14041da177e4SLinus Torvalds 		/* here ends the main loop */
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds last_component:
1407ce57dfc1SAl Viro 		nd->last = this;
1408ce57dfc1SAl Viro 		nd->last_type = type;
1409ce57dfc1SAl Viro 		return 0;
1410ce57dfc1SAl Viro 	}
1411951361f9SAl Viro 	terminate_walk(nd);
14121da177e4SLinus Torvalds 	return err;
14131da177e4SLinus Torvalds }
14141da177e4SLinus Torvalds 
141570e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
141670e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
141731e6b01fSNick Piggin {
141831e6b01fSNick Piggin 	int retval = 0;
141931e6b01fSNick Piggin 	int fput_needed;
142031e6b01fSNick Piggin 	struct file *file;
142131e6b01fSNick Piggin 
142231e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
142316c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
142431e6b01fSNick Piggin 	nd->depth = 0;
14255b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
14265b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
142773d049a4SAl Viro 		if (*name) {
14285b6ca027SAl Viro 			if (!inode->i_op->lookup)
14295b6ca027SAl Viro 				return -ENOTDIR;
14305b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
14315b6ca027SAl Viro 			if (retval)
14325b6ca027SAl Viro 				return retval;
143373d049a4SAl Viro 		}
14345b6ca027SAl Viro 		nd->path = nd->root;
14355b6ca027SAl Viro 		nd->inode = inode;
14365b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
14375b6ca027SAl Viro 			br_read_lock(vfsmount_lock);
14385b6ca027SAl Viro 			rcu_read_lock();
14395b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
14405b6ca027SAl Viro 		} else {
14415b6ca027SAl Viro 			path_get(&nd->path);
14425b6ca027SAl Viro 		}
14435b6ca027SAl Viro 		return 0;
14445b6ca027SAl Viro 	}
14455b6ca027SAl Viro 
144631e6b01fSNick Piggin 	nd->root.mnt = NULL;
144731e6b01fSNick Piggin 
144831e6b01fSNick Piggin 	if (*name=='/') {
1449e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
145031e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
145131e6b01fSNick Piggin 			rcu_read_lock();
1452e41f7d4eSAl Viro 			set_root_rcu(nd);
1453e41f7d4eSAl Viro 		} else {
1454e41f7d4eSAl Viro 			set_root(nd);
1455e41f7d4eSAl Viro 			path_get(&nd->root);
1456e41f7d4eSAl Viro 		}
145731e6b01fSNick Piggin 		nd->path = nd->root;
145831e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1459e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
146031e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1461c28cc364SNick Piggin 			unsigned seq;
146231e6b01fSNick Piggin 
146331e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
146431e6b01fSNick Piggin 			rcu_read_lock();
146531e6b01fSNick Piggin 
1466c28cc364SNick Piggin 			do {
1467c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
146831e6b01fSNick Piggin 				nd->path = fs->pwd;
1469c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1470c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1471e41f7d4eSAl Viro 		} else {
1472e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1473e41f7d4eSAl Viro 		}
147431e6b01fSNick Piggin 	} else {
147531e6b01fSNick Piggin 		struct dentry *dentry;
147631e6b01fSNick Piggin 
14771abf0c71SAl Viro 		file = fget_raw_light(dfd, &fput_needed);
147831e6b01fSNick Piggin 		retval = -EBADF;
147931e6b01fSNick Piggin 		if (!file)
148031e6b01fSNick Piggin 			goto out_fail;
148131e6b01fSNick Piggin 
148231e6b01fSNick Piggin 		dentry = file->f_path.dentry;
148331e6b01fSNick Piggin 
1484f52e0c11SAl Viro 		if (*name) {
148531e6b01fSNick Piggin 			retval = -ENOTDIR;
148631e6b01fSNick Piggin 			if (!S_ISDIR(dentry->d_inode->i_mode))
148731e6b01fSNick Piggin 				goto fput_fail;
148831e6b01fSNick Piggin 
14894ad5abb3SAl Viro 			retval = inode_permission(dentry->d_inode, MAY_EXEC);
149031e6b01fSNick Piggin 			if (retval)
149131e6b01fSNick Piggin 				goto fput_fail;
1492f52e0c11SAl Viro 		}
149331e6b01fSNick Piggin 
149431e6b01fSNick Piggin 		nd->path = file->f_path;
1495e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
149631e6b01fSNick Piggin 			if (fput_needed)
149770e9b357SAl Viro 				*fp = file;
1498c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
149931e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
150031e6b01fSNick Piggin 			rcu_read_lock();
15015590ff0dSUlrich Drepper 		} else {
15025dd784d0SJan Blunck 			path_get(&file->f_path);
15035590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
15041da177e4SLinus Torvalds 		}
1505e41f7d4eSAl Viro 	}
1506e41f7d4eSAl Viro 
150731e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
15089b4a9b14SAl Viro 	return 0;
15092dfdd266SJosef 'Jeff' Sipek 
15109b4a9b14SAl Viro fput_fail:
15119b4a9b14SAl Viro 	fput_light(file, fput_needed);
15129b4a9b14SAl Viro out_fail:
15139b4a9b14SAl Viro 	return retval;
15149b4a9b14SAl Viro }
15159b4a9b14SAl Viro 
1516bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1517bd92d7feSAl Viro {
1518bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1519bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1520bd92d7feSAl Viro 
1521bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1522bd92d7feSAl Viro 	return walk_component(nd, path, &nd->last, nd->last_type,
1523bd92d7feSAl Viro 					nd->flags & LOOKUP_FOLLOW);
1524bd92d7feSAl Viro }
1525bd92d7feSAl Viro 
15269b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1527ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
15289b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
15299b4a9b14SAl Viro {
153070e9b357SAl Viro 	struct file *base = NULL;
1531bd92d7feSAl Viro 	struct path path;
1532bd92d7feSAl Viro 	int err;
153331e6b01fSNick Piggin 
153431e6b01fSNick Piggin 	/*
153531e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
153631e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
153731e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
153831e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
153931e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
154031e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
154131e6b01fSNick Piggin 	 * analogue, foo_rcu().
154231e6b01fSNick Piggin 	 *
154331e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
154431e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
154531e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
154631e6b01fSNick Piggin 	 * be able to complete).
154731e6b01fSNick Piggin 	 */
1548bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1549ee0827cdSAl Viro 
1550bd92d7feSAl Viro 	if (unlikely(err))
1551bd92d7feSAl Viro 		return err;
1552ee0827cdSAl Viro 
1553ee0827cdSAl Viro 	current->total_link_count = 0;
1554bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1555bd92d7feSAl Viro 
1556bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1557bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1558bd92d7feSAl Viro 		while (err > 0) {
1559bd92d7feSAl Viro 			void *cookie;
1560bd92d7feSAl Viro 			struct path link = path;
1561bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1562574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
1563bd92d7feSAl Viro 			if (!err)
1564bd92d7feSAl Viro 				err = lookup_last(nd, &path);
1565574197e0SAl Viro 			put_link(nd, &link, cookie);
1566bd92d7feSAl Viro 		}
1567bd92d7feSAl Viro 	}
1568ee0827cdSAl Viro 
15699f1fafeeSAl Viro 	if (!err)
15709f1fafeeSAl Viro 		err = complete_walk(nd);
1571bd92d7feSAl Viro 
1572bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1573bd92d7feSAl Viro 		if (!nd->inode->i_op->lookup) {
1574bd92d7feSAl Viro 			path_put(&nd->path);
1575bd23a539SAl Viro 			err = -ENOTDIR;
1576bd92d7feSAl Viro 		}
1577bd92d7feSAl Viro 	}
157816c2cd71SAl Viro 
157970e9b357SAl Viro 	if (base)
158070e9b357SAl Viro 		fput(base);
1581ee0827cdSAl Viro 
15825b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
158331e6b01fSNick Piggin 		path_put(&nd->root);
158431e6b01fSNick Piggin 		nd->root.mnt = NULL;
158531e6b01fSNick Piggin 	}
1586bd92d7feSAl Viro 	return err;
158731e6b01fSNick Piggin }
158831e6b01fSNick Piggin 
1589ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1590ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1591ee0827cdSAl Viro {
1592ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1593ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1594ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1595ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1596ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1597ee0827cdSAl Viro 
159831e6b01fSNick Piggin 	if (likely(!retval)) {
159931e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
160031e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
160131e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
160231e6b01fSNick Piggin 		}
160331e6b01fSNick Piggin 	}
1604170aa3d0SUlrich Drepper 	return retval;
16051da177e4SLinus Torvalds }
16061da177e4SLinus Torvalds 
1607c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd)
16085590ff0dSUlrich Drepper {
1609c9c6cac0SAl Viro 	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
16105590ff0dSUlrich Drepper }
16115590ff0dSUlrich Drepper 
1612d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1613d1811465SAl Viro {
1614d1811465SAl Viro 	struct nameidata nd;
1615d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1616d1811465SAl Viro 	if (!res)
1617d1811465SAl Viro 		*path = nd.path;
1618d1811465SAl Viro 	return res;
1619d1811465SAl Viro }
1620d1811465SAl Viro 
162116f18200SJosef 'Jeff' Sipek /**
162216f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
162316f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
162416f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
162516f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
162616f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
1627e0a01249SAl Viro  * @path: pointer to struct path to fill
162816f18200SJosef 'Jeff' Sipek  */
162916f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
163016f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
1631e0a01249SAl Viro 		    struct path *path)
163216f18200SJosef 'Jeff' Sipek {
1633e0a01249SAl Viro 	struct nameidata nd;
1634e0a01249SAl Viro 	int err;
1635e0a01249SAl Viro 	nd.root.dentry = dentry;
1636e0a01249SAl Viro 	nd.root.mnt = mnt;
1637e0a01249SAl Viro 	BUG_ON(flags & LOOKUP_PARENT);
16385b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1639e0a01249SAl Viro 	err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1640e0a01249SAl Viro 	if (!err)
1641e0a01249SAl Viro 		*path = nd.path;
1642e0a01249SAl Viro 	return err;
164316f18200SJosef 'Jeff' Sipek }
164416f18200SJosef 'Jeff' Sipek 
1645eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1646eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
16471da177e4SLinus Torvalds {
164881fca444SChristoph Hellwig 	struct inode *inode = base->d_inode;
16491da177e4SLinus Torvalds 	struct dentry *dentry;
16501da177e4SLinus Torvalds 	int err;
16511da177e4SLinus Torvalds 
16524ad5abb3SAl Viro 	err = inode_permission(inode, MAY_EXEC);
165381fca444SChristoph Hellwig 	if (err)
165481fca444SChristoph Hellwig 		return ERR_PTR(err);
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds 	/*
1657b04f784eSNick Piggin 	 * Don't bother with __d_lookup: callers are for creat as
1658b04f784eSNick Piggin 	 * well as unlink, so a lot of the time it would cost
1659b04f784eSNick Piggin 	 * a double lookup.
16606e6b1bd1SAl Viro 	 */
16616e6b1bd1SAl Viro 	dentry = d_lookup(base, name);
16626e6b1bd1SAl Viro 
166344396f4bSJosef Bacik 	if (dentry && d_need_lookup(dentry)) {
166444396f4bSJosef Bacik 		/*
166544396f4bSJosef Bacik 		 * __lookup_hash is called with the parent dir's i_mutex already
166644396f4bSJosef Bacik 		 * held, so we are good to go here.
166744396f4bSJosef Bacik 		 */
166844396f4bSJosef Bacik 		dentry = d_inode_lookup(base, dentry, nd);
166944396f4bSJosef Bacik 		if (IS_ERR(dentry))
167044396f4bSJosef Bacik 			return dentry;
167144396f4bSJosef Bacik 	}
167244396f4bSJosef Bacik 
1673d2d9e9fbSAl Viro 	if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1674d2d9e9fbSAl Viro 		int status = d_revalidate(dentry, nd);
1675d2d9e9fbSAl Viro 		if (unlikely(status <= 0)) {
1676d2d9e9fbSAl Viro 			/*
1677d2d9e9fbSAl Viro 			 * The dentry failed validation.
1678d2d9e9fbSAl Viro 			 * If d_revalidate returned 0 attempt to invalidate
1679d2d9e9fbSAl Viro 			 * the dentry otherwise d_revalidate is asking us
1680d2d9e9fbSAl Viro 			 * to return a fail status.
1681d2d9e9fbSAl Viro 			 */
1682d2d9e9fbSAl Viro 			if (status < 0) {
1683d2d9e9fbSAl Viro 				dput(dentry);
1684d2d9e9fbSAl Viro 				return ERR_PTR(status);
1685d2d9e9fbSAl Viro 			} else if (!d_invalidate(dentry)) {
1686d2d9e9fbSAl Viro 				dput(dentry);
1687d2d9e9fbSAl Viro 				dentry = NULL;
1688d2d9e9fbSAl Viro 			}
1689d2d9e9fbSAl Viro 		}
1690d2d9e9fbSAl Viro 	}
16916e6b1bd1SAl Viro 
16921da177e4SLinus Torvalds 	if (!dentry)
1693baa03890SNick Piggin 		dentry = d_alloc_and_lookup(base, name, nd);
16945a202bcdSAl Viro 
16951da177e4SLinus Torvalds 	return dentry;
16961da177e4SLinus Torvalds }
16971da177e4SLinus Torvalds 
1698057f6c01SJames Morris /*
1699057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1700057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1701057f6c01SJames Morris  * SMP-safe.
1702057f6c01SJames Morris  */
1703a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
17041da177e4SLinus Torvalds {
17054ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
17061da177e4SLinus Torvalds }
17071da177e4SLinus Torvalds 
1708eead1911SChristoph Hellwig /**
1709a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1710eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1711eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1712eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1713eead1911SChristoph Hellwig  *
1714a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1715a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1716eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1717eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1718eead1911SChristoph Hellwig  */
1719057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1720057f6c01SJames Morris {
1721057f6c01SJames Morris 	struct qstr this;
17226a96ba54SAl Viro 	unsigned long hash;
17236a96ba54SAl Viro 	unsigned int c;
1724057f6c01SJames Morris 
17252f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
17262f9092e1SDavid Woodhouse 
17276a96ba54SAl Viro 	this.name = name;
17286a96ba54SAl Viro 	this.len = len;
17296a96ba54SAl Viro 	if (!len)
17306a96ba54SAl Viro 		return ERR_PTR(-EACCES);
17316a96ba54SAl Viro 
17326a96ba54SAl Viro 	hash = init_name_hash();
17336a96ba54SAl Viro 	while (len--) {
17346a96ba54SAl Viro 		c = *(const unsigned char *)name++;
17356a96ba54SAl Viro 		if (c == '/' || c == '\0')
17366a96ba54SAl Viro 			return ERR_PTR(-EACCES);
17376a96ba54SAl Viro 		hash = partial_name_hash(c, hash);
17386a96ba54SAl Viro 	}
17396a96ba54SAl Viro 	this.hash = end_name_hash(hash);
17405a202bcdSAl Viro 	/*
17415a202bcdSAl Viro 	 * See if the low-level filesystem might want
17425a202bcdSAl Viro 	 * to use its own hash..
17435a202bcdSAl Viro 	 */
17445a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
17455a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
17465a202bcdSAl Viro 		if (err < 0)
17475a202bcdSAl Viro 			return ERR_PTR(err);
17485a202bcdSAl Viro 	}
1749eead1911SChristoph Hellwig 
175049705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1751057f6c01SJames Morris }
1752057f6c01SJames Morris 
17532d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
17542d8f3038SAl Viro 		 struct path *path)
17551da177e4SLinus Torvalds {
17562d8f3038SAl Viro 	struct nameidata nd;
1757f52e0c11SAl Viro 	char *tmp = getname_flags(name, flags);
17581da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
17591da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
17602d8f3038SAl Viro 
17612d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
17622d8f3038SAl Viro 
17632d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
17641da177e4SLinus Torvalds 		putname(tmp);
17652d8f3038SAl Viro 		if (!err)
17662d8f3038SAl Viro 			*path = nd.path;
17671da177e4SLinus Torvalds 	}
17681da177e4SLinus Torvalds 	return err;
17691da177e4SLinus Torvalds }
17701da177e4SLinus Torvalds 
17712ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
17722ad94ae6SAl Viro 			struct nameidata *nd, char **name)
17732ad94ae6SAl Viro {
17742ad94ae6SAl Viro 	char *s = getname(path);
17752ad94ae6SAl Viro 	int error;
17762ad94ae6SAl Viro 
17772ad94ae6SAl Viro 	if (IS_ERR(s))
17782ad94ae6SAl Viro 		return PTR_ERR(s);
17792ad94ae6SAl Viro 
17802ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
17812ad94ae6SAl Viro 	if (error)
17822ad94ae6SAl Viro 		putname(s);
17832ad94ae6SAl Viro 	else
17842ad94ae6SAl Viro 		*name = s;
17852ad94ae6SAl Viro 
17862ad94ae6SAl Viro 	return error;
17872ad94ae6SAl Viro }
17882ad94ae6SAl Viro 
17891da177e4SLinus Torvalds /*
17901da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
17911da177e4SLinus Torvalds  * minimal.
17921da177e4SLinus Torvalds  */
17931da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
17941da177e4SLinus Torvalds {
1795da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1796da9592edSDavid Howells 
17971da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
17981da177e4SLinus Torvalds 		return 0;
1799e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
1800e795b717SSerge E. Hallyn 		goto other_userns;
1801da9592edSDavid Howells 	if (inode->i_uid == fsuid)
18021da177e4SLinus Torvalds 		return 0;
1803da9592edSDavid Howells 	if (dir->i_uid == fsuid)
18041da177e4SLinus Torvalds 		return 0;
1805e795b717SSerge E. Hallyn 
1806e795b717SSerge E. Hallyn other_userns:
1807e795b717SSerge E. Hallyn 	return !ns_capable(inode_userns(inode), CAP_FOWNER);
18081da177e4SLinus Torvalds }
18091da177e4SLinus Torvalds 
18101da177e4SLinus Torvalds /*
18111da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
18121da177e4SLinus Torvalds  *  whether the type of victim is right.
18131da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
18141da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
18151da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
18161da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
18171da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
18181da177e4SLinus Torvalds  *	a. be owner of dir, or
18191da177e4SLinus Torvalds  *	b. be owner of victim, or
18201da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
18211da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
18221da177e4SLinus Torvalds  *     links pointing to it.
18231da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
18241da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
18251da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
18261da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
18271da177e4SLinus Torvalds  *     nfs_async_unlink().
18281da177e4SLinus Torvalds  */
1829858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
18301da177e4SLinus Torvalds {
18311da177e4SLinus Torvalds 	int error;
18321da177e4SLinus Torvalds 
18331da177e4SLinus Torvalds 	if (!victim->d_inode)
18341da177e4SLinus Torvalds 		return -ENOENT;
18351da177e4SLinus Torvalds 
18361da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1837cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
18381da177e4SLinus Torvalds 
1839f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
18401da177e4SLinus Torvalds 	if (error)
18411da177e4SLinus Torvalds 		return error;
18421da177e4SLinus Torvalds 	if (IS_APPEND(dir))
18431da177e4SLinus Torvalds 		return -EPERM;
18441da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1845f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
18461da177e4SLinus Torvalds 		return -EPERM;
18471da177e4SLinus Torvalds 	if (isdir) {
18481da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
18491da177e4SLinus Torvalds 			return -ENOTDIR;
18501da177e4SLinus Torvalds 		if (IS_ROOT(victim))
18511da177e4SLinus Torvalds 			return -EBUSY;
18521da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
18531da177e4SLinus Torvalds 		return -EISDIR;
18541da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18551da177e4SLinus Torvalds 		return -ENOENT;
18561da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
18571da177e4SLinus Torvalds 		return -EBUSY;
18581da177e4SLinus Torvalds 	return 0;
18591da177e4SLinus Torvalds }
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
18621da177e4SLinus Torvalds  *  dir.
18631da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
18641da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
18651da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
18661da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
18671da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
18681da177e4SLinus Torvalds  */
1869a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
18701da177e4SLinus Torvalds {
18711da177e4SLinus Torvalds 	if (child->d_inode)
18721da177e4SLinus Torvalds 		return -EEXIST;
18731da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18741da177e4SLinus Torvalds 		return -ENOENT;
1875f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
18761da177e4SLinus Torvalds }
18771da177e4SLinus Torvalds 
18781da177e4SLinus Torvalds /*
18791da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
18801da177e4SLinus Torvalds  */
18811da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
18821da177e4SLinus Torvalds {
18831da177e4SLinus Torvalds 	struct dentry *p;
18841da177e4SLinus Torvalds 
18851da177e4SLinus Torvalds 	if (p1 == p2) {
1886f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
18871da177e4SLinus Torvalds 		return NULL;
18881da177e4SLinus Torvalds 	}
18891da177e4SLinus Torvalds 
1890a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
18911da177e4SLinus Torvalds 
1892e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1893e2761a11SOGAWA Hirofumi 	if (p) {
1894f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1895f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
18961da177e4SLinus Torvalds 		return p;
18971da177e4SLinus Torvalds 	}
18981da177e4SLinus Torvalds 
1899e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1900e2761a11SOGAWA Hirofumi 	if (p) {
1901f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1902f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19031da177e4SLinus Torvalds 		return p;
19041da177e4SLinus Torvalds 	}
19051da177e4SLinus Torvalds 
1906f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1907f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19081da177e4SLinus Torvalds 	return NULL;
19091da177e4SLinus Torvalds }
19101da177e4SLinus Torvalds 
19111da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
19121da177e4SLinus Torvalds {
19131b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
19141da177e4SLinus Torvalds 	if (p1 != p2) {
19151b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1916a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19171da177e4SLinus Torvalds 	}
19181da177e4SLinus Torvalds }
19191da177e4SLinus Torvalds 
19201da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
19211da177e4SLinus Torvalds 		struct nameidata *nd)
19221da177e4SLinus Torvalds {
1923a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
19241da177e4SLinus Torvalds 
19251da177e4SLinus Torvalds 	if (error)
19261da177e4SLinus Torvalds 		return error;
19271da177e4SLinus Torvalds 
1928acfa4380SAl Viro 	if (!dir->i_op->create)
19291da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
19301da177e4SLinus Torvalds 	mode &= S_IALLUGO;
19311da177e4SLinus Torvalds 	mode |= S_IFREG;
19321da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
19331da177e4SLinus Torvalds 	if (error)
19341da177e4SLinus Torvalds 		return error;
19351da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1936a74574aaSStephen Smalley 	if (!error)
1937f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
19381da177e4SLinus Torvalds 	return error;
19391da177e4SLinus Torvalds }
19401da177e4SLinus Torvalds 
194173d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
19421da177e4SLinus Torvalds {
19433fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
19441da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
19451da177e4SLinus Torvalds 	int error;
19461da177e4SLinus Torvalds 
1947bcda7652SAl Viro 	/* O_PATH? */
1948bcda7652SAl Viro 	if (!acc_mode)
1949bcda7652SAl Viro 		return 0;
1950bcda7652SAl Viro 
19511da177e4SLinus Torvalds 	if (!inode)
19521da177e4SLinus Torvalds 		return -ENOENT;
19531da177e4SLinus Torvalds 
1954c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1955c8fe8f30SChristoph Hellwig 	case S_IFLNK:
19561da177e4SLinus Torvalds 		return -ELOOP;
1957c8fe8f30SChristoph Hellwig 	case S_IFDIR:
1958c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
19591da177e4SLinus Torvalds 			return -EISDIR;
1960c8fe8f30SChristoph Hellwig 		break;
1961c8fe8f30SChristoph Hellwig 	case S_IFBLK:
1962c8fe8f30SChristoph Hellwig 	case S_IFCHR:
19633fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
19641da177e4SLinus Torvalds 			return -EACCES;
1965c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
1966c8fe8f30SChristoph Hellwig 	case S_IFIFO:
1967c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
19681da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
1969c8fe8f30SChristoph Hellwig 		break;
19704a3fd211SDave Hansen 	}
1971b41572e9SDave Hansen 
19723fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
1973b41572e9SDave Hansen 	if (error)
1974b41572e9SDave Hansen 		return error;
19756146f0d5SMimi Zohar 
19761da177e4SLinus Torvalds 	/*
19771da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
19781da177e4SLinus Torvalds 	 */
19791da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
19808737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
19817715b521SAl Viro 			return -EPERM;
19821da177e4SLinus Torvalds 		if (flag & O_TRUNC)
19837715b521SAl Viro 			return -EPERM;
19841da177e4SLinus Torvalds 	}
19851da177e4SLinus Torvalds 
19861da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
19872e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
19887715b521SAl Viro 		return -EPERM;
19891da177e4SLinus Torvalds 
19901da177e4SLinus Torvalds 	/*
19911da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
19921da177e4SLinus Torvalds 	 */
1993b65a9cfcSAl Viro 	return break_lease(inode, flag);
19947715b521SAl Viro }
19957715b521SAl Viro 
1996e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
19977715b521SAl Viro {
1998e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
19997715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
20007715b521SAl Viro 	int error = get_write_access(inode);
20011da177e4SLinus Torvalds 	if (error)
20027715b521SAl Viro 		return error;
20031da177e4SLinus Torvalds 	/*
20041da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
20051da177e4SLinus Torvalds 	 */
20061da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2007be6d3e56SKentaro Takeda 	if (!error)
2008ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
20091da177e4SLinus Torvalds 	if (!error) {
20107715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2011d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2012e1181ee6SJeff Layton 				    filp);
20131da177e4SLinus Torvalds 	}
20141da177e4SLinus Torvalds 	put_write_access(inode);
2015acd0c935SMimi Zohar 	return error;
20161da177e4SLinus Torvalds }
20171da177e4SLinus Torvalds 
2018d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2019d57999e1SDave Hansen {
20208a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
20218a5e929dSAl Viro 		flag--;
2022d57999e1SDave Hansen 	return flag;
2023d57999e1SDave Hansen }
2024d57999e1SDave Hansen 
202531e6b01fSNick Piggin /*
2026fe2d35ffSAl Viro  * Handle the last step of open()
202731e6b01fSNick Piggin  */
2028fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
2029c3e380b0SAl Viro 			    const struct open_flags *op, const char *pathname)
2030fb1cc555SAl Viro {
2031a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
20326c0d46c4SAl Viro 	struct dentry *dentry;
2033ca344a89SAl Viro 	int open_flag = op->open_flag;
20346c0d46c4SAl Viro 	int will_truncate = open_flag & O_TRUNC;
2035ca344a89SAl Viro 	int want_write = 0;
2036bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2037fb1cc555SAl Viro 	struct file *filp;
203816c2cd71SAl Viro 	int error;
2039fb1cc555SAl Viro 
2040c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2041c3e380b0SAl Viro 	nd->flags |= op->intent;
2042c3e380b0SAl Viro 
20431f36f774SAl Viro 	switch (nd->last_type) {
20441f36f774SAl Viro 	case LAST_DOTDOT:
2045176306f5SNeil Brown 	case LAST_DOT:
2046fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2047fe2d35ffSAl Viro 		if (error)
2048fe2d35ffSAl Viro 			return ERR_PTR(error);
20491f36f774SAl Viro 		/* fallthrough */
20501f36f774SAl Viro 	case LAST_ROOT:
20519f1fafeeSAl Viro 		error = complete_walk(nd);
205216c2cd71SAl Viro 		if (error)
20539f1fafeeSAl Viro 			return ERR_PTR(error);
2054fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2055ca344a89SAl Viro 		if (open_flag & O_CREAT) {
205616c2cd71SAl Viro 			error = -EISDIR;
20571f36f774SAl Viro 			goto exit;
2058fe2d35ffSAl Viro 		}
2059fe2d35ffSAl Viro 		goto ok;
20601f36f774SAl Viro 	case LAST_BIND:
20619f1fafeeSAl Viro 		error = complete_walk(nd);
206216c2cd71SAl Viro 		if (error)
20639f1fafeeSAl Viro 			return ERR_PTR(error);
20641f36f774SAl Viro 		audit_inode(pathname, dir);
20651f36f774SAl Viro 		goto ok;
20661f36f774SAl Viro 	}
2067a2c36b45SAl Viro 
2068ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2069bcda7652SAl Viro 		int symlink_ok = 0;
2070fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2071fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2072bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2073bcda7652SAl Viro 			symlink_ok = 1;
2074fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2075ce57dfc1SAl Viro 		error = walk_component(nd, path, &nd->last, LAST_NORM,
2076ce57dfc1SAl Viro 					!symlink_ok);
2077ce57dfc1SAl Viro 		if (error < 0)
2078fe2d35ffSAl Viro 			return ERR_PTR(error);
2079ce57dfc1SAl Viro 		if (error) /* symlink */
2080fe2d35ffSAl Viro 			return NULL;
2081fe2d35ffSAl Viro 		/* sayonara */
20829f1fafeeSAl Viro 		error = complete_walk(nd);
20839f1fafeeSAl Viro 		if (error)
2084fe2d35ffSAl Viro 			return ERR_PTR(-ECHILD);
2085fe2d35ffSAl Viro 
2086fe2d35ffSAl Viro 		error = -ENOTDIR;
2087fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_DIRECTORY) {
2088ce57dfc1SAl Viro 			if (!nd->inode->i_op->lookup)
2089fe2d35ffSAl Viro 				goto exit;
2090fe2d35ffSAl Viro 		}
2091fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2092fe2d35ffSAl Viro 		goto ok;
2093fe2d35ffSAl Viro 	}
2094fe2d35ffSAl Viro 
2095fe2d35ffSAl Viro 	/* create side of things */
20969f1fafeeSAl Viro 	error = complete_walk(nd);
20979f1fafeeSAl Viro 	if (error)
20989f1fafeeSAl Viro 		return ERR_PTR(error);
2099fe2d35ffSAl Viro 
2100fe2d35ffSAl Viro 	audit_inode(pathname, dir);
210116c2cd71SAl Viro 	error = -EISDIR;
21021f36f774SAl Viro 	/* trailing slashes? */
210331e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
21041f36f774SAl Viro 		goto exit;
21051f36f774SAl Viro 
2106a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2107a1e28038SAl Viro 
21086c0d46c4SAl Viro 	dentry = lookup_hash(nd);
21096c0d46c4SAl Viro 	error = PTR_ERR(dentry);
21106c0d46c4SAl Viro 	if (IS_ERR(dentry)) {
2111fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2112fb1cc555SAl Viro 		goto exit;
2113fb1cc555SAl Viro 	}
2114fb1cc555SAl Viro 
21156c0d46c4SAl Viro 	path->dentry = dentry;
21166c0d46c4SAl Viro 	path->mnt = nd->path.mnt;
21176c0d46c4SAl Viro 
2118fb1cc555SAl Viro 	/* Negative dentry, just create the file */
21196c0d46c4SAl Viro 	if (!dentry->d_inode) {
21206c0d46c4SAl Viro 		int mode = op->mode;
21216c0d46c4SAl Viro 		if (!IS_POSIXACL(dir->d_inode))
21226c0d46c4SAl Viro 			mode &= ~current_umask();
2123fb1cc555SAl Viro 		/*
2124fb1cc555SAl Viro 		 * This write is needed to ensure that a
21256c0d46c4SAl Viro 		 * rw->ro transition does not occur between
2126fb1cc555SAl Viro 		 * the time when the file is created and when
2127fb1cc555SAl Viro 		 * a permanent write count is taken through
2128fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2129fb1cc555SAl Viro 		 */
2130fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2131fb1cc555SAl Viro 		if (error)
2132fb1cc555SAl Viro 			goto exit_mutex_unlock;
2133ca344a89SAl Viro 		want_write = 1;
21349b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2135ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
21366c0d46c4SAl Viro 		will_truncate = 0;
2137bcda7652SAl Viro 		acc_mode = MAY_OPEN;
21386c0d46c4SAl Viro 		error = security_path_mknod(&nd->path, dentry, mode, 0);
21396c0d46c4SAl Viro 		if (error)
21406c0d46c4SAl Viro 			goto exit_mutex_unlock;
21416c0d46c4SAl Viro 		error = vfs_create(dir->d_inode, dentry, mode, nd);
21426c0d46c4SAl Viro 		if (error)
21436c0d46c4SAl Viro 			goto exit_mutex_unlock;
21446c0d46c4SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
21456c0d46c4SAl Viro 		dput(nd->path.dentry);
21466c0d46c4SAl Viro 		nd->path.dentry = dentry;
2147ca344a89SAl Viro 		goto common;
2148fb1cc555SAl Viro 	}
2149fb1cc555SAl Viro 
2150fb1cc555SAl Viro 	/*
2151fb1cc555SAl Viro 	 * It already exists.
2152fb1cc555SAl Viro 	 */
2153fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2154fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2155fb1cc555SAl Viro 
2156fb1cc555SAl Viro 	error = -EEXIST;
2157ca344a89SAl Viro 	if (open_flag & O_EXCL)
2158fb1cc555SAl Viro 		goto exit_dput;
2159fb1cc555SAl Viro 
21609875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
21619875cf80SDavid Howells 	if (error < 0)
2162fb1cc555SAl Viro 		goto exit_dput;
2163fb1cc555SAl Viro 
2164fb1cc555SAl Viro 	error = -ENOENT;
2165fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2166fb1cc555SAl Viro 		goto exit_dput;
21679e67f361SAl Viro 
21689e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2169fb1cc555SAl Viro 		return NULL;
2170fb1cc555SAl Viro 
2171fb1cc555SAl Viro 	path_to_nameidata(path, nd);
217231e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2173fb1cc555SAl Viro 	error = -EISDIR;
217431e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2175fb1cc555SAl Viro 		goto exit;
217667ee3ad2SAl Viro ok:
21776c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
21786c0d46c4SAl Viro 		will_truncate = 0;
21796c0d46c4SAl Viro 
21800f9d1a10SAl Viro 	if (will_truncate) {
21810f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
21820f9d1a10SAl Viro 		if (error)
21830f9d1a10SAl Viro 			goto exit;
2184ca344a89SAl Viro 		want_write = 1;
21850f9d1a10SAl Viro 	}
2186ca344a89SAl Viro common:
2187bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2188ca344a89SAl Viro 	if (error)
21890f9d1a10SAl Viro 		goto exit;
21900f9d1a10SAl Viro 	filp = nameidata_to_filp(nd);
21910f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
21920f9d1a10SAl Viro 		error = ima_file_check(filp, op->acc_mode);
21930f9d1a10SAl Viro 		if (error) {
21940f9d1a10SAl Viro 			fput(filp);
21950f9d1a10SAl Viro 			filp = ERR_PTR(error);
21960f9d1a10SAl Viro 		}
21970f9d1a10SAl Viro 	}
21980f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
21990f9d1a10SAl Viro 		if (will_truncate) {
22000f9d1a10SAl Viro 			error = handle_truncate(filp);
22010f9d1a10SAl Viro 			if (error) {
22020f9d1a10SAl Viro 				fput(filp);
22030f9d1a10SAl Viro 				filp = ERR_PTR(error);
22040f9d1a10SAl Viro 			}
22050f9d1a10SAl Viro 		}
22060f9d1a10SAl Viro 	}
2207ca344a89SAl Viro out:
2208ca344a89SAl Viro 	if (want_write)
22090f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
22100f9d1a10SAl Viro 	path_put(&nd->path);
2211fb1cc555SAl Viro 	return filp;
2212fb1cc555SAl Viro 
2213fb1cc555SAl Viro exit_mutex_unlock:
2214fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2215fb1cc555SAl Viro exit_dput:
2216fb1cc555SAl Viro 	path_put_conditional(path, nd);
2217fb1cc555SAl Viro exit:
2218ca344a89SAl Viro 	filp = ERR_PTR(error);
2219ca344a89SAl Viro 	goto out;
2220fb1cc555SAl Viro }
2221fb1cc555SAl Viro 
222213aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
222373d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
22241da177e4SLinus Torvalds {
2225fe2d35ffSAl Viro 	struct file *base = NULL;
22264a3fd211SDave Hansen 	struct file *filp;
22279850c056SAl Viro 	struct path path;
222813aab428SAl Viro 	int error;
222931e6b01fSNick Piggin 
223031e6b01fSNick Piggin 	filp = get_empty_filp();
223131e6b01fSNick Piggin 	if (!filp)
223231e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
223331e6b01fSNick Piggin 
223447c805dcSAl Viro 	filp->f_flags = op->open_flag;
223573d049a4SAl Viro 	nd->intent.open.file = filp;
223673d049a4SAl Viro 	nd->intent.open.flags = open_to_namei_flags(op->open_flag);
223773d049a4SAl Viro 	nd->intent.open.create_mode = op->mode;
223831e6b01fSNick Piggin 
223973d049a4SAl Viro 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
224031e6b01fSNick Piggin 	if (unlikely(error))
224113aab428SAl Viro 		goto out_filp;
224231e6b01fSNick Piggin 
2243fe2d35ffSAl Viro 	current->total_link_count = 0;
224473d049a4SAl Viro 	error = link_path_walk(pathname, nd);
224531e6b01fSNick Piggin 	if (unlikely(error))
224631e6b01fSNick Piggin 		goto out_filp;
22471da177e4SLinus Torvalds 
224873d049a4SAl Viro 	filp = do_last(nd, &path, op, pathname);
2249806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
22507b9337aaSNick Piggin 		struct path link = path;
2251def4af30SAl Viro 		void *cookie;
2252574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
225373d049a4SAl Viro 			path_put_conditional(&path, nd);
225473d049a4SAl Viro 			path_put(&nd->path);
225540b39136SAl Viro 			filp = ERR_PTR(-ELOOP);
225640b39136SAl Viro 			break;
225740b39136SAl Viro 		}
225873d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
225973d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2260574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
2261c3e380b0SAl Viro 		if (unlikely(error))
2262f1afe9efSAl Viro 			filp = ERR_PTR(error);
2263c3e380b0SAl Viro 		else
226473d049a4SAl Viro 			filp = do_last(nd, &path, op, pathname);
2265574197e0SAl Viro 		put_link(nd, &link, cookie);
2266806b681cSAl Viro 	}
226710fa8e62SAl Viro out:
226873d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
226973d049a4SAl Viro 		path_put(&nd->root);
2270fe2d35ffSAl Viro 	if (base)
2271fe2d35ffSAl Viro 		fput(base);
227273d049a4SAl Viro 	release_open_intent(nd);
227310fa8e62SAl Viro 	return filp;
22741da177e4SLinus Torvalds 
227531e6b01fSNick Piggin out_filp:
227610fa8e62SAl Viro 	filp = ERR_PTR(error);
227710fa8e62SAl Viro 	goto out;
2278de459215SKirill Korotaev }
22791da177e4SLinus Torvalds 
228013aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
228113aab428SAl Viro 		const struct open_flags *op, int flags)
228213aab428SAl Viro {
228373d049a4SAl Viro 	struct nameidata nd;
228413aab428SAl Viro 	struct file *filp;
228513aab428SAl Viro 
228673d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
228713aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
228873d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
228913aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
229073d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
229113aab428SAl Viro 	return filp;
229213aab428SAl Viro }
229313aab428SAl Viro 
229473d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
229573d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
229673d049a4SAl Viro {
229773d049a4SAl Viro 	struct nameidata nd;
229873d049a4SAl Viro 	struct file *file;
229973d049a4SAl Viro 
230073d049a4SAl Viro 	nd.root.mnt = mnt;
230173d049a4SAl Viro 	nd.root.dentry = dentry;
230273d049a4SAl Viro 
230373d049a4SAl Viro 	flags |= LOOKUP_ROOT;
230473d049a4SAl Viro 
2305bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
230673d049a4SAl Viro 		return ERR_PTR(-ELOOP);
230773d049a4SAl Viro 
230873d049a4SAl Viro 	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
230973d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
231073d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags);
231173d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
231273d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
231373d049a4SAl Viro 	return file;
231473d049a4SAl Viro }
231573d049a4SAl Viro 
2316ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
23171da177e4SLinus Torvalds {
2318c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
2319ed75e95dSAl Viro 	struct nameidata nd;
2320ed75e95dSAl Viro 	int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2321ed75e95dSAl Viro 	if (error)
2322ed75e95dSAl Viro 		return ERR_PTR(error);
23231da177e4SLinus Torvalds 
2324c663e5d8SChristoph Hellwig 	/*
2325c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2326c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2327c663e5d8SChristoph Hellwig 	 */
2328ed75e95dSAl Viro 	if (nd.last_type != LAST_NORM)
2329ed75e95dSAl Viro 		goto out;
2330ed75e95dSAl Viro 	nd.flags &= ~LOOKUP_PARENT;
2331ed75e95dSAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2332ed75e95dSAl Viro 	nd.intent.open.flags = O_EXCL;
2333c663e5d8SChristoph Hellwig 
2334c663e5d8SChristoph Hellwig 	/*
2335c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2336c663e5d8SChristoph Hellwig 	 */
2337ed75e95dSAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2338ed75e95dSAl Viro 	dentry = lookup_hash(&nd);
23391da177e4SLinus Torvalds 	if (IS_ERR(dentry))
23401da177e4SLinus Torvalds 		goto fail;
2341c663e5d8SChristoph Hellwig 
2342e9baf6e5SAl Viro 	if (dentry->d_inode)
2343e9baf6e5SAl Viro 		goto eexist;
2344c663e5d8SChristoph Hellwig 	/*
2345c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2346c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2347c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2348c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2349c663e5d8SChristoph Hellwig 	 */
2350ed75e95dSAl Viro 	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
23511da177e4SLinus Torvalds 		dput(dentry);
23521da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2353ed75e95dSAl Viro 		goto fail;
2354e9baf6e5SAl Viro 	}
2355ed75e95dSAl Viro 	*path = nd.path;
2356e9baf6e5SAl Viro 	return dentry;
2357e9baf6e5SAl Viro eexist:
2358e9baf6e5SAl Viro 	dput(dentry);
2359e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
23601da177e4SLinus Torvalds fail:
2361dae6ad8fSAl Viro 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2362ed75e95dSAl Viro out:
2363dae6ad8fSAl Viro 	path_put(&nd.path);
2364ed75e95dSAl Viro 	return dentry;
2365dae6ad8fSAl Viro }
2366dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
2367dae6ad8fSAl Viro 
2368dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2369dae6ad8fSAl Viro {
2370dae6ad8fSAl Viro 	char *tmp = getname(pathname);
2371dae6ad8fSAl Viro 	struct dentry *res;
2372dae6ad8fSAl Viro 	if (IS_ERR(tmp))
2373dae6ad8fSAl Viro 		return ERR_CAST(tmp);
2374dae6ad8fSAl Viro 	res = kern_path_create(dfd, tmp, path, is_dir);
2375dae6ad8fSAl Viro 	putname(tmp);
2376dae6ad8fSAl Viro 	return res;
2377dae6ad8fSAl Viro }
2378dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
2379dae6ad8fSAl Viro 
23801da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
23811da177e4SLinus Torvalds {
2382a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
23831da177e4SLinus Torvalds 
23841da177e4SLinus Torvalds 	if (error)
23851da177e4SLinus Torvalds 		return error;
23861da177e4SLinus Torvalds 
2387e795b717SSerge E. Hallyn 	if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2388e795b717SSerge E. Hallyn 	    !ns_capable(inode_userns(dir), CAP_MKNOD))
23891da177e4SLinus Torvalds 		return -EPERM;
23901da177e4SLinus Torvalds 
2391acfa4380SAl Viro 	if (!dir->i_op->mknod)
23921da177e4SLinus Torvalds 		return -EPERM;
23931da177e4SLinus Torvalds 
239408ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
239508ce5f16SSerge E. Hallyn 	if (error)
239608ce5f16SSerge E. Hallyn 		return error;
239708ce5f16SSerge E. Hallyn 
23981da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
23991da177e4SLinus Torvalds 	if (error)
24001da177e4SLinus Torvalds 		return error;
24011da177e4SLinus Torvalds 
24021da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2403a74574aaSStephen Smalley 	if (!error)
2404f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
24051da177e4SLinus Torvalds 	return error;
24061da177e4SLinus Torvalds }
24071da177e4SLinus Torvalds 
2408463c3197SDave Hansen static int may_mknod(mode_t mode)
2409463c3197SDave Hansen {
2410463c3197SDave Hansen 	switch (mode & S_IFMT) {
2411463c3197SDave Hansen 	case S_IFREG:
2412463c3197SDave Hansen 	case S_IFCHR:
2413463c3197SDave Hansen 	case S_IFBLK:
2414463c3197SDave Hansen 	case S_IFIFO:
2415463c3197SDave Hansen 	case S_IFSOCK:
2416463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2417463c3197SDave Hansen 		return 0;
2418463c3197SDave Hansen 	case S_IFDIR:
2419463c3197SDave Hansen 		return -EPERM;
2420463c3197SDave Hansen 	default:
2421463c3197SDave Hansen 		return -EINVAL;
2422463c3197SDave Hansen 	}
2423463c3197SDave Hansen }
2424463c3197SDave Hansen 
24252e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
24262e4d0924SHeiko Carstens 		unsigned, dev)
24271da177e4SLinus Torvalds {
24281da177e4SLinus Torvalds 	struct dentry *dentry;
2429dae6ad8fSAl Viro 	struct path path;
2430dae6ad8fSAl Viro 	int error;
24311da177e4SLinus Torvalds 
24321da177e4SLinus Torvalds 	if (S_ISDIR(mode))
24331da177e4SLinus Torvalds 		return -EPERM;
24341da177e4SLinus Torvalds 
2435dae6ad8fSAl Viro 	dentry = user_path_create(dfd, filename, &path, 0);
2436dae6ad8fSAl Viro 	if (IS_ERR(dentry))
2437dae6ad8fSAl Viro 		return PTR_ERR(dentry);
24382ad94ae6SAl Viro 
2439dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
2440ce3b0f8dSAl Viro 		mode &= ~current_umask();
2441463c3197SDave Hansen 	error = may_mknod(mode);
2442463c3197SDave Hansen 	if (error)
2443463c3197SDave Hansen 		goto out_dput;
2444dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
2445463c3197SDave Hansen 	if (error)
2446463c3197SDave Hansen 		goto out_dput;
2447dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
2448be6d3e56SKentaro Takeda 	if (error)
2449be6d3e56SKentaro Takeda 		goto out_drop_write;
24501da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
24511da177e4SLinus Torvalds 		case 0: case S_IFREG:
2452dae6ad8fSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
24531da177e4SLinus Torvalds 			break;
24541da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
2455dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
24561da177e4SLinus Torvalds 					new_decode_dev(dev));
24571da177e4SLinus Torvalds 			break;
24581da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
2459dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
24601da177e4SLinus Torvalds 			break;
24611da177e4SLinus Torvalds 	}
2462be6d3e56SKentaro Takeda out_drop_write:
2463dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
2464463c3197SDave Hansen out_dput:
24651da177e4SLinus Torvalds 	dput(dentry);
2466dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2467dae6ad8fSAl Viro 	path_put(&path);
24681da177e4SLinus Torvalds 
24691da177e4SLinus Torvalds 	return error;
24701da177e4SLinus Torvalds }
24711da177e4SLinus Torvalds 
24723480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
24735590ff0dSUlrich Drepper {
24745590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
24755590ff0dSUlrich Drepper }
24765590ff0dSUlrich Drepper 
24771da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
24781da177e4SLinus Torvalds {
2479a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
24801da177e4SLinus Torvalds 
24811da177e4SLinus Torvalds 	if (error)
24821da177e4SLinus Torvalds 		return error;
24831da177e4SLinus Torvalds 
2484acfa4380SAl Viro 	if (!dir->i_op->mkdir)
24851da177e4SLinus Torvalds 		return -EPERM;
24861da177e4SLinus Torvalds 
24871da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
24881da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
24891da177e4SLinus Torvalds 	if (error)
24901da177e4SLinus Torvalds 		return error;
24911da177e4SLinus Torvalds 
24921da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2493a74574aaSStephen Smalley 	if (!error)
2494f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
24951da177e4SLinus Torvalds 	return error;
24961da177e4SLinus Torvalds }
24971da177e4SLinus Torvalds 
24982e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
24991da177e4SLinus Torvalds {
25006902d925SDave Hansen 	struct dentry *dentry;
2501dae6ad8fSAl Viro 	struct path path;
2502dae6ad8fSAl Viro 	int error;
25031da177e4SLinus Torvalds 
2504dae6ad8fSAl Viro 	dentry = user_path_create(dfd, pathname, &path, 1);
25056902d925SDave Hansen 	if (IS_ERR(dentry))
2506dae6ad8fSAl Viro 		return PTR_ERR(dentry);
25076902d925SDave Hansen 
2508dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
2509ce3b0f8dSAl Viro 		mode &= ~current_umask();
2510dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
2511463c3197SDave Hansen 	if (error)
2512463c3197SDave Hansen 		goto out_dput;
2513dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
2514be6d3e56SKentaro Takeda 	if (error)
2515be6d3e56SKentaro Takeda 		goto out_drop_write;
2516dae6ad8fSAl Viro 	error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
2517be6d3e56SKentaro Takeda out_drop_write:
2518dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
2519463c3197SDave Hansen out_dput:
25201da177e4SLinus Torvalds 	dput(dentry);
2521dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2522dae6ad8fSAl Viro 	path_put(&path);
25231da177e4SLinus Torvalds 	return error;
25241da177e4SLinus Torvalds }
25251da177e4SLinus Torvalds 
25263cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
25275590ff0dSUlrich Drepper {
25285590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
25295590ff0dSUlrich Drepper }
25305590ff0dSUlrich Drepper 
25311da177e4SLinus Torvalds /*
2532a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
2533a71905f0SSage Weil  * should have a usage count of 2 if we're the only user of this
2534a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
2535a71905f0SSage Weil  * then we drop the dentry now.
25361da177e4SLinus Torvalds  *
25371da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
25381da177e4SLinus Torvalds  * do a
25391da177e4SLinus Torvalds  *
25401da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
25411da177e4SLinus Torvalds  *		return -EBUSY;
25421da177e4SLinus Torvalds  *
25431da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
25441da177e4SLinus Torvalds  * that is still in use by something else..
25451da177e4SLinus Torvalds  */
25461da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
25471da177e4SLinus Torvalds {
25481da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
25491da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
255064252c75SSage Weil 	if (dentry->d_count == 1)
25511da177e4SLinus Torvalds 		__d_drop(dentry);
25521da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
25531da177e4SLinus Torvalds }
25541da177e4SLinus Torvalds 
25551da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
25561da177e4SLinus Torvalds {
25571da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
25581da177e4SLinus Torvalds 
25591da177e4SLinus Torvalds 	if (error)
25601da177e4SLinus Torvalds 		return error;
25611da177e4SLinus Torvalds 
2562acfa4380SAl Viro 	if (!dir->i_op->rmdir)
25631da177e4SLinus Torvalds 		return -EPERM;
25641da177e4SLinus Torvalds 
25651b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
2566912dbc15SSage Weil 
25671da177e4SLinus Torvalds 	error = -EBUSY;
2568912dbc15SSage Weil 	if (d_mountpoint(dentry))
2569912dbc15SSage Weil 		goto out;
2570912dbc15SSage Weil 
25711da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
2572912dbc15SSage Weil 	if (error)
2573912dbc15SSage Weil 		goto out;
2574912dbc15SSage Weil 
25753cebde24SSage Weil 	shrink_dcache_parent(dentry);
25761da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
2577912dbc15SSage Weil 	if (error)
2578912dbc15SSage Weil 		goto out;
2579912dbc15SSage Weil 
25801da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
2581d83c49f3SAl Viro 	dont_mount(dentry);
25821da177e4SLinus Torvalds 
2583912dbc15SSage Weil out:
2584912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
2585912dbc15SSage Weil 	if (!error)
2586912dbc15SSage Weil 		d_delete(dentry);
25871da177e4SLinus Torvalds 	return error;
25881da177e4SLinus Torvalds }
25891da177e4SLinus Torvalds 
25905590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
25911da177e4SLinus Torvalds {
25921da177e4SLinus Torvalds 	int error = 0;
25931da177e4SLinus Torvalds 	char * name;
25941da177e4SLinus Torvalds 	struct dentry *dentry;
25951da177e4SLinus Torvalds 	struct nameidata nd;
25961da177e4SLinus Torvalds 
25972ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
25981da177e4SLinus Torvalds 	if (error)
25992ad94ae6SAl Viro 		return error;
26001da177e4SLinus Torvalds 
26011da177e4SLinus Torvalds 	switch(nd.last_type) {
26021da177e4SLinus Torvalds 	case LAST_DOTDOT:
26031da177e4SLinus Torvalds 		error = -ENOTEMPTY;
26041da177e4SLinus Torvalds 		goto exit1;
26051da177e4SLinus Torvalds 	case LAST_DOT:
26061da177e4SLinus Torvalds 		error = -EINVAL;
26071da177e4SLinus Torvalds 		goto exit1;
26081da177e4SLinus Torvalds 	case LAST_ROOT:
26091da177e4SLinus Torvalds 		error = -EBUSY;
26101da177e4SLinus Torvalds 		goto exit1;
26111da177e4SLinus Torvalds 	}
26120612d9fbSOGAWA Hirofumi 
26130612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
26140612d9fbSOGAWA Hirofumi 
26154ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
261649705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
26171da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
26186902d925SDave Hansen 	if (IS_ERR(dentry))
26196902d925SDave Hansen 		goto exit2;
2620e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
2621e6bc45d6STheodore Ts'o 		error = -ENOENT;
2622e6bc45d6STheodore Ts'o 		goto exit3;
2623e6bc45d6STheodore Ts'o 	}
26240622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
26250622753bSDave Hansen 	if (error)
26260622753bSDave Hansen 		goto exit3;
2627be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2628be6d3e56SKentaro Takeda 	if (error)
2629be6d3e56SKentaro Takeda 		goto exit4;
26304ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2631be6d3e56SKentaro Takeda exit4:
26320622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
26330622753bSDave Hansen exit3:
26341da177e4SLinus Torvalds 	dput(dentry);
26356902d925SDave Hansen exit2:
26364ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
26371da177e4SLinus Torvalds exit1:
26381d957f9bSJan Blunck 	path_put(&nd.path);
26391da177e4SLinus Torvalds 	putname(name);
26401da177e4SLinus Torvalds 	return error;
26411da177e4SLinus Torvalds }
26421da177e4SLinus Torvalds 
26433cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
26445590ff0dSUlrich Drepper {
26455590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
26465590ff0dSUlrich Drepper }
26475590ff0dSUlrich Drepper 
26481da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
26491da177e4SLinus Torvalds {
26501da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
26511da177e4SLinus Torvalds 
26521da177e4SLinus Torvalds 	if (error)
26531da177e4SLinus Torvalds 		return error;
26541da177e4SLinus Torvalds 
2655acfa4380SAl Viro 	if (!dir->i_op->unlink)
26561da177e4SLinus Torvalds 		return -EPERM;
26571da177e4SLinus Torvalds 
26581b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
26591da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
26601da177e4SLinus Torvalds 		error = -EBUSY;
26611da177e4SLinus Torvalds 	else {
26621da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2663bec1052eSAl Viro 		if (!error) {
26641da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2665bec1052eSAl Viro 			if (!error)
2666d83c49f3SAl Viro 				dont_mount(dentry);
2667bec1052eSAl Viro 		}
26681da177e4SLinus Torvalds 	}
26691b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
26701da177e4SLinus Torvalds 
26711da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
26721da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2673ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
26741da177e4SLinus Torvalds 		d_delete(dentry);
26751da177e4SLinus Torvalds 	}
26760eeca283SRobert Love 
26771da177e4SLinus Torvalds 	return error;
26781da177e4SLinus Torvalds }
26791da177e4SLinus Torvalds 
26801da177e4SLinus Torvalds /*
26811da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
26821b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
26831da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
26841da177e4SLinus Torvalds  * while waiting on the I/O.
26851da177e4SLinus Torvalds  */
26865590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
26871da177e4SLinus Torvalds {
26882ad94ae6SAl Viro 	int error;
26891da177e4SLinus Torvalds 	char *name;
26901da177e4SLinus Torvalds 	struct dentry *dentry;
26911da177e4SLinus Torvalds 	struct nameidata nd;
26921da177e4SLinus Torvalds 	struct inode *inode = NULL;
26931da177e4SLinus Torvalds 
26942ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
26951da177e4SLinus Torvalds 	if (error)
26962ad94ae6SAl Viro 		return error;
26972ad94ae6SAl Viro 
26981da177e4SLinus Torvalds 	error = -EISDIR;
26991da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
27001da177e4SLinus Torvalds 		goto exit1;
27010612d9fbSOGAWA Hirofumi 
27020612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
27030612d9fbSOGAWA Hirofumi 
27044ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
270549705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
27061da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27071da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
27081da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
270950338b88STörök Edwin 		if (nd.last.name[nd.last.len])
271050338b88STörök Edwin 			goto slashes;
27111da177e4SLinus Torvalds 		inode = dentry->d_inode;
271250338b88STörök Edwin 		if (!inode)
2713e6bc45d6STheodore Ts'o 			goto slashes;
27147de9c6eeSAl Viro 		ihold(inode);
27150622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
27160622753bSDave Hansen 		if (error)
27170622753bSDave Hansen 			goto exit2;
2718be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2719be6d3e56SKentaro Takeda 		if (error)
2720be6d3e56SKentaro Takeda 			goto exit3;
27214ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2722be6d3e56SKentaro Takeda exit3:
27230622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
27241da177e4SLinus Torvalds 	exit2:
27251da177e4SLinus Torvalds 		dput(dentry);
27261da177e4SLinus Torvalds 	}
27274ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27281da177e4SLinus Torvalds 	if (inode)
27291da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
27301da177e4SLinus Torvalds exit1:
27311d957f9bSJan Blunck 	path_put(&nd.path);
27321da177e4SLinus Torvalds 	putname(name);
27331da177e4SLinus Torvalds 	return error;
27341da177e4SLinus Torvalds 
27351da177e4SLinus Torvalds slashes:
27361da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
27371da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
27381da177e4SLinus Torvalds 	goto exit2;
27391da177e4SLinus Torvalds }
27401da177e4SLinus Torvalds 
27412e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
27425590ff0dSUlrich Drepper {
27435590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
27445590ff0dSUlrich Drepper 		return -EINVAL;
27455590ff0dSUlrich Drepper 
27465590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
27475590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
27485590ff0dSUlrich Drepper 
27495590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
27505590ff0dSUlrich Drepper }
27515590ff0dSUlrich Drepper 
27523480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
27535590ff0dSUlrich Drepper {
27545590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
27555590ff0dSUlrich Drepper }
27565590ff0dSUlrich Drepper 
2757db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
27581da177e4SLinus Torvalds {
2759a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
27601da177e4SLinus Torvalds 
27611da177e4SLinus Torvalds 	if (error)
27621da177e4SLinus Torvalds 		return error;
27631da177e4SLinus Torvalds 
2764acfa4380SAl Viro 	if (!dir->i_op->symlink)
27651da177e4SLinus Torvalds 		return -EPERM;
27661da177e4SLinus Torvalds 
27671da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
27681da177e4SLinus Torvalds 	if (error)
27691da177e4SLinus Torvalds 		return error;
27701da177e4SLinus Torvalds 
27711da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2772a74574aaSStephen Smalley 	if (!error)
2773f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
27741da177e4SLinus Torvalds 	return error;
27751da177e4SLinus Torvalds }
27761da177e4SLinus Torvalds 
27772e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
27782e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
27791da177e4SLinus Torvalds {
27802ad94ae6SAl Viro 	int error;
27811da177e4SLinus Torvalds 	char *from;
27826902d925SDave Hansen 	struct dentry *dentry;
2783dae6ad8fSAl Viro 	struct path path;
27841da177e4SLinus Torvalds 
27851da177e4SLinus Torvalds 	from = getname(oldname);
27861da177e4SLinus Torvalds 	if (IS_ERR(from))
27871da177e4SLinus Torvalds 		return PTR_ERR(from);
27882ad94ae6SAl Viro 
2789dae6ad8fSAl Viro 	dentry = user_path_create(newdfd, newname, &path, 0);
27901da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27916902d925SDave Hansen 	if (IS_ERR(dentry))
2792dae6ad8fSAl Viro 		goto out_putname;
27936902d925SDave Hansen 
2794dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
279575c3f29dSDave Hansen 	if (error)
279675c3f29dSDave Hansen 		goto out_dput;
2797dae6ad8fSAl Viro 	error = security_path_symlink(&path, dentry, from);
2798be6d3e56SKentaro Takeda 	if (error)
2799be6d3e56SKentaro Takeda 		goto out_drop_write;
2800dae6ad8fSAl Viro 	error = vfs_symlink(path.dentry->d_inode, dentry, from);
2801be6d3e56SKentaro Takeda out_drop_write:
2802dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
280375c3f29dSDave Hansen out_dput:
28041da177e4SLinus Torvalds 	dput(dentry);
2805dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2806dae6ad8fSAl Viro 	path_put(&path);
28076902d925SDave Hansen out_putname:
28081da177e4SLinus Torvalds 	putname(from);
28091da177e4SLinus Torvalds 	return error;
28101da177e4SLinus Torvalds }
28111da177e4SLinus Torvalds 
28123480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
28135590ff0dSUlrich Drepper {
28145590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
28155590ff0dSUlrich Drepper }
28165590ff0dSUlrich Drepper 
28171da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
28181da177e4SLinus Torvalds {
28191da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
28201da177e4SLinus Torvalds 	int error;
28211da177e4SLinus Torvalds 
28221da177e4SLinus Torvalds 	if (!inode)
28231da177e4SLinus Torvalds 		return -ENOENT;
28241da177e4SLinus Torvalds 
2825a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
28261da177e4SLinus Torvalds 	if (error)
28271da177e4SLinus Torvalds 		return error;
28281da177e4SLinus Torvalds 
28291da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
28301da177e4SLinus Torvalds 		return -EXDEV;
28311da177e4SLinus Torvalds 
28321da177e4SLinus Torvalds 	/*
28331da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
28341da177e4SLinus Torvalds 	 */
28351da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
28361da177e4SLinus Torvalds 		return -EPERM;
2837acfa4380SAl Viro 	if (!dir->i_op->link)
28381da177e4SLinus Torvalds 		return -EPERM;
28397e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
28401da177e4SLinus Torvalds 		return -EPERM;
28411da177e4SLinus Torvalds 
28421da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
28431da177e4SLinus Torvalds 	if (error)
28441da177e4SLinus Torvalds 		return error;
28451da177e4SLinus Torvalds 
28467e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
2847aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
2848aae8a97dSAneesh Kumar K.V 	if (inode->i_nlink == 0)
2849aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
2850aae8a97dSAneesh Kumar K.V 	else
28511da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
28527e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2853e31e14ecSStephen Smalley 	if (!error)
28547e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
28551da177e4SLinus Torvalds 	return error;
28561da177e4SLinus Torvalds }
28571da177e4SLinus Torvalds 
28581da177e4SLinus Torvalds /*
28591da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
28601da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
28611da177e4SLinus Torvalds  * newname.  --KAB
28621da177e4SLinus Torvalds  *
28631da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
28641da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
28651da177e4SLinus Torvalds  * and other special files.  --ADM
28661da177e4SLinus Torvalds  */
28672e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
28682e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
28691da177e4SLinus Torvalds {
28701da177e4SLinus Torvalds 	struct dentry *new_dentry;
2871dae6ad8fSAl Viro 	struct path old_path, new_path;
287211a7b371SAneesh Kumar K.V 	int how = 0;
28731da177e4SLinus Torvalds 	int error;
28741da177e4SLinus Torvalds 
287511a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
2876c04030e1SUlrich Drepper 		return -EINVAL;
287711a7b371SAneesh Kumar K.V 	/*
287811a7b371SAneesh Kumar K.V 	 * To use null names we require CAP_DAC_READ_SEARCH
287911a7b371SAneesh Kumar K.V 	 * This ensures that not everyone will be able to create
288011a7b371SAneesh Kumar K.V 	 * handlink using the passed filedescriptor.
288111a7b371SAneesh Kumar K.V 	 */
288211a7b371SAneesh Kumar K.V 	if (flags & AT_EMPTY_PATH) {
288311a7b371SAneesh Kumar K.V 		if (!capable(CAP_DAC_READ_SEARCH))
288411a7b371SAneesh Kumar K.V 			return -ENOENT;
288511a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
288611a7b371SAneesh Kumar K.V 	}
2887c04030e1SUlrich Drepper 
288811a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
288911a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
289011a7b371SAneesh Kumar K.V 
289111a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
28921da177e4SLinus Torvalds 	if (error)
28932ad94ae6SAl Viro 		return error;
28942ad94ae6SAl Viro 
2895dae6ad8fSAl Viro 	new_dentry = user_path_create(newdfd, newname, &new_path, 0);
28961da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
28976902d925SDave Hansen 	if (IS_ERR(new_dentry))
2898dae6ad8fSAl Viro 		goto out;
2899dae6ad8fSAl Viro 
2900dae6ad8fSAl Viro 	error = -EXDEV;
2901dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
2902dae6ad8fSAl Viro 		goto out_dput;
2903dae6ad8fSAl Viro 	error = mnt_want_write(new_path.mnt);
290475c3f29dSDave Hansen 	if (error)
290575c3f29dSDave Hansen 		goto out_dput;
2906dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
2907be6d3e56SKentaro Takeda 	if (error)
2908be6d3e56SKentaro Takeda 		goto out_drop_write;
2909dae6ad8fSAl Viro 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
2910be6d3e56SKentaro Takeda out_drop_write:
2911dae6ad8fSAl Viro 	mnt_drop_write(new_path.mnt);
291275c3f29dSDave Hansen out_dput:
29131da177e4SLinus Torvalds 	dput(new_dentry);
2914dae6ad8fSAl Viro 	mutex_unlock(&new_path.dentry->d_inode->i_mutex);
2915dae6ad8fSAl Viro 	path_put(&new_path);
29161da177e4SLinus Torvalds out:
29172d8f3038SAl Viro 	path_put(&old_path);
29181da177e4SLinus Torvalds 
29191da177e4SLinus Torvalds 	return error;
29201da177e4SLinus Torvalds }
29211da177e4SLinus Torvalds 
29223480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
29235590ff0dSUlrich Drepper {
2924c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
29255590ff0dSUlrich Drepper }
29265590ff0dSUlrich Drepper 
29271da177e4SLinus Torvalds /*
29281da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
29291da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
29301da177e4SLinus Torvalds  * Problems:
29311da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
29321da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
29331da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
2934a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
29351da177e4SLinus Torvalds  *	   story.
29361da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
29371b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
29381da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
29391da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
2940a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
29411da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
29421da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
29431da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
2944a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
29451da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
29461da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
29471da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
2948e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
29491b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
29501da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2951c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
29521da177e4SLinus Torvalds  *	   locking].
29531da177e4SLinus Torvalds  */
295475c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
29551da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
29561da177e4SLinus Torvalds {
29571da177e4SLinus Torvalds 	int error = 0;
29589055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
29591da177e4SLinus Torvalds 
29601da177e4SLinus Torvalds 	/*
29611da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
29621da177e4SLinus Torvalds 	 * we'll need to flip '..'.
29631da177e4SLinus Torvalds 	 */
29641da177e4SLinus Torvalds 	if (new_dir != old_dir) {
2965f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
29661da177e4SLinus Torvalds 		if (error)
29671da177e4SLinus Torvalds 			return error;
29681da177e4SLinus Torvalds 	}
29691da177e4SLinus Torvalds 
29701da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
29711da177e4SLinus Torvalds 	if (error)
29721da177e4SLinus Torvalds 		return error;
29731da177e4SLinus Torvalds 
2974d83c49f3SAl Viro 	if (target)
29751b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
29769055cba7SSage Weil 
29771da177e4SLinus Torvalds 	error = -EBUSY;
29789055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
29799055cba7SSage Weil 		goto out;
29809055cba7SSage Weil 
29813cebde24SSage Weil 	if (target)
29823cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
29831da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
29849055cba7SSage Weil 	if (error)
29859055cba7SSage Weil 		goto out;
29869055cba7SSage Weil 
29871da177e4SLinus Torvalds 	if (target) {
29881da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
2989d83c49f3SAl Viro 		dont_mount(new_dentry);
2990d83c49f3SAl Viro 	}
29919055cba7SSage Weil out:
29929055cba7SSage Weil 	if (target)
29931b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
2994e31e14ecSStephen Smalley 	if (!error)
2995349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
29961da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
29971da177e4SLinus Torvalds 	return error;
29981da177e4SLinus Torvalds }
29991da177e4SLinus Torvalds 
300075c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
30011da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
30021da177e4SLinus Torvalds {
300351892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
30041da177e4SLinus Torvalds 	int error;
30051da177e4SLinus Torvalds 
30061da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30071da177e4SLinus Torvalds 	if (error)
30081da177e4SLinus Torvalds 		return error;
30091da177e4SLinus Torvalds 
30101da177e4SLinus Torvalds 	dget(new_dentry);
30111da177e4SLinus Torvalds 	if (target)
30121b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
301351892bbbSSage Weil 
30141da177e4SLinus Torvalds 	error = -EBUSY;
301551892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
301651892bbbSSage Weil 		goto out;
301751892bbbSSage Weil 
30181da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
301951892bbbSSage Weil 	if (error)
302051892bbbSSage Weil 		goto out;
302151892bbbSSage Weil 
3022bec1052eSAl Viro 	if (target)
3023d83c49f3SAl Viro 		dont_mount(new_dentry);
3024349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30251da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
302651892bbbSSage Weil out:
30271da177e4SLinus Torvalds 	if (target)
30281b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
30291da177e4SLinus Torvalds 	dput(new_dentry);
30301da177e4SLinus Torvalds 	return error;
30311da177e4SLinus Torvalds }
30321da177e4SLinus Torvalds 
30331da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
30341da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
30351da177e4SLinus Torvalds {
30361da177e4SLinus Torvalds 	int error;
30371da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
303859b0df21SEric Paris 	const unsigned char *old_name;
30391da177e4SLinus Torvalds 
30401da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
30411da177e4SLinus Torvalds  		return 0;
30421da177e4SLinus Torvalds 
30431da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
30441da177e4SLinus Torvalds 	if (error)
30451da177e4SLinus Torvalds 		return error;
30461da177e4SLinus Torvalds 
30471da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3048a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
30491da177e4SLinus Torvalds 	else
30501da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
30511da177e4SLinus Torvalds 	if (error)
30521da177e4SLinus Torvalds 		return error;
30531da177e4SLinus Torvalds 
3054acfa4380SAl Viro 	if (!old_dir->i_op->rename)
30551da177e4SLinus Torvalds 		return -EPERM;
30561da177e4SLinus Torvalds 
30570eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
30580eeca283SRobert Love 
30591da177e4SLinus Torvalds 	if (is_dir)
30601da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
30611da177e4SLinus Torvalds 	else
30621da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3063123df294SAl Viro 	if (!error)
3064123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
30655a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
30660eeca283SRobert Love 	fsnotify_oldname_free(old_name);
30670eeca283SRobert Love 
30681da177e4SLinus Torvalds 	return error;
30691da177e4SLinus Torvalds }
30701da177e4SLinus Torvalds 
30712e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
30722e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
30731da177e4SLinus Torvalds {
30741da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
30751da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
30761da177e4SLinus Torvalds 	struct dentry *trap;
30771da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
30782ad94ae6SAl Viro 	char *from;
30792ad94ae6SAl Viro 	char *to;
30802ad94ae6SAl Viro 	int error;
30811da177e4SLinus Torvalds 
30822ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
30831da177e4SLinus Torvalds 	if (error)
30841da177e4SLinus Torvalds 		goto exit;
30851da177e4SLinus Torvalds 
30862ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
30871da177e4SLinus Torvalds 	if (error)
30881da177e4SLinus Torvalds 		goto exit1;
30891da177e4SLinus Torvalds 
30901da177e4SLinus Torvalds 	error = -EXDEV;
30914ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
30921da177e4SLinus Torvalds 		goto exit2;
30931da177e4SLinus Torvalds 
30944ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
30951da177e4SLinus Torvalds 	error = -EBUSY;
30961da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
30971da177e4SLinus Torvalds 		goto exit2;
30981da177e4SLinus Torvalds 
30994ac91378SJan Blunck 	new_dir = newnd.path.dentry;
31001da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
31011da177e4SLinus Torvalds 		goto exit2;
31021da177e4SLinus Torvalds 
31030612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
31040612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
31054e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
31060612d9fbSOGAWA Hirofumi 
31071da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
31081da177e4SLinus Torvalds 
310949705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
31101da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
31111da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
31121da177e4SLinus Torvalds 		goto exit3;
31131da177e4SLinus Torvalds 	/* source must exist */
31141da177e4SLinus Torvalds 	error = -ENOENT;
31151da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
31161da177e4SLinus Torvalds 		goto exit4;
31171da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
31181da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
31191da177e4SLinus Torvalds 		error = -ENOTDIR;
31201da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
31211da177e4SLinus Torvalds 			goto exit4;
31221da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
31231da177e4SLinus Torvalds 			goto exit4;
31241da177e4SLinus Torvalds 	}
31251da177e4SLinus Torvalds 	/* source should not be ancestor of target */
31261da177e4SLinus Torvalds 	error = -EINVAL;
31271da177e4SLinus Torvalds 	if (old_dentry == trap)
31281da177e4SLinus Torvalds 		goto exit4;
312949705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
31301da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
31311da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
31321da177e4SLinus Torvalds 		goto exit4;
31331da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
31341da177e4SLinus Torvalds 	error = -ENOTEMPTY;
31351da177e4SLinus Torvalds 	if (new_dentry == trap)
31361da177e4SLinus Torvalds 		goto exit5;
31371da177e4SLinus Torvalds 
31389079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
31399079b1ebSDave Hansen 	if (error)
31409079b1ebSDave Hansen 		goto exit5;
3141be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3142be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3143be6d3e56SKentaro Takeda 	if (error)
3144be6d3e56SKentaro Takeda 		goto exit6;
31451da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
31461da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3147be6d3e56SKentaro Takeda exit6:
31489079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
31491da177e4SLinus Torvalds exit5:
31501da177e4SLinus Torvalds 	dput(new_dentry);
31511da177e4SLinus Torvalds exit4:
31521da177e4SLinus Torvalds 	dput(old_dentry);
31531da177e4SLinus Torvalds exit3:
31541da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
31551da177e4SLinus Torvalds exit2:
31561d957f9bSJan Blunck 	path_put(&newnd.path);
31572ad94ae6SAl Viro 	putname(to);
31581da177e4SLinus Torvalds exit1:
31591d957f9bSJan Blunck 	path_put(&oldnd.path);
31601da177e4SLinus Torvalds 	putname(from);
31612ad94ae6SAl Viro exit:
31621da177e4SLinus Torvalds 	return error;
31631da177e4SLinus Torvalds }
31641da177e4SLinus Torvalds 
3165a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
31665590ff0dSUlrich Drepper {
31675590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
31685590ff0dSUlrich Drepper }
31695590ff0dSUlrich Drepper 
31701da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
31711da177e4SLinus Torvalds {
31721da177e4SLinus Torvalds 	int len;
31731da177e4SLinus Torvalds 
31741da177e4SLinus Torvalds 	len = PTR_ERR(link);
31751da177e4SLinus Torvalds 	if (IS_ERR(link))
31761da177e4SLinus Torvalds 		goto out;
31771da177e4SLinus Torvalds 
31781da177e4SLinus Torvalds 	len = strlen(link);
31791da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
31801da177e4SLinus Torvalds 		len = buflen;
31811da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
31821da177e4SLinus Torvalds 		len = -EFAULT;
31831da177e4SLinus Torvalds out:
31841da177e4SLinus Torvalds 	return len;
31851da177e4SLinus Torvalds }
31861da177e4SLinus Torvalds 
31871da177e4SLinus Torvalds /*
31881da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
31891da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
31901da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
31911da177e4SLinus Torvalds  */
31921da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
31931da177e4SLinus Torvalds {
31941da177e4SLinus Torvalds 	struct nameidata nd;
3195cc314eefSLinus Torvalds 	void *cookie;
3196694a1764SMarcin Slusarz 	int res;
3197cc314eefSLinus Torvalds 
31981da177e4SLinus Torvalds 	nd.depth = 0;
3199cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3200694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3201694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3202694a1764SMarcin Slusarz 
3203694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
32041da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3205cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3206694a1764SMarcin Slusarz 	return res;
32071da177e4SLinus Torvalds }
32081da177e4SLinus Torvalds 
32091da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
32101da177e4SLinus Torvalds {
32111da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
32121da177e4SLinus Torvalds }
32131da177e4SLinus Torvalds 
32141da177e4SLinus Torvalds /* get the link contents into pagecache */
32151da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
32161da177e4SLinus Torvalds {
3217ebd09abbSDuane Griffin 	char *kaddr;
32181da177e4SLinus Torvalds 	struct page *page;
32191da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3220090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
32211da177e4SLinus Torvalds 	if (IS_ERR(page))
32226fe6900eSNick Piggin 		return (char*)page;
32231da177e4SLinus Torvalds 	*ppage = page;
3224ebd09abbSDuane Griffin 	kaddr = kmap(page);
3225ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3226ebd09abbSDuane Griffin 	return kaddr;
32271da177e4SLinus Torvalds }
32281da177e4SLinus Torvalds 
32291da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32301da177e4SLinus Torvalds {
32311da177e4SLinus Torvalds 	struct page *page = NULL;
32321da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
32331da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
32341da177e4SLinus Torvalds 	if (page) {
32351da177e4SLinus Torvalds 		kunmap(page);
32361da177e4SLinus Torvalds 		page_cache_release(page);
32371da177e4SLinus Torvalds 	}
32381da177e4SLinus Torvalds 	return res;
32391da177e4SLinus Torvalds }
32401da177e4SLinus Torvalds 
3241cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
32421da177e4SLinus Torvalds {
3243cc314eefSLinus Torvalds 	struct page *page = NULL;
32441da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3245cc314eefSLinus Torvalds 	return page;
32461da177e4SLinus Torvalds }
32471da177e4SLinus Torvalds 
3248cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
32491da177e4SLinus Torvalds {
3250cc314eefSLinus Torvalds 	struct page *page = cookie;
3251cc314eefSLinus Torvalds 
3252cc314eefSLinus Torvalds 	if (page) {
32531da177e4SLinus Torvalds 		kunmap(page);
32541da177e4SLinus Torvalds 		page_cache_release(page);
32551da177e4SLinus Torvalds 	}
32561da177e4SLinus Torvalds }
32571da177e4SLinus Torvalds 
325854566b2cSNick Piggin /*
325954566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
326054566b2cSNick Piggin  */
326154566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
32621da177e4SLinus Torvalds {
32631da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
32640adb25d2SKirill Korotaev 	struct page *page;
3265afddba49SNick Piggin 	void *fsdata;
3266beb497abSDmitriy Monakhov 	int err;
32671da177e4SLinus Torvalds 	char *kaddr;
326854566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
326954566b2cSNick Piggin 	if (nofs)
327054566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
32711da177e4SLinus Torvalds 
32727e53cac4SNeilBrown retry:
3273afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
327454566b2cSNick Piggin 				flags, &page, &fsdata);
32751da177e4SLinus Torvalds 	if (err)
3276afddba49SNick Piggin 		goto fail;
3277afddba49SNick Piggin 
32781da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
32791da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
32801da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
3281afddba49SNick Piggin 
3282afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3283afddba49SNick Piggin 							page, fsdata);
32841da177e4SLinus Torvalds 	if (err < 0)
32851da177e4SLinus Torvalds 		goto fail;
3286afddba49SNick Piggin 	if (err < len-1)
3287afddba49SNick Piggin 		goto retry;
3288afddba49SNick Piggin 
32891da177e4SLinus Torvalds 	mark_inode_dirty(inode);
32901da177e4SLinus Torvalds 	return 0;
32911da177e4SLinus Torvalds fail:
32921da177e4SLinus Torvalds 	return err;
32931da177e4SLinus Torvalds }
32941da177e4SLinus Torvalds 
32950adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
32960adb25d2SKirill Korotaev {
32970adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
329854566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
32990adb25d2SKirill Korotaev }
33000adb25d2SKirill Korotaev 
330192e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
33021da177e4SLinus Torvalds 	.readlink	= generic_readlink,
33031da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
33041da177e4SLinus Torvalds 	.put_link	= page_put_link,
33051da177e4SLinus Torvalds };
33061da177e4SLinus Torvalds 
33072d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3308cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
33091da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
33101da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
33111da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
33121da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
33131da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
33141da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
33151da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
33161da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
33171da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
33180adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
33191da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
33201da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3321d1811465SAl Viro EXPORT_SYMBOL(kern_path);
332216f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3323f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
33241da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
33251da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
33261da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
33271da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
33281da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
33291da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
33301da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
33311da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
33321da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
33331da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
33341da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
33351da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
33361da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
33371da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3338