xref: /openbmc/linux/fs/namei.c (revision 0714a533)
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"
39c7105365SAl Viro #include "mount.h"
40e81e3f4dSEric Paris 
411da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer]
421da177e4SLinus Torvalds  * Fundamental changes in the pathname lookup mechanisms (namei)
431da177e4SLinus Torvalds  * were necessary because of omirr.  The reason is that omirr needs
441da177e4SLinus Torvalds  * to know the _real_ pathname, not the user-supplied one, in case
451da177e4SLinus Torvalds  * of symlinks (and also when transname replacements occur).
461da177e4SLinus Torvalds  *
471da177e4SLinus Torvalds  * The new code replaces the old recursive symlink resolution with
481da177e4SLinus Torvalds  * an iterative one (in case of non-nested symlink chains).  It does
491da177e4SLinus Torvalds  * this with calls to <fs>_follow_link().
501da177e4SLinus Torvalds  * As a side effect, dir_namei(), _namei() and follow_link() are now
511da177e4SLinus Torvalds  * replaced with a single function lookup_dentry() that can handle all
521da177e4SLinus Torvalds  * the special cases of the former code.
531da177e4SLinus Torvalds  *
541da177e4SLinus Torvalds  * With the new dcache, the pathname is stored at each inode, at least as
551da177e4SLinus Torvalds  * long as the refcount of the inode is positive.  As a side effect, the
561da177e4SLinus Torvalds  * size of the dcache depends on the inode cache and thus is dynamic.
571da177e4SLinus Torvalds  *
581da177e4SLinus Torvalds  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
591da177e4SLinus Torvalds  * resolution to correspond with current state of the code.
601da177e4SLinus Torvalds  *
611da177e4SLinus Torvalds  * Note that the symlink resolution is not *completely* iterative.
621da177e4SLinus Torvalds  * There is still a significant amount of tail- and mid- recursion in
631da177e4SLinus Torvalds  * the algorithm.  Also, note that <fs>_readlink() is not used in
641da177e4SLinus Torvalds  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
651da177e4SLinus Torvalds  * may return different results than <fs>_follow_link().  Many virtual
661da177e4SLinus Torvalds  * filesystems (including /proc) exhibit this behavior.
671da177e4SLinus Torvalds  */
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
701da177e4SLinus Torvalds  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
711da177e4SLinus Torvalds  * and the name already exists in form of a symlink, try to create the new
721da177e4SLinus Torvalds  * name indicated by the symlink. The old code always complained that the
731da177e4SLinus Torvalds  * name already exists, due to not following the symlink even if its target
741da177e4SLinus Torvalds  * is nonexistent.  The new semantics affects also mknod() and link() when
7525985edcSLucas De Marchi  * the name is a symlink pointing to a non-existent name.
761da177e4SLinus Torvalds  *
771da177e4SLinus Torvalds  * I don't know which semantics is the right one, since I have no access
781da177e4SLinus Torvalds  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
791da177e4SLinus Torvalds  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
801da177e4SLinus Torvalds  * "old" one. Personally, I think the new semantics is much more logical.
811da177e4SLinus Torvalds  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
821da177e4SLinus Torvalds  * file does succeed in both HP-UX and SunOs, but not in Solaris
831da177e4SLinus Torvalds  * and in the old Linux semantics.
841da177e4SLinus Torvalds  */
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
871da177e4SLinus Torvalds  * semantics.  See the comments in "open_namei" and "do_link" below.
881da177e4SLinus Torvalds  *
891da177e4SLinus Torvalds  * [10-Sep-98 Alan Modra] Another symlink change.
901da177e4SLinus Torvalds  */
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
931da177e4SLinus Torvalds  *	inside the path - always follow.
941da177e4SLinus Torvalds  *	in the last component in creation/removal/renaming - never follow.
951da177e4SLinus Torvalds  *	if LOOKUP_FOLLOW passed - follow.
961da177e4SLinus Torvalds  *	if the pathname has trailing slashes - follow.
971da177e4SLinus Torvalds  *	otherwise - don't follow.
981da177e4SLinus Torvalds  * (applied in that order).
991da177e4SLinus Torvalds  *
1001da177e4SLinus Torvalds  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
1011da177e4SLinus Torvalds  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
1021da177e4SLinus Torvalds  * During the 2.4 we need to fix the userland stuff depending on it -
1031da177e4SLinus Torvalds  * hopefully we will be able to get rid of that wart in 2.5. So far only
1041da177e4SLinus Torvalds  * XEmacs seems to be relying on it...
1051da177e4SLinus Torvalds  */
1061da177e4SLinus Torvalds /*
1071da177e4SLinus Torvalds  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
108a11f3a05SArjan van de Ven  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1091da177e4SLinus Torvalds  * any extra contention...
1101da177e4SLinus Torvalds  */
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional
1131da177e4SLinus Torvalds  * checking and hopefully speeding things up, we copy filenames to the
1141da177e4SLinus Torvalds  * kernel data space before using them..
1151da177e4SLinus Torvalds  *
1161da177e4SLinus Torvalds  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
1171da177e4SLinus Torvalds  * PATH_MAX includes the nul terminator --RR.
1181da177e4SLinus Torvalds  */
119858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page)
1201da177e4SLinus Torvalds {
1211da177e4SLinus Torvalds 	int retval;
1221da177e4SLinus Torvalds 	unsigned long len = PATH_MAX;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	if (!segment_eq(get_fs(), KERNEL_DS)) {
1251da177e4SLinus Torvalds 		if ((unsigned long) filename >= TASK_SIZE)
1261da177e4SLinus Torvalds 			return -EFAULT;
1271da177e4SLinus Torvalds 		if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
1281da177e4SLinus Torvalds 			len = TASK_SIZE - (unsigned long) filename;
1291da177e4SLinus Torvalds 	}
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	retval = strncpy_from_user(page, filename, len);
1321da177e4SLinus Torvalds 	if (retval > 0) {
1331da177e4SLinus Torvalds 		if (retval < len)
1341da177e4SLinus Torvalds 			return 0;
1351da177e4SLinus Torvalds 		return -ENAMETOOLONG;
1361da177e4SLinus Torvalds 	} else if (!retval)
1371da177e4SLinus Torvalds 		retval = -ENOENT;
1381da177e4SLinus Torvalds 	return retval;
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds 
1411fa1e7f6SAndy Whitcroft static char *getname_flags(const char __user *filename, int flags, int *empty)
1421da177e4SLinus Torvalds {
1431da177e4SLinus Torvalds 	char *tmp, *result;
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	result = ERR_PTR(-ENOMEM);
1461da177e4SLinus Torvalds 	tmp = __getname();
1471da177e4SLinus Torvalds 	if (tmp)  {
1481da177e4SLinus Torvalds 		int retval = do_getname(filename, tmp);
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 		result = tmp;
1511da177e4SLinus Torvalds 		if (retval < 0) {
1521fa1e7f6SAndy Whitcroft 			if (retval == -ENOENT && empty)
1531fa1e7f6SAndy Whitcroft 				*empty = 1;
154f52e0c11SAl Viro 			if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
1551da177e4SLinus Torvalds 				__putname(tmp);
1561da177e4SLinus Torvalds 				result = ERR_PTR(retval);
1571da177e4SLinus Torvalds 			}
1581da177e4SLinus Torvalds 		}
159f52e0c11SAl Viro 	}
1601da177e4SLinus Torvalds 	audit_getname(result);
1611da177e4SLinus Torvalds 	return result;
1621da177e4SLinus Torvalds }
1631da177e4SLinus Torvalds 
164f52e0c11SAl Viro char *getname(const char __user * filename)
165f52e0c11SAl Viro {
1661fa1e7f6SAndy Whitcroft 	return getname_flags(filename, 0, 0);
167f52e0c11SAl Viro }
168f52e0c11SAl Viro 
1691da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
1701da177e4SLinus Torvalds void putname(const char *name)
1711da177e4SLinus Torvalds {
1725ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
1731da177e4SLinus Torvalds 		audit_putname(name);
1741da177e4SLinus Torvalds 	else
1751da177e4SLinus Torvalds 		__putname(name);
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds EXPORT_SYMBOL(putname);
1781da177e4SLinus Torvalds #endif
1791da177e4SLinus Torvalds 
180e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask)
181e77819e5SLinus Torvalds {
18284635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL
183e77819e5SLinus Torvalds 	struct posix_acl *acl;
184e77819e5SLinus Torvalds 
185e77819e5SLinus Torvalds 	if (mask & MAY_NOT_BLOCK) {
1863567866bSAl Viro 		acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
1873567866bSAl Viro 	        if (!acl)
188e77819e5SLinus Torvalds 	                return -EAGAIN;
1893567866bSAl Viro 		/* no ->get_acl() calls in RCU mode... */
1903567866bSAl Viro 		if (acl == ACL_NOT_CACHED)
191e77819e5SLinus Torvalds 			return -ECHILD;
192206b1d09SAri Savolainen 	        return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
193e77819e5SLinus Torvalds 	}
194e77819e5SLinus Torvalds 
195e77819e5SLinus Torvalds 	acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
196e77819e5SLinus Torvalds 
197e77819e5SLinus Torvalds 	/*
1984e34e719SChristoph Hellwig 	 * A filesystem can force a ACL callback by just never filling the
1994e34e719SChristoph Hellwig 	 * ACL cache. But normally you'd fill the cache either at inode
2004e34e719SChristoph Hellwig 	 * instantiation time, or on the first ->get_acl call.
201e77819e5SLinus Torvalds 	 *
2024e34e719SChristoph Hellwig 	 * If the filesystem doesn't have a get_acl() function at all, we'll
2034e34e719SChristoph Hellwig 	 * just create the negative cache entry.
204e77819e5SLinus Torvalds 	 */
205e77819e5SLinus Torvalds 	if (acl == ACL_NOT_CACHED) {
2064e34e719SChristoph Hellwig 	        if (inode->i_op->get_acl) {
2074e34e719SChristoph Hellwig 			acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
2084e34e719SChristoph Hellwig 			if (IS_ERR(acl))
2094e34e719SChristoph Hellwig 				return PTR_ERR(acl);
2104e34e719SChristoph Hellwig 		} else {
211e77819e5SLinus Torvalds 		        set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
212e77819e5SLinus Torvalds 		        return -EAGAIN;
213e77819e5SLinus Torvalds 		}
2144e34e719SChristoph Hellwig 	}
215e77819e5SLinus Torvalds 
216e77819e5SLinus Torvalds 	if (acl) {
217e77819e5SLinus Torvalds 	        int error = posix_acl_permission(inode, acl, mask);
218e77819e5SLinus Torvalds 	        posix_acl_release(acl);
219e77819e5SLinus Torvalds 	        return error;
220e77819e5SLinus Torvalds 	}
22184635d68SLinus Torvalds #endif
222e77819e5SLinus Torvalds 
223e77819e5SLinus Torvalds 	return -EAGAIN;
224e77819e5SLinus Torvalds }
225e77819e5SLinus Torvalds 
2265909ccaaSLinus Torvalds /*
227948409c7SAndreas Gruenbacher  * This does the basic permission checking
2285909ccaaSLinus Torvalds  */
2297e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask)
2305909ccaaSLinus Torvalds {
23126cf46beSLinus Torvalds 	unsigned int mode = inode->i_mode;
2325909ccaaSLinus Torvalds 
233e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
234e795b717SSerge E. Hallyn 		goto other_perms;
235e795b717SSerge E. Hallyn 
23614067ff5SLinus Torvalds 	if (likely(current_fsuid() == inode->i_uid))
2375909ccaaSLinus Torvalds 		mode >>= 6;
2385909ccaaSLinus Torvalds 	else {
239e77819e5SLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
2407e40145eSAl Viro 			int error = check_acl(inode, mask);
2415909ccaaSLinus Torvalds 			if (error != -EAGAIN)
2425909ccaaSLinus Torvalds 				return error;
2435909ccaaSLinus Torvalds 		}
2445909ccaaSLinus Torvalds 
2455909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
2465909ccaaSLinus Torvalds 			mode >>= 3;
2475909ccaaSLinus Torvalds 	}
2485909ccaaSLinus Torvalds 
249e795b717SSerge E. Hallyn other_perms:
2505909ccaaSLinus Torvalds 	/*
2515909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
2525909ccaaSLinus Torvalds 	 */
2539c2c7039SAl Viro 	if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
2545909ccaaSLinus Torvalds 		return 0;
2555909ccaaSLinus Torvalds 	return -EACCES;
2565909ccaaSLinus Torvalds }
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds /**
2591da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
2601da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2618fd90c8dSAndreas Gruenbacher  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
2621da177e4SLinus Torvalds  *
2631da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
2641da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
2651da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
266b74c79e9SNick Piggin  * are used for other things.
267b74c79e9SNick Piggin  *
268b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
269b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
270b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
2711da177e4SLinus Torvalds  */
2722830ba7fSAl Viro int generic_permission(struct inode *inode, int mask)
2731da177e4SLinus Torvalds {
2745909ccaaSLinus Torvalds 	int ret;
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	/*
277948409c7SAndreas Gruenbacher 	 * Do the basic permission checks.
2781da177e4SLinus Torvalds 	 */
2797e40145eSAl Viro 	ret = acl_permission_check(inode, mask);
2805909ccaaSLinus Torvalds 	if (ret != -EACCES)
2815909ccaaSLinus Torvalds 		return ret;
2821da177e4SLinus Torvalds 
283d594e7ecSAl Viro 	if (S_ISDIR(inode->i_mode)) {
284d594e7ecSAl Viro 		/* DACs are overridable for directories */
285d594e7ecSAl Viro 		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
286d594e7ecSAl Viro 			return 0;
287d594e7ecSAl Viro 		if (!(mask & MAY_WRITE))
288d594e7ecSAl Viro 			if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
289d594e7ecSAl Viro 				return 0;
290d594e7ecSAl Viro 		return -EACCES;
291d594e7ecSAl Viro 	}
2921da177e4SLinus Torvalds 	/*
2931da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
294d594e7ecSAl Viro 	 * Executable DACs are overridable when there is
295d594e7ecSAl Viro 	 * at least one exec bit set.
2961da177e4SLinus Torvalds 	 */
297d594e7ecSAl Viro 	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
298e795b717SSerge E. Hallyn 		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
2991da177e4SLinus Torvalds 			return 0;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	/*
3021da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
3031da177e4SLinus Torvalds 	 */
3047ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
305d594e7ecSAl Viro 	if (mask == MAY_READ)
306e795b717SSerge E. Hallyn 		if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
3071da177e4SLinus Torvalds 			return 0;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	return -EACCES;
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
3123ddcd056SLinus Torvalds /*
3133ddcd056SLinus Torvalds  * We _really_ want to just do "generic_permission()" without
3143ddcd056SLinus Torvalds  * even looking at the inode->i_op values. So we keep a cache
3153ddcd056SLinus Torvalds  * flag in inode->i_opflags, that says "this has not special
3163ddcd056SLinus Torvalds  * permission function, use the fast case".
3173ddcd056SLinus Torvalds  */
3183ddcd056SLinus Torvalds static inline int do_inode_permission(struct inode *inode, int mask)
3193ddcd056SLinus Torvalds {
3203ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
3213ddcd056SLinus Torvalds 		if (likely(inode->i_op->permission))
3223ddcd056SLinus Torvalds 			return inode->i_op->permission(inode, mask);
3233ddcd056SLinus Torvalds 
3243ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
3253ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
3263ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_FASTPERM;
3273ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
3283ddcd056SLinus Torvalds 	}
3293ddcd056SLinus Torvalds 	return generic_permission(inode, mask);
3303ddcd056SLinus Torvalds }
3313ddcd056SLinus Torvalds 
332cb23beb5SChristoph Hellwig /**
333cb23beb5SChristoph Hellwig  * inode_permission  -  check for access rights to a given inode
334cb23beb5SChristoph Hellwig  * @inode:	inode to check permission on
3358fd90c8dSAndreas Gruenbacher  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
336cb23beb5SChristoph Hellwig  *
337cb23beb5SChristoph Hellwig  * Used to check for read/write/execute permissions on an inode.
338cb23beb5SChristoph Hellwig  * We use "fsuid" for this, letting us set arbitrary permissions
339cb23beb5SChristoph Hellwig  * for filesystem access without changing the "normal" uids which
340cb23beb5SChristoph Hellwig  * are used for other things.
341948409c7SAndreas Gruenbacher  *
342948409c7SAndreas Gruenbacher  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
343cb23beb5SChristoph Hellwig  */
344f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask)
3451da177e4SLinus Torvalds {
346e6305c43SAl Viro 	int retval;
3471da177e4SLinus Torvalds 
3483ddcd056SLinus Torvalds 	if (unlikely(mask & MAY_WRITE)) {
34922590e41SMiklos Szeredi 		umode_t mode = inode->i_mode;
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 		/*
3521da177e4SLinus Torvalds 		 * Nobody gets write access to a read-only fs.
3531da177e4SLinus Torvalds 		 */
3541da177e4SLinus Torvalds 		if (IS_RDONLY(inode) &&
3551da177e4SLinus Torvalds 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
3561da177e4SLinus Torvalds 			return -EROFS;
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 		/*
3591da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
3601da177e4SLinus Torvalds 		 */
3611da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
3621da177e4SLinus Torvalds 			return -EACCES;
3631da177e4SLinus Torvalds 	}
3641da177e4SLinus Torvalds 
3653ddcd056SLinus Torvalds 	retval = do_inode_permission(inode, mask);
3661da177e4SLinus Torvalds 	if (retval)
3671da177e4SLinus Torvalds 		return retval;
3681da177e4SLinus Torvalds 
36908ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
37008ce5f16SSerge E. Hallyn 	if (retval)
37108ce5f16SSerge E. Hallyn 		return retval;
37208ce5f16SSerge E. Hallyn 
373d09ca739SEric Paris 	return security_inode_permission(inode, mask);
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
376f4d6ff89SAl Viro /**
3775dd784d0SJan Blunck  * path_get - get a reference to a path
3785dd784d0SJan Blunck  * @path: path to get the reference to
3795dd784d0SJan Blunck  *
3805dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3815dd784d0SJan Blunck  */
3825dd784d0SJan Blunck void path_get(struct path *path)
3835dd784d0SJan Blunck {
3845dd784d0SJan Blunck 	mntget(path->mnt);
3855dd784d0SJan Blunck 	dget(path->dentry);
3865dd784d0SJan Blunck }
3875dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
3885dd784d0SJan Blunck 
3895dd784d0SJan Blunck /**
3901d957f9bSJan Blunck  * path_put - put a reference to a path
3911d957f9bSJan Blunck  * @path: path to put the reference to
3921d957f9bSJan Blunck  *
3931d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
3941d957f9bSJan Blunck  */
3951d957f9bSJan Blunck void path_put(struct path *path)
3961da177e4SLinus Torvalds {
3971d957f9bSJan Blunck 	dput(path->dentry);
3981d957f9bSJan Blunck 	mntput(path->mnt);
3991da177e4SLinus Torvalds }
4001d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
4011da177e4SLinus Torvalds 
40219660af7SAl Viro /*
40331e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
40419660af7SAl Viro  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
40519660af7SAl Viro  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
40619660af7SAl Viro  * normal reference counts on dentries and vfsmounts to transition to rcu-walk
40719660af7SAl Viro  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
40819660af7SAl Viro  * got stuck, so ref-walk may continue from there. If this is not successful
40919660af7SAl Viro  * (eg. a seqcount has changed), then failure is returned and it's up to caller
41019660af7SAl Viro  * to restart the path walk from the beginning in ref-walk mode.
41131e6b01fSNick Piggin  */
41231e6b01fSNick Piggin 
41331e6b01fSNick Piggin /**
41419660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
41519660af7SAl Viro  * @nd: nameidata pathwalk data
41619660af7SAl Viro  * @dentry: child of nd->path.dentry or NULL
41739191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
41831e6b01fSNick Piggin  *
41919660af7SAl Viro  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
42019660af7SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
42119660af7SAl Viro  * @nd or NULL.  Must be called from rcu-walk context.
42231e6b01fSNick Piggin  */
42319660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
42431e6b01fSNick Piggin {
42531e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
42631e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
4275b6ca027SAl Viro 	int want_root = 0;
42831e6b01fSNick Piggin 
42931e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
4305b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
4315b6ca027SAl Viro 		want_root = 1;
43231e6b01fSNick Piggin 		spin_lock(&fs->lock);
43331e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
43431e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
43531e6b01fSNick Piggin 			goto err_root;
43631e6b01fSNick Piggin 	}
43731e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
43819660af7SAl Viro 	if (!dentry) {
43919660af7SAl Viro 		if (!__d_rcu_to_refcount(parent, nd->seq))
44019660af7SAl Viro 			goto err_parent;
44119660af7SAl Viro 		BUG_ON(nd->inode != parent->d_inode);
44219660af7SAl Viro 	} else {
44394c0d4ecSAl Viro 		if (dentry->d_parent != parent)
44494c0d4ecSAl Viro 			goto err_parent;
44531e6b01fSNick Piggin 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
44631e6b01fSNick Piggin 		if (!__d_rcu_to_refcount(dentry, nd->seq))
44719660af7SAl Viro 			goto err_child;
44831e6b01fSNick Piggin 		/*
44919660af7SAl Viro 		 * If the sequence check on the child dentry passed, then
45019660af7SAl Viro 		 * the child has not been removed from its parent. This
45119660af7SAl Viro 		 * means the parent dentry must be valid and able to take
45219660af7SAl Viro 		 * a reference at this point.
45331e6b01fSNick Piggin 		 */
45431e6b01fSNick Piggin 		BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
45531e6b01fSNick Piggin 		BUG_ON(!parent->d_count);
45631e6b01fSNick Piggin 		parent->d_count++;
45731e6b01fSNick Piggin 		spin_unlock(&dentry->d_lock);
45819660af7SAl Viro 	}
45931e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
4605b6ca027SAl Viro 	if (want_root) {
46131e6b01fSNick Piggin 		path_get(&nd->root);
46231e6b01fSNick Piggin 		spin_unlock(&fs->lock);
46331e6b01fSNick Piggin 	}
46431e6b01fSNick Piggin 	mntget(nd->path.mnt);
46531e6b01fSNick Piggin 
46631e6b01fSNick Piggin 	rcu_read_unlock();
46731e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
46831e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
46931e6b01fSNick Piggin 	return 0;
47019660af7SAl Viro 
47119660af7SAl Viro err_child:
47231e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
47319660af7SAl Viro err_parent:
47431e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
47531e6b01fSNick Piggin err_root:
4765b6ca027SAl Viro 	if (want_root)
47731e6b01fSNick Piggin 		spin_unlock(&fs->lock);
47831e6b01fSNick Piggin 	return -ECHILD;
47931e6b01fSNick Piggin }
48031e6b01fSNick Piggin 
48131e6b01fSNick Piggin /**
482834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
483834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
484834f2a4aSTrond Myklebust  */
485834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
486834f2a4aSTrond Myklebust {
4872dab5974SLinus Torvalds 	struct file *file = nd->intent.open.file;
4882dab5974SLinus Torvalds 
4892dab5974SLinus Torvalds 	if (file && !IS_ERR(file)) {
4902dab5974SLinus Torvalds 		if (file->f_path.dentry == NULL)
4912dab5974SLinus Torvalds 			put_filp(file);
492834f2a4aSTrond Myklebust 		else
4932dab5974SLinus Torvalds 			fput(file);
4942dab5974SLinus Torvalds 	}
495834f2a4aSTrond Myklebust }
496834f2a4aSTrond Myklebust 
497f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
49834286d66SNick Piggin {
499f60aef7eSAl Viro 	return dentry->d_op->d_revalidate(dentry, nd);
50034286d66SNick Piggin }
50134286d66SNick Piggin 
5029f1fafeeSAl Viro /**
5039f1fafeeSAl Viro  * complete_walk - successful completion of path walk
5049f1fafeeSAl Viro  * @nd:  pointer nameidata
50539159de2SJeff Layton  *
5069f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
5079f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
5089f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
5099f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
5109f1fafeeSAl Viro  * need to drop nd->path.
51139159de2SJeff Layton  */
5129f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
51339159de2SJeff Layton {
51416c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
51539159de2SJeff Layton 	int status;
51639159de2SJeff Layton 
5179f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
5189f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
5199f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
5209f1fafeeSAl Viro 			nd->root.mnt = NULL;
5219f1fafeeSAl Viro 		spin_lock(&dentry->d_lock);
5229f1fafeeSAl Viro 		if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
5239f1fafeeSAl Viro 			spin_unlock(&dentry->d_lock);
5249f1fafeeSAl Viro 			rcu_read_unlock();
5259f1fafeeSAl Viro 			br_read_unlock(vfsmount_lock);
5269f1fafeeSAl Viro 			return -ECHILD;
5279f1fafeeSAl Viro 		}
5289f1fafeeSAl Viro 		BUG_ON(nd->inode != dentry->d_inode);
5299f1fafeeSAl Viro 		spin_unlock(&dentry->d_lock);
5309f1fafeeSAl Viro 		mntget(nd->path.mnt);
5319f1fafeeSAl Viro 		rcu_read_unlock();
5329f1fafeeSAl Viro 		br_read_unlock(vfsmount_lock);
5339f1fafeeSAl Viro 	}
5349f1fafeeSAl Viro 
53516c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
53639159de2SJeff Layton 		return 0;
53739159de2SJeff Layton 
53816c2cd71SAl Viro 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
53916c2cd71SAl Viro 		return 0;
54016c2cd71SAl Viro 
54116c2cd71SAl Viro 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
54216c2cd71SAl Viro 		return 0;
54316c2cd71SAl Viro 
54416c2cd71SAl Viro 	/* Note: we do not d_invalidate() */
54534286d66SNick Piggin 	status = d_revalidate(dentry, nd);
54639159de2SJeff Layton 	if (status > 0)
54739159de2SJeff Layton 		return 0;
54839159de2SJeff Layton 
54916c2cd71SAl Viro 	if (!status)
55039159de2SJeff Layton 		status = -ESTALE;
55116c2cd71SAl Viro 
5529f1fafeeSAl Viro 	path_put(&nd->path);
55339159de2SJeff Layton 	return status;
55439159de2SJeff Layton }
55539159de2SJeff Layton 
5562a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
5572a737871SAl Viro {
558f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
559f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
5602a737871SAl Viro }
5612a737871SAl Viro 
5626de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
5636de88d72SAl Viro 
56431e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
56531e6b01fSNick Piggin {
56631e6b01fSNick Piggin 	if (!nd->root.mnt) {
56731e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
568c28cc364SNick Piggin 		unsigned seq;
569c28cc364SNick Piggin 
570c28cc364SNick Piggin 		do {
571c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
57231e6b01fSNick Piggin 			nd->root = fs->root;
573c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
574c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
57531e6b01fSNick Piggin 	}
57631e6b01fSNick Piggin }
57731e6b01fSNick Piggin 
578f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
5791da177e4SLinus Torvalds {
58031e6b01fSNick Piggin 	int ret;
58131e6b01fSNick Piggin 
5821da177e4SLinus Torvalds 	if (IS_ERR(link))
5831da177e4SLinus Torvalds 		goto fail;
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	if (*link == '/') {
5862a737871SAl Viro 		set_root(nd);
5871d957f9bSJan Blunck 		path_put(&nd->path);
5882a737871SAl Viro 		nd->path = nd->root;
5892a737871SAl Viro 		path_get(&nd->root);
59016c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
5911da177e4SLinus Torvalds 	}
59231e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
593b4091d5fSChristoph Hellwig 
59431e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
59531e6b01fSNick Piggin 	return ret;
5961da177e4SLinus Torvalds fail:
5971d957f9bSJan Blunck 	path_put(&nd->path);
5981da177e4SLinus Torvalds 	return PTR_ERR(link);
5991da177e4SLinus Torvalds }
6001da177e4SLinus Torvalds 
6011d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
602051d3812SIan Kent {
603051d3812SIan Kent 	dput(path->dentry);
6044ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
605051d3812SIan Kent 		mntput(path->mnt);
606051d3812SIan Kent }
607051d3812SIan Kent 
6087b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
6097b9337aaSNick Piggin 					struct nameidata *nd)
610051d3812SIan Kent {
61131e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
6124ac91378SJan Blunck 		dput(nd->path.dentry);
61331e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
6144ac91378SJan Blunck 			mntput(nd->path.mnt);
6159a229683SHuang Shijie 	}
61631e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
6174ac91378SJan Blunck 	nd->path.dentry = path->dentry;
618051d3812SIan Kent }
619051d3812SIan Kent 
620574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
621574197e0SAl Viro {
622574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
623574197e0SAl Viro 	if (!IS_ERR(cookie) && inode->i_op->put_link)
624574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
625574197e0SAl Viro 	path_put(link);
626574197e0SAl Viro }
627574197e0SAl Viro 
628def4af30SAl Viro static __always_inline int
629574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
6301da177e4SLinus Torvalds {
6311da177e4SLinus Torvalds 	int error;
6327b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
6331da177e4SLinus Torvalds 
634844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
635844a3917SAl Viro 
6360e794589SAl Viro 	if (link->mnt == nd->path.mnt)
6370e794589SAl Viro 		mntget(link->mnt);
6380e794589SAl Viro 
639574197e0SAl Viro 	if (unlikely(current->total_link_count >= 40)) {
640574197e0SAl Viro 		*p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
641574197e0SAl Viro 		path_put(&nd->path);
642574197e0SAl Viro 		return -ELOOP;
643574197e0SAl Viro 	}
644574197e0SAl Viro 	cond_resched();
645574197e0SAl Viro 	current->total_link_count++;
646574197e0SAl Viro 
6477b9337aaSNick Piggin 	touch_atime(link->mnt, dentry);
6481da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
649cd4e91d3SAl Viro 
65036f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
65136f3b4f6SAl Viro 	if (error) {
65236f3b4f6SAl Viro 		*p = ERR_PTR(error); /* no ->put_link(), please */
65336f3b4f6SAl Viro 		path_put(&nd->path);
65436f3b4f6SAl Viro 		return error;
65536f3b4f6SAl Viro 	}
65636f3b4f6SAl Viro 
65786acdca1SAl Viro 	nd->last_type = LAST_BIND;
658def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
659def4af30SAl Viro 	error = PTR_ERR(*p);
660def4af30SAl Viro 	if (!IS_ERR(*p)) {
6611da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
662cc314eefSLinus Torvalds 		error = 0;
6631da177e4SLinus Torvalds 		if (s)
6641da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
665bcda7652SAl Viro 		else if (nd->last_type == LAST_BIND) {
66616c2cd71SAl Viro 			nd->flags |= LOOKUP_JUMPED;
667b21041d0SAl Viro 			nd->inode = nd->path.dentry->d_inode;
668b21041d0SAl Viro 			if (nd->inode->i_op->follow_link) {
669bcda7652SAl Viro 				/* stepped on a _really_ weird one */
670bcda7652SAl Viro 				path_put(&nd->path);
671bcda7652SAl Viro 				error = -ELOOP;
672bcda7652SAl Viro 			}
673bcda7652SAl Viro 		}
6741da177e4SLinus Torvalds 	}
6751da177e4SLinus Torvalds 	return error;
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds 
67831e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
67931e6b01fSNick Piggin {
6800714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
6810714a533SAl Viro 	struct mount *parent;
68231e6b01fSNick Piggin 	struct dentry *mountpoint;
68331e6b01fSNick Piggin 
6840714a533SAl Viro 	parent = mnt->mnt_parent;
6850714a533SAl Viro 	if (&parent->mnt == path->mnt)
68631e6b01fSNick Piggin 		return 0;
6870714a533SAl Viro 	mountpoint = mnt->mnt.mnt_mountpoint;
68831e6b01fSNick Piggin 	path->dentry = mountpoint;
6890714a533SAl Viro 	path->mnt = &parent->mnt;
69031e6b01fSNick Piggin 	return 1;
69131e6b01fSNick Piggin }
69231e6b01fSNick Piggin 
693bab77ebfSAl Viro int follow_up(struct path *path)
6941da177e4SLinus Torvalds {
6950714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
6960714a533SAl Viro 	struct mount *parent;
6971da177e4SLinus Torvalds 	struct dentry *mountpoint;
69899b7db7bSNick Piggin 
69999b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
7000714a533SAl Viro 	parent = mnt->mnt_parent;
7010714a533SAl Viro 	if (&parent->mnt == path->mnt) {
70299b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
7031da177e4SLinus Torvalds 		return 0;
7041da177e4SLinus Torvalds 	}
7050714a533SAl Viro 	mntget(&parent->mnt);
7060714a533SAl Viro 	mountpoint = dget(mnt->mnt.mnt_mountpoint);
70799b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
708bab77ebfSAl Viro 	dput(path->dentry);
709bab77ebfSAl Viro 	path->dentry = mountpoint;
710bab77ebfSAl Viro 	mntput(path->mnt);
7110714a533SAl Viro 	path->mnt = &parent->mnt;
7121da177e4SLinus Torvalds 	return 1;
7131da177e4SLinus Torvalds }
7141da177e4SLinus Torvalds 
715b5c84bf6SNick Piggin /*
7169875cf80SDavid Howells  * Perform an automount
7179875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
7189875cf80SDavid Howells  *   were called with.
7191da177e4SLinus Torvalds  */
7209875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
7219875cf80SDavid Howells 			    bool *need_mntput)
72231e6b01fSNick Piggin {
7239875cf80SDavid Howells 	struct vfsmount *mnt;
724ea5b778aSDavid Howells 	int err;
7259875cf80SDavid Howells 
7269875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
7279875cf80SDavid Howells 		return -EREMOTE;
7289875cf80SDavid Howells 
7290ec26fd0SMiklos Szeredi 	/* We don't want to mount if someone's just doing a stat -
7300ec26fd0SMiklos Szeredi 	 * unless they're stat'ing a directory and appended a '/' to
7310ec26fd0SMiklos Szeredi 	 * the name.
7320ec26fd0SMiklos Szeredi 	 *
7330ec26fd0SMiklos Szeredi 	 * We do, however, want to mount if someone wants to open or
7345a30d8a2SDavid Howells 	 * create a file of any type under the mountpoint, wants to
7350ec26fd0SMiklos Szeredi 	 * traverse through the mountpoint or wants to open the
7360ec26fd0SMiklos Szeredi 	 * mounted directory.  Also, autofs may mark negative dentries
7370ec26fd0SMiklos Szeredi 	 * as being automount points.  These will need the attentions
7380ec26fd0SMiklos Szeredi 	 * of the daemon to instantiate them before they can be used.
7395a30d8a2SDavid Howells 	 */
7405a30d8a2SDavid Howells 	if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
741d94c177bSLinus Torvalds 		     LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
7425a30d8a2SDavid Howells 	    path->dentry->d_inode)
7439875cf80SDavid Howells 		return -EISDIR;
7440ec26fd0SMiklos Szeredi 
7459875cf80SDavid Howells 	current->total_link_count++;
7469875cf80SDavid Howells 	if (current->total_link_count >= 40)
7479875cf80SDavid Howells 		return -ELOOP;
7489875cf80SDavid Howells 
7499875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
7509875cf80SDavid Howells 	if (IS_ERR(mnt)) {
7519875cf80SDavid Howells 		/*
7529875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
7539875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
7549875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
7559875cf80SDavid Howells 		 *
7569875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
7579875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
7589875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
7599875cf80SDavid Howells 		 */
76049084c3bSAl Viro 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
7619875cf80SDavid Howells 			return -EREMOTE;
7629875cf80SDavid Howells 		return PTR_ERR(mnt);
76331e6b01fSNick Piggin 	}
764ea5b778aSDavid Howells 
7659875cf80SDavid Howells 	if (!mnt) /* mount collision */
7669875cf80SDavid Howells 		return 0;
7679875cf80SDavid Howells 
7688aef1884SAl Viro 	if (!*need_mntput) {
7698aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
7708aef1884SAl Viro 		mntget(path->mnt);
7718aef1884SAl Viro 		*need_mntput = true;
7728aef1884SAl Viro 	}
77319a167afSAl Viro 	err = finish_automount(mnt, path);
774ea5b778aSDavid Howells 
775ea5b778aSDavid Howells 	switch (err) {
776ea5b778aSDavid Howells 	case -EBUSY:
777ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
77819a167afSAl Viro 		return 0;
779ea5b778aSDavid Howells 	case 0:
7808aef1884SAl Viro 		path_put(path);
7819875cf80SDavid Howells 		path->mnt = mnt;
7829875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
7839875cf80SDavid Howells 		return 0;
78419a167afSAl Viro 	default:
78519a167afSAl Viro 		return err;
7869875cf80SDavid Howells 	}
78719a167afSAl Viro 
788ea5b778aSDavid Howells }
7899875cf80SDavid Howells 
7909875cf80SDavid Howells /*
7919875cf80SDavid Howells  * Handle a dentry that is managed in some way.
792cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
7939875cf80SDavid Howells  * - Flagged as mountpoint
7949875cf80SDavid Howells  * - Flagged as automount point
7959875cf80SDavid Howells  *
7969875cf80SDavid Howells  * This may only be called in refwalk mode.
7979875cf80SDavid Howells  *
7989875cf80SDavid Howells  * Serialization is taken care of in namespace.c
7999875cf80SDavid Howells  */
8009875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
8019875cf80SDavid Howells {
8028aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
8039875cf80SDavid Howells 	unsigned managed;
8049875cf80SDavid Howells 	bool need_mntput = false;
8058aef1884SAl Viro 	int ret = 0;
8069875cf80SDavid Howells 
8079875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
8089875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
8099875cf80SDavid Howells 	 * the components of that value change under us */
8109875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
8119875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
8129875cf80SDavid Howells 	       unlikely(managed != 0)) {
813cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
814cc53ce53SDavid Howells 		 * being held. */
815cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
816cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
817cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
8181aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
819cc53ce53SDavid Howells 			if (ret < 0)
8208aef1884SAl Viro 				break;
821cc53ce53SDavid Howells 		}
822cc53ce53SDavid Howells 
8239875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
8249875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
8259875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
8269875cf80SDavid Howells 			if (mounted) {
8279875cf80SDavid Howells 				dput(path->dentry);
8289875cf80SDavid Howells 				if (need_mntput)
829463ffb2eSAl Viro 					mntput(path->mnt);
830463ffb2eSAl Viro 				path->mnt = mounted;
831463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
8329875cf80SDavid Howells 				need_mntput = true;
8339875cf80SDavid Howells 				continue;
834463ffb2eSAl Viro 			}
835463ffb2eSAl Viro 
8369875cf80SDavid Howells 			/* Something is mounted on this dentry in another
8379875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
8389875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
8399875cf80SDavid Howells 			 * vfsmount_lock */
8401da177e4SLinus Torvalds 		}
8419875cf80SDavid Howells 
8429875cf80SDavid Howells 		/* Handle an automount point */
8439875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
8449875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
8459875cf80SDavid Howells 			if (ret < 0)
8468aef1884SAl Viro 				break;
8479875cf80SDavid Howells 			continue;
8489875cf80SDavid Howells 		}
8499875cf80SDavid Howells 
8509875cf80SDavid Howells 		/* We didn't change the current path point */
8519875cf80SDavid Howells 		break;
8529875cf80SDavid Howells 	}
8538aef1884SAl Viro 
8548aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
8558aef1884SAl Viro 		mntput(path->mnt);
8568aef1884SAl Viro 	if (ret == -EISDIR)
8578aef1884SAl Viro 		ret = 0;
858a3fbbde7SAl Viro 	return ret < 0 ? ret : need_mntput;
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds 
861cc53ce53SDavid Howells int follow_down_one(struct path *path)
8621da177e4SLinus Torvalds {
8631da177e4SLinus Torvalds 	struct vfsmount *mounted;
8641da177e4SLinus Torvalds 
8651c755af4SAl Viro 	mounted = lookup_mnt(path);
8661da177e4SLinus Torvalds 	if (mounted) {
8679393bd07SAl Viro 		dput(path->dentry);
8689393bd07SAl Viro 		mntput(path->mnt);
8699393bd07SAl Viro 		path->mnt = mounted;
8709393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
8711da177e4SLinus Torvalds 		return 1;
8721da177e4SLinus Torvalds 	}
8731da177e4SLinus Torvalds 	return 0;
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
87662a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
87762a7375eSIan Kent {
87862a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
87962a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
88062a7375eSIan Kent }
88162a7375eSIan Kent 
8829875cf80SDavid Howells /*
883287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
884287548e4SAl Viro  * we meet a managed dentry that would need blocking.
8859875cf80SDavid Howells  */
8869875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
887287548e4SAl Viro 			       struct inode **inode)
8889875cf80SDavid Howells {
88962a7375eSIan Kent 	for (;;) {
890c7105365SAl Viro 		struct mount *mounted;
89162a7375eSIan Kent 		/*
89262a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
89362a7375eSIan Kent 		 * that wants to block transit.
89462a7375eSIan Kent 		 */
895287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
896ab90911fSDavid Howells 			return false;
89762a7375eSIan Kent 
89862a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
89962a7375eSIan Kent 			break;
90062a7375eSIan Kent 
9019875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
9029875cf80SDavid Howells 		if (!mounted)
9039875cf80SDavid Howells 			break;
904c7105365SAl Viro 		path->mnt = &mounted->mnt;
905c7105365SAl Viro 		path->dentry = mounted->mnt.mnt_root;
906a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
9079875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
90859430262SLinus Torvalds 		/*
90959430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
91059430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
91159430262SLinus Torvalds 		 * because a mount-point is always pinned.
91259430262SLinus Torvalds 		 */
91359430262SLinus Torvalds 		*inode = path->dentry->d_inode;
9149875cf80SDavid Howells 	}
9159875cf80SDavid Howells 	return true;
9169875cf80SDavid Howells }
9179875cf80SDavid Howells 
918dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
919287548e4SAl Viro {
920dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
921c7105365SAl Viro 		struct mount *mounted;
922dea39376SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
923287548e4SAl Viro 		if (!mounted)
924287548e4SAl Viro 			break;
925c7105365SAl Viro 		nd->path.mnt = &mounted->mnt;
926c7105365SAl Viro 		nd->path.dentry = mounted->mnt.mnt_root;
927dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
928287548e4SAl Viro 	}
929287548e4SAl Viro }
930287548e4SAl Viro 
93131e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
93231e6b01fSNick Piggin {
93331e6b01fSNick Piggin 	set_root_rcu(nd);
93431e6b01fSNick Piggin 
93531e6b01fSNick Piggin 	while (1) {
93631e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
93731e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
93831e6b01fSNick Piggin 			break;
93931e6b01fSNick Piggin 		}
94031e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
94131e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
94231e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
94331e6b01fSNick Piggin 			unsigned seq;
94431e6b01fSNick Piggin 
94531e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
94631e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
947ef7562d5SAl Viro 				goto failed;
94831e6b01fSNick Piggin 			nd->path.dentry = parent;
94931e6b01fSNick Piggin 			nd->seq = seq;
95031e6b01fSNick Piggin 			break;
95131e6b01fSNick Piggin 		}
95231e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
95331e6b01fSNick Piggin 			break;
95431e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
95531e6b01fSNick Piggin 	}
956dea39376SAl Viro 	follow_mount_rcu(nd);
957dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
95831e6b01fSNick Piggin 	return 0;
959ef7562d5SAl Viro 
960ef7562d5SAl Viro failed:
961ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
9625b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
963ef7562d5SAl Viro 		nd->root.mnt = NULL;
964ef7562d5SAl Viro 	rcu_read_unlock();
965ef7562d5SAl Viro 	br_read_unlock(vfsmount_lock);
966ef7562d5SAl Viro 	return -ECHILD;
96731e6b01fSNick Piggin }
96831e6b01fSNick Piggin 
9699875cf80SDavid Howells /*
970cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
971cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
972cc53ce53SDavid Howells  * caller is permitted to proceed or not.
973cc53ce53SDavid Howells  */
9747cc90cc3SAl Viro int follow_down(struct path *path)
975cc53ce53SDavid Howells {
976cc53ce53SDavid Howells 	unsigned managed;
977cc53ce53SDavid Howells 	int ret;
978cc53ce53SDavid Howells 
979cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
980cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
981cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
982cc53ce53SDavid Howells 		 * being held.
983cc53ce53SDavid Howells 		 *
984cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
985cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
986cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
987cc53ce53SDavid Howells 		 * superstructure.
988cc53ce53SDavid Howells 		 *
989cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
990cc53ce53SDavid Howells 		 */
991cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
992cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
993cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
994ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
9951aed3e42SAl Viro 				path->dentry, false);
996cc53ce53SDavid Howells 			if (ret < 0)
997cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
998cc53ce53SDavid Howells 		}
999cc53ce53SDavid Howells 
1000cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
1001cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
1002cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
1003cc53ce53SDavid Howells 			if (!mounted)
1004cc53ce53SDavid Howells 				break;
1005cc53ce53SDavid Howells 			dput(path->dentry);
1006cc53ce53SDavid Howells 			mntput(path->mnt);
1007cc53ce53SDavid Howells 			path->mnt = mounted;
1008cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1009cc53ce53SDavid Howells 			continue;
1010cc53ce53SDavid Howells 		}
1011cc53ce53SDavid Howells 
1012cc53ce53SDavid Howells 		/* Don't handle automount points here */
1013cc53ce53SDavid Howells 		break;
1014cc53ce53SDavid Howells 	}
1015cc53ce53SDavid Howells 	return 0;
1016cc53ce53SDavid Howells }
1017cc53ce53SDavid Howells 
1018cc53ce53SDavid Howells /*
10199875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
10209875cf80SDavid Howells  */
10219875cf80SDavid Howells static void follow_mount(struct path *path)
10229875cf80SDavid Howells {
10239875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
10249875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
10259875cf80SDavid Howells 		if (!mounted)
10269875cf80SDavid Howells 			break;
10279875cf80SDavid Howells 		dput(path->dentry);
10289875cf80SDavid Howells 		mntput(path->mnt);
10299875cf80SDavid Howells 		path->mnt = mounted;
10309875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
10319875cf80SDavid Howells 	}
10329875cf80SDavid Howells }
10339875cf80SDavid Howells 
103431e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
10351da177e4SLinus Torvalds {
10362a737871SAl Viro 	set_root(nd);
1037e518ddb7SAndreas Mohr 
10381da177e4SLinus Torvalds 	while(1) {
10394ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
10401da177e4SLinus Torvalds 
10412a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
10422a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
10431da177e4SLinus Torvalds 			break;
10441da177e4SLinus Torvalds 		}
10454ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
10463088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
10473088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
10481da177e4SLinus Torvalds 			dput(old);
10491da177e4SLinus Torvalds 			break;
10501da177e4SLinus Torvalds 		}
10513088dd70SAl Viro 		if (!follow_up(&nd->path))
10521da177e4SLinus Torvalds 			break;
10531da177e4SLinus Torvalds 	}
105479ed0226SAl Viro 	follow_mount(&nd->path);
105531e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
10561da177e4SLinus Torvalds }
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds /*
1059baa03890SNick Piggin  * Allocate a dentry with name and parent, and perform a parent
1060baa03890SNick Piggin  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1061baa03890SNick Piggin  * on error. parent->d_inode->i_mutex must be held. d_lookup must
1062baa03890SNick Piggin  * have verified that no child exists while under i_mutex.
1063baa03890SNick Piggin  */
1064baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1065baa03890SNick Piggin 				struct qstr *name, struct nameidata *nd)
1066baa03890SNick Piggin {
1067baa03890SNick Piggin 	struct inode *inode = parent->d_inode;
1068baa03890SNick Piggin 	struct dentry *dentry;
1069baa03890SNick Piggin 	struct dentry *old;
1070baa03890SNick Piggin 
1071baa03890SNick Piggin 	/* Don't create child dentry for a dead directory. */
1072baa03890SNick Piggin 	if (unlikely(IS_DEADDIR(inode)))
1073baa03890SNick Piggin 		return ERR_PTR(-ENOENT);
1074baa03890SNick Piggin 
1075baa03890SNick Piggin 	dentry = d_alloc(parent, name);
1076baa03890SNick Piggin 	if (unlikely(!dentry))
1077baa03890SNick Piggin 		return ERR_PTR(-ENOMEM);
1078baa03890SNick Piggin 
1079baa03890SNick Piggin 	old = inode->i_op->lookup(inode, dentry, nd);
1080baa03890SNick Piggin 	if (unlikely(old)) {
1081baa03890SNick Piggin 		dput(dentry);
1082baa03890SNick Piggin 		dentry = old;
1083baa03890SNick Piggin 	}
1084baa03890SNick Piggin 	return dentry;
1085baa03890SNick Piggin }
1086baa03890SNick Piggin 
1087baa03890SNick Piggin /*
108844396f4bSJosef Bacik  * We already have a dentry, but require a lookup to be performed on the parent
108944396f4bSJosef Bacik  * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
109044396f4bSJosef Bacik  * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
109144396f4bSJosef Bacik  * child exists while under i_mutex.
109244396f4bSJosef Bacik  */
109344396f4bSJosef Bacik static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
109444396f4bSJosef Bacik 				     struct nameidata *nd)
109544396f4bSJosef Bacik {
109644396f4bSJosef Bacik 	struct inode *inode = parent->d_inode;
109744396f4bSJosef Bacik 	struct dentry *old;
109844396f4bSJosef Bacik 
109944396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
110044396f4bSJosef Bacik 	if (unlikely(IS_DEADDIR(inode)))
110144396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
110244396f4bSJosef Bacik 
110344396f4bSJosef Bacik 	old = inode->i_op->lookup(inode, dentry, nd);
110444396f4bSJosef Bacik 	if (unlikely(old)) {
110544396f4bSJosef Bacik 		dput(dentry);
110644396f4bSJosef Bacik 		dentry = old;
110744396f4bSJosef Bacik 	}
110844396f4bSJosef Bacik 	return dentry;
110944396f4bSJosef Bacik }
111044396f4bSJosef Bacik 
111144396f4bSJosef Bacik /*
11121da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
11131da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
11141da177e4SLinus Torvalds  *  It _is_ time-critical.
11151da177e4SLinus Torvalds  */
11161da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
111731e6b01fSNick Piggin 			struct path *path, struct inode **inode)
11181da177e4SLinus Torvalds {
11194ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
112031e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
11215a18fff2SAl Viro 	int need_reval = 1;
11225a18fff2SAl Viro 	int status = 1;
11239875cf80SDavid Howells 	int err;
11249875cf80SDavid Howells 
11253cac260aSAl Viro 	/*
1126b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1127b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1128b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1129b04f784eSNick Piggin 	 */
113031e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
113131e6b01fSNick Piggin 		unsigned seq;
113231e6b01fSNick Piggin 		*inode = nd->inode;
113331e6b01fSNick Piggin 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
11345a18fff2SAl Viro 		if (!dentry)
11355a18fff2SAl Viro 			goto unlazy;
11365a18fff2SAl Viro 
113731e6b01fSNick Piggin 		/* Memory barrier in read_seqcount_begin of child is enough */
113831e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
113931e6b01fSNick Piggin 			return -ECHILD;
114031e6b01fSNick Piggin 		nd->seq = seq;
11415a18fff2SAl Viro 
114224643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
11435a18fff2SAl Viro 			status = d_revalidate(dentry, nd);
11445a18fff2SAl Viro 			if (unlikely(status <= 0)) {
11455a18fff2SAl Viro 				if (status != -ECHILD)
11465a18fff2SAl Viro 					need_reval = 0;
11475a18fff2SAl Viro 				goto unlazy;
11485a18fff2SAl Viro 			}
114924643087SAl Viro 		}
115044396f4bSJosef Bacik 		if (unlikely(d_need_lookup(dentry)))
115144396f4bSJosef Bacik 			goto unlazy;
115231e6b01fSNick Piggin 		path->mnt = mnt;
115331e6b01fSNick Piggin 		path->dentry = dentry;
1154d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1155d6e9bd25SAl Viro 			goto unlazy;
1156d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1157d6e9bd25SAl Viro 			goto unlazy;
11589875cf80SDavid Howells 		return 0;
11595a18fff2SAl Viro unlazy:
116019660af7SAl Viro 		if (unlazy_walk(nd, dentry))
11615a18fff2SAl Viro 			return -ECHILD;
11625a18fff2SAl Viro 	} else {
116331e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
116424643087SAl Viro 	}
11655a18fff2SAl Viro 
116644396f4bSJosef Bacik 	if (dentry && unlikely(d_need_lookup(dentry))) {
116744396f4bSJosef Bacik 		dput(dentry);
116844396f4bSJosef Bacik 		dentry = NULL;
116944396f4bSJosef Bacik 	}
11705a18fff2SAl Viro retry:
11715a18fff2SAl Viro 	if (unlikely(!dentry)) {
11725a18fff2SAl Viro 		struct inode *dir = parent->d_inode;
11735a18fff2SAl Viro 		BUG_ON(nd->inode != dir);
11745a18fff2SAl Viro 
11755a18fff2SAl Viro 		mutex_lock(&dir->i_mutex);
11765a18fff2SAl Viro 		dentry = d_lookup(parent, name);
11775a18fff2SAl Viro 		if (likely(!dentry)) {
11785a18fff2SAl Viro 			dentry = d_alloc_and_lookup(parent, name, nd);
11795a18fff2SAl Viro 			if (IS_ERR(dentry)) {
11805a18fff2SAl Viro 				mutex_unlock(&dir->i_mutex);
11815a18fff2SAl Viro 				return PTR_ERR(dentry);
11825a18fff2SAl Viro 			}
11835a18fff2SAl Viro 			/* known good */
11845a18fff2SAl Viro 			need_reval = 0;
11855a18fff2SAl Viro 			status = 1;
118644396f4bSJosef Bacik 		} else if (unlikely(d_need_lookup(dentry))) {
118744396f4bSJosef Bacik 			dentry = d_inode_lookup(parent, dentry, nd);
118844396f4bSJosef Bacik 			if (IS_ERR(dentry)) {
118944396f4bSJosef Bacik 				mutex_unlock(&dir->i_mutex);
119044396f4bSJosef Bacik 				return PTR_ERR(dentry);
119144396f4bSJosef Bacik 			}
119244396f4bSJosef Bacik 			/* known good */
119344396f4bSJosef Bacik 			need_reval = 0;
119444396f4bSJosef Bacik 			status = 1;
11955a18fff2SAl Viro 		}
11965a18fff2SAl Viro 		mutex_unlock(&dir->i_mutex);
11975a18fff2SAl Viro 	}
11985a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
11995a18fff2SAl Viro 		status = d_revalidate(dentry, nd);
12005a18fff2SAl Viro 	if (unlikely(status <= 0)) {
12015a18fff2SAl Viro 		if (status < 0) {
12025a18fff2SAl Viro 			dput(dentry);
12035a18fff2SAl Viro 			return status;
12045a18fff2SAl Viro 		}
12055a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
12065a18fff2SAl Viro 			dput(dentry);
12075a18fff2SAl Viro 			dentry = NULL;
12085a18fff2SAl Viro 			need_reval = 1;
12095a18fff2SAl Viro 			goto retry;
12105a18fff2SAl Viro 		}
12115a18fff2SAl Viro 	}
12125a18fff2SAl Viro 
12131da177e4SLinus Torvalds 	path->mnt = mnt;
12141da177e4SLinus Torvalds 	path->dentry = dentry;
12159875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
121689312214SIan Kent 	if (unlikely(err < 0)) {
121789312214SIan Kent 		path_put_conditional(path, nd);
12189875cf80SDavid Howells 		return err;
121989312214SIan Kent 	}
1220a3fbbde7SAl Viro 	if (err)
1221a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
122231e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
12231da177e4SLinus Torvalds 	return 0;
12241da177e4SLinus Torvalds }
12251da177e4SLinus Torvalds 
122652094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
122752094c8aSAl Viro {
122852094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
12294ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
123052094c8aSAl Viro 		if (err != -ECHILD)
123152094c8aSAl Viro 			return err;
123219660af7SAl Viro 		if (unlazy_walk(nd, NULL))
123352094c8aSAl Viro 			return -ECHILD;
123452094c8aSAl Viro 	}
12354ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
123652094c8aSAl Viro }
123752094c8aSAl Viro 
12389856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
12399856fa1bSAl Viro {
12409856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
12419856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
12429856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
12439856fa1bSAl Viro 				return -ECHILD;
12449856fa1bSAl Viro 		} else
12459856fa1bSAl Viro 			follow_dotdot(nd);
12469856fa1bSAl Viro 	}
12479856fa1bSAl Viro 	return 0;
12489856fa1bSAl Viro }
12499856fa1bSAl Viro 
1250951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1251951361f9SAl Viro {
1252951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1253951361f9SAl Viro 		path_put(&nd->path);
1254951361f9SAl Viro 	} else {
1255951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
12565b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1257951361f9SAl Viro 			nd->root.mnt = NULL;
1258951361f9SAl Viro 		rcu_read_unlock();
1259951361f9SAl Viro 		br_read_unlock(vfsmount_lock);
1260951361f9SAl Viro 	}
1261951361f9SAl Viro }
1262951361f9SAl Viro 
12633ddcd056SLinus Torvalds /*
12643ddcd056SLinus Torvalds  * Do we need to follow links? We _really_ want to be able
12653ddcd056SLinus Torvalds  * to do this check without having to look at inode->i_op,
12663ddcd056SLinus Torvalds  * so we keep a cache of "no, this doesn't need follow_link"
12673ddcd056SLinus Torvalds  * for the common case.
12683ddcd056SLinus Torvalds  */
12697813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow)
12703ddcd056SLinus Torvalds {
12713ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
12723ddcd056SLinus Torvalds 		if (likely(inode->i_op->follow_link))
12733ddcd056SLinus Torvalds 			return follow;
12743ddcd056SLinus Torvalds 
12753ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
12763ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
12773ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_NOFOLLOW;
12783ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
12793ddcd056SLinus Torvalds 	}
12803ddcd056SLinus Torvalds 	return 0;
12813ddcd056SLinus Torvalds }
12823ddcd056SLinus Torvalds 
1283ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
1284ce57dfc1SAl Viro 		struct qstr *name, int type, int follow)
1285ce57dfc1SAl Viro {
1286ce57dfc1SAl Viro 	struct inode *inode;
1287ce57dfc1SAl Viro 	int err;
1288ce57dfc1SAl Viro 	/*
1289ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1290ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1291ce57dfc1SAl Viro 	 * parent relationships.
1292ce57dfc1SAl Viro 	 */
1293ce57dfc1SAl Viro 	if (unlikely(type != LAST_NORM))
1294ce57dfc1SAl Viro 		return handle_dots(nd, type);
1295ce57dfc1SAl Viro 	err = do_lookup(nd, name, path, &inode);
1296ce57dfc1SAl Viro 	if (unlikely(err)) {
1297ce57dfc1SAl Viro 		terminate_walk(nd);
1298ce57dfc1SAl Viro 		return err;
1299ce57dfc1SAl Viro 	}
1300ce57dfc1SAl Viro 	if (!inode) {
1301ce57dfc1SAl Viro 		path_to_nameidata(path, nd);
1302ce57dfc1SAl Viro 		terminate_walk(nd);
1303ce57dfc1SAl Viro 		return -ENOENT;
1304ce57dfc1SAl Viro 	}
13057813b94aSLinus Torvalds 	if (should_follow_link(inode, follow)) {
130619660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
130719660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
130819660af7SAl Viro 				terminate_walk(nd);
1309ce57dfc1SAl Viro 				return -ECHILD;
131019660af7SAl Viro 			}
131119660af7SAl Viro 		}
1312ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1313ce57dfc1SAl Viro 		return 1;
1314ce57dfc1SAl Viro 	}
1315ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1316ce57dfc1SAl Viro 	nd->inode = inode;
1317ce57dfc1SAl Viro 	return 0;
1318ce57dfc1SAl Viro }
1319ce57dfc1SAl Viro 
13201da177e4SLinus Torvalds /*
1321b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1322b356379aSAl Viro  * limiting consecutive symlinks to 40.
1323b356379aSAl Viro  *
1324b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1325b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1326b356379aSAl Viro  */
1327b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1328b356379aSAl Viro {
1329b356379aSAl Viro 	int res;
1330b356379aSAl Viro 
1331b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1332b356379aSAl Viro 		path_put_conditional(path, nd);
1333b356379aSAl Viro 		path_put(&nd->path);
1334b356379aSAl Viro 		return -ELOOP;
1335b356379aSAl Viro 	}
13361a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1337b356379aSAl Viro 
1338b356379aSAl Viro 	nd->depth++;
1339b356379aSAl Viro 	current->link_count++;
1340b356379aSAl Viro 
1341b356379aSAl Viro 	do {
1342b356379aSAl Viro 		struct path link = *path;
1343b356379aSAl Viro 		void *cookie;
1344574197e0SAl Viro 
1345574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
1346b356379aSAl Viro 		if (!res)
1347b356379aSAl Viro 			res = walk_component(nd, path, &nd->last,
1348b356379aSAl Viro 					     nd->last_type, LOOKUP_FOLLOW);
1349574197e0SAl Viro 		put_link(nd, &link, cookie);
1350b356379aSAl Viro 	} while (res > 0);
1351b356379aSAl Viro 
1352b356379aSAl Viro 	current->link_count--;
1353b356379aSAl Viro 	nd->depth--;
1354b356379aSAl Viro 	return res;
1355b356379aSAl Viro }
1356b356379aSAl Viro 
1357b356379aSAl Viro /*
13583ddcd056SLinus Torvalds  * We really don't want to look at inode->i_op->lookup
13593ddcd056SLinus Torvalds  * when we don't have to. So we keep a cache bit in
13603ddcd056SLinus Torvalds  * the inode ->i_opflags field that says "yes, we can
13613ddcd056SLinus Torvalds  * do lookup on this inode".
13623ddcd056SLinus Torvalds  */
13633ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode)
13643ddcd056SLinus Torvalds {
13653ddcd056SLinus Torvalds 	if (likely(inode->i_opflags & IOP_LOOKUP))
13663ddcd056SLinus Torvalds 		return 1;
13673ddcd056SLinus Torvalds 	if (likely(!inode->i_op->lookup))
13683ddcd056SLinus Torvalds 		return 0;
13693ddcd056SLinus Torvalds 
13703ddcd056SLinus Torvalds 	/* We do this once for the lifetime of the inode */
13713ddcd056SLinus Torvalds 	spin_lock(&inode->i_lock);
13723ddcd056SLinus Torvalds 	inode->i_opflags |= IOP_LOOKUP;
13733ddcd056SLinus Torvalds 	spin_unlock(&inode->i_lock);
13743ddcd056SLinus Torvalds 	return 1;
13753ddcd056SLinus Torvalds }
13763ddcd056SLinus Torvalds 
13773ddcd056SLinus Torvalds /*
13781da177e4SLinus Torvalds  * Name resolution.
1379ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1380ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
13811da177e4SLinus Torvalds  *
1382ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1383ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
13841da177e4SLinus Torvalds  */
13856de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
13861da177e4SLinus Torvalds {
13871da177e4SLinus Torvalds 	struct path next;
13881da177e4SLinus Torvalds 	int err;
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds 	while (*name=='/')
13911da177e4SLinus Torvalds 		name++;
13921da177e4SLinus Torvalds 	if (!*name)
1393086e183aSAl Viro 		return 0;
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
13961da177e4SLinus Torvalds 	for(;;) {
13971da177e4SLinus Torvalds 		unsigned long hash;
13981da177e4SLinus Torvalds 		struct qstr this;
13991da177e4SLinus Torvalds 		unsigned int c;
1400fe479a58SAl Viro 		int type;
14011da177e4SLinus Torvalds 
140252094c8aSAl Viro 		err = may_lookup(nd);
14031da177e4SLinus Torvalds  		if (err)
14041da177e4SLinus Torvalds 			break;
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds 		this.name = name;
14071da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds 		hash = init_name_hash();
14101da177e4SLinus Torvalds 		do {
14111da177e4SLinus Torvalds 			name++;
14121da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
14131da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
14141da177e4SLinus Torvalds 		} while (c && (c != '/'));
14151da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
14161da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
14171da177e4SLinus Torvalds 
1418fe479a58SAl Viro 		type = LAST_NORM;
1419fe479a58SAl Viro 		if (this.name[0] == '.') switch (this.len) {
1420fe479a58SAl Viro 			case 2:
142116c2cd71SAl Viro 				if (this.name[1] == '.') {
1422fe479a58SAl Viro 					type = LAST_DOTDOT;
142316c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
142416c2cd71SAl Viro 				}
1425fe479a58SAl Viro 				break;
1426fe479a58SAl Viro 			case 1:
1427fe479a58SAl Viro 				type = LAST_DOT;
1428fe479a58SAl Viro 		}
14295a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
14305a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
143116c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
14325a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
14335a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
14345a202bcdSAl Viro 							   &this);
14355a202bcdSAl Viro 				if (err < 0)
14365a202bcdSAl Viro 					break;
14375a202bcdSAl Viro 			}
14385a202bcdSAl Viro 		}
1439fe479a58SAl Viro 
14401da177e4SLinus Torvalds 		/* remove trailing slashes? */
14411da177e4SLinus Torvalds 		if (!c)
14421da177e4SLinus Torvalds 			goto last_component;
14431da177e4SLinus Torvalds 		while (*++name == '/');
14441da177e4SLinus Torvalds 		if (!*name)
1445b356379aSAl Viro 			goto last_component;
14461da177e4SLinus Torvalds 
1447ce57dfc1SAl Viro 		err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1448ce57dfc1SAl Viro 		if (err < 0)
1449ce57dfc1SAl Viro 			return err;
1450fe479a58SAl Viro 
1451ce57dfc1SAl Viro 		if (err) {
1452b356379aSAl Viro 			err = nested_symlink(&next, nd);
14531da177e4SLinus Torvalds 			if (err)
1454a7472babSAl Viro 				return err;
145531e6b01fSNick Piggin 		}
14563ddcd056SLinus Torvalds 		if (can_lookup(nd->inode))
14571da177e4SLinus Torvalds 			continue;
14583ddcd056SLinus Torvalds 		err = -ENOTDIR;
14593ddcd056SLinus Torvalds 		break;
14601da177e4SLinus Torvalds 		/* here ends the main loop */
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds last_component:
1463ce57dfc1SAl Viro 		nd->last = this;
1464ce57dfc1SAl Viro 		nd->last_type = type;
1465ce57dfc1SAl Viro 		return 0;
1466ce57dfc1SAl Viro 	}
1467951361f9SAl Viro 	terminate_walk(nd);
14681da177e4SLinus Torvalds 	return err;
14691da177e4SLinus Torvalds }
14701da177e4SLinus Torvalds 
147170e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
147270e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
147331e6b01fSNick Piggin {
147431e6b01fSNick Piggin 	int retval = 0;
147531e6b01fSNick Piggin 	int fput_needed;
147631e6b01fSNick Piggin 	struct file *file;
147731e6b01fSNick Piggin 
147831e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
147916c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
148031e6b01fSNick Piggin 	nd->depth = 0;
14815b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
14825b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
148373d049a4SAl Viro 		if (*name) {
14845b6ca027SAl Viro 			if (!inode->i_op->lookup)
14855b6ca027SAl Viro 				return -ENOTDIR;
14865b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
14875b6ca027SAl Viro 			if (retval)
14885b6ca027SAl Viro 				return retval;
148973d049a4SAl Viro 		}
14905b6ca027SAl Viro 		nd->path = nd->root;
14915b6ca027SAl Viro 		nd->inode = inode;
14925b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
14935b6ca027SAl Viro 			br_read_lock(vfsmount_lock);
14945b6ca027SAl Viro 			rcu_read_lock();
14955b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
14965b6ca027SAl Viro 		} else {
14975b6ca027SAl Viro 			path_get(&nd->path);
14985b6ca027SAl Viro 		}
14995b6ca027SAl Viro 		return 0;
15005b6ca027SAl Viro 	}
15015b6ca027SAl Viro 
150231e6b01fSNick Piggin 	nd->root.mnt = NULL;
150331e6b01fSNick Piggin 
150431e6b01fSNick Piggin 	if (*name=='/') {
1505e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
150631e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
150731e6b01fSNick Piggin 			rcu_read_lock();
1508e41f7d4eSAl Viro 			set_root_rcu(nd);
1509e41f7d4eSAl Viro 		} else {
1510e41f7d4eSAl Viro 			set_root(nd);
1511e41f7d4eSAl Viro 			path_get(&nd->root);
1512e41f7d4eSAl Viro 		}
151331e6b01fSNick Piggin 		nd->path = nd->root;
151431e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1515e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
151631e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1517c28cc364SNick Piggin 			unsigned seq;
151831e6b01fSNick Piggin 
151931e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
152031e6b01fSNick Piggin 			rcu_read_lock();
152131e6b01fSNick Piggin 
1522c28cc364SNick Piggin 			do {
1523c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
152431e6b01fSNick Piggin 				nd->path = fs->pwd;
1525c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1526c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1527e41f7d4eSAl Viro 		} else {
1528e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1529e41f7d4eSAl Viro 		}
153031e6b01fSNick Piggin 	} else {
153131e6b01fSNick Piggin 		struct dentry *dentry;
153231e6b01fSNick Piggin 
15331abf0c71SAl Viro 		file = fget_raw_light(dfd, &fput_needed);
153431e6b01fSNick Piggin 		retval = -EBADF;
153531e6b01fSNick Piggin 		if (!file)
153631e6b01fSNick Piggin 			goto out_fail;
153731e6b01fSNick Piggin 
153831e6b01fSNick Piggin 		dentry = file->f_path.dentry;
153931e6b01fSNick Piggin 
1540f52e0c11SAl Viro 		if (*name) {
154131e6b01fSNick Piggin 			retval = -ENOTDIR;
154231e6b01fSNick Piggin 			if (!S_ISDIR(dentry->d_inode->i_mode))
154331e6b01fSNick Piggin 				goto fput_fail;
154431e6b01fSNick Piggin 
15454ad5abb3SAl Viro 			retval = inode_permission(dentry->d_inode, MAY_EXEC);
154631e6b01fSNick Piggin 			if (retval)
154731e6b01fSNick Piggin 				goto fput_fail;
1548f52e0c11SAl Viro 		}
154931e6b01fSNick Piggin 
155031e6b01fSNick Piggin 		nd->path = file->f_path;
1551e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
155231e6b01fSNick Piggin 			if (fput_needed)
155370e9b357SAl Viro 				*fp = file;
1554c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
155531e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
155631e6b01fSNick Piggin 			rcu_read_lock();
15575590ff0dSUlrich Drepper 		} else {
15585dd784d0SJan Blunck 			path_get(&file->f_path);
15595590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
15601da177e4SLinus Torvalds 		}
1561e41f7d4eSAl Viro 	}
1562e41f7d4eSAl Viro 
156331e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
15649b4a9b14SAl Viro 	return 0;
15652dfdd266SJosef 'Jeff' Sipek 
15669b4a9b14SAl Viro fput_fail:
15679b4a9b14SAl Viro 	fput_light(file, fput_needed);
15689b4a9b14SAl Viro out_fail:
15699b4a9b14SAl Viro 	return retval;
15709b4a9b14SAl Viro }
15719b4a9b14SAl Viro 
1572bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1573bd92d7feSAl Viro {
1574bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1575bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1576bd92d7feSAl Viro 
1577bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1578bd92d7feSAl Viro 	return walk_component(nd, path, &nd->last, nd->last_type,
1579bd92d7feSAl Viro 					nd->flags & LOOKUP_FOLLOW);
1580bd92d7feSAl Viro }
1581bd92d7feSAl Viro 
15829b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1583ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
15849b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
15859b4a9b14SAl Viro {
158670e9b357SAl Viro 	struct file *base = NULL;
1587bd92d7feSAl Viro 	struct path path;
1588bd92d7feSAl Viro 	int err;
158931e6b01fSNick Piggin 
159031e6b01fSNick Piggin 	/*
159131e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
159231e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
159331e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
159431e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
159531e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
159631e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
159731e6b01fSNick Piggin 	 * analogue, foo_rcu().
159831e6b01fSNick Piggin 	 *
159931e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
160031e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
160131e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
160231e6b01fSNick Piggin 	 * be able to complete).
160331e6b01fSNick Piggin 	 */
1604bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1605ee0827cdSAl Viro 
1606bd92d7feSAl Viro 	if (unlikely(err))
1607bd92d7feSAl Viro 		return err;
1608ee0827cdSAl Viro 
1609ee0827cdSAl Viro 	current->total_link_count = 0;
1610bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1611bd92d7feSAl Viro 
1612bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1613bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1614bd92d7feSAl Viro 		while (err > 0) {
1615bd92d7feSAl Viro 			void *cookie;
1616bd92d7feSAl Viro 			struct path link = path;
1617bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1618574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
1619bd92d7feSAl Viro 			if (!err)
1620bd92d7feSAl Viro 				err = lookup_last(nd, &path);
1621574197e0SAl Viro 			put_link(nd, &link, cookie);
1622bd92d7feSAl Viro 		}
1623bd92d7feSAl Viro 	}
1624ee0827cdSAl Viro 
16259f1fafeeSAl Viro 	if (!err)
16269f1fafeeSAl Viro 		err = complete_walk(nd);
1627bd92d7feSAl Viro 
1628bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1629bd92d7feSAl Viro 		if (!nd->inode->i_op->lookup) {
1630bd92d7feSAl Viro 			path_put(&nd->path);
1631bd23a539SAl Viro 			err = -ENOTDIR;
1632bd92d7feSAl Viro 		}
1633bd92d7feSAl Viro 	}
163416c2cd71SAl Viro 
163570e9b357SAl Viro 	if (base)
163670e9b357SAl Viro 		fput(base);
1637ee0827cdSAl Viro 
16385b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
163931e6b01fSNick Piggin 		path_put(&nd->root);
164031e6b01fSNick Piggin 		nd->root.mnt = NULL;
164131e6b01fSNick Piggin 	}
1642bd92d7feSAl Viro 	return err;
164331e6b01fSNick Piggin }
164431e6b01fSNick Piggin 
1645ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1646ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1647ee0827cdSAl Viro {
1648ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1649ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1650ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1651ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1652ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1653ee0827cdSAl Viro 
165431e6b01fSNick Piggin 	if (likely(!retval)) {
165531e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
165631e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
165731e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
165831e6b01fSNick Piggin 		}
165931e6b01fSNick Piggin 	}
1660170aa3d0SUlrich Drepper 	return retval;
16611da177e4SLinus Torvalds }
16621da177e4SLinus Torvalds 
1663c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd)
16645590ff0dSUlrich Drepper {
1665c9c6cac0SAl Viro 	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
16665590ff0dSUlrich Drepper }
16675590ff0dSUlrich Drepper 
1668d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1669d1811465SAl Viro {
1670d1811465SAl Viro 	struct nameidata nd;
1671d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1672d1811465SAl Viro 	if (!res)
1673d1811465SAl Viro 		*path = nd.path;
1674d1811465SAl Viro 	return res;
1675d1811465SAl Viro }
1676d1811465SAl Viro 
167716f18200SJosef 'Jeff' Sipek /**
167816f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
167916f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
168016f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
168116f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
168216f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
1683e0a01249SAl Viro  * @path: pointer to struct path to fill
168416f18200SJosef 'Jeff' Sipek  */
168516f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
168616f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
1687e0a01249SAl Viro 		    struct path *path)
168816f18200SJosef 'Jeff' Sipek {
1689e0a01249SAl Viro 	struct nameidata nd;
1690e0a01249SAl Viro 	int err;
1691e0a01249SAl Viro 	nd.root.dentry = dentry;
1692e0a01249SAl Viro 	nd.root.mnt = mnt;
1693e0a01249SAl Viro 	BUG_ON(flags & LOOKUP_PARENT);
16945b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1695e0a01249SAl Viro 	err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1696e0a01249SAl Viro 	if (!err)
1697e0a01249SAl Viro 		*path = nd.path;
1698e0a01249SAl Viro 	return err;
169916f18200SJosef 'Jeff' Sipek }
170016f18200SJosef 'Jeff' Sipek 
1701eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1702eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
17031da177e4SLinus Torvalds {
170481fca444SChristoph Hellwig 	struct inode *inode = base->d_inode;
17051da177e4SLinus Torvalds 	struct dentry *dentry;
17061da177e4SLinus Torvalds 	int err;
17071da177e4SLinus Torvalds 
17084ad5abb3SAl Viro 	err = inode_permission(inode, MAY_EXEC);
170981fca444SChristoph Hellwig 	if (err)
171081fca444SChristoph Hellwig 		return ERR_PTR(err);
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 	/*
1713b04f784eSNick Piggin 	 * Don't bother with __d_lookup: callers are for creat as
1714b04f784eSNick Piggin 	 * well as unlink, so a lot of the time it would cost
1715b04f784eSNick Piggin 	 * a double lookup.
17166e6b1bd1SAl Viro 	 */
17176e6b1bd1SAl Viro 	dentry = d_lookup(base, name);
17186e6b1bd1SAl Viro 
171944396f4bSJosef Bacik 	if (dentry && d_need_lookup(dentry)) {
172044396f4bSJosef Bacik 		/*
172144396f4bSJosef Bacik 		 * __lookup_hash is called with the parent dir's i_mutex already
172244396f4bSJosef Bacik 		 * held, so we are good to go here.
172344396f4bSJosef Bacik 		 */
172444396f4bSJosef Bacik 		dentry = d_inode_lookup(base, dentry, nd);
172544396f4bSJosef Bacik 		if (IS_ERR(dentry))
172644396f4bSJosef Bacik 			return dentry;
172744396f4bSJosef Bacik 	}
172844396f4bSJosef Bacik 
1729d2d9e9fbSAl Viro 	if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1730d2d9e9fbSAl Viro 		int status = d_revalidate(dentry, nd);
1731d2d9e9fbSAl Viro 		if (unlikely(status <= 0)) {
1732d2d9e9fbSAl Viro 			/*
1733d2d9e9fbSAl Viro 			 * The dentry failed validation.
1734d2d9e9fbSAl Viro 			 * If d_revalidate returned 0 attempt to invalidate
1735d2d9e9fbSAl Viro 			 * the dentry otherwise d_revalidate is asking us
1736d2d9e9fbSAl Viro 			 * to return a fail status.
1737d2d9e9fbSAl Viro 			 */
1738d2d9e9fbSAl Viro 			if (status < 0) {
1739d2d9e9fbSAl Viro 				dput(dentry);
1740d2d9e9fbSAl Viro 				return ERR_PTR(status);
1741d2d9e9fbSAl Viro 			} else if (!d_invalidate(dentry)) {
1742d2d9e9fbSAl Viro 				dput(dentry);
1743d2d9e9fbSAl Viro 				dentry = NULL;
1744d2d9e9fbSAl Viro 			}
1745d2d9e9fbSAl Viro 		}
1746d2d9e9fbSAl Viro 	}
17476e6b1bd1SAl Viro 
17481da177e4SLinus Torvalds 	if (!dentry)
1749baa03890SNick Piggin 		dentry = d_alloc_and_lookup(base, name, nd);
17505a202bcdSAl Viro 
17511da177e4SLinus Torvalds 	return dentry;
17521da177e4SLinus Torvalds }
17531da177e4SLinus Torvalds 
1754057f6c01SJames Morris /*
1755057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1756057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1757057f6c01SJames Morris  * SMP-safe.
1758057f6c01SJames Morris  */
1759a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
17601da177e4SLinus Torvalds {
17614ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
17621da177e4SLinus Torvalds }
17631da177e4SLinus Torvalds 
1764eead1911SChristoph Hellwig /**
1765a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1766eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1767eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1768eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1769eead1911SChristoph Hellwig  *
1770a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1771a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1772eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1773eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1774eead1911SChristoph Hellwig  */
1775057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1776057f6c01SJames Morris {
1777057f6c01SJames Morris 	struct qstr this;
17786a96ba54SAl Viro 	unsigned long hash;
17796a96ba54SAl Viro 	unsigned int c;
1780057f6c01SJames Morris 
17812f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
17822f9092e1SDavid Woodhouse 
17836a96ba54SAl Viro 	this.name = name;
17846a96ba54SAl Viro 	this.len = len;
17856a96ba54SAl Viro 	if (!len)
17866a96ba54SAl Viro 		return ERR_PTR(-EACCES);
17876a96ba54SAl Viro 
17886a96ba54SAl Viro 	hash = init_name_hash();
17896a96ba54SAl Viro 	while (len--) {
17906a96ba54SAl Viro 		c = *(const unsigned char *)name++;
17916a96ba54SAl Viro 		if (c == '/' || c == '\0')
17926a96ba54SAl Viro 			return ERR_PTR(-EACCES);
17936a96ba54SAl Viro 		hash = partial_name_hash(c, hash);
17946a96ba54SAl Viro 	}
17956a96ba54SAl Viro 	this.hash = end_name_hash(hash);
17965a202bcdSAl Viro 	/*
17975a202bcdSAl Viro 	 * See if the low-level filesystem might want
17985a202bcdSAl Viro 	 * to use its own hash..
17995a202bcdSAl Viro 	 */
18005a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
18015a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
18025a202bcdSAl Viro 		if (err < 0)
18035a202bcdSAl Viro 			return ERR_PTR(err);
18045a202bcdSAl Viro 	}
1805eead1911SChristoph Hellwig 
180649705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1807057f6c01SJames Morris }
1808057f6c01SJames Morris 
18091fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
18101fa1e7f6SAndy Whitcroft 		 struct path *path, int *empty)
18111da177e4SLinus Torvalds {
18122d8f3038SAl Viro 	struct nameidata nd;
18131fa1e7f6SAndy Whitcroft 	char *tmp = getname_flags(name, flags, empty);
18141da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
18151da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
18162d8f3038SAl Viro 
18172d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
18182d8f3038SAl Viro 
18192d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
18201da177e4SLinus Torvalds 		putname(tmp);
18212d8f3038SAl Viro 		if (!err)
18222d8f3038SAl Viro 			*path = nd.path;
18231da177e4SLinus Torvalds 	}
18241da177e4SLinus Torvalds 	return err;
18251da177e4SLinus Torvalds }
18261da177e4SLinus Torvalds 
18271fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags,
18281fa1e7f6SAndy Whitcroft 		 struct path *path)
18291fa1e7f6SAndy Whitcroft {
18301fa1e7f6SAndy Whitcroft 	return user_path_at_empty(dfd, name, flags, path, 0);
18311fa1e7f6SAndy Whitcroft }
18321fa1e7f6SAndy Whitcroft 
18332ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
18342ad94ae6SAl Viro 			struct nameidata *nd, char **name)
18352ad94ae6SAl Viro {
18362ad94ae6SAl Viro 	char *s = getname(path);
18372ad94ae6SAl Viro 	int error;
18382ad94ae6SAl Viro 
18392ad94ae6SAl Viro 	if (IS_ERR(s))
18402ad94ae6SAl Viro 		return PTR_ERR(s);
18412ad94ae6SAl Viro 
18422ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
18432ad94ae6SAl Viro 	if (error)
18442ad94ae6SAl Viro 		putname(s);
18452ad94ae6SAl Viro 	else
18462ad94ae6SAl Viro 		*name = s;
18472ad94ae6SAl Viro 
18482ad94ae6SAl Viro 	return error;
18492ad94ae6SAl Viro }
18502ad94ae6SAl Viro 
18511da177e4SLinus Torvalds /*
18521da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
18531da177e4SLinus Torvalds  * minimal.
18541da177e4SLinus Torvalds  */
18551da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
18561da177e4SLinus Torvalds {
1857da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1858da9592edSDavid Howells 
18591da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
18601da177e4SLinus Torvalds 		return 0;
1861e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
1862e795b717SSerge E. Hallyn 		goto other_userns;
1863da9592edSDavid Howells 	if (inode->i_uid == fsuid)
18641da177e4SLinus Torvalds 		return 0;
1865da9592edSDavid Howells 	if (dir->i_uid == fsuid)
18661da177e4SLinus Torvalds 		return 0;
1867e795b717SSerge E. Hallyn 
1868e795b717SSerge E. Hallyn other_userns:
1869e795b717SSerge E. Hallyn 	return !ns_capable(inode_userns(inode), CAP_FOWNER);
18701da177e4SLinus Torvalds }
18711da177e4SLinus Torvalds 
18721da177e4SLinus Torvalds /*
18731da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
18741da177e4SLinus Torvalds  *  whether the type of victim is right.
18751da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
18761da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
18771da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
18781da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
18791da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
18801da177e4SLinus Torvalds  *	a. be owner of dir, or
18811da177e4SLinus Torvalds  *	b. be owner of victim, or
18821da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
18831da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
18841da177e4SLinus Torvalds  *     links pointing to it.
18851da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
18861da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
18871da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
18881da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
18891da177e4SLinus Torvalds  *     nfs_async_unlink().
18901da177e4SLinus Torvalds  */
1891858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
18921da177e4SLinus Torvalds {
18931da177e4SLinus Torvalds 	int error;
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds 	if (!victim->d_inode)
18961da177e4SLinus Torvalds 		return -ENOENT;
18971da177e4SLinus Torvalds 
18981da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1899cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
19001da177e4SLinus Torvalds 
1901f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
19021da177e4SLinus Torvalds 	if (error)
19031da177e4SLinus Torvalds 		return error;
19041da177e4SLinus Torvalds 	if (IS_APPEND(dir))
19051da177e4SLinus Torvalds 		return -EPERM;
19061da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1907f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
19081da177e4SLinus Torvalds 		return -EPERM;
19091da177e4SLinus Torvalds 	if (isdir) {
19101da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
19111da177e4SLinus Torvalds 			return -ENOTDIR;
19121da177e4SLinus Torvalds 		if (IS_ROOT(victim))
19131da177e4SLinus Torvalds 			return -EBUSY;
19141da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
19151da177e4SLinus Torvalds 		return -EISDIR;
19161da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
19171da177e4SLinus Torvalds 		return -ENOENT;
19181da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
19191da177e4SLinus Torvalds 		return -EBUSY;
19201da177e4SLinus Torvalds 	return 0;
19211da177e4SLinus Torvalds }
19221da177e4SLinus Torvalds 
19231da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
19241da177e4SLinus Torvalds  *  dir.
19251da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
19261da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
19271da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
19281da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
19291da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
19301da177e4SLinus Torvalds  */
1931a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
19321da177e4SLinus Torvalds {
19331da177e4SLinus Torvalds 	if (child->d_inode)
19341da177e4SLinus Torvalds 		return -EEXIST;
19351da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
19361da177e4SLinus Torvalds 		return -ENOENT;
1937f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
19381da177e4SLinus Torvalds }
19391da177e4SLinus Torvalds 
19401da177e4SLinus Torvalds /*
19411da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
19421da177e4SLinus Torvalds  */
19431da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
19441da177e4SLinus Torvalds {
19451da177e4SLinus Torvalds 	struct dentry *p;
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 	if (p1 == p2) {
1948f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
19491da177e4SLinus Torvalds 		return NULL;
19501da177e4SLinus Torvalds 	}
19511da177e4SLinus Torvalds 
1952a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19531da177e4SLinus Torvalds 
1954e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1955e2761a11SOGAWA Hirofumi 	if (p) {
1956f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1957f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
19581da177e4SLinus Torvalds 		return p;
19591da177e4SLinus Torvalds 	}
19601da177e4SLinus Torvalds 
1961e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1962e2761a11SOGAWA Hirofumi 	if (p) {
1963f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1964f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19651da177e4SLinus Torvalds 		return p;
19661da177e4SLinus Torvalds 	}
19671da177e4SLinus Torvalds 
1968f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1969f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19701da177e4SLinus Torvalds 	return NULL;
19711da177e4SLinus Torvalds }
19721da177e4SLinus Torvalds 
19731da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
19741da177e4SLinus Torvalds {
19751b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
19761da177e4SLinus Torvalds 	if (p1 != p2) {
19771b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1978a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19791da177e4SLinus Torvalds 	}
19801da177e4SLinus Torvalds }
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
19831da177e4SLinus Torvalds 		struct nameidata *nd)
19841da177e4SLinus Torvalds {
1985a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
19861da177e4SLinus Torvalds 
19871da177e4SLinus Torvalds 	if (error)
19881da177e4SLinus Torvalds 		return error;
19891da177e4SLinus Torvalds 
1990acfa4380SAl Viro 	if (!dir->i_op->create)
19911da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
19921da177e4SLinus Torvalds 	mode &= S_IALLUGO;
19931da177e4SLinus Torvalds 	mode |= S_IFREG;
19941da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
19951da177e4SLinus Torvalds 	if (error)
19961da177e4SLinus Torvalds 		return error;
19971da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1998a74574aaSStephen Smalley 	if (!error)
1999f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
20001da177e4SLinus Torvalds 	return error;
20011da177e4SLinus Torvalds }
20021da177e4SLinus Torvalds 
200373d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
20041da177e4SLinus Torvalds {
20053fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
20061da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
20071da177e4SLinus Torvalds 	int error;
20081da177e4SLinus Torvalds 
2009bcda7652SAl Viro 	/* O_PATH? */
2010bcda7652SAl Viro 	if (!acc_mode)
2011bcda7652SAl Viro 		return 0;
2012bcda7652SAl Viro 
20131da177e4SLinus Torvalds 	if (!inode)
20141da177e4SLinus Torvalds 		return -ENOENT;
20151da177e4SLinus Torvalds 
2016c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2017c8fe8f30SChristoph Hellwig 	case S_IFLNK:
20181da177e4SLinus Torvalds 		return -ELOOP;
2019c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2020c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
20211da177e4SLinus Torvalds 			return -EISDIR;
2022c8fe8f30SChristoph Hellwig 		break;
2023c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2024c8fe8f30SChristoph Hellwig 	case S_IFCHR:
20253fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
20261da177e4SLinus Torvalds 			return -EACCES;
2027c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
2028c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2029c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
20301da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2031c8fe8f30SChristoph Hellwig 		break;
20324a3fd211SDave Hansen 	}
2033b41572e9SDave Hansen 
20343fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2035b41572e9SDave Hansen 	if (error)
2036b41572e9SDave Hansen 		return error;
20376146f0d5SMimi Zohar 
20381da177e4SLinus Torvalds 	/*
20391da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
20401da177e4SLinus Torvalds 	 */
20411da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
20428737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
20437715b521SAl Viro 			return -EPERM;
20441da177e4SLinus Torvalds 		if (flag & O_TRUNC)
20457715b521SAl Viro 			return -EPERM;
20461da177e4SLinus Torvalds 	}
20471da177e4SLinus Torvalds 
20481da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
20492e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
20507715b521SAl Viro 		return -EPERM;
20511da177e4SLinus Torvalds 
2052f3c7691eSJ. Bruce Fields 	return 0;
20537715b521SAl Viro }
20547715b521SAl Viro 
2055e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
20567715b521SAl Viro {
2057e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
20587715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
20597715b521SAl Viro 	int error = get_write_access(inode);
20601da177e4SLinus Torvalds 	if (error)
20617715b521SAl Viro 		return error;
20621da177e4SLinus Torvalds 	/*
20631da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
20641da177e4SLinus Torvalds 	 */
20651da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2066be6d3e56SKentaro Takeda 	if (!error)
2067ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
20681da177e4SLinus Torvalds 	if (!error) {
20697715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2070d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2071e1181ee6SJeff Layton 				    filp);
20721da177e4SLinus Torvalds 	}
20731da177e4SLinus Torvalds 	put_write_access(inode);
2074acd0c935SMimi Zohar 	return error;
20751da177e4SLinus Torvalds }
20761da177e4SLinus Torvalds 
2077d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2078d57999e1SDave Hansen {
20798a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
20808a5e929dSAl Viro 		flag--;
2081d57999e1SDave Hansen 	return flag;
2082d57999e1SDave Hansen }
2083d57999e1SDave Hansen 
208431e6b01fSNick Piggin /*
2085fe2d35ffSAl Viro  * Handle the last step of open()
208631e6b01fSNick Piggin  */
2087fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
2088c3e380b0SAl Viro 			    const struct open_flags *op, const char *pathname)
2089fb1cc555SAl Viro {
2090a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
20916c0d46c4SAl Viro 	struct dentry *dentry;
2092ca344a89SAl Viro 	int open_flag = op->open_flag;
20936c0d46c4SAl Viro 	int will_truncate = open_flag & O_TRUNC;
2094ca344a89SAl Viro 	int want_write = 0;
2095bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2096fb1cc555SAl Viro 	struct file *filp;
209716c2cd71SAl Viro 	int error;
2098fb1cc555SAl Viro 
2099c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2100c3e380b0SAl Viro 	nd->flags |= op->intent;
2101c3e380b0SAl Viro 
21021f36f774SAl Viro 	switch (nd->last_type) {
21031f36f774SAl Viro 	case LAST_DOTDOT:
2104176306f5SNeil Brown 	case LAST_DOT:
2105fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2106fe2d35ffSAl Viro 		if (error)
2107fe2d35ffSAl Viro 			return ERR_PTR(error);
21081f36f774SAl Viro 		/* fallthrough */
21091f36f774SAl Viro 	case LAST_ROOT:
21109f1fafeeSAl Viro 		error = complete_walk(nd);
211116c2cd71SAl Viro 		if (error)
21129f1fafeeSAl Viro 			return ERR_PTR(error);
2113fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2114ca344a89SAl Viro 		if (open_flag & O_CREAT) {
211516c2cd71SAl Viro 			error = -EISDIR;
21161f36f774SAl Viro 			goto exit;
2117fe2d35ffSAl Viro 		}
2118fe2d35ffSAl Viro 		goto ok;
21191f36f774SAl Viro 	case LAST_BIND:
21209f1fafeeSAl Viro 		error = complete_walk(nd);
212116c2cd71SAl Viro 		if (error)
21229f1fafeeSAl Viro 			return ERR_PTR(error);
21231f36f774SAl Viro 		audit_inode(pathname, dir);
21241f36f774SAl Viro 		goto ok;
21251f36f774SAl Viro 	}
2126a2c36b45SAl Viro 
2127ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2128bcda7652SAl Viro 		int symlink_ok = 0;
2129fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2130fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2131bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2132bcda7652SAl Viro 			symlink_ok = 1;
2133fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2134ce57dfc1SAl Viro 		error = walk_component(nd, path, &nd->last, LAST_NORM,
2135ce57dfc1SAl Viro 					!symlink_ok);
2136ce57dfc1SAl Viro 		if (error < 0)
2137fe2d35ffSAl Viro 			return ERR_PTR(error);
2138ce57dfc1SAl Viro 		if (error) /* symlink */
2139fe2d35ffSAl Viro 			return NULL;
2140fe2d35ffSAl Viro 		/* sayonara */
21419f1fafeeSAl Viro 		error = complete_walk(nd);
21429f1fafeeSAl Viro 		if (error)
2143fe2d35ffSAl Viro 			return ERR_PTR(-ECHILD);
2144fe2d35ffSAl Viro 
2145fe2d35ffSAl Viro 		error = -ENOTDIR;
2146fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_DIRECTORY) {
2147ce57dfc1SAl Viro 			if (!nd->inode->i_op->lookup)
2148fe2d35ffSAl Viro 				goto exit;
2149fe2d35ffSAl Viro 		}
2150fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2151fe2d35ffSAl Viro 		goto ok;
2152fe2d35ffSAl Viro 	}
2153fe2d35ffSAl Viro 
2154fe2d35ffSAl Viro 	/* create side of things */
2155a3fbbde7SAl Viro 	/*
2156a3fbbde7SAl Viro 	 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED has been
2157a3fbbde7SAl Viro 	 * cleared when we got to the last component we are about to look up
2158a3fbbde7SAl Viro 	 */
21599f1fafeeSAl Viro 	error = complete_walk(nd);
21609f1fafeeSAl Viro 	if (error)
21619f1fafeeSAl Viro 		return ERR_PTR(error);
2162fe2d35ffSAl Viro 
2163fe2d35ffSAl Viro 	audit_inode(pathname, dir);
216416c2cd71SAl Viro 	error = -EISDIR;
21651f36f774SAl Viro 	/* trailing slashes? */
216631e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
21671f36f774SAl Viro 		goto exit;
21681f36f774SAl Viro 
2169a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2170a1e28038SAl Viro 
21716c0d46c4SAl Viro 	dentry = lookup_hash(nd);
21726c0d46c4SAl Viro 	error = PTR_ERR(dentry);
21736c0d46c4SAl Viro 	if (IS_ERR(dentry)) {
2174fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2175fb1cc555SAl Viro 		goto exit;
2176fb1cc555SAl Viro 	}
2177fb1cc555SAl Viro 
21786c0d46c4SAl Viro 	path->dentry = dentry;
21796c0d46c4SAl Viro 	path->mnt = nd->path.mnt;
21806c0d46c4SAl Viro 
2181fb1cc555SAl Viro 	/* Negative dentry, just create the file */
21826c0d46c4SAl Viro 	if (!dentry->d_inode) {
21836c0d46c4SAl Viro 		int mode = op->mode;
21846c0d46c4SAl Viro 		if (!IS_POSIXACL(dir->d_inode))
21856c0d46c4SAl Viro 			mode &= ~current_umask();
2186fb1cc555SAl Viro 		/*
2187fb1cc555SAl Viro 		 * This write is needed to ensure that a
21886c0d46c4SAl Viro 		 * rw->ro transition does not occur between
2189fb1cc555SAl Viro 		 * the time when the file is created and when
2190fb1cc555SAl Viro 		 * a permanent write count is taken through
2191fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2192fb1cc555SAl Viro 		 */
2193fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2194fb1cc555SAl Viro 		if (error)
2195fb1cc555SAl Viro 			goto exit_mutex_unlock;
2196ca344a89SAl Viro 		want_write = 1;
21979b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2198ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
21996c0d46c4SAl Viro 		will_truncate = 0;
2200bcda7652SAl Viro 		acc_mode = MAY_OPEN;
22016c0d46c4SAl Viro 		error = security_path_mknod(&nd->path, dentry, mode, 0);
22026c0d46c4SAl Viro 		if (error)
22036c0d46c4SAl Viro 			goto exit_mutex_unlock;
22046c0d46c4SAl Viro 		error = vfs_create(dir->d_inode, dentry, mode, nd);
22056c0d46c4SAl Viro 		if (error)
22066c0d46c4SAl Viro 			goto exit_mutex_unlock;
22076c0d46c4SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
22086c0d46c4SAl Viro 		dput(nd->path.dentry);
22096c0d46c4SAl Viro 		nd->path.dentry = dentry;
2210ca344a89SAl Viro 		goto common;
2211fb1cc555SAl Viro 	}
2212fb1cc555SAl Viro 
2213fb1cc555SAl Viro 	/*
2214fb1cc555SAl Viro 	 * It already exists.
2215fb1cc555SAl Viro 	 */
2216fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2217fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2218fb1cc555SAl Viro 
2219fb1cc555SAl Viro 	error = -EEXIST;
2220ca344a89SAl Viro 	if (open_flag & O_EXCL)
2221fb1cc555SAl Viro 		goto exit_dput;
2222fb1cc555SAl Viro 
22239875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
22249875cf80SDavid Howells 	if (error < 0)
2225fb1cc555SAl Viro 		goto exit_dput;
2226fb1cc555SAl Viro 
2227a3fbbde7SAl Viro 	if (error)
2228a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
2229a3fbbde7SAl Viro 
2230fb1cc555SAl Viro 	error = -ENOENT;
2231fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2232fb1cc555SAl Viro 		goto exit_dput;
22339e67f361SAl Viro 
22349e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2235fb1cc555SAl Viro 		return NULL;
2236fb1cc555SAl Viro 
2237fb1cc555SAl Viro 	path_to_nameidata(path, nd);
223831e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2239a3fbbde7SAl Viro 	/* Why this, you ask?  _Now_ we might have grown LOOKUP_JUMPED... */
2240a3fbbde7SAl Viro 	error = complete_walk(nd);
2241a3fbbde7SAl Viro 	if (error)
2242a3fbbde7SAl Viro 		goto exit;
2243fb1cc555SAl Viro 	error = -EISDIR;
224431e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2245fb1cc555SAl Viro 		goto exit;
224667ee3ad2SAl Viro ok:
22476c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
22486c0d46c4SAl Viro 		will_truncate = 0;
22496c0d46c4SAl Viro 
22500f9d1a10SAl Viro 	if (will_truncate) {
22510f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
22520f9d1a10SAl Viro 		if (error)
22530f9d1a10SAl Viro 			goto exit;
2254ca344a89SAl Viro 		want_write = 1;
22550f9d1a10SAl Viro 	}
2256ca344a89SAl Viro common:
2257bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2258ca344a89SAl Viro 	if (error)
22590f9d1a10SAl Viro 		goto exit;
22600f9d1a10SAl Viro 	filp = nameidata_to_filp(nd);
22610f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
22620f9d1a10SAl Viro 		error = ima_file_check(filp, op->acc_mode);
22630f9d1a10SAl Viro 		if (error) {
22640f9d1a10SAl Viro 			fput(filp);
22650f9d1a10SAl Viro 			filp = ERR_PTR(error);
22660f9d1a10SAl Viro 		}
22670f9d1a10SAl Viro 	}
22680f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
22690f9d1a10SAl Viro 		if (will_truncate) {
22700f9d1a10SAl Viro 			error = handle_truncate(filp);
22710f9d1a10SAl Viro 			if (error) {
22720f9d1a10SAl Viro 				fput(filp);
22730f9d1a10SAl Viro 				filp = ERR_PTR(error);
22740f9d1a10SAl Viro 			}
22750f9d1a10SAl Viro 		}
22760f9d1a10SAl Viro 	}
2277ca344a89SAl Viro out:
2278ca344a89SAl Viro 	if (want_write)
22790f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
22800f9d1a10SAl Viro 	path_put(&nd->path);
2281fb1cc555SAl Viro 	return filp;
2282fb1cc555SAl Viro 
2283fb1cc555SAl Viro exit_mutex_unlock:
2284fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2285fb1cc555SAl Viro exit_dput:
2286fb1cc555SAl Viro 	path_put_conditional(path, nd);
2287fb1cc555SAl Viro exit:
2288ca344a89SAl Viro 	filp = ERR_PTR(error);
2289ca344a89SAl Viro 	goto out;
2290fb1cc555SAl Viro }
2291fb1cc555SAl Viro 
229213aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
229373d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
22941da177e4SLinus Torvalds {
2295fe2d35ffSAl Viro 	struct file *base = NULL;
22964a3fd211SDave Hansen 	struct file *filp;
22979850c056SAl Viro 	struct path path;
229813aab428SAl Viro 	int error;
229931e6b01fSNick Piggin 
230031e6b01fSNick Piggin 	filp = get_empty_filp();
230131e6b01fSNick Piggin 	if (!filp)
230231e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
230331e6b01fSNick Piggin 
230447c805dcSAl Viro 	filp->f_flags = op->open_flag;
230573d049a4SAl Viro 	nd->intent.open.file = filp;
230673d049a4SAl Viro 	nd->intent.open.flags = open_to_namei_flags(op->open_flag);
230773d049a4SAl Viro 	nd->intent.open.create_mode = op->mode;
230831e6b01fSNick Piggin 
230973d049a4SAl Viro 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
231031e6b01fSNick Piggin 	if (unlikely(error))
231113aab428SAl Viro 		goto out_filp;
231231e6b01fSNick Piggin 
2313fe2d35ffSAl Viro 	current->total_link_count = 0;
231473d049a4SAl Viro 	error = link_path_walk(pathname, nd);
231531e6b01fSNick Piggin 	if (unlikely(error))
231631e6b01fSNick Piggin 		goto out_filp;
23171da177e4SLinus Torvalds 
231873d049a4SAl Viro 	filp = do_last(nd, &path, op, pathname);
2319806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
23207b9337aaSNick Piggin 		struct path link = path;
2321def4af30SAl Viro 		void *cookie;
2322574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
232373d049a4SAl Viro 			path_put_conditional(&path, nd);
232473d049a4SAl Viro 			path_put(&nd->path);
232540b39136SAl Viro 			filp = ERR_PTR(-ELOOP);
232640b39136SAl Viro 			break;
232740b39136SAl Viro 		}
232873d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
232973d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2330574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
2331c3e380b0SAl Viro 		if (unlikely(error))
2332f1afe9efSAl Viro 			filp = ERR_PTR(error);
2333c3e380b0SAl Viro 		else
233473d049a4SAl Viro 			filp = do_last(nd, &path, op, pathname);
2335574197e0SAl Viro 		put_link(nd, &link, cookie);
2336806b681cSAl Viro 	}
233710fa8e62SAl Viro out:
233873d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
233973d049a4SAl Viro 		path_put(&nd->root);
2340fe2d35ffSAl Viro 	if (base)
2341fe2d35ffSAl Viro 		fput(base);
234273d049a4SAl Viro 	release_open_intent(nd);
234310fa8e62SAl Viro 	return filp;
23441da177e4SLinus Torvalds 
234531e6b01fSNick Piggin out_filp:
234610fa8e62SAl Viro 	filp = ERR_PTR(error);
234710fa8e62SAl Viro 	goto out;
2348de459215SKirill Korotaev }
23491da177e4SLinus Torvalds 
235013aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
235113aab428SAl Viro 		const struct open_flags *op, int flags)
235213aab428SAl Viro {
235373d049a4SAl Viro 	struct nameidata nd;
235413aab428SAl Viro 	struct file *filp;
235513aab428SAl Viro 
235673d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
235713aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
235873d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
235913aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
236073d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
236113aab428SAl Viro 	return filp;
236213aab428SAl Viro }
236313aab428SAl Viro 
236473d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
236573d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
236673d049a4SAl Viro {
236773d049a4SAl Viro 	struct nameidata nd;
236873d049a4SAl Viro 	struct file *file;
236973d049a4SAl Viro 
237073d049a4SAl Viro 	nd.root.mnt = mnt;
237173d049a4SAl Viro 	nd.root.dentry = dentry;
237273d049a4SAl Viro 
237373d049a4SAl Viro 	flags |= LOOKUP_ROOT;
237473d049a4SAl Viro 
2375bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
237673d049a4SAl Viro 		return ERR_PTR(-ELOOP);
237773d049a4SAl Viro 
237873d049a4SAl Viro 	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
237973d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
238073d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags);
238173d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
238273d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
238373d049a4SAl Viro 	return file;
238473d049a4SAl Viro }
238573d049a4SAl Viro 
2386ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
23871da177e4SLinus Torvalds {
2388c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
2389ed75e95dSAl Viro 	struct nameidata nd;
2390ed75e95dSAl Viro 	int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2391ed75e95dSAl Viro 	if (error)
2392ed75e95dSAl Viro 		return ERR_PTR(error);
23931da177e4SLinus Torvalds 
2394c663e5d8SChristoph Hellwig 	/*
2395c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2396c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2397c663e5d8SChristoph Hellwig 	 */
2398ed75e95dSAl Viro 	if (nd.last_type != LAST_NORM)
2399ed75e95dSAl Viro 		goto out;
2400ed75e95dSAl Viro 	nd.flags &= ~LOOKUP_PARENT;
2401ed75e95dSAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2402ed75e95dSAl Viro 	nd.intent.open.flags = O_EXCL;
2403c663e5d8SChristoph Hellwig 
2404c663e5d8SChristoph Hellwig 	/*
2405c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2406c663e5d8SChristoph Hellwig 	 */
2407ed75e95dSAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2408ed75e95dSAl Viro 	dentry = lookup_hash(&nd);
24091da177e4SLinus Torvalds 	if (IS_ERR(dentry))
24101da177e4SLinus Torvalds 		goto fail;
2411c663e5d8SChristoph Hellwig 
2412e9baf6e5SAl Viro 	if (dentry->d_inode)
2413e9baf6e5SAl Viro 		goto eexist;
2414c663e5d8SChristoph Hellwig 	/*
2415c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2416c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2417c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2418c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2419c663e5d8SChristoph Hellwig 	 */
2420ed75e95dSAl Viro 	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
24211da177e4SLinus Torvalds 		dput(dentry);
24221da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2423ed75e95dSAl Viro 		goto fail;
2424e9baf6e5SAl Viro 	}
2425ed75e95dSAl Viro 	*path = nd.path;
2426e9baf6e5SAl Viro 	return dentry;
2427e9baf6e5SAl Viro eexist:
2428e9baf6e5SAl Viro 	dput(dentry);
2429e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
24301da177e4SLinus Torvalds fail:
2431dae6ad8fSAl Viro 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2432ed75e95dSAl Viro out:
2433dae6ad8fSAl Viro 	path_put(&nd.path);
2434ed75e95dSAl Viro 	return dentry;
2435dae6ad8fSAl Viro }
2436dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
2437dae6ad8fSAl Viro 
2438dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2439dae6ad8fSAl Viro {
2440dae6ad8fSAl Viro 	char *tmp = getname(pathname);
2441dae6ad8fSAl Viro 	struct dentry *res;
2442dae6ad8fSAl Viro 	if (IS_ERR(tmp))
2443dae6ad8fSAl Viro 		return ERR_CAST(tmp);
2444dae6ad8fSAl Viro 	res = kern_path_create(dfd, tmp, path, is_dir);
2445dae6ad8fSAl Viro 	putname(tmp);
2446dae6ad8fSAl Viro 	return res;
2447dae6ad8fSAl Viro }
2448dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
2449dae6ad8fSAl Viro 
24501da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
24511da177e4SLinus Torvalds {
2452a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
24531da177e4SLinus Torvalds 
24541da177e4SLinus Torvalds 	if (error)
24551da177e4SLinus Torvalds 		return error;
24561da177e4SLinus Torvalds 
2457e795b717SSerge E. Hallyn 	if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2458e795b717SSerge E. Hallyn 	    !ns_capable(inode_userns(dir), CAP_MKNOD))
24591da177e4SLinus Torvalds 		return -EPERM;
24601da177e4SLinus Torvalds 
2461acfa4380SAl Viro 	if (!dir->i_op->mknod)
24621da177e4SLinus Torvalds 		return -EPERM;
24631da177e4SLinus Torvalds 
246408ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
246508ce5f16SSerge E. Hallyn 	if (error)
246608ce5f16SSerge E. Hallyn 		return error;
246708ce5f16SSerge E. Hallyn 
24681da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
24691da177e4SLinus Torvalds 	if (error)
24701da177e4SLinus Torvalds 		return error;
24711da177e4SLinus Torvalds 
24721da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2473a74574aaSStephen Smalley 	if (!error)
2474f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
24751da177e4SLinus Torvalds 	return error;
24761da177e4SLinus Torvalds }
24771da177e4SLinus Torvalds 
2478463c3197SDave Hansen static int may_mknod(mode_t mode)
2479463c3197SDave Hansen {
2480463c3197SDave Hansen 	switch (mode & S_IFMT) {
2481463c3197SDave Hansen 	case S_IFREG:
2482463c3197SDave Hansen 	case S_IFCHR:
2483463c3197SDave Hansen 	case S_IFBLK:
2484463c3197SDave Hansen 	case S_IFIFO:
2485463c3197SDave Hansen 	case S_IFSOCK:
2486463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2487463c3197SDave Hansen 		return 0;
2488463c3197SDave Hansen 	case S_IFDIR:
2489463c3197SDave Hansen 		return -EPERM;
2490463c3197SDave Hansen 	default:
2491463c3197SDave Hansen 		return -EINVAL;
2492463c3197SDave Hansen 	}
2493463c3197SDave Hansen }
2494463c3197SDave Hansen 
24952e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
24962e4d0924SHeiko Carstens 		unsigned, dev)
24971da177e4SLinus Torvalds {
24981da177e4SLinus Torvalds 	struct dentry *dentry;
2499dae6ad8fSAl Viro 	struct path path;
2500dae6ad8fSAl Viro 	int error;
25011da177e4SLinus Torvalds 
25021da177e4SLinus Torvalds 	if (S_ISDIR(mode))
25031da177e4SLinus Torvalds 		return -EPERM;
25041da177e4SLinus Torvalds 
2505dae6ad8fSAl Viro 	dentry = user_path_create(dfd, filename, &path, 0);
2506dae6ad8fSAl Viro 	if (IS_ERR(dentry))
2507dae6ad8fSAl Viro 		return PTR_ERR(dentry);
25082ad94ae6SAl Viro 
2509dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
2510ce3b0f8dSAl Viro 		mode &= ~current_umask();
2511463c3197SDave Hansen 	error = may_mknod(mode);
2512463c3197SDave Hansen 	if (error)
2513463c3197SDave Hansen 		goto out_dput;
2514dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
2515463c3197SDave Hansen 	if (error)
2516463c3197SDave Hansen 		goto out_dput;
2517dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
2518be6d3e56SKentaro Takeda 	if (error)
2519be6d3e56SKentaro Takeda 		goto out_drop_write;
25201da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
25211da177e4SLinus Torvalds 		case 0: case S_IFREG:
2522dae6ad8fSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
25231da177e4SLinus Torvalds 			break;
25241da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
2525dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
25261da177e4SLinus Torvalds 					new_decode_dev(dev));
25271da177e4SLinus Torvalds 			break;
25281da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
2529dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
25301da177e4SLinus Torvalds 			break;
25311da177e4SLinus Torvalds 	}
2532be6d3e56SKentaro Takeda out_drop_write:
2533dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
2534463c3197SDave Hansen out_dput:
25351da177e4SLinus Torvalds 	dput(dentry);
2536dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2537dae6ad8fSAl Viro 	path_put(&path);
25381da177e4SLinus Torvalds 
25391da177e4SLinus Torvalds 	return error;
25401da177e4SLinus Torvalds }
25411da177e4SLinus Torvalds 
25423480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
25435590ff0dSUlrich Drepper {
25445590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
25455590ff0dSUlrich Drepper }
25465590ff0dSUlrich Drepper 
25471da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
25481da177e4SLinus Torvalds {
2549a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
25501da177e4SLinus Torvalds 
25511da177e4SLinus Torvalds 	if (error)
25521da177e4SLinus Torvalds 		return error;
25531da177e4SLinus Torvalds 
2554acfa4380SAl Viro 	if (!dir->i_op->mkdir)
25551da177e4SLinus Torvalds 		return -EPERM;
25561da177e4SLinus Torvalds 
25571da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
25581da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
25591da177e4SLinus Torvalds 	if (error)
25601da177e4SLinus Torvalds 		return error;
25611da177e4SLinus Torvalds 
25621da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2563a74574aaSStephen Smalley 	if (!error)
2564f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
25651da177e4SLinus Torvalds 	return error;
25661da177e4SLinus Torvalds }
25671da177e4SLinus Torvalds 
25682e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
25691da177e4SLinus Torvalds {
25706902d925SDave Hansen 	struct dentry *dentry;
2571dae6ad8fSAl Viro 	struct path path;
2572dae6ad8fSAl Viro 	int error;
25731da177e4SLinus Torvalds 
2574dae6ad8fSAl Viro 	dentry = user_path_create(dfd, pathname, &path, 1);
25756902d925SDave Hansen 	if (IS_ERR(dentry))
2576dae6ad8fSAl Viro 		return PTR_ERR(dentry);
25776902d925SDave Hansen 
2578dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
2579ce3b0f8dSAl Viro 		mode &= ~current_umask();
2580dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
2581463c3197SDave Hansen 	if (error)
2582463c3197SDave Hansen 		goto out_dput;
2583dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
2584be6d3e56SKentaro Takeda 	if (error)
2585be6d3e56SKentaro Takeda 		goto out_drop_write;
2586dae6ad8fSAl Viro 	error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
2587be6d3e56SKentaro Takeda out_drop_write:
2588dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
2589463c3197SDave Hansen out_dput:
25901da177e4SLinus Torvalds 	dput(dentry);
2591dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2592dae6ad8fSAl Viro 	path_put(&path);
25931da177e4SLinus Torvalds 	return error;
25941da177e4SLinus Torvalds }
25951da177e4SLinus Torvalds 
25963cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
25975590ff0dSUlrich Drepper {
25985590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
25995590ff0dSUlrich Drepper }
26005590ff0dSUlrich Drepper 
26011da177e4SLinus Torvalds /*
2602a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
2603a71905f0SSage Weil  * should have a usage count of 2 if we're the only user of this
2604a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
2605a71905f0SSage Weil  * then we drop the dentry now.
26061da177e4SLinus Torvalds  *
26071da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
26081da177e4SLinus Torvalds  * do a
26091da177e4SLinus Torvalds  *
26101da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
26111da177e4SLinus Torvalds  *		return -EBUSY;
26121da177e4SLinus Torvalds  *
26131da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
26141da177e4SLinus Torvalds  * that is still in use by something else..
26151da177e4SLinus Torvalds  */
26161da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
26171da177e4SLinus Torvalds {
26181da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
26191da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
262064252c75SSage Weil 	if (dentry->d_count == 1)
26211da177e4SLinus Torvalds 		__d_drop(dentry);
26221da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
26231da177e4SLinus Torvalds }
26241da177e4SLinus Torvalds 
26251da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
26261da177e4SLinus Torvalds {
26271da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
26281da177e4SLinus Torvalds 
26291da177e4SLinus Torvalds 	if (error)
26301da177e4SLinus Torvalds 		return error;
26311da177e4SLinus Torvalds 
2632acfa4380SAl Viro 	if (!dir->i_op->rmdir)
26331da177e4SLinus Torvalds 		return -EPERM;
26341da177e4SLinus Torvalds 
26351d2ef590SAl Viro 	dget(dentry);
26361b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
2637912dbc15SSage Weil 
26381da177e4SLinus Torvalds 	error = -EBUSY;
2639912dbc15SSage Weil 	if (d_mountpoint(dentry))
2640912dbc15SSage Weil 		goto out;
2641912dbc15SSage Weil 
26421da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
2643912dbc15SSage Weil 	if (error)
2644912dbc15SSage Weil 		goto out;
2645912dbc15SSage Weil 
26463cebde24SSage Weil 	shrink_dcache_parent(dentry);
26471da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
2648912dbc15SSage Weil 	if (error)
2649912dbc15SSage Weil 		goto out;
2650912dbc15SSage Weil 
26511da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
2652d83c49f3SAl Viro 	dont_mount(dentry);
26531da177e4SLinus Torvalds 
2654912dbc15SSage Weil out:
2655912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
26561d2ef590SAl Viro 	dput(dentry);
2657912dbc15SSage Weil 	if (!error)
2658912dbc15SSage Weil 		d_delete(dentry);
26591da177e4SLinus Torvalds 	return error;
26601da177e4SLinus Torvalds }
26611da177e4SLinus Torvalds 
26625590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
26631da177e4SLinus Torvalds {
26641da177e4SLinus Torvalds 	int error = 0;
26651da177e4SLinus Torvalds 	char * name;
26661da177e4SLinus Torvalds 	struct dentry *dentry;
26671da177e4SLinus Torvalds 	struct nameidata nd;
26681da177e4SLinus Torvalds 
26692ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
26701da177e4SLinus Torvalds 	if (error)
26712ad94ae6SAl Viro 		return error;
26721da177e4SLinus Torvalds 
26731da177e4SLinus Torvalds 	switch(nd.last_type) {
26741da177e4SLinus Torvalds 	case LAST_DOTDOT:
26751da177e4SLinus Torvalds 		error = -ENOTEMPTY;
26761da177e4SLinus Torvalds 		goto exit1;
26771da177e4SLinus Torvalds 	case LAST_DOT:
26781da177e4SLinus Torvalds 		error = -EINVAL;
26791da177e4SLinus Torvalds 		goto exit1;
26801da177e4SLinus Torvalds 	case LAST_ROOT:
26811da177e4SLinus Torvalds 		error = -EBUSY;
26821da177e4SLinus Torvalds 		goto exit1;
26831da177e4SLinus Torvalds 	}
26840612d9fbSOGAWA Hirofumi 
26850612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
26860612d9fbSOGAWA Hirofumi 
26874ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
268849705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
26891da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
26906902d925SDave Hansen 	if (IS_ERR(dentry))
26916902d925SDave Hansen 		goto exit2;
2692e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
2693e6bc45d6STheodore Ts'o 		error = -ENOENT;
2694e6bc45d6STheodore Ts'o 		goto exit3;
2695e6bc45d6STheodore Ts'o 	}
26960622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
26970622753bSDave Hansen 	if (error)
26980622753bSDave Hansen 		goto exit3;
2699be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2700be6d3e56SKentaro Takeda 	if (error)
2701be6d3e56SKentaro Takeda 		goto exit4;
27024ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2703be6d3e56SKentaro Takeda exit4:
27040622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
27050622753bSDave Hansen exit3:
27061da177e4SLinus Torvalds 	dput(dentry);
27076902d925SDave Hansen exit2:
27084ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27091da177e4SLinus Torvalds exit1:
27101d957f9bSJan Blunck 	path_put(&nd.path);
27111da177e4SLinus Torvalds 	putname(name);
27121da177e4SLinus Torvalds 	return error;
27131da177e4SLinus Torvalds }
27141da177e4SLinus Torvalds 
27153cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
27165590ff0dSUlrich Drepper {
27175590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
27185590ff0dSUlrich Drepper }
27195590ff0dSUlrich Drepper 
27201da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
27211da177e4SLinus Torvalds {
27221da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
27231da177e4SLinus Torvalds 
27241da177e4SLinus Torvalds 	if (error)
27251da177e4SLinus Torvalds 		return error;
27261da177e4SLinus Torvalds 
2727acfa4380SAl Viro 	if (!dir->i_op->unlink)
27281da177e4SLinus Torvalds 		return -EPERM;
27291da177e4SLinus Torvalds 
27301b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
27311da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
27321da177e4SLinus Torvalds 		error = -EBUSY;
27331da177e4SLinus Torvalds 	else {
27341da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2735bec1052eSAl Viro 		if (!error) {
27361da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2737bec1052eSAl Viro 			if (!error)
2738d83c49f3SAl Viro 				dont_mount(dentry);
2739bec1052eSAl Viro 		}
27401da177e4SLinus Torvalds 	}
27411b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
27421da177e4SLinus Torvalds 
27431da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
27441da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2745ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
27461da177e4SLinus Torvalds 		d_delete(dentry);
27471da177e4SLinus Torvalds 	}
27480eeca283SRobert Love 
27491da177e4SLinus Torvalds 	return error;
27501da177e4SLinus Torvalds }
27511da177e4SLinus Torvalds 
27521da177e4SLinus Torvalds /*
27531da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
27541b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
27551da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
27561da177e4SLinus Torvalds  * while waiting on the I/O.
27571da177e4SLinus Torvalds  */
27585590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
27591da177e4SLinus Torvalds {
27602ad94ae6SAl Viro 	int error;
27611da177e4SLinus Torvalds 	char *name;
27621da177e4SLinus Torvalds 	struct dentry *dentry;
27631da177e4SLinus Torvalds 	struct nameidata nd;
27641da177e4SLinus Torvalds 	struct inode *inode = NULL;
27651da177e4SLinus Torvalds 
27662ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
27671da177e4SLinus Torvalds 	if (error)
27682ad94ae6SAl Viro 		return error;
27692ad94ae6SAl Viro 
27701da177e4SLinus Torvalds 	error = -EISDIR;
27711da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
27721da177e4SLinus Torvalds 		goto exit1;
27730612d9fbSOGAWA Hirofumi 
27740612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
27750612d9fbSOGAWA Hirofumi 
27764ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
277749705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
27781da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27791da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
27801da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
278150338b88STörök Edwin 		if (nd.last.name[nd.last.len])
278250338b88STörök Edwin 			goto slashes;
27831da177e4SLinus Torvalds 		inode = dentry->d_inode;
278450338b88STörök Edwin 		if (!inode)
2785e6bc45d6STheodore Ts'o 			goto slashes;
27867de9c6eeSAl Viro 		ihold(inode);
27870622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
27880622753bSDave Hansen 		if (error)
27890622753bSDave Hansen 			goto exit2;
2790be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2791be6d3e56SKentaro Takeda 		if (error)
2792be6d3e56SKentaro Takeda 			goto exit3;
27934ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2794be6d3e56SKentaro Takeda exit3:
27950622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
27961da177e4SLinus Torvalds 	exit2:
27971da177e4SLinus Torvalds 		dput(dentry);
27981da177e4SLinus Torvalds 	}
27994ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
28001da177e4SLinus Torvalds 	if (inode)
28011da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
28021da177e4SLinus Torvalds exit1:
28031d957f9bSJan Blunck 	path_put(&nd.path);
28041da177e4SLinus Torvalds 	putname(name);
28051da177e4SLinus Torvalds 	return error;
28061da177e4SLinus Torvalds 
28071da177e4SLinus Torvalds slashes:
28081da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
28091da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
28101da177e4SLinus Torvalds 	goto exit2;
28111da177e4SLinus Torvalds }
28121da177e4SLinus Torvalds 
28132e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
28145590ff0dSUlrich Drepper {
28155590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
28165590ff0dSUlrich Drepper 		return -EINVAL;
28175590ff0dSUlrich Drepper 
28185590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
28195590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
28205590ff0dSUlrich Drepper 
28215590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
28225590ff0dSUlrich Drepper }
28235590ff0dSUlrich Drepper 
28243480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
28255590ff0dSUlrich Drepper {
28265590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
28275590ff0dSUlrich Drepper }
28285590ff0dSUlrich Drepper 
2829db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
28301da177e4SLinus Torvalds {
2831a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
28321da177e4SLinus Torvalds 
28331da177e4SLinus Torvalds 	if (error)
28341da177e4SLinus Torvalds 		return error;
28351da177e4SLinus Torvalds 
2836acfa4380SAl Viro 	if (!dir->i_op->symlink)
28371da177e4SLinus Torvalds 		return -EPERM;
28381da177e4SLinus Torvalds 
28391da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
28401da177e4SLinus Torvalds 	if (error)
28411da177e4SLinus Torvalds 		return error;
28421da177e4SLinus Torvalds 
28431da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2844a74574aaSStephen Smalley 	if (!error)
2845f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
28461da177e4SLinus Torvalds 	return error;
28471da177e4SLinus Torvalds }
28481da177e4SLinus Torvalds 
28492e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
28502e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
28511da177e4SLinus Torvalds {
28522ad94ae6SAl Viro 	int error;
28531da177e4SLinus Torvalds 	char *from;
28546902d925SDave Hansen 	struct dentry *dentry;
2855dae6ad8fSAl Viro 	struct path path;
28561da177e4SLinus Torvalds 
28571da177e4SLinus Torvalds 	from = getname(oldname);
28581da177e4SLinus Torvalds 	if (IS_ERR(from))
28591da177e4SLinus Torvalds 		return PTR_ERR(from);
28602ad94ae6SAl Viro 
2861dae6ad8fSAl Viro 	dentry = user_path_create(newdfd, newname, &path, 0);
28621da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
28636902d925SDave Hansen 	if (IS_ERR(dentry))
2864dae6ad8fSAl Viro 		goto out_putname;
28656902d925SDave Hansen 
2866dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
286775c3f29dSDave Hansen 	if (error)
286875c3f29dSDave Hansen 		goto out_dput;
2869dae6ad8fSAl Viro 	error = security_path_symlink(&path, dentry, from);
2870be6d3e56SKentaro Takeda 	if (error)
2871be6d3e56SKentaro Takeda 		goto out_drop_write;
2872dae6ad8fSAl Viro 	error = vfs_symlink(path.dentry->d_inode, dentry, from);
2873be6d3e56SKentaro Takeda out_drop_write:
2874dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
287575c3f29dSDave Hansen out_dput:
28761da177e4SLinus Torvalds 	dput(dentry);
2877dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2878dae6ad8fSAl Viro 	path_put(&path);
28796902d925SDave Hansen out_putname:
28801da177e4SLinus Torvalds 	putname(from);
28811da177e4SLinus Torvalds 	return error;
28821da177e4SLinus Torvalds }
28831da177e4SLinus Torvalds 
28843480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
28855590ff0dSUlrich Drepper {
28865590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
28875590ff0dSUlrich Drepper }
28885590ff0dSUlrich Drepper 
28891da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
28901da177e4SLinus Torvalds {
28911da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
28921da177e4SLinus Torvalds 	int error;
28931da177e4SLinus Torvalds 
28941da177e4SLinus Torvalds 	if (!inode)
28951da177e4SLinus Torvalds 		return -ENOENT;
28961da177e4SLinus Torvalds 
2897a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
28981da177e4SLinus Torvalds 	if (error)
28991da177e4SLinus Torvalds 		return error;
29001da177e4SLinus Torvalds 
29011da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
29021da177e4SLinus Torvalds 		return -EXDEV;
29031da177e4SLinus Torvalds 
29041da177e4SLinus Torvalds 	/*
29051da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
29061da177e4SLinus Torvalds 	 */
29071da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
29081da177e4SLinus Torvalds 		return -EPERM;
2909acfa4380SAl Viro 	if (!dir->i_op->link)
29101da177e4SLinus Torvalds 		return -EPERM;
29117e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
29121da177e4SLinus Torvalds 		return -EPERM;
29131da177e4SLinus Torvalds 
29141da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
29151da177e4SLinus Torvalds 	if (error)
29161da177e4SLinus Torvalds 		return error;
29171da177e4SLinus Torvalds 
29187e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
2919aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
2920aae8a97dSAneesh Kumar K.V 	if (inode->i_nlink == 0)
2921aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
2922aae8a97dSAneesh Kumar K.V 	else
29231da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
29247e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2925e31e14ecSStephen Smalley 	if (!error)
29267e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
29271da177e4SLinus Torvalds 	return error;
29281da177e4SLinus Torvalds }
29291da177e4SLinus Torvalds 
29301da177e4SLinus Torvalds /*
29311da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
29321da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
29331da177e4SLinus Torvalds  * newname.  --KAB
29341da177e4SLinus Torvalds  *
29351da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
29361da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
29371da177e4SLinus Torvalds  * and other special files.  --ADM
29381da177e4SLinus Torvalds  */
29392e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
29402e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
29411da177e4SLinus Torvalds {
29421da177e4SLinus Torvalds 	struct dentry *new_dentry;
2943dae6ad8fSAl Viro 	struct path old_path, new_path;
294411a7b371SAneesh Kumar K.V 	int how = 0;
29451da177e4SLinus Torvalds 	int error;
29461da177e4SLinus Torvalds 
294711a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
2948c04030e1SUlrich Drepper 		return -EINVAL;
294911a7b371SAneesh Kumar K.V 	/*
295011a7b371SAneesh Kumar K.V 	 * To use null names we require CAP_DAC_READ_SEARCH
295111a7b371SAneesh Kumar K.V 	 * This ensures that not everyone will be able to create
295211a7b371SAneesh Kumar K.V 	 * handlink using the passed filedescriptor.
295311a7b371SAneesh Kumar K.V 	 */
295411a7b371SAneesh Kumar K.V 	if (flags & AT_EMPTY_PATH) {
295511a7b371SAneesh Kumar K.V 		if (!capable(CAP_DAC_READ_SEARCH))
295611a7b371SAneesh Kumar K.V 			return -ENOENT;
295711a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
295811a7b371SAneesh Kumar K.V 	}
2959c04030e1SUlrich Drepper 
296011a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
296111a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
296211a7b371SAneesh Kumar K.V 
296311a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
29641da177e4SLinus Torvalds 	if (error)
29652ad94ae6SAl Viro 		return error;
29662ad94ae6SAl Viro 
2967dae6ad8fSAl Viro 	new_dentry = user_path_create(newdfd, newname, &new_path, 0);
29681da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
29696902d925SDave Hansen 	if (IS_ERR(new_dentry))
2970dae6ad8fSAl Viro 		goto out;
2971dae6ad8fSAl Viro 
2972dae6ad8fSAl Viro 	error = -EXDEV;
2973dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
2974dae6ad8fSAl Viro 		goto out_dput;
2975dae6ad8fSAl Viro 	error = mnt_want_write(new_path.mnt);
297675c3f29dSDave Hansen 	if (error)
297775c3f29dSDave Hansen 		goto out_dput;
2978dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
2979be6d3e56SKentaro Takeda 	if (error)
2980be6d3e56SKentaro Takeda 		goto out_drop_write;
2981dae6ad8fSAl Viro 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
2982be6d3e56SKentaro Takeda out_drop_write:
2983dae6ad8fSAl Viro 	mnt_drop_write(new_path.mnt);
298475c3f29dSDave Hansen out_dput:
29851da177e4SLinus Torvalds 	dput(new_dentry);
2986dae6ad8fSAl Viro 	mutex_unlock(&new_path.dentry->d_inode->i_mutex);
2987dae6ad8fSAl Viro 	path_put(&new_path);
29881da177e4SLinus Torvalds out:
29892d8f3038SAl Viro 	path_put(&old_path);
29901da177e4SLinus Torvalds 
29911da177e4SLinus Torvalds 	return error;
29921da177e4SLinus Torvalds }
29931da177e4SLinus Torvalds 
29943480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
29955590ff0dSUlrich Drepper {
2996c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
29975590ff0dSUlrich Drepper }
29985590ff0dSUlrich Drepper 
29991da177e4SLinus Torvalds /*
30001da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
30011da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
30021da177e4SLinus Torvalds  * Problems:
30031da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
30041da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
30051da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
3006a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
30071da177e4SLinus Torvalds  *	   story.
30081da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
30091b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
30101da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
30111da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
3012a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
30131da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
30141da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
30151da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
3016a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
30171da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
30181da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
30191da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
3020e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
30211b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
30221da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3023c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
30241da177e4SLinus Torvalds  *	   locking].
30251da177e4SLinus Torvalds  */
302675c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
30271da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
30281da177e4SLinus Torvalds {
30291da177e4SLinus Torvalds 	int error = 0;
30309055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
30311da177e4SLinus Torvalds 
30321da177e4SLinus Torvalds 	/*
30331da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
30341da177e4SLinus Torvalds 	 * we'll need to flip '..'.
30351da177e4SLinus Torvalds 	 */
30361da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3037f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
30381da177e4SLinus Torvalds 		if (error)
30391da177e4SLinus Torvalds 			return error;
30401da177e4SLinus Torvalds 	}
30411da177e4SLinus Torvalds 
30421da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30431da177e4SLinus Torvalds 	if (error)
30441da177e4SLinus Torvalds 		return error;
30451da177e4SLinus Torvalds 
30461d2ef590SAl Viro 	dget(new_dentry);
3047d83c49f3SAl Viro 	if (target)
30481b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
30499055cba7SSage Weil 
30501da177e4SLinus Torvalds 	error = -EBUSY;
30519055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
30529055cba7SSage Weil 		goto out;
30539055cba7SSage Weil 
30543cebde24SSage Weil 	if (target)
30553cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
30561da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
30579055cba7SSage Weil 	if (error)
30589055cba7SSage Weil 		goto out;
30599055cba7SSage Weil 
30601da177e4SLinus Torvalds 	if (target) {
30611da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
3062d83c49f3SAl Viro 		dont_mount(new_dentry);
3063d83c49f3SAl Viro 	}
30649055cba7SSage Weil out:
30659055cba7SSage Weil 	if (target)
30661b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
30671d2ef590SAl Viro 	dput(new_dentry);
3068e31e14ecSStephen Smalley 	if (!error)
3069349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30701da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
30711da177e4SLinus Torvalds 	return error;
30721da177e4SLinus Torvalds }
30731da177e4SLinus Torvalds 
307475c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
30751da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
30761da177e4SLinus Torvalds {
307751892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
30781da177e4SLinus Torvalds 	int error;
30791da177e4SLinus Torvalds 
30801da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30811da177e4SLinus Torvalds 	if (error)
30821da177e4SLinus Torvalds 		return error;
30831da177e4SLinus Torvalds 
30841da177e4SLinus Torvalds 	dget(new_dentry);
30851da177e4SLinus Torvalds 	if (target)
30861b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
308751892bbbSSage Weil 
30881da177e4SLinus Torvalds 	error = -EBUSY;
308951892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
309051892bbbSSage Weil 		goto out;
309151892bbbSSage Weil 
30921da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
309351892bbbSSage Weil 	if (error)
309451892bbbSSage Weil 		goto out;
309551892bbbSSage Weil 
3096bec1052eSAl Viro 	if (target)
3097d83c49f3SAl Viro 		dont_mount(new_dentry);
3098349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30991da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
310051892bbbSSage Weil out:
31011da177e4SLinus Torvalds 	if (target)
31021b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
31031da177e4SLinus Torvalds 	dput(new_dentry);
31041da177e4SLinus Torvalds 	return error;
31051da177e4SLinus Torvalds }
31061da177e4SLinus Torvalds 
31071da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
31081da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
31091da177e4SLinus Torvalds {
31101da177e4SLinus Torvalds 	int error;
31111da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
311259b0df21SEric Paris 	const unsigned char *old_name;
31131da177e4SLinus Torvalds 
31141da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
31151da177e4SLinus Torvalds  		return 0;
31161da177e4SLinus Torvalds 
31171da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
31181da177e4SLinus Torvalds 	if (error)
31191da177e4SLinus Torvalds 		return error;
31201da177e4SLinus Torvalds 
31211da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3122a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
31231da177e4SLinus Torvalds 	else
31241da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
31251da177e4SLinus Torvalds 	if (error)
31261da177e4SLinus Torvalds 		return error;
31271da177e4SLinus Torvalds 
3128acfa4380SAl Viro 	if (!old_dir->i_op->rename)
31291da177e4SLinus Torvalds 		return -EPERM;
31301da177e4SLinus Torvalds 
31310eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
31320eeca283SRobert Love 
31331da177e4SLinus Torvalds 	if (is_dir)
31341da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
31351da177e4SLinus Torvalds 	else
31361da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3137123df294SAl Viro 	if (!error)
3138123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
31395a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
31400eeca283SRobert Love 	fsnotify_oldname_free(old_name);
31410eeca283SRobert Love 
31421da177e4SLinus Torvalds 	return error;
31431da177e4SLinus Torvalds }
31441da177e4SLinus Torvalds 
31452e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
31462e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
31471da177e4SLinus Torvalds {
31481da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
31491da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
31501da177e4SLinus Torvalds 	struct dentry *trap;
31511da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
31522ad94ae6SAl Viro 	char *from;
31532ad94ae6SAl Viro 	char *to;
31542ad94ae6SAl Viro 	int error;
31551da177e4SLinus Torvalds 
31562ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
31571da177e4SLinus Torvalds 	if (error)
31581da177e4SLinus Torvalds 		goto exit;
31591da177e4SLinus Torvalds 
31602ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
31611da177e4SLinus Torvalds 	if (error)
31621da177e4SLinus Torvalds 		goto exit1;
31631da177e4SLinus Torvalds 
31641da177e4SLinus Torvalds 	error = -EXDEV;
31654ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
31661da177e4SLinus Torvalds 		goto exit2;
31671da177e4SLinus Torvalds 
31684ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
31691da177e4SLinus Torvalds 	error = -EBUSY;
31701da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
31711da177e4SLinus Torvalds 		goto exit2;
31721da177e4SLinus Torvalds 
31734ac91378SJan Blunck 	new_dir = newnd.path.dentry;
31741da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
31751da177e4SLinus Torvalds 		goto exit2;
31761da177e4SLinus Torvalds 
31770612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
31780612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
31794e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
31800612d9fbSOGAWA Hirofumi 
31811da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
31821da177e4SLinus Torvalds 
318349705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
31841da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
31851da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
31861da177e4SLinus Torvalds 		goto exit3;
31871da177e4SLinus Torvalds 	/* source must exist */
31881da177e4SLinus Torvalds 	error = -ENOENT;
31891da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
31901da177e4SLinus Torvalds 		goto exit4;
31911da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
31921da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
31931da177e4SLinus Torvalds 		error = -ENOTDIR;
31941da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
31951da177e4SLinus Torvalds 			goto exit4;
31961da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
31971da177e4SLinus Torvalds 			goto exit4;
31981da177e4SLinus Torvalds 	}
31991da177e4SLinus Torvalds 	/* source should not be ancestor of target */
32001da177e4SLinus Torvalds 	error = -EINVAL;
32011da177e4SLinus Torvalds 	if (old_dentry == trap)
32021da177e4SLinus Torvalds 		goto exit4;
320349705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
32041da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
32051da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
32061da177e4SLinus Torvalds 		goto exit4;
32071da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
32081da177e4SLinus Torvalds 	error = -ENOTEMPTY;
32091da177e4SLinus Torvalds 	if (new_dentry == trap)
32101da177e4SLinus Torvalds 		goto exit5;
32111da177e4SLinus Torvalds 
32129079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
32139079b1ebSDave Hansen 	if (error)
32149079b1ebSDave Hansen 		goto exit5;
3215be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3216be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3217be6d3e56SKentaro Takeda 	if (error)
3218be6d3e56SKentaro Takeda 		goto exit6;
32191da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
32201da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3221be6d3e56SKentaro Takeda exit6:
32229079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
32231da177e4SLinus Torvalds exit5:
32241da177e4SLinus Torvalds 	dput(new_dentry);
32251da177e4SLinus Torvalds exit4:
32261da177e4SLinus Torvalds 	dput(old_dentry);
32271da177e4SLinus Torvalds exit3:
32281da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
32291da177e4SLinus Torvalds exit2:
32301d957f9bSJan Blunck 	path_put(&newnd.path);
32312ad94ae6SAl Viro 	putname(to);
32321da177e4SLinus Torvalds exit1:
32331d957f9bSJan Blunck 	path_put(&oldnd.path);
32341da177e4SLinus Torvalds 	putname(from);
32352ad94ae6SAl Viro exit:
32361da177e4SLinus Torvalds 	return error;
32371da177e4SLinus Torvalds }
32381da177e4SLinus Torvalds 
3239a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
32405590ff0dSUlrich Drepper {
32415590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
32425590ff0dSUlrich Drepper }
32435590ff0dSUlrich Drepper 
32441da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
32451da177e4SLinus Torvalds {
32461da177e4SLinus Torvalds 	int len;
32471da177e4SLinus Torvalds 
32481da177e4SLinus Torvalds 	len = PTR_ERR(link);
32491da177e4SLinus Torvalds 	if (IS_ERR(link))
32501da177e4SLinus Torvalds 		goto out;
32511da177e4SLinus Torvalds 
32521da177e4SLinus Torvalds 	len = strlen(link);
32531da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
32541da177e4SLinus Torvalds 		len = buflen;
32551da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
32561da177e4SLinus Torvalds 		len = -EFAULT;
32571da177e4SLinus Torvalds out:
32581da177e4SLinus Torvalds 	return len;
32591da177e4SLinus Torvalds }
32601da177e4SLinus Torvalds 
32611da177e4SLinus Torvalds /*
32621da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
32631da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
32641da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
32651da177e4SLinus Torvalds  */
32661da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32671da177e4SLinus Torvalds {
32681da177e4SLinus Torvalds 	struct nameidata nd;
3269cc314eefSLinus Torvalds 	void *cookie;
3270694a1764SMarcin Slusarz 	int res;
3271cc314eefSLinus Torvalds 
32721da177e4SLinus Torvalds 	nd.depth = 0;
3273cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3274694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3275694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3276694a1764SMarcin Slusarz 
3277694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
32781da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3279cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3280694a1764SMarcin Slusarz 	return res;
32811da177e4SLinus Torvalds }
32821da177e4SLinus Torvalds 
32831da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
32841da177e4SLinus Torvalds {
32851da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
32861da177e4SLinus Torvalds }
32871da177e4SLinus Torvalds 
32881da177e4SLinus Torvalds /* get the link contents into pagecache */
32891da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
32901da177e4SLinus Torvalds {
3291ebd09abbSDuane Griffin 	char *kaddr;
32921da177e4SLinus Torvalds 	struct page *page;
32931da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3294090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
32951da177e4SLinus Torvalds 	if (IS_ERR(page))
32966fe6900eSNick Piggin 		return (char*)page;
32971da177e4SLinus Torvalds 	*ppage = page;
3298ebd09abbSDuane Griffin 	kaddr = kmap(page);
3299ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3300ebd09abbSDuane Griffin 	return kaddr;
33011da177e4SLinus Torvalds }
33021da177e4SLinus Torvalds 
33031da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
33041da177e4SLinus Torvalds {
33051da177e4SLinus Torvalds 	struct page *page = NULL;
33061da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
33071da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
33081da177e4SLinus Torvalds 	if (page) {
33091da177e4SLinus Torvalds 		kunmap(page);
33101da177e4SLinus Torvalds 		page_cache_release(page);
33111da177e4SLinus Torvalds 	}
33121da177e4SLinus Torvalds 	return res;
33131da177e4SLinus Torvalds }
33141da177e4SLinus Torvalds 
3315cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
33161da177e4SLinus Torvalds {
3317cc314eefSLinus Torvalds 	struct page *page = NULL;
33181da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3319cc314eefSLinus Torvalds 	return page;
33201da177e4SLinus Torvalds }
33211da177e4SLinus Torvalds 
3322cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
33231da177e4SLinus Torvalds {
3324cc314eefSLinus Torvalds 	struct page *page = cookie;
3325cc314eefSLinus Torvalds 
3326cc314eefSLinus Torvalds 	if (page) {
33271da177e4SLinus Torvalds 		kunmap(page);
33281da177e4SLinus Torvalds 		page_cache_release(page);
33291da177e4SLinus Torvalds 	}
33301da177e4SLinus Torvalds }
33311da177e4SLinus Torvalds 
333254566b2cSNick Piggin /*
333354566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
333454566b2cSNick Piggin  */
333554566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
33361da177e4SLinus Torvalds {
33371da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
33380adb25d2SKirill Korotaev 	struct page *page;
3339afddba49SNick Piggin 	void *fsdata;
3340beb497abSDmitriy Monakhov 	int err;
33411da177e4SLinus Torvalds 	char *kaddr;
334254566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
334354566b2cSNick Piggin 	if (nofs)
334454566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
33451da177e4SLinus Torvalds 
33467e53cac4SNeilBrown retry:
3347afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
334854566b2cSNick Piggin 				flags, &page, &fsdata);
33491da177e4SLinus Torvalds 	if (err)
3350afddba49SNick Piggin 		goto fail;
3351afddba49SNick Piggin 
33521da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
33531da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
33541da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
3355afddba49SNick Piggin 
3356afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3357afddba49SNick Piggin 							page, fsdata);
33581da177e4SLinus Torvalds 	if (err < 0)
33591da177e4SLinus Torvalds 		goto fail;
3360afddba49SNick Piggin 	if (err < len-1)
3361afddba49SNick Piggin 		goto retry;
3362afddba49SNick Piggin 
33631da177e4SLinus Torvalds 	mark_inode_dirty(inode);
33641da177e4SLinus Torvalds 	return 0;
33651da177e4SLinus Torvalds fail:
33661da177e4SLinus Torvalds 	return err;
33671da177e4SLinus Torvalds }
33681da177e4SLinus Torvalds 
33690adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
33700adb25d2SKirill Korotaev {
33710adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
337254566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
33730adb25d2SKirill Korotaev }
33740adb25d2SKirill Korotaev 
337592e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
33761da177e4SLinus Torvalds 	.readlink	= generic_readlink,
33771da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
33781da177e4SLinus Torvalds 	.put_link	= page_put_link,
33791da177e4SLinus Torvalds };
33801da177e4SLinus Torvalds 
33812d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3382cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
33831da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
33841da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
33851da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
33861da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
33871da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
33881da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
33891da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
33901da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
33911da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
33920adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
33931da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
33941da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3395d1811465SAl Viro EXPORT_SYMBOL(kern_path);
339616f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3397f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
33981da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
33991da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
34001da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
34011da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
34021da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
34031da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
34041da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
34051da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
34061da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
34071da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
34081da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
34091da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
34101da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
34111da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3412