xref: /openbmc/linux/fs/namei.c (revision e77819e5)
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 {
179e77819e5SLinus Torvalds 	struct posix_acl *acl;
180e77819e5SLinus Torvalds 
181e77819e5SLinus Torvalds 	/*
182e77819e5SLinus Torvalds 	 * Under RCU walk, we cannot even do a "get_cached_acl()",
183e77819e5SLinus Torvalds 	 * because that involves locking and getting a refcount on
184e77819e5SLinus Torvalds 	 * a cached ACL.
185e77819e5SLinus Torvalds 	 *
186e77819e5SLinus Torvalds 	 * So the only case we handle during RCU walking is the
187e77819e5SLinus Torvalds 	 * case of a cached "no ACL at all", which needs no locks
188e77819e5SLinus Torvalds 	 * or refcounts.
189e77819e5SLinus Torvalds 	 */
190e77819e5SLinus Torvalds 	if (mask & MAY_NOT_BLOCK) {
191e77819e5SLinus Torvalds 	        if (negative_cached_acl(inode, ACL_TYPE_ACCESS))
192e77819e5SLinus Torvalds 	                return -EAGAIN;
193e77819e5SLinus Torvalds 	        return -ECHILD;
194e77819e5SLinus Torvalds 	}
195e77819e5SLinus Torvalds 
196e77819e5SLinus Torvalds 	acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
197e77819e5SLinus Torvalds 
198e77819e5SLinus Torvalds 	/*
199e77819e5SLinus Torvalds 	 * A filesystem can force a ACL callback by just never
200e77819e5SLinus Torvalds 	 * filling the ACL cache. But normally you'd fill the
201e77819e5SLinus Torvalds 	 * cache either at inode instantiation time, or on the
202e77819e5SLinus Torvalds 	 * first ->check_acl call.
203e77819e5SLinus Torvalds 	 *
204e77819e5SLinus Torvalds 	 * If the filesystem doesn't have a check_acl() function
205e77819e5SLinus Torvalds 	 * at all, we'll just create the negative cache entry.
206e77819e5SLinus Torvalds 	 */
207e77819e5SLinus Torvalds 	if (acl == ACL_NOT_CACHED) {
208e77819e5SLinus Torvalds 	        if (inode->i_op->check_acl)
209e77819e5SLinus Torvalds 	                return inode->i_op->check_acl(inode, mask);
210e77819e5SLinus Torvalds 
211e77819e5SLinus Torvalds 	        set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
212e77819e5SLinus Torvalds 	        return -EAGAIN;
213e77819e5SLinus Torvalds 	}
214e77819e5SLinus Torvalds 
215e77819e5SLinus Torvalds 	if (acl) {
216e77819e5SLinus Torvalds 	        int error = posix_acl_permission(inode, acl, mask);
217e77819e5SLinus Torvalds 	        posix_acl_release(acl);
218e77819e5SLinus Torvalds 	        return error;
219e77819e5SLinus Torvalds 	}
220e77819e5SLinus Torvalds 
221e77819e5SLinus Torvalds 	return -EAGAIN;
222e77819e5SLinus Torvalds }
223e77819e5SLinus Torvalds 
2245909ccaaSLinus Torvalds /*
2255909ccaaSLinus Torvalds  * This does basic POSIX ACL permission checking
2265909ccaaSLinus Torvalds  */
2277e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask)
2285909ccaaSLinus Torvalds {
22926cf46beSLinus Torvalds 	unsigned int mode = inode->i_mode;
2305909ccaaSLinus Torvalds 
2319c2c7039SAl Viro 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
2325909ccaaSLinus Torvalds 
233e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
234e795b717SSerge E. Hallyn 		goto other_perms;
235e795b717SSerge E. Hallyn 
2365909ccaaSLinus Torvalds 	if (current_fsuid() == inode->i_uid)
2375909ccaaSLinus Torvalds 		mode >>= 6;
2385909ccaaSLinus Torvalds 	else {
239e77819e5SLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
2407e40145eSAl Viro 			int error = check_acl(inode, mask);
2415909ccaaSLinus Torvalds 			if (error != -EAGAIN)
2425909ccaaSLinus Torvalds 				return error;
2435909ccaaSLinus Torvalds 		}
2445909ccaaSLinus Torvalds 
2455909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
2465909ccaaSLinus Torvalds 			mode >>= 3;
2475909ccaaSLinus Torvalds 	}
2485909ccaaSLinus Torvalds 
249e795b717SSerge E. Hallyn other_perms:
2505909ccaaSLinus Torvalds 	/*
2515909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
2525909ccaaSLinus Torvalds 	 */
2539c2c7039SAl Viro 	if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
2545909ccaaSLinus Torvalds 		return 0;
2555909ccaaSLinus Torvalds 	return -EACCES;
2565909ccaaSLinus Torvalds }
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds /**
2591da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
2601da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2611da177e4SLinus Torvalds  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
2621da177e4SLinus Torvalds  *
2631da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
2641da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
2651da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
266b74c79e9SNick Piggin  * are used for other things.
267b74c79e9SNick Piggin  *
268b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
269b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
270b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
2711da177e4SLinus Torvalds  */
2722830ba7fSAl Viro int generic_permission(struct inode *inode, int mask)
2731da177e4SLinus Torvalds {
2745909ccaaSLinus Torvalds 	int ret;
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	/*
2775909ccaaSLinus Torvalds 	 * Do the basic POSIX ACL permission checks.
2781da177e4SLinus Torvalds 	 */
2797e40145eSAl Viro 	ret = acl_permission_check(inode, mask);
2805909ccaaSLinus Torvalds 	if (ret != -EACCES)
2815909ccaaSLinus Torvalds 		return ret;
2821da177e4SLinus Torvalds 
283d594e7ecSAl Viro 	if (S_ISDIR(inode->i_mode)) {
284d594e7ecSAl Viro 		/* DACs are overridable for directories */
285d594e7ecSAl Viro 		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
286d594e7ecSAl Viro 			return 0;
287d594e7ecSAl Viro 		if (!(mask & MAY_WRITE))
288d594e7ecSAl Viro 			if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
289d594e7ecSAl Viro 				return 0;
290d594e7ecSAl Viro 		return -EACCES;
291d594e7ecSAl Viro 	}
2921da177e4SLinus Torvalds 	/*
2931da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
294d594e7ecSAl Viro 	 * Executable DACs are overridable when there is
295d594e7ecSAl Viro 	 * at least one exec bit set.
2961da177e4SLinus Torvalds 	 */
297d594e7ecSAl Viro 	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
298e795b717SSerge E. Hallyn 		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
2991da177e4SLinus Torvalds 			return 0;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	/*
3021da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
3031da177e4SLinus Torvalds 	 */
3047ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
305d594e7ecSAl Viro 	if (mask == MAY_READ)
306e795b717SSerge E. Hallyn 		if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
3071da177e4SLinus Torvalds 			return 0;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	return -EACCES;
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
312cb23beb5SChristoph Hellwig /**
313cb23beb5SChristoph Hellwig  * inode_permission  -  check for access rights to a given inode
314cb23beb5SChristoph Hellwig  * @inode:	inode to check permission on
315cb23beb5SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
316cb23beb5SChristoph Hellwig  *
317cb23beb5SChristoph Hellwig  * Used to check for read/write/execute permissions on an inode.
318cb23beb5SChristoph Hellwig  * We use "fsuid" for this, letting us set arbitrary permissions
319cb23beb5SChristoph Hellwig  * for filesystem access without changing the "normal" uids which
320cb23beb5SChristoph Hellwig  * are used for other things.
321cb23beb5SChristoph Hellwig  */
322f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask)
3231da177e4SLinus Torvalds {
324e6305c43SAl Viro 	int retval;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	if (mask & MAY_WRITE) {
32722590e41SMiklos Szeredi 		umode_t mode = inode->i_mode;
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 		/*
3301da177e4SLinus Torvalds 		 * Nobody gets write access to a read-only fs.
3311da177e4SLinus Torvalds 		 */
3321da177e4SLinus Torvalds 		if (IS_RDONLY(inode) &&
3331da177e4SLinus Torvalds 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
3341da177e4SLinus Torvalds 			return -EROFS;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 		/*
3371da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
3381da177e4SLinus Torvalds 		 */
3391da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
3401da177e4SLinus Torvalds 			return -EACCES;
3411da177e4SLinus Torvalds 	}
3421da177e4SLinus Torvalds 
343acfa4380SAl Viro 	if (inode->i_op->permission)
34410556cb2SAl Viro 		retval = inode->i_op->permission(inode, mask);
345f696a365SMiklos Szeredi 	else
3462830ba7fSAl Viro 		retval = generic_permission(inode, mask);
347f696a365SMiklos Szeredi 
3481da177e4SLinus Torvalds 	if (retval)
3491da177e4SLinus Torvalds 		return retval;
3501da177e4SLinus Torvalds 
35108ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
35208ce5f16SSerge E. Hallyn 	if (retval)
35308ce5f16SSerge E. Hallyn 		return retval;
35408ce5f16SSerge E. Hallyn 
355d09ca739SEric Paris 	return security_inode_permission(inode, mask);
3561da177e4SLinus Torvalds }
3571da177e4SLinus Torvalds 
358f4d6ff89SAl Viro /**
3595dd784d0SJan Blunck  * path_get - get a reference to a path
3605dd784d0SJan Blunck  * @path: path to get the reference to
3615dd784d0SJan Blunck  *
3625dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3635dd784d0SJan Blunck  */
3645dd784d0SJan Blunck void path_get(struct path *path)
3655dd784d0SJan Blunck {
3665dd784d0SJan Blunck 	mntget(path->mnt);
3675dd784d0SJan Blunck 	dget(path->dentry);
3685dd784d0SJan Blunck }
3695dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
3705dd784d0SJan Blunck 
3715dd784d0SJan Blunck /**
3721d957f9bSJan Blunck  * path_put - put a reference to a path
3731d957f9bSJan Blunck  * @path: path to put the reference to
3741d957f9bSJan Blunck  *
3751d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
3761d957f9bSJan Blunck  */
3771d957f9bSJan Blunck void path_put(struct path *path)
3781da177e4SLinus Torvalds {
3791d957f9bSJan Blunck 	dput(path->dentry);
3801d957f9bSJan Blunck 	mntput(path->mnt);
3811da177e4SLinus Torvalds }
3821d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
3831da177e4SLinus Torvalds 
38419660af7SAl Viro /*
38531e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
38619660af7SAl Viro  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
38719660af7SAl Viro  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
38819660af7SAl Viro  * normal reference counts on dentries and vfsmounts to transition to rcu-walk
38919660af7SAl Viro  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
39019660af7SAl Viro  * got stuck, so ref-walk may continue from there. If this is not successful
39119660af7SAl Viro  * (eg. a seqcount has changed), then failure is returned and it's up to caller
39219660af7SAl Viro  * to restart the path walk from the beginning in ref-walk mode.
39331e6b01fSNick Piggin  */
39431e6b01fSNick Piggin 
39531e6b01fSNick Piggin /**
39619660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
39719660af7SAl Viro  * @nd: nameidata pathwalk data
39819660af7SAl Viro  * @dentry: child of nd->path.dentry or NULL
39939191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
40031e6b01fSNick Piggin  *
40119660af7SAl Viro  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
40219660af7SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
40319660af7SAl Viro  * @nd or NULL.  Must be called from rcu-walk context.
40431e6b01fSNick Piggin  */
40519660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
40631e6b01fSNick Piggin {
40731e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
40831e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
4095b6ca027SAl Viro 	int want_root = 0;
41031e6b01fSNick Piggin 
41131e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
4125b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
4135b6ca027SAl Viro 		want_root = 1;
41431e6b01fSNick Piggin 		spin_lock(&fs->lock);
41531e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
41631e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
41731e6b01fSNick Piggin 			goto err_root;
41831e6b01fSNick Piggin 	}
41931e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
42019660af7SAl Viro 	if (!dentry) {
42119660af7SAl Viro 		if (!__d_rcu_to_refcount(parent, nd->seq))
42219660af7SAl Viro 			goto err_parent;
42319660af7SAl Viro 		BUG_ON(nd->inode != parent->d_inode);
42419660af7SAl Viro 	} else {
42594c0d4ecSAl Viro 		if (dentry->d_parent != parent)
42694c0d4ecSAl Viro 			goto err_parent;
42731e6b01fSNick Piggin 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
42831e6b01fSNick Piggin 		if (!__d_rcu_to_refcount(dentry, nd->seq))
42919660af7SAl Viro 			goto err_child;
43031e6b01fSNick Piggin 		/*
43119660af7SAl Viro 		 * If the sequence check on the child dentry passed, then
43219660af7SAl Viro 		 * the child has not been removed from its parent. This
43319660af7SAl Viro 		 * means the parent dentry must be valid and able to take
43419660af7SAl Viro 		 * a reference at this point.
43531e6b01fSNick Piggin 		 */
43631e6b01fSNick Piggin 		BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
43731e6b01fSNick Piggin 		BUG_ON(!parent->d_count);
43831e6b01fSNick Piggin 		parent->d_count++;
43931e6b01fSNick Piggin 		spin_unlock(&dentry->d_lock);
44019660af7SAl Viro 	}
44131e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
4425b6ca027SAl Viro 	if (want_root) {
44331e6b01fSNick Piggin 		path_get(&nd->root);
44431e6b01fSNick Piggin 		spin_unlock(&fs->lock);
44531e6b01fSNick Piggin 	}
44631e6b01fSNick Piggin 	mntget(nd->path.mnt);
44731e6b01fSNick Piggin 
44831e6b01fSNick Piggin 	rcu_read_unlock();
44931e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
45031e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
45131e6b01fSNick Piggin 	return 0;
45219660af7SAl Viro 
45319660af7SAl Viro err_child:
45431e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
45519660af7SAl Viro err_parent:
45631e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
45731e6b01fSNick Piggin err_root:
4585b6ca027SAl Viro 	if (want_root)
45931e6b01fSNick Piggin 		spin_unlock(&fs->lock);
46031e6b01fSNick Piggin 	return -ECHILD;
46131e6b01fSNick Piggin }
46231e6b01fSNick Piggin 
46331e6b01fSNick Piggin /**
464834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
465834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
466834f2a4aSTrond Myklebust  */
467834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
468834f2a4aSTrond Myklebust {
4692dab5974SLinus Torvalds 	struct file *file = nd->intent.open.file;
4702dab5974SLinus Torvalds 
4712dab5974SLinus Torvalds 	if (file && !IS_ERR(file)) {
4722dab5974SLinus Torvalds 		if (file->f_path.dentry == NULL)
4732dab5974SLinus Torvalds 			put_filp(file);
474834f2a4aSTrond Myklebust 		else
4752dab5974SLinus Torvalds 			fput(file);
4762dab5974SLinus Torvalds 	}
477834f2a4aSTrond Myklebust }
478834f2a4aSTrond Myklebust 
479f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
48034286d66SNick Piggin {
481f60aef7eSAl Viro 	return dentry->d_op->d_revalidate(dentry, nd);
48234286d66SNick Piggin }
48334286d66SNick Piggin 
4849f1fafeeSAl Viro /**
4859f1fafeeSAl Viro  * complete_walk - successful completion of path walk
4869f1fafeeSAl Viro  * @nd:  pointer nameidata
48739159de2SJeff Layton  *
4889f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
4899f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
4909f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
4919f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
4929f1fafeeSAl Viro  * need to drop nd->path.
49339159de2SJeff Layton  */
4949f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
49539159de2SJeff Layton {
49616c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
49739159de2SJeff Layton 	int status;
49839159de2SJeff Layton 
4999f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
5009f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
5019f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
5029f1fafeeSAl Viro 			nd->root.mnt = NULL;
5039f1fafeeSAl Viro 		spin_lock(&dentry->d_lock);
5049f1fafeeSAl Viro 		if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
5059f1fafeeSAl Viro 			spin_unlock(&dentry->d_lock);
5069f1fafeeSAl Viro 			rcu_read_unlock();
5079f1fafeeSAl Viro 			br_read_unlock(vfsmount_lock);
5089f1fafeeSAl Viro 			return -ECHILD;
5099f1fafeeSAl Viro 		}
5109f1fafeeSAl Viro 		BUG_ON(nd->inode != dentry->d_inode);
5119f1fafeeSAl Viro 		spin_unlock(&dentry->d_lock);
5129f1fafeeSAl Viro 		mntget(nd->path.mnt);
5139f1fafeeSAl Viro 		rcu_read_unlock();
5149f1fafeeSAl Viro 		br_read_unlock(vfsmount_lock);
5159f1fafeeSAl Viro 	}
5169f1fafeeSAl Viro 
51716c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
51839159de2SJeff Layton 		return 0;
51939159de2SJeff Layton 
52016c2cd71SAl Viro 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
52116c2cd71SAl Viro 		return 0;
52216c2cd71SAl Viro 
52316c2cd71SAl Viro 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
52416c2cd71SAl Viro 		return 0;
52516c2cd71SAl Viro 
52616c2cd71SAl Viro 	/* Note: we do not d_invalidate() */
52734286d66SNick Piggin 	status = d_revalidate(dentry, nd);
52839159de2SJeff Layton 	if (status > 0)
52939159de2SJeff Layton 		return 0;
53039159de2SJeff Layton 
53116c2cd71SAl Viro 	if (!status)
53239159de2SJeff Layton 		status = -ESTALE;
53316c2cd71SAl Viro 
5349f1fafeeSAl Viro 	path_put(&nd->path);
53539159de2SJeff Layton 	return status;
53639159de2SJeff Layton }
53739159de2SJeff Layton 
5382a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
5392a737871SAl Viro {
540f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
541f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
5422a737871SAl Viro }
5432a737871SAl Viro 
5446de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
5456de88d72SAl Viro 
54631e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
54731e6b01fSNick Piggin {
54831e6b01fSNick Piggin 	if (!nd->root.mnt) {
54931e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
550c28cc364SNick Piggin 		unsigned seq;
551c28cc364SNick Piggin 
552c28cc364SNick Piggin 		do {
553c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
55431e6b01fSNick Piggin 			nd->root = fs->root;
555c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
556c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
55731e6b01fSNick Piggin 	}
55831e6b01fSNick Piggin }
55931e6b01fSNick Piggin 
560f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
5611da177e4SLinus Torvalds {
56231e6b01fSNick Piggin 	int ret;
56331e6b01fSNick Piggin 
5641da177e4SLinus Torvalds 	if (IS_ERR(link))
5651da177e4SLinus Torvalds 		goto fail;
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	if (*link == '/') {
5682a737871SAl Viro 		set_root(nd);
5691d957f9bSJan Blunck 		path_put(&nd->path);
5702a737871SAl Viro 		nd->path = nd->root;
5712a737871SAl Viro 		path_get(&nd->root);
57216c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
5731da177e4SLinus Torvalds 	}
57431e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
575b4091d5fSChristoph Hellwig 
57631e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
57731e6b01fSNick Piggin 	return ret;
5781da177e4SLinus Torvalds fail:
5791d957f9bSJan Blunck 	path_put(&nd->path);
5801da177e4SLinus Torvalds 	return PTR_ERR(link);
5811da177e4SLinus Torvalds }
5821da177e4SLinus Torvalds 
5831d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
584051d3812SIan Kent {
585051d3812SIan Kent 	dput(path->dentry);
5864ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
587051d3812SIan Kent 		mntput(path->mnt);
588051d3812SIan Kent }
589051d3812SIan Kent 
5907b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
5917b9337aaSNick Piggin 					struct nameidata *nd)
592051d3812SIan Kent {
59331e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
5944ac91378SJan Blunck 		dput(nd->path.dentry);
59531e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
5964ac91378SJan Blunck 			mntput(nd->path.mnt);
5979a229683SHuang Shijie 	}
59831e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
5994ac91378SJan Blunck 	nd->path.dentry = path->dentry;
600051d3812SIan Kent }
601051d3812SIan Kent 
602574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
603574197e0SAl Viro {
604574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
605574197e0SAl Viro 	if (!IS_ERR(cookie) && inode->i_op->put_link)
606574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
607574197e0SAl Viro 	path_put(link);
608574197e0SAl Viro }
609574197e0SAl Viro 
610def4af30SAl Viro static __always_inline int
611574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
6121da177e4SLinus Torvalds {
6131da177e4SLinus Torvalds 	int error;
6147b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
6151da177e4SLinus Torvalds 
616844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
617844a3917SAl Viro 
6180e794589SAl Viro 	if (link->mnt == nd->path.mnt)
6190e794589SAl Viro 		mntget(link->mnt);
6200e794589SAl Viro 
621574197e0SAl Viro 	if (unlikely(current->total_link_count >= 40)) {
622574197e0SAl Viro 		*p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
623574197e0SAl Viro 		path_put(&nd->path);
624574197e0SAl Viro 		return -ELOOP;
625574197e0SAl Viro 	}
626574197e0SAl Viro 	cond_resched();
627574197e0SAl Viro 	current->total_link_count++;
628574197e0SAl Viro 
6297b9337aaSNick Piggin 	touch_atime(link->mnt, dentry);
6301da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
631cd4e91d3SAl Viro 
63236f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
63336f3b4f6SAl Viro 	if (error) {
63436f3b4f6SAl Viro 		*p = ERR_PTR(error); /* no ->put_link(), please */
63536f3b4f6SAl Viro 		path_put(&nd->path);
63636f3b4f6SAl Viro 		return error;
63736f3b4f6SAl Viro 	}
63836f3b4f6SAl Viro 
63986acdca1SAl Viro 	nd->last_type = LAST_BIND;
640def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
641def4af30SAl Viro 	error = PTR_ERR(*p);
642def4af30SAl Viro 	if (!IS_ERR(*p)) {
6431da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
644cc314eefSLinus Torvalds 		error = 0;
6451da177e4SLinus Torvalds 		if (s)
6461da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
647bcda7652SAl Viro 		else if (nd->last_type == LAST_BIND) {
64816c2cd71SAl Viro 			nd->flags |= LOOKUP_JUMPED;
649b21041d0SAl Viro 			nd->inode = nd->path.dentry->d_inode;
650b21041d0SAl Viro 			if (nd->inode->i_op->follow_link) {
651bcda7652SAl Viro 				/* stepped on a _really_ weird one */
652bcda7652SAl Viro 				path_put(&nd->path);
653bcda7652SAl Viro 				error = -ELOOP;
654bcda7652SAl Viro 			}
655bcda7652SAl Viro 		}
6561da177e4SLinus Torvalds 	}
6571da177e4SLinus Torvalds 	return error;
6581da177e4SLinus Torvalds }
6591da177e4SLinus Torvalds 
66031e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
66131e6b01fSNick Piggin {
66231e6b01fSNick Piggin 	struct vfsmount *parent;
66331e6b01fSNick Piggin 	struct dentry *mountpoint;
66431e6b01fSNick Piggin 
66531e6b01fSNick Piggin 	parent = path->mnt->mnt_parent;
66631e6b01fSNick Piggin 	if (parent == path->mnt)
66731e6b01fSNick Piggin 		return 0;
66831e6b01fSNick Piggin 	mountpoint = path->mnt->mnt_mountpoint;
66931e6b01fSNick Piggin 	path->dentry = mountpoint;
67031e6b01fSNick Piggin 	path->mnt = parent;
67131e6b01fSNick Piggin 	return 1;
67231e6b01fSNick Piggin }
67331e6b01fSNick Piggin 
674bab77ebfSAl Viro int follow_up(struct path *path)
6751da177e4SLinus Torvalds {
6761da177e4SLinus Torvalds 	struct vfsmount *parent;
6771da177e4SLinus Torvalds 	struct dentry *mountpoint;
67899b7db7bSNick Piggin 
67999b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
680bab77ebfSAl Viro 	parent = path->mnt->mnt_parent;
681bab77ebfSAl Viro 	if (parent == path->mnt) {
68299b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
6831da177e4SLinus Torvalds 		return 0;
6841da177e4SLinus Torvalds 	}
6851da177e4SLinus Torvalds 	mntget(parent);
686bab77ebfSAl Viro 	mountpoint = dget(path->mnt->mnt_mountpoint);
68799b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
688bab77ebfSAl Viro 	dput(path->dentry);
689bab77ebfSAl Viro 	path->dentry = mountpoint;
690bab77ebfSAl Viro 	mntput(path->mnt);
691bab77ebfSAl Viro 	path->mnt = parent;
6921da177e4SLinus Torvalds 	return 1;
6931da177e4SLinus Torvalds }
6941da177e4SLinus Torvalds 
695b5c84bf6SNick Piggin /*
6969875cf80SDavid Howells  * Perform an automount
6979875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
6989875cf80SDavid Howells  *   were called with.
6991da177e4SLinus Torvalds  */
7009875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
7019875cf80SDavid Howells 			    bool *need_mntput)
70231e6b01fSNick Piggin {
7039875cf80SDavid Howells 	struct vfsmount *mnt;
704ea5b778aSDavid Howells 	int err;
7059875cf80SDavid Howells 
7069875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
7079875cf80SDavid Howells 		return -EREMOTE;
7089875cf80SDavid Howells 
7096f45b656SDavid Howells 	/* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
7106f45b656SDavid Howells 	 * and this is the terminal part of the path.
7116f45b656SDavid Howells 	 */
71249084c3bSAl Viro 	if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_PARENT))
7136f45b656SDavid Howells 		return -EISDIR; /* we actually want to stop here */
7146f45b656SDavid Howells 
7159875cf80SDavid Howells 	/* We want to mount if someone is trying to open/create a file of any
7169875cf80SDavid Howells 	 * type under the mountpoint, wants to traverse through the mountpoint
7179875cf80SDavid Howells 	 * or wants to open the mounted directory.
7189875cf80SDavid Howells 	 *
7199875cf80SDavid Howells 	 * We don't want to mount if someone's just doing a stat and they've
7209875cf80SDavid Howells 	 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
7219875cf80SDavid Howells 	 * appended a '/' to the name.
7229875cf80SDavid Howells 	 */
7239875cf80SDavid Howells 	if (!(flags & LOOKUP_FOLLOW) &&
72449084c3bSAl Viro 	    !(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
7259875cf80SDavid Howells 		       LOOKUP_OPEN | LOOKUP_CREATE)))
7269875cf80SDavid Howells 		return -EISDIR;
7279875cf80SDavid Howells 
7289875cf80SDavid Howells 	current->total_link_count++;
7299875cf80SDavid Howells 	if (current->total_link_count >= 40)
7309875cf80SDavid Howells 		return -ELOOP;
7319875cf80SDavid Howells 
7329875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
7339875cf80SDavid Howells 	if (IS_ERR(mnt)) {
7349875cf80SDavid Howells 		/*
7359875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
7369875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
7379875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
7389875cf80SDavid Howells 		 *
7399875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
7409875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
7419875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
7429875cf80SDavid Howells 		 */
74349084c3bSAl Viro 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
7449875cf80SDavid Howells 			return -EREMOTE;
7459875cf80SDavid Howells 		return PTR_ERR(mnt);
74631e6b01fSNick Piggin 	}
747ea5b778aSDavid Howells 
7489875cf80SDavid Howells 	if (!mnt) /* mount collision */
7499875cf80SDavid Howells 		return 0;
7509875cf80SDavid Howells 
7518aef1884SAl Viro 	if (!*need_mntput) {
7528aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
7538aef1884SAl Viro 		mntget(path->mnt);
7548aef1884SAl Viro 		*need_mntput = true;
7558aef1884SAl Viro 	}
75619a167afSAl Viro 	err = finish_automount(mnt, path);
757ea5b778aSDavid Howells 
758ea5b778aSDavid Howells 	switch (err) {
759ea5b778aSDavid Howells 	case -EBUSY:
760ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
76119a167afSAl Viro 		return 0;
762ea5b778aSDavid Howells 	case 0:
7638aef1884SAl Viro 		path_put(path);
7649875cf80SDavid Howells 		path->mnt = mnt;
7659875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
7669875cf80SDavid Howells 		return 0;
76719a167afSAl Viro 	default:
76819a167afSAl Viro 		return err;
7699875cf80SDavid Howells 	}
77019a167afSAl Viro 
771ea5b778aSDavid Howells }
7729875cf80SDavid Howells 
7739875cf80SDavid Howells /*
7749875cf80SDavid Howells  * Handle a dentry that is managed in some way.
775cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
7769875cf80SDavid Howells  * - Flagged as mountpoint
7779875cf80SDavid Howells  * - Flagged as automount point
7789875cf80SDavid Howells  *
7799875cf80SDavid Howells  * This may only be called in refwalk mode.
7809875cf80SDavid Howells  *
7819875cf80SDavid Howells  * Serialization is taken care of in namespace.c
7829875cf80SDavid Howells  */
7839875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
7849875cf80SDavid Howells {
7858aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
7869875cf80SDavid Howells 	unsigned managed;
7879875cf80SDavid Howells 	bool need_mntput = false;
7888aef1884SAl Viro 	int ret = 0;
7899875cf80SDavid Howells 
7909875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
7919875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
7929875cf80SDavid Howells 	 * the components of that value change under us */
7939875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
7949875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
7959875cf80SDavid Howells 	       unlikely(managed != 0)) {
796cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
797cc53ce53SDavid Howells 		 * being held. */
798cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
799cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
800cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
8011aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
802cc53ce53SDavid Howells 			if (ret < 0)
8038aef1884SAl Viro 				break;
804cc53ce53SDavid Howells 		}
805cc53ce53SDavid Howells 
8069875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
8079875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
8089875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
8099875cf80SDavid Howells 			if (mounted) {
8109875cf80SDavid Howells 				dput(path->dentry);
8119875cf80SDavid Howells 				if (need_mntput)
812463ffb2eSAl Viro 					mntput(path->mnt);
813463ffb2eSAl Viro 				path->mnt = mounted;
814463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
8159875cf80SDavid Howells 				need_mntput = true;
8169875cf80SDavid Howells 				continue;
817463ffb2eSAl Viro 			}
818463ffb2eSAl Viro 
8199875cf80SDavid Howells 			/* Something is mounted on this dentry in another
8209875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
8219875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
8229875cf80SDavid Howells 			 * vfsmount_lock */
8231da177e4SLinus Torvalds 		}
8249875cf80SDavid Howells 
8259875cf80SDavid Howells 		/* Handle an automount point */
8269875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
8279875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
8289875cf80SDavid Howells 			if (ret < 0)
8298aef1884SAl Viro 				break;
8309875cf80SDavid Howells 			continue;
8319875cf80SDavid Howells 		}
8329875cf80SDavid Howells 
8339875cf80SDavid Howells 		/* We didn't change the current path point */
8349875cf80SDavid Howells 		break;
8359875cf80SDavid Howells 	}
8368aef1884SAl Viro 
8378aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
8388aef1884SAl Viro 		mntput(path->mnt);
8398aef1884SAl Viro 	if (ret == -EISDIR)
8408aef1884SAl Viro 		ret = 0;
8418aef1884SAl Viro 	return ret;
8421da177e4SLinus Torvalds }
8431da177e4SLinus Torvalds 
844cc53ce53SDavid Howells int follow_down_one(struct path *path)
8451da177e4SLinus Torvalds {
8461da177e4SLinus Torvalds 	struct vfsmount *mounted;
8471da177e4SLinus Torvalds 
8481c755af4SAl Viro 	mounted = lookup_mnt(path);
8491da177e4SLinus Torvalds 	if (mounted) {
8509393bd07SAl Viro 		dput(path->dentry);
8519393bd07SAl Viro 		mntput(path->mnt);
8529393bd07SAl Viro 		path->mnt = mounted;
8539393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
8541da177e4SLinus Torvalds 		return 1;
8551da177e4SLinus Torvalds 	}
8561da177e4SLinus Torvalds 	return 0;
8571da177e4SLinus Torvalds }
8581da177e4SLinus Torvalds 
85962a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
86062a7375eSIan Kent {
86162a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
86262a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
86362a7375eSIan Kent }
86462a7375eSIan Kent 
8659875cf80SDavid Howells /*
866287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
867287548e4SAl Viro  * we meet a managed dentry that would need blocking.
8689875cf80SDavid Howells  */
8699875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
870287548e4SAl Viro 			       struct inode **inode)
8719875cf80SDavid Howells {
87262a7375eSIan Kent 	for (;;) {
8739875cf80SDavid Howells 		struct vfsmount *mounted;
87462a7375eSIan Kent 		/*
87562a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
87662a7375eSIan Kent 		 * that wants to block transit.
87762a7375eSIan Kent 		 */
878287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
879ab90911fSDavid Howells 			return false;
88062a7375eSIan Kent 
88162a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
88262a7375eSIan Kent 			break;
88362a7375eSIan Kent 
8849875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
8859875cf80SDavid Howells 		if (!mounted)
8869875cf80SDavid Howells 			break;
8879875cf80SDavid Howells 		path->mnt = mounted;
8889875cf80SDavid Howells 		path->dentry = mounted->mnt_root;
8899875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
89059430262SLinus Torvalds 		/*
89159430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
89259430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
89359430262SLinus Torvalds 		 * because a mount-point is always pinned.
89459430262SLinus Torvalds 		 */
89559430262SLinus Torvalds 		*inode = path->dentry->d_inode;
8969875cf80SDavid Howells 	}
8979875cf80SDavid Howells 	return true;
8989875cf80SDavid Howells }
8999875cf80SDavid Howells 
900dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
901287548e4SAl Viro {
902dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
903287548e4SAl Viro 		struct vfsmount *mounted;
904dea39376SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
905287548e4SAl Viro 		if (!mounted)
906287548e4SAl Viro 			break;
907dea39376SAl Viro 		nd->path.mnt = mounted;
908dea39376SAl Viro 		nd->path.dentry = mounted->mnt_root;
909dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
910287548e4SAl Viro 	}
911287548e4SAl Viro }
912287548e4SAl Viro 
91331e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
91431e6b01fSNick Piggin {
91531e6b01fSNick Piggin 	set_root_rcu(nd);
91631e6b01fSNick Piggin 
91731e6b01fSNick Piggin 	while (1) {
91831e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
91931e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
92031e6b01fSNick Piggin 			break;
92131e6b01fSNick Piggin 		}
92231e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
92331e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
92431e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
92531e6b01fSNick Piggin 			unsigned seq;
92631e6b01fSNick Piggin 
92731e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
92831e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
929ef7562d5SAl Viro 				goto failed;
93031e6b01fSNick Piggin 			nd->path.dentry = parent;
93131e6b01fSNick Piggin 			nd->seq = seq;
93231e6b01fSNick Piggin 			break;
93331e6b01fSNick Piggin 		}
93431e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
93531e6b01fSNick Piggin 			break;
93631e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
93731e6b01fSNick Piggin 	}
938dea39376SAl Viro 	follow_mount_rcu(nd);
939dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
94031e6b01fSNick Piggin 	return 0;
941ef7562d5SAl Viro 
942ef7562d5SAl Viro failed:
943ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
9445b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
945ef7562d5SAl Viro 		nd->root.mnt = NULL;
946ef7562d5SAl Viro 	rcu_read_unlock();
947ef7562d5SAl Viro 	br_read_unlock(vfsmount_lock);
948ef7562d5SAl Viro 	return -ECHILD;
94931e6b01fSNick Piggin }
95031e6b01fSNick Piggin 
9519875cf80SDavid Howells /*
952cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
953cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
954cc53ce53SDavid Howells  * caller is permitted to proceed or not.
955cc53ce53SDavid Howells  */
9567cc90cc3SAl Viro int follow_down(struct path *path)
957cc53ce53SDavid Howells {
958cc53ce53SDavid Howells 	unsigned managed;
959cc53ce53SDavid Howells 	int ret;
960cc53ce53SDavid Howells 
961cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
962cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
963cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
964cc53ce53SDavid Howells 		 * being held.
965cc53ce53SDavid Howells 		 *
966cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
967cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
968cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
969cc53ce53SDavid Howells 		 * superstructure.
970cc53ce53SDavid Howells 		 *
971cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
972cc53ce53SDavid Howells 		 */
973cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
974cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
975cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
976ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
9771aed3e42SAl Viro 				path->dentry, false);
978cc53ce53SDavid Howells 			if (ret < 0)
979cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
980cc53ce53SDavid Howells 		}
981cc53ce53SDavid Howells 
982cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
983cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
984cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
985cc53ce53SDavid Howells 			if (!mounted)
986cc53ce53SDavid Howells 				break;
987cc53ce53SDavid Howells 			dput(path->dentry);
988cc53ce53SDavid Howells 			mntput(path->mnt);
989cc53ce53SDavid Howells 			path->mnt = mounted;
990cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
991cc53ce53SDavid Howells 			continue;
992cc53ce53SDavid Howells 		}
993cc53ce53SDavid Howells 
994cc53ce53SDavid Howells 		/* Don't handle automount points here */
995cc53ce53SDavid Howells 		break;
996cc53ce53SDavid Howells 	}
997cc53ce53SDavid Howells 	return 0;
998cc53ce53SDavid Howells }
999cc53ce53SDavid Howells 
1000cc53ce53SDavid Howells /*
10019875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
10029875cf80SDavid Howells  */
10039875cf80SDavid Howells static void follow_mount(struct path *path)
10049875cf80SDavid Howells {
10059875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
10069875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
10079875cf80SDavid Howells 		if (!mounted)
10089875cf80SDavid Howells 			break;
10099875cf80SDavid Howells 		dput(path->dentry);
10109875cf80SDavid Howells 		mntput(path->mnt);
10119875cf80SDavid Howells 		path->mnt = mounted;
10129875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
10139875cf80SDavid Howells 	}
10149875cf80SDavid Howells }
10159875cf80SDavid Howells 
101631e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
10171da177e4SLinus Torvalds {
10182a737871SAl Viro 	set_root(nd);
1019e518ddb7SAndreas Mohr 
10201da177e4SLinus Torvalds 	while(1) {
10214ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
10221da177e4SLinus Torvalds 
10232a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
10242a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
10251da177e4SLinus Torvalds 			break;
10261da177e4SLinus Torvalds 		}
10274ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
10283088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
10293088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
10301da177e4SLinus Torvalds 			dput(old);
10311da177e4SLinus Torvalds 			break;
10321da177e4SLinus Torvalds 		}
10333088dd70SAl Viro 		if (!follow_up(&nd->path))
10341da177e4SLinus Torvalds 			break;
10351da177e4SLinus Torvalds 	}
103679ed0226SAl Viro 	follow_mount(&nd->path);
103731e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
10381da177e4SLinus Torvalds }
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds /*
1041baa03890SNick Piggin  * Allocate a dentry with name and parent, and perform a parent
1042baa03890SNick Piggin  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1043baa03890SNick Piggin  * on error. parent->d_inode->i_mutex must be held. d_lookup must
1044baa03890SNick Piggin  * have verified that no child exists while under i_mutex.
1045baa03890SNick Piggin  */
1046baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1047baa03890SNick Piggin 				struct qstr *name, struct nameidata *nd)
1048baa03890SNick Piggin {
1049baa03890SNick Piggin 	struct inode *inode = parent->d_inode;
1050baa03890SNick Piggin 	struct dentry *dentry;
1051baa03890SNick Piggin 	struct dentry *old;
1052baa03890SNick Piggin 
1053baa03890SNick Piggin 	/* Don't create child dentry for a dead directory. */
1054baa03890SNick Piggin 	if (unlikely(IS_DEADDIR(inode)))
1055baa03890SNick Piggin 		return ERR_PTR(-ENOENT);
1056baa03890SNick Piggin 
1057baa03890SNick Piggin 	dentry = d_alloc(parent, name);
1058baa03890SNick Piggin 	if (unlikely(!dentry))
1059baa03890SNick Piggin 		return ERR_PTR(-ENOMEM);
1060baa03890SNick Piggin 
1061baa03890SNick Piggin 	old = inode->i_op->lookup(inode, dentry, nd);
1062baa03890SNick Piggin 	if (unlikely(old)) {
1063baa03890SNick Piggin 		dput(dentry);
1064baa03890SNick Piggin 		dentry = old;
1065baa03890SNick Piggin 	}
1066baa03890SNick Piggin 	return dentry;
1067baa03890SNick Piggin }
1068baa03890SNick Piggin 
1069baa03890SNick Piggin /*
107044396f4bSJosef Bacik  * We already have a dentry, but require a lookup to be performed on the parent
107144396f4bSJosef Bacik  * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
107244396f4bSJosef Bacik  * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
107344396f4bSJosef Bacik  * child exists while under i_mutex.
107444396f4bSJosef Bacik  */
107544396f4bSJosef Bacik static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
107644396f4bSJosef Bacik 				     struct nameidata *nd)
107744396f4bSJosef Bacik {
107844396f4bSJosef Bacik 	struct inode *inode = parent->d_inode;
107944396f4bSJosef Bacik 	struct dentry *old;
108044396f4bSJosef Bacik 
108144396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
108244396f4bSJosef Bacik 	if (unlikely(IS_DEADDIR(inode)))
108344396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
108444396f4bSJosef Bacik 
108544396f4bSJosef Bacik 	old = inode->i_op->lookup(inode, dentry, nd);
108644396f4bSJosef Bacik 	if (unlikely(old)) {
108744396f4bSJosef Bacik 		dput(dentry);
108844396f4bSJosef Bacik 		dentry = old;
108944396f4bSJosef Bacik 	}
109044396f4bSJosef Bacik 	return dentry;
109144396f4bSJosef Bacik }
109244396f4bSJosef Bacik 
109344396f4bSJosef Bacik /*
10941da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
10951da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
10961da177e4SLinus Torvalds  *  It _is_ time-critical.
10971da177e4SLinus Torvalds  */
10981da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
109931e6b01fSNick Piggin 			struct path *path, struct inode **inode)
11001da177e4SLinus Torvalds {
11014ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
110231e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
11035a18fff2SAl Viro 	int need_reval = 1;
11045a18fff2SAl Viro 	int status = 1;
11059875cf80SDavid Howells 	int err;
11069875cf80SDavid Howells 
11073cac260aSAl Viro 	/*
1108b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1109b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1110b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1111b04f784eSNick Piggin 	 */
111231e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
111331e6b01fSNick Piggin 		unsigned seq;
111431e6b01fSNick Piggin 		*inode = nd->inode;
111531e6b01fSNick Piggin 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
11165a18fff2SAl Viro 		if (!dentry)
11175a18fff2SAl Viro 			goto unlazy;
11185a18fff2SAl Viro 
111931e6b01fSNick Piggin 		/* Memory barrier in read_seqcount_begin of child is enough */
112031e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
112131e6b01fSNick Piggin 			return -ECHILD;
112231e6b01fSNick Piggin 		nd->seq = seq;
11235a18fff2SAl Viro 
112424643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
11255a18fff2SAl Viro 			status = d_revalidate(dentry, nd);
11265a18fff2SAl Viro 			if (unlikely(status <= 0)) {
11275a18fff2SAl Viro 				if (status != -ECHILD)
11285a18fff2SAl Viro 					need_reval = 0;
11295a18fff2SAl Viro 				goto unlazy;
11305a18fff2SAl Viro 			}
113124643087SAl Viro 		}
113244396f4bSJosef Bacik 		if (unlikely(d_need_lookup(dentry)))
113344396f4bSJosef Bacik 			goto unlazy;
113431e6b01fSNick Piggin 		path->mnt = mnt;
113531e6b01fSNick Piggin 		path->dentry = dentry;
1136d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1137d6e9bd25SAl Viro 			goto unlazy;
1138d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1139d6e9bd25SAl Viro 			goto unlazy;
11409875cf80SDavid Howells 		return 0;
11415a18fff2SAl Viro unlazy:
114219660af7SAl Viro 		if (unlazy_walk(nd, dentry))
11435a18fff2SAl Viro 			return -ECHILD;
11445a18fff2SAl Viro 	} else {
114531e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
114624643087SAl Viro 	}
11475a18fff2SAl Viro 
114844396f4bSJosef Bacik 	if (dentry && unlikely(d_need_lookup(dentry))) {
114944396f4bSJosef Bacik 		dput(dentry);
115044396f4bSJosef Bacik 		dentry = NULL;
115144396f4bSJosef Bacik 	}
11525a18fff2SAl Viro retry:
11535a18fff2SAl Viro 	if (unlikely(!dentry)) {
11545a18fff2SAl Viro 		struct inode *dir = parent->d_inode;
11555a18fff2SAl Viro 		BUG_ON(nd->inode != dir);
11565a18fff2SAl Viro 
11575a18fff2SAl Viro 		mutex_lock(&dir->i_mutex);
11585a18fff2SAl Viro 		dentry = d_lookup(parent, name);
11595a18fff2SAl Viro 		if (likely(!dentry)) {
11605a18fff2SAl Viro 			dentry = d_alloc_and_lookup(parent, name, nd);
11615a18fff2SAl Viro 			if (IS_ERR(dentry)) {
11625a18fff2SAl Viro 				mutex_unlock(&dir->i_mutex);
11635a18fff2SAl Viro 				return PTR_ERR(dentry);
11645a18fff2SAl Viro 			}
11655a18fff2SAl Viro 			/* known good */
11665a18fff2SAl Viro 			need_reval = 0;
11675a18fff2SAl Viro 			status = 1;
116844396f4bSJosef Bacik 		} else if (unlikely(d_need_lookup(dentry))) {
116944396f4bSJosef Bacik 			dentry = d_inode_lookup(parent, dentry, nd);
117044396f4bSJosef Bacik 			if (IS_ERR(dentry)) {
117144396f4bSJosef Bacik 				mutex_unlock(&dir->i_mutex);
117244396f4bSJosef Bacik 				return PTR_ERR(dentry);
117344396f4bSJosef Bacik 			}
117444396f4bSJosef Bacik 			/* known good */
117544396f4bSJosef Bacik 			need_reval = 0;
117644396f4bSJosef Bacik 			status = 1;
11775a18fff2SAl Viro 		}
11785a18fff2SAl Viro 		mutex_unlock(&dir->i_mutex);
11795a18fff2SAl Viro 	}
11805a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
11815a18fff2SAl Viro 		status = d_revalidate(dentry, nd);
11825a18fff2SAl Viro 	if (unlikely(status <= 0)) {
11835a18fff2SAl Viro 		if (status < 0) {
11845a18fff2SAl Viro 			dput(dentry);
11855a18fff2SAl Viro 			return status;
11865a18fff2SAl Viro 		}
11875a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
11885a18fff2SAl Viro 			dput(dentry);
11895a18fff2SAl Viro 			dentry = NULL;
11905a18fff2SAl Viro 			need_reval = 1;
11915a18fff2SAl Viro 			goto retry;
11925a18fff2SAl Viro 		}
11935a18fff2SAl Viro 	}
11945a18fff2SAl Viro 
11951da177e4SLinus Torvalds 	path->mnt = mnt;
11961da177e4SLinus Torvalds 	path->dentry = dentry;
11979875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
119889312214SIan Kent 	if (unlikely(err < 0)) {
119989312214SIan Kent 		path_put_conditional(path, nd);
12009875cf80SDavid Howells 		return err;
120189312214SIan Kent 	}
120231e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
12031da177e4SLinus Torvalds 	return 0;
12041da177e4SLinus Torvalds }
12051da177e4SLinus Torvalds 
120652094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
120752094c8aSAl Viro {
120852094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
12094ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
121052094c8aSAl Viro 		if (err != -ECHILD)
121152094c8aSAl Viro 			return err;
121219660af7SAl Viro 		if (unlazy_walk(nd, NULL))
121352094c8aSAl Viro 			return -ECHILD;
121452094c8aSAl Viro 	}
12154ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
121652094c8aSAl Viro }
121752094c8aSAl Viro 
12189856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
12199856fa1bSAl Viro {
12209856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
12219856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
12229856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
12239856fa1bSAl Viro 				return -ECHILD;
12249856fa1bSAl Viro 		} else
12259856fa1bSAl Viro 			follow_dotdot(nd);
12269856fa1bSAl Viro 	}
12279856fa1bSAl Viro 	return 0;
12289856fa1bSAl Viro }
12299856fa1bSAl Viro 
1230951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1231951361f9SAl Viro {
1232951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1233951361f9SAl Viro 		path_put(&nd->path);
1234951361f9SAl Viro 	} else {
1235951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
12365b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1237951361f9SAl Viro 			nd->root.mnt = NULL;
1238951361f9SAl Viro 		rcu_read_unlock();
1239951361f9SAl Viro 		br_read_unlock(vfsmount_lock);
1240951361f9SAl Viro 	}
1241951361f9SAl Viro }
1242951361f9SAl Viro 
1243ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
1244ce57dfc1SAl Viro 		struct qstr *name, int type, int follow)
1245ce57dfc1SAl Viro {
1246ce57dfc1SAl Viro 	struct inode *inode;
1247ce57dfc1SAl Viro 	int err;
1248ce57dfc1SAl Viro 	/*
1249ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1250ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1251ce57dfc1SAl Viro 	 * parent relationships.
1252ce57dfc1SAl Viro 	 */
1253ce57dfc1SAl Viro 	if (unlikely(type != LAST_NORM))
1254ce57dfc1SAl Viro 		return handle_dots(nd, type);
1255ce57dfc1SAl Viro 	err = do_lookup(nd, name, path, &inode);
1256ce57dfc1SAl Viro 	if (unlikely(err)) {
1257ce57dfc1SAl Viro 		terminate_walk(nd);
1258ce57dfc1SAl Viro 		return err;
1259ce57dfc1SAl Viro 	}
1260ce57dfc1SAl Viro 	if (!inode) {
1261ce57dfc1SAl Viro 		path_to_nameidata(path, nd);
1262ce57dfc1SAl Viro 		terminate_walk(nd);
1263ce57dfc1SAl Viro 		return -ENOENT;
1264ce57dfc1SAl Viro 	}
1265ce57dfc1SAl Viro 	if (unlikely(inode->i_op->follow_link) && follow) {
126619660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
126719660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
126819660af7SAl Viro 				terminate_walk(nd);
1269ce57dfc1SAl Viro 				return -ECHILD;
127019660af7SAl Viro 			}
127119660af7SAl Viro 		}
1272ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1273ce57dfc1SAl Viro 		return 1;
1274ce57dfc1SAl Viro 	}
1275ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1276ce57dfc1SAl Viro 	nd->inode = inode;
1277ce57dfc1SAl Viro 	return 0;
1278ce57dfc1SAl Viro }
1279ce57dfc1SAl Viro 
12801da177e4SLinus Torvalds /*
1281b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1282b356379aSAl Viro  * limiting consecutive symlinks to 40.
1283b356379aSAl Viro  *
1284b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1285b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1286b356379aSAl Viro  */
1287b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1288b356379aSAl Viro {
1289b356379aSAl Viro 	int res;
1290b356379aSAl Viro 
1291b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1292b356379aSAl Viro 		path_put_conditional(path, nd);
1293b356379aSAl Viro 		path_put(&nd->path);
1294b356379aSAl Viro 		return -ELOOP;
1295b356379aSAl Viro 	}
12961a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1297b356379aSAl Viro 
1298b356379aSAl Viro 	nd->depth++;
1299b356379aSAl Viro 	current->link_count++;
1300b356379aSAl Viro 
1301b356379aSAl Viro 	do {
1302b356379aSAl Viro 		struct path link = *path;
1303b356379aSAl Viro 		void *cookie;
1304574197e0SAl Viro 
1305574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
1306b356379aSAl Viro 		if (!res)
1307b356379aSAl Viro 			res = walk_component(nd, path, &nd->last,
1308b356379aSAl Viro 					     nd->last_type, LOOKUP_FOLLOW);
1309574197e0SAl Viro 		put_link(nd, &link, cookie);
1310b356379aSAl Viro 	} while (res > 0);
1311b356379aSAl Viro 
1312b356379aSAl Viro 	current->link_count--;
1313b356379aSAl Viro 	nd->depth--;
1314b356379aSAl Viro 	return res;
1315b356379aSAl Viro }
1316b356379aSAl Viro 
1317b356379aSAl Viro /*
13181da177e4SLinus Torvalds  * Name resolution.
1319ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1320ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
13211da177e4SLinus Torvalds  *
1322ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1323ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
13241da177e4SLinus Torvalds  */
13256de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
13261da177e4SLinus Torvalds {
13271da177e4SLinus Torvalds 	struct path next;
13281da177e4SLinus Torvalds 	int err;
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds 	while (*name=='/')
13311da177e4SLinus Torvalds 		name++;
13321da177e4SLinus Torvalds 	if (!*name)
1333086e183aSAl Viro 		return 0;
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
13361da177e4SLinus Torvalds 	for(;;) {
13371da177e4SLinus Torvalds 		unsigned long hash;
13381da177e4SLinus Torvalds 		struct qstr this;
13391da177e4SLinus Torvalds 		unsigned int c;
1340fe479a58SAl Viro 		int type;
13411da177e4SLinus Torvalds 
134252094c8aSAl Viro 		err = may_lookup(nd);
13431da177e4SLinus Torvalds  		if (err)
13441da177e4SLinus Torvalds 			break;
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 		this.name = name;
13471da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 		hash = init_name_hash();
13501da177e4SLinus Torvalds 		do {
13511da177e4SLinus Torvalds 			name++;
13521da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
13531da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
13541da177e4SLinus Torvalds 		} while (c && (c != '/'));
13551da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
13561da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
13571da177e4SLinus Torvalds 
1358fe479a58SAl Viro 		type = LAST_NORM;
1359fe479a58SAl Viro 		if (this.name[0] == '.') switch (this.len) {
1360fe479a58SAl Viro 			case 2:
136116c2cd71SAl Viro 				if (this.name[1] == '.') {
1362fe479a58SAl Viro 					type = LAST_DOTDOT;
136316c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
136416c2cd71SAl Viro 				}
1365fe479a58SAl Viro 				break;
1366fe479a58SAl Viro 			case 1:
1367fe479a58SAl Viro 				type = LAST_DOT;
1368fe479a58SAl Viro 		}
13695a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
13705a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
137116c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
13725a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
13735a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
13745a202bcdSAl Viro 							   &this);
13755a202bcdSAl Viro 				if (err < 0)
13765a202bcdSAl Viro 					break;
13775a202bcdSAl Viro 			}
13785a202bcdSAl Viro 		}
1379fe479a58SAl Viro 
13801da177e4SLinus Torvalds 		/* remove trailing slashes? */
13811da177e4SLinus Torvalds 		if (!c)
13821da177e4SLinus Torvalds 			goto last_component;
13831da177e4SLinus Torvalds 		while (*++name == '/');
13841da177e4SLinus Torvalds 		if (!*name)
1385b356379aSAl Viro 			goto last_component;
13861da177e4SLinus Torvalds 
1387ce57dfc1SAl Viro 		err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1388ce57dfc1SAl Viro 		if (err < 0)
1389ce57dfc1SAl Viro 			return err;
1390fe479a58SAl Viro 
1391ce57dfc1SAl Viro 		if (err) {
1392b356379aSAl Viro 			err = nested_symlink(&next, nd);
13931da177e4SLinus Torvalds 			if (err)
1394a7472babSAl Viro 				return err;
139531e6b01fSNick Piggin 		}
13961da177e4SLinus Torvalds 		err = -ENOTDIR;
139731e6b01fSNick Piggin 		if (!nd->inode->i_op->lookup)
13981da177e4SLinus Torvalds 			break;
13991da177e4SLinus Torvalds 		continue;
14001da177e4SLinus Torvalds 		/* here ends the main loop */
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds last_component:
1403ce57dfc1SAl Viro 		nd->last = this;
1404ce57dfc1SAl Viro 		nd->last_type = type;
1405ce57dfc1SAl Viro 		return 0;
1406ce57dfc1SAl Viro 	}
1407951361f9SAl Viro 	terminate_walk(nd);
14081da177e4SLinus Torvalds 	return err;
14091da177e4SLinus Torvalds }
14101da177e4SLinus Torvalds 
141170e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
141270e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
141331e6b01fSNick Piggin {
141431e6b01fSNick Piggin 	int retval = 0;
141531e6b01fSNick Piggin 	int fput_needed;
141631e6b01fSNick Piggin 	struct file *file;
141731e6b01fSNick Piggin 
141831e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
141916c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
142031e6b01fSNick Piggin 	nd->depth = 0;
14215b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
14225b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
142373d049a4SAl Viro 		if (*name) {
14245b6ca027SAl Viro 			if (!inode->i_op->lookup)
14255b6ca027SAl Viro 				return -ENOTDIR;
14265b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
14275b6ca027SAl Viro 			if (retval)
14285b6ca027SAl Viro 				return retval;
142973d049a4SAl Viro 		}
14305b6ca027SAl Viro 		nd->path = nd->root;
14315b6ca027SAl Viro 		nd->inode = inode;
14325b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
14335b6ca027SAl Viro 			br_read_lock(vfsmount_lock);
14345b6ca027SAl Viro 			rcu_read_lock();
14355b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
14365b6ca027SAl Viro 		} else {
14375b6ca027SAl Viro 			path_get(&nd->path);
14385b6ca027SAl Viro 		}
14395b6ca027SAl Viro 		return 0;
14405b6ca027SAl Viro 	}
14415b6ca027SAl Viro 
144231e6b01fSNick Piggin 	nd->root.mnt = NULL;
144331e6b01fSNick Piggin 
144431e6b01fSNick Piggin 	if (*name=='/') {
1445e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
144631e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
144731e6b01fSNick Piggin 			rcu_read_lock();
1448e41f7d4eSAl Viro 			set_root_rcu(nd);
1449e41f7d4eSAl Viro 		} else {
1450e41f7d4eSAl Viro 			set_root(nd);
1451e41f7d4eSAl Viro 			path_get(&nd->root);
1452e41f7d4eSAl Viro 		}
145331e6b01fSNick Piggin 		nd->path = nd->root;
145431e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1455e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
145631e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1457c28cc364SNick Piggin 			unsigned seq;
145831e6b01fSNick Piggin 
145931e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
146031e6b01fSNick Piggin 			rcu_read_lock();
146131e6b01fSNick Piggin 
1462c28cc364SNick Piggin 			do {
1463c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
146431e6b01fSNick Piggin 				nd->path = fs->pwd;
1465c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1466c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1467e41f7d4eSAl Viro 		} else {
1468e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1469e41f7d4eSAl Viro 		}
147031e6b01fSNick Piggin 	} else {
147131e6b01fSNick Piggin 		struct dentry *dentry;
147231e6b01fSNick Piggin 
14731abf0c71SAl Viro 		file = fget_raw_light(dfd, &fput_needed);
147431e6b01fSNick Piggin 		retval = -EBADF;
147531e6b01fSNick Piggin 		if (!file)
147631e6b01fSNick Piggin 			goto out_fail;
147731e6b01fSNick Piggin 
147831e6b01fSNick Piggin 		dentry = file->f_path.dentry;
147931e6b01fSNick Piggin 
1480f52e0c11SAl Viro 		if (*name) {
148131e6b01fSNick Piggin 			retval = -ENOTDIR;
148231e6b01fSNick Piggin 			if (!S_ISDIR(dentry->d_inode->i_mode))
148331e6b01fSNick Piggin 				goto fput_fail;
148431e6b01fSNick Piggin 
14854ad5abb3SAl Viro 			retval = inode_permission(dentry->d_inode, MAY_EXEC);
148631e6b01fSNick Piggin 			if (retval)
148731e6b01fSNick Piggin 				goto fput_fail;
1488f52e0c11SAl Viro 		}
148931e6b01fSNick Piggin 
149031e6b01fSNick Piggin 		nd->path = file->f_path;
1491e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
149231e6b01fSNick Piggin 			if (fput_needed)
149370e9b357SAl Viro 				*fp = file;
1494c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
149531e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
149631e6b01fSNick Piggin 			rcu_read_lock();
14975590ff0dSUlrich Drepper 		} else {
14985dd784d0SJan Blunck 			path_get(&file->f_path);
14995590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
15001da177e4SLinus Torvalds 		}
1501e41f7d4eSAl Viro 	}
1502e41f7d4eSAl Viro 
150331e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
15049b4a9b14SAl Viro 	return 0;
15052dfdd266SJosef 'Jeff' Sipek 
15069b4a9b14SAl Viro fput_fail:
15079b4a9b14SAl Viro 	fput_light(file, fput_needed);
15089b4a9b14SAl Viro out_fail:
15099b4a9b14SAl Viro 	return retval;
15109b4a9b14SAl Viro }
15119b4a9b14SAl Viro 
1512bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1513bd92d7feSAl Viro {
1514bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1515bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1516bd92d7feSAl Viro 
1517bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1518bd92d7feSAl Viro 	return walk_component(nd, path, &nd->last, nd->last_type,
1519bd92d7feSAl Viro 					nd->flags & LOOKUP_FOLLOW);
1520bd92d7feSAl Viro }
1521bd92d7feSAl Viro 
15229b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1523ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
15249b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
15259b4a9b14SAl Viro {
152670e9b357SAl Viro 	struct file *base = NULL;
1527bd92d7feSAl Viro 	struct path path;
1528bd92d7feSAl Viro 	int err;
152931e6b01fSNick Piggin 
153031e6b01fSNick Piggin 	/*
153131e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
153231e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
153331e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
153431e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
153531e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
153631e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
153731e6b01fSNick Piggin 	 * analogue, foo_rcu().
153831e6b01fSNick Piggin 	 *
153931e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
154031e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
154131e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
154231e6b01fSNick Piggin 	 * be able to complete).
154331e6b01fSNick Piggin 	 */
1544bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1545ee0827cdSAl Viro 
1546bd92d7feSAl Viro 	if (unlikely(err))
1547bd92d7feSAl Viro 		return err;
1548ee0827cdSAl Viro 
1549ee0827cdSAl Viro 	current->total_link_count = 0;
1550bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1551bd92d7feSAl Viro 
1552bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1553bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1554bd92d7feSAl Viro 		while (err > 0) {
1555bd92d7feSAl Viro 			void *cookie;
1556bd92d7feSAl Viro 			struct path link = path;
1557bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1558574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
1559bd92d7feSAl Viro 			if (!err)
1560bd92d7feSAl Viro 				err = lookup_last(nd, &path);
1561574197e0SAl Viro 			put_link(nd, &link, cookie);
1562bd92d7feSAl Viro 		}
1563bd92d7feSAl Viro 	}
1564ee0827cdSAl Viro 
15659f1fafeeSAl Viro 	if (!err)
15669f1fafeeSAl Viro 		err = complete_walk(nd);
1567bd92d7feSAl Viro 
1568bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1569bd92d7feSAl Viro 		if (!nd->inode->i_op->lookup) {
1570bd92d7feSAl Viro 			path_put(&nd->path);
1571bd23a539SAl Viro 			err = -ENOTDIR;
1572bd92d7feSAl Viro 		}
1573bd92d7feSAl Viro 	}
157416c2cd71SAl Viro 
157570e9b357SAl Viro 	if (base)
157670e9b357SAl Viro 		fput(base);
1577ee0827cdSAl Viro 
15785b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
157931e6b01fSNick Piggin 		path_put(&nd->root);
158031e6b01fSNick Piggin 		nd->root.mnt = NULL;
158131e6b01fSNick Piggin 	}
1582bd92d7feSAl Viro 	return err;
158331e6b01fSNick Piggin }
158431e6b01fSNick Piggin 
1585ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1586ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1587ee0827cdSAl Viro {
1588ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1589ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1590ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1591ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1592ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1593ee0827cdSAl Viro 
159431e6b01fSNick Piggin 	if (likely(!retval)) {
159531e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
159631e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
159731e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
159831e6b01fSNick Piggin 		}
159931e6b01fSNick Piggin 	}
1600170aa3d0SUlrich Drepper 	return retval;
16011da177e4SLinus Torvalds }
16021da177e4SLinus Torvalds 
1603c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd)
16045590ff0dSUlrich Drepper {
1605c9c6cac0SAl Viro 	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
16065590ff0dSUlrich Drepper }
16075590ff0dSUlrich Drepper 
1608d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1609d1811465SAl Viro {
1610d1811465SAl Viro 	struct nameidata nd;
1611d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1612d1811465SAl Viro 	if (!res)
1613d1811465SAl Viro 		*path = nd.path;
1614d1811465SAl Viro 	return res;
1615d1811465SAl Viro }
1616d1811465SAl Viro 
161716f18200SJosef 'Jeff' Sipek /**
161816f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
161916f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
162016f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
162116f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
162216f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
1623e0a01249SAl Viro  * @path: pointer to struct path to fill
162416f18200SJosef 'Jeff' Sipek  */
162516f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
162616f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
1627e0a01249SAl Viro 		    struct path *path)
162816f18200SJosef 'Jeff' Sipek {
1629e0a01249SAl Viro 	struct nameidata nd;
1630e0a01249SAl Viro 	int err;
1631e0a01249SAl Viro 	nd.root.dentry = dentry;
1632e0a01249SAl Viro 	nd.root.mnt = mnt;
1633e0a01249SAl Viro 	BUG_ON(flags & LOOKUP_PARENT);
16345b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1635e0a01249SAl Viro 	err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1636e0a01249SAl Viro 	if (!err)
1637e0a01249SAl Viro 		*path = nd.path;
1638e0a01249SAl Viro 	return err;
163916f18200SJosef 'Jeff' Sipek }
164016f18200SJosef 'Jeff' Sipek 
1641eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1642eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
16431da177e4SLinus Torvalds {
164481fca444SChristoph Hellwig 	struct inode *inode = base->d_inode;
16451da177e4SLinus Torvalds 	struct dentry *dentry;
16461da177e4SLinus Torvalds 	int err;
16471da177e4SLinus Torvalds 
16484ad5abb3SAl Viro 	err = inode_permission(inode, MAY_EXEC);
164981fca444SChristoph Hellwig 	if (err)
165081fca444SChristoph Hellwig 		return ERR_PTR(err);
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds 	/*
1653b04f784eSNick Piggin 	 * Don't bother with __d_lookup: callers are for creat as
1654b04f784eSNick Piggin 	 * well as unlink, so a lot of the time it would cost
1655b04f784eSNick Piggin 	 * a double lookup.
16566e6b1bd1SAl Viro 	 */
16576e6b1bd1SAl Viro 	dentry = d_lookup(base, name);
16586e6b1bd1SAl Viro 
165944396f4bSJosef Bacik 	if (dentry && d_need_lookup(dentry)) {
166044396f4bSJosef Bacik 		/*
166144396f4bSJosef Bacik 		 * __lookup_hash is called with the parent dir's i_mutex already
166244396f4bSJosef Bacik 		 * held, so we are good to go here.
166344396f4bSJosef Bacik 		 */
166444396f4bSJosef Bacik 		dentry = d_inode_lookup(base, dentry, nd);
166544396f4bSJosef Bacik 		if (IS_ERR(dentry))
166644396f4bSJosef Bacik 			return dentry;
166744396f4bSJosef Bacik 	}
166844396f4bSJosef Bacik 
1669d2d9e9fbSAl Viro 	if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1670d2d9e9fbSAl Viro 		int status = d_revalidate(dentry, nd);
1671d2d9e9fbSAl Viro 		if (unlikely(status <= 0)) {
1672d2d9e9fbSAl Viro 			/*
1673d2d9e9fbSAl Viro 			 * The dentry failed validation.
1674d2d9e9fbSAl Viro 			 * If d_revalidate returned 0 attempt to invalidate
1675d2d9e9fbSAl Viro 			 * the dentry otherwise d_revalidate is asking us
1676d2d9e9fbSAl Viro 			 * to return a fail status.
1677d2d9e9fbSAl Viro 			 */
1678d2d9e9fbSAl Viro 			if (status < 0) {
1679d2d9e9fbSAl Viro 				dput(dentry);
1680d2d9e9fbSAl Viro 				return ERR_PTR(status);
1681d2d9e9fbSAl Viro 			} else if (!d_invalidate(dentry)) {
1682d2d9e9fbSAl Viro 				dput(dentry);
1683d2d9e9fbSAl Viro 				dentry = NULL;
1684d2d9e9fbSAl Viro 			}
1685d2d9e9fbSAl Viro 		}
1686d2d9e9fbSAl Viro 	}
16876e6b1bd1SAl Viro 
16881da177e4SLinus Torvalds 	if (!dentry)
1689baa03890SNick Piggin 		dentry = d_alloc_and_lookup(base, name, nd);
16905a202bcdSAl Viro 
16911da177e4SLinus Torvalds 	return dentry;
16921da177e4SLinus Torvalds }
16931da177e4SLinus Torvalds 
1694057f6c01SJames Morris /*
1695057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1696057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1697057f6c01SJames Morris  * SMP-safe.
1698057f6c01SJames Morris  */
1699a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
17001da177e4SLinus Torvalds {
17014ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
17021da177e4SLinus Torvalds }
17031da177e4SLinus Torvalds 
1704eead1911SChristoph Hellwig /**
1705a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1706eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1707eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1708eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1709eead1911SChristoph Hellwig  *
1710a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1711a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1712eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1713eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1714eead1911SChristoph Hellwig  */
1715057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1716057f6c01SJames Morris {
1717057f6c01SJames Morris 	struct qstr this;
17186a96ba54SAl Viro 	unsigned long hash;
17196a96ba54SAl Viro 	unsigned int c;
1720057f6c01SJames Morris 
17212f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
17222f9092e1SDavid Woodhouse 
17236a96ba54SAl Viro 	this.name = name;
17246a96ba54SAl Viro 	this.len = len;
17256a96ba54SAl Viro 	if (!len)
17266a96ba54SAl Viro 		return ERR_PTR(-EACCES);
17276a96ba54SAl Viro 
17286a96ba54SAl Viro 	hash = init_name_hash();
17296a96ba54SAl Viro 	while (len--) {
17306a96ba54SAl Viro 		c = *(const unsigned char *)name++;
17316a96ba54SAl Viro 		if (c == '/' || c == '\0')
17326a96ba54SAl Viro 			return ERR_PTR(-EACCES);
17336a96ba54SAl Viro 		hash = partial_name_hash(c, hash);
17346a96ba54SAl Viro 	}
17356a96ba54SAl Viro 	this.hash = end_name_hash(hash);
17365a202bcdSAl Viro 	/*
17375a202bcdSAl Viro 	 * See if the low-level filesystem might want
17385a202bcdSAl Viro 	 * to use its own hash..
17395a202bcdSAl Viro 	 */
17405a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
17415a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
17425a202bcdSAl Viro 		if (err < 0)
17435a202bcdSAl Viro 			return ERR_PTR(err);
17445a202bcdSAl Viro 	}
1745eead1911SChristoph Hellwig 
174649705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1747057f6c01SJames Morris }
1748057f6c01SJames Morris 
17492d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
17502d8f3038SAl Viro 		 struct path *path)
17511da177e4SLinus Torvalds {
17522d8f3038SAl Viro 	struct nameidata nd;
1753f52e0c11SAl Viro 	char *tmp = getname_flags(name, flags);
17541da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
17551da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
17562d8f3038SAl Viro 
17572d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
17582d8f3038SAl Viro 
17592d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
17601da177e4SLinus Torvalds 		putname(tmp);
17612d8f3038SAl Viro 		if (!err)
17622d8f3038SAl Viro 			*path = nd.path;
17631da177e4SLinus Torvalds 	}
17641da177e4SLinus Torvalds 	return err;
17651da177e4SLinus Torvalds }
17661da177e4SLinus Torvalds 
17672ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
17682ad94ae6SAl Viro 			struct nameidata *nd, char **name)
17692ad94ae6SAl Viro {
17702ad94ae6SAl Viro 	char *s = getname(path);
17712ad94ae6SAl Viro 	int error;
17722ad94ae6SAl Viro 
17732ad94ae6SAl Viro 	if (IS_ERR(s))
17742ad94ae6SAl Viro 		return PTR_ERR(s);
17752ad94ae6SAl Viro 
17762ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
17772ad94ae6SAl Viro 	if (error)
17782ad94ae6SAl Viro 		putname(s);
17792ad94ae6SAl Viro 	else
17802ad94ae6SAl Viro 		*name = s;
17812ad94ae6SAl Viro 
17822ad94ae6SAl Viro 	return error;
17832ad94ae6SAl Viro }
17842ad94ae6SAl Viro 
17851da177e4SLinus Torvalds /*
17861da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
17871da177e4SLinus Torvalds  * minimal.
17881da177e4SLinus Torvalds  */
17891da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
17901da177e4SLinus Torvalds {
1791da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1792da9592edSDavid Howells 
17931da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
17941da177e4SLinus Torvalds 		return 0;
1795e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
1796e795b717SSerge E. Hallyn 		goto other_userns;
1797da9592edSDavid Howells 	if (inode->i_uid == fsuid)
17981da177e4SLinus Torvalds 		return 0;
1799da9592edSDavid Howells 	if (dir->i_uid == fsuid)
18001da177e4SLinus Torvalds 		return 0;
1801e795b717SSerge E. Hallyn 
1802e795b717SSerge E. Hallyn other_userns:
1803e795b717SSerge E. Hallyn 	return !ns_capable(inode_userns(inode), CAP_FOWNER);
18041da177e4SLinus Torvalds }
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds /*
18071da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
18081da177e4SLinus Torvalds  *  whether the type of victim is right.
18091da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
18101da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
18111da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
18121da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
18131da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
18141da177e4SLinus Torvalds  *	a. be owner of dir, or
18151da177e4SLinus Torvalds  *	b. be owner of victim, or
18161da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
18171da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
18181da177e4SLinus Torvalds  *     links pointing to it.
18191da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
18201da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
18211da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
18221da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
18231da177e4SLinus Torvalds  *     nfs_async_unlink().
18241da177e4SLinus Torvalds  */
1825858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
18261da177e4SLinus Torvalds {
18271da177e4SLinus Torvalds 	int error;
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 	if (!victim->d_inode)
18301da177e4SLinus Torvalds 		return -ENOENT;
18311da177e4SLinus Torvalds 
18321da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1833cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
18341da177e4SLinus Torvalds 
1835f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
18361da177e4SLinus Torvalds 	if (error)
18371da177e4SLinus Torvalds 		return error;
18381da177e4SLinus Torvalds 	if (IS_APPEND(dir))
18391da177e4SLinus Torvalds 		return -EPERM;
18401da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1841f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
18421da177e4SLinus Torvalds 		return -EPERM;
18431da177e4SLinus Torvalds 	if (isdir) {
18441da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
18451da177e4SLinus Torvalds 			return -ENOTDIR;
18461da177e4SLinus Torvalds 		if (IS_ROOT(victim))
18471da177e4SLinus Torvalds 			return -EBUSY;
18481da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
18491da177e4SLinus Torvalds 		return -EISDIR;
18501da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18511da177e4SLinus Torvalds 		return -ENOENT;
18521da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
18531da177e4SLinus Torvalds 		return -EBUSY;
18541da177e4SLinus Torvalds 	return 0;
18551da177e4SLinus Torvalds }
18561da177e4SLinus Torvalds 
18571da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
18581da177e4SLinus Torvalds  *  dir.
18591da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
18601da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
18611da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
18621da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
18631da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
18641da177e4SLinus Torvalds  */
1865a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
18661da177e4SLinus Torvalds {
18671da177e4SLinus Torvalds 	if (child->d_inode)
18681da177e4SLinus Torvalds 		return -EEXIST;
18691da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18701da177e4SLinus Torvalds 		return -ENOENT;
1871f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
18721da177e4SLinus Torvalds }
18731da177e4SLinus Torvalds 
18741da177e4SLinus Torvalds /*
18751da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
18761da177e4SLinus Torvalds  */
18771da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
18781da177e4SLinus Torvalds {
18791da177e4SLinus Torvalds 	struct dentry *p;
18801da177e4SLinus Torvalds 
18811da177e4SLinus Torvalds 	if (p1 == p2) {
1882f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
18831da177e4SLinus Torvalds 		return NULL;
18841da177e4SLinus Torvalds 	}
18851da177e4SLinus Torvalds 
1886a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
18871da177e4SLinus Torvalds 
1888e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1889e2761a11SOGAWA Hirofumi 	if (p) {
1890f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1891f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
18921da177e4SLinus Torvalds 		return p;
18931da177e4SLinus Torvalds 	}
18941da177e4SLinus Torvalds 
1895e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1896e2761a11SOGAWA Hirofumi 	if (p) {
1897f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1898f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
18991da177e4SLinus Torvalds 		return p;
19001da177e4SLinus Torvalds 	}
19011da177e4SLinus Torvalds 
1902f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1903f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19041da177e4SLinus Torvalds 	return NULL;
19051da177e4SLinus Torvalds }
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
19081da177e4SLinus Torvalds {
19091b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
19101da177e4SLinus Torvalds 	if (p1 != p2) {
19111b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1912a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19131da177e4SLinus Torvalds 	}
19141da177e4SLinus Torvalds }
19151da177e4SLinus Torvalds 
19161da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
19171da177e4SLinus Torvalds 		struct nameidata *nd)
19181da177e4SLinus Torvalds {
1919a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds 	if (error)
19221da177e4SLinus Torvalds 		return error;
19231da177e4SLinus Torvalds 
1924acfa4380SAl Viro 	if (!dir->i_op->create)
19251da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
19261da177e4SLinus Torvalds 	mode &= S_IALLUGO;
19271da177e4SLinus Torvalds 	mode |= S_IFREG;
19281da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
19291da177e4SLinus Torvalds 	if (error)
19301da177e4SLinus Torvalds 		return error;
19311da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1932a74574aaSStephen Smalley 	if (!error)
1933f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
19341da177e4SLinus Torvalds 	return error;
19351da177e4SLinus Torvalds }
19361da177e4SLinus Torvalds 
193773d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
19381da177e4SLinus Torvalds {
19393fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
19401da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
19411da177e4SLinus Torvalds 	int error;
19421da177e4SLinus Torvalds 
1943bcda7652SAl Viro 	/* O_PATH? */
1944bcda7652SAl Viro 	if (!acc_mode)
1945bcda7652SAl Viro 		return 0;
1946bcda7652SAl Viro 
19471da177e4SLinus Torvalds 	if (!inode)
19481da177e4SLinus Torvalds 		return -ENOENT;
19491da177e4SLinus Torvalds 
1950c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1951c8fe8f30SChristoph Hellwig 	case S_IFLNK:
19521da177e4SLinus Torvalds 		return -ELOOP;
1953c8fe8f30SChristoph Hellwig 	case S_IFDIR:
1954c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
19551da177e4SLinus Torvalds 			return -EISDIR;
1956c8fe8f30SChristoph Hellwig 		break;
1957c8fe8f30SChristoph Hellwig 	case S_IFBLK:
1958c8fe8f30SChristoph Hellwig 	case S_IFCHR:
19593fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
19601da177e4SLinus Torvalds 			return -EACCES;
1961c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
1962c8fe8f30SChristoph Hellwig 	case S_IFIFO:
1963c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
19641da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
1965c8fe8f30SChristoph Hellwig 		break;
19664a3fd211SDave Hansen 	}
1967b41572e9SDave Hansen 
19683fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
1969b41572e9SDave Hansen 	if (error)
1970b41572e9SDave Hansen 		return error;
19716146f0d5SMimi Zohar 
19721da177e4SLinus Torvalds 	/*
19731da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
19741da177e4SLinus Torvalds 	 */
19751da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
19768737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
19777715b521SAl Viro 			return -EPERM;
19781da177e4SLinus Torvalds 		if (flag & O_TRUNC)
19797715b521SAl Viro 			return -EPERM;
19801da177e4SLinus Torvalds 	}
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
19832e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
19847715b521SAl Viro 		return -EPERM;
19851da177e4SLinus Torvalds 
19861da177e4SLinus Torvalds 	/*
19871da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
19881da177e4SLinus Torvalds 	 */
1989b65a9cfcSAl Viro 	return break_lease(inode, flag);
19907715b521SAl Viro }
19917715b521SAl Viro 
1992e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
19937715b521SAl Viro {
1994e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
19957715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
19967715b521SAl Viro 	int error = get_write_access(inode);
19971da177e4SLinus Torvalds 	if (error)
19987715b521SAl Viro 		return error;
19991da177e4SLinus Torvalds 	/*
20001da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
20011da177e4SLinus Torvalds 	 */
20021da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2003be6d3e56SKentaro Takeda 	if (!error)
2004ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
20051da177e4SLinus Torvalds 	if (!error) {
20067715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2007d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2008e1181ee6SJeff Layton 				    filp);
20091da177e4SLinus Torvalds 	}
20101da177e4SLinus Torvalds 	put_write_access(inode);
2011acd0c935SMimi Zohar 	return error;
20121da177e4SLinus Torvalds }
20131da177e4SLinus Torvalds 
2014d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2015d57999e1SDave Hansen {
20168a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
20178a5e929dSAl Viro 		flag--;
2018d57999e1SDave Hansen 	return flag;
2019d57999e1SDave Hansen }
2020d57999e1SDave Hansen 
202131e6b01fSNick Piggin /*
2022fe2d35ffSAl Viro  * Handle the last step of open()
202331e6b01fSNick Piggin  */
2024fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
2025c3e380b0SAl Viro 			    const struct open_flags *op, const char *pathname)
2026fb1cc555SAl Viro {
2027a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
20286c0d46c4SAl Viro 	struct dentry *dentry;
2029ca344a89SAl Viro 	int open_flag = op->open_flag;
20306c0d46c4SAl Viro 	int will_truncate = open_flag & O_TRUNC;
2031ca344a89SAl Viro 	int want_write = 0;
2032bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2033fb1cc555SAl Viro 	struct file *filp;
203416c2cd71SAl Viro 	int error;
2035fb1cc555SAl Viro 
2036c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2037c3e380b0SAl Viro 	nd->flags |= op->intent;
2038c3e380b0SAl Viro 
20391f36f774SAl Viro 	switch (nd->last_type) {
20401f36f774SAl Viro 	case LAST_DOTDOT:
2041176306f5SNeil Brown 	case LAST_DOT:
2042fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2043fe2d35ffSAl Viro 		if (error)
2044fe2d35ffSAl Viro 			return ERR_PTR(error);
20451f36f774SAl Viro 		/* fallthrough */
20461f36f774SAl Viro 	case LAST_ROOT:
20479f1fafeeSAl Viro 		error = complete_walk(nd);
204816c2cd71SAl Viro 		if (error)
20499f1fafeeSAl Viro 			return ERR_PTR(error);
2050fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2051ca344a89SAl Viro 		if (open_flag & O_CREAT) {
205216c2cd71SAl Viro 			error = -EISDIR;
20531f36f774SAl Viro 			goto exit;
2054fe2d35ffSAl Viro 		}
2055fe2d35ffSAl Viro 		goto ok;
20561f36f774SAl Viro 	case LAST_BIND:
20579f1fafeeSAl Viro 		error = complete_walk(nd);
205816c2cd71SAl Viro 		if (error)
20599f1fafeeSAl Viro 			return ERR_PTR(error);
20601f36f774SAl Viro 		audit_inode(pathname, dir);
20611f36f774SAl Viro 		goto ok;
20621f36f774SAl Viro 	}
2063a2c36b45SAl Viro 
2064ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2065bcda7652SAl Viro 		int symlink_ok = 0;
2066fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2067fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2068bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2069bcda7652SAl Viro 			symlink_ok = 1;
2070fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2071ce57dfc1SAl Viro 		error = walk_component(nd, path, &nd->last, LAST_NORM,
2072ce57dfc1SAl Viro 					!symlink_ok);
2073ce57dfc1SAl Viro 		if (error < 0)
2074fe2d35ffSAl Viro 			return ERR_PTR(error);
2075ce57dfc1SAl Viro 		if (error) /* symlink */
2076fe2d35ffSAl Viro 			return NULL;
2077fe2d35ffSAl Viro 		/* sayonara */
20789f1fafeeSAl Viro 		error = complete_walk(nd);
20799f1fafeeSAl Viro 		if (error)
2080fe2d35ffSAl Viro 			return ERR_PTR(-ECHILD);
2081fe2d35ffSAl Viro 
2082fe2d35ffSAl Viro 		error = -ENOTDIR;
2083fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_DIRECTORY) {
2084ce57dfc1SAl Viro 			if (!nd->inode->i_op->lookup)
2085fe2d35ffSAl Viro 				goto exit;
2086fe2d35ffSAl Viro 		}
2087fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2088fe2d35ffSAl Viro 		goto ok;
2089fe2d35ffSAl Viro 	}
2090fe2d35ffSAl Viro 
2091fe2d35ffSAl Viro 	/* create side of things */
20929f1fafeeSAl Viro 	error = complete_walk(nd);
20939f1fafeeSAl Viro 	if (error)
20949f1fafeeSAl Viro 		return ERR_PTR(error);
2095fe2d35ffSAl Viro 
2096fe2d35ffSAl Viro 	audit_inode(pathname, dir);
209716c2cd71SAl Viro 	error = -EISDIR;
20981f36f774SAl Viro 	/* trailing slashes? */
209931e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
21001f36f774SAl Viro 		goto exit;
21011f36f774SAl Viro 
2102a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2103a1e28038SAl Viro 
21046c0d46c4SAl Viro 	dentry = lookup_hash(nd);
21056c0d46c4SAl Viro 	error = PTR_ERR(dentry);
21066c0d46c4SAl Viro 	if (IS_ERR(dentry)) {
2107fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2108fb1cc555SAl Viro 		goto exit;
2109fb1cc555SAl Viro 	}
2110fb1cc555SAl Viro 
21116c0d46c4SAl Viro 	path->dentry = dentry;
21126c0d46c4SAl Viro 	path->mnt = nd->path.mnt;
21136c0d46c4SAl Viro 
2114fb1cc555SAl Viro 	/* Negative dentry, just create the file */
21156c0d46c4SAl Viro 	if (!dentry->d_inode) {
21166c0d46c4SAl Viro 		int mode = op->mode;
21176c0d46c4SAl Viro 		if (!IS_POSIXACL(dir->d_inode))
21186c0d46c4SAl Viro 			mode &= ~current_umask();
2119fb1cc555SAl Viro 		/*
2120fb1cc555SAl Viro 		 * This write is needed to ensure that a
21216c0d46c4SAl Viro 		 * rw->ro transition does not occur between
2122fb1cc555SAl Viro 		 * the time when the file is created and when
2123fb1cc555SAl Viro 		 * a permanent write count is taken through
2124fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2125fb1cc555SAl Viro 		 */
2126fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2127fb1cc555SAl Viro 		if (error)
2128fb1cc555SAl Viro 			goto exit_mutex_unlock;
2129ca344a89SAl Viro 		want_write = 1;
21309b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2131ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
21326c0d46c4SAl Viro 		will_truncate = 0;
2133bcda7652SAl Viro 		acc_mode = MAY_OPEN;
21346c0d46c4SAl Viro 		error = security_path_mknod(&nd->path, dentry, mode, 0);
21356c0d46c4SAl Viro 		if (error)
21366c0d46c4SAl Viro 			goto exit_mutex_unlock;
21376c0d46c4SAl Viro 		error = vfs_create(dir->d_inode, dentry, mode, nd);
21386c0d46c4SAl Viro 		if (error)
21396c0d46c4SAl Viro 			goto exit_mutex_unlock;
21406c0d46c4SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
21416c0d46c4SAl Viro 		dput(nd->path.dentry);
21426c0d46c4SAl Viro 		nd->path.dentry = dentry;
2143ca344a89SAl Viro 		goto common;
2144fb1cc555SAl Viro 	}
2145fb1cc555SAl Viro 
2146fb1cc555SAl Viro 	/*
2147fb1cc555SAl Viro 	 * It already exists.
2148fb1cc555SAl Viro 	 */
2149fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2150fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2151fb1cc555SAl Viro 
2152fb1cc555SAl Viro 	error = -EEXIST;
2153ca344a89SAl Viro 	if (open_flag & O_EXCL)
2154fb1cc555SAl Viro 		goto exit_dput;
2155fb1cc555SAl Viro 
21569875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
21579875cf80SDavid Howells 	if (error < 0)
2158fb1cc555SAl Viro 		goto exit_dput;
2159fb1cc555SAl Viro 
2160fb1cc555SAl Viro 	error = -ENOENT;
2161fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2162fb1cc555SAl Viro 		goto exit_dput;
21639e67f361SAl Viro 
21649e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2165fb1cc555SAl Viro 		return NULL;
2166fb1cc555SAl Viro 
2167fb1cc555SAl Viro 	path_to_nameidata(path, nd);
216831e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2169fb1cc555SAl Viro 	error = -EISDIR;
217031e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2171fb1cc555SAl Viro 		goto exit;
217267ee3ad2SAl Viro ok:
21736c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
21746c0d46c4SAl Viro 		will_truncate = 0;
21756c0d46c4SAl Viro 
21760f9d1a10SAl Viro 	if (will_truncate) {
21770f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
21780f9d1a10SAl Viro 		if (error)
21790f9d1a10SAl Viro 			goto exit;
2180ca344a89SAl Viro 		want_write = 1;
21810f9d1a10SAl Viro 	}
2182ca344a89SAl Viro common:
2183bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2184ca344a89SAl Viro 	if (error)
21850f9d1a10SAl Viro 		goto exit;
21860f9d1a10SAl Viro 	filp = nameidata_to_filp(nd);
21870f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
21880f9d1a10SAl Viro 		error = ima_file_check(filp, op->acc_mode);
21890f9d1a10SAl Viro 		if (error) {
21900f9d1a10SAl Viro 			fput(filp);
21910f9d1a10SAl Viro 			filp = ERR_PTR(error);
21920f9d1a10SAl Viro 		}
21930f9d1a10SAl Viro 	}
21940f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
21950f9d1a10SAl Viro 		if (will_truncate) {
21960f9d1a10SAl Viro 			error = handle_truncate(filp);
21970f9d1a10SAl Viro 			if (error) {
21980f9d1a10SAl Viro 				fput(filp);
21990f9d1a10SAl Viro 				filp = ERR_PTR(error);
22000f9d1a10SAl Viro 			}
22010f9d1a10SAl Viro 		}
22020f9d1a10SAl Viro 	}
2203ca344a89SAl Viro out:
2204ca344a89SAl Viro 	if (want_write)
22050f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
22060f9d1a10SAl Viro 	path_put(&nd->path);
2207fb1cc555SAl Viro 	return filp;
2208fb1cc555SAl Viro 
2209fb1cc555SAl Viro exit_mutex_unlock:
2210fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2211fb1cc555SAl Viro exit_dput:
2212fb1cc555SAl Viro 	path_put_conditional(path, nd);
2213fb1cc555SAl Viro exit:
2214ca344a89SAl Viro 	filp = ERR_PTR(error);
2215ca344a89SAl Viro 	goto out;
2216fb1cc555SAl Viro }
2217fb1cc555SAl Viro 
221813aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
221973d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
22201da177e4SLinus Torvalds {
2221fe2d35ffSAl Viro 	struct file *base = NULL;
22224a3fd211SDave Hansen 	struct file *filp;
22239850c056SAl Viro 	struct path path;
222413aab428SAl Viro 	int error;
222531e6b01fSNick Piggin 
222631e6b01fSNick Piggin 	filp = get_empty_filp();
222731e6b01fSNick Piggin 	if (!filp)
222831e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
222931e6b01fSNick Piggin 
223047c805dcSAl Viro 	filp->f_flags = op->open_flag;
223173d049a4SAl Viro 	nd->intent.open.file = filp;
223273d049a4SAl Viro 	nd->intent.open.flags = open_to_namei_flags(op->open_flag);
223373d049a4SAl Viro 	nd->intent.open.create_mode = op->mode;
223431e6b01fSNick Piggin 
223573d049a4SAl Viro 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
223631e6b01fSNick Piggin 	if (unlikely(error))
223713aab428SAl Viro 		goto out_filp;
223831e6b01fSNick Piggin 
2239fe2d35ffSAl Viro 	current->total_link_count = 0;
224073d049a4SAl Viro 	error = link_path_walk(pathname, nd);
224131e6b01fSNick Piggin 	if (unlikely(error))
224231e6b01fSNick Piggin 		goto out_filp;
22431da177e4SLinus Torvalds 
224473d049a4SAl Viro 	filp = do_last(nd, &path, op, pathname);
2245806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
22467b9337aaSNick Piggin 		struct path link = path;
2247def4af30SAl Viro 		void *cookie;
2248574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
224973d049a4SAl Viro 			path_put_conditional(&path, nd);
225073d049a4SAl Viro 			path_put(&nd->path);
225140b39136SAl Viro 			filp = ERR_PTR(-ELOOP);
225240b39136SAl Viro 			break;
225340b39136SAl Viro 		}
225473d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
225573d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2256574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
2257c3e380b0SAl Viro 		if (unlikely(error))
2258f1afe9efSAl Viro 			filp = ERR_PTR(error);
2259c3e380b0SAl Viro 		else
226073d049a4SAl Viro 			filp = do_last(nd, &path, op, pathname);
2261574197e0SAl Viro 		put_link(nd, &link, cookie);
2262806b681cSAl Viro 	}
226310fa8e62SAl Viro out:
226473d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
226573d049a4SAl Viro 		path_put(&nd->root);
2266fe2d35ffSAl Viro 	if (base)
2267fe2d35ffSAl Viro 		fput(base);
226873d049a4SAl Viro 	release_open_intent(nd);
226910fa8e62SAl Viro 	return filp;
22701da177e4SLinus Torvalds 
227131e6b01fSNick Piggin out_filp:
227210fa8e62SAl Viro 	filp = ERR_PTR(error);
227310fa8e62SAl Viro 	goto out;
2274de459215SKirill Korotaev }
22751da177e4SLinus Torvalds 
227613aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
227713aab428SAl Viro 		const struct open_flags *op, int flags)
227813aab428SAl Viro {
227973d049a4SAl Viro 	struct nameidata nd;
228013aab428SAl Viro 	struct file *filp;
228113aab428SAl Viro 
228273d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
228313aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
228473d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
228513aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
228673d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
228713aab428SAl Viro 	return filp;
228813aab428SAl Viro }
228913aab428SAl Viro 
229073d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
229173d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
229273d049a4SAl Viro {
229373d049a4SAl Viro 	struct nameidata nd;
229473d049a4SAl Viro 	struct file *file;
229573d049a4SAl Viro 
229673d049a4SAl Viro 	nd.root.mnt = mnt;
229773d049a4SAl Viro 	nd.root.dentry = dentry;
229873d049a4SAl Viro 
229973d049a4SAl Viro 	flags |= LOOKUP_ROOT;
230073d049a4SAl Viro 
2301bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
230273d049a4SAl Viro 		return ERR_PTR(-ELOOP);
230373d049a4SAl Viro 
230473d049a4SAl Viro 	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
230573d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
230673d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags);
230773d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
230873d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
230973d049a4SAl Viro 	return file;
231073d049a4SAl Viro }
231173d049a4SAl Viro 
2312ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
23131da177e4SLinus Torvalds {
2314c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
2315ed75e95dSAl Viro 	struct nameidata nd;
2316ed75e95dSAl Viro 	int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2317ed75e95dSAl Viro 	if (error)
2318ed75e95dSAl Viro 		return ERR_PTR(error);
23191da177e4SLinus Torvalds 
2320c663e5d8SChristoph Hellwig 	/*
2321c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2322c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2323c663e5d8SChristoph Hellwig 	 */
2324ed75e95dSAl Viro 	if (nd.last_type != LAST_NORM)
2325ed75e95dSAl Viro 		goto out;
2326ed75e95dSAl Viro 	nd.flags &= ~LOOKUP_PARENT;
2327ed75e95dSAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2328ed75e95dSAl Viro 	nd.intent.open.flags = O_EXCL;
2329c663e5d8SChristoph Hellwig 
2330c663e5d8SChristoph Hellwig 	/*
2331c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2332c663e5d8SChristoph Hellwig 	 */
2333ed75e95dSAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2334ed75e95dSAl Viro 	dentry = lookup_hash(&nd);
23351da177e4SLinus Torvalds 	if (IS_ERR(dentry))
23361da177e4SLinus Torvalds 		goto fail;
2337c663e5d8SChristoph Hellwig 
2338e9baf6e5SAl Viro 	if (dentry->d_inode)
2339e9baf6e5SAl Viro 		goto eexist;
2340c663e5d8SChristoph Hellwig 	/*
2341c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2342c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2343c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2344c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2345c663e5d8SChristoph Hellwig 	 */
2346ed75e95dSAl Viro 	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
23471da177e4SLinus Torvalds 		dput(dentry);
23481da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2349ed75e95dSAl Viro 		goto fail;
2350e9baf6e5SAl Viro 	}
2351ed75e95dSAl Viro 	*path = nd.path;
2352e9baf6e5SAl Viro 	return dentry;
2353e9baf6e5SAl Viro eexist:
2354e9baf6e5SAl Viro 	dput(dentry);
2355e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
23561da177e4SLinus Torvalds fail:
2357dae6ad8fSAl Viro 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2358ed75e95dSAl Viro out:
2359dae6ad8fSAl Viro 	path_put(&nd.path);
2360ed75e95dSAl Viro 	return dentry;
2361dae6ad8fSAl Viro }
2362dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
2363dae6ad8fSAl Viro 
2364dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2365dae6ad8fSAl Viro {
2366dae6ad8fSAl Viro 	char *tmp = getname(pathname);
2367dae6ad8fSAl Viro 	struct dentry *res;
2368dae6ad8fSAl Viro 	if (IS_ERR(tmp))
2369dae6ad8fSAl Viro 		return ERR_CAST(tmp);
2370dae6ad8fSAl Viro 	res = kern_path_create(dfd, tmp, path, is_dir);
2371dae6ad8fSAl Viro 	putname(tmp);
2372dae6ad8fSAl Viro 	return res;
2373dae6ad8fSAl Viro }
2374dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
2375dae6ad8fSAl Viro 
23761da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
23771da177e4SLinus Torvalds {
2378a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
23791da177e4SLinus Torvalds 
23801da177e4SLinus Torvalds 	if (error)
23811da177e4SLinus Torvalds 		return error;
23821da177e4SLinus Torvalds 
2383e795b717SSerge E. Hallyn 	if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2384e795b717SSerge E. Hallyn 	    !ns_capable(inode_userns(dir), CAP_MKNOD))
23851da177e4SLinus Torvalds 		return -EPERM;
23861da177e4SLinus Torvalds 
2387acfa4380SAl Viro 	if (!dir->i_op->mknod)
23881da177e4SLinus Torvalds 		return -EPERM;
23891da177e4SLinus Torvalds 
239008ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
239108ce5f16SSerge E. Hallyn 	if (error)
239208ce5f16SSerge E. Hallyn 		return error;
239308ce5f16SSerge E. Hallyn 
23941da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
23951da177e4SLinus Torvalds 	if (error)
23961da177e4SLinus Torvalds 		return error;
23971da177e4SLinus Torvalds 
23981da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2399a74574aaSStephen Smalley 	if (!error)
2400f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
24011da177e4SLinus Torvalds 	return error;
24021da177e4SLinus Torvalds }
24031da177e4SLinus Torvalds 
2404463c3197SDave Hansen static int may_mknod(mode_t mode)
2405463c3197SDave Hansen {
2406463c3197SDave Hansen 	switch (mode & S_IFMT) {
2407463c3197SDave Hansen 	case S_IFREG:
2408463c3197SDave Hansen 	case S_IFCHR:
2409463c3197SDave Hansen 	case S_IFBLK:
2410463c3197SDave Hansen 	case S_IFIFO:
2411463c3197SDave Hansen 	case S_IFSOCK:
2412463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2413463c3197SDave Hansen 		return 0;
2414463c3197SDave Hansen 	case S_IFDIR:
2415463c3197SDave Hansen 		return -EPERM;
2416463c3197SDave Hansen 	default:
2417463c3197SDave Hansen 		return -EINVAL;
2418463c3197SDave Hansen 	}
2419463c3197SDave Hansen }
2420463c3197SDave Hansen 
24212e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
24222e4d0924SHeiko Carstens 		unsigned, dev)
24231da177e4SLinus Torvalds {
24241da177e4SLinus Torvalds 	struct dentry *dentry;
2425dae6ad8fSAl Viro 	struct path path;
2426dae6ad8fSAl Viro 	int error;
24271da177e4SLinus Torvalds 
24281da177e4SLinus Torvalds 	if (S_ISDIR(mode))
24291da177e4SLinus Torvalds 		return -EPERM;
24301da177e4SLinus Torvalds 
2431dae6ad8fSAl Viro 	dentry = user_path_create(dfd, filename, &path, 0);
2432dae6ad8fSAl Viro 	if (IS_ERR(dentry))
2433dae6ad8fSAl Viro 		return PTR_ERR(dentry);
24342ad94ae6SAl Viro 
2435dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
2436ce3b0f8dSAl Viro 		mode &= ~current_umask();
2437463c3197SDave Hansen 	error = may_mknod(mode);
2438463c3197SDave Hansen 	if (error)
2439463c3197SDave Hansen 		goto out_dput;
2440dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
2441463c3197SDave Hansen 	if (error)
2442463c3197SDave Hansen 		goto out_dput;
2443dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
2444be6d3e56SKentaro Takeda 	if (error)
2445be6d3e56SKentaro Takeda 		goto out_drop_write;
24461da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
24471da177e4SLinus Torvalds 		case 0: case S_IFREG:
2448dae6ad8fSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
24491da177e4SLinus Torvalds 			break;
24501da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
2451dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
24521da177e4SLinus Torvalds 					new_decode_dev(dev));
24531da177e4SLinus Torvalds 			break;
24541da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
2455dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
24561da177e4SLinus Torvalds 			break;
24571da177e4SLinus Torvalds 	}
2458be6d3e56SKentaro Takeda out_drop_write:
2459dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
2460463c3197SDave Hansen out_dput:
24611da177e4SLinus Torvalds 	dput(dentry);
2462dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2463dae6ad8fSAl Viro 	path_put(&path);
24641da177e4SLinus Torvalds 
24651da177e4SLinus Torvalds 	return error;
24661da177e4SLinus Torvalds }
24671da177e4SLinus Torvalds 
24683480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
24695590ff0dSUlrich Drepper {
24705590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
24715590ff0dSUlrich Drepper }
24725590ff0dSUlrich Drepper 
24731da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
24741da177e4SLinus Torvalds {
2475a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
24761da177e4SLinus Torvalds 
24771da177e4SLinus Torvalds 	if (error)
24781da177e4SLinus Torvalds 		return error;
24791da177e4SLinus Torvalds 
2480acfa4380SAl Viro 	if (!dir->i_op->mkdir)
24811da177e4SLinus Torvalds 		return -EPERM;
24821da177e4SLinus Torvalds 
24831da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
24841da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
24851da177e4SLinus Torvalds 	if (error)
24861da177e4SLinus Torvalds 		return error;
24871da177e4SLinus Torvalds 
24881da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2489a74574aaSStephen Smalley 	if (!error)
2490f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
24911da177e4SLinus Torvalds 	return error;
24921da177e4SLinus Torvalds }
24931da177e4SLinus Torvalds 
24942e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
24951da177e4SLinus Torvalds {
24966902d925SDave Hansen 	struct dentry *dentry;
2497dae6ad8fSAl Viro 	struct path path;
2498dae6ad8fSAl Viro 	int error;
24991da177e4SLinus Torvalds 
2500dae6ad8fSAl Viro 	dentry = user_path_create(dfd, pathname, &path, 1);
25016902d925SDave Hansen 	if (IS_ERR(dentry))
2502dae6ad8fSAl Viro 		return PTR_ERR(dentry);
25036902d925SDave Hansen 
2504dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
2505ce3b0f8dSAl Viro 		mode &= ~current_umask();
2506dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
2507463c3197SDave Hansen 	if (error)
2508463c3197SDave Hansen 		goto out_dput;
2509dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
2510be6d3e56SKentaro Takeda 	if (error)
2511be6d3e56SKentaro Takeda 		goto out_drop_write;
2512dae6ad8fSAl Viro 	error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
2513be6d3e56SKentaro Takeda out_drop_write:
2514dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
2515463c3197SDave Hansen out_dput:
25161da177e4SLinus Torvalds 	dput(dentry);
2517dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2518dae6ad8fSAl Viro 	path_put(&path);
25191da177e4SLinus Torvalds 	return error;
25201da177e4SLinus Torvalds }
25211da177e4SLinus Torvalds 
25223cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
25235590ff0dSUlrich Drepper {
25245590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
25255590ff0dSUlrich Drepper }
25265590ff0dSUlrich Drepper 
25271da177e4SLinus Torvalds /*
2528a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
2529a71905f0SSage Weil  * should have a usage count of 2 if we're the only user of this
2530a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
2531a71905f0SSage Weil  * then we drop the dentry now.
25321da177e4SLinus Torvalds  *
25331da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
25341da177e4SLinus Torvalds  * do a
25351da177e4SLinus Torvalds  *
25361da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
25371da177e4SLinus Torvalds  *		return -EBUSY;
25381da177e4SLinus Torvalds  *
25391da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
25401da177e4SLinus Torvalds  * that is still in use by something else..
25411da177e4SLinus Torvalds  */
25421da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
25431da177e4SLinus Torvalds {
25441da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
25451da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
254664252c75SSage Weil 	if (dentry->d_count == 1)
25471da177e4SLinus Torvalds 		__d_drop(dentry);
25481da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
25491da177e4SLinus Torvalds }
25501da177e4SLinus Torvalds 
25511da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
25521da177e4SLinus Torvalds {
25531da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
25541da177e4SLinus Torvalds 
25551da177e4SLinus Torvalds 	if (error)
25561da177e4SLinus Torvalds 		return error;
25571da177e4SLinus Torvalds 
2558acfa4380SAl Viro 	if (!dir->i_op->rmdir)
25591da177e4SLinus Torvalds 		return -EPERM;
25601da177e4SLinus Torvalds 
25611b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
2562912dbc15SSage Weil 
25631da177e4SLinus Torvalds 	error = -EBUSY;
2564912dbc15SSage Weil 	if (d_mountpoint(dentry))
2565912dbc15SSage Weil 		goto out;
2566912dbc15SSage Weil 
25671da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
2568912dbc15SSage Weil 	if (error)
2569912dbc15SSage Weil 		goto out;
2570912dbc15SSage Weil 
25713cebde24SSage Weil 	shrink_dcache_parent(dentry);
25721da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
2573912dbc15SSage Weil 	if (error)
2574912dbc15SSage Weil 		goto out;
2575912dbc15SSage Weil 
25761da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
2577d83c49f3SAl Viro 	dont_mount(dentry);
25781da177e4SLinus Torvalds 
2579912dbc15SSage Weil out:
2580912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
2581912dbc15SSage Weil 	if (!error)
2582912dbc15SSage Weil 		d_delete(dentry);
25831da177e4SLinus Torvalds 	return error;
25841da177e4SLinus Torvalds }
25851da177e4SLinus Torvalds 
25865590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
25871da177e4SLinus Torvalds {
25881da177e4SLinus Torvalds 	int error = 0;
25891da177e4SLinus Torvalds 	char * name;
25901da177e4SLinus Torvalds 	struct dentry *dentry;
25911da177e4SLinus Torvalds 	struct nameidata nd;
25921da177e4SLinus Torvalds 
25932ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
25941da177e4SLinus Torvalds 	if (error)
25952ad94ae6SAl Viro 		return error;
25961da177e4SLinus Torvalds 
25971da177e4SLinus Torvalds 	switch(nd.last_type) {
25981da177e4SLinus Torvalds 	case LAST_DOTDOT:
25991da177e4SLinus Torvalds 		error = -ENOTEMPTY;
26001da177e4SLinus Torvalds 		goto exit1;
26011da177e4SLinus Torvalds 	case LAST_DOT:
26021da177e4SLinus Torvalds 		error = -EINVAL;
26031da177e4SLinus Torvalds 		goto exit1;
26041da177e4SLinus Torvalds 	case LAST_ROOT:
26051da177e4SLinus Torvalds 		error = -EBUSY;
26061da177e4SLinus Torvalds 		goto exit1;
26071da177e4SLinus Torvalds 	}
26080612d9fbSOGAWA Hirofumi 
26090612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
26100612d9fbSOGAWA Hirofumi 
26114ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
261249705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
26131da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
26146902d925SDave Hansen 	if (IS_ERR(dentry))
26156902d925SDave Hansen 		goto exit2;
2616e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
2617e6bc45d6STheodore Ts'o 		error = -ENOENT;
2618e6bc45d6STheodore Ts'o 		goto exit3;
2619e6bc45d6STheodore Ts'o 	}
26200622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
26210622753bSDave Hansen 	if (error)
26220622753bSDave Hansen 		goto exit3;
2623be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2624be6d3e56SKentaro Takeda 	if (error)
2625be6d3e56SKentaro Takeda 		goto exit4;
26264ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2627be6d3e56SKentaro Takeda exit4:
26280622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
26290622753bSDave Hansen exit3:
26301da177e4SLinus Torvalds 	dput(dentry);
26316902d925SDave Hansen exit2:
26324ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
26331da177e4SLinus Torvalds exit1:
26341d957f9bSJan Blunck 	path_put(&nd.path);
26351da177e4SLinus Torvalds 	putname(name);
26361da177e4SLinus Torvalds 	return error;
26371da177e4SLinus Torvalds }
26381da177e4SLinus Torvalds 
26393cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
26405590ff0dSUlrich Drepper {
26415590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
26425590ff0dSUlrich Drepper }
26435590ff0dSUlrich Drepper 
26441da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
26451da177e4SLinus Torvalds {
26461da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
26471da177e4SLinus Torvalds 
26481da177e4SLinus Torvalds 	if (error)
26491da177e4SLinus Torvalds 		return error;
26501da177e4SLinus Torvalds 
2651acfa4380SAl Viro 	if (!dir->i_op->unlink)
26521da177e4SLinus Torvalds 		return -EPERM;
26531da177e4SLinus Torvalds 
26541b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
26551da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
26561da177e4SLinus Torvalds 		error = -EBUSY;
26571da177e4SLinus Torvalds 	else {
26581da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2659bec1052eSAl Viro 		if (!error) {
26601da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2661bec1052eSAl Viro 			if (!error)
2662d83c49f3SAl Viro 				dont_mount(dentry);
2663bec1052eSAl Viro 		}
26641da177e4SLinus Torvalds 	}
26651b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
26661da177e4SLinus Torvalds 
26671da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
26681da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2669ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
26701da177e4SLinus Torvalds 		d_delete(dentry);
26711da177e4SLinus Torvalds 	}
26720eeca283SRobert Love 
26731da177e4SLinus Torvalds 	return error;
26741da177e4SLinus Torvalds }
26751da177e4SLinus Torvalds 
26761da177e4SLinus Torvalds /*
26771da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
26781b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
26791da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
26801da177e4SLinus Torvalds  * while waiting on the I/O.
26811da177e4SLinus Torvalds  */
26825590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
26831da177e4SLinus Torvalds {
26842ad94ae6SAl Viro 	int error;
26851da177e4SLinus Torvalds 	char *name;
26861da177e4SLinus Torvalds 	struct dentry *dentry;
26871da177e4SLinus Torvalds 	struct nameidata nd;
26881da177e4SLinus Torvalds 	struct inode *inode = NULL;
26891da177e4SLinus Torvalds 
26902ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
26911da177e4SLinus Torvalds 	if (error)
26922ad94ae6SAl Viro 		return error;
26932ad94ae6SAl Viro 
26941da177e4SLinus Torvalds 	error = -EISDIR;
26951da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
26961da177e4SLinus Torvalds 		goto exit1;
26970612d9fbSOGAWA Hirofumi 
26980612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
26990612d9fbSOGAWA Hirofumi 
27004ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
270149705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
27021da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27031da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
27041da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
270550338b88STörök Edwin 		if (nd.last.name[nd.last.len])
270650338b88STörök Edwin 			goto slashes;
27071da177e4SLinus Torvalds 		inode = dentry->d_inode;
270850338b88STörök Edwin 		if (!inode)
2709e6bc45d6STheodore Ts'o 			goto slashes;
27107de9c6eeSAl Viro 		ihold(inode);
27110622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
27120622753bSDave Hansen 		if (error)
27130622753bSDave Hansen 			goto exit2;
2714be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2715be6d3e56SKentaro Takeda 		if (error)
2716be6d3e56SKentaro Takeda 			goto exit3;
27174ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2718be6d3e56SKentaro Takeda exit3:
27190622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
27201da177e4SLinus Torvalds 	exit2:
27211da177e4SLinus Torvalds 		dput(dentry);
27221da177e4SLinus Torvalds 	}
27234ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27241da177e4SLinus Torvalds 	if (inode)
27251da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
27261da177e4SLinus Torvalds exit1:
27271d957f9bSJan Blunck 	path_put(&nd.path);
27281da177e4SLinus Torvalds 	putname(name);
27291da177e4SLinus Torvalds 	return error;
27301da177e4SLinus Torvalds 
27311da177e4SLinus Torvalds slashes:
27321da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
27331da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
27341da177e4SLinus Torvalds 	goto exit2;
27351da177e4SLinus Torvalds }
27361da177e4SLinus Torvalds 
27372e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
27385590ff0dSUlrich Drepper {
27395590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
27405590ff0dSUlrich Drepper 		return -EINVAL;
27415590ff0dSUlrich Drepper 
27425590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
27435590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
27445590ff0dSUlrich Drepper 
27455590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
27465590ff0dSUlrich Drepper }
27475590ff0dSUlrich Drepper 
27483480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
27495590ff0dSUlrich Drepper {
27505590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
27515590ff0dSUlrich Drepper }
27525590ff0dSUlrich Drepper 
2753db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
27541da177e4SLinus Torvalds {
2755a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
27561da177e4SLinus Torvalds 
27571da177e4SLinus Torvalds 	if (error)
27581da177e4SLinus Torvalds 		return error;
27591da177e4SLinus Torvalds 
2760acfa4380SAl Viro 	if (!dir->i_op->symlink)
27611da177e4SLinus Torvalds 		return -EPERM;
27621da177e4SLinus Torvalds 
27631da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
27641da177e4SLinus Torvalds 	if (error)
27651da177e4SLinus Torvalds 		return error;
27661da177e4SLinus Torvalds 
27671da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2768a74574aaSStephen Smalley 	if (!error)
2769f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
27701da177e4SLinus Torvalds 	return error;
27711da177e4SLinus Torvalds }
27721da177e4SLinus Torvalds 
27732e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
27742e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
27751da177e4SLinus Torvalds {
27762ad94ae6SAl Viro 	int error;
27771da177e4SLinus Torvalds 	char *from;
27786902d925SDave Hansen 	struct dentry *dentry;
2779dae6ad8fSAl Viro 	struct path path;
27801da177e4SLinus Torvalds 
27811da177e4SLinus Torvalds 	from = getname(oldname);
27821da177e4SLinus Torvalds 	if (IS_ERR(from))
27831da177e4SLinus Torvalds 		return PTR_ERR(from);
27842ad94ae6SAl Viro 
2785dae6ad8fSAl Viro 	dentry = user_path_create(newdfd, newname, &path, 0);
27861da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27876902d925SDave Hansen 	if (IS_ERR(dentry))
2788dae6ad8fSAl Viro 		goto out_putname;
27896902d925SDave Hansen 
2790dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
279175c3f29dSDave Hansen 	if (error)
279275c3f29dSDave Hansen 		goto out_dput;
2793dae6ad8fSAl Viro 	error = security_path_symlink(&path, dentry, from);
2794be6d3e56SKentaro Takeda 	if (error)
2795be6d3e56SKentaro Takeda 		goto out_drop_write;
2796dae6ad8fSAl Viro 	error = vfs_symlink(path.dentry->d_inode, dentry, from);
2797be6d3e56SKentaro Takeda out_drop_write:
2798dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
279975c3f29dSDave Hansen out_dput:
28001da177e4SLinus Torvalds 	dput(dentry);
2801dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2802dae6ad8fSAl Viro 	path_put(&path);
28036902d925SDave Hansen out_putname:
28041da177e4SLinus Torvalds 	putname(from);
28051da177e4SLinus Torvalds 	return error;
28061da177e4SLinus Torvalds }
28071da177e4SLinus Torvalds 
28083480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
28095590ff0dSUlrich Drepper {
28105590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
28115590ff0dSUlrich Drepper }
28125590ff0dSUlrich Drepper 
28131da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
28141da177e4SLinus Torvalds {
28151da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
28161da177e4SLinus Torvalds 	int error;
28171da177e4SLinus Torvalds 
28181da177e4SLinus Torvalds 	if (!inode)
28191da177e4SLinus Torvalds 		return -ENOENT;
28201da177e4SLinus Torvalds 
2821a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
28221da177e4SLinus Torvalds 	if (error)
28231da177e4SLinus Torvalds 		return error;
28241da177e4SLinus Torvalds 
28251da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
28261da177e4SLinus Torvalds 		return -EXDEV;
28271da177e4SLinus Torvalds 
28281da177e4SLinus Torvalds 	/*
28291da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
28301da177e4SLinus Torvalds 	 */
28311da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
28321da177e4SLinus Torvalds 		return -EPERM;
2833acfa4380SAl Viro 	if (!dir->i_op->link)
28341da177e4SLinus Torvalds 		return -EPERM;
28357e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
28361da177e4SLinus Torvalds 		return -EPERM;
28371da177e4SLinus Torvalds 
28381da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
28391da177e4SLinus Torvalds 	if (error)
28401da177e4SLinus Torvalds 		return error;
28411da177e4SLinus Torvalds 
28427e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
2843aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
2844aae8a97dSAneesh Kumar K.V 	if (inode->i_nlink == 0)
2845aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
2846aae8a97dSAneesh Kumar K.V 	else
28471da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
28487e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2849e31e14ecSStephen Smalley 	if (!error)
28507e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
28511da177e4SLinus Torvalds 	return error;
28521da177e4SLinus Torvalds }
28531da177e4SLinus Torvalds 
28541da177e4SLinus Torvalds /*
28551da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
28561da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
28571da177e4SLinus Torvalds  * newname.  --KAB
28581da177e4SLinus Torvalds  *
28591da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
28601da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
28611da177e4SLinus Torvalds  * and other special files.  --ADM
28621da177e4SLinus Torvalds  */
28632e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
28642e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
28651da177e4SLinus Torvalds {
28661da177e4SLinus Torvalds 	struct dentry *new_dentry;
2867dae6ad8fSAl Viro 	struct path old_path, new_path;
286811a7b371SAneesh Kumar K.V 	int how = 0;
28691da177e4SLinus Torvalds 	int error;
28701da177e4SLinus Torvalds 
287111a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
2872c04030e1SUlrich Drepper 		return -EINVAL;
287311a7b371SAneesh Kumar K.V 	/*
287411a7b371SAneesh Kumar K.V 	 * To use null names we require CAP_DAC_READ_SEARCH
287511a7b371SAneesh Kumar K.V 	 * This ensures that not everyone will be able to create
287611a7b371SAneesh Kumar K.V 	 * handlink using the passed filedescriptor.
287711a7b371SAneesh Kumar K.V 	 */
287811a7b371SAneesh Kumar K.V 	if (flags & AT_EMPTY_PATH) {
287911a7b371SAneesh Kumar K.V 		if (!capable(CAP_DAC_READ_SEARCH))
288011a7b371SAneesh Kumar K.V 			return -ENOENT;
288111a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
288211a7b371SAneesh Kumar K.V 	}
2883c04030e1SUlrich Drepper 
288411a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
288511a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
288611a7b371SAneesh Kumar K.V 
288711a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
28881da177e4SLinus Torvalds 	if (error)
28892ad94ae6SAl Viro 		return error;
28902ad94ae6SAl Viro 
2891dae6ad8fSAl Viro 	new_dentry = user_path_create(newdfd, newname, &new_path, 0);
28921da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
28936902d925SDave Hansen 	if (IS_ERR(new_dentry))
2894dae6ad8fSAl Viro 		goto out;
2895dae6ad8fSAl Viro 
2896dae6ad8fSAl Viro 	error = -EXDEV;
2897dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
2898dae6ad8fSAl Viro 		goto out_dput;
2899dae6ad8fSAl Viro 	error = mnt_want_write(new_path.mnt);
290075c3f29dSDave Hansen 	if (error)
290175c3f29dSDave Hansen 		goto out_dput;
2902dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
2903be6d3e56SKentaro Takeda 	if (error)
2904be6d3e56SKentaro Takeda 		goto out_drop_write;
2905dae6ad8fSAl Viro 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
2906be6d3e56SKentaro Takeda out_drop_write:
2907dae6ad8fSAl Viro 	mnt_drop_write(new_path.mnt);
290875c3f29dSDave Hansen out_dput:
29091da177e4SLinus Torvalds 	dput(new_dentry);
2910dae6ad8fSAl Viro 	mutex_unlock(&new_path.dentry->d_inode->i_mutex);
2911dae6ad8fSAl Viro 	path_put(&new_path);
29121da177e4SLinus Torvalds out:
29132d8f3038SAl Viro 	path_put(&old_path);
29141da177e4SLinus Torvalds 
29151da177e4SLinus Torvalds 	return error;
29161da177e4SLinus Torvalds }
29171da177e4SLinus Torvalds 
29183480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
29195590ff0dSUlrich Drepper {
2920c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
29215590ff0dSUlrich Drepper }
29225590ff0dSUlrich Drepper 
29231da177e4SLinus Torvalds /*
29241da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
29251da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
29261da177e4SLinus Torvalds  * Problems:
29271da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
29281da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
29291da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
2930a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
29311da177e4SLinus Torvalds  *	   story.
29321da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
29331b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
29341da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
29351da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
2936a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
29371da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
29381da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
29391da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
2940a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
29411da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
29421da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
29431da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
2944e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
29451b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
29461da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2947c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
29481da177e4SLinus Torvalds  *	   locking].
29491da177e4SLinus Torvalds  */
295075c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
29511da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
29521da177e4SLinus Torvalds {
29531da177e4SLinus Torvalds 	int error = 0;
29549055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
29551da177e4SLinus Torvalds 
29561da177e4SLinus Torvalds 	/*
29571da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
29581da177e4SLinus Torvalds 	 * we'll need to flip '..'.
29591da177e4SLinus Torvalds 	 */
29601da177e4SLinus Torvalds 	if (new_dir != old_dir) {
2961f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
29621da177e4SLinus Torvalds 		if (error)
29631da177e4SLinus Torvalds 			return error;
29641da177e4SLinus Torvalds 	}
29651da177e4SLinus Torvalds 
29661da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
29671da177e4SLinus Torvalds 	if (error)
29681da177e4SLinus Torvalds 		return error;
29691da177e4SLinus Torvalds 
2970d83c49f3SAl Viro 	if (target)
29711b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
29729055cba7SSage Weil 
29731da177e4SLinus Torvalds 	error = -EBUSY;
29749055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
29759055cba7SSage Weil 		goto out;
29769055cba7SSage Weil 
29773cebde24SSage Weil 	if (target)
29783cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
29791da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
29809055cba7SSage Weil 	if (error)
29819055cba7SSage Weil 		goto out;
29829055cba7SSage Weil 
29831da177e4SLinus Torvalds 	if (target) {
29841da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
2985d83c49f3SAl Viro 		dont_mount(new_dentry);
2986d83c49f3SAl Viro 	}
29879055cba7SSage Weil out:
29889055cba7SSage Weil 	if (target)
29891b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
2990e31e14ecSStephen Smalley 	if (!error)
2991349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
29921da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
29931da177e4SLinus Torvalds 	return error;
29941da177e4SLinus Torvalds }
29951da177e4SLinus Torvalds 
299675c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
29971da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
29981da177e4SLinus Torvalds {
299951892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
30001da177e4SLinus Torvalds 	int error;
30011da177e4SLinus Torvalds 
30021da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30031da177e4SLinus Torvalds 	if (error)
30041da177e4SLinus Torvalds 		return error;
30051da177e4SLinus Torvalds 
30061da177e4SLinus Torvalds 	dget(new_dentry);
30071da177e4SLinus Torvalds 	if (target)
30081b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
300951892bbbSSage Weil 
30101da177e4SLinus Torvalds 	error = -EBUSY;
301151892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
301251892bbbSSage Weil 		goto out;
301351892bbbSSage Weil 
30141da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
301551892bbbSSage Weil 	if (error)
301651892bbbSSage Weil 		goto out;
301751892bbbSSage Weil 
3018bec1052eSAl Viro 	if (target)
3019d83c49f3SAl Viro 		dont_mount(new_dentry);
3020349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30211da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
302251892bbbSSage Weil out:
30231da177e4SLinus Torvalds 	if (target)
30241b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
30251da177e4SLinus Torvalds 	dput(new_dentry);
30261da177e4SLinus Torvalds 	return error;
30271da177e4SLinus Torvalds }
30281da177e4SLinus Torvalds 
30291da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
30301da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
30311da177e4SLinus Torvalds {
30321da177e4SLinus Torvalds 	int error;
30331da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
303459b0df21SEric Paris 	const unsigned char *old_name;
30351da177e4SLinus Torvalds 
30361da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
30371da177e4SLinus Torvalds  		return 0;
30381da177e4SLinus Torvalds 
30391da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
30401da177e4SLinus Torvalds 	if (error)
30411da177e4SLinus Torvalds 		return error;
30421da177e4SLinus Torvalds 
30431da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3044a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
30451da177e4SLinus Torvalds 	else
30461da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
30471da177e4SLinus Torvalds 	if (error)
30481da177e4SLinus Torvalds 		return error;
30491da177e4SLinus Torvalds 
3050acfa4380SAl Viro 	if (!old_dir->i_op->rename)
30511da177e4SLinus Torvalds 		return -EPERM;
30521da177e4SLinus Torvalds 
30530eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
30540eeca283SRobert Love 
30551da177e4SLinus Torvalds 	if (is_dir)
30561da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
30571da177e4SLinus Torvalds 	else
30581da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3059123df294SAl Viro 	if (!error)
3060123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
30615a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
30620eeca283SRobert Love 	fsnotify_oldname_free(old_name);
30630eeca283SRobert Love 
30641da177e4SLinus Torvalds 	return error;
30651da177e4SLinus Torvalds }
30661da177e4SLinus Torvalds 
30672e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
30682e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
30691da177e4SLinus Torvalds {
30701da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
30711da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
30721da177e4SLinus Torvalds 	struct dentry *trap;
30731da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
30742ad94ae6SAl Viro 	char *from;
30752ad94ae6SAl Viro 	char *to;
30762ad94ae6SAl Viro 	int error;
30771da177e4SLinus Torvalds 
30782ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
30791da177e4SLinus Torvalds 	if (error)
30801da177e4SLinus Torvalds 		goto exit;
30811da177e4SLinus Torvalds 
30822ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
30831da177e4SLinus Torvalds 	if (error)
30841da177e4SLinus Torvalds 		goto exit1;
30851da177e4SLinus Torvalds 
30861da177e4SLinus Torvalds 	error = -EXDEV;
30874ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
30881da177e4SLinus Torvalds 		goto exit2;
30891da177e4SLinus Torvalds 
30904ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
30911da177e4SLinus Torvalds 	error = -EBUSY;
30921da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
30931da177e4SLinus Torvalds 		goto exit2;
30941da177e4SLinus Torvalds 
30954ac91378SJan Blunck 	new_dir = newnd.path.dentry;
30961da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
30971da177e4SLinus Torvalds 		goto exit2;
30981da177e4SLinus Torvalds 
30990612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
31000612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
31014e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
31020612d9fbSOGAWA Hirofumi 
31031da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
31041da177e4SLinus Torvalds 
310549705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
31061da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
31071da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
31081da177e4SLinus Torvalds 		goto exit3;
31091da177e4SLinus Torvalds 	/* source must exist */
31101da177e4SLinus Torvalds 	error = -ENOENT;
31111da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
31121da177e4SLinus Torvalds 		goto exit4;
31131da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
31141da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
31151da177e4SLinus Torvalds 		error = -ENOTDIR;
31161da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
31171da177e4SLinus Torvalds 			goto exit4;
31181da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
31191da177e4SLinus Torvalds 			goto exit4;
31201da177e4SLinus Torvalds 	}
31211da177e4SLinus Torvalds 	/* source should not be ancestor of target */
31221da177e4SLinus Torvalds 	error = -EINVAL;
31231da177e4SLinus Torvalds 	if (old_dentry == trap)
31241da177e4SLinus Torvalds 		goto exit4;
312549705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
31261da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
31271da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
31281da177e4SLinus Torvalds 		goto exit4;
31291da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
31301da177e4SLinus Torvalds 	error = -ENOTEMPTY;
31311da177e4SLinus Torvalds 	if (new_dentry == trap)
31321da177e4SLinus Torvalds 		goto exit5;
31331da177e4SLinus Torvalds 
31349079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
31359079b1ebSDave Hansen 	if (error)
31369079b1ebSDave Hansen 		goto exit5;
3137be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3138be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3139be6d3e56SKentaro Takeda 	if (error)
3140be6d3e56SKentaro Takeda 		goto exit6;
31411da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
31421da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3143be6d3e56SKentaro Takeda exit6:
31449079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
31451da177e4SLinus Torvalds exit5:
31461da177e4SLinus Torvalds 	dput(new_dentry);
31471da177e4SLinus Torvalds exit4:
31481da177e4SLinus Torvalds 	dput(old_dentry);
31491da177e4SLinus Torvalds exit3:
31501da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
31511da177e4SLinus Torvalds exit2:
31521d957f9bSJan Blunck 	path_put(&newnd.path);
31532ad94ae6SAl Viro 	putname(to);
31541da177e4SLinus Torvalds exit1:
31551d957f9bSJan Blunck 	path_put(&oldnd.path);
31561da177e4SLinus Torvalds 	putname(from);
31572ad94ae6SAl Viro exit:
31581da177e4SLinus Torvalds 	return error;
31591da177e4SLinus Torvalds }
31601da177e4SLinus Torvalds 
3161a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
31625590ff0dSUlrich Drepper {
31635590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
31645590ff0dSUlrich Drepper }
31655590ff0dSUlrich Drepper 
31661da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
31671da177e4SLinus Torvalds {
31681da177e4SLinus Torvalds 	int len;
31691da177e4SLinus Torvalds 
31701da177e4SLinus Torvalds 	len = PTR_ERR(link);
31711da177e4SLinus Torvalds 	if (IS_ERR(link))
31721da177e4SLinus Torvalds 		goto out;
31731da177e4SLinus Torvalds 
31741da177e4SLinus Torvalds 	len = strlen(link);
31751da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
31761da177e4SLinus Torvalds 		len = buflen;
31771da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
31781da177e4SLinus Torvalds 		len = -EFAULT;
31791da177e4SLinus Torvalds out:
31801da177e4SLinus Torvalds 	return len;
31811da177e4SLinus Torvalds }
31821da177e4SLinus Torvalds 
31831da177e4SLinus Torvalds /*
31841da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
31851da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
31861da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
31871da177e4SLinus Torvalds  */
31881da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
31891da177e4SLinus Torvalds {
31901da177e4SLinus Torvalds 	struct nameidata nd;
3191cc314eefSLinus Torvalds 	void *cookie;
3192694a1764SMarcin Slusarz 	int res;
3193cc314eefSLinus Torvalds 
31941da177e4SLinus Torvalds 	nd.depth = 0;
3195cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3196694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3197694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3198694a1764SMarcin Slusarz 
3199694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
32001da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3201cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3202694a1764SMarcin Slusarz 	return res;
32031da177e4SLinus Torvalds }
32041da177e4SLinus Torvalds 
32051da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
32061da177e4SLinus Torvalds {
32071da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
32081da177e4SLinus Torvalds }
32091da177e4SLinus Torvalds 
32101da177e4SLinus Torvalds /* get the link contents into pagecache */
32111da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
32121da177e4SLinus Torvalds {
3213ebd09abbSDuane Griffin 	char *kaddr;
32141da177e4SLinus Torvalds 	struct page *page;
32151da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3216090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
32171da177e4SLinus Torvalds 	if (IS_ERR(page))
32186fe6900eSNick Piggin 		return (char*)page;
32191da177e4SLinus Torvalds 	*ppage = page;
3220ebd09abbSDuane Griffin 	kaddr = kmap(page);
3221ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3222ebd09abbSDuane Griffin 	return kaddr;
32231da177e4SLinus Torvalds }
32241da177e4SLinus Torvalds 
32251da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32261da177e4SLinus Torvalds {
32271da177e4SLinus Torvalds 	struct page *page = NULL;
32281da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
32291da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
32301da177e4SLinus Torvalds 	if (page) {
32311da177e4SLinus Torvalds 		kunmap(page);
32321da177e4SLinus Torvalds 		page_cache_release(page);
32331da177e4SLinus Torvalds 	}
32341da177e4SLinus Torvalds 	return res;
32351da177e4SLinus Torvalds }
32361da177e4SLinus Torvalds 
3237cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
32381da177e4SLinus Torvalds {
3239cc314eefSLinus Torvalds 	struct page *page = NULL;
32401da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3241cc314eefSLinus Torvalds 	return page;
32421da177e4SLinus Torvalds }
32431da177e4SLinus Torvalds 
3244cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
32451da177e4SLinus Torvalds {
3246cc314eefSLinus Torvalds 	struct page *page = cookie;
3247cc314eefSLinus Torvalds 
3248cc314eefSLinus Torvalds 	if (page) {
32491da177e4SLinus Torvalds 		kunmap(page);
32501da177e4SLinus Torvalds 		page_cache_release(page);
32511da177e4SLinus Torvalds 	}
32521da177e4SLinus Torvalds }
32531da177e4SLinus Torvalds 
325454566b2cSNick Piggin /*
325554566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
325654566b2cSNick Piggin  */
325754566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
32581da177e4SLinus Torvalds {
32591da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
32600adb25d2SKirill Korotaev 	struct page *page;
3261afddba49SNick Piggin 	void *fsdata;
3262beb497abSDmitriy Monakhov 	int err;
32631da177e4SLinus Torvalds 	char *kaddr;
326454566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
326554566b2cSNick Piggin 	if (nofs)
326654566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
32671da177e4SLinus Torvalds 
32687e53cac4SNeilBrown retry:
3269afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
327054566b2cSNick Piggin 				flags, &page, &fsdata);
32711da177e4SLinus Torvalds 	if (err)
3272afddba49SNick Piggin 		goto fail;
3273afddba49SNick Piggin 
32741da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
32751da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
32761da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
3277afddba49SNick Piggin 
3278afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3279afddba49SNick Piggin 							page, fsdata);
32801da177e4SLinus Torvalds 	if (err < 0)
32811da177e4SLinus Torvalds 		goto fail;
3282afddba49SNick Piggin 	if (err < len-1)
3283afddba49SNick Piggin 		goto retry;
3284afddba49SNick Piggin 
32851da177e4SLinus Torvalds 	mark_inode_dirty(inode);
32861da177e4SLinus Torvalds 	return 0;
32871da177e4SLinus Torvalds fail:
32881da177e4SLinus Torvalds 	return err;
32891da177e4SLinus Torvalds }
32901da177e4SLinus Torvalds 
32910adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
32920adb25d2SKirill Korotaev {
32930adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
329454566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
32950adb25d2SKirill Korotaev }
32960adb25d2SKirill Korotaev 
329792e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
32981da177e4SLinus Torvalds 	.readlink	= generic_readlink,
32991da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
33001da177e4SLinus Torvalds 	.put_link	= page_put_link,
33011da177e4SLinus Torvalds };
33021da177e4SLinus Torvalds 
33032d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3304cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
33051da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
33061da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
33071da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
33081da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
33091da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
33101da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
33111da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
33121da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
33131da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
33140adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
33151da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
33161da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3317d1811465SAl Viro EXPORT_SYMBOL(kern_path);
331816f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3319f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
33201da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
33211da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
33221da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
33231da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
33241da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
33251da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
33261da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
33271da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
33281da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
33291da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
33301da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
33311da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
33321da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
33331da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3334