xref: /openbmc/linux/fs/namei.c (revision f3c7691e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/namei.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  * Some corrections by tytso.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
121da177e4SLinus Torvalds  * lookup logic.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/init.h>
181da177e4SLinus Torvalds #include <linux/module.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/fs.h>
211da177e4SLinus Torvalds #include <linux/namei.h>
221da177e4SLinus Torvalds #include <linux/pagemap.h>
230eeca283SRobert Love #include <linux/fsnotify.h>
241da177e4SLinus Torvalds #include <linux/personality.h>
251da177e4SLinus Torvalds #include <linux/security.h>
266146f0d5SMimi Zohar #include <linux/ima.h>
271da177e4SLinus Torvalds #include <linux/syscalls.h>
281da177e4SLinus Torvalds #include <linux/mount.h>
291da177e4SLinus Torvalds #include <linux/audit.h>
3016f7e0feSRandy Dunlap #include <linux/capability.h>
31834f2a4aSTrond Myklebust #include <linux/file.h>
325590ff0dSUlrich Drepper #include <linux/fcntl.h>
3308ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h>
345ad4e53bSAl Viro #include <linux/fs_struct.h>
35e77819e5SLinus Torvalds #include <linux/posix_acl.h>
361da177e4SLinus Torvalds #include <asm/uaccess.h>
371da177e4SLinus Torvalds 
38e81e3f4dSEric Paris #include "internal.h"
39e81e3f4dSEric Paris 
401da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer]
411da177e4SLinus Torvalds  * Fundamental changes in the pathname lookup mechanisms (namei)
421da177e4SLinus Torvalds  * were necessary because of omirr.  The reason is that omirr needs
431da177e4SLinus Torvalds  * to know the _real_ pathname, not the user-supplied one, in case
441da177e4SLinus Torvalds  * of symlinks (and also when transname replacements occur).
451da177e4SLinus Torvalds  *
461da177e4SLinus Torvalds  * The new code replaces the old recursive symlink resolution with
471da177e4SLinus Torvalds  * an iterative one (in case of non-nested symlink chains).  It does
481da177e4SLinus Torvalds  * this with calls to <fs>_follow_link().
491da177e4SLinus Torvalds  * As a side effect, dir_namei(), _namei() and follow_link() are now
501da177e4SLinus Torvalds  * replaced with a single function lookup_dentry() that can handle all
511da177e4SLinus Torvalds  * the special cases of the former code.
521da177e4SLinus Torvalds  *
531da177e4SLinus Torvalds  * With the new dcache, the pathname is stored at each inode, at least as
541da177e4SLinus Torvalds  * long as the refcount of the inode is positive.  As a side effect, the
551da177e4SLinus Torvalds  * size of the dcache depends on the inode cache and thus is dynamic.
561da177e4SLinus Torvalds  *
571da177e4SLinus Torvalds  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
581da177e4SLinus Torvalds  * resolution to correspond with current state of the code.
591da177e4SLinus Torvalds  *
601da177e4SLinus Torvalds  * Note that the symlink resolution is not *completely* iterative.
611da177e4SLinus Torvalds  * There is still a significant amount of tail- and mid- recursion in
621da177e4SLinus Torvalds  * the algorithm.  Also, note that <fs>_readlink() is not used in
631da177e4SLinus Torvalds  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
641da177e4SLinus Torvalds  * may return different results than <fs>_follow_link().  Many virtual
651da177e4SLinus Torvalds  * filesystems (including /proc) exhibit this behavior.
661da177e4SLinus Torvalds  */
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
691da177e4SLinus Torvalds  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
701da177e4SLinus Torvalds  * and the name already exists in form of a symlink, try to create the new
711da177e4SLinus Torvalds  * name indicated by the symlink. The old code always complained that the
721da177e4SLinus Torvalds  * name already exists, due to not following the symlink even if its target
731da177e4SLinus Torvalds  * is nonexistent.  The new semantics affects also mknod() and link() when
7425985edcSLucas De Marchi  * the name is a symlink pointing to a non-existent name.
751da177e4SLinus Torvalds  *
761da177e4SLinus Torvalds  * I don't know which semantics is the right one, since I have no access
771da177e4SLinus Torvalds  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
781da177e4SLinus Torvalds  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
791da177e4SLinus Torvalds  * "old" one. Personally, I think the new semantics is much more logical.
801da177e4SLinus Torvalds  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
811da177e4SLinus Torvalds  * file does succeed in both HP-UX and SunOs, but not in Solaris
821da177e4SLinus Torvalds  * and in the old Linux semantics.
831da177e4SLinus Torvalds  */
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
861da177e4SLinus Torvalds  * semantics.  See the comments in "open_namei" and "do_link" below.
871da177e4SLinus Torvalds  *
881da177e4SLinus Torvalds  * [10-Sep-98 Alan Modra] Another symlink change.
891da177e4SLinus Torvalds  */
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
921da177e4SLinus Torvalds  *	inside the path - always follow.
931da177e4SLinus Torvalds  *	in the last component in creation/removal/renaming - never follow.
941da177e4SLinus Torvalds  *	if LOOKUP_FOLLOW passed - follow.
951da177e4SLinus Torvalds  *	if the pathname has trailing slashes - follow.
961da177e4SLinus Torvalds  *	otherwise - don't follow.
971da177e4SLinus Torvalds  * (applied in that order).
981da177e4SLinus Torvalds  *
991da177e4SLinus Torvalds  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
1001da177e4SLinus Torvalds  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
1011da177e4SLinus Torvalds  * During the 2.4 we need to fix the userland stuff depending on it -
1021da177e4SLinus Torvalds  * hopefully we will be able to get rid of that wart in 2.5. So far only
1031da177e4SLinus Torvalds  * XEmacs seems to be relying on it...
1041da177e4SLinus Torvalds  */
1051da177e4SLinus Torvalds /*
1061da177e4SLinus Torvalds  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
107a11f3a05SArjan van de Ven  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1081da177e4SLinus Torvalds  * any extra contention...
1091da177e4SLinus Torvalds  */
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional
1121da177e4SLinus Torvalds  * checking and hopefully speeding things up, we copy filenames to the
1131da177e4SLinus Torvalds  * kernel data space before using them..
1141da177e4SLinus Torvalds  *
1151da177e4SLinus Torvalds  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
1161da177e4SLinus Torvalds  * PATH_MAX includes the nul terminator --RR.
1171da177e4SLinus Torvalds  */
118858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page)
1191da177e4SLinus Torvalds {
1201da177e4SLinus Torvalds 	int retval;
1211da177e4SLinus Torvalds 	unsigned long len = PATH_MAX;
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	if (!segment_eq(get_fs(), KERNEL_DS)) {
1241da177e4SLinus Torvalds 		if ((unsigned long) filename >= TASK_SIZE)
1251da177e4SLinus Torvalds 			return -EFAULT;
1261da177e4SLinus Torvalds 		if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
1271da177e4SLinus Torvalds 			len = TASK_SIZE - (unsigned long) filename;
1281da177e4SLinus Torvalds 	}
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	retval = strncpy_from_user(page, filename, len);
1311da177e4SLinus Torvalds 	if (retval > 0) {
1321da177e4SLinus Torvalds 		if (retval < len)
1331da177e4SLinus Torvalds 			return 0;
1341da177e4SLinus Torvalds 		return -ENAMETOOLONG;
1351da177e4SLinus Torvalds 	} else if (!retval)
1361da177e4SLinus Torvalds 		retval = -ENOENT;
1371da177e4SLinus Torvalds 	return retval;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
140f52e0c11SAl Viro static char *getname_flags(const char __user * filename, int flags)
1411da177e4SLinus Torvalds {
1421da177e4SLinus Torvalds 	char *tmp, *result;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	result = ERR_PTR(-ENOMEM);
1451da177e4SLinus Torvalds 	tmp = __getname();
1461da177e4SLinus Torvalds 	if (tmp)  {
1471da177e4SLinus Torvalds 		int retval = do_getname(filename, tmp);
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 		result = tmp;
1501da177e4SLinus Torvalds 		if (retval < 0) {
151f52e0c11SAl Viro 			if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
1521da177e4SLinus Torvalds 				__putname(tmp);
1531da177e4SLinus Torvalds 				result = ERR_PTR(retval);
1541da177e4SLinus Torvalds 			}
1551da177e4SLinus Torvalds 		}
156f52e0c11SAl Viro 	}
1571da177e4SLinus Torvalds 	audit_getname(result);
1581da177e4SLinus Torvalds 	return result;
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds 
161f52e0c11SAl Viro char *getname(const char __user * filename)
162f52e0c11SAl Viro {
163f52e0c11SAl Viro 	return getname_flags(filename, 0);
164f52e0c11SAl Viro }
165f52e0c11SAl Viro 
1661da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
1671da177e4SLinus Torvalds void putname(const char *name)
1681da177e4SLinus Torvalds {
1695ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
1701da177e4SLinus Torvalds 		audit_putname(name);
1711da177e4SLinus Torvalds 	else
1721da177e4SLinus Torvalds 		__putname(name);
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds EXPORT_SYMBOL(putname);
1751da177e4SLinus Torvalds #endif
1761da177e4SLinus Torvalds 
177e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask)
178e77819e5SLinus Torvalds {
17984635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL
180e77819e5SLinus Torvalds 	struct posix_acl *acl;
181e77819e5SLinus Torvalds 
182e77819e5SLinus Torvalds 	if (mask & MAY_NOT_BLOCK) {
1833567866bSAl Viro 		acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
1843567866bSAl Viro 	        if (!acl)
185e77819e5SLinus Torvalds 	                return -EAGAIN;
1863567866bSAl Viro 		/* no ->get_acl() calls in RCU mode... */
1873567866bSAl Viro 		if (acl == ACL_NOT_CACHED)
188e77819e5SLinus Torvalds 			return -ECHILD;
189206b1d09SAri Savolainen 	        return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
190e77819e5SLinus Torvalds 	}
191e77819e5SLinus Torvalds 
192e77819e5SLinus Torvalds 	acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
193e77819e5SLinus Torvalds 
194e77819e5SLinus Torvalds 	/*
1954e34e719SChristoph Hellwig 	 * A filesystem can force a ACL callback by just never filling the
1964e34e719SChristoph Hellwig 	 * ACL cache. But normally you'd fill the cache either at inode
1974e34e719SChristoph Hellwig 	 * instantiation time, or on the first ->get_acl call.
198e77819e5SLinus Torvalds 	 *
1994e34e719SChristoph Hellwig 	 * If the filesystem doesn't have a get_acl() function at all, we'll
2004e34e719SChristoph Hellwig 	 * just create the negative cache entry.
201e77819e5SLinus Torvalds 	 */
202e77819e5SLinus Torvalds 	if (acl == ACL_NOT_CACHED) {
2034e34e719SChristoph Hellwig 	        if (inode->i_op->get_acl) {
2044e34e719SChristoph Hellwig 			acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
2054e34e719SChristoph Hellwig 			if (IS_ERR(acl))
2064e34e719SChristoph Hellwig 				return PTR_ERR(acl);
2074e34e719SChristoph Hellwig 		} else {
208e77819e5SLinus Torvalds 		        set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
209e77819e5SLinus Torvalds 		        return -EAGAIN;
210e77819e5SLinus Torvalds 		}
2114e34e719SChristoph Hellwig 	}
212e77819e5SLinus Torvalds 
213e77819e5SLinus Torvalds 	if (acl) {
214e77819e5SLinus Torvalds 	        int error = posix_acl_permission(inode, acl, mask);
215e77819e5SLinus Torvalds 	        posix_acl_release(acl);
216e77819e5SLinus Torvalds 	        return error;
217e77819e5SLinus Torvalds 	}
21884635d68SLinus Torvalds #endif
219e77819e5SLinus Torvalds 
220e77819e5SLinus Torvalds 	return -EAGAIN;
221e77819e5SLinus Torvalds }
222e77819e5SLinus Torvalds 
2235909ccaaSLinus Torvalds /*
224948409c7SAndreas Gruenbacher  * This does the basic permission checking
2255909ccaaSLinus Torvalds  */
2267e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask)
2275909ccaaSLinus Torvalds {
22826cf46beSLinus Torvalds 	unsigned int mode = inode->i_mode;
2295909ccaaSLinus Torvalds 
230e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
231e795b717SSerge E. Hallyn 		goto other_perms;
232e795b717SSerge E. Hallyn 
23314067ff5SLinus Torvalds 	if (likely(current_fsuid() == inode->i_uid))
2345909ccaaSLinus Torvalds 		mode >>= 6;
2355909ccaaSLinus Torvalds 	else {
236e77819e5SLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
2377e40145eSAl Viro 			int error = check_acl(inode, mask);
2385909ccaaSLinus Torvalds 			if (error != -EAGAIN)
2395909ccaaSLinus Torvalds 				return error;
2405909ccaaSLinus Torvalds 		}
2415909ccaaSLinus Torvalds 
2425909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
2435909ccaaSLinus Torvalds 			mode >>= 3;
2445909ccaaSLinus Torvalds 	}
2455909ccaaSLinus Torvalds 
246e795b717SSerge E. Hallyn other_perms:
2475909ccaaSLinus Torvalds 	/*
2485909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
2495909ccaaSLinus Torvalds 	 */
2509c2c7039SAl Viro 	if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
2515909ccaaSLinus Torvalds 		return 0;
2525909ccaaSLinus Torvalds 	return -EACCES;
2535909ccaaSLinus Torvalds }
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds /**
2561da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
2571da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2588fd90c8dSAndreas Gruenbacher  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
2591da177e4SLinus Torvalds  *
2601da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
2611da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
2621da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
263b74c79e9SNick Piggin  * are used for other things.
264b74c79e9SNick Piggin  *
265b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
266b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
267b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
2681da177e4SLinus Torvalds  */
2692830ba7fSAl Viro int generic_permission(struct inode *inode, int mask)
2701da177e4SLinus Torvalds {
2715909ccaaSLinus Torvalds 	int ret;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	/*
274948409c7SAndreas Gruenbacher 	 * Do the basic permission checks.
2751da177e4SLinus Torvalds 	 */
2767e40145eSAl Viro 	ret = acl_permission_check(inode, mask);
2775909ccaaSLinus Torvalds 	if (ret != -EACCES)
2785909ccaaSLinus Torvalds 		return ret;
2791da177e4SLinus Torvalds 
280d594e7ecSAl Viro 	if (S_ISDIR(inode->i_mode)) {
281d594e7ecSAl Viro 		/* DACs are overridable for directories */
282d594e7ecSAl Viro 		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
283d594e7ecSAl Viro 			return 0;
284d594e7ecSAl Viro 		if (!(mask & MAY_WRITE))
285d594e7ecSAl Viro 			if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
286d594e7ecSAl Viro 				return 0;
287d594e7ecSAl Viro 		return -EACCES;
288d594e7ecSAl Viro 	}
2891da177e4SLinus Torvalds 	/*
2901da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
291d594e7ecSAl Viro 	 * Executable DACs are overridable when there is
292d594e7ecSAl Viro 	 * at least one exec bit set.
2931da177e4SLinus Torvalds 	 */
294d594e7ecSAl Viro 	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
295e795b717SSerge E. Hallyn 		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
2961da177e4SLinus Torvalds 			return 0;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	/*
2991da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
3001da177e4SLinus Torvalds 	 */
3017ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
302d594e7ecSAl Viro 	if (mask == MAY_READ)
303e795b717SSerge E. Hallyn 		if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
3041da177e4SLinus Torvalds 			return 0;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	return -EACCES;
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
3093ddcd056SLinus Torvalds /*
3103ddcd056SLinus Torvalds  * We _really_ want to just do "generic_permission()" without
3113ddcd056SLinus Torvalds  * even looking at the inode->i_op values. So we keep a cache
3123ddcd056SLinus Torvalds  * flag in inode->i_opflags, that says "this has not special
3133ddcd056SLinus Torvalds  * permission function, use the fast case".
3143ddcd056SLinus Torvalds  */
3153ddcd056SLinus Torvalds static inline int do_inode_permission(struct inode *inode, int mask)
3163ddcd056SLinus Torvalds {
3173ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
3183ddcd056SLinus Torvalds 		if (likely(inode->i_op->permission))
3193ddcd056SLinus Torvalds 			return inode->i_op->permission(inode, mask);
3203ddcd056SLinus Torvalds 
3213ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
3223ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
3233ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_FASTPERM;
3243ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
3253ddcd056SLinus Torvalds 	}
3263ddcd056SLinus Torvalds 	return generic_permission(inode, mask);
3273ddcd056SLinus Torvalds }
3283ddcd056SLinus Torvalds 
329cb23beb5SChristoph Hellwig /**
330cb23beb5SChristoph Hellwig  * inode_permission  -  check for access rights to a given inode
331cb23beb5SChristoph Hellwig  * @inode:	inode to check permission on
3328fd90c8dSAndreas Gruenbacher  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
333cb23beb5SChristoph Hellwig  *
334cb23beb5SChristoph Hellwig  * Used to check for read/write/execute permissions on an inode.
335cb23beb5SChristoph Hellwig  * We use "fsuid" for this, letting us set arbitrary permissions
336cb23beb5SChristoph Hellwig  * for filesystem access without changing the "normal" uids which
337cb23beb5SChristoph Hellwig  * are used for other things.
338948409c7SAndreas Gruenbacher  *
339948409c7SAndreas Gruenbacher  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
340cb23beb5SChristoph Hellwig  */
341f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask)
3421da177e4SLinus Torvalds {
343e6305c43SAl Viro 	int retval;
3441da177e4SLinus Torvalds 
3453ddcd056SLinus Torvalds 	if (unlikely(mask & MAY_WRITE)) {
34622590e41SMiklos Szeredi 		umode_t mode = inode->i_mode;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 		/*
3491da177e4SLinus Torvalds 		 * Nobody gets write access to a read-only fs.
3501da177e4SLinus Torvalds 		 */
3511da177e4SLinus Torvalds 		if (IS_RDONLY(inode) &&
3521da177e4SLinus Torvalds 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
3531da177e4SLinus Torvalds 			return -EROFS;
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 		/*
3561da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
3571da177e4SLinus Torvalds 		 */
3581da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
3591da177e4SLinus Torvalds 			return -EACCES;
3601da177e4SLinus Torvalds 	}
3611da177e4SLinus Torvalds 
3623ddcd056SLinus Torvalds 	retval = do_inode_permission(inode, mask);
3631da177e4SLinus Torvalds 	if (retval)
3641da177e4SLinus Torvalds 		return retval;
3651da177e4SLinus Torvalds 
36608ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
36708ce5f16SSerge E. Hallyn 	if (retval)
36808ce5f16SSerge E. Hallyn 		return retval;
36908ce5f16SSerge E. Hallyn 
370d09ca739SEric Paris 	return security_inode_permission(inode, mask);
3711da177e4SLinus Torvalds }
3721da177e4SLinus Torvalds 
373f4d6ff89SAl Viro /**
3745dd784d0SJan Blunck  * path_get - get a reference to a path
3755dd784d0SJan Blunck  * @path: path to get the reference to
3765dd784d0SJan Blunck  *
3775dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3785dd784d0SJan Blunck  */
3795dd784d0SJan Blunck void path_get(struct path *path)
3805dd784d0SJan Blunck {
3815dd784d0SJan Blunck 	mntget(path->mnt);
3825dd784d0SJan Blunck 	dget(path->dentry);
3835dd784d0SJan Blunck }
3845dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
3855dd784d0SJan Blunck 
3865dd784d0SJan Blunck /**
3871d957f9bSJan Blunck  * path_put - put a reference to a path
3881d957f9bSJan Blunck  * @path: path to put the reference to
3891d957f9bSJan Blunck  *
3901d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
3911d957f9bSJan Blunck  */
3921d957f9bSJan Blunck void path_put(struct path *path)
3931da177e4SLinus Torvalds {
3941d957f9bSJan Blunck 	dput(path->dentry);
3951d957f9bSJan Blunck 	mntput(path->mnt);
3961da177e4SLinus Torvalds }
3971d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
3981da177e4SLinus Torvalds 
39919660af7SAl Viro /*
40031e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
40119660af7SAl Viro  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
40219660af7SAl Viro  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
40319660af7SAl Viro  * normal reference counts on dentries and vfsmounts to transition to rcu-walk
40419660af7SAl Viro  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
40519660af7SAl Viro  * got stuck, so ref-walk may continue from there. If this is not successful
40619660af7SAl Viro  * (eg. a seqcount has changed), then failure is returned and it's up to caller
40719660af7SAl Viro  * to restart the path walk from the beginning in ref-walk mode.
40831e6b01fSNick Piggin  */
40931e6b01fSNick Piggin 
41031e6b01fSNick Piggin /**
41119660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
41219660af7SAl Viro  * @nd: nameidata pathwalk data
41319660af7SAl Viro  * @dentry: child of nd->path.dentry or NULL
41439191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
41531e6b01fSNick Piggin  *
41619660af7SAl Viro  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
41719660af7SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
41819660af7SAl Viro  * @nd or NULL.  Must be called from rcu-walk context.
41931e6b01fSNick Piggin  */
42019660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
42131e6b01fSNick Piggin {
42231e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
42331e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
4245b6ca027SAl Viro 	int want_root = 0;
42531e6b01fSNick Piggin 
42631e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
4275b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
4285b6ca027SAl Viro 		want_root = 1;
42931e6b01fSNick Piggin 		spin_lock(&fs->lock);
43031e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
43131e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
43231e6b01fSNick Piggin 			goto err_root;
43331e6b01fSNick Piggin 	}
43431e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
43519660af7SAl Viro 	if (!dentry) {
43619660af7SAl Viro 		if (!__d_rcu_to_refcount(parent, nd->seq))
43719660af7SAl Viro 			goto err_parent;
43819660af7SAl Viro 		BUG_ON(nd->inode != parent->d_inode);
43919660af7SAl Viro 	} else {
44094c0d4ecSAl Viro 		if (dentry->d_parent != parent)
44194c0d4ecSAl Viro 			goto err_parent;
44231e6b01fSNick Piggin 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
44331e6b01fSNick Piggin 		if (!__d_rcu_to_refcount(dentry, nd->seq))
44419660af7SAl Viro 			goto err_child;
44531e6b01fSNick Piggin 		/*
44619660af7SAl Viro 		 * If the sequence check on the child dentry passed, then
44719660af7SAl Viro 		 * the child has not been removed from its parent. This
44819660af7SAl Viro 		 * means the parent dentry must be valid and able to take
44919660af7SAl Viro 		 * a reference at this point.
45031e6b01fSNick Piggin 		 */
45131e6b01fSNick Piggin 		BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
45231e6b01fSNick Piggin 		BUG_ON(!parent->d_count);
45331e6b01fSNick Piggin 		parent->d_count++;
45431e6b01fSNick Piggin 		spin_unlock(&dentry->d_lock);
45519660af7SAl Viro 	}
45631e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
4575b6ca027SAl Viro 	if (want_root) {
45831e6b01fSNick Piggin 		path_get(&nd->root);
45931e6b01fSNick Piggin 		spin_unlock(&fs->lock);
46031e6b01fSNick Piggin 	}
46131e6b01fSNick Piggin 	mntget(nd->path.mnt);
46231e6b01fSNick Piggin 
46331e6b01fSNick Piggin 	rcu_read_unlock();
46431e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
46531e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
46631e6b01fSNick Piggin 	return 0;
46719660af7SAl Viro 
46819660af7SAl Viro err_child:
46931e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
47019660af7SAl Viro err_parent:
47131e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
47231e6b01fSNick Piggin err_root:
4735b6ca027SAl Viro 	if (want_root)
47431e6b01fSNick Piggin 		spin_unlock(&fs->lock);
47531e6b01fSNick Piggin 	return -ECHILD;
47631e6b01fSNick Piggin }
47731e6b01fSNick Piggin 
47831e6b01fSNick Piggin /**
479834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
480834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
481834f2a4aSTrond Myklebust  */
482834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
483834f2a4aSTrond Myklebust {
4842dab5974SLinus Torvalds 	struct file *file = nd->intent.open.file;
4852dab5974SLinus Torvalds 
4862dab5974SLinus Torvalds 	if (file && !IS_ERR(file)) {
4872dab5974SLinus Torvalds 		if (file->f_path.dentry == NULL)
4882dab5974SLinus Torvalds 			put_filp(file);
489834f2a4aSTrond Myklebust 		else
4902dab5974SLinus Torvalds 			fput(file);
4912dab5974SLinus Torvalds 	}
492834f2a4aSTrond Myklebust }
493834f2a4aSTrond Myklebust 
494f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
49534286d66SNick Piggin {
496f60aef7eSAl Viro 	return dentry->d_op->d_revalidate(dentry, nd);
49734286d66SNick Piggin }
49834286d66SNick Piggin 
4999f1fafeeSAl Viro /**
5009f1fafeeSAl Viro  * complete_walk - successful completion of path walk
5019f1fafeeSAl Viro  * @nd:  pointer nameidata
50239159de2SJeff Layton  *
5039f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
5049f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
5059f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
5069f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
5079f1fafeeSAl Viro  * need to drop nd->path.
50839159de2SJeff Layton  */
5099f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
51039159de2SJeff Layton {
51116c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
51239159de2SJeff Layton 	int status;
51339159de2SJeff Layton 
5149f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
5159f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
5169f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
5179f1fafeeSAl Viro 			nd->root.mnt = NULL;
5189f1fafeeSAl Viro 		spin_lock(&dentry->d_lock);
5199f1fafeeSAl Viro 		if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
5209f1fafeeSAl Viro 			spin_unlock(&dentry->d_lock);
5219f1fafeeSAl Viro 			rcu_read_unlock();
5229f1fafeeSAl Viro 			br_read_unlock(vfsmount_lock);
5239f1fafeeSAl Viro 			return -ECHILD;
5249f1fafeeSAl Viro 		}
5259f1fafeeSAl Viro 		BUG_ON(nd->inode != dentry->d_inode);
5269f1fafeeSAl Viro 		spin_unlock(&dentry->d_lock);
5279f1fafeeSAl Viro 		mntget(nd->path.mnt);
5289f1fafeeSAl Viro 		rcu_read_unlock();
5299f1fafeeSAl Viro 		br_read_unlock(vfsmount_lock);
5309f1fafeeSAl Viro 	}
5319f1fafeeSAl Viro 
53216c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
53339159de2SJeff Layton 		return 0;
53439159de2SJeff Layton 
53516c2cd71SAl Viro 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
53616c2cd71SAl Viro 		return 0;
53716c2cd71SAl Viro 
53816c2cd71SAl Viro 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
53916c2cd71SAl Viro 		return 0;
54016c2cd71SAl Viro 
54116c2cd71SAl Viro 	/* Note: we do not d_invalidate() */
54234286d66SNick Piggin 	status = d_revalidate(dentry, nd);
54339159de2SJeff Layton 	if (status > 0)
54439159de2SJeff Layton 		return 0;
54539159de2SJeff Layton 
54616c2cd71SAl Viro 	if (!status)
54739159de2SJeff Layton 		status = -ESTALE;
54816c2cd71SAl Viro 
5499f1fafeeSAl Viro 	path_put(&nd->path);
55039159de2SJeff Layton 	return status;
55139159de2SJeff Layton }
55239159de2SJeff Layton 
5532a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
5542a737871SAl Viro {
555f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
556f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
5572a737871SAl Viro }
5582a737871SAl Viro 
5596de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
5606de88d72SAl Viro 
56131e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
56231e6b01fSNick Piggin {
56331e6b01fSNick Piggin 	if (!nd->root.mnt) {
56431e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
565c28cc364SNick Piggin 		unsigned seq;
566c28cc364SNick Piggin 
567c28cc364SNick Piggin 		do {
568c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
56931e6b01fSNick Piggin 			nd->root = fs->root;
570c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
571c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
57231e6b01fSNick Piggin 	}
57331e6b01fSNick Piggin }
57431e6b01fSNick Piggin 
575f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
5761da177e4SLinus Torvalds {
57731e6b01fSNick Piggin 	int ret;
57831e6b01fSNick Piggin 
5791da177e4SLinus Torvalds 	if (IS_ERR(link))
5801da177e4SLinus Torvalds 		goto fail;
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	if (*link == '/') {
5832a737871SAl Viro 		set_root(nd);
5841d957f9bSJan Blunck 		path_put(&nd->path);
5852a737871SAl Viro 		nd->path = nd->root;
5862a737871SAl Viro 		path_get(&nd->root);
58716c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
5881da177e4SLinus Torvalds 	}
58931e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
590b4091d5fSChristoph Hellwig 
59131e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
59231e6b01fSNick Piggin 	return ret;
5931da177e4SLinus Torvalds fail:
5941d957f9bSJan Blunck 	path_put(&nd->path);
5951da177e4SLinus Torvalds 	return PTR_ERR(link);
5961da177e4SLinus Torvalds }
5971da177e4SLinus Torvalds 
5981d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
599051d3812SIan Kent {
600051d3812SIan Kent 	dput(path->dentry);
6014ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
602051d3812SIan Kent 		mntput(path->mnt);
603051d3812SIan Kent }
604051d3812SIan Kent 
6057b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
6067b9337aaSNick Piggin 					struct nameidata *nd)
607051d3812SIan Kent {
60831e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
6094ac91378SJan Blunck 		dput(nd->path.dentry);
61031e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
6114ac91378SJan Blunck 			mntput(nd->path.mnt);
6129a229683SHuang Shijie 	}
61331e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
6144ac91378SJan Blunck 	nd->path.dentry = path->dentry;
615051d3812SIan Kent }
616051d3812SIan Kent 
617574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
618574197e0SAl Viro {
619574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
620574197e0SAl Viro 	if (!IS_ERR(cookie) && inode->i_op->put_link)
621574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
622574197e0SAl Viro 	path_put(link);
623574197e0SAl Viro }
624574197e0SAl Viro 
625def4af30SAl Viro static __always_inline int
626574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
6271da177e4SLinus Torvalds {
6281da177e4SLinus Torvalds 	int error;
6297b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
6301da177e4SLinus Torvalds 
631844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
632844a3917SAl Viro 
6330e794589SAl Viro 	if (link->mnt == nd->path.mnt)
6340e794589SAl Viro 		mntget(link->mnt);
6350e794589SAl Viro 
636574197e0SAl Viro 	if (unlikely(current->total_link_count >= 40)) {
637574197e0SAl Viro 		*p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
638574197e0SAl Viro 		path_put(&nd->path);
639574197e0SAl Viro 		return -ELOOP;
640574197e0SAl Viro 	}
641574197e0SAl Viro 	cond_resched();
642574197e0SAl Viro 	current->total_link_count++;
643574197e0SAl Viro 
6447b9337aaSNick Piggin 	touch_atime(link->mnt, dentry);
6451da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
646cd4e91d3SAl Viro 
64736f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
64836f3b4f6SAl Viro 	if (error) {
64936f3b4f6SAl Viro 		*p = ERR_PTR(error); /* no ->put_link(), please */
65036f3b4f6SAl Viro 		path_put(&nd->path);
65136f3b4f6SAl Viro 		return error;
65236f3b4f6SAl Viro 	}
65336f3b4f6SAl Viro 
65486acdca1SAl Viro 	nd->last_type = LAST_BIND;
655def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
656def4af30SAl Viro 	error = PTR_ERR(*p);
657def4af30SAl Viro 	if (!IS_ERR(*p)) {
6581da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
659cc314eefSLinus Torvalds 		error = 0;
6601da177e4SLinus Torvalds 		if (s)
6611da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
662bcda7652SAl Viro 		else if (nd->last_type == LAST_BIND) {
66316c2cd71SAl Viro 			nd->flags |= LOOKUP_JUMPED;
664b21041d0SAl Viro 			nd->inode = nd->path.dentry->d_inode;
665b21041d0SAl Viro 			if (nd->inode->i_op->follow_link) {
666bcda7652SAl Viro 				/* stepped on a _really_ weird one */
667bcda7652SAl Viro 				path_put(&nd->path);
668bcda7652SAl Viro 				error = -ELOOP;
669bcda7652SAl Viro 			}
670bcda7652SAl Viro 		}
6711da177e4SLinus Torvalds 	}
6721da177e4SLinus Torvalds 	return error;
6731da177e4SLinus Torvalds }
6741da177e4SLinus Torvalds 
67531e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
67631e6b01fSNick Piggin {
67731e6b01fSNick Piggin 	struct vfsmount *parent;
67831e6b01fSNick Piggin 	struct dentry *mountpoint;
67931e6b01fSNick Piggin 
68031e6b01fSNick Piggin 	parent = path->mnt->mnt_parent;
68131e6b01fSNick Piggin 	if (parent == path->mnt)
68231e6b01fSNick Piggin 		return 0;
68331e6b01fSNick Piggin 	mountpoint = path->mnt->mnt_mountpoint;
68431e6b01fSNick Piggin 	path->dentry = mountpoint;
68531e6b01fSNick Piggin 	path->mnt = parent;
68631e6b01fSNick Piggin 	return 1;
68731e6b01fSNick Piggin }
68831e6b01fSNick Piggin 
689bab77ebfSAl Viro int follow_up(struct path *path)
6901da177e4SLinus Torvalds {
6911da177e4SLinus Torvalds 	struct vfsmount *parent;
6921da177e4SLinus Torvalds 	struct dentry *mountpoint;
69399b7db7bSNick Piggin 
69499b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
695bab77ebfSAl Viro 	parent = path->mnt->mnt_parent;
696bab77ebfSAl Viro 	if (parent == path->mnt) {
69799b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
6981da177e4SLinus Torvalds 		return 0;
6991da177e4SLinus Torvalds 	}
7001da177e4SLinus Torvalds 	mntget(parent);
701bab77ebfSAl Viro 	mountpoint = dget(path->mnt->mnt_mountpoint);
70299b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
703bab77ebfSAl Viro 	dput(path->dentry);
704bab77ebfSAl Viro 	path->dentry = mountpoint;
705bab77ebfSAl Viro 	mntput(path->mnt);
706bab77ebfSAl Viro 	path->mnt = parent;
7071da177e4SLinus Torvalds 	return 1;
7081da177e4SLinus Torvalds }
7091da177e4SLinus Torvalds 
710b5c84bf6SNick Piggin /*
7119875cf80SDavid Howells  * Perform an automount
7129875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
7139875cf80SDavid Howells  *   were called with.
7141da177e4SLinus Torvalds  */
7159875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
7169875cf80SDavid Howells 			    bool *need_mntput)
71731e6b01fSNick Piggin {
7189875cf80SDavid Howells 	struct vfsmount *mnt;
719ea5b778aSDavid Howells 	int err;
7209875cf80SDavid Howells 
7219875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
7229875cf80SDavid Howells 		return -EREMOTE;
7239875cf80SDavid Howells 
7240ec26fd0SMiklos Szeredi 	/* We don't want to mount if someone's just doing a stat -
7250ec26fd0SMiklos Szeredi 	 * unless they're stat'ing a directory and appended a '/' to
7260ec26fd0SMiklos Szeredi 	 * the name.
7270ec26fd0SMiklos Szeredi 	 *
7280ec26fd0SMiklos Szeredi 	 * We do, however, want to mount if someone wants to open or
7295a30d8a2SDavid Howells 	 * create a file of any type under the mountpoint, wants to
7300ec26fd0SMiklos Szeredi 	 * traverse through the mountpoint or wants to open the
7310ec26fd0SMiklos Szeredi 	 * mounted directory.  Also, autofs may mark negative dentries
7320ec26fd0SMiklos Szeredi 	 * as being automount points.  These will need the attentions
7330ec26fd0SMiklos Szeredi 	 * of the daemon to instantiate them before they can be used.
7345a30d8a2SDavid Howells 	 */
7355a30d8a2SDavid Howells 	if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
736d94c177bSLinus Torvalds 		     LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
7375a30d8a2SDavid Howells 	    path->dentry->d_inode)
7389875cf80SDavid Howells 		return -EISDIR;
7390ec26fd0SMiklos Szeredi 
7409875cf80SDavid Howells 	current->total_link_count++;
7419875cf80SDavid Howells 	if (current->total_link_count >= 40)
7429875cf80SDavid Howells 		return -ELOOP;
7439875cf80SDavid Howells 
7449875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
7459875cf80SDavid Howells 	if (IS_ERR(mnt)) {
7469875cf80SDavid Howells 		/*
7479875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
7489875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
7499875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
7509875cf80SDavid Howells 		 *
7519875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
7529875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
7539875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
7549875cf80SDavid Howells 		 */
75549084c3bSAl Viro 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
7569875cf80SDavid Howells 			return -EREMOTE;
7579875cf80SDavid Howells 		return PTR_ERR(mnt);
75831e6b01fSNick Piggin 	}
759ea5b778aSDavid Howells 
7609875cf80SDavid Howells 	if (!mnt) /* mount collision */
7619875cf80SDavid Howells 		return 0;
7629875cf80SDavid Howells 
7638aef1884SAl Viro 	if (!*need_mntput) {
7648aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
7658aef1884SAl Viro 		mntget(path->mnt);
7668aef1884SAl Viro 		*need_mntput = true;
7678aef1884SAl Viro 	}
76819a167afSAl Viro 	err = finish_automount(mnt, path);
769ea5b778aSDavid Howells 
770ea5b778aSDavid Howells 	switch (err) {
771ea5b778aSDavid Howells 	case -EBUSY:
772ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
77319a167afSAl Viro 		return 0;
774ea5b778aSDavid Howells 	case 0:
7758aef1884SAl Viro 		path_put(path);
7769875cf80SDavid Howells 		path->mnt = mnt;
7779875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
7789875cf80SDavid Howells 		return 0;
77919a167afSAl Viro 	default:
78019a167afSAl Viro 		return err;
7819875cf80SDavid Howells 	}
78219a167afSAl Viro 
783ea5b778aSDavid Howells }
7849875cf80SDavid Howells 
7859875cf80SDavid Howells /*
7869875cf80SDavid Howells  * Handle a dentry that is managed in some way.
787cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
7889875cf80SDavid Howells  * - Flagged as mountpoint
7899875cf80SDavid Howells  * - Flagged as automount point
7909875cf80SDavid Howells  *
7919875cf80SDavid Howells  * This may only be called in refwalk mode.
7929875cf80SDavid Howells  *
7939875cf80SDavid Howells  * Serialization is taken care of in namespace.c
7949875cf80SDavid Howells  */
7959875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
7969875cf80SDavid Howells {
7978aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
7989875cf80SDavid Howells 	unsigned managed;
7999875cf80SDavid Howells 	bool need_mntput = false;
8008aef1884SAl Viro 	int ret = 0;
8019875cf80SDavid Howells 
8029875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
8039875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
8049875cf80SDavid Howells 	 * the components of that value change under us */
8059875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
8069875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
8079875cf80SDavid Howells 	       unlikely(managed != 0)) {
808cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
809cc53ce53SDavid Howells 		 * being held. */
810cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
811cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
812cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
8131aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
814cc53ce53SDavid Howells 			if (ret < 0)
8158aef1884SAl Viro 				break;
816cc53ce53SDavid Howells 		}
817cc53ce53SDavid Howells 
8189875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
8199875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
8209875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
8219875cf80SDavid Howells 			if (mounted) {
8229875cf80SDavid Howells 				dput(path->dentry);
8239875cf80SDavid Howells 				if (need_mntput)
824463ffb2eSAl Viro 					mntput(path->mnt);
825463ffb2eSAl Viro 				path->mnt = mounted;
826463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
8279875cf80SDavid Howells 				need_mntput = true;
8289875cf80SDavid Howells 				continue;
829463ffb2eSAl Viro 			}
830463ffb2eSAl Viro 
8319875cf80SDavid Howells 			/* Something is mounted on this dentry in another
8329875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
8339875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
8349875cf80SDavid Howells 			 * vfsmount_lock */
8351da177e4SLinus Torvalds 		}
8369875cf80SDavid Howells 
8379875cf80SDavid Howells 		/* Handle an automount point */
8389875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
8399875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
8409875cf80SDavid Howells 			if (ret < 0)
8418aef1884SAl Viro 				break;
8429875cf80SDavid Howells 			continue;
8439875cf80SDavid Howells 		}
8449875cf80SDavid Howells 
8459875cf80SDavid Howells 		/* We didn't change the current path point */
8469875cf80SDavid Howells 		break;
8479875cf80SDavid Howells 	}
8488aef1884SAl Viro 
8498aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
8508aef1884SAl Viro 		mntput(path->mnt);
8518aef1884SAl Viro 	if (ret == -EISDIR)
8528aef1884SAl Viro 		ret = 0;
8538aef1884SAl Viro 	return ret;
8541da177e4SLinus Torvalds }
8551da177e4SLinus Torvalds 
856cc53ce53SDavid Howells int follow_down_one(struct path *path)
8571da177e4SLinus Torvalds {
8581da177e4SLinus Torvalds 	struct vfsmount *mounted;
8591da177e4SLinus Torvalds 
8601c755af4SAl Viro 	mounted = lookup_mnt(path);
8611da177e4SLinus Torvalds 	if (mounted) {
8629393bd07SAl Viro 		dput(path->dentry);
8639393bd07SAl Viro 		mntput(path->mnt);
8649393bd07SAl Viro 		path->mnt = mounted;
8659393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
8661da177e4SLinus Torvalds 		return 1;
8671da177e4SLinus Torvalds 	}
8681da177e4SLinus Torvalds 	return 0;
8691da177e4SLinus Torvalds }
8701da177e4SLinus Torvalds 
87162a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
87262a7375eSIan Kent {
87362a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
87462a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
87562a7375eSIan Kent }
87662a7375eSIan Kent 
8779875cf80SDavid Howells /*
878287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
879287548e4SAl Viro  * we meet a managed dentry that would need blocking.
8809875cf80SDavid Howells  */
8819875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
882287548e4SAl Viro 			       struct inode **inode)
8839875cf80SDavid Howells {
88462a7375eSIan Kent 	for (;;) {
8859875cf80SDavid Howells 		struct vfsmount *mounted;
88662a7375eSIan Kent 		/*
88762a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
88862a7375eSIan Kent 		 * that wants to block transit.
88962a7375eSIan Kent 		 */
890287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
891ab90911fSDavid Howells 			return false;
89262a7375eSIan Kent 
89362a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
89462a7375eSIan Kent 			break;
89562a7375eSIan Kent 
8969875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
8979875cf80SDavid Howells 		if (!mounted)
8989875cf80SDavid Howells 			break;
8999875cf80SDavid Howells 		path->mnt = mounted;
9009875cf80SDavid Howells 		path->dentry = mounted->mnt_root;
9019875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
90259430262SLinus Torvalds 		/*
90359430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
90459430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
90559430262SLinus Torvalds 		 * because a mount-point is always pinned.
90659430262SLinus Torvalds 		 */
90759430262SLinus Torvalds 		*inode = path->dentry->d_inode;
9089875cf80SDavid Howells 	}
9099875cf80SDavid Howells 	return true;
9109875cf80SDavid Howells }
9119875cf80SDavid Howells 
912dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
913287548e4SAl Viro {
914dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
915287548e4SAl Viro 		struct vfsmount *mounted;
916dea39376SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
917287548e4SAl Viro 		if (!mounted)
918287548e4SAl Viro 			break;
919dea39376SAl Viro 		nd->path.mnt = mounted;
920dea39376SAl Viro 		nd->path.dentry = mounted->mnt_root;
921dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
922287548e4SAl Viro 	}
923287548e4SAl Viro }
924287548e4SAl Viro 
92531e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
92631e6b01fSNick Piggin {
92731e6b01fSNick Piggin 	set_root_rcu(nd);
92831e6b01fSNick Piggin 
92931e6b01fSNick Piggin 	while (1) {
93031e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
93131e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
93231e6b01fSNick Piggin 			break;
93331e6b01fSNick Piggin 		}
93431e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
93531e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
93631e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
93731e6b01fSNick Piggin 			unsigned seq;
93831e6b01fSNick Piggin 
93931e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
94031e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
941ef7562d5SAl Viro 				goto failed;
94231e6b01fSNick Piggin 			nd->path.dentry = parent;
94331e6b01fSNick Piggin 			nd->seq = seq;
94431e6b01fSNick Piggin 			break;
94531e6b01fSNick Piggin 		}
94631e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
94731e6b01fSNick Piggin 			break;
94831e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
94931e6b01fSNick Piggin 	}
950dea39376SAl Viro 	follow_mount_rcu(nd);
951dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
95231e6b01fSNick Piggin 	return 0;
953ef7562d5SAl Viro 
954ef7562d5SAl Viro failed:
955ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
9565b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
957ef7562d5SAl Viro 		nd->root.mnt = NULL;
958ef7562d5SAl Viro 	rcu_read_unlock();
959ef7562d5SAl Viro 	br_read_unlock(vfsmount_lock);
960ef7562d5SAl Viro 	return -ECHILD;
96131e6b01fSNick Piggin }
96231e6b01fSNick Piggin 
9639875cf80SDavid Howells /*
964cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
965cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
966cc53ce53SDavid Howells  * caller is permitted to proceed or not.
967cc53ce53SDavid Howells  */
9687cc90cc3SAl Viro int follow_down(struct path *path)
969cc53ce53SDavid Howells {
970cc53ce53SDavid Howells 	unsigned managed;
971cc53ce53SDavid Howells 	int ret;
972cc53ce53SDavid Howells 
973cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
974cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
975cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
976cc53ce53SDavid Howells 		 * being held.
977cc53ce53SDavid Howells 		 *
978cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
979cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
980cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
981cc53ce53SDavid Howells 		 * superstructure.
982cc53ce53SDavid Howells 		 *
983cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
984cc53ce53SDavid Howells 		 */
985cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
986cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
987cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
988ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
9891aed3e42SAl Viro 				path->dentry, false);
990cc53ce53SDavid Howells 			if (ret < 0)
991cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
992cc53ce53SDavid Howells 		}
993cc53ce53SDavid Howells 
994cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
995cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
996cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
997cc53ce53SDavid Howells 			if (!mounted)
998cc53ce53SDavid Howells 				break;
999cc53ce53SDavid Howells 			dput(path->dentry);
1000cc53ce53SDavid Howells 			mntput(path->mnt);
1001cc53ce53SDavid Howells 			path->mnt = mounted;
1002cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1003cc53ce53SDavid Howells 			continue;
1004cc53ce53SDavid Howells 		}
1005cc53ce53SDavid Howells 
1006cc53ce53SDavid Howells 		/* Don't handle automount points here */
1007cc53ce53SDavid Howells 		break;
1008cc53ce53SDavid Howells 	}
1009cc53ce53SDavid Howells 	return 0;
1010cc53ce53SDavid Howells }
1011cc53ce53SDavid Howells 
1012cc53ce53SDavid Howells /*
10139875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
10149875cf80SDavid Howells  */
10159875cf80SDavid Howells static void follow_mount(struct path *path)
10169875cf80SDavid Howells {
10179875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
10189875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
10199875cf80SDavid Howells 		if (!mounted)
10209875cf80SDavid Howells 			break;
10219875cf80SDavid Howells 		dput(path->dentry);
10229875cf80SDavid Howells 		mntput(path->mnt);
10239875cf80SDavid Howells 		path->mnt = mounted;
10249875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
10259875cf80SDavid Howells 	}
10269875cf80SDavid Howells }
10279875cf80SDavid Howells 
102831e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
10291da177e4SLinus Torvalds {
10302a737871SAl Viro 	set_root(nd);
1031e518ddb7SAndreas Mohr 
10321da177e4SLinus Torvalds 	while(1) {
10334ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
10341da177e4SLinus Torvalds 
10352a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
10362a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
10371da177e4SLinus Torvalds 			break;
10381da177e4SLinus Torvalds 		}
10394ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
10403088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
10413088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
10421da177e4SLinus Torvalds 			dput(old);
10431da177e4SLinus Torvalds 			break;
10441da177e4SLinus Torvalds 		}
10453088dd70SAl Viro 		if (!follow_up(&nd->path))
10461da177e4SLinus Torvalds 			break;
10471da177e4SLinus Torvalds 	}
104879ed0226SAl Viro 	follow_mount(&nd->path);
104931e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
10501da177e4SLinus Torvalds }
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds /*
1053baa03890SNick Piggin  * Allocate a dentry with name and parent, and perform a parent
1054baa03890SNick Piggin  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1055baa03890SNick Piggin  * on error. parent->d_inode->i_mutex must be held. d_lookup must
1056baa03890SNick Piggin  * have verified that no child exists while under i_mutex.
1057baa03890SNick Piggin  */
1058baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1059baa03890SNick Piggin 				struct qstr *name, struct nameidata *nd)
1060baa03890SNick Piggin {
1061baa03890SNick Piggin 	struct inode *inode = parent->d_inode;
1062baa03890SNick Piggin 	struct dentry *dentry;
1063baa03890SNick Piggin 	struct dentry *old;
1064baa03890SNick Piggin 
1065baa03890SNick Piggin 	/* Don't create child dentry for a dead directory. */
1066baa03890SNick Piggin 	if (unlikely(IS_DEADDIR(inode)))
1067baa03890SNick Piggin 		return ERR_PTR(-ENOENT);
1068baa03890SNick Piggin 
1069baa03890SNick Piggin 	dentry = d_alloc(parent, name);
1070baa03890SNick Piggin 	if (unlikely(!dentry))
1071baa03890SNick Piggin 		return ERR_PTR(-ENOMEM);
1072baa03890SNick Piggin 
1073baa03890SNick Piggin 	old = inode->i_op->lookup(inode, dentry, nd);
1074baa03890SNick Piggin 	if (unlikely(old)) {
1075baa03890SNick Piggin 		dput(dentry);
1076baa03890SNick Piggin 		dentry = old;
1077baa03890SNick Piggin 	}
1078baa03890SNick Piggin 	return dentry;
1079baa03890SNick Piggin }
1080baa03890SNick Piggin 
1081baa03890SNick Piggin /*
108244396f4bSJosef Bacik  * We already have a dentry, but require a lookup to be performed on the parent
108344396f4bSJosef Bacik  * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
108444396f4bSJosef Bacik  * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
108544396f4bSJosef Bacik  * child exists while under i_mutex.
108644396f4bSJosef Bacik  */
108744396f4bSJosef Bacik static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
108844396f4bSJosef Bacik 				     struct nameidata *nd)
108944396f4bSJosef Bacik {
109044396f4bSJosef Bacik 	struct inode *inode = parent->d_inode;
109144396f4bSJosef Bacik 	struct dentry *old;
109244396f4bSJosef Bacik 
109344396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
109444396f4bSJosef Bacik 	if (unlikely(IS_DEADDIR(inode)))
109544396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
109644396f4bSJosef Bacik 
109744396f4bSJosef Bacik 	old = inode->i_op->lookup(inode, dentry, nd);
109844396f4bSJosef Bacik 	if (unlikely(old)) {
109944396f4bSJosef Bacik 		dput(dentry);
110044396f4bSJosef Bacik 		dentry = old;
110144396f4bSJosef Bacik 	}
110244396f4bSJosef Bacik 	return dentry;
110344396f4bSJosef Bacik }
110444396f4bSJosef Bacik 
110544396f4bSJosef Bacik /*
11061da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
11071da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
11081da177e4SLinus Torvalds  *  It _is_ time-critical.
11091da177e4SLinus Torvalds  */
11101da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
111131e6b01fSNick Piggin 			struct path *path, struct inode **inode)
11121da177e4SLinus Torvalds {
11134ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
111431e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
11155a18fff2SAl Viro 	int need_reval = 1;
11165a18fff2SAl Viro 	int status = 1;
11179875cf80SDavid Howells 	int err;
11189875cf80SDavid Howells 
11193cac260aSAl Viro 	/*
1120b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1121b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1122b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1123b04f784eSNick Piggin 	 */
112431e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
112531e6b01fSNick Piggin 		unsigned seq;
112631e6b01fSNick Piggin 		*inode = nd->inode;
112731e6b01fSNick Piggin 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
11285a18fff2SAl Viro 		if (!dentry)
11295a18fff2SAl Viro 			goto unlazy;
11305a18fff2SAl Viro 
113131e6b01fSNick Piggin 		/* Memory barrier in read_seqcount_begin of child is enough */
113231e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
113331e6b01fSNick Piggin 			return -ECHILD;
113431e6b01fSNick Piggin 		nd->seq = seq;
11355a18fff2SAl Viro 
113624643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
11375a18fff2SAl Viro 			status = d_revalidate(dentry, nd);
11385a18fff2SAl Viro 			if (unlikely(status <= 0)) {
11395a18fff2SAl Viro 				if (status != -ECHILD)
11405a18fff2SAl Viro 					need_reval = 0;
11415a18fff2SAl Viro 				goto unlazy;
11425a18fff2SAl Viro 			}
114324643087SAl Viro 		}
114444396f4bSJosef Bacik 		if (unlikely(d_need_lookup(dentry)))
114544396f4bSJosef Bacik 			goto unlazy;
114631e6b01fSNick Piggin 		path->mnt = mnt;
114731e6b01fSNick Piggin 		path->dentry = dentry;
1148d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1149d6e9bd25SAl Viro 			goto unlazy;
1150d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1151d6e9bd25SAl Viro 			goto unlazy;
11529875cf80SDavid Howells 		return 0;
11535a18fff2SAl Viro unlazy:
115419660af7SAl Viro 		if (unlazy_walk(nd, dentry))
11555a18fff2SAl Viro 			return -ECHILD;
11565a18fff2SAl Viro 	} else {
115731e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
115824643087SAl Viro 	}
11595a18fff2SAl Viro 
116044396f4bSJosef Bacik 	if (dentry && unlikely(d_need_lookup(dentry))) {
116144396f4bSJosef Bacik 		dput(dentry);
116244396f4bSJosef Bacik 		dentry = NULL;
116344396f4bSJosef Bacik 	}
11645a18fff2SAl Viro retry:
11655a18fff2SAl Viro 	if (unlikely(!dentry)) {
11665a18fff2SAl Viro 		struct inode *dir = parent->d_inode;
11675a18fff2SAl Viro 		BUG_ON(nd->inode != dir);
11685a18fff2SAl Viro 
11695a18fff2SAl Viro 		mutex_lock(&dir->i_mutex);
11705a18fff2SAl Viro 		dentry = d_lookup(parent, name);
11715a18fff2SAl Viro 		if (likely(!dentry)) {
11725a18fff2SAl Viro 			dentry = d_alloc_and_lookup(parent, name, nd);
11735a18fff2SAl Viro 			if (IS_ERR(dentry)) {
11745a18fff2SAl Viro 				mutex_unlock(&dir->i_mutex);
11755a18fff2SAl Viro 				return PTR_ERR(dentry);
11765a18fff2SAl Viro 			}
11775a18fff2SAl Viro 			/* known good */
11785a18fff2SAl Viro 			need_reval = 0;
11795a18fff2SAl Viro 			status = 1;
118044396f4bSJosef Bacik 		} else if (unlikely(d_need_lookup(dentry))) {
118144396f4bSJosef Bacik 			dentry = d_inode_lookup(parent, dentry, nd);
118244396f4bSJosef Bacik 			if (IS_ERR(dentry)) {
118344396f4bSJosef Bacik 				mutex_unlock(&dir->i_mutex);
118444396f4bSJosef Bacik 				return PTR_ERR(dentry);
118544396f4bSJosef Bacik 			}
118644396f4bSJosef Bacik 			/* known good */
118744396f4bSJosef Bacik 			need_reval = 0;
118844396f4bSJosef Bacik 			status = 1;
11895a18fff2SAl Viro 		}
11905a18fff2SAl Viro 		mutex_unlock(&dir->i_mutex);
11915a18fff2SAl Viro 	}
11925a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
11935a18fff2SAl Viro 		status = d_revalidate(dentry, nd);
11945a18fff2SAl Viro 	if (unlikely(status <= 0)) {
11955a18fff2SAl Viro 		if (status < 0) {
11965a18fff2SAl Viro 			dput(dentry);
11975a18fff2SAl Viro 			return status;
11985a18fff2SAl Viro 		}
11995a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
12005a18fff2SAl Viro 			dput(dentry);
12015a18fff2SAl Viro 			dentry = NULL;
12025a18fff2SAl Viro 			need_reval = 1;
12035a18fff2SAl Viro 			goto retry;
12045a18fff2SAl Viro 		}
12055a18fff2SAl Viro 	}
12065a18fff2SAl Viro 
12071da177e4SLinus Torvalds 	path->mnt = mnt;
12081da177e4SLinus Torvalds 	path->dentry = dentry;
12099875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
121089312214SIan Kent 	if (unlikely(err < 0)) {
121189312214SIan Kent 		path_put_conditional(path, nd);
12129875cf80SDavid Howells 		return err;
121389312214SIan Kent 	}
121431e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
12151da177e4SLinus Torvalds 	return 0;
12161da177e4SLinus Torvalds }
12171da177e4SLinus Torvalds 
121852094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
121952094c8aSAl Viro {
122052094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
12214ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
122252094c8aSAl Viro 		if (err != -ECHILD)
122352094c8aSAl Viro 			return err;
122419660af7SAl Viro 		if (unlazy_walk(nd, NULL))
122552094c8aSAl Viro 			return -ECHILD;
122652094c8aSAl Viro 	}
12274ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
122852094c8aSAl Viro }
122952094c8aSAl Viro 
12309856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
12319856fa1bSAl Viro {
12329856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
12339856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
12349856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
12359856fa1bSAl Viro 				return -ECHILD;
12369856fa1bSAl Viro 		} else
12379856fa1bSAl Viro 			follow_dotdot(nd);
12389856fa1bSAl Viro 	}
12399856fa1bSAl Viro 	return 0;
12409856fa1bSAl Viro }
12419856fa1bSAl Viro 
1242951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1243951361f9SAl Viro {
1244951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1245951361f9SAl Viro 		path_put(&nd->path);
1246951361f9SAl Viro 	} else {
1247951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
12485b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1249951361f9SAl Viro 			nd->root.mnt = NULL;
1250951361f9SAl Viro 		rcu_read_unlock();
1251951361f9SAl Viro 		br_read_unlock(vfsmount_lock);
1252951361f9SAl Viro 	}
1253951361f9SAl Viro }
1254951361f9SAl Viro 
12553ddcd056SLinus Torvalds /*
12563ddcd056SLinus Torvalds  * Do we need to follow links? We _really_ want to be able
12573ddcd056SLinus Torvalds  * to do this check without having to look at inode->i_op,
12583ddcd056SLinus Torvalds  * so we keep a cache of "no, this doesn't need follow_link"
12593ddcd056SLinus Torvalds  * for the common case.
12603ddcd056SLinus Torvalds  */
12617813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow)
12623ddcd056SLinus Torvalds {
12633ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
12643ddcd056SLinus Torvalds 		if (likely(inode->i_op->follow_link))
12653ddcd056SLinus Torvalds 			return follow;
12663ddcd056SLinus Torvalds 
12673ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
12683ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
12693ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_NOFOLLOW;
12703ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
12713ddcd056SLinus Torvalds 	}
12723ddcd056SLinus Torvalds 	return 0;
12733ddcd056SLinus Torvalds }
12743ddcd056SLinus Torvalds 
1275ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
1276ce57dfc1SAl Viro 		struct qstr *name, int type, int follow)
1277ce57dfc1SAl Viro {
1278ce57dfc1SAl Viro 	struct inode *inode;
1279ce57dfc1SAl Viro 	int err;
1280ce57dfc1SAl Viro 	/*
1281ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1282ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1283ce57dfc1SAl Viro 	 * parent relationships.
1284ce57dfc1SAl Viro 	 */
1285ce57dfc1SAl Viro 	if (unlikely(type != LAST_NORM))
1286ce57dfc1SAl Viro 		return handle_dots(nd, type);
1287ce57dfc1SAl Viro 	err = do_lookup(nd, name, path, &inode);
1288ce57dfc1SAl Viro 	if (unlikely(err)) {
1289ce57dfc1SAl Viro 		terminate_walk(nd);
1290ce57dfc1SAl Viro 		return err;
1291ce57dfc1SAl Viro 	}
1292ce57dfc1SAl Viro 	if (!inode) {
1293ce57dfc1SAl Viro 		path_to_nameidata(path, nd);
1294ce57dfc1SAl Viro 		terminate_walk(nd);
1295ce57dfc1SAl Viro 		return -ENOENT;
1296ce57dfc1SAl Viro 	}
12977813b94aSLinus Torvalds 	if (should_follow_link(inode, follow)) {
129819660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
129919660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
130019660af7SAl Viro 				terminate_walk(nd);
1301ce57dfc1SAl Viro 				return -ECHILD;
130219660af7SAl Viro 			}
130319660af7SAl Viro 		}
1304ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1305ce57dfc1SAl Viro 		return 1;
1306ce57dfc1SAl Viro 	}
1307ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1308ce57dfc1SAl Viro 	nd->inode = inode;
1309ce57dfc1SAl Viro 	return 0;
1310ce57dfc1SAl Viro }
1311ce57dfc1SAl Viro 
13121da177e4SLinus Torvalds /*
1313b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1314b356379aSAl Viro  * limiting consecutive symlinks to 40.
1315b356379aSAl Viro  *
1316b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1317b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1318b356379aSAl Viro  */
1319b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1320b356379aSAl Viro {
1321b356379aSAl Viro 	int res;
1322b356379aSAl Viro 
1323b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1324b356379aSAl Viro 		path_put_conditional(path, nd);
1325b356379aSAl Viro 		path_put(&nd->path);
1326b356379aSAl Viro 		return -ELOOP;
1327b356379aSAl Viro 	}
13281a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1329b356379aSAl Viro 
1330b356379aSAl Viro 	nd->depth++;
1331b356379aSAl Viro 	current->link_count++;
1332b356379aSAl Viro 
1333b356379aSAl Viro 	do {
1334b356379aSAl Viro 		struct path link = *path;
1335b356379aSAl Viro 		void *cookie;
1336574197e0SAl Viro 
1337574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
1338b356379aSAl Viro 		if (!res)
1339b356379aSAl Viro 			res = walk_component(nd, path, &nd->last,
1340b356379aSAl Viro 					     nd->last_type, LOOKUP_FOLLOW);
1341574197e0SAl Viro 		put_link(nd, &link, cookie);
1342b356379aSAl Viro 	} while (res > 0);
1343b356379aSAl Viro 
1344b356379aSAl Viro 	current->link_count--;
1345b356379aSAl Viro 	nd->depth--;
1346b356379aSAl Viro 	return res;
1347b356379aSAl Viro }
1348b356379aSAl Viro 
1349b356379aSAl Viro /*
13503ddcd056SLinus Torvalds  * We really don't want to look at inode->i_op->lookup
13513ddcd056SLinus Torvalds  * when we don't have to. So we keep a cache bit in
13523ddcd056SLinus Torvalds  * the inode ->i_opflags field that says "yes, we can
13533ddcd056SLinus Torvalds  * do lookup on this inode".
13543ddcd056SLinus Torvalds  */
13553ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode)
13563ddcd056SLinus Torvalds {
13573ddcd056SLinus Torvalds 	if (likely(inode->i_opflags & IOP_LOOKUP))
13583ddcd056SLinus Torvalds 		return 1;
13593ddcd056SLinus Torvalds 	if (likely(!inode->i_op->lookup))
13603ddcd056SLinus Torvalds 		return 0;
13613ddcd056SLinus Torvalds 
13623ddcd056SLinus Torvalds 	/* We do this once for the lifetime of the inode */
13633ddcd056SLinus Torvalds 	spin_lock(&inode->i_lock);
13643ddcd056SLinus Torvalds 	inode->i_opflags |= IOP_LOOKUP;
13653ddcd056SLinus Torvalds 	spin_unlock(&inode->i_lock);
13663ddcd056SLinus Torvalds 	return 1;
13673ddcd056SLinus Torvalds }
13683ddcd056SLinus Torvalds 
13693ddcd056SLinus Torvalds /*
13701da177e4SLinus Torvalds  * Name resolution.
1371ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1372ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
13731da177e4SLinus Torvalds  *
1374ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1375ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
13761da177e4SLinus Torvalds  */
13776de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
13781da177e4SLinus Torvalds {
13791da177e4SLinus Torvalds 	struct path next;
13801da177e4SLinus Torvalds 	int err;
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 	while (*name=='/')
13831da177e4SLinus Torvalds 		name++;
13841da177e4SLinus Torvalds 	if (!*name)
1385086e183aSAl Viro 		return 0;
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
13881da177e4SLinus Torvalds 	for(;;) {
13891da177e4SLinus Torvalds 		unsigned long hash;
13901da177e4SLinus Torvalds 		struct qstr this;
13911da177e4SLinus Torvalds 		unsigned int c;
1392fe479a58SAl Viro 		int type;
13931da177e4SLinus Torvalds 
139452094c8aSAl Viro 		err = may_lookup(nd);
13951da177e4SLinus Torvalds  		if (err)
13961da177e4SLinus Torvalds 			break;
13971da177e4SLinus Torvalds 
13981da177e4SLinus Torvalds 		this.name = name;
13991da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 		hash = init_name_hash();
14021da177e4SLinus Torvalds 		do {
14031da177e4SLinus Torvalds 			name++;
14041da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
14051da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
14061da177e4SLinus Torvalds 		} while (c && (c != '/'));
14071da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
14081da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
14091da177e4SLinus Torvalds 
1410fe479a58SAl Viro 		type = LAST_NORM;
1411fe479a58SAl Viro 		if (this.name[0] == '.') switch (this.len) {
1412fe479a58SAl Viro 			case 2:
141316c2cd71SAl Viro 				if (this.name[1] == '.') {
1414fe479a58SAl Viro 					type = LAST_DOTDOT;
141516c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
141616c2cd71SAl Viro 				}
1417fe479a58SAl Viro 				break;
1418fe479a58SAl Viro 			case 1:
1419fe479a58SAl Viro 				type = LAST_DOT;
1420fe479a58SAl Viro 		}
14215a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
14225a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
142316c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
14245a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
14255a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
14265a202bcdSAl Viro 							   &this);
14275a202bcdSAl Viro 				if (err < 0)
14285a202bcdSAl Viro 					break;
14295a202bcdSAl Viro 			}
14305a202bcdSAl Viro 		}
1431fe479a58SAl Viro 
14321da177e4SLinus Torvalds 		/* remove trailing slashes? */
14331da177e4SLinus Torvalds 		if (!c)
14341da177e4SLinus Torvalds 			goto last_component;
14351da177e4SLinus Torvalds 		while (*++name == '/');
14361da177e4SLinus Torvalds 		if (!*name)
1437b356379aSAl Viro 			goto last_component;
14381da177e4SLinus Torvalds 
1439ce57dfc1SAl Viro 		err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1440ce57dfc1SAl Viro 		if (err < 0)
1441ce57dfc1SAl Viro 			return err;
1442fe479a58SAl Viro 
1443ce57dfc1SAl Viro 		if (err) {
1444b356379aSAl Viro 			err = nested_symlink(&next, nd);
14451da177e4SLinus Torvalds 			if (err)
1446a7472babSAl Viro 				return err;
144731e6b01fSNick Piggin 		}
14483ddcd056SLinus Torvalds 		if (can_lookup(nd->inode))
14491da177e4SLinus Torvalds 			continue;
14503ddcd056SLinus Torvalds 		err = -ENOTDIR;
14513ddcd056SLinus Torvalds 		break;
14521da177e4SLinus Torvalds 		/* here ends the main loop */
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds last_component:
1455ce57dfc1SAl Viro 		nd->last = this;
1456ce57dfc1SAl Viro 		nd->last_type = type;
1457ce57dfc1SAl Viro 		return 0;
1458ce57dfc1SAl Viro 	}
1459951361f9SAl Viro 	terminate_walk(nd);
14601da177e4SLinus Torvalds 	return err;
14611da177e4SLinus Torvalds }
14621da177e4SLinus Torvalds 
146370e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
146470e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
146531e6b01fSNick Piggin {
146631e6b01fSNick Piggin 	int retval = 0;
146731e6b01fSNick Piggin 	int fput_needed;
146831e6b01fSNick Piggin 	struct file *file;
146931e6b01fSNick Piggin 
147031e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
147116c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
147231e6b01fSNick Piggin 	nd->depth = 0;
14735b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
14745b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
147573d049a4SAl Viro 		if (*name) {
14765b6ca027SAl Viro 			if (!inode->i_op->lookup)
14775b6ca027SAl Viro 				return -ENOTDIR;
14785b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
14795b6ca027SAl Viro 			if (retval)
14805b6ca027SAl Viro 				return retval;
148173d049a4SAl Viro 		}
14825b6ca027SAl Viro 		nd->path = nd->root;
14835b6ca027SAl Viro 		nd->inode = inode;
14845b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
14855b6ca027SAl Viro 			br_read_lock(vfsmount_lock);
14865b6ca027SAl Viro 			rcu_read_lock();
14875b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
14885b6ca027SAl Viro 		} else {
14895b6ca027SAl Viro 			path_get(&nd->path);
14905b6ca027SAl Viro 		}
14915b6ca027SAl Viro 		return 0;
14925b6ca027SAl Viro 	}
14935b6ca027SAl Viro 
149431e6b01fSNick Piggin 	nd->root.mnt = NULL;
149531e6b01fSNick Piggin 
149631e6b01fSNick Piggin 	if (*name=='/') {
1497e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
149831e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
149931e6b01fSNick Piggin 			rcu_read_lock();
1500e41f7d4eSAl Viro 			set_root_rcu(nd);
1501e41f7d4eSAl Viro 		} else {
1502e41f7d4eSAl Viro 			set_root(nd);
1503e41f7d4eSAl Viro 			path_get(&nd->root);
1504e41f7d4eSAl Viro 		}
150531e6b01fSNick Piggin 		nd->path = nd->root;
150631e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1507e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
150831e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1509c28cc364SNick Piggin 			unsigned seq;
151031e6b01fSNick Piggin 
151131e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
151231e6b01fSNick Piggin 			rcu_read_lock();
151331e6b01fSNick Piggin 
1514c28cc364SNick Piggin 			do {
1515c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
151631e6b01fSNick Piggin 				nd->path = fs->pwd;
1517c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1518c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1519e41f7d4eSAl Viro 		} else {
1520e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1521e41f7d4eSAl Viro 		}
152231e6b01fSNick Piggin 	} else {
152331e6b01fSNick Piggin 		struct dentry *dentry;
152431e6b01fSNick Piggin 
15251abf0c71SAl Viro 		file = fget_raw_light(dfd, &fput_needed);
152631e6b01fSNick Piggin 		retval = -EBADF;
152731e6b01fSNick Piggin 		if (!file)
152831e6b01fSNick Piggin 			goto out_fail;
152931e6b01fSNick Piggin 
153031e6b01fSNick Piggin 		dentry = file->f_path.dentry;
153131e6b01fSNick Piggin 
1532f52e0c11SAl Viro 		if (*name) {
153331e6b01fSNick Piggin 			retval = -ENOTDIR;
153431e6b01fSNick Piggin 			if (!S_ISDIR(dentry->d_inode->i_mode))
153531e6b01fSNick Piggin 				goto fput_fail;
153631e6b01fSNick Piggin 
15374ad5abb3SAl Viro 			retval = inode_permission(dentry->d_inode, MAY_EXEC);
153831e6b01fSNick Piggin 			if (retval)
153931e6b01fSNick Piggin 				goto fput_fail;
1540f52e0c11SAl Viro 		}
154131e6b01fSNick Piggin 
154231e6b01fSNick Piggin 		nd->path = file->f_path;
1543e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
154431e6b01fSNick Piggin 			if (fput_needed)
154570e9b357SAl Viro 				*fp = file;
1546c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
154731e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
154831e6b01fSNick Piggin 			rcu_read_lock();
15495590ff0dSUlrich Drepper 		} else {
15505dd784d0SJan Blunck 			path_get(&file->f_path);
15515590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
15521da177e4SLinus Torvalds 		}
1553e41f7d4eSAl Viro 	}
1554e41f7d4eSAl Viro 
155531e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
15569b4a9b14SAl Viro 	return 0;
15572dfdd266SJosef 'Jeff' Sipek 
15589b4a9b14SAl Viro fput_fail:
15599b4a9b14SAl Viro 	fput_light(file, fput_needed);
15609b4a9b14SAl Viro out_fail:
15619b4a9b14SAl Viro 	return retval;
15629b4a9b14SAl Viro }
15639b4a9b14SAl Viro 
1564bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1565bd92d7feSAl Viro {
1566bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1567bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1568bd92d7feSAl Viro 
1569bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1570bd92d7feSAl Viro 	return walk_component(nd, path, &nd->last, nd->last_type,
1571bd92d7feSAl Viro 					nd->flags & LOOKUP_FOLLOW);
1572bd92d7feSAl Viro }
1573bd92d7feSAl Viro 
15749b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1575ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
15769b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
15779b4a9b14SAl Viro {
157870e9b357SAl Viro 	struct file *base = NULL;
1579bd92d7feSAl Viro 	struct path path;
1580bd92d7feSAl Viro 	int err;
158131e6b01fSNick Piggin 
158231e6b01fSNick Piggin 	/*
158331e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
158431e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
158531e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
158631e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
158731e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
158831e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
158931e6b01fSNick Piggin 	 * analogue, foo_rcu().
159031e6b01fSNick Piggin 	 *
159131e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
159231e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
159331e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
159431e6b01fSNick Piggin 	 * be able to complete).
159531e6b01fSNick Piggin 	 */
1596bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1597ee0827cdSAl Viro 
1598bd92d7feSAl Viro 	if (unlikely(err))
1599bd92d7feSAl Viro 		return err;
1600ee0827cdSAl Viro 
1601ee0827cdSAl Viro 	current->total_link_count = 0;
1602bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1603bd92d7feSAl Viro 
1604bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1605bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1606bd92d7feSAl Viro 		while (err > 0) {
1607bd92d7feSAl Viro 			void *cookie;
1608bd92d7feSAl Viro 			struct path link = path;
1609bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1610574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
1611bd92d7feSAl Viro 			if (!err)
1612bd92d7feSAl Viro 				err = lookup_last(nd, &path);
1613574197e0SAl Viro 			put_link(nd, &link, cookie);
1614bd92d7feSAl Viro 		}
1615bd92d7feSAl Viro 	}
1616ee0827cdSAl Viro 
16179f1fafeeSAl Viro 	if (!err)
16189f1fafeeSAl Viro 		err = complete_walk(nd);
1619bd92d7feSAl Viro 
1620bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1621bd92d7feSAl Viro 		if (!nd->inode->i_op->lookup) {
1622bd92d7feSAl Viro 			path_put(&nd->path);
1623bd23a539SAl Viro 			err = -ENOTDIR;
1624bd92d7feSAl Viro 		}
1625bd92d7feSAl Viro 	}
162616c2cd71SAl Viro 
162770e9b357SAl Viro 	if (base)
162870e9b357SAl Viro 		fput(base);
1629ee0827cdSAl Viro 
16305b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
163131e6b01fSNick Piggin 		path_put(&nd->root);
163231e6b01fSNick Piggin 		nd->root.mnt = NULL;
163331e6b01fSNick Piggin 	}
1634bd92d7feSAl Viro 	return err;
163531e6b01fSNick Piggin }
163631e6b01fSNick Piggin 
1637ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1638ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1639ee0827cdSAl Viro {
1640ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1641ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1642ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1643ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1644ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1645ee0827cdSAl Viro 
164631e6b01fSNick Piggin 	if (likely(!retval)) {
164731e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
164831e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
164931e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
165031e6b01fSNick Piggin 		}
165131e6b01fSNick Piggin 	}
1652170aa3d0SUlrich Drepper 	return retval;
16531da177e4SLinus Torvalds }
16541da177e4SLinus Torvalds 
1655c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd)
16565590ff0dSUlrich Drepper {
1657c9c6cac0SAl Viro 	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
16585590ff0dSUlrich Drepper }
16595590ff0dSUlrich Drepper 
1660d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1661d1811465SAl Viro {
1662d1811465SAl Viro 	struct nameidata nd;
1663d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1664d1811465SAl Viro 	if (!res)
1665d1811465SAl Viro 		*path = nd.path;
1666d1811465SAl Viro 	return res;
1667d1811465SAl Viro }
1668d1811465SAl Viro 
166916f18200SJosef 'Jeff' Sipek /**
167016f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
167116f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
167216f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
167316f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
167416f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
1675e0a01249SAl Viro  * @path: pointer to struct path to fill
167616f18200SJosef 'Jeff' Sipek  */
167716f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
167816f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
1679e0a01249SAl Viro 		    struct path *path)
168016f18200SJosef 'Jeff' Sipek {
1681e0a01249SAl Viro 	struct nameidata nd;
1682e0a01249SAl Viro 	int err;
1683e0a01249SAl Viro 	nd.root.dentry = dentry;
1684e0a01249SAl Viro 	nd.root.mnt = mnt;
1685e0a01249SAl Viro 	BUG_ON(flags & LOOKUP_PARENT);
16865b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1687e0a01249SAl Viro 	err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1688e0a01249SAl Viro 	if (!err)
1689e0a01249SAl Viro 		*path = nd.path;
1690e0a01249SAl Viro 	return err;
169116f18200SJosef 'Jeff' Sipek }
169216f18200SJosef 'Jeff' Sipek 
1693eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1694eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
16951da177e4SLinus Torvalds {
169681fca444SChristoph Hellwig 	struct inode *inode = base->d_inode;
16971da177e4SLinus Torvalds 	struct dentry *dentry;
16981da177e4SLinus Torvalds 	int err;
16991da177e4SLinus Torvalds 
17004ad5abb3SAl Viro 	err = inode_permission(inode, MAY_EXEC);
170181fca444SChristoph Hellwig 	if (err)
170281fca444SChristoph Hellwig 		return ERR_PTR(err);
17031da177e4SLinus Torvalds 
17041da177e4SLinus Torvalds 	/*
1705b04f784eSNick Piggin 	 * Don't bother with __d_lookup: callers are for creat as
1706b04f784eSNick Piggin 	 * well as unlink, so a lot of the time it would cost
1707b04f784eSNick Piggin 	 * a double lookup.
17086e6b1bd1SAl Viro 	 */
17096e6b1bd1SAl Viro 	dentry = d_lookup(base, name);
17106e6b1bd1SAl Viro 
171144396f4bSJosef Bacik 	if (dentry && d_need_lookup(dentry)) {
171244396f4bSJosef Bacik 		/*
171344396f4bSJosef Bacik 		 * __lookup_hash is called with the parent dir's i_mutex already
171444396f4bSJosef Bacik 		 * held, so we are good to go here.
171544396f4bSJosef Bacik 		 */
171644396f4bSJosef Bacik 		dentry = d_inode_lookup(base, dentry, nd);
171744396f4bSJosef Bacik 		if (IS_ERR(dentry))
171844396f4bSJosef Bacik 			return dentry;
171944396f4bSJosef Bacik 	}
172044396f4bSJosef Bacik 
1721d2d9e9fbSAl Viro 	if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1722d2d9e9fbSAl Viro 		int status = d_revalidate(dentry, nd);
1723d2d9e9fbSAl Viro 		if (unlikely(status <= 0)) {
1724d2d9e9fbSAl Viro 			/*
1725d2d9e9fbSAl Viro 			 * The dentry failed validation.
1726d2d9e9fbSAl Viro 			 * If d_revalidate returned 0 attempt to invalidate
1727d2d9e9fbSAl Viro 			 * the dentry otherwise d_revalidate is asking us
1728d2d9e9fbSAl Viro 			 * to return a fail status.
1729d2d9e9fbSAl Viro 			 */
1730d2d9e9fbSAl Viro 			if (status < 0) {
1731d2d9e9fbSAl Viro 				dput(dentry);
1732d2d9e9fbSAl Viro 				return ERR_PTR(status);
1733d2d9e9fbSAl Viro 			} else if (!d_invalidate(dentry)) {
1734d2d9e9fbSAl Viro 				dput(dentry);
1735d2d9e9fbSAl Viro 				dentry = NULL;
1736d2d9e9fbSAl Viro 			}
1737d2d9e9fbSAl Viro 		}
1738d2d9e9fbSAl Viro 	}
17396e6b1bd1SAl Viro 
17401da177e4SLinus Torvalds 	if (!dentry)
1741baa03890SNick Piggin 		dentry = d_alloc_and_lookup(base, name, nd);
17425a202bcdSAl Viro 
17431da177e4SLinus Torvalds 	return dentry;
17441da177e4SLinus Torvalds }
17451da177e4SLinus Torvalds 
1746057f6c01SJames Morris /*
1747057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1748057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1749057f6c01SJames Morris  * SMP-safe.
1750057f6c01SJames Morris  */
1751a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
17521da177e4SLinus Torvalds {
17534ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
17541da177e4SLinus Torvalds }
17551da177e4SLinus Torvalds 
1756eead1911SChristoph Hellwig /**
1757a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1758eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1759eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1760eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1761eead1911SChristoph Hellwig  *
1762a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1763a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1764eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1765eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1766eead1911SChristoph Hellwig  */
1767057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1768057f6c01SJames Morris {
1769057f6c01SJames Morris 	struct qstr this;
17706a96ba54SAl Viro 	unsigned long hash;
17716a96ba54SAl Viro 	unsigned int c;
1772057f6c01SJames Morris 
17732f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
17742f9092e1SDavid Woodhouse 
17756a96ba54SAl Viro 	this.name = name;
17766a96ba54SAl Viro 	this.len = len;
17776a96ba54SAl Viro 	if (!len)
17786a96ba54SAl Viro 		return ERR_PTR(-EACCES);
17796a96ba54SAl Viro 
17806a96ba54SAl Viro 	hash = init_name_hash();
17816a96ba54SAl Viro 	while (len--) {
17826a96ba54SAl Viro 		c = *(const unsigned char *)name++;
17836a96ba54SAl Viro 		if (c == '/' || c == '\0')
17846a96ba54SAl Viro 			return ERR_PTR(-EACCES);
17856a96ba54SAl Viro 		hash = partial_name_hash(c, hash);
17866a96ba54SAl Viro 	}
17876a96ba54SAl Viro 	this.hash = end_name_hash(hash);
17885a202bcdSAl Viro 	/*
17895a202bcdSAl Viro 	 * See if the low-level filesystem might want
17905a202bcdSAl Viro 	 * to use its own hash..
17915a202bcdSAl Viro 	 */
17925a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
17935a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
17945a202bcdSAl Viro 		if (err < 0)
17955a202bcdSAl Viro 			return ERR_PTR(err);
17965a202bcdSAl Viro 	}
1797eead1911SChristoph Hellwig 
179849705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1799057f6c01SJames Morris }
1800057f6c01SJames Morris 
18012d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
18022d8f3038SAl Viro 		 struct path *path)
18031da177e4SLinus Torvalds {
18042d8f3038SAl Viro 	struct nameidata nd;
1805f52e0c11SAl Viro 	char *tmp = getname_flags(name, flags);
18061da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
18071da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
18082d8f3038SAl Viro 
18092d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
18102d8f3038SAl Viro 
18112d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
18121da177e4SLinus Torvalds 		putname(tmp);
18132d8f3038SAl Viro 		if (!err)
18142d8f3038SAl Viro 			*path = nd.path;
18151da177e4SLinus Torvalds 	}
18161da177e4SLinus Torvalds 	return err;
18171da177e4SLinus Torvalds }
18181da177e4SLinus Torvalds 
18192ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
18202ad94ae6SAl Viro 			struct nameidata *nd, char **name)
18212ad94ae6SAl Viro {
18222ad94ae6SAl Viro 	char *s = getname(path);
18232ad94ae6SAl Viro 	int error;
18242ad94ae6SAl Viro 
18252ad94ae6SAl Viro 	if (IS_ERR(s))
18262ad94ae6SAl Viro 		return PTR_ERR(s);
18272ad94ae6SAl Viro 
18282ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
18292ad94ae6SAl Viro 	if (error)
18302ad94ae6SAl Viro 		putname(s);
18312ad94ae6SAl Viro 	else
18322ad94ae6SAl Viro 		*name = s;
18332ad94ae6SAl Viro 
18342ad94ae6SAl Viro 	return error;
18352ad94ae6SAl Viro }
18362ad94ae6SAl Viro 
18371da177e4SLinus Torvalds /*
18381da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
18391da177e4SLinus Torvalds  * minimal.
18401da177e4SLinus Torvalds  */
18411da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
18421da177e4SLinus Torvalds {
1843da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1844da9592edSDavid Howells 
18451da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
18461da177e4SLinus Torvalds 		return 0;
1847e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
1848e795b717SSerge E. Hallyn 		goto other_userns;
1849da9592edSDavid Howells 	if (inode->i_uid == fsuid)
18501da177e4SLinus Torvalds 		return 0;
1851da9592edSDavid Howells 	if (dir->i_uid == fsuid)
18521da177e4SLinus Torvalds 		return 0;
1853e795b717SSerge E. Hallyn 
1854e795b717SSerge E. Hallyn other_userns:
1855e795b717SSerge E. Hallyn 	return !ns_capable(inode_userns(inode), CAP_FOWNER);
18561da177e4SLinus Torvalds }
18571da177e4SLinus Torvalds 
18581da177e4SLinus Torvalds /*
18591da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
18601da177e4SLinus Torvalds  *  whether the type of victim is right.
18611da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
18621da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
18631da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
18641da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
18651da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
18661da177e4SLinus Torvalds  *	a. be owner of dir, or
18671da177e4SLinus Torvalds  *	b. be owner of victim, or
18681da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
18691da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
18701da177e4SLinus Torvalds  *     links pointing to it.
18711da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
18721da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
18731da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
18741da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
18751da177e4SLinus Torvalds  *     nfs_async_unlink().
18761da177e4SLinus Torvalds  */
1877858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
18781da177e4SLinus Torvalds {
18791da177e4SLinus Torvalds 	int error;
18801da177e4SLinus Torvalds 
18811da177e4SLinus Torvalds 	if (!victim->d_inode)
18821da177e4SLinus Torvalds 		return -ENOENT;
18831da177e4SLinus Torvalds 
18841da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1885cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
18861da177e4SLinus Torvalds 
1887f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
18881da177e4SLinus Torvalds 	if (error)
18891da177e4SLinus Torvalds 		return error;
18901da177e4SLinus Torvalds 	if (IS_APPEND(dir))
18911da177e4SLinus Torvalds 		return -EPERM;
18921da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1893f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
18941da177e4SLinus Torvalds 		return -EPERM;
18951da177e4SLinus Torvalds 	if (isdir) {
18961da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
18971da177e4SLinus Torvalds 			return -ENOTDIR;
18981da177e4SLinus Torvalds 		if (IS_ROOT(victim))
18991da177e4SLinus Torvalds 			return -EBUSY;
19001da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
19011da177e4SLinus Torvalds 		return -EISDIR;
19021da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
19031da177e4SLinus Torvalds 		return -ENOENT;
19041da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
19051da177e4SLinus Torvalds 		return -EBUSY;
19061da177e4SLinus Torvalds 	return 0;
19071da177e4SLinus Torvalds }
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
19101da177e4SLinus Torvalds  *  dir.
19111da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
19121da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
19131da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
19141da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
19151da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
19161da177e4SLinus Torvalds  */
1917a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
19181da177e4SLinus Torvalds {
19191da177e4SLinus Torvalds 	if (child->d_inode)
19201da177e4SLinus Torvalds 		return -EEXIST;
19211da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
19221da177e4SLinus Torvalds 		return -ENOENT;
1923f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
19241da177e4SLinus Torvalds }
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds /*
19271da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
19281da177e4SLinus Torvalds  */
19291da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
19301da177e4SLinus Torvalds {
19311da177e4SLinus Torvalds 	struct dentry *p;
19321da177e4SLinus Torvalds 
19331da177e4SLinus Torvalds 	if (p1 == p2) {
1934f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
19351da177e4SLinus Torvalds 		return NULL;
19361da177e4SLinus Torvalds 	}
19371da177e4SLinus Torvalds 
1938a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19391da177e4SLinus Torvalds 
1940e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1941e2761a11SOGAWA Hirofumi 	if (p) {
1942f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1943f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
19441da177e4SLinus Torvalds 		return p;
19451da177e4SLinus Torvalds 	}
19461da177e4SLinus Torvalds 
1947e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1948e2761a11SOGAWA Hirofumi 	if (p) {
1949f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1950f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19511da177e4SLinus Torvalds 		return p;
19521da177e4SLinus Torvalds 	}
19531da177e4SLinus Torvalds 
1954f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1955f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19561da177e4SLinus Torvalds 	return NULL;
19571da177e4SLinus Torvalds }
19581da177e4SLinus Torvalds 
19591da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
19601da177e4SLinus Torvalds {
19611b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
19621da177e4SLinus Torvalds 	if (p1 != p2) {
19631b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1964a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19651da177e4SLinus Torvalds 	}
19661da177e4SLinus Torvalds }
19671da177e4SLinus Torvalds 
19681da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
19691da177e4SLinus Torvalds 		struct nameidata *nd)
19701da177e4SLinus Torvalds {
1971a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
19721da177e4SLinus Torvalds 
19731da177e4SLinus Torvalds 	if (error)
19741da177e4SLinus Torvalds 		return error;
19751da177e4SLinus Torvalds 
1976acfa4380SAl Viro 	if (!dir->i_op->create)
19771da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
19781da177e4SLinus Torvalds 	mode &= S_IALLUGO;
19791da177e4SLinus Torvalds 	mode |= S_IFREG;
19801da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
19811da177e4SLinus Torvalds 	if (error)
19821da177e4SLinus Torvalds 		return error;
19831da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1984a74574aaSStephen Smalley 	if (!error)
1985f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
19861da177e4SLinus Torvalds 	return error;
19871da177e4SLinus Torvalds }
19881da177e4SLinus Torvalds 
198973d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
19901da177e4SLinus Torvalds {
19913fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
19921da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
19931da177e4SLinus Torvalds 	int error;
19941da177e4SLinus Torvalds 
1995bcda7652SAl Viro 	/* O_PATH? */
1996bcda7652SAl Viro 	if (!acc_mode)
1997bcda7652SAl Viro 		return 0;
1998bcda7652SAl Viro 
19991da177e4SLinus Torvalds 	if (!inode)
20001da177e4SLinus Torvalds 		return -ENOENT;
20011da177e4SLinus Torvalds 
2002c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2003c8fe8f30SChristoph Hellwig 	case S_IFLNK:
20041da177e4SLinus Torvalds 		return -ELOOP;
2005c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2006c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
20071da177e4SLinus Torvalds 			return -EISDIR;
2008c8fe8f30SChristoph Hellwig 		break;
2009c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2010c8fe8f30SChristoph Hellwig 	case S_IFCHR:
20113fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
20121da177e4SLinus Torvalds 			return -EACCES;
2013c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
2014c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2015c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
20161da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2017c8fe8f30SChristoph Hellwig 		break;
20184a3fd211SDave Hansen 	}
2019b41572e9SDave Hansen 
20203fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2021b41572e9SDave Hansen 	if (error)
2022b41572e9SDave Hansen 		return error;
20236146f0d5SMimi Zohar 
20241da177e4SLinus Torvalds 	/*
20251da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
20261da177e4SLinus Torvalds 	 */
20271da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
20288737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
20297715b521SAl Viro 			return -EPERM;
20301da177e4SLinus Torvalds 		if (flag & O_TRUNC)
20317715b521SAl Viro 			return -EPERM;
20321da177e4SLinus Torvalds 	}
20331da177e4SLinus Torvalds 
20341da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
20352e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
20367715b521SAl Viro 		return -EPERM;
20371da177e4SLinus Torvalds 
2038f3c7691eSJ. Bruce Fields 	return 0;
20397715b521SAl Viro }
20407715b521SAl Viro 
2041e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
20427715b521SAl Viro {
2043e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
20447715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
20457715b521SAl Viro 	int error = get_write_access(inode);
20461da177e4SLinus Torvalds 	if (error)
20477715b521SAl Viro 		return error;
20481da177e4SLinus Torvalds 	/*
20491da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
20501da177e4SLinus Torvalds 	 */
20511da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2052be6d3e56SKentaro Takeda 	if (!error)
2053ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
20541da177e4SLinus Torvalds 	if (!error) {
20557715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2056d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2057e1181ee6SJeff Layton 				    filp);
20581da177e4SLinus Torvalds 	}
20591da177e4SLinus Torvalds 	put_write_access(inode);
2060acd0c935SMimi Zohar 	return error;
20611da177e4SLinus Torvalds }
20621da177e4SLinus Torvalds 
2063d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2064d57999e1SDave Hansen {
20658a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
20668a5e929dSAl Viro 		flag--;
2067d57999e1SDave Hansen 	return flag;
2068d57999e1SDave Hansen }
2069d57999e1SDave Hansen 
207031e6b01fSNick Piggin /*
2071fe2d35ffSAl Viro  * Handle the last step of open()
207231e6b01fSNick Piggin  */
2073fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
2074c3e380b0SAl Viro 			    const struct open_flags *op, const char *pathname)
2075fb1cc555SAl Viro {
2076a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
20776c0d46c4SAl Viro 	struct dentry *dentry;
2078ca344a89SAl Viro 	int open_flag = op->open_flag;
20796c0d46c4SAl Viro 	int will_truncate = open_flag & O_TRUNC;
2080ca344a89SAl Viro 	int want_write = 0;
2081bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2082fb1cc555SAl Viro 	struct file *filp;
208316c2cd71SAl Viro 	int error;
2084fb1cc555SAl Viro 
2085c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2086c3e380b0SAl Viro 	nd->flags |= op->intent;
2087c3e380b0SAl Viro 
20881f36f774SAl Viro 	switch (nd->last_type) {
20891f36f774SAl Viro 	case LAST_DOTDOT:
2090176306f5SNeil Brown 	case LAST_DOT:
2091fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2092fe2d35ffSAl Viro 		if (error)
2093fe2d35ffSAl Viro 			return ERR_PTR(error);
20941f36f774SAl Viro 		/* fallthrough */
20951f36f774SAl Viro 	case LAST_ROOT:
20969f1fafeeSAl Viro 		error = complete_walk(nd);
209716c2cd71SAl Viro 		if (error)
20989f1fafeeSAl Viro 			return ERR_PTR(error);
2099fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2100ca344a89SAl Viro 		if (open_flag & O_CREAT) {
210116c2cd71SAl Viro 			error = -EISDIR;
21021f36f774SAl Viro 			goto exit;
2103fe2d35ffSAl Viro 		}
2104fe2d35ffSAl Viro 		goto ok;
21051f36f774SAl Viro 	case LAST_BIND:
21069f1fafeeSAl Viro 		error = complete_walk(nd);
210716c2cd71SAl Viro 		if (error)
21089f1fafeeSAl Viro 			return ERR_PTR(error);
21091f36f774SAl Viro 		audit_inode(pathname, dir);
21101f36f774SAl Viro 		goto ok;
21111f36f774SAl Viro 	}
2112a2c36b45SAl Viro 
2113ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2114bcda7652SAl Viro 		int symlink_ok = 0;
2115fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2116fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2117bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2118bcda7652SAl Viro 			symlink_ok = 1;
2119fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2120ce57dfc1SAl Viro 		error = walk_component(nd, path, &nd->last, LAST_NORM,
2121ce57dfc1SAl Viro 					!symlink_ok);
2122ce57dfc1SAl Viro 		if (error < 0)
2123fe2d35ffSAl Viro 			return ERR_PTR(error);
2124ce57dfc1SAl Viro 		if (error) /* symlink */
2125fe2d35ffSAl Viro 			return NULL;
2126fe2d35ffSAl Viro 		/* sayonara */
21279f1fafeeSAl Viro 		error = complete_walk(nd);
21289f1fafeeSAl Viro 		if (error)
2129fe2d35ffSAl Viro 			return ERR_PTR(-ECHILD);
2130fe2d35ffSAl Viro 
2131fe2d35ffSAl Viro 		error = -ENOTDIR;
2132fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_DIRECTORY) {
2133ce57dfc1SAl Viro 			if (!nd->inode->i_op->lookup)
2134fe2d35ffSAl Viro 				goto exit;
2135fe2d35ffSAl Viro 		}
2136fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2137fe2d35ffSAl Viro 		goto ok;
2138fe2d35ffSAl Viro 	}
2139fe2d35ffSAl Viro 
2140fe2d35ffSAl Viro 	/* create side of things */
21419f1fafeeSAl Viro 	error = complete_walk(nd);
21429f1fafeeSAl Viro 	if (error)
21439f1fafeeSAl Viro 		return ERR_PTR(error);
2144fe2d35ffSAl Viro 
2145fe2d35ffSAl Viro 	audit_inode(pathname, dir);
214616c2cd71SAl Viro 	error = -EISDIR;
21471f36f774SAl Viro 	/* trailing slashes? */
214831e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
21491f36f774SAl Viro 		goto exit;
21501f36f774SAl Viro 
2151a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2152a1e28038SAl Viro 
21536c0d46c4SAl Viro 	dentry = lookup_hash(nd);
21546c0d46c4SAl Viro 	error = PTR_ERR(dentry);
21556c0d46c4SAl Viro 	if (IS_ERR(dentry)) {
2156fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2157fb1cc555SAl Viro 		goto exit;
2158fb1cc555SAl Viro 	}
2159fb1cc555SAl Viro 
21606c0d46c4SAl Viro 	path->dentry = dentry;
21616c0d46c4SAl Viro 	path->mnt = nd->path.mnt;
21626c0d46c4SAl Viro 
2163fb1cc555SAl Viro 	/* Negative dentry, just create the file */
21646c0d46c4SAl Viro 	if (!dentry->d_inode) {
21656c0d46c4SAl Viro 		int mode = op->mode;
21666c0d46c4SAl Viro 		if (!IS_POSIXACL(dir->d_inode))
21676c0d46c4SAl Viro 			mode &= ~current_umask();
2168fb1cc555SAl Viro 		/*
2169fb1cc555SAl Viro 		 * This write is needed to ensure that a
21706c0d46c4SAl Viro 		 * rw->ro transition does not occur between
2171fb1cc555SAl Viro 		 * the time when the file is created and when
2172fb1cc555SAl Viro 		 * a permanent write count is taken through
2173fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2174fb1cc555SAl Viro 		 */
2175fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2176fb1cc555SAl Viro 		if (error)
2177fb1cc555SAl Viro 			goto exit_mutex_unlock;
2178ca344a89SAl Viro 		want_write = 1;
21799b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2180ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
21816c0d46c4SAl Viro 		will_truncate = 0;
2182bcda7652SAl Viro 		acc_mode = MAY_OPEN;
21836c0d46c4SAl Viro 		error = security_path_mknod(&nd->path, dentry, mode, 0);
21846c0d46c4SAl Viro 		if (error)
21856c0d46c4SAl Viro 			goto exit_mutex_unlock;
21866c0d46c4SAl Viro 		error = vfs_create(dir->d_inode, dentry, mode, nd);
21876c0d46c4SAl Viro 		if (error)
21886c0d46c4SAl Viro 			goto exit_mutex_unlock;
21896c0d46c4SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
21906c0d46c4SAl Viro 		dput(nd->path.dentry);
21916c0d46c4SAl Viro 		nd->path.dentry = dentry;
2192ca344a89SAl Viro 		goto common;
2193fb1cc555SAl Viro 	}
2194fb1cc555SAl Viro 
2195fb1cc555SAl Viro 	/*
2196fb1cc555SAl Viro 	 * It already exists.
2197fb1cc555SAl Viro 	 */
2198fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2199fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2200fb1cc555SAl Viro 
2201fb1cc555SAl Viro 	error = -EEXIST;
2202ca344a89SAl Viro 	if (open_flag & O_EXCL)
2203fb1cc555SAl Viro 		goto exit_dput;
2204fb1cc555SAl Viro 
22059875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
22069875cf80SDavid Howells 	if (error < 0)
2207fb1cc555SAl Viro 		goto exit_dput;
2208fb1cc555SAl Viro 
2209fb1cc555SAl Viro 	error = -ENOENT;
2210fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2211fb1cc555SAl Viro 		goto exit_dput;
22129e67f361SAl Viro 
22139e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2214fb1cc555SAl Viro 		return NULL;
2215fb1cc555SAl Viro 
2216fb1cc555SAl Viro 	path_to_nameidata(path, nd);
221731e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2218fb1cc555SAl Viro 	error = -EISDIR;
221931e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2220fb1cc555SAl Viro 		goto exit;
222167ee3ad2SAl Viro ok:
22226c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
22236c0d46c4SAl Viro 		will_truncate = 0;
22246c0d46c4SAl Viro 
22250f9d1a10SAl Viro 	if (will_truncate) {
22260f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
22270f9d1a10SAl Viro 		if (error)
22280f9d1a10SAl Viro 			goto exit;
2229ca344a89SAl Viro 		want_write = 1;
22300f9d1a10SAl Viro 	}
2231ca344a89SAl Viro common:
2232bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2233ca344a89SAl Viro 	if (error)
22340f9d1a10SAl Viro 		goto exit;
22350f9d1a10SAl Viro 	filp = nameidata_to_filp(nd);
22360f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
22370f9d1a10SAl Viro 		error = ima_file_check(filp, op->acc_mode);
22380f9d1a10SAl Viro 		if (error) {
22390f9d1a10SAl Viro 			fput(filp);
22400f9d1a10SAl Viro 			filp = ERR_PTR(error);
22410f9d1a10SAl Viro 		}
22420f9d1a10SAl Viro 	}
22430f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
22440f9d1a10SAl Viro 		if (will_truncate) {
22450f9d1a10SAl Viro 			error = handle_truncate(filp);
22460f9d1a10SAl Viro 			if (error) {
22470f9d1a10SAl Viro 				fput(filp);
22480f9d1a10SAl Viro 				filp = ERR_PTR(error);
22490f9d1a10SAl Viro 			}
22500f9d1a10SAl Viro 		}
22510f9d1a10SAl Viro 	}
2252ca344a89SAl Viro out:
2253ca344a89SAl Viro 	if (want_write)
22540f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
22550f9d1a10SAl Viro 	path_put(&nd->path);
2256fb1cc555SAl Viro 	return filp;
2257fb1cc555SAl Viro 
2258fb1cc555SAl Viro exit_mutex_unlock:
2259fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2260fb1cc555SAl Viro exit_dput:
2261fb1cc555SAl Viro 	path_put_conditional(path, nd);
2262fb1cc555SAl Viro exit:
2263ca344a89SAl Viro 	filp = ERR_PTR(error);
2264ca344a89SAl Viro 	goto out;
2265fb1cc555SAl Viro }
2266fb1cc555SAl Viro 
226713aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
226873d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
22691da177e4SLinus Torvalds {
2270fe2d35ffSAl Viro 	struct file *base = NULL;
22714a3fd211SDave Hansen 	struct file *filp;
22729850c056SAl Viro 	struct path path;
227313aab428SAl Viro 	int error;
227431e6b01fSNick Piggin 
227531e6b01fSNick Piggin 	filp = get_empty_filp();
227631e6b01fSNick Piggin 	if (!filp)
227731e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
227831e6b01fSNick Piggin 
227947c805dcSAl Viro 	filp->f_flags = op->open_flag;
228073d049a4SAl Viro 	nd->intent.open.file = filp;
228173d049a4SAl Viro 	nd->intent.open.flags = open_to_namei_flags(op->open_flag);
228273d049a4SAl Viro 	nd->intent.open.create_mode = op->mode;
228331e6b01fSNick Piggin 
228473d049a4SAl Viro 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
228531e6b01fSNick Piggin 	if (unlikely(error))
228613aab428SAl Viro 		goto out_filp;
228731e6b01fSNick Piggin 
2288fe2d35ffSAl Viro 	current->total_link_count = 0;
228973d049a4SAl Viro 	error = link_path_walk(pathname, nd);
229031e6b01fSNick Piggin 	if (unlikely(error))
229131e6b01fSNick Piggin 		goto out_filp;
22921da177e4SLinus Torvalds 
229373d049a4SAl Viro 	filp = do_last(nd, &path, op, pathname);
2294806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
22957b9337aaSNick Piggin 		struct path link = path;
2296def4af30SAl Viro 		void *cookie;
2297574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
229873d049a4SAl Viro 			path_put_conditional(&path, nd);
229973d049a4SAl Viro 			path_put(&nd->path);
230040b39136SAl Viro 			filp = ERR_PTR(-ELOOP);
230140b39136SAl Viro 			break;
230240b39136SAl Viro 		}
230373d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
230473d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2305574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
2306c3e380b0SAl Viro 		if (unlikely(error))
2307f1afe9efSAl Viro 			filp = ERR_PTR(error);
2308c3e380b0SAl Viro 		else
230973d049a4SAl Viro 			filp = do_last(nd, &path, op, pathname);
2310574197e0SAl Viro 		put_link(nd, &link, cookie);
2311806b681cSAl Viro 	}
231210fa8e62SAl Viro out:
231373d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
231473d049a4SAl Viro 		path_put(&nd->root);
2315fe2d35ffSAl Viro 	if (base)
2316fe2d35ffSAl Viro 		fput(base);
231773d049a4SAl Viro 	release_open_intent(nd);
231810fa8e62SAl Viro 	return filp;
23191da177e4SLinus Torvalds 
232031e6b01fSNick Piggin out_filp:
232110fa8e62SAl Viro 	filp = ERR_PTR(error);
232210fa8e62SAl Viro 	goto out;
2323de459215SKirill Korotaev }
23241da177e4SLinus Torvalds 
232513aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
232613aab428SAl Viro 		const struct open_flags *op, int flags)
232713aab428SAl Viro {
232873d049a4SAl Viro 	struct nameidata nd;
232913aab428SAl Viro 	struct file *filp;
233013aab428SAl Viro 
233173d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
233213aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
233373d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
233413aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
233573d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
233613aab428SAl Viro 	return filp;
233713aab428SAl Viro }
233813aab428SAl Viro 
233973d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
234073d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
234173d049a4SAl Viro {
234273d049a4SAl Viro 	struct nameidata nd;
234373d049a4SAl Viro 	struct file *file;
234473d049a4SAl Viro 
234573d049a4SAl Viro 	nd.root.mnt = mnt;
234673d049a4SAl Viro 	nd.root.dentry = dentry;
234773d049a4SAl Viro 
234873d049a4SAl Viro 	flags |= LOOKUP_ROOT;
234973d049a4SAl Viro 
2350bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
235173d049a4SAl Viro 		return ERR_PTR(-ELOOP);
235273d049a4SAl Viro 
235373d049a4SAl Viro 	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
235473d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
235573d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags);
235673d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
235773d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
235873d049a4SAl Viro 	return file;
235973d049a4SAl Viro }
236073d049a4SAl Viro 
2361ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
23621da177e4SLinus Torvalds {
2363c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
2364ed75e95dSAl Viro 	struct nameidata nd;
2365ed75e95dSAl Viro 	int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2366ed75e95dSAl Viro 	if (error)
2367ed75e95dSAl Viro 		return ERR_PTR(error);
23681da177e4SLinus Torvalds 
2369c663e5d8SChristoph Hellwig 	/*
2370c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2371c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2372c663e5d8SChristoph Hellwig 	 */
2373ed75e95dSAl Viro 	if (nd.last_type != LAST_NORM)
2374ed75e95dSAl Viro 		goto out;
2375ed75e95dSAl Viro 	nd.flags &= ~LOOKUP_PARENT;
2376ed75e95dSAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2377ed75e95dSAl Viro 	nd.intent.open.flags = O_EXCL;
2378c663e5d8SChristoph Hellwig 
2379c663e5d8SChristoph Hellwig 	/*
2380c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2381c663e5d8SChristoph Hellwig 	 */
2382ed75e95dSAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2383ed75e95dSAl Viro 	dentry = lookup_hash(&nd);
23841da177e4SLinus Torvalds 	if (IS_ERR(dentry))
23851da177e4SLinus Torvalds 		goto fail;
2386c663e5d8SChristoph Hellwig 
2387e9baf6e5SAl Viro 	if (dentry->d_inode)
2388e9baf6e5SAl Viro 		goto eexist;
2389c663e5d8SChristoph Hellwig 	/*
2390c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2391c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2392c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2393c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2394c663e5d8SChristoph Hellwig 	 */
2395ed75e95dSAl Viro 	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
23961da177e4SLinus Torvalds 		dput(dentry);
23971da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2398ed75e95dSAl Viro 		goto fail;
2399e9baf6e5SAl Viro 	}
2400ed75e95dSAl Viro 	*path = nd.path;
2401e9baf6e5SAl Viro 	return dentry;
2402e9baf6e5SAl Viro eexist:
2403e9baf6e5SAl Viro 	dput(dentry);
2404e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
24051da177e4SLinus Torvalds fail:
2406dae6ad8fSAl Viro 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2407ed75e95dSAl Viro out:
2408dae6ad8fSAl Viro 	path_put(&nd.path);
2409ed75e95dSAl Viro 	return dentry;
2410dae6ad8fSAl Viro }
2411dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
2412dae6ad8fSAl Viro 
2413dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2414dae6ad8fSAl Viro {
2415dae6ad8fSAl Viro 	char *tmp = getname(pathname);
2416dae6ad8fSAl Viro 	struct dentry *res;
2417dae6ad8fSAl Viro 	if (IS_ERR(tmp))
2418dae6ad8fSAl Viro 		return ERR_CAST(tmp);
2419dae6ad8fSAl Viro 	res = kern_path_create(dfd, tmp, path, is_dir);
2420dae6ad8fSAl Viro 	putname(tmp);
2421dae6ad8fSAl Viro 	return res;
2422dae6ad8fSAl Viro }
2423dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
2424dae6ad8fSAl Viro 
24251da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
24261da177e4SLinus Torvalds {
2427a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
24281da177e4SLinus Torvalds 
24291da177e4SLinus Torvalds 	if (error)
24301da177e4SLinus Torvalds 		return error;
24311da177e4SLinus Torvalds 
2432e795b717SSerge E. Hallyn 	if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2433e795b717SSerge E. Hallyn 	    !ns_capable(inode_userns(dir), CAP_MKNOD))
24341da177e4SLinus Torvalds 		return -EPERM;
24351da177e4SLinus Torvalds 
2436acfa4380SAl Viro 	if (!dir->i_op->mknod)
24371da177e4SLinus Torvalds 		return -EPERM;
24381da177e4SLinus Torvalds 
243908ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
244008ce5f16SSerge E. Hallyn 	if (error)
244108ce5f16SSerge E. Hallyn 		return error;
244208ce5f16SSerge E. Hallyn 
24431da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
24441da177e4SLinus Torvalds 	if (error)
24451da177e4SLinus Torvalds 		return error;
24461da177e4SLinus Torvalds 
24471da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2448a74574aaSStephen Smalley 	if (!error)
2449f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
24501da177e4SLinus Torvalds 	return error;
24511da177e4SLinus Torvalds }
24521da177e4SLinus Torvalds 
2453463c3197SDave Hansen static int may_mknod(mode_t mode)
2454463c3197SDave Hansen {
2455463c3197SDave Hansen 	switch (mode & S_IFMT) {
2456463c3197SDave Hansen 	case S_IFREG:
2457463c3197SDave Hansen 	case S_IFCHR:
2458463c3197SDave Hansen 	case S_IFBLK:
2459463c3197SDave Hansen 	case S_IFIFO:
2460463c3197SDave Hansen 	case S_IFSOCK:
2461463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2462463c3197SDave Hansen 		return 0;
2463463c3197SDave Hansen 	case S_IFDIR:
2464463c3197SDave Hansen 		return -EPERM;
2465463c3197SDave Hansen 	default:
2466463c3197SDave Hansen 		return -EINVAL;
2467463c3197SDave Hansen 	}
2468463c3197SDave Hansen }
2469463c3197SDave Hansen 
24702e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
24712e4d0924SHeiko Carstens 		unsigned, dev)
24721da177e4SLinus Torvalds {
24731da177e4SLinus Torvalds 	struct dentry *dentry;
2474dae6ad8fSAl Viro 	struct path path;
2475dae6ad8fSAl Viro 	int error;
24761da177e4SLinus Torvalds 
24771da177e4SLinus Torvalds 	if (S_ISDIR(mode))
24781da177e4SLinus Torvalds 		return -EPERM;
24791da177e4SLinus Torvalds 
2480dae6ad8fSAl Viro 	dentry = user_path_create(dfd, filename, &path, 0);
2481dae6ad8fSAl Viro 	if (IS_ERR(dentry))
2482dae6ad8fSAl Viro 		return PTR_ERR(dentry);
24832ad94ae6SAl Viro 
2484dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
2485ce3b0f8dSAl Viro 		mode &= ~current_umask();
2486463c3197SDave Hansen 	error = may_mknod(mode);
2487463c3197SDave Hansen 	if (error)
2488463c3197SDave Hansen 		goto out_dput;
2489dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
2490463c3197SDave Hansen 	if (error)
2491463c3197SDave Hansen 		goto out_dput;
2492dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
2493be6d3e56SKentaro Takeda 	if (error)
2494be6d3e56SKentaro Takeda 		goto out_drop_write;
24951da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
24961da177e4SLinus Torvalds 		case 0: case S_IFREG:
2497dae6ad8fSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
24981da177e4SLinus Torvalds 			break;
24991da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
2500dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
25011da177e4SLinus Torvalds 					new_decode_dev(dev));
25021da177e4SLinus Torvalds 			break;
25031da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
2504dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
25051da177e4SLinus Torvalds 			break;
25061da177e4SLinus Torvalds 	}
2507be6d3e56SKentaro Takeda out_drop_write:
2508dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
2509463c3197SDave Hansen out_dput:
25101da177e4SLinus Torvalds 	dput(dentry);
2511dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2512dae6ad8fSAl Viro 	path_put(&path);
25131da177e4SLinus Torvalds 
25141da177e4SLinus Torvalds 	return error;
25151da177e4SLinus Torvalds }
25161da177e4SLinus Torvalds 
25173480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
25185590ff0dSUlrich Drepper {
25195590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
25205590ff0dSUlrich Drepper }
25215590ff0dSUlrich Drepper 
25221da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
25231da177e4SLinus Torvalds {
2524a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
25251da177e4SLinus Torvalds 
25261da177e4SLinus Torvalds 	if (error)
25271da177e4SLinus Torvalds 		return error;
25281da177e4SLinus Torvalds 
2529acfa4380SAl Viro 	if (!dir->i_op->mkdir)
25301da177e4SLinus Torvalds 		return -EPERM;
25311da177e4SLinus Torvalds 
25321da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
25331da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
25341da177e4SLinus Torvalds 	if (error)
25351da177e4SLinus Torvalds 		return error;
25361da177e4SLinus Torvalds 
25371da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2538a74574aaSStephen Smalley 	if (!error)
2539f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
25401da177e4SLinus Torvalds 	return error;
25411da177e4SLinus Torvalds }
25421da177e4SLinus Torvalds 
25432e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
25441da177e4SLinus Torvalds {
25456902d925SDave Hansen 	struct dentry *dentry;
2546dae6ad8fSAl Viro 	struct path path;
2547dae6ad8fSAl Viro 	int error;
25481da177e4SLinus Torvalds 
2549dae6ad8fSAl Viro 	dentry = user_path_create(dfd, pathname, &path, 1);
25506902d925SDave Hansen 	if (IS_ERR(dentry))
2551dae6ad8fSAl Viro 		return PTR_ERR(dentry);
25526902d925SDave Hansen 
2553dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
2554ce3b0f8dSAl Viro 		mode &= ~current_umask();
2555dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
2556463c3197SDave Hansen 	if (error)
2557463c3197SDave Hansen 		goto out_dput;
2558dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
2559be6d3e56SKentaro Takeda 	if (error)
2560be6d3e56SKentaro Takeda 		goto out_drop_write;
2561dae6ad8fSAl Viro 	error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
2562be6d3e56SKentaro Takeda out_drop_write:
2563dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
2564463c3197SDave Hansen out_dput:
25651da177e4SLinus Torvalds 	dput(dentry);
2566dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2567dae6ad8fSAl Viro 	path_put(&path);
25681da177e4SLinus Torvalds 	return error;
25691da177e4SLinus Torvalds }
25701da177e4SLinus Torvalds 
25713cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
25725590ff0dSUlrich Drepper {
25735590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
25745590ff0dSUlrich Drepper }
25755590ff0dSUlrich Drepper 
25761da177e4SLinus Torvalds /*
2577a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
2578a71905f0SSage Weil  * should have a usage count of 2 if we're the only user of this
2579a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
2580a71905f0SSage Weil  * then we drop the dentry now.
25811da177e4SLinus Torvalds  *
25821da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
25831da177e4SLinus Torvalds  * do a
25841da177e4SLinus Torvalds  *
25851da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
25861da177e4SLinus Torvalds  *		return -EBUSY;
25871da177e4SLinus Torvalds  *
25881da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
25891da177e4SLinus Torvalds  * that is still in use by something else..
25901da177e4SLinus Torvalds  */
25911da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
25921da177e4SLinus Torvalds {
25931da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
25941da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
259564252c75SSage Weil 	if (dentry->d_count == 1)
25961da177e4SLinus Torvalds 		__d_drop(dentry);
25971da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
25981da177e4SLinus Torvalds }
25991da177e4SLinus Torvalds 
26001da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
26011da177e4SLinus Torvalds {
26021da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
26031da177e4SLinus Torvalds 
26041da177e4SLinus Torvalds 	if (error)
26051da177e4SLinus Torvalds 		return error;
26061da177e4SLinus Torvalds 
2607acfa4380SAl Viro 	if (!dir->i_op->rmdir)
26081da177e4SLinus Torvalds 		return -EPERM;
26091da177e4SLinus Torvalds 
26101d2ef590SAl Viro 	dget(dentry);
26111b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
2612912dbc15SSage Weil 
26131da177e4SLinus Torvalds 	error = -EBUSY;
2614912dbc15SSage Weil 	if (d_mountpoint(dentry))
2615912dbc15SSage Weil 		goto out;
2616912dbc15SSage Weil 
26171da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
2618912dbc15SSage Weil 	if (error)
2619912dbc15SSage Weil 		goto out;
2620912dbc15SSage Weil 
26213cebde24SSage Weil 	shrink_dcache_parent(dentry);
26221da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
2623912dbc15SSage Weil 	if (error)
2624912dbc15SSage Weil 		goto out;
2625912dbc15SSage Weil 
26261da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
2627d83c49f3SAl Viro 	dont_mount(dentry);
26281da177e4SLinus Torvalds 
2629912dbc15SSage Weil out:
2630912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
26311d2ef590SAl Viro 	dput(dentry);
2632912dbc15SSage Weil 	if (!error)
2633912dbc15SSage Weil 		d_delete(dentry);
26341da177e4SLinus Torvalds 	return error;
26351da177e4SLinus Torvalds }
26361da177e4SLinus Torvalds 
26375590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
26381da177e4SLinus Torvalds {
26391da177e4SLinus Torvalds 	int error = 0;
26401da177e4SLinus Torvalds 	char * name;
26411da177e4SLinus Torvalds 	struct dentry *dentry;
26421da177e4SLinus Torvalds 	struct nameidata nd;
26431da177e4SLinus Torvalds 
26442ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
26451da177e4SLinus Torvalds 	if (error)
26462ad94ae6SAl Viro 		return error;
26471da177e4SLinus Torvalds 
26481da177e4SLinus Torvalds 	switch(nd.last_type) {
26491da177e4SLinus Torvalds 	case LAST_DOTDOT:
26501da177e4SLinus Torvalds 		error = -ENOTEMPTY;
26511da177e4SLinus Torvalds 		goto exit1;
26521da177e4SLinus Torvalds 	case LAST_DOT:
26531da177e4SLinus Torvalds 		error = -EINVAL;
26541da177e4SLinus Torvalds 		goto exit1;
26551da177e4SLinus Torvalds 	case LAST_ROOT:
26561da177e4SLinus Torvalds 		error = -EBUSY;
26571da177e4SLinus Torvalds 		goto exit1;
26581da177e4SLinus Torvalds 	}
26590612d9fbSOGAWA Hirofumi 
26600612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
26610612d9fbSOGAWA Hirofumi 
26624ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
266349705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
26641da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
26656902d925SDave Hansen 	if (IS_ERR(dentry))
26666902d925SDave Hansen 		goto exit2;
2667e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
2668e6bc45d6STheodore Ts'o 		error = -ENOENT;
2669e6bc45d6STheodore Ts'o 		goto exit3;
2670e6bc45d6STheodore Ts'o 	}
26710622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
26720622753bSDave Hansen 	if (error)
26730622753bSDave Hansen 		goto exit3;
2674be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2675be6d3e56SKentaro Takeda 	if (error)
2676be6d3e56SKentaro Takeda 		goto exit4;
26774ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2678be6d3e56SKentaro Takeda exit4:
26790622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
26800622753bSDave Hansen exit3:
26811da177e4SLinus Torvalds 	dput(dentry);
26826902d925SDave Hansen exit2:
26834ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
26841da177e4SLinus Torvalds exit1:
26851d957f9bSJan Blunck 	path_put(&nd.path);
26861da177e4SLinus Torvalds 	putname(name);
26871da177e4SLinus Torvalds 	return error;
26881da177e4SLinus Torvalds }
26891da177e4SLinus Torvalds 
26903cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
26915590ff0dSUlrich Drepper {
26925590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
26935590ff0dSUlrich Drepper }
26945590ff0dSUlrich Drepper 
26951da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
26961da177e4SLinus Torvalds {
26971da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
26981da177e4SLinus Torvalds 
26991da177e4SLinus Torvalds 	if (error)
27001da177e4SLinus Torvalds 		return error;
27011da177e4SLinus Torvalds 
2702acfa4380SAl Viro 	if (!dir->i_op->unlink)
27031da177e4SLinus Torvalds 		return -EPERM;
27041da177e4SLinus Torvalds 
27051b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
27061da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
27071da177e4SLinus Torvalds 		error = -EBUSY;
27081da177e4SLinus Torvalds 	else {
27091da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2710bec1052eSAl Viro 		if (!error) {
27111da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2712bec1052eSAl Viro 			if (!error)
2713d83c49f3SAl Viro 				dont_mount(dentry);
2714bec1052eSAl Viro 		}
27151da177e4SLinus Torvalds 	}
27161b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
27171da177e4SLinus Torvalds 
27181da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
27191da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2720ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
27211da177e4SLinus Torvalds 		d_delete(dentry);
27221da177e4SLinus Torvalds 	}
27230eeca283SRobert Love 
27241da177e4SLinus Torvalds 	return error;
27251da177e4SLinus Torvalds }
27261da177e4SLinus Torvalds 
27271da177e4SLinus Torvalds /*
27281da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
27291b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
27301da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
27311da177e4SLinus Torvalds  * while waiting on the I/O.
27321da177e4SLinus Torvalds  */
27335590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
27341da177e4SLinus Torvalds {
27352ad94ae6SAl Viro 	int error;
27361da177e4SLinus Torvalds 	char *name;
27371da177e4SLinus Torvalds 	struct dentry *dentry;
27381da177e4SLinus Torvalds 	struct nameidata nd;
27391da177e4SLinus Torvalds 	struct inode *inode = NULL;
27401da177e4SLinus Torvalds 
27412ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
27421da177e4SLinus Torvalds 	if (error)
27432ad94ae6SAl Viro 		return error;
27442ad94ae6SAl Viro 
27451da177e4SLinus Torvalds 	error = -EISDIR;
27461da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
27471da177e4SLinus Torvalds 		goto exit1;
27480612d9fbSOGAWA Hirofumi 
27490612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
27500612d9fbSOGAWA Hirofumi 
27514ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
275249705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
27531da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27541da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
27551da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
275650338b88STörök Edwin 		if (nd.last.name[nd.last.len])
275750338b88STörök Edwin 			goto slashes;
27581da177e4SLinus Torvalds 		inode = dentry->d_inode;
275950338b88STörök Edwin 		if (!inode)
2760e6bc45d6STheodore Ts'o 			goto slashes;
27617de9c6eeSAl Viro 		ihold(inode);
27620622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
27630622753bSDave Hansen 		if (error)
27640622753bSDave Hansen 			goto exit2;
2765be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2766be6d3e56SKentaro Takeda 		if (error)
2767be6d3e56SKentaro Takeda 			goto exit3;
27684ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2769be6d3e56SKentaro Takeda exit3:
27700622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
27711da177e4SLinus Torvalds 	exit2:
27721da177e4SLinus Torvalds 		dput(dentry);
27731da177e4SLinus Torvalds 	}
27744ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27751da177e4SLinus Torvalds 	if (inode)
27761da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
27771da177e4SLinus Torvalds exit1:
27781d957f9bSJan Blunck 	path_put(&nd.path);
27791da177e4SLinus Torvalds 	putname(name);
27801da177e4SLinus Torvalds 	return error;
27811da177e4SLinus Torvalds 
27821da177e4SLinus Torvalds slashes:
27831da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
27841da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
27851da177e4SLinus Torvalds 	goto exit2;
27861da177e4SLinus Torvalds }
27871da177e4SLinus Torvalds 
27882e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
27895590ff0dSUlrich Drepper {
27905590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
27915590ff0dSUlrich Drepper 		return -EINVAL;
27925590ff0dSUlrich Drepper 
27935590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
27945590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
27955590ff0dSUlrich Drepper 
27965590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
27975590ff0dSUlrich Drepper }
27985590ff0dSUlrich Drepper 
27993480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
28005590ff0dSUlrich Drepper {
28015590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
28025590ff0dSUlrich Drepper }
28035590ff0dSUlrich Drepper 
2804db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
28051da177e4SLinus Torvalds {
2806a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
28071da177e4SLinus Torvalds 
28081da177e4SLinus Torvalds 	if (error)
28091da177e4SLinus Torvalds 		return error;
28101da177e4SLinus Torvalds 
2811acfa4380SAl Viro 	if (!dir->i_op->symlink)
28121da177e4SLinus Torvalds 		return -EPERM;
28131da177e4SLinus Torvalds 
28141da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
28151da177e4SLinus Torvalds 	if (error)
28161da177e4SLinus Torvalds 		return error;
28171da177e4SLinus Torvalds 
28181da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2819a74574aaSStephen Smalley 	if (!error)
2820f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
28211da177e4SLinus Torvalds 	return error;
28221da177e4SLinus Torvalds }
28231da177e4SLinus Torvalds 
28242e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
28252e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
28261da177e4SLinus Torvalds {
28272ad94ae6SAl Viro 	int error;
28281da177e4SLinus Torvalds 	char *from;
28296902d925SDave Hansen 	struct dentry *dentry;
2830dae6ad8fSAl Viro 	struct path path;
28311da177e4SLinus Torvalds 
28321da177e4SLinus Torvalds 	from = getname(oldname);
28331da177e4SLinus Torvalds 	if (IS_ERR(from))
28341da177e4SLinus Torvalds 		return PTR_ERR(from);
28352ad94ae6SAl Viro 
2836dae6ad8fSAl Viro 	dentry = user_path_create(newdfd, newname, &path, 0);
28371da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
28386902d925SDave Hansen 	if (IS_ERR(dentry))
2839dae6ad8fSAl Viro 		goto out_putname;
28406902d925SDave Hansen 
2841dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
284275c3f29dSDave Hansen 	if (error)
284375c3f29dSDave Hansen 		goto out_dput;
2844dae6ad8fSAl Viro 	error = security_path_symlink(&path, dentry, from);
2845be6d3e56SKentaro Takeda 	if (error)
2846be6d3e56SKentaro Takeda 		goto out_drop_write;
2847dae6ad8fSAl Viro 	error = vfs_symlink(path.dentry->d_inode, dentry, from);
2848be6d3e56SKentaro Takeda out_drop_write:
2849dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
285075c3f29dSDave Hansen out_dput:
28511da177e4SLinus Torvalds 	dput(dentry);
2852dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2853dae6ad8fSAl Viro 	path_put(&path);
28546902d925SDave Hansen out_putname:
28551da177e4SLinus Torvalds 	putname(from);
28561da177e4SLinus Torvalds 	return error;
28571da177e4SLinus Torvalds }
28581da177e4SLinus Torvalds 
28593480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
28605590ff0dSUlrich Drepper {
28615590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
28625590ff0dSUlrich Drepper }
28635590ff0dSUlrich Drepper 
28641da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
28651da177e4SLinus Torvalds {
28661da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
28671da177e4SLinus Torvalds 	int error;
28681da177e4SLinus Torvalds 
28691da177e4SLinus Torvalds 	if (!inode)
28701da177e4SLinus Torvalds 		return -ENOENT;
28711da177e4SLinus Torvalds 
2872a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
28731da177e4SLinus Torvalds 	if (error)
28741da177e4SLinus Torvalds 		return error;
28751da177e4SLinus Torvalds 
28761da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
28771da177e4SLinus Torvalds 		return -EXDEV;
28781da177e4SLinus Torvalds 
28791da177e4SLinus Torvalds 	/*
28801da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
28811da177e4SLinus Torvalds 	 */
28821da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
28831da177e4SLinus Torvalds 		return -EPERM;
2884acfa4380SAl Viro 	if (!dir->i_op->link)
28851da177e4SLinus Torvalds 		return -EPERM;
28867e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
28871da177e4SLinus Torvalds 		return -EPERM;
28881da177e4SLinus Torvalds 
28891da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
28901da177e4SLinus Torvalds 	if (error)
28911da177e4SLinus Torvalds 		return error;
28921da177e4SLinus Torvalds 
28937e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
2894aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
2895aae8a97dSAneesh Kumar K.V 	if (inode->i_nlink == 0)
2896aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
2897aae8a97dSAneesh Kumar K.V 	else
28981da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
28997e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2900e31e14ecSStephen Smalley 	if (!error)
29017e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
29021da177e4SLinus Torvalds 	return error;
29031da177e4SLinus Torvalds }
29041da177e4SLinus Torvalds 
29051da177e4SLinus Torvalds /*
29061da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
29071da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
29081da177e4SLinus Torvalds  * newname.  --KAB
29091da177e4SLinus Torvalds  *
29101da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
29111da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
29121da177e4SLinus Torvalds  * and other special files.  --ADM
29131da177e4SLinus Torvalds  */
29142e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
29152e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
29161da177e4SLinus Torvalds {
29171da177e4SLinus Torvalds 	struct dentry *new_dentry;
2918dae6ad8fSAl Viro 	struct path old_path, new_path;
291911a7b371SAneesh Kumar K.V 	int how = 0;
29201da177e4SLinus Torvalds 	int error;
29211da177e4SLinus Torvalds 
292211a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
2923c04030e1SUlrich Drepper 		return -EINVAL;
292411a7b371SAneesh Kumar K.V 	/*
292511a7b371SAneesh Kumar K.V 	 * To use null names we require CAP_DAC_READ_SEARCH
292611a7b371SAneesh Kumar K.V 	 * This ensures that not everyone will be able to create
292711a7b371SAneesh Kumar K.V 	 * handlink using the passed filedescriptor.
292811a7b371SAneesh Kumar K.V 	 */
292911a7b371SAneesh Kumar K.V 	if (flags & AT_EMPTY_PATH) {
293011a7b371SAneesh Kumar K.V 		if (!capable(CAP_DAC_READ_SEARCH))
293111a7b371SAneesh Kumar K.V 			return -ENOENT;
293211a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
293311a7b371SAneesh Kumar K.V 	}
2934c04030e1SUlrich Drepper 
293511a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
293611a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
293711a7b371SAneesh Kumar K.V 
293811a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
29391da177e4SLinus Torvalds 	if (error)
29402ad94ae6SAl Viro 		return error;
29412ad94ae6SAl Viro 
2942dae6ad8fSAl Viro 	new_dentry = user_path_create(newdfd, newname, &new_path, 0);
29431da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
29446902d925SDave Hansen 	if (IS_ERR(new_dentry))
2945dae6ad8fSAl Viro 		goto out;
2946dae6ad8fSAl Viro 
2947dae6ad8fSAl Viro 	error = -EXDEV;
2948dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
2949dae6ad8fSAl Viro 		goto out_dput;
2950dae6ad8fSAl Viro 	error = mnt_want_write(new_path.mnt);
295175c3f29dSDave Hansen 	if (error)
295275c3f29dSDave Hansen 		goto out_dput;
2953dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
2954be6d3e56SKentaro Takeda 	if (error)
2955be6d3e56SKentaro Takeda 		goto out_drop_write;
2956dae6ad8fSAl Viro 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
2957be6d3e56SKentaro Takeda out_drop_write:
2958dae6ad8fSAl Viro 	mnt_drop_write(new_path.mnt);
295975c3f29dSDave Hansen out_dput:
29601da177e4SLinus Torvalds 	dput(new_dentry);
2961dae6ad8fSAl Viro 	mutex_unlock(&new_path.dentry->d_inode->i_mutex);
2962dae6ad8fSAl Viro 	path_put(&new_path);
29631da177e4SLinus Torvalds out:
29642d8f3038SAl Viro 	path_put(&old_path);
29651da177e4SLinus Torvalds 
29661da177e4SLinus Torvalds 	return error;
29671da177e4SLinus Torvalds }
29681da177e4SLinus Torvalds 
29693480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
29705590ff0dSUlrich Drepper {
2971c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
29725590ff0dSUlrich Drepper }
29735590ff0dSUlrich Drepper 
29741da177e4SLinus Torvalds /*
29751da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
29761da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
29771da177e4SLinus Torvalds  * Problems:
29781da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
29791da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
29801da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
2981a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
29821da177e4SLinus Torvalds  *	   story.
29831da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
29841b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
29851da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
29861da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
2987a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
29881da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
29891da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
29901da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
2991a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
29921da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
29931da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
29941da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
2995e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
29961b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
29971da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2998c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
29991da177e4SLinus Torvalds  *	   locking].
30001da177e4SLinus Torvalds  */
300175c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
30021da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
30031da177e4SLinus Torvalds {
30041da177e4SLinus Torvalds 	int error = 0;
30059055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
30061da177e4SLinus Torvalds 
30071da177e4SLinus Torvalds 	/*
30081da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
30091da177e4SLinus Torvalds 	 * we'll need to flip '..'.
30101da177e4SLinus Torvalds 	 */
30111da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3012f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
30131da177e4SLinus Torvalds 		if (error)
30141da177e4SLinus Torvalds 			return error;
30151da177e4SLinus Torvalds 	}
30161da177e4SLinus Torvalds 
30171da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30181da177e4SLinus Torvalds 	if (error)
30191da177e4SLinus Torvalds 		return error;
30201da177e4SLinus Torvalds 
30211d2ef590SAl Viro 	dget(new_dentry);
3022d83c49f3SAl Viro 	if (target)
30231b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
30249055cba7SSage Weil 
30251da177e4SLinus Torvalds 	error = -EBUSY;
30269055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
30279055cba7SSage Weil 		goto out;
30289055cba7SSage Weil 
30293cebde24SSage Weil 	if (target)
30303cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
30311da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
30329055cba7SSage Weil 	if (error)
30339055cba7SSage Weil 		goto out;
30349055cba7SSage Weil 
30351da177e4SLinus Torvalds 	if (target) {
30361da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
3037d83c49f3SAl Viro 		dont_mount(new_dentry);
3038d83c49f3SAl Viro 	}
30399055cba7SSage Weil out:
30409055cba7SSage Weil 	if (target)
30411b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
30421d2ef590SAl Viro 	dput(new_dentry);
3043e31e14ecSStephen Smalley 	if (!error)
3044349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30451da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
30461da177e4SLinus Torvalds 	return error;
30471da177e4SLinus Torvalds }
30481da177e4SLinus Torvalds 
304975c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
30501da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
30511da177e4SLinus Torvalds {
305251892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
30531da177e4SLinus Torvalds 	int error;
30541da177e4SLinus Torvalds 
30551da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30561da177e4SLinus Torvalds 	if (error)
30571da177e4SLinus Torvalds 		return error;
30581da177e4SLinus Torvalds 
30591da177e4SLinus Torvalds 	dget(new_dentry);
30601da177e4SLinus Torvalds 	if (target)
30611b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
306251892bbbSSage Weil 
30631da177e4SLinus Torvalds 	error = -EBUSY;
306451892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
306551892bbbSSage Weil 		goto out;
306651892bbbSSage Weil 
30671da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
306851892bbbSSage Weil 	if (error)
306951892bbbSSage Weil 		goto out;
307051892bbbSSage Weil 
3071bec1052eSAl Viro 	if (target)
3072d83c49f3SAl Viro 		dont_mount(new_dentry);
3073349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30741da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
307551892bbbSSage Weil out:
30761da177e4SLinus Torvalds 	if (target)
30771b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
30781da177e4SLinus Torvalds 	dput(new_dentry);
30791da177e4SLinus Torvalds 	return error;
30801da177e4SLinus Torvalds }
30811da177e4SLinus Torvalds 
30821da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
30831da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
30841da177e4SLinus Torvalds {
30851da177e4SLinus Torvalds 	int error;
30861da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
308759b0df21SEric Paris 	const unsigned char *old_name;
30881da177e4SLinus Torvalds 
30891da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
30901da177e4SLinus Torvalds  		return 0;
30911da177e4SLinus Torvalds 
30921da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
30931da177e4SLinus Torvalds 	if (error)
30941da177e4SLinus Torvalds 		return error;
30951da177e4SLinus Torvalds 
30961da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3097a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
30981da177e4SLinus Torvalds 	else
30991da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
31001da177e4SLinus Torvalds 	if (error)
31011da177e4SLinus Torvalds 		return error;
31021da177e4SLinus Torvalds 
3103acfa4380SAl Viro 	if (!old_dir->i_op->rename)
31041da177e4SLinus Torvalds 		return -EPERM;
31051da177e4SLinus Torvalds 
31060eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
31070eeca283SRobert Love 
31081da177e4SLinus Torvalds 	if (is_dir)
31091da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
31101da177e4SLinus Torvalds 	else
31111da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3112123df294SAl Viro 	if (!error)
3113123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
31145a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
31150eeca283SRobert Love 	fsnotify_oldname_free(old_name);
31160eeca283SRobert Love 
31171da177e4SLinus Torvalds 	return error;
31181da177e4SLinus Torvalds }
31191da177e4SLinus Torvalds 
31202e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
31212e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
31221da177e4SLinus Torvalds {
31231da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
31241da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
31251da177e4SLinus Torvalds 	struct dentry *trap;
31261da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
31272ad94ae6SAl Viro 	char *from;
31282ad94ae6SAl Viro 	char *to;
31292ad94ae6SAl Viro 	int error;
31301da177e4SLinus Torvalds 
31312ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
31321da177e4SLinus Torvalds 	if (error)
31331da177e4SLinus Torvalds 		goto exit;
31341da177e4SLinus Torvalds 
31352ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
31361da177e4SLinus Torvalds 	if (error)
31371da177e4SLinus Torvalds 		goto exit1;
31381da177e4SLinus Torvalds 
31391da177e4SLinus Torvalds 	error = -EXDEV;
31404ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
31411da177e4SLinus Torvalds 		goto exit2;
31421da177e4SLinus Torvalds 
31434ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
31441da177e4SLinus Torvalds 	error = -EBUSY;
31451da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
31461da177e4SLinus Torvalds 		goto exit2;
31471da177e4SLinus Torvalds 
31484ac91378SJan Blunck 	new_dir = newnd.path.dentry;
31491da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
31501da177e4SLinus Torvalds 		goto exit2;
31511da177e4SLinus Torvalds 
31520612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
31530612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
31544e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
31550612d9fbSOGAWA Hirofumi 
31561da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
31571da177e4SLinus Torvalds 
315849705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
31591da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
31601da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
31611da177e4SLinus Torvalds 		goto exit3;
31621da177e4SLinus Torvalds 	/* source must exist */
31631da177e4SLinus Torvalds 	error = -ENOENT;
31641da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
31651da177e4SLinus Torvalds 		goto exit4;
31661da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
31671da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
31681da177e4SLinus Torvalds 		error = -ENOTDIR;
31691da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
31701da177e4SLinus Torvalds 			goto exit4;
31711da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
31721da177e4SLinus Torvalds 			goto exit4;
31731da177e4SLinus Torvalds 	}
31741da177e4SLinus Torvalds 	/* source should not be ancestor of target */
31751da177e4SLinus Torvalds 	error = -EINVAL;
31761da177e4SLinus Torvalds 	if (old_dentry == trap)
31771da177e4SLinus Torvalds 		goto exit4;
317849705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
31791da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
31801da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
31811da177e4SLinus Torvalds 		goto exit4;
31821da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
31831da177e4SLinus Torvalds 	error = -ENOTEMPTY;
31841da177e4SLinus Torvalds 	if (new_dentry == trap)
31851da177e4SLinus Torvalds 		goto exit5;
31861da177e4SLinus Torvalds 
31879079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
31889079b1ebSDave Hansen 	if (error)
31899079b1ebSDave Hansen 		goto exit5;
3190be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3191be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3192be6d3e56SKentaro Takeda 	if (error)
3193be6d3e56SKentaro Takeda 		goto exit6;
31941da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
31951da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3196be6d3e56SKentaro Takeda exit6:
31979079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
31981da177e4SLinus Torvalds exit5:
31991da177e4SLinus Torvalds 	dput(new_dentry);
32001da177e4SLinus Torvalds exit4:
32011da177e4SLinus Torvalds 	dput(old_dentry);
32021da177e4SLinus Torvalds exit3:
32031da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
32041da177e4SLinus Torvalds exit2:
32051d957f9bSJan Blunck 	path_put(&newnd.path);
32062ad94ae6SAl Viro 	putname(to);
32071da177e4SLinus Torvalds exit1:
32081d957f9bSJan Blunck 	path_put(&oldnd.path);
32091da177e4SLinus Torvalds 	putname(from);
32102ad94ae6SAl Viro exit:
32111da177e4SLinus Torvalds 	return error;
32121da177e4SLinus Torvalds }
32131da177e4SLinus Torvalds 
3214a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
32155590ff0dSUlrich Drepper {
32165590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
32175590ff0dSUlrich Drepper }
32185590ff0dSUlrich Drepper 
32191da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
32201da177e4SLinus Torvalds {
32211da177e4SLinus Torvalds 	int len;
32221da177e4SLinus Torvalds 
32231da177e4SLinus Torvalds 	len = PTR_ERR(link);
32241da177e4SLinus Torvalds 	if (IS_ERR(link))
32251da177e4SLinus Torvalds 		goto out;
32261da177e4SLinus Torvalds 
32271da177e4SLinus Torvalds 	len = strlen(link);
32281da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
32291da177e4SLinus Torvalds 		len = buflen;
32301da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
32311da177e4SLinus Torvalds 		len = -EFAULT;
32321da177e4SLinus Torvalds out:
32331da177e4SLinus Torvalds 	return len;
32341da177e4SLinus Torvalds }
32351da177e4SLinus Torvalds 
32361da177e4SLinus Torvalds /*
32371da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
32381da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
32391da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
32401da177e4SLinus Torvalds  */
32411da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32421da177e4SLinus Torvalds {
32431da177e4SLinus Torvalds 	struct nameidata nd;
3244cc314eefSLinus Torvalds 	void *cookie;
3245694a1764SMarcin Slusarz 	int res;
3246cc314eefSLinus Torvalds 
32471da177e4SLinus Torvalds 	nd.depth = 0;
3248cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3249694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3250694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3251694a1764SMarcin Slusarz 
3252694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
32531da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3254cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3255694a1764SMarcin Slusarz 	return res;
32561da177e4SLinus Torvalds }
32571da177e4SLinus Torvalds 
32581da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
32591da177e4SLinus Torvalds {
32601da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
32611da177e4SLinus Torvalds }
32621da177e4SLinus Torvalds 
32631da177e4SLinus Torvalds /* get the link contents into pagecache */
32641da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
32651da177e4SLinus Torvalds {
3266ebd09abbSDuane Griffin 	char *kaddr;
32671da177e4SLinus Torvalds 	struct page *page;
32681da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3269090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
32701da177e4SLinus Torvalds 	if (IS_ERR(page))
32716fe6900eSNick Piggin 		return (char*)page;
32721da177e4SLinus Torvalds 	*ppage = page;
3273ebd09abbSDuane Griffin 	kaddr = kmap(page);
3274ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3275ebd09abbSDuane Griffin 	return kaddr;
32761da177e4SLinus Torvalds }
32771da177e4SLinus Torvalds 
32781da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32791da177e4SLinus Torvalds {
32801da177e4SLinus Torvalds 	struct page *page = NULL;
32811da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
32821da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
32831da177e4SLinus Torvalds 	if (page) {
32841da177e4SLinus Torvalds 		kunmap(page);
32851da177e4SLinus Torvalds 		page_cache_release(page);
32861da177e4SLinus Torvalds 	}
32871da177e4SLinus Torvalds 	return res;
32881da177e4SLinus Torvalds }
32891da177e4SLinus Torvalds 
3290cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
32911da177e4SLinus Torvalds {
3292cc314eefSLinus Torvalds 	struct page *page = NULL;
32931da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3294cc314eefSLinus Torvalds 	return page;
32951da177e4SLinus Torvalds }
32961da177e4SLinus Torvalds 
3297cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
32981da177e4SLinus Torvalds {
3299cc314eefSLinus Torvalds 	struct page *page = cookie;
3300cc314eefSLinus Torvalds 
3301cc314eefSLinus Torvalds 	if (page) {
33021da177e4SLinus Torvalds 		kunmap(page);
33031da177e4SLinus Torvalds 		page_cache_release(page);
33041da177e4SLinus Torvalds 	}
33051da177e4SLinus Torvalds }
33061da177e4SLinus Torvalds 
330754566b2cSNick Piggin /*
330854566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
330954566b2cSNick Piggin  */
331054566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
33111da177e4SLinus Torvalds {
33121da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
33130adb25d2SKirill Korotaev 	struct page *page;
3314afddba49SNick Piggin 	void *fsdata;
3315beb497abSDmitriy Monakhov 	int err;
33161da177e4SLinus Torvalds 	char *kaddr;
331754566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
331854566b2cSNick Piggin 	if (nofs)
331954566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
33201da177e4SLinus Torvalds 
33217e53cac4SNeilBrown retry:
3322afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
332354566b2cSNick Piggin 				flags, &page, &fsdata);
33241da177e4SLinus Torvalds 	if (err)
3325afddba49SNick Piggin 		goto fail;
3326afddba49SNick Piggin 
33271da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
33281da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
33291da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
3330afddba49SNick Piggin 
3331afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3332afddba49SNick Piggin 							page, fsdata);
33331da177e4SLinus Torvalds 	if (err < 0)
33341da177e4SLinus Torvalds 		goto fail;
3335afddba49SNick Piggin 	if (err < len-1)
3336afddba49SNick Piggin 		goto retry;
3337afddba49SNick Piggin 
33381da177e4SLinus Torvalds 	mark_inode_dirty(inode);
33391da177e4SLinus Torvalds 	return 0;
33401da177e4SLinus Torvalds fail:
33411da177e4SLinus Torvalds 	return err;
33421da177e4SLinus Torvalds }
33431da177e4SLinus Torvalds 
33440adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
33450adb25d2SKirill Korotaev {
33460adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
334754566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
33480adb25d2SKirill Korotaev }
33490adb25d2SKirill Korotaev 
335092e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
33511da177e4SLinus Torvalds 	.readlink	= generic_readlink,
33521da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
33531da177e4SLinus Torvalds 	.put_link	= page_put_link,
33541da177e4SLinus Torvalds };
33551da177e4SLinus Torvalds 
33562d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3357cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
33581da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
33591da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
33601da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
33611da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
33621da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
33631da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
33641da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
33651da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
33661da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
33670adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
33681da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
33691da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3370d1811465SAl Viro EXPORT_SYMBOL(kern_path);
337116f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3372f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
33731da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
33741da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
33751da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
33761da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
33771da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
33781da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
33791da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
33801da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
33811da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
33821da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
33831da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
33841da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
33851da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
33861da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3387