xref: /openbmc/linux/fs/namei.c (revision d594e7ec)
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>
351da177e4SLinus Torvalds #include <asm/uaccess.h>
361da177e4SLinus Torvalds 
37e81e3f4dSEric Paris #include "internal.h"
38e81e3f4dSEric Paris 
391da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer]
401da177e4SLinus Torvalds  * Fundamental changes in the pathname lookup mechanisms (namei)
411da177e4SLinus Torvalds  * were necessary because of omirr.  The reason is that omirr needs
421da177e4SLinus Torvalds  * to know the _real_ pathname, not the user-supplied one, in case
431da177e4SLinus Torvalds  * of symlinks (and also when transname replacements occur).
441da177e4SLinus Torvalds  *
451da177e4SLinus Torvalds  * The new code replaces the old recursive symlink resolution with
461da177e4SLinus Torvalds  * an iterative one (in case of non-nested symlink chains).  It does
471da177e4SLinus Torvalds  * this with calls to <fs>_follow_link().
481da177e4SLinus Torvalds  * As a side effect, dir_namei(), _namei() and follow_link() are now
491da177e4SLinus Torvalds  * replaced with a single function lookup_dentry() that can handle all
501da177e4SLinus Torvalds  * the special cases of the former code.
511da177e4SLinus Torvalds  *
521da177e4SLinus Torvalds  * With the new dcache, the pathname is stored at each inode, at least as
531da177e4SLinus Torvalds  * long as the refcount of the inode is positive.  As a side effect, the
541da177e4SLinus Torvalds  * size of the dcache depends on the inode cache and thus is dynamic.
551da177e4SLinus Torvalds  *
561da177e4SLinus Torvalds  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
571da177e4SLinus Torvalds  * resolution to correspond with current state of the code.
581da177e4SLinus Torvalds  *
591da177e4SLinus Torvalds  * Note that the symlink resolution is not *completely* iterative.
601da177e4SLinus Torvalds  * There is still a significant amount of tail- and mid- recursion in
611da177e4SLinus Torvalds  * the algorithm.  Also, note that <fs>_readlink() is not used in
621da177e4SLinus Torvalds  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
631da177e4SLinus Torvalds  * may return different results than <fs>_follow_link().  Many virtual
641da177e4SLinus Torvalds  * filesystems (including /proc) exhibit this behavior.
651da177e4SLinus Torvalds  */
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
681da177e4SLinus Torvalds  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
691da177e4SLinus Torvalds  * and the name already exists in form of a symlink, try to create the new
701da177e4SLinus Torvalds  * name indicated by the symlink. The old code always complained that the
711da177e4SLinus Torvalds  * name already exists, due to not following the symlink even if its target
721da177e4SLinus Torvalds  * is nonexistent.  The new semantics affects also mknod() and link() when
7325985edcSLucas De Marchi  * the name is a symlink pointing to a non-existent name.
741da177e4SLinus Torvalds  *
751da177e4SLinus Torvalds  * I don't know which semantics is the right one, since I have no access
761da177e4SLinus Torvalds  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
771da177e4SLinus Torvalds  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
781da177e4SLinus Torvalds  * "old" one. Personally, I think the new semantics is much more logical.
791da177e4SLinus Torvalds  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
801da177e4SLinus Torvalds  * file does succeed in both HP-UX and SunOs, but not in Solaris
811da177e4SLinus Torvalds  * and in the old Linux semantics.
821da177e4SLinus Torvalds  */
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
851da177e4SLinus Torvalds  * semantics.  See the comments in "open_namei" and "do_link" below.
861da177e4SLinus Torvalds  *
871da177e4SLinus Torvalds  * [10-Sep-98 Alan Modra] Another symlink change.
881da177e4SLinus Torvalds  */
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
911da177e4SLinus Torvalds  *	inside the path - always follow.
921da177e4SLinus Torvalds  *	in the last component in creation/removal/renaming - never follow.
931da177e4SLinus Torvalds  *	if LOOKUP_FOLLOW passed - follow.
941da177e4SLinus Torvalds  *	if the pathname has trailing slashes - follow.
951da177e4SLinus Torvalds  *	otherwise - don't follow.
961da177e4SLinus Torvalds  * (applied in that order).
971da177e4SLinus Torvalds  *
981da177e4SLinus Torvalds  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
991da177e4SLinus Torvalds  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
1001da177e4SLinus Torvalds  * During the 2.4 we need to fix the userland stuff depending on it -
1011da177e4SLinus Torvalds  * hopefully we will be able to get rid of that wart in 2.5. So far only
1021da177e4SLinus Torvalds  * XEmacs seems to be relying on it...
1031da177e4SLinus Torvalds  */
1041da177e4SLinus Torvalds /*
1051da177e4SLinus Torvalds  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
106a11f3a05SArjan van de Ven  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1071da177e4SLinus Torvalds  * any extra contention...
1081da177e4SLinus Torvalds  */
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional
1111da177e4SLinus Torvalds  * checking and hopefully speeding things up, we copy filenames to the
1121da177e4SLinus Torvalds  * kernel data space before using them..
1131da177e4SLinus Torvalds  *
1141da177e4SLinus Torvalds  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
1151da177e4SLinus Torvalds  * PATH_MAX includes the nul terminator --RR.
1161da177e4SLinus Torvalds  */
117858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page)
1181da177e4SLinus Torvalds {
1191da177e4SLinus Torvalds 	int retval;
1201da177e4SLinus Torvalds 	unsigned long len = PATH_MAX;
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	if (!segment_eq(get_fs(), KERNEL_DS)) {
1231da177e4SLinus Torvalds 		if ((unsigned long) filename >= TASK_SIZE)
1241da177e4SLinus Torvalds 			return -EFAULT;
1251da177e4SLinus Torvalds 		if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
1261da177e4SLinus Torvalds 			len = TASK_SIZE - (unsigned long) filename;
1271da177e4SLinus Torvalds 	}
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	retval = strncpy_from_user(page, filename, len);
1301da177e4SLinus Torvalds 	if (retval > 0) {
1311da177e4SLinus Torvalds 		if (retval < len)
1321da177e4SLinus Torvalds 			return 0;
1331da177e4SLinus Torvalds 		return -ENAMETOOLONG;
1341da177e4SLinus Torvalds 	} else if (!retval)
1351da177e4SLinus Torvalds 		retval = -ENOENT;
1361da177e4SLinus Torvalds 	return retval;
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
139f52e0c11SAl Viro static char *getname_flags(const char __user * filename, int flags)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	char *tmp, *result;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	result = ERR_PTR(-ENOMEM);
1441da177e4SLinus Torvalds 	tmp = __getname();
1451da177e4SLinus Torvalds 	if (tmp)  {
1461da177e4SLinus Torvalds 		int retval = do_getname(filename, tmp);
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 		result = tmp;
1491da177e4SLinus Torvalds 		if (retval < 0) {
150f52e0c11SAl Viro 			if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
1511da177e4SLinus Torvalds 				__putname(tmp);
1521da177e4SLinus Torvalds 				result = ERR_PTR(retval);
1531da177e4SLinus Torvalds 			}
1541da177e4SLinus Torvalds 		}
155f52e0c11SAl Viro 	}
1561da177e4SLinus Torvalds 	audit_getname(result);
1571da177e4SLinus Torvalds 	return result;
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
160f52e0c11SAl Viro char *getname(const char __user * filename)
161f52e0c11SAl Viro {
162f52e0c11SAl Viro 	return getname_flags(filename, 0);
163f52e0c11SAl Viro }
164f52e0c11SAl Viro 
1651da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
1661da177e4SLinus Torvalds void putname(const char *name)
1671da177e4SLinus Torvalds {
1685ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
1691da177e4SLinus Torvalds 		audit_putname(name);
1701da177e4SLinus Torvalds 	else
1711da177e4SLinus Torvalds 		__putname(name);
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds EXPORT_SYMBOL(putname);
1741da177e4SLinus Torvalds #endif
1751da177e4SLinus Torvalds 
1765909ccaaSLinus Torvalds /*
1775909ccaaSLinus Torvalds  * This does basic POSIX ACL permission checking
1785909ccaaSLinus Torvalds  */
1797e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask)
1805909ccaaSLinus Torvalds {
1817e40145eSAl Viro 	int (*check_acl)(struct inode *inode, int mask);
18226cf46beSLinus Torvalds 	unsigned int mode = inode->i_mode;
1835909ccaaSLinus Torvalds 
1849c2c7039SAl Viro 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
1855909ccaaSLinus Torvalds 
186e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
187e795b717SSerge E. Hallyn 		goto other_perms;
188e795b717SSerge E. Hallyn 
1895909ccaaSLinus Torvalds 	if (current_fsuid() == inode->i_uid)
1905909ccaaSLinus Torvalds 		mode >>= 6;
1915909ccaaSLinus Torvalds 	else {
192178ea735SAl Viro 		check_acl = inode->i_op->check_acl;
1935909ccaaSLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
1947e40145eSAl Viro 			int error = check_acl(inode, mask);
1955909ccaaSLinus Torvalds 			if (error != -EAGAIN)
1965909ccaaSLinus Torvalds 				return error;
1975909ccaaSLinus Torvalds 		}
1985909ccaaSLinus Torvalds 
1995909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
2005909ccaaSLinus Torvalds 			mode >>= 3;
2015909ccaaSLinus Torvalds 	}
2025909ccaaSLinus Torvalds 
203e795b717SSerge E. Hallyn other_perms:
2045909ccaaSLinus Torvalds 	/*
2055909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
2065909ccaaSLinus Torvalds 	 */
2079c2c7039SAl Viro 	if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
2085909ccaaSLinus Torvalds 		return 0;
2095909ccaaSLinus Torvalds 	return -EACCES;
2105909ccaaSLinus Torvalds }
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds /**
2131da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
2141da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2151da177e4SLinus Torvalds  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
21639191628SRandy Dunlap  * @flags:	IPERM_FLAG_ flags.
2171da177e4SLinus Torvalds  *
2181da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
2191da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
2201da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
221b74c79e9SNick Piggin  * are used for other things.
222b74c79e9SNick Piggin  *
223b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
224b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
225b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
2261da177e4SLinus Torvalds  */
2272830ba7fSAl Viro int generic_permission(struct inode *inode, int mask)
2281da177e4SLinus Torvalds {
2295909ccaaSLinus Torvalds 	int ret;
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	/*
2325909ccaaSLinus Torvalds 	 * Do the basic POSIX ACL permission checks.
2331da177e4SLinus Torvalds 	 */
2347e40145eSAl Viro 	ret = acl_permission_check(inode, mask);
2355909ccaaSLinus Torvalds 	if (ret != -EACCES)
2365909ccaaSLinus Torvalds 		return ret;
2371da177e4SLinus Torvalds 
238d594e7ecSAl Viro 	if (S_ISDIR(inode->i_mode)) {
239d594e7ecSAl Viro 		/* DACs are overridable for directories */
240d594e7ecSAl Viro 		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
241d594e7ecSAl Viro 			return 0;
242d594e7ecSAl Viro 		if (!(mask & MAY_WRITE))
243d594e7ecSAl Viro 			if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
244d594e7ecSAl Viro 				return 0;
245d594e7ecSAl Viro 		return -EACCES;
246d594e7ecSAl Viro 	}
2471da177e4SLinus Torvalds 	/*
2481da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
249d594e7ecSAl Viro 	 * Executable DACs are overridable when there is
250d594e7ecSAl Viro 	 * at least one exec bit set.
2511da177e4SLinus Torvalds 	 */
252d594e7ecSAl Viro 	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
253e795b717SSerge E. Hallyn 		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
2541da177e4SLinus Torvalds 			return 0;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	/*
2571da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
2581da177e4SLinus Torvalds 	 */
2597ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
260d594e7ecSAl Viro 	if (mask == MAY_READ)
261e795b717SSerge E. Hallyn 		if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
2621da177e4SLinus Torvalds 			return 0;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	return -EACCES;
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds 
267cb23beb5SChristoph Hellwig /**
268cb23beb5SChristoph Hellwig  * inode_permission  -  check for access rights to a given inode
269cb23beb5SChristoph Hellwig  * @inode:	inode to check permission on
270cb23beb5SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
271cb23beb5SChristoph Hellwig  *
272cb23beb5SChristoph Hellwig  * Used to check for read/write/execute permissions on an inode.
273cb23beb5SChristoph Hellwig  * We use "fsuid" for this, letting us set arbitrary permissions
274cb23beb5SChristoph Hellwig  * for filesystem access without changing the "normal" uids which
275cb23beb5SChristoph Hellwig  * are used for other things.
276cb23beb5SChristoph Hellwig  */
277f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask)
2781da177e4SLinus Torvalds {
279e6305c43SAl Viro 	int retval;
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	if (mask & MAY_WRITE) {
28222590e41SMiklos Szeredi 		umode_t mode = inode->i_mode;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 		/*
2851da177e4SLinus Torvalds 		 * Nobody gets write access to a read-only fs.
2861da177e4SLinus Torvalds 		 */
2871da177e4SLinus Torvalds 		if (IS_RDONLY(inode) &&
2881da177e4SLinus Torvalds 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
2891da177e4SLinus Torvalds 			return -EROFS;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 		/*
2921da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
2931da177e4SLinus Torvalds 		 */
2941da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
2951da177e4SLinus Torvalds 			return -EACCES;
2961da177e4SLinus Torvalds 	}
2971da177e4SLinus Torvalds 
298acfa4380SAl Viro 	if (inode->i_op->permission)
29910556cb2SAl Viro 		retval = inode->i_op->permission(inode, mask);
300f696a365SMiklos Szeredi 	else
3012830ba7fSAl Viro 		retval = generic_permission(inode, mask);
302f696a365SMiklos Szeredi 
3031da177e4SLinus Torvalds 	if (retval)
3041da177e4SLinus Torvalds 		return retval;
3051da177e4SLinus Torvalds 
30608ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
30708ce5f16SSerge E. Hallyn 	if (retval)
30808ce5f16SSerge E. Hallyn 		return retval;
30908ce5f16SSerge E. Hallyn 
310d09ca739SEric Paris 	return security_inode_permission(inode, mask);
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds 
313f4d6ff89SAl Viro /**
314f4d6ff89SAl Viro  * exec_permission  -  check for right to do lookups in a given directory
315f4d6ff89SAl Viro  * @inode:	inode to check permission on
316eecdd358SAl Viro  * @mask:	MAY_EXEC and possibly MAY_NOT_BLOCK flags.
317f4d6ff89SAl Viro  *
318f4d6ff89SAl Viro  * Short-cut version of inode_permission(), for calling on directories
319f4d6ff89SAl Viro  * during pathname resolution.  Combines parts of inode_permission()
320f4d6ff89SAl Viro  * and generic_permission(), and tests ONLY for MAY_EXEC permission.
321f4d6ff89SAl Viro  *
322f4d6ff89SAl Viro  * If appropriate, check DAC only.  If not appropriate, or
323f4d6ff89SAl Viro  * short-cut DAC fails, then call ->permission() to do more
324f4d6ff89SAl Viro  * complete permission check.
325f4d6ff89SAl Viro  */
326eecdd358SAl Viro static inline int exec_permission(struct inode *inode, int mask)
327f4d6ff89SAl Viro {
328f4d6ff89SAl Viro 	int ret;
329f4d6ff89SAl Viro 	struct user_namespace *ns = inode_userns(inode);
330f4d6ff89SAl Viro 
331f4d6ff89SAl Viro 	if (inode->i_op->permission) {
33210556cb2SAl Viro 		ret = inode->i_op->permission(inode, mask);
333f4d6ff89SAl Viro 		if (likely(!ret))
334f4d6ff89SAl Viro 			goto ok;
335f4d6ff89SAl Viro 	} else {
3367e40145eSAl Viro 		ret = acl_permission_check(inode, mask);
337f4d6ff89SAl Viro 		if (likely(!ret))
338f4d6ff89SAl Viro 			goto ok;
339f4d6ff89SAl Viro 		if (ret != -EACCES)
340f4d6ff89SAl Viro 			return ret;
341f4d6ff89SAl Viro 		if (ns_capable(ns, CAP_DAC_OVERRIDE) ||
342f4d6ff89SAl Viro 				ns_capable(ns, CAP_DAC_READ_SEARCH))
343f4d6ff89SAl Viro 			goto ok;
344f4d6ff89SAl Viro 	}
345f4d6ff89SAl Viro 	return ret;
346f4d6ff89SAl Viro ok:
347eecdd358SAl Viro 	return security_inode_permission(inode, mask);
348f4d6ff89SAl Viro }
349f4d6ff89SAl Viro 
3501d957f9bSJan Blunck /**
3515dd784d0SJan Blunck  * path_get - get a reference to a path
3525dd784d0SJan Blunck  * @path: path to get the reference to
3535dd784d0SJan Blunck  *
3545dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3555dd784d0SJan Blunck  */
3565dd784d0SJan Blunck void path_get(struct path *path)
3575dd784d0SJan Blunck {
3585dd784d0SJan Blunck 	mntget(path->mnt);
3595dd784d0SJan Blunck 	dget(path->dentry);
3605dd784d0SJan Blunck }
3615dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
3625dd784d0SJan Blunck 
3635dd784d0SJan Blunck /**
3641d957f9bSJan Blunck  * path_put - put a reference to a path
3651d957f9bSJan Blunck  * @path: path to put the reference to
3661d957f9bSJan Blunck  *
3671d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
3681d957f9bSJan Blunck  */
3691d957f9bSJan Blunck void path_put(struct path *path)
3701da177e4SLinus Torvalds {
3711d957f9bSJan Blunck 	dput(path->dentry);
3721d957f9bSJan Blunck 	mntput(path->mnt);
3731da177e4SLinus Torvalds }
3741d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
3751da177e4SLinus Torvalds 
37619660af7SAl Viro /*
37731e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
37819660af7SAl Viro  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
37919660af7SAl Viro  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
38019660af7SAl Viro  * normal reference counts on dentries and vfsmounts to transition to rcu-walk
38119660af7SAl Viro  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
38219660af7SAl Viro  * got stuck, so ref-walk may continue from there. If this is not successful
38319660af7SAl Viro  * (eg. a seqcount has changed), then failure is returned and it's up to caller
38419660af7SAl Viro  * to restart the path walk from the beginning in ref-walk mode.
38531e6b01fSNick Piggin  */
38631e6b01fSNick Piggin 
38731e6b01fSNick Piggin /**
38819660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
38919660af7SAl Viro  * @nd: nameidata pathwalk data
39019660af7SAl Viro  * @dentry: child of nd->path.dentry or NULL
39139191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
39231e6b01fSNick Piggin  *
39319660af7SAl Viro  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
39419660af7SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
39519660af7SAl Viro  * @nd or NULL.  Must be called from rcu-walk context.
39631e6b01fSNick Piggin  */
39719660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
39831e6b01fSNick Piggin {
39931e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
40031e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
4015b6ca027SAl Viro 	int want_root = 0;
40231e6b01fSNick Piggin 
40331e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
4045b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
4055b6ca027SAl Viro 		want_root = 1;
40631e6b01fSNick Piggin 		spin_lock(&fs->lock);
40731e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
40831e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
40931e6b01fSNick Piggin 			goto err_root;
41031e6b01fSNick Piggin 	}
41131e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
41219660af7SAl Viro 	if (!dentry) {
41319660af7SAl Viro 		if (!__d_rcu_to_refcount(parent, nd->seq))
41419660af7SAl Viro 			goto err_parent;
41519660af7SAl Viro 		BUG_ON(nd->inode != parent->d_inode);
41619660af7SAl Viro 	} else {
41794c0d4ecSAl Viro 		if (dentry->d_parent != parent)
41894c0d4ecSAl Viro 			goto err_parent;
41931e6b01fSNick Piggin 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
42031e6b01fSNick Piggin 		if (!__d_rcu_to_refcount(dentry, nd->seq))
42119660af7SAl Viro 			goto err_child;
42231e6b01fSNick Piggin 		/*
42319660af7SAl Viro 		 * If the sequence check on the child dentry passed, then
42419660af7SAl Viro 		 * the child has not been removed from its parent. This
42519660af7SAl Viro 		 * means the parent dentry must be valid and able to take
42619660af7SAl Viro 		 * a reference at this point.
42731e6b01fSNick Piggin 		 */
42831e6b01fSNick Piggin 		BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
42931e6b01fSNick Piggin 		BUG_ON(!parent->d_count);
43031e6b01fSNick Piggin 		parent->d_count++;
43131e6b01fSNick Piggin 		spin_unlock(&dentry->d_lock);
43219660af7SAl Viro 	}
43331e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
4345b6ca027SAl Viro 	if (want_root) {
43531e6b01fSNick Piggin 		path_get(&nd->root);
43631e6b01fSNick Piggin 		spin_unlock(&fs->lock);
43731e6b01fSNick Piggin 	}
43831e6b01fSNick Piggin 	mntget(nd->path.mnt);
43931e6b01fSNick Piggin 
44031e6b01fSNick Piggin 	rcu_read_unlock();
44131e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
44231e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
44331e6b01fSNick Piggin 	return 0;
44419660af7SAl Viro 
44519660af7SAl Viro err_child:
44631e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
44719660af7SAl Viro err_parent:
44831e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
44931e6b01fSNick Piggin err_root:
4505b6ca027SAl Viro 	if (want_root)
45131e6b01fSNick Piggin 		spin_unlock(&fs->lock);
45231e6b01fSNick Piggin 	return -ECHILD;
45331e6b01fSNick Piggin }
45431e6b01fSNick Piggin 
45531e6b01fSNick Piggin /**
456834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
457834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
458834f2a4aSTrond Myklebust  */
459834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
460834f2a4aSTrond Myklebust {
4612dab5974SLinus Torvalds 	struct file *file = nd->intent.open.file;
4622dab5974SLinus Torvalds 
4632dab5974SLinus Torvalds 	if (file && !IS_ERR(file)) {
4642dab5974SLinus Torvalds 		if (file->f_path.dentry == NULL)
4652dab5974SLinus Torvalds 			put_filp(file);
466834f2a4aSTrond Myklebust 		else
4672dab5974SLinus Torvalds 			fput(file);
4682dab5974SLinus Torvalds 	}
469834f2a4aSTrond Myklebust }
470834f2a4aSTrond Myklebust 
471f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
47234286d66SNick Piggin {
473f60aef7eSAl Viro 	return dentry->d_op->d_revalidate(dentry, nd);
47434286d66SNick Piggin }
47534286d66SNick Piggin 
476f5e1c1c1SAl Viro static struct dentry *
477bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd)
478bcdc5e01SIan Kent {
479f5e1c1c1SAl Viro 	int status = d_revalidate(dentry, nd);
480bcdc5e01SIan Kent 	if (unlikely(status <= 0)) {
481bcdc5e01SIan Kent 		/*
482bcdc5e01SIan Kent 		 * The dentry failed validation.
483bcdc5e01SIan Kent 		 * If d_revalidate returned 0 attempt to invalidate
484bcdc5e01SIan Kent 		 * the dentry otherwise d_revalidate is asking us
485bcdc5e01SIan Kent 		 * to return a fail status.
486bcdc5e01SIan Kent 		 */
48734286d66SNick Piggin 		if (status < 0) {
48834286d66SNick Piggin 			dput(dentry);
48934286d66SNick Piggin 			dentry = ERR_PTR(status);
490f5e1c1c1SAl Viro 		} else if (!d_invalidate(dentry)) {
491bcdc5e01SIan Kent 			dput(dentry);
492bcdc5e01SIan Kent 			dentry = NULL;
493bcdc5e01SIan Kent 		}
494bcdc5e01SIan Kent 	}
495f5e1c1c1SAl Viro 	return dentry;
496f5e1c1c1SAl Viro }
497f5e1c1c1SAl Viro 
4989f1fafeeSAl Viro /**
4999f1fafeeSAl Viro  * complete_walk - successful completion of path walk
5009f1fafeeSAl Viro  * @nd:  pointer nameidata
50139159de2SJeff Layton  *
5029f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
5039f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
5049f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
5059f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
5069f1fafeeSAl Viro  * need to drop nd->path.
50739159de2SJeff Layton  */
5089f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
50939159de2SJeff Layton {
51016c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
51139159de2SJeff Layton 	int status;
51239159de2SJeff Layton 
5139f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
5149f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
5159f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
5169f1fafeeSAl Viro 			nd->root.mnt = NULL;
5179f1fafeeSAl Viro 		spin_lock(&dentry->d_lock);
5189f1fafeeSAl Viro 		if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
5199f1fafeeSAl Viro 			spin_unlock(&dentry->d_lock);
5209f1fafeeSAl Viro 			rcu_read_unlock();
5219f1fafeeSAl Viro 			br_read_unlock(vfsmount_lock);
5229f1fafeeSAl Viro 			return -ECHILD;
5239f1fafeeSAl Viro 		}
5249f1fafeeSAl Viro 		BUG_ON(nd->inode != dentry->d_inode);
5259f1fafeeSAl Viro 		spin_unlock(&dentry->d_lock);
5269f1fafeeSAl Viro 		mntget(nd->path.mnt);
5279f1fafeeSAl Viro 		rcu_read_unlock();
5289f1fafeeSAl Viro 		br_read_unlock(vfsmount_lock);
5299f1fafeeSAl Viro 	}
5309f1fafeeSAl Viro 
53116c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
53239159de2SJeff Layton 		return 0;
53339159de2SJeff Layton 
53416c2cd71SAl Viro 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
53516c2cd71SAl Viro 		return 0;
53616c2cd71SAl Viro 
53716c2cd71SAl Viro 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
53816c2cd71SAl Viro 		return 0;
53916c2cd71SAl Viro 
54016c2cd71SAl Viro 	/* Note: we do not d_invalidate() */
54134286d66SNick Piggin 	status = d_revalidate(dentry, nd);
54239159de2SJeff Layton 	if (status > 0)
54339159de2SJeff Layton 		return 0;
54439159de2SJeff Layton 
54516c2cd71SAl Viro 	if (!status)
54639159de2SJeff Layton 		status = -ESTALE;
54716c2cd71SAl Viro 
5489f1fafeeSAl Viro 	path_put(&nd->path);
54939159de2SJeff Layton 	return status;
55039159de2SJeff Layton }
55139159de2SJeff Layton 
5522a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
5532a737871SAl Viro {
554f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
555f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
5562a737871SAl Viro }
5572a737871SAl Viro 
5586de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
5596de88d72SAl Viro 
56031e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
56131e6b01fSNick Piggin {
56231e6b01fSNick Piggin 	if (!nd->root.mnt) {
56331e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
564c28cc364SNick Piggin 		unsigned seq;
565c28cc364SNick Piggin 
566c28cc364SNick Piggin 		do {
567c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
56831e6b01fSNick Piggin 			nd->root = fs->root;
569c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
570c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
57131e6b01fSNick Piggin 	}
57231e6b01fSNick Piggin }
57331e6b01fSNick Piggin 
574f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
5751da177e4SLinus Torvalds {
57631e6b01fSNick Piggin 	int ret;
57731e6b01fSNick Piggin 
5781da177e4SLinus Torvalds 	if (IS_ERR(link))
5791da177e4SLinus Torvalds 		goto fail;
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	if (*link == '/') {
5822a737871SAl Viro 		set_root(nd);
5831d957f9bSJan Blunck 		path_put(&nd->path);
5842a737871SAl Viro 		nd->path = nd->root;
5852a737871SAl Viro 		path_get(&nd->root);
58616c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
5871da177e4SLinus Torvalds 	}
58831e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
589b4091d5fSChristoph Hellwig 
59031e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
59131e6b01fSNick Piggin 	return ret;
5921da177e4SLinus Torvalds fail:
5931d957f9bSJan Blunck 	path_put(&nd->path);
5941da177e4SLinus Torvalds 	return PTR_ERR(link);
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds 
5971d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
598051d3812SIan Kent {
599051d3812SIan Kent 	dput(path->dentry);
6004ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
601051d3812SIan Kent 		mntput(path->mnt);
602051d3812SIan Kent }
603051d3812SIan Kent 
6047b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
6057b9337aaSNick Piggin 					struct nameidata *nd)
606051d3812SIan Kent {
60731e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
6084ac91378SJan Blunck 		dput(nd->path.dentry);
60931e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
6104ac91378SJan Blunck 			mntput(nd->path.mnt);
6119a229683SHuang Shijie 	}
61231e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
6134ac91378SJan Blunck 	nd->path.dentry = path->dentry;
614051d3812SIan Kent }
615051d3812SIan Kent 
616574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
617574197e0SAl Viro {
618574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
619574197e0SAl Viro 	if (!IS_ERR(cookie) && inode->i_op->put_link)
620574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
621574197e0SAl Viro 	path_put(link);
622574197e0SAl Viro }
623574197e0SAl Viro 
624def4af30SAl Viro static __always_inline int
625574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
6261da177e4SLinus Torvalds {
6271da177e4SLinus Torvalds 	int error;
6287b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
6291da177e4SLinus Torvalds 
630844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
631844a3917SAl Viro 
6320e794589SAl Viro 	if (link->mnt == nd->path.mnt)
6330e794589SAl Viro 		mntget(link->mnt);
6340e794589SAl Viro 
635574197e0SAl Viro 	if (unlikely(current->total_link_count >= 40)) {
636574197e0SAl Viro 		*p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
637574197e0SAl Viro 		path_put(&nd->path);
638574197e0SAl Viro 		return -ELOOP;
639574197e0SAl Viro 	}
640574197e0SAl Viro 	cond_resched();
641574197e0SAl Viro 	current->total_link_count++;
642574197e0SAl Viro 
6437b9337aaSNick Piggin 	touch_atime(link->mnt, dentry);
6441da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
645cd4e91d3SAl Viro 
64636f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
64736f3b4f6SAl Viro 	if (error) {
64836f3b4f6SAl Viro 		*p = ERR_PTR(error); /* no ->put_link(), please */
64936f3b4f6SAl Viro 		path_put(&nd->path);
65036f3b4f6SAl Viro 		return error;
65136f3b4f6SAl Viro 	}
65236f3b4f6SAl Viro 
65386acdca1SAl Viro 	nd->last_type = LAST_BIND;
654def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
655def4af30SAl Viro 	error = PTR_ERR(*p);
656def4af30SAl Viro 	if (!IS_ERR(*p)) {
6571da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
658cc314eefSLinus Torvalds 		error = 0;
6591da177e4SLinus Torvalds 		if (s)
6601da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
661bcda7652SAl Viro 		else if (nd->last_type == LAST_BIND) {
66216c2cd71SAl Viro 			nd->flags |= LOOKUP_JUMPED;
663b21041d0SAl Viro 			nd->inode = nd->path.dentry->d_inode;
664b21041d0SAl Viro 			if (nd->inode->i_op->follow_link) {
665bcda7652SAl Viro 				/* stepped on a _really_ weird one */
666bcda7652SAl Viro 				path_put(&nd->path);
667bcda7652SAl Viro 				error = -ELOOP;
668bcda7652SAl Viro 			}
669bcda7652SAl Viro 		}
6701da177e4SLinus Torvalds 	}
6711da177e4SLinus Torvalds 	return error;
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds 
67431e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
67531e6b01fSNick Piggin {
67631e6b01fSNick Piggin 	struct vfsmount *parent;
67731e6b01fSNick Piggin 	struct dentry *mountpoint;
67831e6b01fSNick Piggin 
67931e6b01fSNick Piggin 	parent = path->mnt->mnt_parent;
68031e6b01fSNick Piggin 	if (parent == path->mnt)
68131e6b01fSNick Piggin 		return 0;
68231e6b01fSNick Piggin 	mountpoint = path->mnt->mnt_mountpoint;
68331e6b01fSNick Piggin 	path->dentry = mountpoint;
68431e6b01fSNick Piggin 	path->mnt = parent;
68531e6b01fSNick Piggin 	return 1;
68631e6b01fSNick Piggin }
68731e6b01fSNick Piggin 
688bab77ebfSAl Viro int follow_up(struct path *path)
6891da177e4SLinus Torvalds {
6901da177e4SLinus Torvalds 	struct vfsmount *parent;
6911da177e4SLinus Torvalds 	struct dentry *mountpoint;
69299b7db7bSNick Piggin 
69399b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
694bab77ebfSAl Viro 	parent = path->mnt->mnt_parent;
695bab77ebfSAl Viro 	if (parent == path->mnt) {
69699b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
6971da177e4SLinus Torvalds 		return 0;
6981da177e4SLinus Torvalds 	}
6991da177e4SLinus Torvalds 	mntget(parent);
700bab77ebfSAl Viro 	mountpoint = dget(path->mnt->mnt_mountpoint);
70199b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
702bab77ebfSAl Viro 	dput(path->dentry);
703bab77ebfSAl Viro 	path->dentry = mountpoint;
704bab77ebfSAl Viro 	mntput(path->mnt);
705bab77ebfSAl Viro 	path->mnt = parent;
7061da177e4SLinus Torvalds 	return 1;
7071da177e4SLinus Torvalds }
7081da177e4SLinus Torvalds 
709b5c84bf6SNick Piggin /*
7109875cf80SDavid Howells  * Perform an automount
7119875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
7129875cf80SDavid Howells  *   were called with.
7131da177e4SLinus Torvalds  */
7149875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
7159875cf80SDavid Howells 			    bool *need_mntput)
71631e6b01fSNick Piggin {
7179875cf80SDavid Howells 	struct vfsmount *mnt;
718ea5b778aSDavid Howells 	int err;
7199875cf80SDavid Howells 
7209875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
7219875cf80SDavid Howells 		return -EREMOTE;
7229875cf80SDavid Howells 
7236f45b656SDavid Howells 	/* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
7246f45b656SDavid Howells 	 * and this is the terminal part of the path.
7256f45b656SDavid Howells 	 */
7266f45b656SDavid Howells 	if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
7276f45b656SDavid Howells 		return -EISDIR; /* we actually want to stop here */
7286f45b656SDavid Howells 
7299875cf80SDavid Howells 	/* We want to mount if someone is trying to open/create a file of any
7309875cf80SDavid Howells 	 * type under the mountpoint, wants to traverse through the mountpoint
7319875cf80SDavid Howells 	 * or wants to open the mounted directory.
7329875cf80SDavid Howells 	 *
7339875cf80SDavid Howells 	 * We don't want to mount if someone's just doing a stat and they've
7349875cf80SDavid Howells 	 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
7359875cf80SDavid Howells 	 * appended a '/' to the name.
7369875cf80SDavid Howells 	 */
7379875cf80SDavid Howells 	if (!(flags & LOOKUP_FOLLOW) &&
7389875cf80SDavid Howells 	    !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
7399875cf80SDavid Howells 		       LOOKUP_OPEN | LOOKUP_CREATE)))
7409875cf80SDavid Howells 		return -EISDIR;
7419875cf80SDavid Howells 
7429875cf80SDavid Howells 	current->total_link_count++;
7439875cf80SDavid Howells 	if (current->total_link_count >= 40)
7449875cf80SDavid Howells 		return -ELOOP;
7459875cf80SDavid Howells 
7469875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
7479875cf80SDavid Howells 	if (IS_ERR(mnt)) {
7489875cf80SDavid Howells 		/*
7499875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
7509875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
7519875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
7529875cf80SDavid Howells 		 *
7539875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
7549875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
7559875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
7569875cf80SDavid Howells 		 */
7579875cf80SDavid Howells 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
7589875cf80SDavid Howells 			return -EREMOTE;
7599875cf80SDavid Howells 		return PTR_ERR(mnt);
76031e6b01fSNick Piggin 	}
761ea5b778aSDavid Howells 
7629875cf80SDavid Howells 	if (!mnt) /* mount collision */
7639875cf80SDavid Howells 		return 0;
7649875cf80SDavid Howells 
7658aef1884SAl Viro 	if (!*need_mntput) {
7668aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
7678aef1884SAl Viro 		mntget(path->mnt);
7688aef1884SAl Viro 		*need_mntput = true;
7698aef1884SAl Viro 	}
77019a167afSAl Viro 	err = finish_automount(mnt, path);
771ea5b778aSDavid Howells 
772ea5b778aSDavid Howells 	switch (err) {
773ea5b778aSDavid Howells 	case -EBUSY:
774ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
77519a167afSAl Viro 		return 0;
776ea5b778aSDavid Howells 	case 0:
7778aef1884SAl Viro 		path_put(path);
7789875cf80SDavid Howells 		path->mnt = mnt;
7799875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
7809875cf80SDavid Howells 		return 0;
78119a167afSAl Viro 	default:
78219a167afSAl Viro 		return err;
7839875cf80SDavid Howells 	}
78419a167afSAl Viro 
785ea5b778aSDavid Howells }
7869875cf80SDavid Howells 
7879875cf80SDavid Howells /*
7889875cf80SDavid Howells  * Handle a dentry that is managed in some way.
789cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
7909875cf80SDavid Howells  * - Flagged as mountpoint
7919875cf80SDavid Howells  * - Flagged as automount point
7929875cf80SDavid Howells  *
7939875cf80SDavid Howells  * This may only be called in refwalk mode.
7949875cf80SDavid Howells  *
7959875cf80SDavid Howells  * Serialization is taken care of in namespace.c
7969875cf80SDavid Howells  */
7979875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
7989875cf80SDavid Howells {
7998aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
8009875cf80SDavid Howells 	unsigned managed;
8019875cf80SDavid Howells 	bool need_mntput = false;
8028aef1884SAl Viro 	int ret = 0;
8039875cf80SDavid Howells 
8049875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
8059875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
8069875cf80SDavid Howells 	 * the components of that value change under us */
8079875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
8089875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
8099875cf80SDavid Howells 	       unlikely(managed != 0)) {
810cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
811cc53ce53SDavid Howells 		 * being held. */
812cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
813cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
814cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
8151aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
816cc53ce53SDavid Howells 			if (ret < 0)
8178aef1884SAl Viro 				break;
818cc53ce53SDavid Howells 		}
819cc53ce53SDavid Howells 
8209875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
8219875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
8229875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
8239875cf80SDavid Howells 			if (mounted) {
8249875cf80SDavid Howells 				dput(path->dentry);
8259875cf80SDavid Howells 				if (need_mntput)
826463ffb2eSAl Viro 					mntput(path->mnt);
827463ffb2eSAl Viro 				path->mnt = mounted;
828463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
8299875cf80SDavid Howells 				need_mntput = true;
8309875cf80SDavid Howells 				continue;
831463ffb2eSAl Viro 			}
832463ffb2eSAl Viro 
8339875cf80SDavid Howells 			/* Something is mounted on this dentry in another
8349875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
8359875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
8369875cf80SDavid Howells 			 * vfsmount_lock */
8371da177e4SLinus Torvalds 		}
8389875cf80SDavid Howells 
8399875cf80SDavid Howells 		/* Handle an automount point */
8409875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
8419875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
8429875cf80SDavid Howells 			if (ret < 0)
8438aef1884SAl Viro 				break;
8449875cf80SDavid Howells 			continue;
8459875cf80SDavid Howells 		}
8469875cf80SDavid Howells 
8479875cf80SDavid Howells 		/* We didn't change the current path point */
8489875cf80SDavid Howells 		break;
8499875cf80SDavid Howells 	}
8508aef1884SAl Viro 
8518aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
8528aef1884SAl Viro 		mntput(path->mnt);
8538aef1884SAl Viro 	if (ret == -EISDIR)
8548aef1884SAl Viro 		ret = 0;
8558aef1884SAl Viro 	return ret;
8561da177e4SLinus Torvalds }
8571da177e4SLinus Torvalds 
858cc53ce53SDavid Howells int follow_down_one(struct path *path)
8591da177e4SLinus Torvalds {
8601da177e4SLinus Torvalds 	struct vfsmount *mounted;
8611da177e4SLinus Torvalds 
8621c755af4SAl Viro 	mounted = lookup_mnt(path);
8631da177e4SLinus Torvalds 	if (mounted) {
8649393bd07SAl Viro 		dput(path->dentry);
8659393bd07SAl Viro 		mntput(path->mnt);
8669393bd07SAl Viro 		path->mnt = mounted;
8679393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
8681da177e4SLinus Torvalds 		return 1;
8691da177e4SLinus Torvalds 	}
8701da177e4SLinus Torvalds 	return 0;
8711da177e4SLinus Torvalds }
8721da177e4SLinus Torvalds 
87362a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
87462a7375eSIan Kent {
87562a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
87662a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
87762a7375eSIan Kent }
87862a7375eSIan Kent 
8799875cf80SDavid Howells /*
880287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
881287548e4SAl Viro  * we meet a managed dentry that would need blocking.
8829875cf80SDavid Howells  */
8839875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
884287548e4SAl Viro 			       struct inode **inode)
8859875cf80SDavid Howells {
88662a7375eSIan Kent 	for (;;) {
8879875cf80SDavid Howells 		struct vfsmount *mounted;
88862a7375eSIan Kent 		/*
88962a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
89062a7375eSIan Kent 		 * that wants to block transit.
89162a7375eSIan Kent 		 */
892287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
893ab90911fSDavid Howells 			return false;
89462a7375eSIan Kent 
89562a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
89662a7375eSIan Kent 			break;
89762a7375eSIan Kent 
8989875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
8999875cf80SDavid Howells 		if (!mounted)
9009875cf80SDavid Howells 			break;
9019875cf80SDavid Howells 		path->mnt = mounted;
9029875cf80SDavid Howells 		path->dentry = mounted->mnt_root;
9039875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
90459430262SLinus Torvalds 		/*
90559430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
90659430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
90759430262SLinus Torvalds 		 * because a mount-point is always pinned.
90859430262SLinus Torvalds 		 */
90959430262SLinus Torvalds 		*inode = path->dentry->d_inode;
9109875cf80SDavid Howells 	}
9119875cf80SDavid Howells 	return true;
9129875cf80SDavid Howells }
9139875cf80SDavid Howells 
914dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
915287548e4SAl Viro {
916dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
917287548e4SAl Viro 		struct vfsmount *mounted;
918dea39376SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
919287548e4SAl Viro 		if (!mounted)
920287548e4SAl Viro 			break;
921dea39376SAl Viro 		nd->path.mnt = mounted;
922dea39376SAl Viro 		nd->path.dentry = mounted->mnt_root;
923dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
924287548e4SAl Viro 	}
925287548e4SAl Viro }
926287548e4SAl Viro 
92731e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
92831e6b01fSNick Piggin {
92931e6b01fSNick Piggin 	set_root_rcu(nd);
93031e6b01fSNick Piggin 
93131e6b01fSNick Piggin 	while (1) {
93231e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
93331e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
93431e6b01fSNick Piggin 			break;
93531e6b01fSNick Piggin 		}
93631e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
93731e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
93831e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
93931e6b01fSNick Piggin 			unsigned seq;
94031e6b01fSNick Piggin 
94131e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
94231e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
943ef7562d5SAl Viro 				goto failed;
94431e6b01fSNick Piggin 			nd->path.dentry = parent;
94531e6b01fSNick Piggin 			nd->seq = seq;
94631e6b01fSNick Piggin 			break;
94731e6b01fSNick Piggin 		}
94831e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
94931e6b01fSNick Piggin 			break;
95031e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
95131e6b01fSNick Piggin 	}
952dea39376SAl Viro 	follow_mount_rcu(nd);
953dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
95431e6b01fSNick Piggin 	return 0;
955ef7562d5SAl Viro 
956ef7562d5SAl Viro failed:
957ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
9585b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
959ef7562d5SAl Viro 		nd->root.mnt = NULL;
960ef7562d5SAl Viro 	rcu_read_unlock();
961ef7562d5SAl Viro 	br_read_unlock(vfsmount_lock);
962ef7562d5SAl Viro 	return -ECHILD;
96331e6b01fSNick Piggin }
96431e6b01fSNick Piggin 
9659875cf80SDavid Howells /*
966cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
967cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
968cc53ce53SDavid Howells  * caller is permitted to proceed or not.
969cc53ce53SDavid Howells  */
9707cc90cc3SAl Viro int follow_down(struct path *path)
971cc53ce53SDavid Howells {
972cc53ce53SDavid Howells 	unsigned managed;
973cc53ce53SDavid Howells 	int ret;
974cc53ce53SDavid Howells 
975cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
976cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
977cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
978cc53ce53SDavid Howells 		 * being held.
979cc53ce53SDavid Howells 		 *
980cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
981cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
982cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
983cc53ce53SDavid Howells 		 * superstructure.
984cc53ce53SDavid Howells 		 *
985cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
986cc53ce53SDavid Howells 		 */
987cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
988cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
989cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
990ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
9911aed3e42SAl Viro 				path->dentry, false);
992cc53ce53SDavid Howells 			if (ret < 0)
993cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
994cc53ce53SDavid Howells 		}
995cc53ce53SDavid Howells 
996cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
997cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
998cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
999cc53ce53SDavid Howells 			if (!mounted)
1000cc53ce53SDavid Howells 				break;
1001cc53ce53SDavid Howells 			dput(path->dentry);
1002cc53ce53SDavid Howells 			mntput(path->mnt);
1003cc53ce53SDavid Howells 			path->mnt = mounted;
1004cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1005cc53ce53SDavid Howells 			continue;
1006cc53ce53SDavid Howells 		}
1007cc53ce53SDavid Howells 
1008cc53ce53SDavid Howells 		/* Don't handle automount points here */
1009cc53ce53SDavid Howells 		break;
1010cc53ce53SDavid Howells 	}
1011cc53ce53SDavid Howells 	return 0;
1012cc53ce53SDavid Howells }
1013cc53ce53SDavid Howells 
1014cc53ce53SDavid Howells /*
10159875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
10169875cf80SDavid Howells  */
10179875cf80SDavid Howells static void follow_mount(struct path *path)
10189875cf80SDavid Howells {
10199875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
10209875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
10219875cf80SDavid Howells 		if (!mounted)
10229875cf80SDavid Howells 			break;
10239875cf80SDavid Howells 		dput(path->dentry);
10249875cf80SDavid Howells 		mntput(path->mnt);
10259875cf80SDavid Howells 		path->mnt = mounted;
10269875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
10279875cf80SDavid Howells 	}
10289875cf80SDavid Howells }
10299875cf80SDavid Howells 
103031e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
10311da177e4SLinus Torvalds {
10322a737871SAl Viro 	set_root(nd);
1033e518ddb7SAndreas Mohr 
10341da177e4SLinus Torvalds 	while(1) {
10354ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
10361da177e4SLinus Torvalds 
10372a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
10382a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
10391da177e4SLinus Torvalds 			break;
10401da177e4SLinus Torvalds 		}
10414ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
10423088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
10433088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
10441da177e4SLinus Torvalds 			dput(old);
10451da177e4SLinus Torvalds 			break;
10461da177e4SLinus Torvalds 		}
10473088dd70SAl Viro 		if (!follow_up(&nd->path))
10481da177e4SLinus Torvalds 			break;
10491da177e4SLinus Torvalds 	}
105079ed0226SAl Viro 	follow_mount(&nd->path);
105131e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
10521da177e4SLinus Torvalds }
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds /*
1055baa03890SNick Piggin  * Allocate a dentry with name and parent, and perform a parent
1056baa03890SNick Piggin  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1057baa03890SNick Piggin  * on error. parent->d_inode->i_mutex must be held. d_lookup must
1058baa03890SNick Piggin  * have verified that no child exists while under i_mutex.
1059baa03890SNick Piggin  */
1060baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1061baa03890SNick Piggin 				struct qstr *name, struct nameidata *nd)
1062baa03890SNick Piggin {
1063baa03890SNick Piggin 	struct inode *inode = parent->d_inode;
1064baa03890SNick Piggin 	struct dentry *dentry;
1065baa03890SNick Piggin 	struct dentry *old;
1066baa03890SNick Piggin 
1067baa03890SNick Piggin 	/* Don't create child dentry for a dead directory. */
1068baa03890SNick Piggin 	if (unlikely(IS_DEADDIR(inode)))
1069baa03890SNick Piggin 		return ERR_PTR(-ENOENT);
1070baa03890SNick Piggin 
1071baa03890SNick Piggin 	dentry = d_alloc(parent, name);
1072baa03890SNick Piggin 	if (unlikely(!dentry))
1073baa03890SNick Piggin 		return ERR_PTR(-ENOMEM);
1074baa03890SNick Piggin 
1075baa03890SNick Piggin 	old = inode->i_op->lookup(inode, dentry, nd);
1076baa03890SNick Piggin 	if (unlikely(old)) {
1077baa03890SNick Piggin 		dput(dentry);
1078baa03890SNick Piggin 		dentry = old;
1079baa03890SNick Piggin 	}
1080baa03890SNick Piggin 	return dentry;
1081baa03890SNick Piggin }
1082baa03890SNick Piggin 
1083baa03890SNick Piggin /*
108444396f4bSJosef Bacik  * We already have a dentry, but require a lookup to be performed on the parent
108544396f4bSJosef Bacik  * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
108644396f4bSJosef Bacik  * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
108744396f4bSJosef Bacik  * child exists while under i_mutex.
108844396f4bSJosef Bacik  */
108944396f4bSJosef Bacik static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
109044396f4bSJosef Bacik 				     struct nameidata *nd)
109144396f4bSJosef Bacik {
109244396f4bSJosef Bacik 	struct inode *inode = parent->d_inode;
109344396f4bSJosef Bacik 	struct dentry *old;
109444396f4bSJosef Bacik 
109544396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
109644396f4bSJosef Bacik 	if (unlikely(IS_DEADDIR(inode)))
109744396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
109844396f4bSJosef Bacik 
109944396f4bSJosef Bacik 	old = inode->i_op->lookup(inode, dentry, nd);
110044396f4bSJosef Bacik 	if (unlikely(old)) {
110144396f4bSJosef Bacik 		dput(dentry);
110244396f4bSJosef Bacik 		dentry = old;
110344396f4bSJosef Bacik 	}
110444396f4bSJosef Bacik 	return dentry;
110544396f4bSJosef Bacik }
110644396f4bSJosef Bacik 
110744396f4bSJosef Bacik /*
11081da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
11091da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
11101da177e4SLinus Torvalds  *  It _is_ time-critical.
11111da177e4SLinus Torvalds  */
11121da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
111331e6b01fSNick Piggin 			struct path *path, struct inode **inode)
11141da177e4SLinus Torvalds {
11154ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
111631e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
11175a18fff2SAl Viro 	int need_reval = 1;
11185a18fff2SAl Viro 	int status = 1;
11199875cf80SDavid Howells 	int err;
11209875cf80SDavid Howells 
11213cac260aSAl Viro 	/*
1122b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1123b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1124b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1125b04f784eSNick Piggin 	 */
112631e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
112731e6b01fSNick Piggin 		unsigned seq;
112831e6b01fSNick Piggin 		*inode = nd->inode;
112931e6b01fSNick Piggin 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
11305a18fff2SAl Viro 		if (!dentry)
11315a18fff2SAl Viro 			goto unlazy;
11325a18fff2SAl Viro 
113331e6b01fSNick Piggin 		/* Memory barrier in read_seqcount_begin of child is enough */
113431e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
113531e6b01fSNick Piggin 			return -ECHILD;
113631e6b01fSNick Piggin 		nd->seq = seq;
11375a18fff2SAl Viro 
113824643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
11395a18fff2SAl Viro 			status = d_revalidate(dentry, nd);
11405a18fff2SAl Viro 			if (unlikely(status <= 0)) {
11415a18fff2SAl Viro 				if (status != -ECHILD)
11425a18fff2SAl Viro 					need_reval = 0;
11435a18fff2SAl Viro 				goto unlazy;
11445a18fff2SAl Viro 			}
114524643087SAl Viro 		}
114644396f4bSJosef Bacik 		if (unlikely(d_need_lookup(dentry)))
114744396f4bSJosef Bacik 			goto unlazy;
114831e6b01fSNick Piggin 		path->mnt = mnt;
114931e6b01fSNick Piggin 		path->dentry = dentry;
1150d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1151d6e9bd25SAl Viro 			goto unlazy;
1152d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1153d6e9bd25SAl Viro 			goto unlazy;
11549875cf80SDavid Howells 		return 0;
11555a18fff2SAl Viro unlazy:
115619660af7SAl Viro 		if (unlazy_walk(nd, dentry))
11575a18fff2SAl Viro 			return -ECHILD;
11585a18fff2SAl Viro 	} else {
115931e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
116024643087SAl Viro 	}
11615a18fff2SAl Viro 
116244396f4bSJosef Bacik 	if (dentry && unlikely(d_need_lookup(dentry))) {
116344396f4bSJosef Bacik 		dput(dentry);
116444396f4bSJosef Bacik 		dentry = NULL;
116544396f4bSJosef Bacik 	}
11665a18fff2SAl Viro retry:
11675a18fff2SAl Viro 	if (unlikely(!dentry)) {
11685a18fff2SAl Viro 		struct inode *dir = parent->d_inode;
11695a18fff2SAl Viro 		BUG_ON(nd->inode != dir);
11705a18fff2SAl Viro 
11715a18fff2SAl Viro 		mutex_lock(&dir->i_mutex);
11725a18fff2SAl Viro 		dentry = d_lookup(parent, name);
11735a18fff2SAl Viro 		if (likely(!dentry)) {
11745a18fff2SAl Viro 			dentry = d_alloc_and_lookup(parent, name, nd);
11755a18fff2SAl Viro 			if (IS_ERR(dentry)) {
11765a18fff2SAl Viro 				mutex_unlock(&dir->i_mutex);
11775a18fff2SAl Viro 				return PTR_ERR(dentry);
11785a18fff2SAl Viro 			}
11795a18fff2SAl Viro 			/* known good */
11805a18fff2SAl Viro 			need_reval = 0;
11815a18fff2SAl Viro 			status = 1;
118244396f4bSJosef Bacik 		} else if (unlikely(d_need_lookup(dentry))) {
118344396f4bSJosef Bacik 			dentry = d_inode_lookup(parent, dentry, nd);
118444396f4bSJosef Bacik 			if (IS_ERR(dentry)) {
118544396f4bSJosef Bacik 				mutex_unlock(&dir->i_mutex);
118644396f4bSJosef Bacik 				return PTR_ERR(dentry);
118744396f4bSJosef Bacik 			}
118844396f4bSJosef Bacik 			/* known good */
118944396f4bSJosef Bacik 			need_reval = 0;
119044396f4bSJosef Bacik 			status = 1;
11915a18fff2SAl Viro 		}
11925a18fff2SAl Viro 		mutex_unlock(&dir->i_mutex);
11935a18fff2SAl Viro 	}
11945a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
11955a18fff2SAl Viro 		status = d_revalidate(dentry, nd);
11965a18fff2SAl Viro 	if (unlikely(status <= 0)) {
11975a18fff2SAl Viro 		if (status < 0) {
11985a18fff2SAl Viro 			dput(dentry);
11995a18fff2SAl Viro 			return status;
12005a18fff2SAl Viro 		}
12015a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
12025a18fff2SAl Viro 			dput(dentry);
12035a18fff2SAl Viro 			dentry = NULL;
12045a18fff2SAl Viro 			need_reval = 1;
12055a18fff2SAl Viro 			goto retry;
12065a18fff2SAl Viro 		}
12075a18fff2SAl Viro 	}
12085a18fff2SAl Viro 
12091da177e4SLinus Torvalds 	path->mnt = mnt;
12101da177e4SLinus Torvalds 	path->dentry = dentry;
12119875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
121289312214SIan Kent 	if (unlikely(err < 0)) {
121389312214SIan Kent 		path_put_conditional(path, nd);
12149875cf80SDavid Howells 		return err;
121589312214SIan Kent 	}
121631e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
12171da177e4SLinus Torvalds 	return 0;
12181da177e4SLinus Torvalds }
12191da177e4SLinus Torvalds 
122052094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
122152094c8aSAl Viro {
122252094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
1223eecdd358SAl Viro 		int err = exec_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
122452094c8aSAl Viro 		if (err != -ECHILD)
122552094c8aSAl Viro 			return err;
122619660af7SAl Viro 		if (unlazy_walk(nd, NULL))
122752094c8aSAl Viro 			return -ECHILD;
122852094c8aSAl Viro 	}
1229eecdd358SAl Viro 	return exec_permission(nd->inode, MAY_EXEC);
123052094c8aSAl Viro }
123152094c8aSAl Viro 
12329856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
12339856fa1bSAl Viro {
12349856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
12359856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
12369856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
12379856fa1bSAl Viro 				return -ECHILD;
12389856fa1bSAl Viro 		} else
12399856fa1bSAl Viro 			follow_dotdot(nd);
12409856fa1bSAl Viro 	}
12419856fa1bSAl Viro 	return 0;
12429856fa1bSAl Viro }
12439856fa1bSAl Viro 
1244951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1245951361f9SAl Viro {
1246951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1247951361f9SAl Viro 		path_put(&nd->path);
1248951361f9SAl Viro 	} else {
1249951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
12505b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1251951361f9SAl Viro 			nd->root.mnt = NULL;
1252951361f9SAl Viro 		rcu_read_unlock();
1253951361f9SAl Viro 		br_read_unlock(vfsmount_lock);
1254951361f9SAl Viro 	}
1255951361f9SAl Viro }
1256951361f9SAl Viro 
1257ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
1258ce57dfc1SAl Viro 		struct qstr *name, int type, int follow)
1259ce57dfc1SAl Viro {
1260ce57dfc1SAl Viro 	struct inode *inode;
1261ce57dfc1SAl Viro 	int err;
1262ce57dfc1SAl Viro 	/*
1263ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1264ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1265ce57dfc1SAl Viro 	 * parent relationships.
1266ce57dfc1SAl Viro 	 */
1267ce57dfc1SAl Viro 	if (unlikely(type != LAST_NORM))
1268ce57dfc1SAl Viro 		return handle_dots(nd, type);
1269ce57dfc1SAl Viro 	err = do_lookup(nd, name, path, &inode);
1270ce57dfc1SAl Viro 	if (unlikely(err)) {
1271ce57dfc1SAl Viro 		terminate_walk(nd);
1272ce57dfc1SAl Viro 		return err;
1273ce57dfc1SAl Viro 	}
1274ce57dfc1SAl Viro 	if (!inode) {
1275ce57dfc1SAl Viro 		path_to_nameidata(path, nd);
1276ce57dfc1SAl Viro 		terminate_walk(nd);
1277ce57dfc1SAl Viro 		return -ENOENT;
1278ce57dfc1SAl Viro 	}
1279ce57dfc1SAl Viro 	if (unlikely(inode->i_op->follow_link) && follow) {
128019660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
128119660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
128219660af7SAl Viro 				terminate_walk(nd);
1283ce57dfc1SAl Viro 				return -ECHILD;
128419660af7SAl Viro 			}
128519660af7SAl Viro 		}
1286ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1287ce57dfc1SAl Viro 		return 1;
1288ce57dfc1SAl Viro 	}
1289ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1290ce57dfc1SAl Viro 	nd->inode = inode;
1291ce57dfc1SAl Viro 	return 0;
1292ce57dfc1SAl Viro }
1293ce57dfc1SAl Viro 
12941da177e4SLinus Torvalds /*
1295b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1296b356379aSAl Viro  * limiting consecutive symlinks to 40.
1297b356379aSAl Viro  *
1298b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1299b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1300b356379aSAl Viro  */
1301b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1302b356379aSAl Viro {
1303b356379aSAl Viro 	int res;
1304b356379aSAl Viro 
1305b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1306b356379aSAl Viro 		path_put_conditional(path, nd);
1307b356379aSAl Viro 		path_put(&nd->path);
1308b356379aSAl Viro 		return -ELOOP;
1309b356379aSAl Viro 	}
13101a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1311b356379aSAl Viro 
1312b356379aSAl Viro 	nd->depth++;
1313b356379aSAl Viro 	current->link_count++;
1314b356379aSAl Viro 
1315b356379aSAl Viro 	do {
1316b356379aSAl Viro 		struct path link = *path;
1317b356379aSAl Viro 		void *cookie;
1318574197e0SAl Viro 
1319574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
1320b356379aSAl Viro 		if (!res)
1321b356379aSAl Viro 			res = walk_component(nd, path, &nd->last,
1322b356379aSAl Viro 					     nd->last_type, LOOKUP_FOLLOW);
1323574197e0SAl Viro 		put_link(nd, &link, cookie);
1324b356379aSAl Viro 	} while (res > 0);
1325b356379aSAl Viro 
1326b356379aSAl Viro 	current->link_count--;
1327b356379aSAl Viro 	nd->depth--;
1328b356379aSAl Viro 	return res;
1329b356379aSAl Viro }
1330b356379aSAl Viro 
1331b356379aSAl Viro /*
13321da177e4SLinus Torvalds  * Name resolution.
1333ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1334ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
13351da177e4SLinus Torvalds  *
1336ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1337ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
13381da177e4SLinus Torvalds  */
13396de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
13401da177e4SLinus Torvalds {
13411da177e4SLinus Torvalds 	struct path next;
13421da177e4SLinus Torvalds 	int err;
13431da177e4SLinus Torvalds 	unsigned int lookup_flags = nd->flags;
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds 	while (*name=='/')
13461da177e4SLinus Torvalds 		name++;
13471da177e4SLinus Torvalds 	if (!*name)
1348086e183aSAl Viro 		return 0;
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
13511da177e4SLinus Torvalds 	for(;;) {
13521da177e4SLinus Torvalds 		unsigned long hash;
13531da177e4SLinus Torvalds 		struct qstr this;
13541da177e4SLinus Torvalds 		unsigned int c;
1355fe479a58SAl Viro 		int type;
13561da177e4SLinus Torvalds 
1357cdce5d6bSTrond Myklebust 		nd->flags |= LOOKUP_CONTINUE;
135852094c8aSAl Viro 
135952094c8aSAl Viro 		err = may_lookup(nd);
13601da177e4SLinus Torvalds  		if (err)
13611da177e4SLinus Torvalds 			break;
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 		this.name = name;
13641da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 		hash = init_name_hash();
13671da177e4SLinus Torvalds 		do {
13681da177e4SLinus Torvalds 			name++;
13691da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
13701da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
13711da177e4SLinus Torvalds 		} while (c && (c != '/'));
13721da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
13731da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
13741da177e4SLinus Torvalds 
1375fe479a58SAl Viro 		type = LAST_NORM;
1376fe479a58SAl Viro 		if (this.name[0] == '.') switch (this.len) {
1377fe479a58SAl Viro 			case 2:
137816c2cd71SAl Viro 				if (this.name[1] == '.') {
1379fe479a58SAl Viro 					type = LAST_DOTDOT;
138016c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
138116c2cd71SAl Viro 				}
1382fe479a58SAl Viro 				break;
1383fe479a58SAl Viro 			case 1:
1384fe479a58SAl Viro 				type = LAST_DOT;
1385fe479a58SAl Viro 		}
13865a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
13875a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
138816c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
13895a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
13905a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
13915a202bcdSAl Viro 							   &this);
13925a202bcdSAl Viro 				if (err < 0)
13935a202bcdSAl Viro 					break;
13945a202bcdSAl Viro 			}
13955a202bcdSAl Viro 		}
1396fe479a58SAl Viro 
13971da177e4SLinus Torvalds 		/* remove trailing slashes? */
13981da177e4SLinus Torvalds 		if (!c)
13991da177e4SLinus Torvalds 			goto last_component;
14001da177e4SLinus Torvalds 		while (*++name == '/');
14011da177e4SLinus Torvalds 		if (!*name)
1402b356379aSAl Viro 			goto last_component;
14031da177e4SLinus Torvalds 
1404ce57dfc1SAl Viro 		err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1405ce57dfc1SAl Viro 		if (err < 0)
1406ce57dfc1SAl Viro 			return err;
1407fe479a58SAl Viro 
1408ce57dfc1SAl Viro 		if (err) {
1409b356379aSAl Viro 			err = nested_symlink(&next, nd);
14101da177e4SLinus Torvalds 			if (err)
1411a7472babSAl Viro 				return err;
141231e6b01fSNick Piggin 		}
14131da177e4SLinus Torvalds 		err = -ENOTDIR;
141431e6b01fSNick Piggin 		if (!nd->inode->i_op->lookup)
14151da177e4SLinus Torvalds 			break;
14161da177e4SLinus Torvalds 		continue;
14171da177e4SLinus Torvalds 		/* here ends the main loop */
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds last_component:
1420f55eab82STrond Myklebust 		/* Clear LOOKUP_CONTINUE iff it was previously unset */
1421f55eab82STrond Myklebust 		nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
1422ce57dfc1SAl Viro 		nd->last = this;
1423ce57dfc1SAl Viro 		nd->last_type = type;
1424ce57dfc1SAl Viro 		return 0;
1425ce57dfc1SAl Viro 	}
1426951361f9SAl Viro 	terminate_walk(nd);
14271da177e4SLinus Torvalds 	return err;
14281da177e4SLinus Torvalds }
14291da177e4SLinus Torvalds 
143070e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
143170e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
143231e6b01fSNick Piggin {
143331e6b01fSNick Piggin 	int retval = 0;
143431e6b01fSNick Piggin 	int fput_needed;
143531e6b01fSNick Piggin 	struct file *file;
143631e6b01fSNick Piggin 
143731e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
143816c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
143931e6b01fSNick Piggin 	nd->depth = 0;
14405b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
14415b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
144273d049a4SAl Viro 		if (*name) {
14435b6ca027SAl Viro 			if (!inode->i_op->lookup)
14445b6ca027SAl Viro 				return -ENOTDIR;
14455b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
14465b6ca027SAl Viro 			if (retval)
14475b6ca027SAl Viro 				return retval;
144873d049a4SAl Viro 		}
14495b6ca027SAl Viro 		nd->path = nd->root;
14505b6ca027SAl Viro 		nd->inode = inode;
14515b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
14525b6ca027SAl Viro 			br_read_lock(vfsmount_lock);
14535b6ca027SAl Viro 			rcu_read_lock();
14545b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
14555b6ca027SAl Viro 		} else {
14565b6ca027SAl Viro 			path_get(&nd->path);
14575b6ca027SAl Viro 		}
14585b6ca027SAl Viro 		return 0;
14595b6ca027SAl Viro 	}
14605b6ca027SAl Viro 
146131e6b01fSNick Piggin 	nd->root.mnt = NULL;
146231e6b01fSNick Piggin 
146331e6b01fSNick Piggin 	if (*name=='/') {
1464e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
146531e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
146631e6b01fSNick Piggin 			rcu_read_lock();
1467e41f7d4eSAl Viro 			set_root_rcu(nd);
1468e41f7d4eSAl Viro 		} else {
1469e41f7d4eSAl Viro 			set_root(nd);
1470e41f7d4eSAl Viro 			path_get(&nd->root);
1471e41f7d4eSAl Viro 		}
147231e6b01fSNick Piggin 		nd->path = nd->root;
147331e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1474e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
147531e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1476c28cc364SNick Piggin 			unsigned seq;
147731e6b01fSNick Piggin 
147831e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
147931e6b01fSNick Piggin 			rcu_read_lock();
148031e6b01fSNick Piggin 
1481c28cc364SNick Piggin 			do {
1482c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
148331e6b01fSNick Piggin 				nd->path = fs->pwd;
1484c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1485c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1486e41f7d4eSAl Viro 		} else {
1487e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1488e41f7d4eSAl Viro 		}
148931e6b01fSNick Piggin 	} else {
149031e6b01fSNick Piggin 		struct dentry *dentry;
149131e6b01fSNick Piggin 
14921abf0c71SAl Viro 		file = fget_raw_light(dfd, &fput_needed);
149331e6b01fSNick Piggin 		retval = -EBADF;
149431e6b01fSNick Piggin 		if (!file)
149531e6b01fSNick Piggin 			goto out_fail;
149631e6b01fSNick Piggin 
149731e6b01fSNick Piggin 		dentry = file->f_path.dentry;
149831e6b01fSNick Piggin 
1499f52e0c11SAl Viro 		if (*name) {
150031e6b01fSNick Piggin 			retval = -ENOTDIR;
150131e6b01fSNick Piggin 			if (!S_ISDIR(dentry->d_inode->i_mode))
150231e6b01fSNick Piggin 				goto fput_fail;
150331e6b01fSNick Piggin 
1504eecdd358SAl Viro 			retval = exec_permission(dentry->d_inode, MAY_EXEC);
150531e6b01fSNick Piggin 			if (retval)
150631e6b01fSNick Piggin 				goto fput_fail;
1507f52e0c11SAl Viro 		}
150831e6b01fSNick Piggin 
150931e6b01fSNick Piggin 		nd->path = file->f_path;
1510e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
151131e6b01fSNick Piggin 			if (fput_needed)
151270e9b357SAl Viro 				*fp = file;
1513c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
151431e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
151531e6b01fSNick Piggin 			rcu_read_lock();
15165590ff0dSUlrich Drepper 		} else {
15175dd784d0SJan Blunck 			path_get(&file->f_path);
15185590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
15191da177e4SLinus Torvalds 		}
1520e41f7d4eSAl Viro 	}
1521e41f7d4eSAl Viro 
152231e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
15239b4a9b14SAl Viro 	return 0;
15242dfdd266SJosef 'Jeff' Sipek 
15259b4a9b14SAl Viro fput_fail:
15269b4a9b14SAl Viro 	fput_light(file, fput_needed);
15279b4a9b14SAl Viro out_fail:
15289b4a9b14SAl Viro 	return retval;
15299b4a9b14SAl Viro }
15309b4a9b14SAl Viro 
1531bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1532bd92d7feSAl Viro {
1533bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1534bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1535bd92d7feSAl Viro 
1536bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1537bd92d7feSAl Viro 	return walk_component(nd, path, &nd->last, nd->last_type,
1538bd92d7feSAl Viro 					nd->flags & LOOKUP_FOLLOW);
1539bd92d7feSAl Viro }
1540bd92d7feSAl Viro 
15419b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1542ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
15439b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
15449b4a9b14SAl Viro {
154570e9b357SAl Viro 	struct file *base = NULL;
1546bd92d7feSAl Viro 	struct path path;
1547bd92d7feSAl Viro 	int err;
154831e6b01fSNick Piggin 
154931e6b01fSNick Piggin 	/*
155031e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
155131e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
155231e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
155331e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
155431e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
155531e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
155631e6b01fSNick Piggin 	 * analogue, foo_rcu().
155731e6b01fSNick Piggin 	 *
155831e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
155931e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
156031e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
156131e6b01fSNick Piggin 	 * be able to complete).
156231e6b01fSNick Piggin 	 */
1563bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1564ee0827cdSAl Viro 
1565bd92d7feSAl Viro 	if (unlikely(err))
1566bd92d7feSAl Viro 		return err;
1567ee0827cdSAl Viro 
1568ee0827cdSAl Viro 	current->total_link_count = 0;
1569bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1570bd92d7feSAl Viro 
1571bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1572bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1573bd92d7feSAl Viro 		while (err > 0) {
1574bd92d7feSAl Viro 			void *cookie;
1575bd92d7feSAl Viro 			struct path link = path;
1576bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1577574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
1578bd92d7feSAl Viro 			if (!err)
1579bd92d7feSAl Viro 				err = lookup_last(nd, &path);
1580574197e0SAl Viro 			put_link(nd, &link, cookie);
1581bd92d7feSAl Viro 		}
1582bd92d7feSAl Viro 	}
1583ee0827cdSAl Viro 
15849f1fafeeSAl Viro 	if (!err)
15859f1fafeeSAl Viro 		err = complete_walk(nd);
1586bd92d7feSAl Viro 
1587bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1588bd92d7feSAl Viro 		if (!nd->inode->i_op->lookup) {
1589bd92d7feSAl Viro 			path_put(&nd->path);
1590bd23a539SAl Viro 			err = -ENOTDIR;
1591bd92d7feSAl Viro 		}
1592bd92d7feSAl Viro 	}
159316c2cd71SAl Viro 
159470e9b357SAl Viro 	if (base)
159570e9b357SAl Viro 		fput(base);
1596ee0827cdSAl Viro 
15975b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
159831e6b01fSNick Piggin 		path_put(&nd->root);
159931e6b01fSNick Piggin 		nd->root.mnt = NULL;
160031e6b01fSNick Piggin 	}
1601bd92d7feSAl Viro 	return err;
160231e6b01fSNick Piggin }
160331e6b01fSNick Piggin 
1604ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1605ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1606ee0827cdSAl Viro {
1607ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1608ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1609ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1610ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1611ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1612ee0827cdSAl Viro 
161331e6b01fSNick Piggin 	if (likely(!retval)) {
161431e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
161531e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
161631e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
161731e6b01fSNick Piggin 		}
161831e6b01fSNick Piggin 	}
1619170aa3d0SUlrich Drepper 	return retval;
16201da177e4SLinus Torvalds }
16211da177e4SLinus Torvalds 
1622c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd)
16235590ff0dSUlrich Drepper {
1624c9c6cac0SAl Viro 	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
16255590ff0dSUlrich Drepper }
16265590ff0dSUlrich Drepper 
1627d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1628d1811465SAl Viro {
1629d1811465SAl Viro 	struct nameidata nd;
1630d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1631d1811465SAl Viro 	if (!res)
1632d1811465SAl Viro 		*path = nd.path;
1633d1811465SAl Viro 	return res;
1634d1811465SAl Viro }
1635d1811465SAl Viro 
163616f18200SJosef 'Jeff' Sipek /**
163716f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
163816f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
163916f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
164016f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
164116f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
164216f18200SJosef 'Jeff' Sipek  * @nd: pointer to nameidata
164316f18200SJosef 'Jeff' Sipek  */
164416f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
164516f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
164616f18200SJosef 'Jeff' Sipek 		    struct nameidata *nd)
164716f18200SJosef 'Jeff' Sipek {
16485b6ca027SAl Viro 	nd->root.dentry = dentry;
16495b6ca027SAl Viro 	nd->root.mnt = mnt;
16505b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
16515b6ca027SAl Viro 	return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
165216f18200SJosef 'Jeff' Sipek }
165316f18200SJosef 'Jeff' Sipek 
1654eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1655eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
16561da177e4SLinus Torvalds {
165781fca444SChristoph Hellwig 	struct inode *inode = base->d_inode;
16581da177e4SLinus Torvalds 	struct dentry *dentry;
16591da177e4SLinus Torvalds 	int err;
16601da177e4SLinus Torvalds 
1661eecdd358SAl Viro 	err = exec_permission(inode, MAY_EXEC);
166281fca444SChristoph Hellwig 	if (err)
166381fca444SChristoph Hellwig 		return ERR_PTR(err);
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds 	/*
1666b04f784eSNick Piggin 	 * Don't bother with __d_lookup: callers are for creat as
1667b04f784eSNick Piggin 	 * well as unlink, so a lot of the time it would cost
1668b04f784eSNick Piggin 	 * a double lookup.
16696e6b1bd1SAl Viro 	 */
16706e6b1bd1SAl Viro 	dentry = d_lookup(base, name);
16716e6b1bd1SAl Viro 
167244396f4bSJosef Bacik 	if (dentry && d_need_lookup(dentry)) {
167344396f4bSJosef Bacik 		/*
167444396f4bSJosef Bacik 		 * __lookup_hash is called with the parent dir's i_mutex already
167544396f4bSJosef Bacik 		 * held, so we are good to go here.
167644396f4bSJosef Bacik 		 */
167744396f4bSJosef Bacik 		dentry = d_inode_lookup(base, dentry, nd);
167844396f4bSJosef Bacik 		if (IS_ERR(dentry))
167944396f4bSJosef Bacik 			return dentry;
168044396f4bSJosef Bacik 	}
168144396f4bSJosef Bacik 
1682fb045adbSNick Piggin 	if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
16836e6b1bd1SAl Viro 		dentry = do_revalidate(dentry, nd);
16846e6b1bd1SAl Viro 
16851da177e4SLinus Torvalds 	if (!dentry)
1686baa03890SNick Piggin 		dentry = d_alloc_and_lookup(base, name, nd);
16875a202bcdSAl Viro 
16881da177e4SLinus Torvalds 	return dentry;
16891da177e4SLinus Torvalds }
16901da177e4SLinus Torvalds 
1691057f6c01SJames Morris /*
1692057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1693057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1694057f6c01SJames Morris  * SMP-safe.
1695057f6c01SJames Morris  */
1696a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
16971da177e4SLinus Torvalds {
16984ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
16991da177e4SLinus Torvalds }
17001da177e4SLinus Torvalds 
1701eead1911SChristoph Hellwig /**
1702a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1703eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1704eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1705eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1706eead1911SChristoph Hellwig  *
1707a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1708a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1709eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1710eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1711eead1911SChristoph Hellwig  */
1712057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1713057f6c01SJames Morris {
1714057f6c01SJames Morris 	struct qstr this;
17156a96ba54SAl Viro 	unsigned long hash;
17166a96ba54SAl Viro 	unsigned int c;
1717057f6c01SJames Morris 
17182f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
17192f9092e1SDavid Woodhouse 
17206a96ba54SAl Viro 	this.name = name;
17216a96ba54SAl Viro 	this.len = len;
17226a96ba54SAl Viro 	if (!len)
17236a96ba54SAl Viro 		return ERR_PTR(-EACCES);
17246a96ba54SAl Viro 
17256a96ba54SAl Viro 	hash = init_name_hash();
17266a96ba54SAl Viro 	while (len--) {
17276a96ba54SAl Viro 		c = *(const unsigned char *)name++;
17286a96ba54SAl Viro 		if (c == '/' || c == '\0')
17296a96ba54SAl Viro 			return ERR_PTR(-EACCES);
17306a96ba54SAl Viro 		hash = partial_name_hash(c, hash);
17316a96ba54SAl Viro 	}
17326a96ba54SAl Viro 	this.hash = end_name_hash(hash);
17335a202bcdSAl Viro 	/*
17345a202bcdSAl Viro 	 * See if the low-level filesystem might want
17355a202bcdSAl Viro 	 * to use its own hash..
17365a202bcdSAl Viro 	 */
17375a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
17385a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
17395a202bcdSAl Viro 		if (err < 0)
17405a202bcdSAl Viro 			return ERR_PTR(err);
17415a202bcdSAl Viro 	}
1742eead1911SChristoph Hellwig 
174349705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1744057f6c01SJames Morris }
1745057f6c01SJames Morris 
17462d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
17472d8f3038SAl Viro 		 struct path *path)
17481da177e4SLinus Torvalds {
17492d8f3038SAl Viro 	struct nameidata nd;
1750f52e0c11SAl Viro 	char *tmp = getname_flags(name, flags);
17511da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
17521da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
17532d8f3038SAl Viro 
17542d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
17552d8f3038SAl Viro 
17562d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
17571da177e4SLinus Torvalds 		putname(tmp);
17582d8f3038SAl Viro 		if (!err)
17592d8f3038SAl Viro 			*path = nd.path;
17601da177e4SLinus Torvalds 	}
17611da177e4SLinus Torvalds 	return err;
17621da177e4SLinus Torvalds }
17631da177e4SLinus Torvalds 
17642ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
17652ad94ae6SAl Viro 			struct nameidata *nd, char **name)
17662ad94ae6SAl Viro {
17672ad94ae6SAl Viro 	char *s = getname(path);
17682ad94ae6SAl Viro 	int error;
17692ad94ae6SAl Viro 
17702ad94ae6SAl Viro 	if (IS_ERR(s))
17712ad94ae6SAl Viro 		return PTR_ERR(s);
17722ad94ae6SAl Viro 
17732ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
17742ad94ae6SAl Viro 	if (error)
17752ad94ae6SAl Viro 		putname(s);
17762ad94ae6SAl Viro 	else
17772ad94ae6SAl Viro 		*name = s;
17782ad94ae6SAl Viro 
17792ad94ae6SAl Viro 	return error;
17802ad94ae6SAl Viro }
17812ad94ae6SAl Viro 
17821da177e4SLinus Torvalds /*
17831da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
17841da177e4SLinus Torvalds  * minimal.
17851da177e4SLinus Torvalds  */
17861da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
17871da177e4SLinus Torvalds {
1788da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1789da9592edSDavid Howells 
17901da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
17911da177e4SLinus Torvalds 		return 0;
1792e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
1793e795b717SSerge E. Hallyn 		goto other_userns;
1794da9592edSDavid Howells 	if (inode->i_uid == fsuid)
17951da177e4SLinus Torvalds 		return 0;
1796da9592edSDavid Howells 	if (dir->i_uid == fsuid)
17971da177e4SLinus Torvalds 		return 0;
1798e795b717SSerge E. Hallyn 
1799e795b717SSerge E. Hallyn other_userns:
1800e795b717SSerge E. Hallyn 	return !ns_capable(inode_userns(inode), CAP_FOWNER);
18011da177e4SLinus Torvalds }
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds /*
18041da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
18051da177e4SLinus Torvalds  *  whether the type of victim is right.
18061da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
18071da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
18081da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
18091da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
18101da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
18111da177e4SLinus Torvalds  *	a. be owner of dir, or
18121da177e4SLinus Torvalds  *	b. be owner of victim, or
18131da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
18141da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
18151da177e4SLinus Torvalds  *     links pointing to it.
18161da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
18171da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
18181da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
18191da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
18201da177e4SLinus Torvalds  *     nfs_async_unlink().
18211da177e4SLinus Torvalds  */
1822858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
18231da177e4SLinus Torvalds {
18241da177e4SLinus Torvalds 	int error;
18251da177e4SLinus Torvalds 
18261da177e4SLinus Torvalds 	if (!victim->d_inode)
18271da177e4SLinus Torvalds 		return -ENOENT;
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1830cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
18311da177e4SLinus Torvalds 
1832f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
18331da177e4SLinus Torvalds 	if (error)
18341da177e4SLinus Torvalds 		return error;
18351da177e4SLinus Torvalds 	if (IS_APPEND(dir))
18361da177e4SLinus Torvalds 		return -EPERM;
18371da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1838f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
18391da177e4SLinus Torvalds 		return -EPERM;
18401da177e4SLinus Torvalds 	if (isdir) {
18411da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
18421da177e4SLinus Torvalds 			return -ENOTDIR;
18431da177e4SLinus Torvalds 		if (IS_ROOT(victim))
18441da177e4SLinus Torvalds 			return -EBUSY;
18451da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
18461da177e4SLinus Torvalds 		return -EISDIR;
18471da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18481da177e4SLinus Torvalds 		return -ENOENT;
18491da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
18501da177e4SLinus Torvalds 		return -EBUSY;
18511da177e4SLinus Torvalds 	return 0;
18521da177e4SLinus Torvalds }
18531da177e4SLinus Torvalds 
18541da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
18551da177e4SLinus Torvalds  *  dir.
18561da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
18571da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
18581da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
18591da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
18601da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
18611da177e4SLinus Torvalds  */
1862a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
18631da177e4SLinus Torvalds {
18641da177e4SLinus Torvalds 	if (child->d_inode)
18651da177e4SLinus Torvalds 		return -EEXIST;
18661da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18671da177e4SLinus Torvalds 		return -ENOENT;
1868f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
18691da177e4SLinus Torvalds }
18701da177e4SLinus Torvalds 
18711da177e4SLinus Torvalds /*
18721da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
18731da177e4SLinus Torvalds  */
18741da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
18751da177e4SLinus Torvalds {
18761da177e4SLinus Torvalds 	struct dentry *p;
18771da177e4SLinus Torvalds 
18781da177e4SLinus Torvalds 	if (p1 == p2) {
1879f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
18801da177e4SLinus Torvalds 		return NULL;
18811da177e4SLinus Torvalds 	}
18821da177e4SLinus Torvalds 
1883a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
18841da177e4SLinus Torvalds 
1885e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1886e2761a11SOGAWA Hirofumi 	if (p) {
1887f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1888f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
18891da177e4SLinus Torvalds 		return p;
18901da177e4SLinus Torvalds 	}
18911da177e4SLinus Torvalds 
1892e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1893e2761a11SOGAWA Hirofumi 	if (p) {
1894f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1895f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
18961da177e4SLinus Torvalds 		return p;
18971da177e4SLinus Torvalds 	}
18981da177e4SLinus Torvalds 
1899f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1900f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19011da177e4SLinus Torvalds 	return NULL;
19021da177e4SLinus Torvalds }
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
19051da177e4SLinus Torvalds {
19061b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
19071da177e4SLinus Torvalds 	if (p1 != p2) {
19081b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1909a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19101da177e4SLinus Torvalds 	}
19111da177e4SLinus Torvalds }
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
19141da177e4SLinus Torvalds 		struct nameidata *nd)
19151da177e4SLinus Torvalds {
1916a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
19171da177e4SLinus Torvalds 
19181da177e4SLinus Torvalds 	if (error)
19191da177e4SLinus Torvalds 		return error;
19201da177e4SLinus Torvalds 
1921acfa4380SAl Viro 	if (!dir->i_op->create)
19221da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
19231da177e4SLinus Torvalds 	mode &= S_IALLUGO;
19241da177e4SLinus Torvalds 	mode |= S_IFREG;
19251da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
19261da177e4SLinus Torvalds 	if (error)
19271da177e4SLinus Torvalds 		return error;
19281da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1929a74574aaSStephen Smalley 	if (!error)
1930f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
19311da177e4SLinus Torvalds 	return error;
19321da177e4SLinus Torvalds }
19331da177e4SLinus Torvalds 
193473d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
19351da177e4SLinus Torvalds {
19363fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
19371da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
19381da177e4SLinus Torvalds 	int error;
19391da177e4SLinus Torvalds 
1940bcda7652SAl Viro 	/* O_PATH? */
1941bcda7652SAl Viro 	if (!acc_mode)
1942bcda7652SAl Viro 		return 0;
1943bcda7652SAl Viro 
19441da177e4SLinus Torvalds 	if (!inode)
19451da177e4SLinus Torvalds 		return -ENOENT;
19461da177e4SLinus Torvalds 
1947c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1948c8fe8f30SChristoph Hellwig 	case S_IFLNK:
19491da177e4SLinus Torvalds 		return -ELOOP;
1950c8fe8f30SChristoph Hellwig 	case S_IFDIR:
1951c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
19521da177e4SLinus Torvalds 			return -EISDIR;
1953c8fe8f30SChristoph Hellwig 		break;
1954c8fe8f30SChristoph Hellwig 	case S_IFBLK:
1955c8fe8f30SChristoph Hellwig 	case S_IFCHR:
19563fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
19571da177e4SLinus Torvalds 			return -EACCES;
1958c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
1959c8fe8f30SChristoph Hellwig 	case S_IFIFO:
1960c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
19611da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
1962c8fe8f30SChristoph Hellwig 		break;
19634a3fd211SDave Hansen 	}
1964b41572e9SDave Hansen 
19653fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
1966b41572e9SDave Hansen 	if (error)
1967b41572e9SDave Hansen 		return error;
19686146f0d5SMimi Zohar 
19691da177e4SLinus Torvalds 	/*
19701da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
19711da177e4SLinus Torvalds 	 */
19721da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
19738737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
19747715b521SAl Viro 			return -EPERM;
19751da177e4SLinus Torvalds 		if (flag & O_TRUNC)
19767715b521SAl Viro 			return -EPERM;
19771da177e4SLinus Torvalds 	}
19781da177e4SLinus Torvalds 
19791da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
19802e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
19817715b521SAl Viro 		return -EPERM;
19821da177e4SLinus Torvalds 
19831da177e4SLinus Torvalds 	/*
19841da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
19851da177e4SLinus Torvalds 	 */
1986b65a9cfcSAl Viro 	return break_lease(inode, flag);
19877715b521SAl Viro }
19887715b521SAl Viro 
1989e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
19907715b521SAl Viro {
1991e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
19927715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
19937715b521SAl Viro 	int error = get_write_access(inode);
19941da177e4SLinus Torvalds 	if (error)
19957715b521SAl Viro 		return error;
19961da177e4SLinus Torvalds 	/*
19971da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
19981da177e4SLinus Torvalds 	 */
19991da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2000be6d3e56SKentaro Takeda 	if (!error)
2001ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
20021da177e4SLinus Torvalds 	if (!error) {
20037715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2004d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2005e1181ee6SJeff Layton 				    filp);
20061da177e4SLinus Torvalds 	}
20071da177e4SLinus Torvalds 	put_write_access(inode);
2008acd0c935SMimi Zohar 	return error;
20091da177e4SLinus Torvalds }
20101da177e4SLinus Torvalds 
2011d57999e1SDave Hansen /*
2012d57999e1SDave Hansen  * Note that while the flag value (low two bits) for sys_open means:
2013d57999e1SDave Hansen  *	00 - read-only
2014d57999e1SDave Hansen  *	01 - write-only
2015d57999e1SDave Hansen  *	10 - read-write
2016d57999e1SDave Hansen  *	11 - special
2017d57999e1SDave Hansen  * it is changed into
2018d57999e1SDave Hansen  *	00 - no permissions needed
2019d57999e1SDave Hansen  *	01 - read-permission
2020d57999e1SDave Hansen  *	10 - write-permission
2021d57999e1SDave Hansen  *	11 - read-write
2022d57999e1SDave Hansen  * for the internal routines (ie open_namei()/follow_link() etc)
2023d57999e1SDave Hansen  * This is more logical, and also allows the 00 "no perm needed"
2024d57999e1SDave Hansen  * to be used for symlinks (where the permissions are checked
2025d57999e1SDave Hansen  * later).
2026d57999e1SDave Hansen  *
2027d57999e1SDave Hansen */
2028d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2029d57999e1SDave Hansen {
2030d57999e1SDave Hansen 	if ((flag+1) & O_ACCMODE)
2031d57999e1SDave Hansen 		flag++;
2032d57999e1SDave Hansen 	return flag;
2033d57999e1SDave Hansen }
2034d57999e1SDave Hansen 
203531e6b01fSNick Piggin /*
2036fe2d35ffSAl Viro  * Handle the last step of open()
203731e6b01fSNick Piggin  */
2038fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
2039c3e380b0SAl Viro 			    const struct open_flags *op, const char *pathname)
2040fb1cc555SAl Viro {
2041a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
20426c0d46c4SAl Viro 	struct dentry *dentry;
2043ca344a89SAl Viro 	int open_flag = op->open_flag;
20446c0d46c4SAl Viro 	int will_truncate = open_flag & O_TRUNC;
2045ca344a89SAl Viro 	int want_write = 0;
2046bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2047fb1cc555SAl Viro 	struct file *filp;
204816c2cd71SAl Viro 	int error;
2049fb1cc555SAl Viro 
2050c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2051c3e380b0SAl Viro 	nd->flags |= op->intent;
2052c3e380b0SAl Viro 
20531f36f774SAl Viro 	switch (nd->last_type) {
20541f36f774SAl Viro 	case LAST_DOTDOT:
2055176306f5SNeil Brown 	case LAST_DOT:
2056fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2057fe2d35ffSAl Viro 		if (error)
2058fe2d35ffSAl Viro 			return ERR_PTR(error);
20591f36f774SAl Viro 		/* fallthrough */
20601f36f774SAl Viro 	case LAST_ROOT:
20619f1fafeeSAl Viro 		error = complete_walk(nd);
206216c2cd71SAl Viro 		if (error)
20639f1fafeeSAl Viro 			return ERR_PTR(error);
2064fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2065ca344a89SAl Viro 		if (open_flag & O_CREAT) {
206616c2cd71SAl Viro 			error = -EISDIR;
20671f36f774SAl Viro 			goto exit;
2068fe2d35ffSAl Viro 		}
2069fe2d35ffSAl Viro 		goto ok;
20701f36f774SAl Viro 	case LAST_BIND:
20719f1fafeeSAl Viro 		error = complete_walk(nd);
207216c2cd71SAl Viro 		if (error)
20739f1fafeeSAl Viro 			return ERR_PTR(error);
20741f36f774SAl Viro 		audit_inode(pathname, dir);
20751f36f774SAl Viro 		goto ok;
20761f36f774SAl Viro 	}
2077a2c36b45SAl Viro 
2078ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2079bcda7652SAl Viro 		int symlink_ok = 0;
2080fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2081fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2082bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2083bcda7652SAl Viro 			symlink_ok = 1;
2084fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2085ce57dfc1SAl Viro 		error = walk_component(nd, path, &nd->last, LAST_NORM,
2086ce57dfc1SAl Viro 					!symlink_ok);
2087ce57dfc1SAl Viro 		if (error < 0)
2088fe2d35ffSAl Viro 			return ERR_PTR(error);
2089ce57dfc1SAl Viro 		if (error) /* symlink */
2090fe2d35ffSAl Viro 			return NULL;
2091fe2d35ffSAl Viro 		/* sayonara */
20929f1fafeeSAl Viro 		error = complete_walk(nd);
20939f1fafeeSAl Viro 		if (error)
2094fe2d35ffSAl Viro 			return ERR_PTR(-ECHILD);
2095fe2d35ffSAl Viro 
2096fe2d35ffSAl Viro 		error = -ENOTDIR;
2097fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_DIRECTORY) {
2098ce57dfc1SAl Viro 			if (!nd->inode->i_op->lookup)
2099fe2d35ffSAl Viro 				goto exit;
2100fe2d35ffSAl Viro 		}
2101fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2102fe2d35ffSAl Viro 		goto ok;
2103fe2d35ffSAl Viro 	}
2104fe2d35ffSAl Viro 
2105fe2d35ffSAl Viro 	/* create side of things */
21069f1fafeeSAl Viro 	error = complete_walk(nd);
21079f1fafeeSAl Viro 	if (error)
21089f1fafeeSAl Viro 		return ERR_PTR(error);
2109fe2d35ffSAl Viro 
2110fe2d35ffSAl Viro 	audit_inode(pathname, dir);
211116c2cd71SAl Viro 	error = -EISDIR;
21121f36f774SAl Viro 	/* trailing slashes? */
211331e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
21141f36f774SAl Viro 		goto exit;
21151f36f774SAl Viro 
2116a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2117a1e28038SAl Viro 
21186c0d46c4SAl Viro 	dentry = lookup_hash(nd);
21196c0d46c4SAl Viro 	error = PTR_ERR(dentry);
21206c0d46c4SAl Viro 	if (IS_ERR(dentry)) {
2121fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2122fb1cc555SAl Viro 		goto exit;
2123fb1cc555SAl Viro 	}
2124fb1cc555SAl Viro 
21256c0d46c4SAl Viro 	path->dentry = dentry;
21266c0d46c4SAl Viro 	path->mnt = nd->path.mnt;
21276c0d46c4SAl Viro 
2128fb1cc555SAl Viro 	/* Negative dentry, just create the file */
21296c0d46c4SAl Viro 	if (!dentry->d_inode) {
21306c0d46c4SAl Viro 		int mode = op->mode;
21316c0d46c4SAl Viro 		if (!IS_POSIXACL(dir->d_inode))
21326c0d46c4SAl Viro 			mode &= ~current_umask();
2133fb1cc555SAl Viro 		/*
2134fb1cc555SAl Viro 		 * This write is needed to ensure that a
21356c0d46c4SAl Viro 		 * rw->ro transition does not occur between
2136fb1cc555SAl Viro 		 * the time when the file is created and when
2137fb1cc555SAl Viro 		 * a permanent write count is taken through
2138fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2139fb1cc555SAl Viro 		 */
2140fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2141fb1cc555SAl Viro 		if (error)
2142fb1cc555SAl Viro 			goto exit_mutex_unlock;
2143ca344a89SAl Viro 		want_write = 1;
21449b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2145ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
21466c0d46c4SAl Viro 		will_truncate = 0;
2147bcda7652SAl Viro 		acc_mode = MAY_OPEN;
21486c0d46c4SAl Viro 		error = security_path_mknod(&nd->path, dentry, mode, 0);
21496c0d46c4SAl Viro 		if (error)
21506c0d46c4SAl Viro 			goto exit_mutex_unlock;
21516c0d46c4SAl Viro 		error = vfs_create(dir->d_inode, dentry, mode, nd);
21526c0d46c4SAl Viro 		if (error)
21536c0d46c4SAl Viro 			goto exit_mutex_unlock;
21546c0d46c4SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
21556c0d46c4SAl Viro 		dput(nd->path.dentry);
21566c0d46c4SAl Viro 		nd->path.dentry = dentry;
2157ca344a89SAl Viro 		goto common;
2158fb1cc555SAl Viro 	}
2159fb1cc555SAl Viro 
2160fb1cc555SAl Viro 	/*
2161fb1cc555SAl Viro 	 * It already exists.
2162fb1cc555SAl Viro 	 */
2163fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2164fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2165fb1cc555SAl Viro 
2166fb1cc555SAl Viro 	error = -EEXIST;
2167ca344a89SAl Viro 	if (open_flag & O_EXCL)
2168fb1cc555SAl Viro 		goto exit_dput;
2169fb1cc555SAl Viro 
21709875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
21719875cf80SDavid Howells 	if (error < 0)
2172fb1cc555SAl Viro 		goto exit_dput;
2173fb1cc555SAl Viro 
2174fb1cc555SAl Viro 	error = -ENOENT;
2175fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2176fb1cc555SAl Viro 		goto exit_dput;
21779e67f361SAl Viro 
21789e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2179fb1cc555SAl Viro 		return NULL;
2180fb1cc555SAl Viro 
2181fb1cc555SAl Viro 	path_to_nameidata(path, nd);
218231e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2183fb1cc555SAl Viro 	error = -EISDIR;
218431e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2185fb1cc555SAl Viro 		goto exit;
218667ee3ad2SAl Viro ok:
21876c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
21886c0d46c4SAl Viro 		will_truncate = 0;
21896c0d46c4SAl Viro 
21900f9d1a10SAl Viro 	if (will_truncate) {
21910f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
21920f9d1a10SAl Viro 		if (error)
21930f9d1a10SAl Viro 			goto exit;
2194ca344a89SAl Viro 		want_write = 1;
21950f9d1a10SAl Viro 	}
2196ca344a89SAl Viro common:
2197bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2198ca344a89SAl Viro 	if (error)
21990f9d1a10SAl Viro 		goto exit;
22000f9d1a10SAl Viro 	filp = nameidata_to_filp(nd);
22010f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
22020f9d1a10SAl Viro 		error = ima_file_check(filp, op->acc_mode);
22030f9d1a10SAl Viro 		if (error) {
22040f9d1a10SAl Viro 			fput(filp);
22050f9d1a10SAl Viro 			filp = ERR_PTR(error);
22060f9d1a10SAl Viro 		}
22070f9d1a10SAl Viro 	}
22080f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
22090f9d1a10SAl Viro 		if (will_truncate) {
22100f9d1a10SAl Viro 			error = handle_truncate(filp);
22110f9d1a10SAl Viro 			if (error) {
22120f9d1a10SAl Viro 				fput(filp);
22130f9d1a10SAl Viro 				filp = ERR_PTR(error);
22140f9d1a10SAl Viro 			}
22150f9d1a10SAl Viro 		}
22160f9d1a10SAl Viro 	}
2217ca344a89SAl Viro out:
2218ca344a89SAl Viro 	if (want_write)
22190f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
22200f9d1a10SAl Viro 	path_put(&nd->path);
2221fb1cc555SAl Viro 	return filp;
2222fb1cc555SAl Viro 
2223fb1cc555SAl Viro exit_mutex_unlock:
2224fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2225fb1cc555SAl Viro exit_dput:
2226fb1cc555SAl Viro 	path_put_conditional(path, nd);
2227fb1cc555SAl Viro exit:
2228ca344a89SAl Viro 	filp = ERR_PTR(error);
2229ca344a89SAl Viro 	goto out;
2230fb1cc555SAl Viro }
2231fb1cc555SAl Viro 
223213aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
223373d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
22341da177e4SLinus Torvalds {
2235fe2d35ffSAl Viro 	struct file *base = NULL;
22364a3fd211SDave Hansen 	struct file *filp;
22379850c056SAl Viro 	struct path path;
223813aab428SAl Viro 	int error;
223931e6b01fSNick Piggin 
224031e6b01fSNick Piggin 	filp = get_empty_filp();
224131e6b01fSNick Piggin 	if (!filp)
224231e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
224331e6b01fSNick Piggin 
224447c805dcSAl Viro 	filp->f_flags = op->open_flag;
224573d049a4SAl Viro 	nd->intent.open.file = filp;
224673d049a4SAl Viro 	nd->intent.open.flags = open_to_namei_flags(op->open_flag);
224773d049a4SAl Viro 	nd->intent.open.create_mode = op->mode;
224831e6b01fSNick Piggin 
224973d049a4SAl Viro 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
225031e6b01fSNick Piggin 	if (unlikely(error))
225113aab428SAl Viro 		goto out_filp;
225231e6b01fSNick Piggin 
2253fe2d35ffSAl Viro 	current->total_link_count = 0;
225473d049a4SAl Viro 	error = link_path_walk(pathname, nd);
225531e6b01fSNick Piggin 	if (unlikely(error))
225631e6b01fSNick Piggin 		goto out_filp;
22571da177e4SLinus Torvalds 
225873d049a4SAl Viro 	filp = do_last(nd, &path, op, pathname);
2259806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
22607b9337aaSNick Piggin 		struct path link = path;
2261def4af30SAl Viro 		void *cookie;
2262574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
226373d049a4SAl Viro 			path_put_conditional(&path, nd);
226473d049a4SAl Viro 			path_put(&nd->path);
226540b39136SAl Viro 			filp = ERR_PTR(-ELOOP);
226640b39136SAl Viro 			break;
226740b39136SAl Viro 		}
226873d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
226973d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2270574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
2271c3e380b0SAl Viro 		if (unlikely(error))
2272f1afe9efSAl Viro 			filp = ERR_PTR(error);
2273c3e380b0SAl Viro 		else
227473d049a4SAl Viro 			filp = do_last(nd, &path, op, pathname);
2275574197e0SAl Viro 		put_link(nd, &link, cookie);
2276806b681cSAl Viro 	}
227710fa8e62SAl Viro out:
227873d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
227973d049a4SAl Viro 		path_put(&nd->root);
2280fe2d35ffSAl Viro 	if (base)
2281fe2d35ffSAl Viro 		fput(base);
228273d049a4SAl Viro 	release_open_intent(nd);
228310fa8e62SAl Viro 	return filp;
22841da177e4SLinus Torvalds 
228531e6b01fSNick Piggin out_filp:
228610fa8e62SAl Viro 	filp = ERR_PTR(error);
228710fa8e62SAl Viro 	goto out;
2288de459215SKirill Korotaev }
22891da177e4SLinus Torvalds 
229013aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
229113aab428SAl Viro 		const struct open_flags *op, int flags)
229213aab428SAl Viro {
229373d049a4SAl Viro 	struct nameidata nd;
229413aab428SAl Viro 	struct file *filp;
229513aab428SAl Viro 
229673d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
229713aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
229873d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
229913aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
230073d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
230113aab428SAl Viro 	return filp;
230213aab428SAl Viro }
230313aab428SAl Viro 
230473d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
230573d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
230673d049a4SAl Viro {
230773d049a4SAl Viro 	struct nameidata nd;
230873d049a4SAl Viro 	struct file *file;
230973d049a4SAl Viro 
231073d049a4SAl Viro 	nd.root.mnt = mnt;
231173d049a4SAl Viro 	nd.root.dentry = dentry;
231273d049a4SAl Viro 
231373d049a4SAl Viro 	flags |= LOOKUP_ROOT;
231473d049a4SAl Viro 
2315bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
231673d049a4SAl Viro 		return ERR_PTR(-ELOOP);
231773d049a4SAl Viro 
231873d049a4SAl Viro 	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
231973d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
232073d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags);
232173d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
232273d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
232373d049a4SAl Viro 	return file;
232473d049a4SAl Viro }
232573d049a4SAl Viro 
23261da177e4SLinus Torvalds /**
23271da177e4SLinus Torvalds  * lookup_create - lookup a dentry, creating it if it doesn't exist
23281da177e4SLinus Torvalds  * @nd: nameidata info
23291da177e4SLinus Torvalds  * @is_dir: directory flag
23301da177e4SLinus Torvalds  *
23311da177e4SLinus Torvalds  * Simple function to lookup and return a dentry and create it
23321da177e4SLinus Torvalds  * if it doesn't exist.  Is SMP-safe.
2333c663e5d8SChristoph Hellwig  *
23344ac91378SJan Blunck  * Returns with nd->path.dentry->d_inode->i_mutex locked.
23351da177e4SLinus Torvalds  */
23361da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir)
23371da177e4SLinus Torvalds {
2338c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
23391da177e4SLinus Torvalds 
23404ac91378SJan Blunck 	mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2341c663e5d8SChristoph Hellwig 	/*
2342c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2343c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2344c663e5d8SChristoph Hellwig 	 */
23451da177e4SLinus Torvalds 	if (nd->last_type != LAST_NORM)
23461da177e4SLinus Torvalds 		goto fail;
23471da177e4SLinus Torvalds 	nd->flags &= ~LOOKUP_PARENT;
23483516586aSAl Viro 	nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2349a634904aSASANO Masahiro 	nd->intent.open.flags = O_EXCL;
2350c663e5d8SChristoph Hellwig 
2351c663e5d8SChristoph Hellwig 	/*
2352c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2353c663e5d8SChristoph Hellwig 	 */
235449705b77SChristoph Hellwig 	dentry = lookup_hash(nd);
23551da177e4SLinus Torvalds 	if (IS_ERR(dentry))
23561da177e4SLinus Torvalds 		goto fail;
2357c663e5d8SChristoph Hellwig 
2358e9baf6e5SAl Viro 	if (dentry->d_inode)
2359e9baf6e5SAl Viro 		goto eexist;
2360c663e5d8SChristoph Hellwig 	/*
2361c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2362c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2363c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2364c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2365c663e5d8SChristoph Hellwig 	 */
2366e9baf6e5SAl Viro 	if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
23671da177e4SLinus Torvalds 		dput(dentry);
23681da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2369e9baf6e5SAl Viro 	}
2370e9baf6e5SAl Viro 	return dentry;
2371e9baf6e5SAl Viro eexist:
2372e9baf6e5SAl Viro 	dput(dentry);
2373e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
23741da177e4SLinus Torvalds fail:
23751da177e4SLinus Torvalds 	return dentry;
23761da177e4SLinus Torvalds }
2377f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create);
23781da177e4SLinus Torvalds 
23791da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
23801da177e4SLinus Torvalds {
2381a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
23821da177e4SLinus Torvalds 
23831da177e4SLinus Torvalds 	if (error)
23841da177e4SLinus Torvalds 		return error;
23851da177e4SLinus Torvalds 
2386e795b717SSerge E. Hallyn 	if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2387e795b717SSerge E. Hallyn 	    !ns_capable(inode_userns(dir), CAP_MKNOD))
23881da177e4SLinus Torvalds 		return -EPERM;
23891da177e4SLinus Torvalds 
2390acfa4380SAl Viro 	if (!dir->i_op->mknod)
23911da177e4SLinus Torvalds 		return -EPERM;
23921da177e4SLinus Torvalds 
239308ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
239408ce5f16SSerge E. Hallyn 	if (error)
239508ce5f16SSerge E. Hallyn 		return error;
239608ce5f16SSerge E. Hallyn 
23971da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
23981da177e4SLinus Torvalds 	if (error)
23991da177e4SLinus Torvalds 		return error;
24001da177e4SLinus Torvalds 
24011da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2402a74574aaSStephen Smalley 	if (!error)
2403f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
24041da177e4SLinus Torvalds 	return error;
24051da177e4SLinus Torvalds }
24061da177e4SLinus Torvalds 
2407463c3197SDave Hansen static int may_mknod(mode_t mode)
2408463c3197SDave Hansen {
2409463c3197SDave Hansen 	switch (mode & S_IFMT) {
2410463c3197SDave Hansen 	case S_IFREG:
2411463c3197SDave Hansen 	case S_IFCHR:
2412463c3197SDave Hansen 	case S_IFBLK:
2413463c3197SDave Hansen 	case S_IFIFO:
2414463c3197SDave Hansen 	case S_IFSOCK:
2415463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2416463c3197SDave Hansen 		return 0;
2417463c3197SDave Hansen 	case S_IFDIR:
2418463c3197SDave Hansen 		return -EPERM;
2419463c3197SDave Hansen 	default:
2420463c3197SDave Hansen 		return -EINVAL;
2421463c3197SDave Hansen 	}
2422463c3197SDave Hansen }
2423463c3197SDave Hansen 
24242e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
24252e4d0924SHeiko Carstens 		unsigned, dev)
24261da177e4SLinus Torvalds {
24272ad94ae6SAl Viro 	int error;
24281da177e4SLinus Torvalds 	char *tmp;
24291da177e4SLinus Torvalds 	struct dentry *dentry;
24301da177e4SLinus Torvalds 	struct nameidata nd;
24311da177e4SLinus Torvalds 
24321da177e4SLinus Torvalds 	if (S_ISDIR(mode))
24331da177e4SLinus Torvalds 		return -EPERM;
24341da177e4SLinus Torvalds 
24352ad94ae6SAl Viro 	error = user_path_parent(dfd, filename, &nd, &tmp);
24361da177e4SLinus Torvalds 	if (error)
24372ad94ae6SAl Viro 		return error;
24382ad94ae6SAl Viro 
24391da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
2440463c3197SDave Hansen 	if (IS_ERR(dentry)) {
24411da177e4SLinus Torvalds 		error = PTR_ERR(dentry);
2442463c3197SDave Hansen 		goto out_unlock;
2443463c3197SDave Hansen 	}
24444ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2445ce3b0f8dSAl Viro 		mode &= ~current_umask();
2446463c3197SDave Hansen 	error = may_mknod(mode);
2447463c3197SDave Hansen 	if (error)
2448463c3197SDave Hansen 		goto out_dput;
2449463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2450463c3197SDave Hansen 	if (error)
2451463c3197SDave Hansen 		goto out_dput;
2452be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd.path, dentry, mode, dev);
2453be6d3e56SKentaro Takeda 	if (error)
2454be6d3e56SKentaro Takeda 		goto out_drop_write;
24551da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
24561da177e4SLinus Torvalds 		case 0: case S_IFREG:
24574ac91378SJan Blunck 			error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
24581da177e4SLinus Torvalds 			break;
24591da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
24604ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
24611da177e4SLinus Torvalds 					new_decode_dev(dev));
24621da177e4SLinus Torvalds 			break;
24631da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
24644ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
24651da177e4SLinus Torvalds 			break;
24661da177e4SLinus Torvalds 	}
2467be6d3e56SKentaro Takeda out_drop_write:
2468463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2469463c3197SDave Hansen out_dput:
24701da177e4SLinus Torvalds 	dput(dentry);
2471463c3197SDave Hansen out_unlock:
24724ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
24731d957f9bSJan Blunck 	path_put(&nd.path);
24741da177e4SLinus Torvalds 	putname(tmp);
24751da177e4SLinus Torvalds 
24761da177e4SLinus Torvalds 	return error;
24771da177e4SLinus Torvalds }
24781da177e4SLinus Torvalds 
24793480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
24805590ff0dSUlrich Drepper {
24815590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
24825590ff0dSUlrich Drepper }
24835590ff0dSUlrich Drepper 
24841da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
24851da177e4SLinus Torvalds {
2486a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
24871da177e4SLinus Torvalds 
24881da177e4SLinus Torvalds 	if (error)
24891da177e4SLinus Torvalds 		return error;
24901da177e4SLinus Torvalds 
2491acfa4380SAl Viro 	if (!dir->i_op->mkdir)
24921da177e4SLinus Torvalds 		return -EPERM;
24931da177e4SLinus Torvalds 
24941da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
24951da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
24961da177e4SLinus Torvalds 	if (error)
24971da177e4SLinus Torvalds 		return error;
24981da177e4SLinus Torvalds 
24991da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2500a74574aaSStephen Smalley 	if (!error)
2501f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
25021da177e4SLinus Torvalds 	return error;
25031da177e4SLinus Torvalds }
25041da177e4SLinus Torvalds 
25052e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
25061da177e4SLinus Torvalds {
25071da177e4SLinus Torvalds 	int error = 0;
25081da177e4SLinus Torvalds 	char * tmp;
25096902d925SDave Hansen 	struct dentry *dentry;
25106902d925SDave Hansen 	struct nameidata nd;
25111da177e4SLinus Torvalds 
25122ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &tmp);
25132ad94ae6SAl Viro 	if (error)
25146902d925SDave Hansen 		goto out_err;
25151da177e4SLinus Torvalds 
25161da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 1);
25171da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
25186902d925SDave Hansen 	if (IS_ERR(dentry))
25196902d925SDave Hansen 		goto out_unlock;
25206902d925SDave Hansen 
25214ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2522ce3b0f8dSAl Viro 		mode &= ~current_umask();
2523463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2524463c3197SDave Hansen 	if (error)
2525463c3197SDave Hansen 		goto out_dput;
2526be6d3e56SKentaro Takeda 	error = security_path_mkdir(&nd.path, dentry, mode);
2527be6d3e56SKentaro Takeda 	if (error)
2528be6d3e56SKentaro Takeda 		goto out_drop_write;
25294ac91378SJan Blunck 	error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2530be6d3e56SKentaro Takeda out_drop_write:
2531463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2532463c3197SDave Hansen out_dput:
25331da177e4SLinus Torvalds 	dput(dentry);
25346902d925SDave Hansen out_unlock:
25354ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
25361d957f9bSJan Blunck 	path_put(&nd.path);
25371da177e4SLinus Torvalds 	putname(tmp);
25386902d925SDave Hansen out_err:
25391da177e4SLinus Torvalds 	return error;
25401da177e4SLinus Torvalds }
25411da177e4SLinus Torvalds 
25423cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
25435590ff0dSUlrich Drepper {
25445590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
25455590ff0dSUlrich Drepper }
25465590ff0dSUlrich Drepper 
25471da177e4SLinus Torvalds /*
2548a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
2549a71905f0SSage Weil  * should have a usage count of 2 if we're the only user of this
2550a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
2551a71905f0SSage Weil  * then we drop the dentry now.
25521da177e4SLinus Torvalds  *
25531da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
25541da177e4SLinus Torvalds  * do a
25551da177e4SLinus Torvalds  *
25561da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
25571da177e4SLinus Torvalds  *		return -EBUSY;
25581da177e4SLinus Torvalds  *
25591da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
25601da177e4SLinus Torvalds  * that is still in use by something else..
25611da177e4SLinus Torvalds  */
25621da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
25631da177e4SLinus Torvalds {
25641da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
25651da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
256664252c75SSage Weil 	if (dentry->d_count == 1)
25671da177e4SLinus Torvalds 		__d_drop(dentry);
25681da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
25691da177e4SLinus Torvalds }
25701da177e4SLinus Torvalds 
25711da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
25721da177e4SLinus Torvalds {
25731da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
25741da177e4SLinus Torvalds 
25751da177e4SLinus Torvalds 	if (error)
25761da177e4SLinus Torvalds 		return error;
25771da177e4SLinus Torvalds 
2578acfa4380SAl Viro 	if (!dir->i_op->rmdir)
25791da177e4SLinus Torvalds 		return -EPERM;
25801da177e4SLinus Torvalds 
25811b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
2582912dbc15SSage Weil 
25831da177e4SLinus Torvalds 	error = -EBUSY;
2584912dbc15SSage Weil 	if (d_mountpoint(dentry))
2585912dbc15SSage Weil 		goto out;
2586912dbc15SSage Weil 
25871da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
2588912dbc15SSage Weil 	if (error)
2589912dbc15SSage Weil 		goto out;
2590912dbc15SSage Weil 
25913cebde24SSage Weil 	shrink_dcache_parent(dentry);
25921da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
2593912dbc15SSage Weil 	if (error)
2594912dbc15SSage Weil 		goto out;
2595912dbc15SSage Weil 
25961da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
2597d83c49f3SAl Viro 	dont_mount(dentry);
25981da177e4SLinus Torvalds 
2599912dbc15SSage Weil out:
2600912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
2601912dbc15SSage Weil 	if (!error)
2602912dbc15SSage Weil 		d_delete(dentry);
26031da177e4SLinus Torvalds 	return error;
26041da177e4SLinus Torvalds }
26051da177e4SLinus Torvalds 
26065590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
26071da177e4SLinus Torvalds {
26081da177e4SLinus Torvalds 	int error = 0;
26091da177e4SLinus Torvalds 	char * name;
26101da177e4SLinus Torvalds 	struct dentry *dentry;
26111da177e4SLinus Torvalds 	struct nameidata nd;
26121da177e4SLinus Torvalds 
26132ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
26141da177e4SLinus Torvalds 	if (error)
26152ad94ae6SAl Viro 		return error;
26161da177e4SLinus Torvalds 
26171da177e4SLinus Torvalds 	switch(nd.last_type) {
26181da177e4SLinus Torvalds 	case LAST_DOTDOT:
26191da177e4SLinus Torvalds 		error = -ENOTEMPTY;
26201da177e4SLinus Torvalds 		goto exit1;
26211da177e4SLinus Torvalds 	case LAST_DOT:
26221da177e4SLinus Torvalds 		error = -EINVAL;
26231da177e4SLinus Torvalds 		goto exit1;
26241da177e4SLinus Torvalds 	case LAST_ROOT:
26251da177e4SLinus Torvalds 		error = -EBUSY;
26261da177e4SLinus Torvalds 		goto exit1;
26271da177e4SLinus Torvalds 	}
26280612d9fbSOGAWA Hirofumi 
26290612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
26300612d9fbSOGAWA Hirofumi 
26314ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
263249705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
26331da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
26346902d925SDave Hansen 	if (IS_ERR(dentry))
26356902d925SDave Hansen 		goto exit2;
2636e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
2637e6bc45d6STheodore Ts'o 		error = -ENOENT;
2638e6bc45d6STheodore Ts'o 		goto exit3;
2639e6bc45d6STheodore Ts'o 	}
26400622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
26410622753bSDave Hansen 	if (error)
26420622753bSDave Hansen 		goto exit3;
2643be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2644be6d3e56SKentaro Takeda 	if (error)
2645be6d3e56SKentaro Takeda 		goto exit4;
26464ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2647be6d3e56SKentaro Takeda exit4:
26480622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
26490622753bSDave Hansen exit3:
26501da177e4SLinus Torvalds 	dput(dentry);
26516902d925SDave Hansen exit2:
26524ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
26531da177e4SLinus Torvalds exit1:
26541d957f9bSJan Blunck 	path_put(&nd.path);
26551da177e4SLinus Torvalds 	putname(name);
26561da177e4SLinus Torvalds 	return error;
26571da177e4SLinus Torvalds }
26581da177e4SLinus Torvalds 
26593cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
26605590ff0dSUlrich Drepper {
26615590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
26625590ff0dSUlrich Drepper }
26635590ff0dSUlrich Drepper 
26641da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
26651da177e4SLinus Torvalds {
26661da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
26671da177e4SLinus Torvalds 
26681da177e4SLinus Torvalds 	if (error)
26691da177e4SLinus Torvalds 		return error;
26701da177e4SLinus Torvalds 
2671acfa4380SAl Viro 	if (!dir->i_op->unlink)
26721da177e4SLinus Torvalds 		return -EPERM;
26731da177e4SLinus Torvalds 
26741b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
26751da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
26761da177e4SLinus Torvalds 		error = -EBUSY;
26771da177e4SLinus Torvalds 	else {
26781da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2679bec1052eSAl Viro 		if (!error) {
26801da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2681bec1052eSAl Viro 			if (!error)
2682d83c49f3SAl Viro 				dont_mount(dentry);
2683bec1052eSAl Viro 		}
26841da177e4SLinus Torvalds 	}
26851b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
26861da177e4SLinus Torvalds 
26871da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
26881da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2689ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
26901da177e4SLinus Torvalds 		d_delete(dentry);
26911da177e4SLinus Torvalds 	}
26920eeca283SRobert Love 
26931da177e4SLinus Torvalds 	return error;
26941da177e4SLinus Torvalds }
26951da177e4SLinus Torvalds 
26961da177e4SLinus Torvalds /*
26971da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
26981b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
26991da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
27001da177e4SLinus Torvalds  * while waiting on the I/O.
27011da177e4SLinus Torvalds  */
27025590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
27031da177e4SLinus Torvalds {
27042ad94ae6SAl Viro 	int error;
27051da177e4SLinus Torvalds 	char *name;
27061da177e4SLinus Torvalds 	struct dentry *dentry;
27071da177e4SLinus Torvalds 	struct nameidata nd;
27081da177e4SLinus Torvalds 	struct inode *inode = NULL;
27091da177e4SLinus Torvalds 
27102ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
27111da177e4SLinus Torvalds 	if (error)
27122ad94ae6SAl Viro 		return error;
27132ad94ae6SAl Viro 
27141da177e4SLinus Torvalds 	error = -EISDIR;
27151da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
27161da177e4SLinus Torvalds 		goto exit1;
27170612d9fbSOGAWA Hirofumi 
27180612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
27190612d9fbSOGAWA Hirofumi 
27204ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
272149705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
27221da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27231da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
27241da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
272550338b88STörök Edwin 		if (nd.last.name[nd.last.len])
272650338b88STörök Edwin 			goto slashes;
27271da177e4SLinus Torvalds 		inode = dentry->d_inode;
272850338b88STörök Edwin 		if (!inode)
2729e6bc45d6STheodore Ts'o 			goto slashes;
27307de9c6eeSAl Viro 		ihold(inode);
27310622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
27320622753bSDave Hansen 		if (error)
27330622753bSDave Hansen 			goto exit2;
2734be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2735be6d3e56SKentaro Takeda 		if (error)
2736be6d3e56SKentaro Takeda 			goto exit3;
27374ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2738be6d3e56SKentaro Takeda exit3:
27390622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
27401da177e4SLinus Torvalds 	exit2:
27411da177e4SLinus Torvalds 		dput(dentry);
27421da177e4SLinus Torvalds 	}
27434ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27441da177e4SLinus Torvalds 	if (inode)
27451da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
27461da177e4SLinus Torvalds exit1:
27471d957f9bSJan Blunck 	path_put(&nd.path);
27481da177e4SLinus Torvalds 	putname(name);
27491da177e4SLinus Torvalds 	return error;
27501da177e4SLinus Torvalds 
27511da177e4SLinus Torvalds slashes:
27521da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
27531da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
27541da177e4SLinus Torvalds 	goto exit2;
27551da177e4SLinus Torvalds }
27561da177e4SLinus Torvalds 
27572e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
27585590ff0dSUlrich Drepper {
27595590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
27605590ff0dSUlrich Drepper 		return -EINVAL;
27615590ff0dSUlrich Drepper 
27625590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
27635590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
27645590ff0dSUlrich Drepper 
27655590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
27665590ff0dSUlrich Drepper }
27675590ff0dSUlrich Drepper 
27683480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
27695590ff0dSUlrich Drepper {
27705590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
27715590ff0dSUlrich Drepper }
27725590ff0dSUlrich Drepper 
2773db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
27741da177e4SLinus Torvalds {
2775a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
27761da177e4SLinus Torvalds 
27771da177e4SLinus Torvalds 	if (error)
27781da177e4SLinus Torvalds 		return error;
27791da177e4SLinus Torvalds 
2780acfa4380SAl Viro 	if (!dir->i_op->symlink)
27811da177e4SLinus Torvalds 		return -EPERM;
27821da177e4SLinus Torvalds 
27831da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
27841da177e4SLinus Torvalds 	if (error)
27851da177e4SLinus Torvalds 		return error;
27861da177e4SLinus Torvalds 
27871da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2788a74574aaSStephen Smalley 	if (!error)
2789f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
27901da177e4SLinus Torvalds 	return error;
27911da177e4SLinus Torvalds }
27921da177e4SLinus Torvalds 
27932e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
27942e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
27951da177e4SLinus Torvalds {
27962ad94ae6SAl Viro 	int error;
27971da177e4SLinus Torvalds 	char *from;
27981da177e4SLinus Torvalds 	char *to;
27996902d925SDave Hansen 	struct dentry *dentry;
28006902d925SDave Hansen 	struct nameidata nd;
28011da177e4SLinus Torvalds 
28021da177e4SLinus Torvalds 	from = getname(oldname);
28031da177e4SLinus Torvalds 	if (IS_ERR(from))
28041da177e4SLinus Torvalds 		return PTR_ERR(from);
28052ad94ae6SAl Viro 
28062ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
28072ad94ae6SAl Viro 	if (error)
28086902d925SDave Hansen 		goto out_putname;
28091da177e4SLinus Torvalds 
28101da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
28111da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
28126902d925SDave Hansen 	if (IS_ERR(dentry))
28136902d925SDave Hansen 		goto out_unlock;
28146902d925SDave Hansen 
281575c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
281675c3f29dSDave Hansen 	if (error)
281775c3f29dSDave Hansen 		goto out_dput;
2818be6d3e56SKentaro Takeda 	error = security_path_symlink(&nd.path, dentry, from);
2819be6d3e56SKentaro Takeda 	if (error)
2820be6d3e56SKentaro Takeda 		goto out_drop_write;
2821db2e747bSMiklos Szeredi 	error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2822be6d3e56SKentaro Takeda out_drop_write:
282375c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
282475c3f29dSDave Hansen out_dput:
28251da177e4SLinus Torvalds 	dput(dentry);
28266902d925SDave Hansen out_unlock:
28274ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
28281d957f9bSJan Blunck 	path_put(&nd.path);
28291da177e4SLinus Torvalds 	putname(to);
28306902d925SDave Hansen out_putname:
28311da177e4SLinus Torvalds 	putname(from);
28321da177e4SLinus Torvalds 	return error;
28331da177e4SLinus Torvalds }
28341da177e4SLinus Torvalds 
28353480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
28365590ff0dSUlrich Drepper {
28375590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
28385590ff0dSUlrich Drepper }
28395590ff0dSUlrich Drepper 
28401da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
28411da177e4SLinus Torvalds {
28421da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
28431da177e4SLinus Torvalds 	int error;
28441da177e4SLinus Torvalds 
28451da177e4SLinus Torvalds 	if (!inode)
28461da177e4SLinus Torvalds 		return -ENOENT;
28471da177e4SLinus Torvalds 
2848a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
28491da177e4SLinus Torvalds 	if (error)
28501da177e4SLinus Torvalds 		return error;
28511da177e4SLinus Torvalds 
28521da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
28531da177e4SLinus Torvalds 		return -EXDEV;
28541da177e4SLinus Torvalds 
28551da177e4SLinus Torvalds 	/*
28561da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
28571da177e4SLinus Torvalds 	 */
28581da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
28591da177e4SLinus Torvalds 		return -EPERM;
2860acfa4380SAl Viro 	if (!dir->i_op->link)
28611da177e4SLinus Torvalds 		return -EPERM;
28627e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
28631da177e4SLinus Torvalds 		return -EPERM;
28641da177e4SLinus Torvalds 
28651da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
28661da177e4SLinus Torvalds 	if (error)
28671da177e4SLinus Torvalds 		return error;
28681da177e4SLinus Torvalds 
28697e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
2870aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
2871aae8a97dSAneesh Kumar K.V 	if (inode->i_nlink == 0)
2872aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
2873aae8a97dSAneesh Kumar K.V 	else
28741da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
28757e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2876e31e14ecSStephen Smalley 	if (!error)
28777e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
28781da177e4SLinus Torvalds 	return error;
28791da177e4SLinus Torvalds }
28801da177e4SLinus Torvalds 
28811da177e4SLinus Torvalds /*
28821da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
28831da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
28841da177e4SLinus Torvalds  * newname.  --KAB
28851da177e4SLinus Torvalds  *
28861da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
28871da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
28881da177e4SLinus Torvalds  * and other special files.  --ADM
28891da177e4SLinus Torvalds  */
28902e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
28912e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
28921da177e4SLinus Torvalds {
28931da177e4SLinus Torvalds 	struct dentry *new_dentry;
28942d8f3038SAl Viro 	struct nameidata nd;
28952d8f3038SAl Viro 	struct path old_path;
289611a7b371SAneesh Kumar K.V 	int how = 0;
28971da177e4SLinus Torvalds 	int error;
28981da177e4SLinus Torvalds 	char *to;
28991da177e4SLinus Torvalds 
290011a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
2901c04030e1SUlrich Drepper 		return -EINVAL;
290211a7b371SAneesh Kumar K.V 	/*
290311a7b371SAneesh Kumar K.V 	 * To use null names we require CAP_DAC_READ_SEARCH
290411a7b371SAneesh Kumar K.V 	 * This ensures that not everyone will be able to create
290511a7b371SAneesh Kumar K.V 	 * handlink using the passed filedescriptor.
290611a7b371SAneesh Kumar K.V 	 */
290711a7b371SAneesh Kumar K.V 	if (flags & AT_EMPTY_PATH) {
290811a7b371SAneesh Kumar K.V 		if (!capable(CAP_DAC_READ_SEARCH))
290911a7b371SAneesh Kumar K.V 			return -ENOENT;
291011a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
291111a7b371SAneesh Kumar K.V 	}
2912c04030e1SUlrich Drepper 
291311a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
291411a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
291511a7b371SAneesh Kumar K.V 
291611a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
29171da177e4SLinus Torvalds 	if (error)
29182ad94ae6SAl Viro 		return error;
29192ad94ae6SAl Viro 
29202ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
29211da177e4SLinus Torvalds 	if (error)
29221da177e4SLinus Torvalds 		goto out;
29231da177e4SLinus Torvalds 	error = -EXDEV;
29242d8f3038SAl Viro 	if (old_path.mnt != nd.path.mnt)
29251da177e4SLinus Torvalds 		goto out_release;
29261da177e4SLinus Torvalds 	new_dentry = lookup_create(&nd, 0);
29271da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
29286902d925SDave Hansen 	if (IS_ERR(new_dentry))
29296902d925SDave Hansen 		goto out_unlock;
293075c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
293175c3f29dSDave Hansen 	if (error)
293275c3f29dSDave Hansen 		goto out_dput;
2933be6d3e56SKentaro Takeda 	error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2934be6d3e56SKentaro Takeda 	if (error)
2935be6d3e56SKentaro Takeda 		goto out_drop_write;
29362d8f3038SAl Viro 	error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
2937be6d3e56SKentaro Takeda out_drop_write:
293875c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
293975c3f29dSDave Hansen out_dput:
29401da177e4SLinus Torvalds 	dput(new_dentry);
29416902d925SDave Hansen out_unlock:
29424ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
29431da177e4SLinus Torvalds out_release:
29441d957f9bSJan Blunck 	path_put(&nd.path);
29452ad94ae6SAl Viro 	putname(to);
29461da177e4SLinus Torvalds out:
29472d8f3038SAl Viro 	path_put(&old_path);
29481da177e4SLinus Torvalds 
29491da177e4SLinus Torvalds 	return error;
29501da177e4SLinus Torvalds }
29511da177e4SLinus Torvalds 
29523480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
29535590ff0dSUlrich Drepper {
2954c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
29555590ff0dSUlrich Drepper }
29565590ff0dSUlrich Drepper 
29571da177e4SLinus Torvalds /*
29581da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
29591da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
29601da177e4SLinus Torvalds  * Problems:
29611da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
29621da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
29631da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
2964a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
29651da177e4SLinus Torvalds  *	   story.
29661da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
29671b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
29681da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
29691da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
2970a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
29711da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
29721da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
29731da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
2974a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
29751da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
29761da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
29771da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
2978e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
29791b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
29801da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2981c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
29821da177e4SLinus Torvalds  *	   locking].
29831da177e4SLinus Torvalds  */
298475c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
29851da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
29861da177e4SLinus Torvalds {
29871da177e4SLinus Torvalds 	int error = 0;
29889055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
29891da177e4SLinus Torvalds 
29901da177e4SLinus Torvalds 	/*
29911da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
29921da177e4SLinus Torvalds 	 * we'll need to flip '..'.
29931da177e4SLinus Torvalds 	 */
29941da177e4SLinus Torvalds 	if (new_dir != old_dir) {
2995f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
29961da177e4SLinus Torvalds 		if (error)
29971da177e4SLinus Torvalds 			return error;
29981da177e4SLinus Torvalds 	}
29991da177e4SLinus Torvalds 
30001da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30011da177e4SLinus Torvalds 	if (error)
30021da177e4SLinus Torvalds 		return error;
30031da177e4SLinus Torvalds 
3004d83c49f3SAl Viro 	if (target)
30051b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
30069055cba7SSage Weil 
30071da177e4SLinus Torvalds 	error = -EBUSY;
30089055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
30099055cba7SSage Weil 		goto out;
30109055cba7SSage Weil 
30113cebde24SSage Weil 	if (target)
30123cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
30131da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
30149055cba7SSage Weil 	if (error)
30159055cba7SSage Weil 		goto out;
30169055cba7SSage Weil 
30171da177e4SLinus Torvalds 	if (target) {
30181da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
3019d83c49f3SAl Viro 		dont_mount(new_dentry);
3020d83c49f3SAl Viro 	}
30219055cba7SSage Weil out:
30229055cba7SSage Weil 	if (target)
30231b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
3024e31e14ecSStephen Smalley 	if (!error)
3025349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30261da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
30271da177e4SLinus Torvalds 	return error;
30281da177e4SLinus Torvalds }
30291da177e4SLinus Torvalds 
303075c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
30311da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
30321da177e4SLinus Torvalds {
303351892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
30341da177e4SLinus Torvalds 	int error;
30351da177e4SLinus Torvalds 
30361da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30371da177e4SLinus Torvalds 	if (error)
30381da177e4SLinus Torvalds 		return error;
30391da177e4SLinus Torvalds 
30401da177e4SLinus Torvalds 	dget(new_dentry);
30411da177e4SLinus Torvalds 	if (target)
30421b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
304351892bbbSSage Weil 
30441da177e4SLinus Torvalds 	error = -EBUSY;
304551892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
304651892bbbSSage Weil 		goto out;
304751892bbbSSage Weil 
30481da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
304951892bbbSSage Weil 	if (error)
305051892bbbSSage Weil 		goto out;
305151892bbbSSage Weil 
3052bec1052eSAl Viro 	if (target)
3053d83c49f3SAl Viro 		dont_mount(new_dentry);
3054349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30551da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
305651892bbbSSage Weil out:
30571da177e4SLinus Torvalds 	if (target)
30581b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
30591da177e4SLinus Torvalds 	dput(new_dentry);
30601da177e4SLinus Torvalds 	return error;
30611da177e4SLinus Torvalds }
30621da177e4SLinus Torvalds 
30631da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
30641da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
30651da177e4SLinus Torvalds {
30661da177e4SLinus Torvalds 	int error;
30671da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
306859b0df21SEric Paris 	const unsigned char *old_name;
30691da177e4SLinus Torvalds 
30701da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
30711da177e4SLinus Torvalds  		return 0;
30721da177e4SLinus Torvalds 
30731da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
30741da177e4SLinus Torvalds 	if (error)
30751da177e4SLinus Torvalds 		return error;
30761da177e4SLinus Torvalds 
30771da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3078a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
30791da177e4SLinus Torvalds 	else
30801da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
30811da177e4SLinus Torvalds 	if (error)
30821da177e4SLinus Torvalds 		return error;
30831da177e4SLinus Torvalds 
3084acfa4380SAl Viro 	if (!old_dir->i_op->rename)
30851da177e4SLinus Torvalds 		return -EPERM;
30861da177e4SLinus Torvalds 
30870eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
30880eeca283SRobert Love 
30891da177e4SLinus Torvalds 	if (is_dir)
30901da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
30911da177e4SLinus Torvalds 	else
30921da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3093123df294SAl Viro 	if (!error)
3094123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
30955a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
30960eeca283SRobert Love 	fsnotify_oldname_free(old_name);
30970eeca283SRobert Love 
30981da177e4SLinus Torvalds 	return error;
30991da177e4SLinus Torvalds }
31001da177e4SLinus Torvalds 
31012e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
31022e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
31031da177e4SLinus Torvalds {
31041da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
31051da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
31061da177e4SLinus Torvalds 	struct dentry *trap;
31071da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
31082ad94ae6SAl Viro 	char *from;
31092ad94ae6SAl Viro 	char *to;
31102ad94ae6SAl Viro 	int error;
31111da177e4SLinus Torvalds 
31122ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
31131da177e4SLinus Torvalds 	if (error)
31141da177e4SLinus Torvalds 		goto exit;
31151da177e4SLinus Torvalds 
31162ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
31171da177e4SLinus Torvalds 	if (error)
31181da177e4SLinus Torvalds 		goto exit1;
31191da177e4SLinus Torvalds 
31201da177e4SLinus Torvalds 	error = -EXDEV;
31214ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
31221da177e4SLinus Torvalds 		goto exit2;
31231da177e4SLinus Torvalds 
31244ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
31251da177e4SLinus Torvalds 	error = -EBUSY;
31261da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
31271da177e4SLinus Torvalds 		goto exit2;
31281da177e4SLinus Torvalds 
31294ac91378SJan Blunck 	new_dir = newnd.path.dentry;
31301da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
31311da177e4SLinus Torvalds 		goto exit2;
31321da177e4SLinus Torvalds 
31330612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
31340612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
31354e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
31360612d9fbSOGAWA Hirofumi 
31371da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
31381da177e4SLinus Torvalds 
313949705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
31401da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
31411da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
31421da177e4SLinus Torvalds 		goto exit3;
31431da177e4SLinus Torvalds 	/* source must exist */
31441da177e4SLinus Torvalds 	error = -ENOENT;
31451da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
31461da177e4SLinus Torvalds 		goto exit4;
31471da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
31481da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
31491da177e4SLinus Torvalds 		error = -ENOTDIR;
31501da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
31511da177e4SLinus Torvalds 			goto exit4;
31521da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
31531da177e4SLinus Torvalds 			goto exit4;
31541da177e4SLinus Torvalds 	}
31551da177e4SLinus Torvalds 	/* source should not be ancestor of target */
31561da177e4SLinus Torvalds 	error = -EINVAL;
31571da177e4SLinus Torvalds 	if (old_dentry == trap)
31581da177e4SLinus Torvalds 		goto exit4;
315949705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
31601da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
31611da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
31621da177e4SLinus Torvalds 		goto exit4;
31631da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
31641da177e4SLinus Torvalds 	error = -ENOTEMPTY;
31651da177e4SLinus Torvalds 	if (new_dentry == trap)
31661da177e4SLinus Torvalds 		goto exit5;
31671da177e4SLinus Torvalds 
31689079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
31699079b1ebSDave Hansen 	if (error)
31709079b1ebSDave Hansen 		goto exit5;
3171be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3172be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3173be6d3e56SKentaro Takeda 	if (error)
3174be6d3e56SKentaro Takeda 		goto exit6;
31751da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
31761da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3177be6d3e56SKentaro Takeda exit6:
31789079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
31791da177e4SLinus Torvalds exit5:
31801da177e4SLinus Torvalds 	dput(new_dentry);
31811da177e4SLinus Torvalds exit4:
31821da177e4SLinus Torvalds 	dput(old_dentry);
31831da177e4SLinus Torvalds exit3:
31841da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
31851da177e4SLinus Torvalds exit2:
31861d957f9bSJan Blunck 	path_put(&newnd.path);
31872ad94ae6SAl Viro 	putname(to);
31881da177e4SLinus Torvalds exit1:
31891d957f9bSJan Blunck 	path_put(&oldnd.path);
31901da177e4SLinus Torvalds 	putname(from);
31912ad94ae6SAl Viro exit:
31921da177e4SLinus Torvalds 	return error;
31931da177e4SLinus Torvalds }
31941da177e4SLinus Torvalds 
3195a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
31965590ff0dSUlrich Drepper {
31975590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
31985590ff0dSUlrich Drepper }
31995590ff0dSUlrich Drepper 
32001da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
32011da177e4SLinus Torvalds {
32021da177e4SLinus Torvalds 	int len;
32031da177e4SLinus Torvalds 
32041da177e4SLinus Torvalds 	len = PTR_ERR(link);
32051da177e4SLinus Torvalds 	if (IS_ERR(link))
32061da177e4SLinus Torvalds 		goto out;
32071da177e4SLinus Torvalds 
32081da177e4SLinus Torvalds 	len = strlen(link);
32091da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
32101da177e4SLinus Torvalds 		len = buflen;
32111da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
32121da177e4SLinus Torvalds 		len = -EFAULT;
32131da177e4SLinus Torvalds out:
32141da177e4SLinus Torvalds 	return len;
32151da177e4SLinus Torvalds }
32161da177e4SLinus Torvalds 
32171da177e4SLinus Torvalds /*
32181da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
32191da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
32201da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
32211da177e4SLinus Torvalds  */
32221da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32231da177e4SLinus Torvalds {
32241da177e4SLinus Torvalds 	struct nameidata nd;
3225cc314eefSLinus Torvalds 	void *cookie;
3226694a1764SMarcin Slusarz 	int res;
3227cc314eefSLinus Torvalds 
32281da177e4SLinus Torvalds 	nd.depth = 0;
3229cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3230694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3231694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3232694a1764SMarcin Slusarz 
3233694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
32341da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3235cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3236694a1764SMarcin Slusarz 	return res;
32371da177e4SLinus Torvalds }
32381da177e4SLinus Torvalds 
32391da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
32401da177e4SLinus Torvalds {
32411da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
32421da177e4SLinus Torvalds }
32431da177e4SLinus Torvalds 
32441da177e4SLinus Torvalds /* get the link contents into pagecache */
32451da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
32461da177e4SLinus Torvalds {
3247ebd09abbSDuane Griffin 	char *kaddr;
32481da177e4SLinus Torvalds 	struct page *page;
32491da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3250090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
32511da177e4SLinus Torvalds 	if (IS_ERR(page))
32526fe6900eSNick Piggin 		return (char*)page;
32531da177e4SLinus Torvalds 	*ppage = page;
3254ebd09abbSDuane Griffin 	kaddr = kmap(page);
3255ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3256ebd09abbSDuane Griffin 	return kaddr;
32571da177e4SLinus Torvalds }
32581da177e4SLinus Torvalds 
32591da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32601da177e4SLinus Torvalds {
32611da177e4SLinus Torvalds 	struct page *page = NULL;
32621da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
32631da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
32641da177e4SLinus Torvalds 	if (page) {
32651da177e4SLinus Torvalds 		kunmap(page);
32661da177e4SLinus Torvalds 		page_cache_release(page);
32671da177e4SLinus Torvalds 	}
32681da177e4SLinus Torvalds 	return res;
32691da177e4SLinus Torvalds }
32701da177e4SLinus Torvalds 
3271cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
32721da177e4SLinus Torvalds {
3273cc314eefSLinus Torvalds 	struct page *page = NULL;
32741da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3275cc314eefSLinus Torvalds 	return page;
32761da177e4SLinus Torvalds }
32771da177e4SLinus Torvalds 
3278cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
32791da177e4SLinus Torvalds {
3280cc314eefSLinus Torvalds 	struct page *page = cookie;
3281cc314eefSLinus Torvalds 
3282cc314eefSLinus Torvalds 	if (page) {
32831da177e4SLinus Torvalds 		kunmap(page);
32841da177e4SLinus Torvalds 		page_cache_release(page);
32851da177e4SLinus Torvalds 	}
32861da177e4SLinus Torvalds }
32871da177e4SLinus Torvalds 
328854566b2cSNick Piggin /*
328954566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
329054566b2cSNick Piggin  */
329154566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
32921da177e4SLinus Torvalds {
32931da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
32940adb25d2SKirill Korotaev 	struct page *page;
3295afddba49SNick Piggin 	void *fsdata;
3296beb497abSDmitriy Monakhov 	int err;
32971da177e4SLinus Torvalds 	char *kaddr;
329854566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
329954566b2cSNick Piggin 	if (nofs)
330054566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
33011da177e4SLinus Torvalds 
33027e53cac4SNeilBrown retry:
3303afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
330454566b2cSNick Piggin 				flags, &page, &fsdata);
33051da177e4SLinus Torvalds 	if (err)
3306afddba49SNick Piggin 		goto fail;
3307afddba49SNick Piggin 
33081da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
33091da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
33101da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
3311afddba49SNick Piggin 
3312afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3313afddba49SNick Piggin 							page, fsdata);
33141da177e4SLinus Torvalds 	if (err < 0)
33151da177e4SLinus Torvalds 		goto fail;
3316afddba49SNick Piggin 	if (err < len-1)
3317afddba49SNick Piggin 		goto retry;
3318afddba49SNick Piggin 
33191da177e4SLinus Torvalds 	mark_inode_dirty(inode);
33201da177e4SLinus Torvalds 	return 0;
33211da177e4SLinus Torvalds fail:
33221da177e4SLinus Torvalds 	return err;
33231da177e4SLinus Torvalds }
33241da177e4SLinus Torvalds 
33250adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
33260adb25d2SKirill Korotaev {
33270adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
332854566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
33290adb25d2SKirill Korotaev }
33300adb25d2SKirill Korotaev 
333192e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
33321da177e4SLinus Torvalds 	.readlink	= generic_readlink,
33331da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
33341da177e4SLinus Torvalds 	.put_link	= page_put_link,
33351da177e4SLinus Torvalds };
33361da177e4SLinus Torvalds 
33372d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3338cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
33391da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
33401da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
33411da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
33421da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
33431da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
33441da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
33451da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
33461da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
33471da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
33480adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
33491da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
33501da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3351c9c6cac0SAl Viro EXPORT_SYMBOL(kern_path_parent);
3352d1811465SAl Viro EXPORT_SYMBOL(kern_path);
335316f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3354f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
33551da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
33561da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
33571da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
33581da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
33591da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
33601da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
33611da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
33621da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
33631da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
33641da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
33651da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
33661da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
33671da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
33681da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3369