xref: /openbmc/linux/fs/namei.c (revision 8208a22b)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/namei.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  * Some corrections by tytso.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
121da177e4SLinus Torvalds  * lookup logic.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/init.h>
181da177e4SLinus Torvalds #include <linux/module.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/fs.h>
211da177e4SLinus Torvalds #include <linux/namei.h>
221da177e4SLinus Torvalds #include <linux/pagemap.h>
230eeca283SRobert Love #include <linux/fsnotify.h>
241da177e4SLinus Torvalds #include <linux/personality.h>
251da177e4SLinus Torvalds #include <linux/security.h>
266146f0d5SMimi Zohar #include <linux/ima.h>
271da177e4SLinus Torvalds #include <linux/syscalls.h>
281da177e4SLinus Torvalds #include <linux/mount.h>
291da177e4SLinus Torvalds #include <linux/audit.h>
3016f7e0feSRandy Dunlap #include <linux/capability.h>
31834f2a4aSTrond Myklebust #include <linux/file.h>
325590ff0dSUlrich Drepper #include <linux/fcntl.h>
3308ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h>
345ad4e53bSAl Viro #include <linux/fs_struct.h>
35e77819e5SLinus Torvalds #include <linux/posix_acl.h>
361da177e4SLinus Torvalds #include <asm/uaccess.h>
371da177e4SLinus Torvalds 
38e81e3f4dSEric Paris #include "internal.h"
39e81e3f4dSEric Paris 
401da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer]
411da177e4SLinus Torvalds  * Fundamental changes in the pathname lookup mechanisms (namei)
421da177e4SLinus Torvalds  * were necessary because of omirr.  The reason is that omirr needs
431da177e4SLinus Torvalds  * to know the _real_ pathname, not the user-supplied one, in case
441da177e4SLinus Torvalds  * of symlinks (and also when transname replacements occur).
451da177e4SLinus Torvalds  *
461da177e4SLinus Torvalds  * The new code replaces the old recursive symlink resolution with
471da177e4SLinus Torvalds  * an iterative one (in case of non-nested symlink chains).  It does
481da177e4SLinus Torvalds  * this with calls to <fs>_follow_link().
491da177e4SLinus Torvalds  * As a side effect, dir_namei(), _namei() and follow_link() are now
501da177e4SLinus Torvalds  * replaced with a single function lookup_dentry() that can handle all
511da177e4SLinus Torvalds  * the special cases of the former code.
521da177e4SLinus Torvalds  *
531da177e4SLinus Torvalds  * With the new dcache, the pathname is stored at each inode, at least as
541da177e4SLinus Torvalds  * long as the refcount of the inode is positive.  As a side effect, the
551da177e4SLinus Torvalds  * size of the dcache depends on the inode cache and thus is dynamic.
561da177e4SLinus Torvalds  *
571da177e4SLinus Torvalds  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
581da177e4SLinus Torvalds  * resolution to correspond with current state of the code.
591da177e4SLinus Torvalds  *
601da177e4SLinus Torvalds  * Note that the symlink resolution is not *completely* iterative.
611da177e4SLinus Torvalds  * There is still a significant amount of tail- and mid- recursion in
621da177e4SLinus Torvalds  * the algorithm.  Also, note that <fs>_readlink() is not used in
631da177e4SLinus Torvalds  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
641da177e4SLinus Torvalds  * may return different results than <fs>_follow_link().  Many virtual
651da177e4SLinus Torvalds  * filesystems (including /proc) exhibit this behavior.
661da177e4SLinus Torvalds  */
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
691da177e4SLinus Torvalds  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
701da177e4SLinus Torvalds  * and the name already exists in form of a symlink, try to create the new
711da177e4SLinus Torvalds  * name indicated by the symlink. The old code always complained that the
721da177e4SLinus Torvalds  * name already exists, due to not following the symlink even if its target
731da177e4SLinus Torvalds  * is nonexistent.  The new semantics affects also mknod() and link() when
7425985edcSLucas De Marchi  * the name is a symlink pointing to a non-existent name.
751da177e4SLinus Torvalds  *
761da177e4SLinus Torvalds  * I don't know which semantics is the right one, since I have no access
771da177e4SLinus Torvalds  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
781da177e4SLinus Torvalds  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
791da177e4SLinus Torvalds  * "old" one. Personally, I think the new semantics is much more logical.
801da177e4SLinus Torvalds  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
811da177e4SLinus Torvalds  * file does succeed in both HP-UX and SunOs, but not in Solaris
821da177e4SLinus Torvalds  * and in the old Linux semantics.
831da177e4SLinus Torvalds  */
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
861da177e4SLinus Torvalds  * semantics.  See the comments in "open_namei" and "do_link" below.
871da177e4SLinus Torvalds  *
881da177e4SLinus Torvalds  * [10-Sep-98 Alan Modra] Another symlink change.
891da177e4SLinus Torvalds  */
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
921da177e4SLinus Torvalds  *	inside the path - always follow.
931da177e4SLinus Torvalds  *	in the last component in creation/removal/renaming - never follow.
941da177e4SLinus Torvalds  *	if LOOKUP_FOLLOW passed - follow.
951da177e4SLinus Torvalds  *	if the pathname has trailing slashes - follow.
961da177e4SLinus Torvalds  *	otherwise - don't follow.
971da177e4SLinus Torvalds  * (applied in that order).
981da177e4SLinus Torvalds  *
991da177e4SLinus Torvalds  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
1001da177e4SLinus Torvalds  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
1011da177e4SLinus Torvalds  * During the 2.4 we need to fix the userland stuff depending on it -
1021da177e4SLinus Torvalds  * hopefully we will be able to get rid of that wart in 2.5. So far only
1031da177e4SLinus Torvalds  * XEmacs seems to be relying on it...
1041da177e4SLinus Torvalds  */
1051da177e4SLinus Torvalds /*
1061da177e4SLinus Torvalds  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
107a11f3a05SArjan van de Ven  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1081da177e4SLinus Torvalds  * any extra contention...
1091da177e4SLinus Torvalds  */
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional
1121da177e4SLinus Torvalds  * checking and hopefully speeding things up, we copy filenames to the
1131da177e4SLinus Torvalds  * kernel data space before using them..
1141da177e4SLinus Torvalds  *
1151da177e4SLinus Torvalds  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
1161da177e4SLinus Torvalds  * PATH_MAX includes the nul terminator --RR.
1171da177e4SLinus Torvalds  */
118858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page)
1191da177e4SLinus Torvalds {
1201da177e4SLinus Torvalds 	int retval;
1211da177e4SLinus Torvalds 	unsigned long len = PATH_MAX;
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	if (!segment_eq(get_fs(), KERNEL_DS)) {
1241da177e4SLinus Torvalds 		if ((unsigned long) filename >= TASK_SIZE)
1251da177e4SLinus Torvalds 			return -EFAULT;
1261da177e4SLinus Torvalds 		if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
1271da177e4SLinus Torvalds 			len = TASK_SIZE - (unsigned long) filename;
1281da177e4SLinus Torvalds 	}
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	retval = strncpy_from_user(page, filename, len);
1311da177e4SLinus Torvalds 	if (retval > 0) {
1321da177e4SLinus Torvalds 		if (retval < len)
1331da177e4SLinus Torvalds 			return 0;
1341da177e4SLinus Torvalds 		return -ENAMETOOLONG;
1351da177e4SLinus Torvalds 	} else if (!retval)
1361da177e4SLinus Torvalds 		retval = -ENOENT;
1371da177e4SLinus Torvalds 	return retval;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1401fa1e7f6SAndy Whitcroft static char *getname_flags(const char __user *filename, int flags, int *empty)
1411da177e4SLinus Torvalds {
1421da177e4SLinus Torvalds 	char *tmp, *result;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	result = ERR_PTR(-ENOMEM);
1451da177e4SLinus Torvalds 	tmp = __getname();
1461da177e4SLinus Torvalds 	if (tmp)  {
1471da177e4SLinus Torvalds 		int retval = do_getname(filename, tmp);
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 		result = tmp;
1501da177e4SLinus Torvalds 		if (retval < 0) {
1511fa1e7f6SAndy Whitcroft 			if (retval == -ENOENT && empty)
1521fa1e7f6SAndy Whitcroft 				*empty = 1;
153f52e0c11SAl Viro 			if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
1541da177e4SLinus Torvalds 				__putname(tmp);
1551da177e4SLinus Torvalds 				result = ERR_PTR(retval);
1561da177e4SLinus Torvalds 			}
1571da177e4SLinus Torvalds 		}
158f52e0c11SAl Viro 	}
1591da177e4SLinus Torvalds 	audit_getname(result);
1601da177e4SLinus Torvalds 	return result;
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
163f52e0c11SAl Viro char *getname(const char __user * filename)
164f52e0c11SAl Viro {
1651fa1e7f6SAndy Whitcroft 	return getname_flags(filename, 0, 0);
166f52e0c11SAl Viro }
167f52e0c11SAl Viro 
1681da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
1691da177e4SLinus Torvalds void putname(const char *name)
1701da177e4SLinus Torvalds {
1715ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
1721da177e4SLinus Torvalds 		audit_putname(name);
1731da177e4SLinus Torvalds 	else
1741da177e4SLinus Torvalds 		__putname(name);
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds EXPORT_SYMBOL(putname);
1771da177e4SLinus Torvalds #endif
1781da177e4SLinus Torvalds 
179e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask)
180e77819e5SLinus Torvalds {
18184635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL
182e77819e5SLinus Torvalds 	struct posix_acl *acl;
183e77819e5SLinus Torvalds 
184e77819e5SLinus Torvalds 	if (mask & MAY_NOT_BLOCK) {
1853567866bSAl Viro 		acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
1863567866bSAl Viro 	        if (!acl)
187e77819e5SLinus Torvalds 	                return -EAGAIN;
1883567866bSAl Viro 		/* no ->get_acl() calls in RCU mode... */
1893567866bSAl Viro 		if (acl == ACL_NOT_CACHED)
190e77819e5SLinus Torvalds 			return -ECHILD;
191206b1d09SAri Savolainen 	        return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
192e77819e5SLinus Torvalds 	}
193e77819e5SLinus Torvalds 
194e77819e5SLinus Torvalds 	acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
195e77819e5SLinus Torvalds 
196e77819e5SLinus Torvalds 	/*
1974e34e719SChristoph Hellwig 	 * A filesystem can force a ACL callback by just never filling the
1984e34e719SChristoph Hellwig 	 * ACL cache. But normally you'd fill the cache either at inode
1994e34e719SChristoph Hellwig 	 * instantiation time, or on the first ->get_acl call.
200e77819e5SLinus Torvalds 	 *
2014e34e719SChristoph Hellwig 	 * If the filesystem doesn't have a get_acl() function at all, we'll
2024e34e719SChristoph Hellwig 	 * just create the negative cache entry.
203e77819e5SLinus Torvalds 	 */
204e77819e5SLinus Torvalds 	if (acl == ACL_NOT_CACHED) {
2054e34e719SChristoph Hellwig 	        if (inode->i_op->get_acl) {
2064e34e719SChristoph Hellwig 			acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
2074e34e719SChristoph Hellwig 			if (IS_ERR(acl))
2084e34e719SChristoph Hellwig 				return PTR_ERR(acl);
2094e34e719SChristoph Hellwig 		} else {
210e77819e5SLinus Torvalds 		        set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
211e77819e5SLinus Torvalds 		        return -EAGAIN;
212e77819e5SLinus Torvalds 		}
2134e34e719SChristoph Hellwig 	}
214e77819e5SLinus Torvalds 
215e77819e5SLinus Torvalds 	if (acl) {
216e77819e5SLinus Torvalds 	        int error = posix_acl_permission(inode, acl, mask);
217e77819e5SLinus Torvalds 	        posix_acl_release(acl);
218e77819e5SLinus Torvalds 	        return error;
219e77819e5SLinus Torvalds 	}
22084635d68SLinus Torvalds #endif
221e77819e5SLinus Torvalds 
222e77819e5SLinus Torvalds 	return -EAGAIN;
223e77819e5SLinus Torvalds }
224e77819e5SLinus Torvalds 
2255909ccaaSLinus Torvalds /*
226948409c7SAndreas Gruenbacher  * This does the basic permission checking
2275909ccaaSLinus Torvalds  */
2287e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask)
2295909ccaaSLinus Torvalds {
23026cf46beSLinus Torvalds 	unsigned int mode = inode->i_mode;
2315909ccaaSLinus Torvalds 
232e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
233e795b717SSerge E. Hallyn 		goto other_perms;
234e795b717SSerge E. Hallyn 
23514067ff5SLinus Torvalds 	if (likely(current_fsuid() == inode->i_uid))
2365909ccaaSLinus Torvalds 		mode >>= 6;
2375909ccaaSLinus Torvalds 	else {
238e77819e5SLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
2397e40145eSAl Viro 			int error = check_acl(inode, mask);
2405909ccaaSLinus Torvalds 			if (error != -EAGAIN)
2415909ccaaSLinus Torvalds 				return error;
2425909ccaaSLinus Torvalds 		}
2435909ccaaSLinus Torvalds 
2445909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
2455909ccaaSLinus Torvalds 			mode >>= 3;
2465909ccaaSLinus Torvalds 	}
2475909ccaaSLinus Torvalds 
248e795b717SSerge E. Hallyn other_perms:
2495909ccaaSLinus Torvalds 	/*
2505909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
2515909ccaaSLinus Torvalds 	 */
2529c2c7039SAl Viro 	if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
2535909ccaaSLinus Torvalds 		return 0;
2545909ccaaSLinus Torvalds 	return -EACCES;
2555909ccaaSLinus Torvalds }
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds /**
2581da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
2591da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2608fd90c8dSAndreas Gruenbacher  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
2611da177e4SLinus Torvalds  *
2621da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
2631da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
2641da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
265b74c79e9SNick Piggin  * are used for other things.
266b74c79e9SNick Piggin  *
267b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
268b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
269b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
2701da177e4SLinus Torvalds  */
2712830ba7fSAl Viro int generic_permission(struct inode *inode, int mask)
2721da177e4SLinus Torvalds {
2735909ccaaSLinus Torvalds 	int ret;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	/*
276948409c7SAndreas Gruenbacher 	 * Do the basic permission checks.
2771da177e4SLinus Torvalds 	 */
2787e40145eSAl Viro 	ret = acl_permission_check(inode, mask);
2795909ccaaSLinus Torvalds 	if (ret != -EACCES)
2805909ccaaSLinus Torvalds 		return ret;
2811da177e4SLinus Torvalds 
282d594e7ecSAl Viro 	if (S_ISDIR(inode->i_mode)) {
283d594e7ecSAl Viro 		/* DACs are overridable for directories */
284d594e7ecSAl Viro 		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
285d594e7ecSAl Viro 			return 0;
286d594e7ecSAl Viro 		if (!(mask & MAY_WRITE))
287d594e7ecSAl Viro 			if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
288d594e7ecSAl Viro 				return 0;
289d594e7ecSAl Viro 		return -EACCES;
290d594e7ecSAl Viro 	}
2911da177e4SLinus Torvalds 	/*
2921da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
293d594e7ecSAl Viro 	 * Executable DACs are overridable when there is
294d594e7ecSAl Viro 	 * at least one exec bit set.
2951da177e4SLinus Torvalds 	 */
296d594e7ecSAl Viro 	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
297e795b717SSerge E. Hallyn 		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
2981da177e4SLinus Torvalds 			return 0;
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	/*
3011da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
3021da177e4SLinus Torvalds 	 */
3037ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
304d594e7ecSAl Viro 	if (mask == MAY_READ)
305e795b717SSerge E. Hallyn 		if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
3061da177e4SLinus Torvalds 			return 0;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	return -EACCES;
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
3113ddcd056SLinus Torvalds /*
3123ddcd056SLinus Torvalds  * We _really_ want to just do "generic_permission()" without
3133ddcd056SLinus Torvalds  * even looking at the inode->i_op values. So we keep a cache
3143ddcd056SLinus Torvalds  * flag in inode->i_opflags, that says "this has not special
3153ddcd056SLinus Torvalds  * permission function, use the fast case".
3163ddcd056SLinus Torvalds  */
3173ddcd056SLinus Torvalds static inline int do_inode_permission(struct inode *inode, int mask)
3183ddcd056SLinus Torvalds {
3193ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
3203ddcd056SLinus Torvalds 		if (likely(inode->i_op->permission))
3213ddcd056SLinus Torvalds 			return inode->i_op->permission(inode, mask);
3223ddcd056SLinus Torvalds 
3233ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
3243ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
3253ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_FASTPERM;
3263ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
3273ddcd056SLinus Torvalds 	}
3283ddcd056SLinus Torvalds 	return generic_permission(inode, mask);
3293ddcd056SLinus Torvalds }
3303ddcd056SLinus Torvalds 
331cb23beb5SChristoph Hellwig /**
332cb23beb5SChristoph Hellwig  * inode_permission  -  check for access rights to a given inode
333cb23beb5SChristoph Hellwig  * @inode:	inode to check permission on
3348fd90c8dSAndreas Gruenbacher  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
335cb23beb5SChristoph Hellwig  *
336cb23beb5SChristoph Hellwig  * Used to check for read/write/execute permissions on an inode.
337cb23beb5SChristoph Hellwig  * We use "fsuid" for this, letting us set arbitrary permissions
338cb23beb5SChristoph Hellwig  * for filesystem access without changing the "normal" uids which
339cb23beb5SChristoph Hellwig  * are used for other things.
340948409c7SAndreas Gruenbacher  *
341948409c7SAndreas Gruenbacher  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
342cb23beb5SChristoph Hellwig  */
343f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask)
3441da177e4SLinus Torvalds {
345e6305c43SAl Viro 	int retval;
3461da177e4SLinus Torvalds 
3473ddcd056SLinus Torvalds 	if (unlikely(mask & MAY_WRITE)) {
34822590e41SMiklos Szeredi 		umode_t mode = inode->i_mode;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 		/*
3511da177e4SLinus Torvalds 		 * Nobody gets write access to a read-only fs.
3521da177e4SLinus Torvalds 		 */
3531da177e4SLinus Torvalds 		if (IS_RDONLY(inode) &&
3541da177e4SLinus Torvalds 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
3551da177e4SLinus Torvalds 			return -EROFS;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 		/*
3581da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
3591da177e4SLinus Torvalds 		 */
3601da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
3611da177e4SLinus Torvalds 			return -EACCES;
3621da177e4SLinus Torvalds 	}
3631da177e4SLinus Torvalds 
3643ddcd056SLinus Torvalds 	retval = do_inode_permission(inode, mask);
3651da177e4SLinus Torvalds 	if (retval)
3661da177e4SLinus Torvalds 		return retval;
3671da177e4SLinus Torvalds 
36808ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
36908ce5f16SSerge E. Hallyn 	if (retval)
37008ce5f16SSerge E. Hallyn 		return retval;
37108ce5f16SSerge E. Hallyn 
372d09ca739SEric Paris 	return security_inode_permission(inode, mask);
3731da177e4SLinus Torvalds }
3741da177e4SLinus Torvalds 
375f4d6ff89SAl Viro /**
3765dd784d0SJan Blunck  * path_get - get a reference to a path
3775dd784d0SJan Blunck  * @path: path to get the reference to
3785dd784d0SJan Blunck  *
3795dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3805dd784d0SJan Blunck  */
3815dd784d0SJan Blunck void path_get(struct path *path)
3825dd784d0SJan Blunck {
3835dd784d0SJan Blunck 	mntget(path->mnt);
3845dd784d0SJan Blunck 	dget(path->dentry);
3855dd784d0SJan Blunck }
3865dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
3875dd784d0SJan Blunck 
3885dd784d0SJan Blunck /**
3891d957f9bSJan Blunck  * path_put - put a reference to a path
3901d957f9bSJan Blunck  * @path: path to put the reference to
3911d957f9bSJan Blunck  *
3921d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
3931d957f9bSJan Blunck  */
3941d957f9bSJan Blunck void path_put(struct path *path)
3951da177e4SLinus Torvalds {
3961d957f9bSJan Blunck 	dput(path->dentry);
3971d957f9bSJan Blunck 	mntput(path->mnt);
3981da177e4SLinus Torvalds }
3991d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
4001da177e4SLinus Torvalds 
40119660af7SAl Viro /*
40231e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
40319660af7SAl Viro  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
40419660af7SAl Viro  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
40519660af7SAl Viro  * normal reference counts on dentries and vfsmounts to transition to rcu-walk
40619660af7SAl Viro  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
40719660af7SAl Viro  * got stuck, so ref-walk may continue from there. If this is not successful
40819660af7SAl Viro  * (eg. a seqcount has changed), then failure is returned and it's up to caller
40919660af7SAl Viro  * to restart the path walk from the beginning in ref-walk mode.
41031e6b01fSNick Piggin  */
41131e6b01fSNick Piggin 
41231e6b01fSNick Piggin /**
41319660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
41419660af7SAl Viro  * @nd: nameidata pathwalk data
41519660af7SAl Viro  * @dentry: child of nd->path.dentry or NULL
41639191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
41731e6b01fSNick Piggin  *
41819660af7SAl Viro  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
41919660af7SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
42019660af7SAl Viro  * @nd or NULL.  Must be called from rcu-walk context.
42131e6b01fSNick Piggin  */
42219660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
42331e6b01fSNick Piggin {
42431e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
42531e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
4265b6ca027SAl Viro 	int want_root = 0;
42731e6b01fSNick Piggin 
42831e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
4295b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
4305b6ca027SAl Viro 		want_root = 1;
43131e6b01fSNick Piggin 		spin_lock(&fs->lock);
43231e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
43331e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
43431e6b01fSNick Piggin 			goto err_root;
43531e6b01fSNick Piggin 	}
43631e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
43719660af7SAl Viro 	if (!dentry) {
43819660af7SAl Viro 		if (!__d_rcu_to_refcount(parent, nd->seq))
43919660af7SAl Viro 			goto err_parent;
44019660af7SAl Viro 		BUG_ON(nd->inode != parent->d_inode);
44119660af7SAl Viro 	} else {
44294c0d4ecSAl Viro 		if (dentry->d_parent != parent)
44394c0d4ecSAl Viro 			goto err_parent;
44431e6b01fSNick Piggin 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
44531e6b01fSNick Piggin 		if (!__d_rcu_to_refcount(dentry, nd->seq))
44619660af7SAl Viro 			goto err_child;
44731e6b01fSNick Piggin 		/*
44819660af7SAl Viro 		 * If the sequence check on the child dentry passed, then
44919660af7SAl Viro 		 * the child has not been removed from its parent. This
45019660af7SAl Viro 		 * means the parent dentry must be valid and able to take
45119660af7SAl Viro 		 * a reference at this point.
45231e6b01fSNick Piggin 		 */
45331e6b01fSNick Piggin 		BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
45431e6b01fSNick Piggin 		BUG_ON(!parent->d_count);
45531e6b01fSNick Piggin 		parent->d_count++;
45631e6b01fSNick Piggin 		spin_unlock(&dentry->d_lock);
45719660af7SAl Viro 	}
45831e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
4595b6ca027SAl Viro 	if (want_root) {
46031e6b01fSNick Piggin 		path_get(&nd->root);
46131e6b01fSNick Piggin 		spin_unlock(&fs->lock);
46231e6b01fSNick Piggin 	}
46331e6b01fSNick Piggin 	mntget(nd->path.mnt);
46431e6b01fSNick Piggin 
46531e6b01fSNick Piggin 	rcu_read_unlock();
46631e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
46731e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
46831e6b01fSNick Piggin 	return 0;
46919660af7SAl Viro 
47019660af7SAl Viro err_child:
47131e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
47219660af7SAl Viro err_parent:
47331e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
47431e6b01fSNick Piggin err_root:
4755b6ca027SAl Viro 	if (want_root)
47631e6b01fSNick Piggin 		spin_unlock(&fs->lock);
47731e6b01fSNick Piggin 	return -ECHILD;
47831e6b01fSNick Piggin }
47931e6b01fSNick Piggin 
48031e6b01fSNick Piggin /**
481834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
482834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
483834f2a4aSTrond Myklebust  */
484834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
485834f2a4aSTrond Myklebust {
4862dab5974SLinus Torvalds 	struct file *file = nd->intent.open.file;
4872dab5974SLinus Torvalds 
4882dab5974SLinus Torvalds 	if (file && !IS_ERR(file)) {
4892dab5974SLinus Torvalds 		if (file->f_path.dentry == NULL)
4902dab5974SLinus Torvalds 			put_filp(file);
491834f2a4aSTrond Myklebust 		else
4922dab5974SLinus Torvalds 			fput(file);
4932dab5974SLinus Torvalds 	}
494834f2a4aSTrond Myklebust }
495834f2a4aSTrond Myklebust 
496f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
49734286d66SNick Piggin {
498f60aef7eSAl Viro 	return dentry->d_op->d_revalidate(dentry, nd);
49934286d66SNick Piggin }
50034286d66SNick Piggin 
5019f1fafeeSAl Viro /**
5029f1fafeeSAl Viro  * complete_walk - successful completion of path walk
5039f1fafeeSAl Viro  * @nd:  pointer nameidata
50439159de2SJeff Layton  *
5059f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
5069f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
5079f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
5089f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
5099f1fafeeSAl Viro  * need to drop nd->path.
51039159de2SJeff Layton  */
5119f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
51239159de2SJeff Layton {
51316c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
51439159de2SJeff Layton 	int status;
51539159de2SJeff Layton 
5169f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
5179f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
5189f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
5199f1fafeeSAl Viro 			nd->root.mnt = NULL;
5209f1fafeeSAl Viro 		spin_lock(&dentry->d_lock);
5219f1fafeeSAl Viro 		if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
5229f1fafeeSAl Viro 			spin_unlock(&dentry->d_lock);
5239f1fafeeSAl Viro 			rcu_read_unlock();
5249f1fafeeSAl Viro 			br_read_unlock(vfsmount_lock);
5259f1fafeeSAl Viro 			return -ECHILD;
5269f1fafeeSAl Viro 		}
5279f1fafeeSAl Viro 		BUG_ON(nd->inode != dentry->d_inode);
5289f1fafeeSAl Viro 		spin_unlock(&dentry->d_lock);
5299f1fafeeSAl Viro 		mntget(nd->path.mnt);
5309f1fafeeSAl Viro 		rcu_read_unlock();
5319f1fafeeSAl Viro 		br_read_unlock(vfsmount_lock);
5329f1fafeeSAl Viro 	}
5339f1fafeeSAl Viro 
53416c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
53539159de2SJeff Layton 		return 0;
53639159de2SJeff Layton 
53716c2cd71SAl Viro 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
53816c2cd71SAl Viro 		return 0;
53916c2cd71SAl Viro 
54016c2cd71SAl Viro 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
54116c2cd71SAl Viro 		return 0;
54216c2cd71SAl Viro 
54316c2cd71SAl Viro 	/* Note: we do not d_invalidate() */
54434286d66SNick Piggin 	status = d_revalidate(dentry, nd);
54539159de2SJeff Layton 	if (status > 0)
54639159de2SJeff Layton 		return 0;
54739159de2SJeff Layton 
54816c2cd71SAl Viro 	if (!status)
54939159de2SJeff Layton 		status = -ESTALE;
55016c2cd71SAl Viro 
5519f1fafeeSAl Viro 	path_put(&nd->path);
55239159de2SJeff Layton 	return status;
55339159de2SJeff Layton }
55439159de2SJeff Layton 
5552a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
5562a737871SAl Viro {
557f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
558f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
5592a737871SAl Viro }
5602a737871SAl Viro 
5616de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
5626de88d72SAl Viro 
56331e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
56431e6b01fSNick Piggin {
56531e6b01fSNick Piggin 	if (!nd->root.mnt) {
56631e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
567c28cc364SNick Piggin 		unsigned seq;
568c28cc364SNick Piggin 
569c28cc364SNick Piggin 		do {
570c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
57131e6b01fSNick Piggin 			nd->root = fs->root;
572c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
573c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
57431e6b01fSNick Piggin 	}
57531e6b01fSNick Piggin }
57631e6b01fSNick Piggin 
577f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
5781da177e4SLinus Torvalds {
57931e6b01fSNick Piggin 	int ret;
58031e6b01fSNick Piggin 
5811da177e4SLinus Torvalds 	if (IS_ERR(link))
5821da177e4SLinus Torvalds 		goto fail;
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	if (*link == '/') {
5852a737871SAl Viro 		set_root(nd);
5861d957f9bSJan Blunck 		path_put(&nd->path);
5872a737871SAl Viro 		nd->path = nd->root;
5882a737871SAl Viro 		path_get(&nd->root);
58916c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
5901da177e4SLinus Torvalds 	}
59131e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
592b4091d5fSChristoph Hellwig 
59331e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
59431e6b01fSNick Piggin 	return ret;
5951da177e4SLinus Torvalds fail:
5961d957f9bSJan Blunck 	path_put(&nd->path);
5971da177e4SLinus Torvalds 	return PTR_ERR(link);
5981da177e4SLinus Torvalds }
5991da177e4SLinus Torvalds 
6001d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
601051d3812SIan Kent {
602051d3812SIan Kent 	dput(path->dentry);
6034ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
604051d3812SIan Kent 		mntput(path->mnt);
605051d3812SIan Kent }
606051d3812SIan Kent 
6077b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
6087b9337aaSNick Piggin 					struct nameidata *nd)
609051d3812SIan Kent {
61031e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
6114ac91378SJan Blunck 		dput(nd->path.dentry);
61231e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
6134ac91378SJan Blunck 			mntput(nd->path.mnt);
6149a229683SHuang Shijie 	}
61531e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
6164ac91378SJan Blunck 	nd->path.dentry = path->dentry;
617051d3812SIan Kent }
618051d3812SIan Kent 
619574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
620574197e0SAl Viro {
621574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
622574197e0SAl Viro 	if (!IS_ERR(cookie) && inode->i_op->put_link)
623574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
624574197e0SAl Viro 	path_put(link);
625574197e0SAl Viro }
626574197e0SAl Viro 
627def4af30SAl Viro static __always_inline int
628574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
6291da177e4SLinus Torvalds {
6301da177e4SLinus Torvalds 	int error;
6317b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
6321da177e4SLinus Torvalds 
633844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
634844a3917SAl Viro 
6350e794589SAl Viro 	if (link->mnt == nd->path.mnt)
6360e794589SAl Viro 		mntget(link->mnt);
6370e794589SAl Viro 
638574197e0SAl Viro 	if (unlikely(current->total_link_count >= 40)) {
639574197e0SAl Viro 		*p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
640574197e0SAl Viro 		path_put(&nd->path);
641574197e0SAl Viro 		return -ELOOP;
642574197e0SAl Viro 	}
643574197e0SAl Viro 	cond_resched();
644574197e0SAl Viro 	current->total_link_count++;
645574197e0SAl Viro 
6467b9337aaSNick Piggin 	touch_atime(link->mnt, dentry);
6471da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
648cd4e91d3SAl Viro 
64936f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
65036f3b4f6SAl Viro 	if (error) {
65136f3b4f6SAl Viro 		*p = ERR_PTR(error); /* no ->put_link(), please */
65236f3b4f6SAl Viro 		path_put(&nd->path);
65336f3b4f6SAl Viro 		return error;
65436f3b4f6SAl Viro 	}
65536f3b4f6SAl Viro 
65686acdca1SAl Viro 	nd->last_type = LAST_BIND;
657def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
658def4af30SAl Viro 	error = PTR_ERR(*p);
659def4af30SAl Viro 	if (!IS_ERR(*p)) {
6601da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
661cc314eefSLinus Torvalds 		error = 0;
6621da177e4SLinus Torvalds 		if (s)
6631da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
664bcda7652SAl Viro 		else if (nd->last_type == LAST_BIND) {
66516c2cd71SAl Viro 			nd->flags |= LOOKUP_JUMPED;
666b21041d0SAl Viro 			nd->inode = nd->path.dentry->d_inode;
667b21041d0SAl Viro 			if (nd->inode->i_op->follow_link) {
668bcda7652SAl Viro 				/* stepped on a _really_ weird one */
669bcda7652SAl Viro 				path_put(&nd->path);
670bcda7652SAl Viro 				error = -ELOOP;
671bcda7652SAl Viro 			}
672bcda7652SAl Viro 		}
6731da177e4SLinus Torvalds 	}
6741da177e4SLinus Torvalds 	return error;
6751da177e4SLinus Torvalds }
6761da177e4SLinus Torvalds 
67731e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
67831e6b01fSNick Piggin {
67931e6b01fSNick Piggin 	struct vfsmount *parent;
68031e6b01fSNick Piggin 	struct dentry *mountpoint;
68131e6b01fSNick Piggin 
68231e6b01fSNick Piggin 	parent = path->mnt->mnt_parent;
68331e6b01fSNick Piggin 	if (parent == path->mnt)
68431e6b01fSNick Piggin 		return 0;
68531e6b01fSNick Piggin 	mountpoint = path->mnt->mnt_mountpoint;
68631e6b01fSNick Piggin 	path->dentry = mountpoint;
68731e6b01fSNick Piggin 	path->mnt = parent;
68831e6b01fSNick Piggin 	return 1;
68931e6b01fSNick Piggin }
69031e6b01fSNick Piggin 
691bab77ebfSAl Viro int follow_up(struct path *path)
6921da177e4SLinus Torvalds {
6931da177e4SLinus Torvalds 	struct vfsmount *parent;
6941da177e4SLinus Torvalds 	struct dentry *mountpoint;
69599b7db7bSNick Piggin 
69699b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
697bab77ebfSAl Viro 	parent = path->mnt->mnt_parent;
698bab77ebfSAl Viro 	if (parent == path->mnt) {
69999b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
7001da177e4SLinus Torvalds 		return 0;
7011da177e4SLinus Torvalds 	}
7021da177e4SLinus Torvalds 	mntget(parent);
703bab77ebfSAl Viro 	mountpoint = dget(path->mnt->mnt_mountpoint);
70499b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
705bab77ebfSAl Viro 	dput(path->dentry);
706bab77ebfSAl Viro 	path->dentry = mountpoint;
707bab77ebfSAl Viro 	mntput(path->mnt);
708bab77ebfSAl Viro 	path->mnt = parent;
7091da177e4SLinus Torvalds 	return 1;
7101da177e4SLinus Torvalds }
7111da177e4SLinus Torvalds 
712b5c84bf6SNick Piggin /*
7139875cf80SDavid Howells  * Perform an automount
7149875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
7159875cf80SDavid Howells  *   were called with.
7161da177e4SLinus Torvalds  */
7179875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
7189875cf80SDavid Howells 			    bool *need_mntput)
71931e6b01fSNick Piggin {
7209875cf80SDavid Howells 	struct vfsmount *mnt;
721ea5b778aSDavid Howells 	int err;
7229875cf80SDavid Howells 
7239875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
7249875cf80SDavid Howells 		return -EREMOTE;
7259875cf80SDavid Howells 
7260ec26fd0SMiklos Szeredi 	/* We don't want to mount if someone's just doing a stat -
7270ec26fd0SMiklos Szeredi 	 * unless they're stat'ing a directory and appended a '/' to
7280ec26fd0SMiklos Szeredi 	 * the name.
7290ec26fd0SMiklos Szeredi 	 *
7300ec26fd0SMiklos Szeredi 	 * We do, however, want to mount if someone wants to open or
7315a30d8a2SDavid Howells 	 * create a file of any type under the mountpoint, wants to
7320ec26fd0SMiklos Szeredi 	 * traverse through the mountpoint or wants to open the
7330ec26fd0SMiklos Szeredi 	 * mounted directory.  Also, autofs may mark negative dentries
7340ec26fd0SMiklos Szeredi 	 * as being automount points.  These will need the attentions
7350ec26fd0SMiklos Szeredi 	 * of the daemon to instantiate them before they can be used.
7365a30d8a2SDavid Howells 	 */
7375a30d8a2SDavid Howells 	if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
738d94c177bSLinus Torvalds 		     LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
7395a30d8a2SDavid Howells 	    path->dentry->d_inode)
7409875cf80SDavid Howells 		return -EISDIR;
7410ec26fd0SMiklos Szeredi 
7429875cf80SDavid Howells 	current->total_link_count++;
7439875cf80SDavid Howells 	if (current->total_link_count >= 40)
7449875cf80SDavid Howells 		return -ELOOP;
7459875cf80SDavid Howells 
7469875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
7479875cf80SDavid Howells 	if (IS_ERR(mnt)) {
7489875cf80SDavid Howells 		/*
7499875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
7509875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
7519875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
7529875cf80SDavid Howells 		 *
7539875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
7549875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
7559875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
7569875cf80SDavid Howells 		 */
75749084c3bSAl Viro 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
7589875cf80SDavid Howells 			return -EREMOTE;
7599875cf80SDavid Howells 		return PTR_ERR(mnt);
76031e6b01fSNick Piggin 	}
761ea5b778aSDavid Howells 
7629875cf80SDavid Howells 	if (!mnt) /* mount collision */
7639875cf80SDavid Howells 		return 0;
7649875cf80SDavid Howells 
7658aef1884SAl Viro 	if (!*need_mntput) {
7668aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
7678aef1884SAl Viro 		mntget(path->mnt);
7688aef1884SAl Viro 		*need_mntput = true;
7698aef1884SAl Viro 	}
77019a167afSAl Viro 	err = finish_automount(mnt, path);
771ea5b778aSDavid Howells 
772ea5b778aSDavid Howells 	switch (err) {
773ea5b778aSDavid Howells 	case -EBUSY:
774ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
77519a167afSAl Viro 		return 0;
776ea5b778aSDavid Howells 	case 0:
7778aef1884SAl Viro 		path_put(path);
7789875cf80SDavid Howells 		path->mnt = mnt;
7799875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
7809875cf80SDavid Howells 		return 0;
78119a167afSAl Viro 	default:
78219a167afSAl Viro 		return err;
7839875cf80SDavid Howells 	}
78419a167afSAl Viro 
785ea5b778aSDavid Howells }
7869875cf80SDavid Howells 
7879875cf80SDavid Howells /*
7889875cf80SDavid Howells  * Handle a dentry that is managed in some way.
789cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
7909875cf80SDavid Howells  * - Flagged as mountpoint
7919875cf80SDavid Howells  * - Flagged as automount point
7929875cf80SDavid Howells  *
7939875cf80SDavid Howells  * This may only be called in refwalk mode.
7949875cf80SDavid Howells  *
7959875cf80SDavid Howells  * Serialization is taken care of in namespace.c
7969875cf80SDavid Howells  */
7979875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
7989875cf80SDavid Howells {
7998aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
8009875cf80SDavid Howells 	unsigned managed;
8019875cf80SDavid Howells 	bool need_mntput = false;
8028aef1884SAl Viro 	int ret = 0;
8039875cf80SDavid Howells 
8049875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
8059875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
8069875cf80SDavid Howells 	 * the components of that value change under us */
8079875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
8089875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
8099875cf80SDavid Howells 	       unlikely(managed != 0)) {
810cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
811cc53ce53SDavid Howells 		 * being held. */
812cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
813cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
814cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
8151aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
816cc53ce53SDavid Howells 			if (ret < 0)
8178aef1884SAl Viro 				break;
818cc53ce53SDavid Howells 		}
819cc53ce53SDavid Howells 
8209875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
8219875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
8229875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
8239875cf80SDavid Howells 			if (mounted) {
8249875cf80SDavid Howells 				dput(path->dentry);
8259875cf80SDavid Howells 				if (need_mntput)
826463ffb2eSAl Viro 					mntput(path->mnt);
827463ffb2eSAl Viro 				path->mnt = mounted;
828463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
8299875cf80SDavid Howells 				need_mntput = true;
8309875cf80SDavid Howells 				continue;
831463ffb2eSAl Viro 			}
832463ffb2eSAl Viro 
8339875cf80SDavid Howells 			/* Something is mounted on this dentry in another
8349875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
8359875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
8369875cf80SDavid Howells 			 * vfsmount_lock */
8371da177e4SLinus Torvalds 		}
8389875cf80SDavid Howells 
8399875cf80SDavid Howells 		/* Handle an automount point */
8409875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
8419875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
8429875cf80SDavid Howells 			if (ret < 0)
8438aef1884SAl Viro 				break;
8449875cf80SDavid Howells 			continue;
8459875cf80SDavid Howells 		}
8469875cf80SDavid Howells 
8479875cf80SDavid Howells 		/* We didn't change the current path point */
8489875cf80SDavid Howells 		break;
8499875cf80SDavid Howells 	}
8508aef1884SAl Viro 
8518aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
8528aef1884SAl Viro 		mntput(path->mnt);
8538aef1884SAl Viro 	if (ret == -EISDIR)
8548aef1884SAl Viro 		ret = 0;
855a3fbbde7SAl Viro 	return ret < 0 ? ret : need_mntput;
8561da177e4SLinus Torvalds }
8571da177e4SLinus Torvalds 
858cc53ce53SDavid Howells int follow_down_one(struct path *path)
8591da177e4SLinus Torvalds {
8601da177e4SLinus Torvalds 	struct vfsmount *mounted;
8611da177e4SLinus Torvalds 
8621c755af4SAl Viro 	mounted = lookup_mnt(path);
8631da177e4SLinus Torvalds 	if (mounted) {
8649393bd07SAl Viro 		dput(path->dentry);
8659393bd07SAl Viro 		mntput(path->mnt);
8669393bd07SAl Viro 		path->mnt = mounted;
8679393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
8681da177e4SLinus Torvalds 		return 1;
8691da177e4SLinus Torvalds 	}
8701da177e4SLinus Torvalds 	return 0;
8711da177e4SLinus Torvalds }
8721da177e4SLinus Torvalds 
87362a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
87462a7375eSIan Kent {
87562a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
87662a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
87762a7375eSIan Kent }
87862a7375eSIan Kent 
8799875cf80SDavid Howells /*
880287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
881287548e4SAl Viro  * we meet a managed dentry that would need blocking.
8829875cf80SDavid Howells  */
8839875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
884287548e4SAl Viro 			       struct inode **inode)
8859875cf80SDavid Howells {
88662a7375eSIan Kent 	for (;;) {
8879875cf80SDavid Howells 		struct vfsmount *mounted;
88862a7375eSIan Kent 		/*
88962a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
89062a7375eSIan Kent 		 * that wants to block transit.
89162a7375eSIan Kent 		 */
892287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
893ab90911fSDavid Howells 			return false;
89462a7375eSIan Kent 
89562a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
89662a7375eSIan Kent 			break;
89762a7375eSIan Kent 
8989875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
8999875cf80SDavid Howells 		if (!mounted)
9009875cf80SDavid Howells 			break;
9019875cf80SDavid Howells 		path->mnt = mounted;
9029875cf80SDavid Howells 		path->dentry = mounted->mnt_root;
903a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
9049875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
90559430262SLinus Torvalds 		/*
90659430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
90759430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
90859430262SLinus Torvalds 		 * because a mount-point is always pinned.
90959430262SLinus Torvalds 		 */
91059430262SLinus Torvalds 		*inode = path->dentry->d_inode;
9119875cf80SDavid Howells 	}
9129875cf80SDavid Howells 	return true;
9139875cf80SDavid Howells }
9149875cf80SDavid Howells 
915dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
916287548e4SAl Viro {
917dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
918287548e4SAl Viro 		struct vfsmount *mounted;
919dea39376SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
920287548e4SAl Viro 		if (!mounted)
921287548e4SAl Viro 			break;
922dea39376SAl Viro 		nd->path.mnt = mounted;
923dea39376SAl Viro 		nd->path.dentry = mounted->mnt_root;
924dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
925287548e4SAl Viro 	}
926287548e4SAl Viro }
927287548e4SAl Viro 
92831e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
92931e6b01fSNick Piggin {
93031e6b01fSNick Piggin 	set_root_rcu(nd);
93131e6b01fSNick Piggin 
93231e6b01fSNick Piggin 	while (1) {
93331e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
93431e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
93531e6b01fSNick Piggin 			break;
93631e6b01fSNick Piggin 		}
93731e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
93831e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
93931e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
94031e6b01fSNick Piggin 			unsigned seq;
94131e6b01fSNick Piggin 
94231e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
94331e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
944ef7562d5SAl Viro 				goto failed;
94531e6b01fSNick Piggin 			nd->path.dentry = parent;
94631e6b01fSNick Piggin 			nd->seq = seq;
94731e6b01fSNick Piggin 			break;
94831e6b01fSNick Piggin 		}
94931e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
95031e6b01fSNick Piggin 			break;
95131e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
95231e6b01fSNick Piggin 	}
953dea39376SAl Viro 	follow_mount_rcu(nd);
954dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
95531e6b01fSNick Piggin 	return 0;
956ef7562d5SAl Viro 
957ef7562d5SAl Viro failed:
958ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
9595b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
960ef7562d5SAl Viro 		nd->root.mnt = NULL;
961ef7562d5SAl Viro 	rcu_read_unlock();
962ef7562d5SAl Viro 	br_read_unlock(vfsmount_lock);
963ef7562d5SAl Viro 	return -ECHILD;
96431e6b01fSNick Piggin }
96531e6b01fSNick Piggin 
9669875cf80SDavid Howells /*
967cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
968cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
969cc53ce53SDavid Howells  * caller is permitted to proceed or not.
970cc53ce53SDavid Howells  */
9717cc90cc3SAl Viro int follow_down(struct path *path)
972cc53ce53SDavid Howells {
973cc53ce53SDavid Howells 	unsigned managed;
974cc53ce53SDavid Howells 	int ret;
975cc53ce53SDavid Howells 
976cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
977cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
978cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
979cc53ce53SDavid Howells 		 * being held.
980cc53ce53SDavid Howells 		 *
981cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
982cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
983cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
984cc53ce53SDavid Howells 		 * superstructure.
985cc53ce53SDavid Howells 		 *
986cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
987cc53ce53SDavid Howells 		 */
988cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
989cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
990cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
991ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
9921aed3e42SAl Viro 				path->dentry, false);
993cc53ce53SDavid Howells 			if (ret < 0)
994cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
995cc53ce53SDavid Howells 		}
996cc53ce53SDavid Howells 
997cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
998cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
999cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
1000cc53ce53SDavid Howells 			if (!mounted)
1001cc53ce53SDavid Howells 				break;
1002cc53ce53SDavid Howells 			dput(path->dentry);
1003cc53ce53SDavid Howells 			mntput(path->mnt);
1004cc53ce53SDavid Howells 			path->mnt = mounted;
1005cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1006cc53ce53SDavid Howells 			continue;
1007cc53ce53SDavid Howells 		}
1008cc53ce53SDavid Howells 
1009cc53ce53SDavid Howells 		/* Don't handle automount points here */
1010cc53ce53SDavid Howells 		break;
1011cc53ce53SDavid Howells 	}
1012cc53ce53SDavid Howells 	return 0;
1013cc53ce53SDavid Howells }
1014cc53ce53SDavid Howells 
1015cc53ce53SDavid Howells /*
10169875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
10179875cf80SDavid Howells  */
10189875cf80SDavid Howells static void follow_mount(struct path *path)
10199875cf80SDavid Howells {
10209875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
10219875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
10229875cf80SDavid Howells 		if (!mounted)
10239875cf80SDavid Howells 			break;
10249875cf80SDavid Howells 		dput(path->dentry);
10259875cf80SDavid Howells 		mntput(path->mnt);
10269875cf80SDavid Howells 		path->mnt = mounted;
10279875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
10289875cf80SDavid Howells 	}
10299875cf80SDavid Howells }
10309875cf80SDavid Howells 
103131e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
10321da177e4SLinus Torvalds {
10332a737871SAl Viro 	set_root(nd);
1034e518ddb7SAndreas Mohr 
10351da177e4SLinus Torvalds 	while(1) {
10364ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
10371da177e4SLinus Torvalds 
10382a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
10392a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
10401da177e4SLinus Torvalds 			break;
10411da177e4SLinus Torvalds 		}
10424ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
10433088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
10443088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
10451da177e4SLinus Torvalds 			dput(old);
10461da177e4SLinus Torvalds 			break;
10471da177e4SLinus Torvalds 		}
10483088dd70SAl Viro 		if (!follow_up(&nd->path))
10491da177e4SLinus Torvalds 			break;
10501da177e4SLinus Torvalds 	}
105179ed0226SAl Viro 	follow_mount(&nd->path);
105231e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
10531da177e4SLinus Torvalds }
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds /*
1056baa03890SNick Piggin  * Allocate a dentry with name and parent, and perform a parent
1057baa03890SNick Piggin  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1058baa03890SNick Piggin  * on error. parent->d_inode->i_mutex must be held. d_lookup must
1059baa03890SNick Piggin  * have verified that no child exists while under i_mutex.
1060baa03890SNick Piggin  */
1061baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1062baa03890SNick Piggin 				struct qstr *name, struct nameidata *nd)
1063baa03890SNick Piggin {
1064baa03890SNick Piggin 	struct inode *inode = parent->d_inode;
1065baa03890SNick Piggin 	struct dentry *dentry;
1066baa03890SNick Piggin 	struct dentry *old;
1067baa03890SNick Piggin 
1068baa03890SNick Piggin 	/* Don't create child dentry for a dead directory. */
1069baa03890SNick Piggin 	if (unlikely(IS_DEADDIR(inode)))
1070baa03890SNick Piggin 		return ERR_PTR(-ENOENT);
1071baa03890SNick Piggin 
1072baa03890SNick Piggin 	dentry = d_alloc(parent, name);
1073baa03890SNick Piggin 	if (unlikely(!dentry))
1074baa03890SNick Piggin 		return ERR_PTR(-ENOMEM);
1075baa03890SNick Piggin 
1076baa03890SNick Piggin 	old = inode->i_op->lookup(inode, dentry, nd);
1077baa03890SNick Piggin 	if (unlikely(old)) {
1078baa03890SNick Piggin 		dput(dentry);
1079baa03890SNick Piggin 		dentry = old;
1080baa03890SNick Piggin 	}
1081baa03890SNick Piggin 	return dentry;
1082baa03890SNick Piggin }
1083baa03890SNick Piggin 
1084baa03890SNick Piggin /*
108544396f4bSJosef Bacik  * We already have a dentry, but require a lookup to be performed on the parent
108644396f4bSJosef Bacik  * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
108744396f4bSJosef Bacik  * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
108844396f4bSJosef Bacik  * child exists while under i_mutex.
108944396f4bSJosef Bacik  */
109044396f4bSJosef Bacik static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
109144396f4bSJosef Bacik 				     struct nameidata *nd)
109244396f4bSJosef Bacik {
109344396f4bSJosef Bacik 	struct inode *inode = parent->d_inode;
109444396f4bSJosef Bacik 	struct dentry *old;
109544396f4bSJosef Bacik 
109644396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
109744396f4bSJosef Bacik 	if (unlikely(IS_DEADDIR(inode)))
109844396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
109944396f4bSJosef Bacik 
110044396f4bSJosef Bacik 	old = inode->i_op->lookup(inode, dentry, nd);
110144396f4bSJosef Bacik 	if (unlikely(old)) {
110244396f4bSJosef Bacik 		dput(dentry);
110344396f4bSJosef Bacik 		dentry = old;
110444396f4bSJosef Bacik 	}
110544396f4bSJosef Bacik 	return dentry;
110644396f4bSJosef Bacik }
110744396f4bSJosef Bacik 
110844396f4bSJosef Bacik /*
11091da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
11101da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
11111da177e4SLinus Torvalds  *  It _is_ time-critical.
11121da177e4SLinus Torvalds  */
11131da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
111431e6b01fSNick Piggin 			struct path *path, struct inode **inode)
11151da177e4SLinus Torvalds {
11164ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
111731e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
11185a18fff2SAl Viro 	int need_reval = 1;
11195a18fff2SAl Viro 	int status = 1;
11209875cf80SDavid Howells 	int err;
11219875cf80SDavid Howells 
11223cac260aSAl Viro 	/*
1123b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1124b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1125b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1126b04f784eSNick Piggin 	 */
112731e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
112831e6b01fSNick Piggin 		unsigned seq;
112931e6b01fSNick Piggin 		*inode = nd->inode;
113031e6b01fSNick Piggin 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
11315a18fff2SAl Viro 		if (!dentry)
11325a18fff2SAl Viro 			goto unlazy;
11335a18fff2SAl Viro 
113431e6b01fSNick Piggin 		/* Memory barrier in read_seqcount_begin of child is enough */
113531e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
113631e6b01fSNick Piggin 			return -ECHILD;
113731e6b01fSNick Piggin 		nd->seq = seq;
11385a18fff2SAl Viro 
113924643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
11405a18fff2SAl Viro 			status = d_revalidate(dentry, nd);
11415a18fff2SAl Viro 			if (unlikely(status <= 0)) {
11425a18fff2SAl Viro 				if (status != -ECHILD)
11435a18fff2SAl Viro 					need_reval = 0;
11445a18fff2SAl Viro 				goto unlazy;
11455a18fff2SAl Viro 			}
114624643087SAl Viro 		}
114744396f4bSJosef Bacik 		if (unlikely(d_need_lookup(dentry)))
114844396f4bSJosef Bacik 			goto unlazy;
114931e6b01fSNick Piggin 		path->mnt = mnt;
115031e6b01fSNick Piggin 		path->dentry = dentry;
1151d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1152d6e9bd25SAl Viro 			goto unlazy;
1153d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1154d6e9bd25SAl Viro 			goto unlazy;
11559875cf80SDavid Howells 		return 0;
11565a18fff2SAl Viro unlazy:
115719660af7SAl Viro 		if (unlazy_walk(nd, dentry))
11585a18fff2SAl Viro 			return -ECHILD;
11595a18fff2SAl Viro 	} else {
116031e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
116124643087SAl Viro 	}
11625a18fff2SAl Viro 
116344396f4bSJosef Bacik 	if (dentry && unlikely(d_need_lookup(dentry))) {
116444396f4bSJosef Bacik 		dput(dentry);
116544396f4bSJosef Bacik 		dentry = NULL;
116644396f4bSJosef Bacik 	}
11675a18fff2SAl Viro retry:
11685a18fff2SAl Viro 	if (unlikely(!dentry)) {
11695a18fff2SAl Viro 		struct inode *dir = parent->d_inode;
11705a18fff2SAl Viro 		BUG_ON(nd->inode != dir);
11715a18fff2SAl Viro 
11725a18fff2SAl Viro 		mutex_lock(&dir->i_mutex);
11735a18fff2SAl Viro 		dentry = d_lookup(parent, name);
11745a18fff2SAl Viro 		if (likely(!dentry)) {
11755a18fff2SAl Viro 			dentry = d_alloc_and_lookup(parent, name, nd);
11765a18fff2SAl Viro 			if (IS_ERR(dentry)) {
11775a18fff2SAl Viro 				mutex_unlock(&dir->i_mutex);
11785a18fff2SAl Viro 				return PTR_ERR(dentry);
11795a18fff2SAl Viro 			}
11805a18fff2SAl Viro 			/* known good */
11815a18fff2SAl Viro 			need_reval = 0;
11825a18fff2SAl Viro 			status = 1;
118344396f4bSJosef Bacik 		} else if (unlikely(d_need_lookup(dentry))) {
118444396f4bSJosef Bacik 			dentry = d_inode_lookup(parent, dentry, nd);
118544396f4bSJosef Bacik 			if (IS_ERR(dentry)) {
118644396f4bSJosef Bacik 				mutex_unlock(&dir->i_mutex);
118744396f4bSJosef Bacik 				return PTR_ERR(dentry);
118844396f4bSJosef Bacik 			}
118944396f4bSJosef Bacik 			/* known good */
119044396f4bSJosef Bacik 			need_reval = 0;
119144396f4bSJosef Bacik 			status = 1;
11925a18fff2SAl Viro 		}
11935a18fff2SAl Viro 		mutex_unlock(&dir->i_mutex);
11945a18fff2SAl Viro 	}
11955a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
11965a18fff2SAl Viro 		status = d_revalidate(dentry, nd);
11975a18fff2SAl Viro 	if (unlikely(status <= 0)) {
11985a18fff2SAl Viro 		if (status < 0) {
11995a18fff2SAl Viro 			dput(dentry);
12005a18fff2SAl Viro 			return status;
12015a18fff2SAl Viro 		}
12025a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
12035a18fff2SAl Viro 			dput(dentry);
12045a18fff2SAl Viro 			dentry = NULL;
12055a18fff2SAl Viro 			need_reval = 1;
12065a18fff2SAl Viro 			goto retry;
12075a18fff2SAl Viro 		}
12085a18fff2SAl Viro 	}
12095a18fff2SAl Viro 
12101da177e4SLinus Torvalds 	path->mnt = mnt;
12111da177e4SLinus Torvalds 	path->dentry = dentry;
12129875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
121389312214SIan Kent 	if (unlikely(err < 0)) {
121489312214SIan Kent 		path_put_conditional(path, nd);
12159875cf80SDavid Howells 		return err;
121689312214SIan Kent 	}
1217a3fbbde7SAl Viro 	if (err)
1218a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
121931e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
12201da177e4SLinus Torvalds 	return 0;
12211da177e4SLinus Torvalds }
12221da177e4SLinus Torvalds 
122352094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
122452094c8aSAl Viro {
122552094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
12264ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
122752094c8aSAl Viro 		if (err != -ECHILD)
122852094c8aSAl Viro 			return err;
122919660af7SAl Viro 		if (unlazy_walk(nd, NULL))
123052094c8aSAl Viro 			return -ECHILD;
123152094c8aSAl Viro 	}
12324ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
123352094c8aSAl Viro }
123452094c8aSAl Viro 
12359856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
12369856fa1bSAl Viro {
12379856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
12389856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
12399856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
12409856fa1bSAl Viro 				return -ECHILD;
12419856fa1bSAl Viro 		} else
12429856fa1bSAl Viro 			follow_dotdot(nd);
12439856fa1bSAl Viro 	}
12449856fa1bSAl Viro 	return 0;
12459856fa1bSAl Viro }
12469856fa1bSAl Viro 
1247951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1248951361f9SAl Viro {
1249951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1250951361f9SAl Viro 		path_put(&nd->path);
1251951361f9SAl Viro 	} else {
1252951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
12535b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1254951361f9SAl Viro 			nd->root.mnt = NULL;
1255951361f9SAl Viro 		rcu_read_unlock();
1256951361f9SAl Viro 		br_read_unlock(vfsmount_lock);
1257951361f9SAl Viro 	}
1258951361f9SAl Viro }
1259951361f9SAl Viro 
12603ddcd056SLinus Torvalds /*
12613ddcd056SLinus Torvalds  * Do we need to follow links? We _really_ want to be able
12623ddcd056SLinus Torvalds  * to do this check without having to look at inode->i_op,
12633ddcd056SLinus Torvalds  * so we keep a cache of "no, this doesn't need follow_link"
12643ddcd056SLinus Torvalds  * for the common case.
12653ddcd056SLinus Torvalds  */
12667813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow)
12673ddcd056SLinus Torvalds {
12683ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
12693ddcd056SLinus Torvalds 		if (likely(inode->i_op->follow_link))
12703ddcd056SLinus Torvalds 			return follow;
12713ddcd056SLinus Torvalds 
12723ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
12733ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
12743ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_NOFOLLOW;
12753ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
12763ddcd056SLinus Torvalds 	}
12773ddcd056SLinus Torvalds 	return 0;
12783ddcd056SLinus Torvalds }
12793ddcd056SLinus Torvalds 
1280ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
1281ce57dfc1SAl Viro 		struct qstr *name, int type, int follow)
1282ce57dfc1SAl Viro {
1283ce57dfc1SAl Viro 	struct inode *inode;
1284ce57dfc1SAl Viro 	int err;
1285ce57dfc1SAl Viro 	/*
1286ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1287ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1288ce57dfc1SAl Viro 	 * parent relationships.
1289ce57dfc1SAl Viro 	 */
1290ce57dfc1SAl Viro 	if (unlikely(type != LAST_NORM))
1291ce57dfc1SAl Viro 		return handle_dots(nd, type);
1292ce57dfc1SAl Viro 	err = do_lookup(nd, name, path, &inode);
1293ce57dfc1SAl Viro 	if (unlikely(err)) {
1294ce57dfc1SAl Viro 		terminate_walk(nd);
1295ce57dfc1SAl Viro 		return err;
1296ce57dfc1SAl Viro 	}
1297ce57dfc1SAl Viro 	if (!inode) {
1298ce57dfc1SAl Viro 		path_to_nameidata(path, nd);
1299ce57dfc1SAl Viro 		terminate_walk(nd);
1300ce57dfc1SAl Viro 		return -ENOENT;
1301ce57dfc1SAl Viro 	}
13027813b94aSLinus Torvalds 	if (should_follow_link(inode, follow)) {
130319660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
130419660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
130519660af7SAl Viro 				terminate_walk(nd);
1306ce57dfc1SAl Viro 				return -ECHILD;
130719660af7SAl Viro 			}
130819660af7SAl Viro 		}
1309ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1310ce57dfc1SAl Viro 		return 1;
1311ce57dfc1SAl Viro 	}
1312ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1313ce57dfc1SAl Viro 	nd->inode = inode;
1314ce57dfc1SAl Viro 	return 0;
1315ce57dfc1SAl Viro }
1316ce57dfc1SAl Viro 
13171da177e4SLinus Torvalds /*
1318b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1319b356379aSAl Viro  * limiting consecutive symlinks to 40.
1320b356379aSAl Viro  *
1321b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1322b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1323b356379aSAl Viro  */
1324b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1325b356379aSAl Viro {
1326b356379aSAl Viro 	int res;
1327b356379aSAl Viro 
1328b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1329b356379aSAl Viro 		path_put_conditional(path, nd);
1330b356379aSAl Viro 		path_put(&nd->path);
1331b356379aSAl Viro 		return -ELOOP;
1332b356379aSAl Viro 	}
13331a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1334b356379aSAl Viro 
1335b356379aSAl Viro 	nd->depth++;
1336b356379aSAl Viro 	current->link_count++;
1337b356379aSAl Viro 
1338b356379aSAl Viro 	do {
1339b356379aSAl Viro 		struct path link = *path;
1340b356379aSAl Viro 		void *cookie;
1341574197e0SAl Viro 
1342574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
1343b356379aSAl Viro 		if (!res)
1344b356379aSAl Viro 			res = walk_component(nd, path, &nd->last,
1345b356379aSAl Viro 					     nd->last_type, LOOKUP_FOLLOW);
1346574197e0SAl Viro 		put_link(nd, &link, cookie);
1347b356379aSAl Viro 	} while (res > 0);
1348b356379aSAl Viro 
1349b356379aSAl Viro 	current->link_count--;
1350b356379aSAl Viro 	nd->depth--;
1351b356379aSAl Viro 	return res;
1352b356379aSAl Viro }
1353b356379aSAl Viro 
1354b356379aSAl Viro /*
13553ddcd056SLinus Torvalds  * We really don't want to look at inode->i_op->lookup
13563ddcd056SLinus Torvalds  * when we don't have to. So we keep a cache bit in
13573ddcd056SLinus Torvalds  * the inode ->i_opflags field that says "yes, we can
13583ddcd056SLinus Torvalds  * do lookup on this inode".
13593ddcd056SLinus Torvalds  */
13603ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode)
13613ddcd056SLinus Torvalds {
13623ddcd056SLinus Torvalds 	if (likely(inode->i_opflags & IOP_LOOKUP))
13633ddcd056SLinus Torvalds 		return 1;
13643ddcd056SLinus Torvalds 	if (likely(!inode->i_op->lookup))
13653ddcd056SLinus Torvalds 		return 0;
13663ddcd056SLinus Torvalds 
13673ddcd056SLinus Torvalds 	/* We do this once for the lifetime of the inode */
13683ddcd056SLinus Torvalds 	spin_lock(&inode->i_lock);
13693ddcd056SLinus Torvalds 	inode->i_opflags |= IOP_LOOKUP;
13703ddcd056SLinus Torvalds 	spin_unlock(&inode->i_lock);
13713ddcd056SLinus Torvalds 	return 1;
13723ddcd056SLinus Torvalds }
13733ddcd056SLinus Torvalds 
13743ddcd056SLinus Torvalds /*
13751da177e4SLinus Torvalds  * Name resolution.
1376ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1377ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
13781da177e4SLinus Torvalds  *
1379ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1380ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
13811da177e4SLinus Torvalds  */
13826de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
13831da177e4SLinus Torvalds {
13841da177e4SLinus Torvalds 	struct path next;
13851da177e4SLinus Torvalds 	int err;
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 	while (*name=='/')
13881da177e4SLinus Torvalds 		name++;
13891da177e4SLinus Torvalds 	if (!*name)
1390086e183aSAl Viro 		return 0;
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
13931da177e4SLinus Torvalds 	for(;;) {
13941da177e4SLinus Torvalds 		unsigned long hash;
13951da177e4SLinus Torvalds 		struct qstr this;
13961da177e4SLinus Torvalds 		unsigned int c;
1397fe479a58SAl Viro 		int type;
13981da177e4SLinus Torvalds 
139952094c8aSAl Viro 		err = may_lookup(nd);
14001da177e4SLinus Torvalds  		if (err)
14011da177e4SLinus Torvalds 			break;
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds 		this.name = name;
14041da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds 		hash = init_name_hash();
14071da177e4SLinus Torvalds 		do {
14081da177e4SLinus Torvalds 			name++;
14091da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
14101da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
14111da177e4SLinus Torvalds 		} while (c && (c != '/'));
14121da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
14131da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
14141da177e4SLinus Torvalds 
1415fe479a58SAl Viro 		type = LAST_NORM;
1416fe479a58SAl Viro 		if (this.name[0] == '.') switch (this.len) {
1417fe479a58SAl Viro 			case 2:
141816c2cd71SAl Viro 				if (this.name[1] == '.') {
1419fe479a58SAl Viro 					type = LAST_DOTDOT;
142016c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
142116c2cd71SAl Viro 				}
1422fe479a58SAl Viro 				break;
1423fe479a58SAl Viro 			case 1:
1424fe479a58SAl Viro 				type = LAST_DOT;
1425fe479a58SAl Viro 		}
14265a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
14275a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
142816c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
14295a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
14305a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
14315a202bcdSAl Viro 							   &this);
14325a202bcdSAl Viro 				if (err < 0)
14335a202bcdSAl Viro 					break;
14345a202bcdSAl Viro 			}
14355a202bcdSAl Viro 		}
1436fe479a58SAl Viro 
14371da177e4SLinus Torvalds 		/* remove trailing slashes? */
14381da177e4SLinus Torvalds 		if (!c)
14391da177e4SLinus Torvalds 			goto last_component;
14401da177e4SLinus Torvalds 		while (*++name == '/');
14411da177e4SLinus Torvalds 		if (!*name)
1442b356379aSAl Viro 			goto last_component;
14431da177e4SLinus Torvalds 
1444ce57dfc1SAl Viro 		err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1445ce57dfc1SAl Viro 		if (err < 0)
1446ce57dfc1SAl Viro 			return err;
1447fe479a58SAl Viro 
1448ce57dfc1SAl Viro 		if (err) {
1449b356379aSAl Viro 			err = nested_symlink(&next, nd);
14501da177e4SLinus Torvalds 			if (err)
1451a7472babSAl Viro 				return err;
145231e6b01fSNick Piggin 		}
14533ddcd056SLinus Torvalds 		if (can_lookup(nd->inode))
14541da177e4SLinus Torvalds 			continue;
14553ddcd056SLinus Torvalds 		err = -ENOTDIR;
14563ddcd056SLinus Torvalds 		break;
14571da177e4SLinus Torvalds 		/* here ends the main loop */
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds last_component:
1460ce57dfc1SAl Viro 		nd->last = this;
1461ce57dfc1SAl Viro 		nd->last_type = type;
1462ce57dfc1SAl Viro 		return 0;
1463ce57dfc1SAl Viro 	}
1464951361f9SAl Viro 	terminate_walk(nd);
14651da177e4SLinus Torvalds 	return err;
14661da177e4SLinus Torvalds }
14671da177e4SLinus Torvalds 
146870e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
146970e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
147031e6b01fSNick Piggin {
147131e6b01fSNick Piggin 	int retval = 0;
147231e6b01fSNick Piggin 	int fput_needed;
147331e6b01fSNick Piggin 	struct file *file;
147431e6b01fSNick Piggin 
147531e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
147616c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
147731e6b01fSNick Piggin 	nd->depth = 0;
14785b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
14795b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
148073d049a4SAl Viro 		if (*name) {
14815b6ca027SAl Viro 			if (!inode->i_op->lookup)
14825b6ca027SAl Viro 				return -ENOTDIR;
14835b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
14845b6ca027SAl Viro 			if (retval)
14855b6ca027SAl Viro 				return retval;
148673d049a4SAl Viro 		}
14875b6ca027SAl Viro 		nd->path = nd->root;
14885b6ca027SAl Viro 		nd->inode = inode;
14895b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
14905b6ca027SAl Viro 			br_read_lock(vfsmount_lock);
14915b6ca027SAl Viro 			rcu_read_lock();
14925b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
14935b6ca027SAl Viro 		} else {
14945b6ca027SAl Viro 			path_get(&nd->path);
14955b6ca027SAl Viro 		}
14965b6ca027SAl Viro 		return 0;
14975b6ca027SAl Viro 	}
14985b6ca027SAl Viro 
149931e6b01fSNick Piggin 	nd->root.mnt = NULL;
150031e6b01fSNick Piggin 
150131e6b01fSNick Piggin 	if (*name=='/') {
1502e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
150331e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
150431e6b01fSNick Piggin 			rcu_read_lock();
1505e41f7d4eSAl Viro 			set_root_rcu(nd);
1506e41f7d4eSAl Viro 		} else {
1507e41f7d4eSAl Viro 			set_root(nd);
1508e41f7d4eSAl Viro 			path_get(&nd->root);
1509e41f7d4eSAl Viro 		}
151031e6b01fSNick Piggin 		nd->path = nd->root;
151131e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1512e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
151331e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1514c28cc364SNick Piggin 			unsigned seq;
151531e6b01fSNick Piggin 
151631e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
151731e6b01fSNick Piggin 			rcu_read_lock();
151831e6b01fSNick Piggin 
1519c28cc364SNick Piggin 			do {
1520c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
152131e6b01fSNick Piggin 				nd->path = fs->pwd;
1522c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1523c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1524e41f7d4eSAl Viro 		} else {
1525e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1526e41f7d4eSAl Viro 		}
152731e6b01fSNick Piggin 	} else {
152831e6b01fSNick Piggin 		struct dentry *dentry;
152931e6b01fSNick Piggin 
15301abf0c71SAl Viro 		file = fget_raw_light(dfd, &fput_needed);
153131e6b01fSNick Piggin 		retval = -EBADF;
153231e6b01fSNick Piggin 		if (!file)
153331e6b01fSNick Piggin 			goto out_fail;
153431e6b01fSNick Piggin 
153531e6b01fSNick Piggin 		dentry = file->f_path.dentry;
153631e6b01fSNick Piggin 
1537f52e0c11SAl Viro 		if (*name) {
153831e6b01fSNick Piggin 			retval = -ENOTDIR;
153931e6b01fSNick Piggin 			if (!S_ISDIR(dentry->d_inode->i_mode))
154031e6b01fSNick Piggin 				goto fput_fail;
154131e6b01fSNick Piggin 
15424ad5abb3SAl Viro 			retval = inode_permission(dentry->d_inode, MAY_EXEC);
154331e6b01fSNick Piggin 			if (retval)
154431e6b01fSNick Piggin 				goto fput_fail;
1545f52e0c11SAl Viro 		}
154631e6b01fSNick Piggin 
154731e6b01fSNick Piggin 		nd->path = file->f_path;
1548e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
154931e6b01fSNick Piggin 			if (fput_needed)
155070e9b357SAl Viro 				*fp = file;
1551c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
155231e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
155331e6b01fSNick Piggin 			rcu_read_lock();
15545590ff0dSUlrich Drepper 		} else {
15555dd784d0SJan Blunck 			path_get(&file->f_path);
15565590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
15571da177e4SLinus Torvalds 		}
1558e41f7d4eSAl Viro 	}
1559e41f7d4eSAl Viro 
156031e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
15619b4a9b14SAl Viro 	return 0;
15622dfdd266SJosef 'Jeff' Sipek 
15639b4a9b14SAl Viro fput_fail:
15649b4a9b14SAl Viro 	fput_light(file, fput_needed);
15659b4a9b14SAl Viro out_fail:
15669b4a9b14SAl Viro 	return retval;
15679b4a9b14SAl Viro }
15689b4a9b14SAl Viro 
1569bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1570bd92d7feSAl Viro {
1571bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1572bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1573bd92d7feSAl Viro 
1574bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1575bd92d7feSAl Viro 	return walk_component(nd, path, &nd->last, nd->last_type,
1576bd92d7feSAl Viro 					nd->flags & LOOKUP_FOLLOW);
1577bd92d7feSAl Viro }
1578bd92d7feSAl Viro 
15799b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1580ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
15819b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
15829b4a9b14SAl Viro {
158370e9b357SAl Viro 	struct file *base = NULL;
1584bd92d7feSAl Viro 	struct path path;
1585bd92d7feSAl Viro 	int err;
158631e6b01fSNick Piggin 
158731e6b01fSNick Piggin 	/*
158831e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
158931e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
159031e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
159131e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
159231e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
159331e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
159431e6b01fSNick Piggin 	 * analogue, foo_rcu().
159531e6b01fSNick Piggin 	 *
159631e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
159731e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
159831e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
159931e6b01fSNick Piggin 	 * be able to complete).
160031e6b01fSNick Piggin 	 */
1601bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1602ee0827cdSAl Viro 
1603bd92d7feSAl Viro 	if (unlikely(err))
1604bd92d7feSAl Viro 		return err;
1605ee0827cdSAl Viro 
1606ee0827cdSAl Viro 	current->total_link_count = 0;
1607bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1608bd92d7feSAl Viro 
1609bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1610bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1611bd92d7feSAl Viro 		while (err > 0) {
1612bd92d7feSAl Viro 			void *cookie;
1613bd92d7feSAl Viro 			struct path link = path;
1614bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1615574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
1616bd92d7feSAl Viro 			if (!err)
1617bd92d7feSAl Viro 				err = lookup_last(nd, &path);
1618574197e0SAl Viro 			put_link(nd, &link, cookie);
1619bd92d7feSAl Viro 		}
1620bd92d7feSAl Viro 	}
1621ee0827cdSAl Viro 
16229f1fafeeSAl Viro 	if (!err)
16239f1fafeeSAl Viro 		err = complete_walk(nd);
1624bd92d7feSAl Viro 
1625bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1626bd92d7feSAl Viro 		if (!nd->inode->i_op->lookup) {
1627bd92d7feSAl Viro 			path_put(&nd->path);
1628bd23a539SAl Viro 			err = -ENOTDIR;
1629bd92d7feSAl Viro 		}
1630bd92d7feSAl Viro 	}
163116c2cd71SAl Viro 
163270e9b357SAl Viro 	if (base)
163370e9b357SAl Viro 		fput(base);
1634ee0827cdSAl Viro 
16355b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
163631e6b01fSNick Piggin 		path_put(&nd->root);
163731e6b01fSNick Piggin 		nd->root.mnt = NULL;
163831e6b01fSNick Piggin 	}
1639bd92d7feSAl Viro 	return err;
164031e6b01fSNick Piggin }
164131e6b01fSNick Piggin 
1642ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1643ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1644ee0827cdSAl Viro {
1645ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1646ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1647ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1648ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1649ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1650ee0827cdSAl Viro 
165131e6b01fSNick Piggin 	if (likely(!retval)) {
165231e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
165331e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
165431e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
165531e6b01fSNick Piggin 		}
165631e6b01fSNick Piggin 	}
1657170aa3d0SUlrich Drepper 	return retval;
16581da177e4SLinus Torvalds }
16591da177e4SLinus Torvalds 
1660c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd)
16615590ff0dSUlrich Drepper {
1662c9c6cac0SAl Viro 	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
16635590ff0dSUlrich Drepper }
16645590ff0dSUlrich Drepper 
1665d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1666d1811465SAl Viro {
1667d1811465SAl Viro 	struct nameidata nd;
1668d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1669d1811465SAl Viro 	if (!res)
1670d1811465SAl Viro 		*path = nd.path;
1671d1811465SAl Viro 	return res;
1672d1811465SAl Viro }
1673d1811465SAl Viro 
167416f18200SJosef 'Jeff' Sipek /**
167516f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
167616f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
167716f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
167816f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
167916f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
1680e0a01249SAl Viro  * @path: pointer to struct path to fill
168116f18200SJosef 'Jeff' Sipek  */
168216f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
168316f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
1684e0a01249SAl Viro 		    struct path *path)
168516f18200SJosef 'Jeff' Sipek {
1686e0a01249SAl Viro 	struct nameidata nd;
1687e0a01249SAl Viro 	int err;
1688e0a01249SAl Viro 	nd.root.dentry = dentry;
1689e0a01249SAl Viro 	nd.root.mnt = mnt;
1690e0a01249SAl Viro 	BUG_ON(flags & LOOKUP_PARENT);
16915b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1692e0a01249SAl Viro 	err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1693e0a01249SAl Viro 	if (!err)
1694e0a01249SAl Viro 		*path = nd.path;
1695e0a01249SAl Viro 	return err;
169616f18200SJosef 'Jeff' Sipek }
169716f18200SJosef 'Jeff' Sipek 
1698eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1699eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
17001da177e4SLinus Torvalds {
170181fca444SChristoph Hellwig 	struct inode *inode = base->d_inode;
17021da177e4SLinus Torvalds 	struct dentry *dentry;
17031da177e4SLinus Torvalds 	int err;
17041da177e4SLinus Torvalds 
17054ad5abb3SAl Viro 	err = inode_permission(inode, MAY_EXEC);
170681fca444SChristoph Hellwig 	if (err)
170781fca444SChristoph Hellwig 		return ERR_PTR(err);
17081da177e4SLinus Torvalds 
17091da177e4SLinus Torvalds 	/*
1710b04f784eSNick Piggin 	 * Don't bother with __d_lookup: callers are for creat as
1711b04f784eSNick Piggin 	 * well as unlink, so a lot of the time it would cost
1712b04f784eSNick Piggin 	 * a double lookup.
17136e6b1bd1SAl Viro 	 */
17146e6b1bd1SAl Viro 	dentry = d_lookup(base, name);
17156e6b1bd1SAl Viro 
171644396f4bSJosef Bacik 	if (dentry && d_need_lookup(dentry)) {
171744396f4bSJosef Bacik 		/*
171844396f4bSJosef Bacik 		 * __lookup_hash is called with the parent dir's i_mutex already
171944396f4bSJosef Bacik 		 * held, so we are good to go here.
172044396f4bSJosef Bacik 		 */
172144396f4bSJosef Bacik 		dentry = d_inode_lookup(base, dentry, nd);
172244396f4bSJosef Bacik 		if (IS_ERR(dentry))
172344396f4bSJosef Bacik 			return dentry;
172444396f4bSJosef Bacik 	}
172544396f4bSJosef Bacik 
1726d2d9e9fbSAl Viro 	if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1727d2d9e9fbSAl Viro 		int status = d_revalidate(dentry, nd);
1728d2d9e9fbSAl Viro 		if (unlikely(status <= 0)) {
1729d2d9e9fbSAl Viro 			/*
1730d2d9e9fbSAl Viro 			 * The dentry failed validation.
1731d2d9e9fbSAl Viro 			 * If d_revalidate returned 0 attempt to invalidate
1732d2d9e9fbSAl Viro 			 * the dentry otherwise d_revalidate is asking us
1733d2d9e9fbSAl Viro 			 * to return a fail status.
1734d2d9e9fbSAl Viro 			 */
1735d2d9e9fbSAl Viro 			if (status < 0) {
1736d2d9e9fbSAl Viro 				dput(dentry);
1737d2d9e9fbSAl Viro 				return ERR_PTR(status);
1738d2d9e9fbSAl Viro 			} else if (!d_invalidate(dentry)) {
1739d2d9e9fbSAl Viro 				dput(dentry);
1740d2d9e9fbSAl Viro 				dentry = NULL;
1741d2d9e9fbSAl Viro 			}
1742d2d9e9fbSAl Viro 		}
1743d2d9e9fbSAl Viro 	}
17446e6b1bd1SAl Viro 
17451da177e4SLinus Torvalds 	if (!dentry)
1746baa03890SNick Piggin 		dentry = d_alloc_and_lookup(base, name, nd);
17475a202bcdSAl Viro 
17481da177e4SLinus Torvalds 	return dentry;
17491da177e4SLinus Torvalds }
17501da177e4SLinus Torvalds 
1751057f6c01SJames Morris /*
1752057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1753057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1754057f6c01SJames Morris  * SMP-safe.
1755057f6c01SJames Morris  */
1756a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
17571da177e4SLinus Torvalds {
17584ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
17591da177e4SLinus Torvalds }
17601da177e4SLinus Torvalds 
1761eead1911SChristoph Hellwig /**
1762a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1763eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1764eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1765eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1766eead1911SChristoph Hellwig  *
1767a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1768a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1769eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1770eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1771eead1911SChristoph Hellwig  */
1772057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1773057f6c01SJames Morris {
1774057f6c01SJames Morris 	struct qstr this;
17756a96ba54SAl Viro 	unsigned long hash;
17766a96ba54SAl Viro 	unsigned int c;
1777057f6c01SJames Morris 
17782f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
17792f9092e1SDavid Woodhouse 
17806a96ba54SAl Viro 	this.name = name;
17816a96ba54SAl Viro 	this.len = len;
17826a96ba54SAl Viro 	if (!len)
17836a96ba54SAl Viro 		return ERR_PTR(-EACCES);
17846a96ba54SAl Viro 
17856a96ba54SAl Viro 	hash = init_name_hash();
17866a96ba54SAl Viro 	while (len--) {
17876a96ba54SAl Viro 		c = *(const unsigned char *)name++;
17886a96ba54SAl Viro 		if (c == '/' || c == '\0')
17896a96ba54SAl Viro 			return ERR_PTR(-EACCES);
17906a96ba54SAl Viro 		hash = partial_name_hash(c, hash);
17916a96ba54SAl Viro 	}
17926a96ba54SAl Viro 	this.hash = end_name_hash(hash);
17935a202bcdSAl Viro 	/*
17945a202bcdSAl Viro 	 * See if the low-level filesystem might want
17955a202bcdSAl Viro 	 * to use its own hash..
17965a202bcdSAl Viro 	 */
17975a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
17985a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
17995a202bcdSAl Viro 		if (err < 0)
18005a202bcdSAl Viro 			return ERR_PTR(err);
18015a202bcdSAl Viro 	}
1802eead1911SChristoph Hellwig 
180349705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1804057f6c01SJames Morris }
1805057f6c01SJames Morris 
18061fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
18071fa1e7f6SAndy Whitcroft 		 struct path *path, int *empty)
18081da177e4SLinus Torvalds {
18092d8f3038SAl Viro 	struct nameidata nd;
18101fa1e7f6SAndy Whitcroft 	char *tmp = getname_flags(name, flags, empty);
18111da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
18121da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
18132d8f3038SAl Viro 
18142d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
18152d8f3038SAl Viro 
18162d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
18171da177e4SLinus Torvalds 		putname(tmp);
18182d8f3038SAl Viro 		if (!err)
18192d8f3038SAl Viro 			*path = nd.path;
18201da177e4SLinus Torvalds 	}
18211da177e4SLinus Torvalds 	return err;
18221da177e4SLinus Torvalds }
18231da177e4SLinus Torvalds 
18241fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags,
18251fa1e7f6SAndy Whitcroft 		 struct path *path)
18261fa1e7f6SAndy Whitcroft {
18271fa1e7f6SAndy Whitcroft 	return user_path_at_empty(dfd, name, flags, path, 0);
18281fa1e7f6SAndy Whitcroft }
18291fa1e7f6SAndy Whitcroft 
18302ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
18312ad94ae6SAl Viro 			struct nameidata *nd, char **name)
18322ad94ae6SAl Viro {
18332ad94ae6SAl Viro 	char *s = getname(path);
18342ad94ae6SAl Viro 	int error;
18352ad94ae6SAl Viro 
18362ad94ae6SAl Viro 	if (IS_ERR(s))
18372ad94ae6SAl Viro 		return PTR_ERR(s);
18382ad94ae6SAl Viro 
18392ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
18402ad94ae6SAl Viro 	if (error)
18412ad94ae6SAl Viro 		putname(s);
18422ad94ae6SAl Viro 	else
18432ad94ae6SAl Viro 		*name = s;
18442ad94ae6SAl Viro 
18452ad94ae6SAl Viro 	return error;
18462ad94ae6SAl Viro }
18472ad94ae6SAl Viro 
18481da177e4SLinus Torvalds /*
18491da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
18501da177e4SLinus Torvalds  * minimal.
18511da177e4SLinus Torvalds  */
18521da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
18531da177e4SLinus Torvalds {
1854da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1855da9592edSDavid Howells 
18561da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
18571da177e4SLinus Torvalds 		return 0;
1858e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
1859e795b717SSerge E. Hallyn 		goto other_userns;
1860da9592edSDavid Howells 	if (inode->i_uid == fsuid)
18611da177e4SLinus Torvalds 		return 0;
1862da9592edSDavid Howells 	if (dir->i_uid == fsuid)
18631da177e4SLinus Torvalds 		return 0;
1864e795b717SSerge E. Hallyn 
1865e795b717SSerge E. Hallyn other_userns:
1866e795b717SSerge E. Hallyn 	return !ns_capable(inode_userns(inode), CAP_FOWNER);
18671da177e4SLinus Torvalds }
18681da177e4SLinus Torvalds 
18691da177e4SLinus Torvalds /*
18701da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
18711da177e4SLinus Torvalds  *  whether the type of victim is right.
18721da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
18731da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
18741da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
18751da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
18761da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
18771da177e4SLinus Torvalds  *	a. be owner of dir, or
18781da177e4SLinus Torvalds  *	b. be owner of victim, or
18791da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
18801da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
18811da177e4SLinus Torvalds  *     links pointing to it.
18821da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
18831da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
18841da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
18851da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
18861da177e4SLinus Torvalds  *     nfs_async_unlink().
18871da177e4SLinus Torvalds  */
1888858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
18891da177e4SLinus Torvalds {
18901da177e4SLinus Torvalds 	int error;
18911da177e4SLinus Torvalds 
18921da177e4SLinus Torvalds 	if (!victim->d_inode)
18931da177e4SLinus Torvalds 		return -ENOENT;
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1896cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
18971da177e4SLinus Torvalds 
1898f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
18991da177e4SLinus Torvalds 	if (error)
19001da177e4SLinus Torvalds 		return error;
19011da177e4SLinus Torvalds 	if (IS_APPEND(dir))
19021da177e4SLinus Torvalds 		return -EPERM;
19031da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1904f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
19051da177e4SLinus Torvalds 		return -EPERM;
19061da177e4SLinus Torvalds 	if (isdir) {
19071da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
19081da177e4SLinus Torvalds 			return -ENOTDIR;
19091da177e4SLinus Torvalds 		if (IS_ROOT(victim))
19101da177e4SLinus Torvalds 			return -EBUSY;
19111da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
19121da177e4SLinus Torvalds 		return -EISDIR;
19131da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
19141da177e4SLinus Torvalds 		return -ENOENT;
19151da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
19161da177e4SLinus Torvalds 		return -EBUSY;
19171da177e4SLinus Torvalds 	return 0;
19181da177e4SLinus Torvalds }
19191da177e4SLinus Torvalds 
19201da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
19211da177e4SLinus Torvalds  *  dir.
19221da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
19231da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
19241da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
19251da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
19261da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
19271da177e4SLinus Torvalds  */
1928a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
19291da177e4SLinus Torvalds {
19301da177e4SLinus Torvalds 	if (child->d_inode)
19311da177e4SLinus Torvalds 		return -EEXIST;
19321da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
19331da177e4SLinus Torvalds 		return -ENOENT;
1934f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
19351da177e4SLinus Torvalds }
19361da177e4SLinus Torvalds 
19371da177e4SLinus Torvalds /*
19381da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
19391da177e4SLinus Torvalds  */
19401da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
19411da177e4SLinus Torvalds {
19421da177e4SLinus Torvalds 	struct dentry *p;
19431da177e4SLinus Torvalds 
19441da177e4SLinus Torvalds 	if (p1 == p2) {
1945f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
19461da177e4SLinus Torvalds 		return NULL;
19471da177e4SLinus Torvalds 	}
19481da177e4SLinus Torvalds 
1949a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19501da177e4SLinus Torvalds 
1951e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1952e2761a11SOGAWA Hirofumi 	if (p) {
1953f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1954f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
19551da177e4SLinus Torvalds 		return p;
19561da177e4SLinus Torvalds 	}
19571da177e4SLinus Torvalds 
1958e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1959e2761a11SOGAWA Hirofumi 	if (p) {
1960f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1961f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19621da177e4SLinus Torvalds 		return p;
19631da177e4SLinus Torvalds 	}
19641da177e4SLinus Torvalds 
1965f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1966f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19671da177e4SLinus Torvalds 	return NULL;
19681da177e4SLinus Torvalds }
19691da177e4SLinus Torvalds 
19701da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
19711da177e4SLinus Torvalds {
19721b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
19731da177e4SLinus Torvalds 	if (p1 != p2) {
19741b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1975a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19761da177e4SLinus Torvalds 	}
19771da177e4SLinus Torvalds }
19781da177e4SLinus Torvalds 
19791da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
19801da177e4SLinus Torvalds 		struct nameidata *nd)
19811da177e4SLinus Torvalds {
1982a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds 	if (error)
19851da177e4SLinus Torvalds 		return error;
19861da177e4SLinus Torvalds 
1987acfa4380SAl Viro 	if (!dir->i_op->create)
19881da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
19891da177e4SLinus Torvalds 	mode &= S_IALLUGO;
19901da177e4SLinus Torvalds 	mode |= S_IFREG;
19911da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
19921da177e4SLinus Torvalds 	if (error)
19931da177e4SLinus Torvalds 		return error;
19941da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1995a74574aaSStephen Smalley 	if (!error)
1996f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
19971da177e4SLinus Torvalds 	return error;
19981da177e4SLinus Torvalds }
19991da177e4SLinus Torvalds 
200073d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
20011da177e4SLinus Torvalds {
20023fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
20031da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
20041da177e4SLinus Torvalds 	int error;
20051da177e4SLinus Torvalds 
2006bcda7652SAl Viro 	/* O_PATH? */
2007bcda7652SAl Viro 	if (!acc_mode)
2008bcda7652SAl Viro 		return 0;
2009bcda7652SAl Viro 
20101da177e4SLinus Torvalds 	if (!inode)
20111da177e4SLinus Torvalds 		return -ENOENT;
20121da177e4SLinus Torvalds 
2013c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2014c8fe8f30SChristoph Hellwig 	case S_IFLNK:
20151da177e4SLinus Torvalds 		return -ELOOP;
2016c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2017c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
20181da177e4SLinus Torvalds 			return -EISDIR;
2019c8fe8f30SChristoph Hellwig 		break;
2020c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2021c8fe8f30SChristoph Hellwig 	case S_IFCHR:
20223fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
20231da177e4SLinus Torvalds 			return -EACCES;
2024c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
2025c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2026c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
20271da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2028c8fe8f30SChristoph Hellwig 		break;
20294a3fd211SDave Hansen 	}
2030b41572e9SDave Hansen 
20313fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2032b41572e9SDave Hansen 	if (error)
2033b41572e9SDave Hansen 		return error;
20346146f0d5SMimi Zohar 
20351da177e4SLinus Torvalds 	/*
20361da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
20371da177e4SLinus Torvalds 	 */
20381da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
20398737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
20407715b521SAl Viro 			return -EPERM;
20411da177e4SLinus Torvalds 		if (flag & O_TRUNC)
20427715b521SAl Viro 			return -EPERM;
20431da177e4SLinus Torvalds 	}
20441da177e4SLinus Torvalds 
20451da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
20462e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
20477715b521SAl Viro 		return -EPERM;
20481da177e4SLinus Torvalds 
2049f3c7691eSJ. Bruce Fields 	return 0;
20507715b521SAl Viro }
20517715b521SAl Viro 
2052e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
20537715b521SAl Viro {
2054e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
20557715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
20567715b521SAl Viro 	int error = get_write_access(inode);
20571da177e4SLinus Torvalds 	if (error)
20587715b521SAl Viro 		return error;
20591da177e4SLinus Torvalds 	/*
20601da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
20611da177e4SLinus Torvalds 	 */
20621da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2063be6d3e56SKentaro Takeda 	if (!error)
2064ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
20651da177e4SLinus Torvalds 	if (!error) {
20667715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2067d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2068e1181ee6SJeff Layton 				    filp);
20691da177e4SLinus Torvalds 	}
20701da177e4SLinus Torvalds 	put_write_access(inode);
2071acd0c935SMimi Zohar 	return error;
20721da177e4SLinus Torvalds }
20731da177e4SLinus Torvalds 
2074d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2075d57999e1SDave Hansen {
20768a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
20778a5e929dSAl Viro 		flag--;
2078d57999e1SDave Hansen 	return flag;
2079d57999e1SDave Hansen }
2080d57999e1SDave Hansen 
208131e6b01fSNick Piggin /*
2082fe2d35ffSAl Viro  * Handle the last step of open()
208331e6b01fSNick Piggin  */
2084fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
2085c3e380b0SAl Viro 			    const struct open_flags *op, const char *pathname)
2086fb1cc555SAl Viro {
2087a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
20886c0d46c4SAl Viro 	struct dentry *dentry;
2089ca344a89SAl Viro 	int open_flag = op->open_flag;
20906c0d46c4SAl Viro 	int will_truncate = open_flag & O_TRUNC;
2091ca344a89SAl Viro 	int want_write = 0;
2092bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2093fb1cc555SAl Viro 	struct file *filp;
209416c2cd71SAl Viro 	int error;
2095fb1cc555SAl Viro 
2096c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2097c3e380b0SAl Viro 	nd->flags |= op->intent;
2098c3e380b0SAl Viro 
20991f36f774SAl Viro 	switch (nd->last_type) {
21001f36f774SAl Viro 	case LAST_DOTDOT:
2101176306f5SNeil Brown 	case LAST_DOT:
2102fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2103fe2d35ffSAl Viro 		if (error)
2104fe2d35ffSAl Viro 			return ERR_PTR(error);
21051f36f774SAl Viro 		/* fallthrough */
21061f36f774SAl Viro 	case LAST_ROOT:
21079f1fafeeSAl Viro 		error = complete_walk(nd);
210816c2cd71SAl Viro 		if (error)
21099f1fafeeSAl Viro 			return ERR_PTR(error);
2110fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2111ca344a89SAl Viro 		if (open_flag & O_CREAT) {
211216c2cd71SAl Viro 			error = -EISDIR;
21131f36f774SAl Viro 			goto exit;
2114fe2d35ffSAl Viro 		}
2115fe2d35ffSAl Viro 		goto ok;
21161f36f774SAl Viro 	case LAST_BIND:
21179f1fafeeSAl Viro 		error = complete_walk(nd);
211816c2cd71SAl Viro 		if (error)
21199f1fafeeSAl Viro 			return ERR_PTR(error);
21201f36f774SAl Viro 		audit_inode(pathname, dir);
21211f36f774SAl Viro 		goto ok;
21221f36f774SAl Viro 	}
2123a2c36b45SAl Viro 
2124ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2125bcda7652SAl Viro 		int symlink_ok = 0;
2126fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2127fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2128bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2129bcda7652SAl Viro 			symlink_ok = 1;
2130fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2131ce57dfc1SAl Viro 		error = walk_component(nd, path, &nd->last, LAST_NORM,
2132ce57dfc1SAl Viro 					!symlink_ok);
2133ce57dfc1SAl Viro 		if (error < 0)
2134fe2d35ffSAl Viro 			return ERR_PTR(error);
2135ce57dfc1SAl Viro 		if (error) /* symlink */
2136fe2d35ffSAl Viro 			return NULL;
2137fe2d35ffSAl Viro 		/* sayonara */
21389f1fafeeSAl Viro 		error = complete_walk(nd);
21399f1fafeeSAl Viro 		if (error)
2140fe2d35ffSAl Viro 			return ERR_PTR(-ECHILD);
2141fe2d35ffSAl Viro 
2142fe2d35ffSAl Viro 		error = -ENOTDIR;
2143fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_DIRECTORY) {
2144ce57dfc1SAl Viro 			if (!nd->inode->i_op->lookup)
2145fe2d35ffSAl Viro 				goto exit;
2146fe2d35ffSAl Viro 		}
2147fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2148fe2d35ffSAl Viro 		goto ok;
2149fe2d35ffSAl Viro 	}
2150fe2d35ffSAl Viro 
2151fe2d35ffSAl Viro 	/* create side of things */
2152a3fbbde7SAl Viro 	/*
2153a3fbbde7SAl Viro 	 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED has been
2154a3fbbde7SAl Viro 	 * cleared when we got to the last component we are about to look up
2155a3fbbde7SAl Viro 	 */
21569f1fafeeSAl Viro 	error = complete_walk(nd);
21579f1fafeeSAl Viro 	if (error)
21589f1fafeeSAl Viro 		return ERR_PTR(error);
2159fe2d35ffSAl Viro 
2160fe2d35ffSAl Viro 	audit_inode(pathname, dir);
216116c2cd71SAl Viro 	error = -EISDIR;
21621f36f774SAl Viro 	/* trailing slashes? */
216331e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
21641f36f774SAl Viro 		goto exit;
21651f36f774SAl Viro 
2166a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2167a1e28038SAl Viro 
21686c0d46c4SAl Viro 	dentry = lookup_hash(nd);
21696c0d46c4SAl Viro 	error = PTR_ERR(dentry);
21706c0d46c4SAl Viro 	if (IS_ERR(dentry)) {
2171fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2172fb1cc555SAl Viro 		goto exit;
2173fb1cc555SAl Viro 	}
2174fb1cc555SAl Viro 
21756c0d46c4SAl Viro 	path->dentry = dentry;
21766c0d46c4SAl Viro 	path->mnt = nd->path.mnt;
21776c0d46c4SAl Viro 
2178fb1cc555SAl Viro 	/* Negative dentry, just create the file */
21796c0d46c4SAl Viro 	if (!dentry->d_inode) {
21806c0d46c4SAl Viro 		int mode = op->mode;
21816c0d46c4SAl Viro 		if (!IS_POSIXACL(dir->d_inode))
21826c0d46c4SAl Viro 			mode &= ~current_umask();
2183fb1cc555SAl Viro 		/*
2184fb1cc555SAl Viro 		 * This write is needed to ensure that a
21856c0d46c4SAl Viro 		 * rw->ro transition does not occur between
2186fb1cc555SAl Viro 		 * the time when the file is created and when
2187fb1cc555SAl Viro 		 * a permanent write count is taken through
2188fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2189fb1cc555SAl Viro 		 */
2190fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2191fb1cc555SAl Viro 		if (error)
2192fb1cc555SAl Viro 			goto exit_mutex_unlock;
2193ca344a89SAl Viro 		want_write = 1;
21949b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2195ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
21966c0d46c4SAl Viro 		will_truncate = 0;
2197bcda7652SAl Viro 		acc_mode = MAY_OPEN;
21986c0d46c4SAl Viro 		error = security_path_mknod(&nd->path, dentry, mode, 0);
21996c0d46c4SAl Viro 		if (error)
22006c0d46c4SAl Viro 			goto exit_mutex_unlock;
22016c0d46c4SAl Viro 		error = vfs_create(dir->d_inode, dentry, mode, nd);
22026c0d46c4SAl Viro 		if (error)
22036c0d46c4SAl Viro 			goto exit_mutex_unlock;
22046c0d46c4SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
22056c0d46c4SAl Viro 		dput(nd->path.dentry);
22066c0d46c4SAl Viro 		nd->path.dentry = dentry;
2207ca344a89SAl Viro 		goto common;
2208fb1cc555SAl Viro 	}
2209fb1cc555SAl Viro 
2210fb1cc555SAl Viro 	/*
2211fb1cc555SAl Viro 	 * It already exists.
2212fb1cc555SAl Viro 	 */
2213fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2214fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2215fb1cc555SAl Viro 
2216fb1cc555SAl Viro 	error = -EEXIST;
2217ca344a89SAl Viro 	if (open_flag & O_EXCL)
2218fb1cc555SAl Viro 		goto exit_dput;
2219fb1cc555SAl Viro 
22209875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
22219875cf80SDavid Howells 	if (error < 0)
2222fb1cc555SAl Viro 		goto exit_dput;
2223fb1cc555SAl Viro 
2224a3fbbde7SAl Viro 	if (error)
2225a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
2226a3fbbde7SAl Viro 
2227fb1cc555SAl Viro 	error = -ENOENT;
2228fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2229fb1cc555SAl Viro 		goto exit_dput;
22309e67f361SAl Viro 
22319e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2232fb1cc555SAl Viro 		return NULL;
2233fb1cc555SAl Viro 
2234fb1cc555SAl Viro 	path_to_nameidata(path, nd);
223531e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2236a3fbbde7SAl Viro 	/* Why this, you ask?  _Now_ we might have grown LOOKUP_JUMPED... */
2237a3fbbde7SAl Viro 	error = complete_walk(nd);
2238a3fbbde7SAl Viro 	if (error)
2239a3fbbde7SAl Viro 		goto exit;
2240fb1cc555SAl Viro 	error = -EISDIR;
224131e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2242fb1cc555SAl Viro 		goto exit;
224367ee3ad2SAl Viro ok:
22446c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
22456c0d46c4SAl Viro 		will_truncate = 0;
22466c0d46c4SAl Viro 
22470f9d1a10SAl Viro 	if (will_truncate) {
22480f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
22490f9d1a10SAl Viro 		if (error)
22500f9d1a10SAl Viro 			goto exit;
2251ca344a89SAl Viro 		want_write = 1;
22520f9d1a10SAl Viro 	}
2253ca344a89SAl Viro common:
2254bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2255ca344a89SAl Viro 	if (error)
22560f9d1a10SAl Viro 		goto exit;
22570f9d1a10SAl Viro 	filp = nameidata_to_filp(nd);
22580f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
22590f9d1a10SAl Viro 		error = ima_file_check(filp, op->acc_mode);
22600f9d1a10SAl Viro 		if (error) {
22610f9d1a10SAl Viro 			fput(filp);
22620f9d1a10SAl Viro 			filp = ERR_PTR(error);
22630f9d1a10SAl Viro 		}
22640f9d1a10SAl Viro 	}
22650f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
22660f9d1a10SAl Viro 		if (will_truncate) {
22670f9d1a10SAl Viro 			error = handle_truncate(filp);
22680f9d1a10SAl Viro 			if (error) {
22690f9d1a10SAl Viro 				fput(filp);
22700f9d1a10SAl Viro 				filp = ERR_PTR(error);
22710f9d1a10SAl Viro 			}
22720f9d1a10SAl Viro 		}
22730f9d1a10SAl Viro 	}
2274ca344a89SAl Viro out:
2275ca344a89SAl Viro 	if (want_write)
22760f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
22770f9d1a10SAl Viro 	path_put(&nd->path);
2278fb1cc555SAl Viro 	return filp;
2279fb1cc555SAl Viro 
2280fb1cc555SAl Viro exit_mutex_unlock:
2281fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2282fb1cc555SAl Viro exit_dput:
2283fb1cc555SAl Viro 	path_put_conditional(path, nd);
2284fb1cc555SAl Viro exit:
2285ca344a89SAl Viro 	filp = ERR_PTR(error);
2286ca344a89SAl Viro 	goto out;
2287fb1cc555SAl Viro }
2288fb1cc555SAl Viro 
228913aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
229073d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
22911da177e4SLinus Torvalds {
2292fe2d35ffSAl Viro 	struct file *base = NULL;
22934a3fd211SDave Hansen 	struct file *filp;
22949850c056SAl Viro 	struct path path;
229513aab428SAl Viro 	int error;
229631e6b01fSNick Piggin 
229731e6b01fSNick Piggin 	filp = get_empty_filp();
229831e6b01fSNick Piggin 	if (!filp)
229931e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
230031e6b01fSNick Piggin 
230147c805dcSAl Viro 	filp->f_flags = op->open_flag;
230273d049a4SAl Viro 	nd->intent.open.file = filp;
230373d049a4SAl Viro 	nd->intent.open.flags = open_to_namei_flags(op->open_flag);
230473d049a4SAl Viro 	nd->intent.open.create_mode = op->mode;
230531e6b01fSNick Piggin 
230673d049a4SAl Viro 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
230731e6b01fSNick Piggin 	if (unlikely(error))
230813aab428SAl Viro 		goto out_filp;
230931e6b01fSNick Piggin 
2310fe2d35ffSAl Viro 	current->total_link_count = 0;
231173d049a4SAl Viro 	error = link_path_walk(pathname, nd);
231231e6b01fSNick Piggin 	if (unlikely(error))
231331e6b01fSNick Piggin 		goto out_filp;
23141da177e4SLinus Torvalds 
231573d049a4SAl Viro 	filp = do_last(nd, &path, op, pathname);
2316806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
23177b9337aaSNick Piggin 		struct path link = path;
2318def4af30SAl Viro 		void *cookie;
2319574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
232073d049a4SAl Viro 			path_put_conditional(&path, nd);
232173d049a4SAl Viro 			path_put(&nd->path);
232240b39136SAl Viro 			filp = ERR_PTR(-ELOOP);
232340b39136SAl Viro 			break;
232440b39136SAl Viro 		}
232573d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
232673d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2327574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
2328c3e380b0SAl Viro 		if (unlikely(error))
2329f1afe9efSAl Viro 			filp = ERR_PTR(error);
2330c3e380b0SAl Viro 		else
233173d049a4SAl Viro 			filp = do_last(nd, &path, op, pathname);
2332574197e0SAl Viro 		put_link(nd, &link, cookie);
2333806b681cSAl Viro 	}
233410fa8e62SAl Viro out:
233573d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
233673d049a4SAl Viro 		path_put(&nd->root);
2337fe2d35ffSAl Viro 	if (base)
2338fe2d35ffSAl Viro 		fput(base);
233973d049a4SAl Viro 	release_open_intent(nd);
234010fa8e62SAl Viro 	return filp;
23411da177e4SLinus Torvalds 
234231e6b01fSNick Piggin out_filp:
234310fa8e62SAl Viro 	filp = ERR_PTR(error);
234410fa8e62SAl Viro 	goto out;
2345de459215SKirill Korotaev }
23461da177e4SLinus Torvalds 
234713aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
234813aab428SAl Viro 		const struct open_flags *op, int flags)
234913aab428SAl Viro {
235073d049a4SAl Viro 	struct nameidata nd;
235113aab428SAl Viro 	struct file *filp;
235213aab428SAl Viro 
235373d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
235413aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
235573d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
235613aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
235773d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
235813aab428SAl Viro 	return filp;
235913aab428SAl Viro }
236013aab428SAl Viro 
236173d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
236273d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
236373d049a4SAl Viro {
236473d049a4SAl Viro 	struct nameidata nd;
236573d049a4SAl Viro 	struct file *file;
236673d049a4SAl Viro 
236773d049a4SAl Viro 	nd.root.mnt = mnt;
236873d049a4SAl Viro 	nd.root.dentry = dentry;
236973d049a4SAl Viro 
237073d049a4SAl Viro 	flags |= LOOKUP_ROOT;
237173d049a4SAl Viro 
2372bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
237373d049a4SAl Viro 		return ERR_PTR(-ELOOP);
237473d049a4SAl Viro 
237573d049a4SAl Viro 	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
237673d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
237773d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags);
237873d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
237973d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
238073d049a4SAl Viro 	return file;
238173d049a4SAl Viro }
238273d049a4SAl Viro 
2383ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
23841da177e4SLinus Torvalds {
2385c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
2386ed75e95dSAl Viro 	struct nameidata nd;
2387ed75e95dSAl Viro 	int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2388ed75e95dSAl Viro 	if (error)
2389ed75e95dSAl Viro 		return ERR_PTR(error);
23901da177e4SLinus Torvalds 
2391c663e5d8SChristoph Hellwig 	/*
2392c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2393c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2394c663e5d8SChristoph Hellwig 	 */
2395ed75e95dSAl Viro 	if (nd.last_type != LAST_NORM)
2396ed75e95dSAl Viro 		goto out;
2397ed75e95dSAl Viro 	nd.flags &= ~LOOKUP_PARENT;
2398ed75e95dSAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2399ed75e95dSAl Viro 	nd.intent.open.flags = O_EXCL;
2400c663e5d8SChristoph Hellwig 
2401c663e5d8SChristoph Hellwig 	/*
2402c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2403c663e5d8SChristoph Hellwig 	 */
2404ed75e95dSAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2405ed75e95dSAl Viro 	dentry = lookup_hash(&nd);
24061da177e4SLinus Torvalds 	if (IS_ERR(dentry))
24071da177e4SLinus Torvalds 		goto fail;
2408c663e5d8SChristoph Hellwig 
2409e9baf6e5SAl Viro 	if (dentry->d_inode)
2410e9baf6e5SAl Viro 		goto eexist;
2411c663e5d8SChristoph Hellwig 	/*
2412c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2413c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2414c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2415c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2416c663e5d8SChristoph Hellwig 	 */
2417ed75e95dSAl Viro 	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
24181da177e4SLinus Torvalds 		dput(dentry);
24191da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2420ed75e95dSAl Viro 		goto fail;
2421e9baf6e5SAl Viro 	}
2422ed75e95dSAl Viro 	*path = nd.path;
2423e9baf6e5SAl Viro 	return dentry;
2424e9baf6e5SAl Viro eexist:
2425e9baf6e5SAl Viro 	dput(dentry);
2426e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
24271da177e4SLinus Torvalds fail:
2428dae6ad8fSAl Viro 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2429ed75e95dSAl Viro out:
2430dae6ad8fSAl Viro 	path_put(&nd.path);
2431ed75e95dSAl Viro 	return dentry;
2432dae6ad8fSAl Viro }
2433dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
2434dae6ad8fSAl Viro 
2435dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2436dae6ad8fSAl Viro {
2437dae6ad8fSAl Viro 	char *tmp = getname(pathname);
2438dae6ad8fSAl Viro 	struct dentry *res;
2439dae6ad8fSAl Viro 	if (IS_ERR(tmp))
2440dae6ad8fSAl Viro 		return ERR_CAST(tmp);
2441dae6ad8fSAl Viro 	res = kern_path_create(dfd, tmp, path, is_dir);
2442dae6ad8fSAl Viro 	putname(tmp);
2443dae6ad8fSAl Viro 	return res;
2444dae6ad8fSAl Viro }
2445dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
2446dae6ad8fSAl Viro 
24471da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
24481da177e4SLinus Torvalds {
2449a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
24501da177e4SLinus Torvalds 
24511da177e4SLinus Torvalds 	if (error)
24521da177e4SLinus Torvalds 		return error;
24531da177e4SLinus Torvalds 
2454e795b717SSerge E. Hallyn 	if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2455e795b717SSerge E. Hallyn 	    !ns_capable(inode_userns(dir), CAP_MKNOD))
24561da177e4SLinus Torvalds 		return -EPERM;
24571da177e4SLinus Torvalds 
2458acfa4380SAl Viro 	if (!dir->i_op->mknod)
24591da177e4SLinus Torvalds 		return -EPERM;
24601da177e4SLinus Torvalds 
246108ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
246208ce5f16SSerge E. Hallyn 	if (error)
246308ce5f16SSerge E. Hallyn 		return error;
246408ce5f16SSerge E. Hallyn 
24651da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
24661da177e4SLinus Torvalds 	if (error)
24671da177e4SLinus Torvalds 		return error;
24681da177e4SLinus Torvalds 
24691da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2470a74574aaSStephen Smalley 	if (!error)
2471f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
24721da177e4SLinus Torvalds 	return error;
24731da177e4SLinus Torvalds }
24741da177e4SLinus Torvalds 
2475463c3197SDave Hansen static int may_mknod(mode_t mode)
2476463c3197SDave Hansen {
2477463c3197SDave Hansen 	switch (mode & S_IFMT) {
2478463c3197SDave Hansen 	case S_IFREG:
2479463c3197SDave Hansen 	case S_IFCHR:
2480463c3197SDave Hansen 	case S_IFBLK:
2481463c3197SDave Hansen 	case S_IFIFO:
2482463c3197SDave Hansen 	case S_IFSOCK:
2483463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2484463c3197SDave Hansen 		return 0;
2485463c3197SDave Hansen 	case S_IFDIR:
2486463c3197SDave Hansen 		return -EPERM;
2487463c3197SDave Hansen 	default:
2488463c3197SDave Hansen 		return -EINVAL;
2489463c3197SDave Hansen 	}
2490463c3197SDave Hansen }
2491463c3197SDave Hansen 
24928208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
24932e4d0924SHeiko Carstens 		unsigned, dev)
24941da177e4SLinus Torvalds {
24951da177e4SLinus Torvalds 	struct dentry *dentry;
2496dae6ad8fSAl Viro 	struct path path;
2497dae6ad8fSAl Viro 	int error;
24981da177e4SLinus Torvalds 
24991da177e4SLinus Torvalds 	if (S_ISDIR(mode))
25001da177e4SLinus Torvalds 		return -EPERM;
25011da177e4SLinus Torvalds 
2502dae6ad8fSAl Viro 	dentry = user_path_create(dfd, filename, &path, 0);
2503dae6ad8fSAl Viro 	if (IS_ERR(dentry))
2504dae6ad8fSAl Viro 		return PTR_ERR(dentry);
25052ad94ae6SAl Viro 
2506dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
2507ce3b0f8dSAl Viro 		mode &= ~current_umask();
2508463c3197SDave Hansen 	error = may_mknod(mode);
2509463c3197SDave Hansen 	if (error)
2510463c3197SDave Hansen 		goto out_dput;
2511dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
2512463c3197SDave Hansen 	if (error)
2513463c3197SDave Hansen 		goto out_dput;
2514dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
2515be6d3e56SKentaro Takeda 	if (error)
2516be6d3e56SKentaro Takeda 		goto out_drop_write;
25171da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
25181da177e4SLinus Torvalds 		case 0: case S_IFREG:
2519dae6ad8fSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
25201da177e4SLinus Torvalds 			break;
25211da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
2522dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
25231da177e4SLinus Torvalds 					new_decode_dev(dev));
25241da177e4SLinus Torvalds 			break;
25251da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
2526dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
25271da177e4SLinus Torvalds 			break;
25281da177e4SLinus Torvalds 	}
2529be6d3e56SKentaro Takeda out_drop_write:
2530dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
2531463c3197SDave Hansen out_dput:
25321da177e4SLinus Torvalds 	dput(dentry);
2533dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2534dae6ad8fSAl Viro 	path_put(&path);
25351da177e4SLinus Torvalds 
25361da177e4SLinus Torvalds 	return error;
25371da177e4SLinus Torvalds }
25381da177e4SLinus Torvalds 
25398208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
25405590ff0dSUlrich Drepper {
25415590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
25425590ff0dSUlrich Drepper }
25435590ff0dSUlrich Drepper 
25441da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
25451da177e4SLinus Torvalds {
2546a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
25471da177e4SLinus Torvalds 
25481da177e4SLinus Torvalds 	if (error)
25491da177e4SLinus Torvalds 		return error;
25501da177e4SLinus Torvalds 
2551acfa4380SAl Viro 	if (!dir->i_op->mkdir)
25521da177e4SLinus Torvalds 		return -EPERM;
25531da177e4SLinus Torvalds 
25541da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
25551da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
25561da177e4SLinus Torvalds 	if (error)
25571da177e4SLinus Torvalds 		return error;
25581da177e4SLinus Torvalds 
25591da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2560a74574aaSStephen Smalley 	if (!error)
2561f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
25621da177e4SLinus Torvalds 	return error;
25631da177e4SLinus Torvalds }
25641da177e4SLinus Torvalds 
25652e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
25661da177e4SLinus Torvalds {
25676902d925SDave Hansen 	struct dentry *dentry;
2568dae6ad8fSAl Viro 	struct path path;
2569dae6ad8fSAl Viro 	int error;
25701da177e4SLinus Torvalds 
2571dae6ad8fSAl Viro 	dentry = user_path_create(dfd, pathname, &path, 1);
25726902d925SDave Hansen 	if (IS_ERR(dentry))
2573dae6ad8fSAl Viro 		return PTR_ERR(dentry);
25746902d925SDave Hansen 
2575dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
2576ce3b0f8dSAl Viro 		mode &= ~current_umask();
2577dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
2578463c3197SDave Hansen 	if (error)
2579463c3197SDave Hansen 		goto out_dput;
2580dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
2581be6d3e56SKentaro Takeda 	if (error)
2582be6d3e56SKentaro Takeda 		goto out_drop_write;
2583dae6ad8fSAl Viro 	error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
2584be6d3e56SKentaro Takeda out_drop_write:
2585dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
2586463c3197SDave Hansen out_dput:
25871da177e4SLinus Torvalds 	dput(dentry);
2588dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2589dae6ad8fSAl Viro 	path_put(&path);
25901da177e4SLinus Torvalds 	return error;
25911da177e4SLinus Torvalds }
25921da177e4SLinus Torvalds 
25933cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
25945590ff0dSUlrich Drepper {
25955590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
25965590ff0dSUlrich Drepper }
25975590ff0dSUlrich Drepper 
25981da177e4SLinus Torvalds /*
2599a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
2600a71905f0SSage Weil  * should have a usage count of 2 if we're the only user of this
2601a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
2602a71905f0SSage Weil  * then we drop the dentry now.
26031da177e4SLinus Torvalds  *
26041da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
26051da177e4SLinus Torvalds  * do a
26061da177e4SLinus Torvalds  *
26071da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
26081da177e4SLinus Torvalds  *		return -EBUSY;
26091da177e4SLinus Torvalds  *
26101da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
26111da177e4SLinus Torvalds  * that is still in use by something else..
26121da177e4SLinus Torvalds  */
26131da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
26141da177e4SLinus Torvalds {
26151da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
26161da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
261764252c75SSage Weil 	if (dentry->d_count == 1)
26181da177e4SLinus Torvalds 		__d_drop(dentry);
26191da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
26201da177e4SLinus Torvalds }
26211da177e4SLinus Torvalds 
26221da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
26231da177e4SLinus Torvalds {
26241da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
26251da177e4SLinus Torvalds 
26261da177e4SLinus Torvalds 	if (error)
26271da177e4SLinus Torvalds 		return error;
26281da177e4SLinus Torvalds 
2629acfa4380SAl Viro 	if (!dir->i_op->rmdir)
26301da177e4SLinus Torvalds 		return -EPERM;
26311da177e4SLinus Torvalds 
26321d2ef590SAl Viro 	dget(dentry);
26331b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
2634912dbc15SSage Weil 
26351da177e4SLinus Torvalds 	error = -EBUSY;
2636912dbc15SSage Weil 	if (d_mountpoint(dentry))
2637912dbc15SSage Weil 		goto out;
2638912dbc15SSage Weil 
26391da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
2640912dbc15SSage Weil 	if (error)
2641912dbc15SSage Weil 		goto out;
2642912dbc15SSage Weil 
26433cebde24SSage Weil 	shrink_dcache_parent(dentry);
26441da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
2645912dbc15SSage Weil 	if (error)
2646912dbc15SSage Weil 		goto out;
2647912dbc15SSage Weil 
26481da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
2649d83c49f3SAl Viro 	dont_mount(dentry);
26501da177e4SLinus Torvalds 
2651912dbc15SSage Weil out:
2652912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
26531d2ef590SAl Viro 	dput(dentry);
2654912dbc15SSage Weil 	if (!error)
2655912dbc15SSage Weil 		d_delete(dentry);
26561da177e4SLinus Torvalds 	return error;
26571da177e4SLinus Torvalds }
26581da177e4SLinus Torvalds 
26595590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
26601da177e4SLinus Torvalds {
26611da177e4SLinus Torvalds 	int error = 0;
26621da177e4SLinus Torvalds 	char * name;
26631da177e4SLinus Torvalds 	struct dentry *dentry;
26641da177e4SLinus Torvalds 	struct nameidata nd;
26651da177e4SLinus Torvalds 
26662ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
26671da177e4SLinus Torvalds 	if (error)
26682ad94ae6SAl Viro 		return error;
26691da177e4SLinus Torvalds 
26701da177e4SLinus Torvalds 	switch(nd.last_type) {
26711da177e4SLinus Torvalds 	case LAST_DOTDOT:
26721da177e4SLinus Torvalds 		error = -ENOTEMPTY;
26731da177e4SLinus Torvalds 		goto exit1;
26741da177e4SLinus Torvalds 	case LAST_DOT:
26751da177e4SLinus Torvalds 		error = -EINVAL;
26761da177e4SLinus Torvalds 		goto exit1;
26771da177e4SLinus Torvalds 	case LAST_ROOT:
26781da177e4SLinus Torvalds 		error = -EBUSY;
26791da177e4SLinus Torvalds 		goto exit1;
26801da177e4SLinus Torvalds 	}
26810612d9fbSOGAWA Hirofumi 
26820612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
26830612d9fbSOGAWA Hirofumi 
26844ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
268549705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
26861da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
26876902d925SDave Hansen 	if (IS_ERR(dentry))
26886902d925SDave Hansen 		goto exit2;
2689e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
2690e6bc45d6STheodore Ts'o 		error = -ENOENT;
2691e6bc45d6STheodore Ts'o 		goto exit3;
2692e6bc45d6STheodore Ts'o 	}
26930622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
26940622753bSDave Hansen 	if (error)
26950622753bSDave Hansen 		goto exit3;
2696be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2697be6d3e56SKentaro Takeda 	if (error)
2698be6d3e56SKentaro Takeda 		goto exit4;
26994ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2700be6d3e56SKentaro Takeda exit4:
27010622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
27020622753bSDave Hansen exit3:
27031da177e4SLinus Torvalds 	dput(dentry);
27046902d925SDave Hansen exit2:
27054ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27061da177e4SLinus Torvalds exit1:
27071d957f9bSJan Blunck 	path_put(&nd.path);
27081da177e4SLinus Torvalds 	putname(name);
27091da177e4SLinus Torvalds 	return error;
27101da177e4SLinus Torvalds }
27111da177e4SLinus Torvalds 
27123cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
27135590ff0dSUlrich Drepper {
27145590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
27155590ff0dSUlrich Drepper }
27165590ff0dSUlrich Drepper 
27171da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
27181da177e4SLinus Torvalds {
27191da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
27201da177e4SLinus Torvalds 
27211da177e4SLinus Torvalds 	if (error)
27221da177e4SLinus Torvalds 		return error;
27231da177e4SLinus Torvalds 
2724acfa4380SAl Viro 	if (!dir->i_op->unlink)
27251da177e4SLinus Torvalds 		return -EPERM;
27261da177e4SLinus Torvalds 
27271b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
27281da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
27291da177e4SLinus Torvalds 		error = -EBUSY;
27301da177e4SLinus Torvalds 	else {
27311da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2732bec1052eSAl Viro 		if (!error) {
27331da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2734bec1052eSAl Viro 			if (!error)
2735d83c49f3SAl Viro 				dont_mount(dentry);
2736bec1052eSAl Viro 		}
27371da177e4SLinus Torvalds 	}
27381b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
27391da177e4SLinus Torvalds 
27401da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
27411da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2742ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
27431da177e4SLinus Torvalds 		d_delete(dentry);
27441da177e4SLinus Torvalds 	}
27450eeca283SRobert Love 
27461da177e4SLinus Torvalds 	return error;
27471da177e4SLinus Torvalds }
27481da177e4SLinus Torvalds 
27491da177e4SLinus Torvalds /*
27501da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
27511b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
27521da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
27531da177e4SLinus Torvalds  * while waiting on the I/O.
27541da177e4SLinus Torvalds  */
27555590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
27561da177e4SLinus Torvalds {
27572ad94ae6SAl Viro 	int error;
27581da177e4SLinus Torvalds 	char *name;
27591da177e4SLinus Torvalds 	struct dentry *dentry;
27601da177e4SLinus Torvalds 	struct nameidata nd;
27611da177e4SLinus Torvalds 	struct inode *inode = NULL;
27621da177e4SLinus Torvalds 
27632ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
27641da177e4SLinus Torvalds 	if (error)
27652ad94ae6SAl Viro 		return error;
27662ad94ae6SAl Viro 
27671da177e4SLinus Torvalds 	error = -EISDIR;
27681da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
27691da177e4SLinus Torvalds 		goto exit1;
27700612d9fbSOGAWA Hirofumi 
27710612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
27720612d9fbSOGAWA Hirofumi 
27734ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
277449705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
27751da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27761da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
27771da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
277850338b88STörök Edwin 		if (nd.last.name[nd.last.len])
277950338b88STörök Edwin 			goto slashes;
27801da177e4SLinus Torvalds 		inode = dentry->d_inode;
278150338b88STörök Edwin 		if (!inode)
2782e6bc45d6STheodore Ts'o 			goto slashes;
27837de9c6eeSAl Viro 		ihold(inode);
27840622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
27850622753bSDave Hansen 		if (error)
27860622753bSDave Hansen 			goto exit2;
2787be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2788be6d3e56SKentaro Takeda 		if (error)
2789be6d3e56SKentaro Takeda 			goto exit3;
27904ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2791be6d3e56SKentaro Takeda exit3:
27920622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
27931da177e4SLinus Torvalds 	exit2:
27941da177e4SLinus Torvalds 		dput(dentry);
27951da177e4SLinus Torvalds 	}
27964ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27971da177e4SLinus Torvalds 	if (inode)
27981da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
27991da177e4SLinus Torvalds exit1:
28001d957f9bSJan Blunck 	path_put(&nd.path);
28011da177e4SLinus Torvalds 	putname(name);
28021da177e4SLinus Torvalds 	return error;
28031da177e4SLinus Torvalds 
28041da177e4SLinus Torvalds slashes:
28051da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
28061da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
28071da177e4SLinus Torvalds 	goto exit2;
28081da177e4SLinus Torvalds }
28091da177e4SLinus Torvalds 
28102e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
28115590ff0dSUlrich Drepper {
28125590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
28135590ff0dSUlrich Drepper 		return -EINVAL;
28145590ff0dSUlrich Drepper 
28155590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
28165590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
28175590ff0dSUlrich Drepper 
28185590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
28195590ff0dSUlrich Drepper }
28205590ff0dSUlrich Drepper 
28213480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
28225590ff0dSUlrich Drepper {
28235590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
28245590ff0dSUlrich Drepper }
28255590ff0dSUlrich Drepper 
2826db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
28271da177e4SLinus Torvalds {
2828a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
28291da177e4SLinus Torvalds 
28301da177e4SLinus Torvalds 	if (error)
28311da177e4SLinus Torvalds 		return error;
28321da177e4SLinus Torvalds 
2833acfa4380SAl Viro 	if (!dir->i_op->symlink)
28341da177e4SLinus Torvalds 		return -EPERM;
28351da177e4SLinus Torvalds 
28361da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
28371da177e4SLinus Torvalds 	if (error)
28381da177e4SLinus Torvalds 		return error;
28391da177e4SLinus Torvalds 
28401da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2841a74574aaSStephen Smalley 	if (!error)
2842f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
28431da177e4SLinus Torvalds 	return error;
28441da177e4SLinus Torvalds }
28451da177e4SLinus Torvalds 
28462e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
28472e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
28481da177e4SLinus Torvalds {
28492ad94ae6SAl Viro 	int error;
28501da177e4SLinus Torvalds 	char *from;
28516902d925SDave Hansen 	struct dentry *dentry;
2852dae6ad8fSAl Viro 	struct path path;
28531da177e4SLinus Torvalds 
28541da177e4SLinus Torvalds 	from = getname(oldname);
28551da177e4SLinus Torvalds 	if (IS_ERR(from))
28561da177e4SLinus Torvalds 		return PTR_ERR(from);
28572ad94ae6SAl Viro 
2858dae6ad8fSAl Viro 	dentry = user_path_create(newdfd, newname, &path, 0);
28591da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
28606902d925SDave Hansen 	if (IS_ERR(dentry))
2861dae6ad8fSAl Viro 		goto out_putname;
28626902d925SDave Hansen 
2863dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
286475c3f29dSDave Hansen 	if (error)
286575c3f29dSDave Hansen 		goto out_dput;
2866dae6ad8fSAl Viro 	error = security_path_symlink(&path, dentry, from);
2867be6d3e56SKentaro Takeda 	if (error)
2868be6d3e56SKentaro Takeda 		goto out_drop_write;
2869dae6ad8fSAl Viro 	error = vfs_symlink(path.dentry->d_inode, dentry, from);
2870be6d3e56SKentaro Takeda out_drop_write:
2871dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
287275c3f29dSDave Hansen out_dput:
28731da177e4SLinus Torvalds 	dput(dentry);
2874dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2875dae6ad8fSAl Viro 	path_put(&path);
28766902d925SDave Hansen out_putname:
28771da177e4SLinus Torvalds 	putname(from);
28781da177e4SLinus Torvalds 	return error;
28791da177e4SLinus Torvalds }
28801da177e4SLinus Torvalds 
28813480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
28825590ff0dSUlrich Drepper {
28835590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
28845590ff0dSUlrich Drepper }
28855590ff0dSUlrich Drepper 
28861da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
28871da177e4SLinus Torvalds {
28881da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
28891da177e4SLinus Torvalds 	int error;
28901da177e4SLinus Torvalds 
28911da177e4SLinus Torvalds 	if (!inode)
28921da177e4SLinus Torvalds 		return -ENOENT;
28931da177e4SLinus Torvalds 
2894a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
28951da177e4SLinus Torvalds 	if (error)
28961da177e4SLinus Torvalds 		return error;
28971da177e4SLinus Torvalds 
28981da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
28991da177e4SLinus Torvalds 		return -EXDEV;
29001da177e4SLinus Torvalds 
29011da177e4SLinus Torvalds 	/*
29021da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
29031da177e4SLinus Torvalds 	 */
29041da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
29051da177e4SLinus Torvalds 		return -EPERM;
2906acfa4380SAl Viro 	if (!dir->i_op->link)
29071da177e4SLinus Torvalds 		return -EPERM;
29087e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
29091da177e4SLinus Torvalds 		return -EPERM;
29101da177e4SLinus Torvalds 
29111da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
29121da177e4SLinus Torvalds 	if (error)
29131da177e4SLinus Torvalds 		return error;
29141da177e4SLinus Torvalds 
29157e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
2916aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
2917aae8a97dSAneesh Kumar K.V 	if (inode->i_nlink == 0)
2918aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
2919aae8a97dSAneesh Kumar K.V 	else
29201da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
29217e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2922e31e14ecSStephen Smalley 	if (!error)
29237e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
29241da177e4SLinus Torvalds 	return error;
29251da177e4SLinus Torvalds }
29261da177e4SLinus Torvalds 
29271da177e4SLinus Torvalds /*
29281da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
29291da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
29301da177e4SLinus Torvalds  * newname.  --KAB
29311da177e4SLinus Torvalds  *
29321da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
29331da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
29341da177e4SLinus Torvalds  * and other special files.  --ADM
29351da177e4SLinus Torvalds  */
29362e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
29372e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
29381da177e4SLinus Torvalds {
29391da177e4SLinus Torvalds 	struct dentry *new_dentry;
2940dae6ad8fSAl Viro 	struct path old_path, new_path;
294111a7b371SAneesh Kumar K.V 	int how = 0;
29421da177e4SLinus Torvalds 	int error;
29431da177e4SLinus Torvalds 
294411a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
2945c04030e1SUlrich Drepper 		return -EINVAL;
294611a7b371SAneesh Kumar K.V 	/*
294711a7b371SAneesh Kumar K.V 	 * To use null names we require CAP_DAC_READ_SEARCH
294811a7b371SAneesh Kumar K.V 	 * This ensures that not everyone will be able to create
294911a7b371SAneesh Kumar K.V 	 * handlink using the passed filedescriptor.
295011a7b371SAneesh Kumar K.V 	 */
295111a7b371SAneesh Kumar K.V 	if (flags & AT_EMPTY_PATH) {
295211a7b371SAneesh Kumar K.V 		if (!capable(CAP_DAC_READ_SEARCH))
295311a7b371SAneesh Kumar K.V 			return -ENOENT;
295411a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
295511a7b371SAneesh Kumar K.V 	}
2956c04030e1SUlrich Drepper 
295711a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
295811a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
295911a7b371SAneesh Kumar K.V 
296011a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
29611da177e4SLinus Torvalds 	if (error)
29622ad94ae6SAl Viro 		return error;
29632ad94ae6SAl Viro 
2964dae6ad8fSAl Viro 	new_dentry = user_path_create(newdfd, newname, &new_path, 0);
29651da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
29666902d925SDave Hansen 	if (IS_ERR(new_dentry))
2967dae6ad8fSAl Viro 		goto out;
2968dae6ad8fSAl Viro 
2969dae6ad8fSAl Viro 	error = -EXDEV;
2970dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
2971dae6ad8fSAl Viro 		goto out_dput;
2972dae6ad8fSAl Viro 	error = mnt_want_write(new_path.mnt);
297375c3f29dSDave Hansen 	if (error)
297475c3f29dSDave Hansen 		goto out_dput;
2975dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
2976be6d3e56SKentaro Takeda 	if (error)
2977be6d3e56SKentaro Takeda 		goto out_drop_write;
2978dae6ad8fSAl Viro 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
2979be6d3e56SKentaro Takeda out_drop_write:
2980dae6ad8fSAl Viro 	mnt_drop_write(new_path.mnt);
298175c3f29dSDave Hansen out_dput:
29821da177e4SLinus Torvalds 	dput(new_dentry);
2983dae6ad8fSAl Viro 	mutex_unlock(&new_path.dentry->d_inode->i_mutex);
2984dae6ad8fSAl Viro 	path_put(&new_path);
29851da177e4SLinus Torvalds out:
29862d8f3038SAl Viro 	path_put(&old_path);
29871da177e4SLinus Torvalds 
29881da177e4SLinus Torvalds 	return error;
29891da177e4SLinus Torvalds }
29901da177e4SLinus Torvalds 
29913480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
29925590ff0dSUlrich Drepper {
2993c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
29945590ff0dSUlrich Drepper }
29955590ff0dSUlrich Drepper 
29961da177e4SLinus Torvalds /*
29971da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
29981da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
29991da177e4SLinus Torvalds  * Problems:
30001da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
30011da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
30021da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
3003a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
30041da177e4SLinus Torvalds  *	   story.
30051da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
30061b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
30071da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
30081da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
3009a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
30101da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
30111da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
30121da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
3013a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
30141da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
30151da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
30161da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
3017e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
30181b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
30191da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3020c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
30211da177e4SLinus Torvalds  *	   locking].
30221da177e4SLinus Torvalds  */
302375c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
30241da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
30251da177e4SLinus Torvalds {
30261da177e4SLinus Torvalds 	int error = 0;
30279055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
30281da177e4SLinus Torvalds 
30291da177e4SLinus Torvalds 	/*
30301da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
30311da177e4SLinus Torvalds 	 * we'll need to flip '..'.
30321da177e4SLinus Torvalds 	 */
30331da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3034f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
30351da177e4SLinus Torvalds 		if (error)
30361da177e4SLinus Torvalds 			return error;
30371da177e4SLinus Torvalds 	}
30381da177e4SLinus Torvalds 
30391da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30401da177e4SLinus Torvalds 	if (error)
30411da177e4SLinus Torvalds 		return error;
30421da177e4SLinus Torvalds 
30431d2ef590SAl Viro 	dget(new_dentry);
3044d83c49f3SAl Viro 	if (target)
30451b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
30469055cba7SSage Weil 
30471da177e4SLinus Torvalds 	error = -EBUSY;
30489055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
30499055cba7SSage Weil 		goto out;
30509055cba7SSage Weil 
30513cebde24SSage Weil 	if (target)
30523cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
30531da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
30549055cba7SSage Weil 	if (error)
30559055cba7SSage Weil 		goto out;
30569055cba7SSage Weil 
30571da177e4SLinus Torvalds 	if (target) {
30581da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
3059d83c49f3SAl Viro 		dont_mount(new_dentry);
3060d83c49f3SAl Viro 	}
30619055cba7SSage Weil out:
30629055cba7SSage Weil 	if (target)
30631b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
30641d2ef590SAl Viro 	dput(new_dentry);
3065e31e14ecSStephen Smalley 	if (!error)
3066349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30671da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
30681da177e4SLinus Torvalds 	return error;
30691da177e4SLinus Torvalds }
30701da177e4SLinus Torvalds 
307175c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
30721da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
30731da177e4SLinus Torvalds {
307451892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
30751da177e4SLinus Torvalds 	int error;
30761da177e4SLinus Torvalds 
30771da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30781da177e4SLinus Torvalds 	if (error)
30791da177e4SLinus Torvalds 		return error;
30801da177e4SLinus Torvalds 
30811da177e4SLinus Torvalds 	dget(new_dentry);
30821da177e4SLinus Torvalds 	if (target)
30831b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
308451892bbbSSage Weil 
30851da177e4SLinus Torvalds 	error = -EBUSY;
308651892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
308751892bbbSSage Weil 		goto out;
308851892bbbSSage Weil 
30891da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
309051892bbbSSage Weil 	if (error)
309151892bbbSSage Weil 		goto out;
309251892bbbSSage Weil 
3093bec1052eSAl Viro 	if (target)
3094d83c49f3SAl Viro 		dont_mount(new_dentry);
3095349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30961da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
309751892bbbSSage Weil out:
30981da177e4SLinus Torvalds 	if (target)
30991b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
31001da177e4SLinus Torvalds 	dput(new_dentry);
31011da177e4SLinus Torvalds 	return error;
31021da177e4SLinus Torvalds }
31031da177e4SLinus Torvalds 
31041da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
31051da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
31061da177e4SLinus Torvalds {
31071da177e4SLinus Torvalds 	int error;
31081da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
310959b0df21SEric Paris 	const unsigned char *old_name;
31101da177e4SLinus Torvalds 
31111da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
31121da177e4SLinus Torvalds  		return 0;
31131da177e4SLinus Torvalds 
31141da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
31151da177e4SLinus Torvalds 	if (error)
31161da177e4SLinus Torvalds 		return error;
31171da177e4SLinus Torvalds 
31181da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3119a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
31201da177e4SLinus Torvalds 	else
31211da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
31221da177e4SLinus Torvalds 	if (error)
31231da177e4SLinus Torvalds 		return error;
31241da177e4SLinus Torvalds 
3125acfa4380SAl Viro 	if (!old_dir->i_op->rename)
31261da177e4SLinus Torvalds 		return -EPERM;
31271da177e4SLinus Torvalds 
31280eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
31290eeca283SRobert Love 
31301da177e4SLinus Torvalds 	if (is_dir)
31311da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
31321da177e4SLinus Torvalds 	else
31331da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3134123df294SAl Viro 	if (!error)
3135123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
31365a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
31370eeca283SRobert Love 	fsnotify_oldname_free(old_name);
31380eeca283SRobert Love 
31391da177e4SLinus Torvalds 	return error;
31401da177e4SLinus Torvalds }
31411da177e4SLinus Torvalds 
31422e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
31432e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
31441da177e4SLinus Torvalds {
31451da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
31461da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
31471da177e4SLinus Torvalds 	struct dentry *trap;
31481da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
31492ad94ae6SAl Viro 	char *from;
31502ad94ae6SAl Viro 	char *to;
31512ad94ae6SAl Viro 	int error;
31521da177e4SLinus Torvalds 
31532ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
31541da177e4SLinus Torvalds 	if (error)
31551da177e4SLinus Torvalds 		goto exit;
31561da177e4SLinus Torvalds 
31572ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
31581da177e4SLinus Torvalds 	if (error)
31591da177e4SLinus Torvalds 		goto exit1;
31601da177e4SLinus Torvalds 
31611da177e4SLinus Torvalds 	error = -EXDEV;
31624ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
31631da177e4SLinus Torvalds 		goto exit2;
31641da177e4SLinus Torvalds 
31654ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
31661da177e4SLinus Torvalds 	error = -EBUSY;
31671da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
31681da177e4SLinus Torvalds 		goto exit2;
31691da177e4SLinus Torvalds 
31704ac91378SJan Blunck 	new_dir = newnd.path.dentry;
31711da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
31721da177e4SLinus Torvalds 		goto exit2;
31731da177e4SLinus Torvalds 
31740612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
31750612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
31764e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
31770612d9fbSOGAWA Hirofumi 
31781da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
31791da177e4SLinus Torvalds 
318049705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
31811da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
31821da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
31831da177e4SLinus Torvalds 		goto exit3;
31841da177e4SLinus Torvalds 	/* source must exist */
31851da177e4SLinus Torvalds 	error = -ENOENT;
31861da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
31871da177e4SLinus Torvalds 		goto exit4;
31881da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
31891da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
31901da177e4SLinus Torvalds 		error = -ENOTDIR;
31911da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
31921da177e4SLinus Torvalds 			goto exit4;
31931da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
31941da177e4SLinus Torvalds 			goto exit4;
31951da177e4SLinus Torvalds 	}
31961da177e4SLinus Torvalds 	/* source should not be ancestor of target */
31971da177e4SLinus Torvalds 	error = -EINVAL;
31981da177e4SLinus Torvalds 	if (old_dentry == trap)
31991da177e4SLinus Torvalds 		goto exit4;
320049705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
32011da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
32021da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
32031da177e4SLinus Torvalds 		goto exit4;
32041da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
32051da177e4SLinus Torvalds 	error = -ENOTEMPTY;
32061da177e4SLinus Torvalds 	if (new_dentry == trap)
32071da177e4SLinus Torvalds 		goto exit5;
32081da177e4SLinus Torvalds 
32099079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
32109079b1ebSDave Hansen 	if (error)
32119079b1ebSDave Hansen 		goto exit5;
3212be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3213be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3214be6d3e56SKentaro Takeda 	if (error)
3215be6d3e56SKentaro Takeda 		goto exit6;
32161da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
32171da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3218be6d3e56SKentaro Takeda exit6:
32199079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
32201da177e4SLinus Torvalds exit5:
32211da177e4SLinus Torvalds 	dput(new_dentry);
32221da177e4SLinus Torvalds exit4:
32231da177e4SLinus Torvalds 	dput(old_dentry);
32241da177e4SLinus Torvalds exit3:
32251da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
32261da177e4SLinus Torvalds exit2:
32271d957f9bSJan Blunck 	path_put(&newnd.path);
32282ad94ae6SAl Viro 	putname(to);
32291da177e4SLinus Torvalds exit1:
32301d957f9bSJan Blunck 	path_put(&oldnd.path);
32311da177e4SLinus Torvalds 	putname(from);
32322ad94ae6SAl Viro exit:
32331da177e4SLinus Torvalds 	return error;
32341da177e4SLinus Torvalds }
32351da177e4SLinus Torvalds 
3236a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
32375590ff0dSUlrich Drepper {
32385590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
32395590ff0dSUlrich Drepper }
32405590ff0dSUlrich Drepper 
32411da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
32421da177e4SLinus Torvalds {
32431da177e4SLinus Torvalds 	int len;
32441da177e4SLinus Torvalds 
32451da177e4SLinus Torvalds 	len = PTR_ERR(link);
32461da177e4SLinus Torvalds 	if (IS_ERR(link))
32471da177e4SLinus Torvalds 		goto out;
32481da177e4SLinus Torvalds 
32491da177e4SLinus Torvalds 	len = strlen(link);
32501da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
32511da177e4SLinus Torvalds 		len = buflen;
32521da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
32531da177e4SLinus Torvalds 		len = -EFAULT;
32541da177e4SLinus Torvalds out:
32551da177e4SLinus Torvalds 	return len;
32561da177e4SLinus Torvalds }
32571da177e4SLinus Torvalds 
32581da177e4SLinus Torvalds /*
32591da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
32601da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
32611da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
32621da177e4SLinus Torvalds  */
32631da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32641da177e4SLinus Torvalds {
32651da177e4SLinus Torvalds 	struct nameidata nd;
3266cc314eefSLinus Torvalds 	void *cookie;
3267694a1764SMarcin Slusarz 	int res;
3268cc314eefSLinus Torvalds 
32691da177e4SLinus Torvalds 	nd.depth = 0;
3270cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3271694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3272694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3273694a1764SMarcin Slusarz 
3274694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
32751da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3276cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3277694a1764SMarcin Slusarz 	return res;
32781da177e4SLinus Torvalds }
32791da177e4SLinus Torvalds 
32801da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
32811da177e4SLinus Torvalds {
32821da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
32831da177e4SLinus Torvalds }
32841da177e4SLinus Torvalds 
32851da177e4SLinus Torvalds /* get the link contents into pagecache */
32861da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
32871da177e4SLinus Torvalds {
3288ebd09abbSDuane Griffin 	char *kaddr;
32891da177e4SLinus Torvalds 	struct page *page;
32901da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3291090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
32921da177e4SLinus Torvalds 	if (IS_ERR(page))
32936fe6900eSNick Piggin 		return (char*)page;
32941da177e4SLinus Torvalds 	*ppage = page;
3295ebd09abbSDuane Griffin 	kaddr = kmap(page);
3296ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3297ebd09abbSDuane Griffin 	return kaddr;
32981da177e4SLinus Torvalds }
32991da177e4SLinus Torvalds 
33001da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
33011da177e4SLinus Torvalds {
33021da177e4SLinus Torvalds 	struct page *page = NULL;
33031da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
33041da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
33051da177e4SLinus Torvalds 	if (page) {
33061da177e4SLinus Torvalds 		kunmap(page);
33071da177e4SLinus Torvalds 		page_cache_release(page);
33081da177e4SLinus Torvalds 	}
33091da177e4SLinus Torvalds 	return res;
33101da177e4SLinus Torvalds }
33111da177e4SLinus Torvalds 
3312cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
33131da177e4SLinus Torvalds {
3314cc314eefSLinus Torvalds 	struct page *page = NULL;
33151da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3316cc314eefSLinus Torvalds 	return page;
33171da177e4SLinus Torvalds }
33181da177e4SLinus Torvalds 
3319cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
33201da177e4SLinus Torvalds {
3321cc314eefSLinus Torvalds 	struct page *page = cookie;
3322cc314eefSLinus Torvalds 
3323cc314eefSLinus Torvalds 	if (page) {
33241da177e4SLinus Torvalds 		kunmap(page);
33251da177e4SLinus Torvalds 		page_cache_release(page);
33261da177e4SLinus Torvalds 	}
33271da177e4SLinus Torvalds }
33281da177e4SLinus Torvalds 
332954566b2cSNick Piggin /*
333054566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
333154566b2cSNick Piggin  */
333254566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
33331da177e4SLinus Torvalds {
33341da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
33350adb25d2SKirill Korotaev 	struct page *page;
3336afddba49SNick Piggin 	void *fsdata;
3337beb497abSDmitriy Monakhov 	int err;
33381da177e4SLinus Torvalds 	char *kaddr;
333954566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
334054566b2cSNick Piggin 	if (nofs)
334154566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
33421da177e4SLinus Torvalds 
33437e53cac4SNeilBrown retry:
3344afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
334554566b2cSNick Piggin 				flags, &page, &fsdata);
33461da177e4SLinus Torvalds 	if (err)
3347afddba49SNick Piggin 		goto fail;
3348afddba49SNick Piggin 
33491da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
33501da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
33511da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
3352afddba49SNick Piggin 
3353afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3354afddba49SNick Piggin 							page, fsdata);
33551da177e4SLinus Torvalds 	if (err < 0)
33561da177e4SLinus Torvalds 		goto fail;
3357afddba49SNick Piggin 	if (err < len-1)
3358afddba49SNick Piggin 		goto retry;
3359afddba49SNick Piggin 
33601da177e4SLinus Torvalds 	mark_inode_dirty(inode);
33611da177e4SLinus Torvalds 	return 0;
33621da177e4SLinus Torvalds fail:
33631da177e4SLinus Torvalds 	return err;
33641da177e4SLinus Torvalds }
33651da177e4SLinus Torvalds 
33660adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
33670adb25d2SKirill Korotaev {
33680adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
336954566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
33700adb25d2SKirill Korotaev }
33710adb25d2SKirill Korotaev 
337292e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
33731da177e4SLinus Torvalds 	.readlink	= generic_readlink,
33741da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
33751da177e4SLinus Torvalds 	.put_link	= page_put_link,
33761da177e4SLinus Torvalds };
33771da177e4SLinus Torvalds 
33782d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3379cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
33801da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
33811da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
33821da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
33831da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
33841da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
33851da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
33861da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
33871da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
33881da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
33890adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
33901da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
33911da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3392d1811465SAl Viro EXPORT_SYMBOL(kern_path);
339316f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3394f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
33951da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
33961da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
33971da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
33981da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
33991da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
34001da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
34011da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
34021da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
34031da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
34041da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
34051da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
34061da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
34071da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
34081da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3409