xref: /openbmc/linux/fs/namei.c (revision f4d6ff89)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/namei.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  * Some corrections by tytso.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
121da177e4SLinus Torvalds  * lookup logic.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/init.h>
181da177e4SLinus Torvalds #include <linux/module.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/fs.h>
211da177e4SLinus Torvalds #include <linux/namei.h>
221da177e4SLinus Torvalds #include <linux/pagemap.h>
230eeca283SRobert Love #include <linux/fsnotify.h>
241da177e4SLinus Torvalds #include <linux/personality.h>
251da177e4SLinus Torvalds #include <linux/security.h>
266146f0d5SMimi Zohar #include <linux/ima.h>
271da177e4SLinus Torvalds #include <linux/syscalls.h>
281da177e4SLinus Torvalds #include <linux/mount.h>
291da177e4SLinus Torvalds #include <linux/audit.h>
3016f7e0feSRandy Dunlap #include <linux/capability.h>
31834f2a4aSTrond Myklebust #include <linux/file.h>
325590ff0dSUlrich Drepper #include <linux/fcntl.h>
3308ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h>
345ad4e53bSAl Viro #include <linux/fs_struct.h>
351da177e4SLinus Torvalds #include <asm/uaccess.h>
361da177e4SLinus Torvalds 
37e81e3f4dSEric Paris #include "internal.h"
38e81e3f4dSEric Paris 
391da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer]
401da177e4SLinus Torvalds  * Fundamental changes in the pathname lookup mechanisms (namei)
411da177e4SLinus Torvalds  * were necessary because of omirr.  The reason is that omirr needs
421da177e4SLinus Torvalds  * to know the _real_ pathname, not the user-supplied one, in case
431da177e4SLinus Torvalds  * of symlinks (and also when transname replacements occur).
441da177e4SLinus Torvalds  *
451da177e4SLinus Torvalds  * The new code replaces the old recursive symlink resolution with
461da177e4SLinus Torvalds  * an iterative one (in case of non-nested symlink chains).  It does
471da177e4SLinus Torvalds  * this with calls to <fs>_follow_link().
481da177e4SLinus Torvalds  * As a side effect, dir_namei(), _namei() and follow_link() are now
491da177e4SLinus Torvalds  * replaced with a single function lookup_dentry() that can handle all
501da177e4SLinus Torvalds  * the special cases of the former code.
511da177e4SLinus Torvalds  *
521da177e4SLinus Torvalds  * With the new dcache, the pathname is stored at each inode, at least as
531da177e4SLinus Torvalds  * long as the refcount of the inode is positive.  As a side effect, the
541da177e4SLinus Torvalds  * size of the dcache depends on the inode cache and thus is dynamic.
551da177e4SLinus Torvalds  *
561da177e4SLinus Torvalds  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
571da177e4SLinus Torvalds  * resolution to correspond with current state of the code.
581da177e4SLinus Torvalds  *
591da177e4SLinus Torvalds  * Note that the symlink resolution is not *completely* iterative.
601da177e4SLinus Torvalds  * There is still a significant amount of tail- and mid- recursion in
611da177e4SLinus Torvalds  * the algorithm.  Also, note that <fs>_readlink() is not used in
621da177e4SLinus Torvalds  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
631da177e4SLinus Torvalds  * may return different results than <fs>_follow_link().  Many virtual
641da177e4SLinus Torvalds  * filesystems (including /proc) exhibit this behavior.
651da177e4SLinus Torvalds  */
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
681da177e4SLinus Torvalds  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
691da177e4SLinus Torvalds  * and the name already exists in form of a symlink, try to create the new
701da177e4SLinus Torvalds  * name indicated by the symlink. The old code always complained that the
711da177e4SLinus Torvalds  * name already exists, due to not following the symlink even if its target
721da177e4SLinus Torvalds  * is nonexistent.  The new semantics affects also mknod() and link() when
7325985edcSLucas De Marchi  * the name is a symlink pointing to a non-existent name.
741da177e4SLinus Torvalds  *
751da177e4SLinus Torvalds  * I don't know which semantics is the right one, since I have no access
761da177e4SLinus Torvalds  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
771da177e4SLinus Torvalds  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
781da177e4SLinus Torvalds  * "old" one. Personally, I think the new semantics is much more logical.
791da177e4SLinus Torvalds  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
801da177e4SLinus Torvalds  * file does succeed in both HP-UX and SunOs, but not in Solaris
811da177e4SLinus Torvalds  * and in the old Linux semantics.
821da177e4SLinus Torvalds  */
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
851da177e4SLinus Torvalds  * semantics.  See the comments in "open_namei" and "do_link" below.
861da177e4SLinus Torvalds  *
871da177e4SLinus Torvalds  * [10-Sep-98 Alan Modra] Another symlink change.
881da177e4SLinus Torvalds  */
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
911da177e4SLinus Torvalds  *	inside the path - always follow.
921da177e4SLinus Torvalds  *	in the last component in creation/removal/renaming - never follow.
931da177e4SLinus Torvalds  *	if LOOKUP_FOLLOW passed - follow.
941da177e4SLinus Torvalds  *	if the pathname has trailing slashes - follow.
951da177e4SLinus Torvalds  *	otherwise - don't follow.
961da177e4SLinus Torvalds  * (applied in that order).
971da177e4SLinus Torvalds  *
981da177e4SLinus Torvalds  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
991da177e4SLinus Torvalds  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
1001da177e4SLinus Torvalds  * During the 2.4 we need to fix the userland stuff depending on it -
1011da177e4SLinus Torvalds  * hopefully we will be able to get rid of that wart in 2.5. So far only
1021da177e4SLinus Torvalds  * XEmacs seems to be relying on it...
1031da177e4SLinus Torvalds  */
1041da177e4SLinus Torvalds /*
1051da177e4SLinus Torvalds  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
106a11f3a05SArjan van de Ven  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1071da177e4SLinus Torvalds  * any extra contention...
1081da177e4SLinus Torvalds  */
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional
1111da177e4SLinus Torvalds  * checking and hopefully speeding things up, we copy filenames to the
1121da177e4SLinus Torvalds  * kernel data space before using them..
1131da177e4SLinus Torvalds  *
1141da177e4SLinus Torvalds  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
1151da177e4SLinus Torvalds  * PATH_MAX includes the nul terminator --RR.
1161da177e4SLinus Torvalds  */
117858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page)
1181da177e4SLinus Torvalds {
1191da177e4SLinus Torvalds 	int retval;
1201da177e4SLinus Torvalds 	unsigned long len = PATH_MAX;
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	if (!segment_eq(get_fs(), KERNEL_DS)) {
1231da177e4SLinus Torvalds 		if ((unsigned long) filename >= TASK_SIZE)
1241da177e4SLinus Torvalds 			return -EFAULT;
1251da177e4SLinus Torvalds 		if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
1261da177e4SLinus Torvalds 			len = TASK_SIZE - (unsigned long) filename;
1271da177e4SLinus Torvalds 	}
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	retval = strncpy_from_user(page, filename, len);
1301da177e4SLinus Torvalds 	if (retval > 0) {
1311da177e4SLinus Torvalds 		if (retval < len)
1321da177e4SLinus Torvalds 			return 0;
1331da177e4SLinus Torvalds 		return -ENAMETOOLONG;
1341da177e4SLinus Torvalds 	} else if (!retval)
1351da177e4SLinus Torvalds 		retval = -ENOENT;
1361da177e4SLinus Torvalds 	return retval;
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
139f52e0c11SAl Viro static char *getname_flags(const char __user * filename, int flags)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	char *tmp, *result;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	result = ERR_PTR(-ENOMEM);
1441da177e4SLinus Torvalds 	tmp = __getname();
1451da177e4SLinus Torvalds 	if (tmp)  {
1461da177e4SLinus Torvalds 		int retval = do_getname(filename, tmp);
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 		result = tmp;
1491da177e4SLinus Torvalds 		if (retval < 0) {
150f52e0c11SAl Viro 			if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
1511da177e4SLinus Torvalds 				__putname(tmp);
1521da177e4SLinus Torvalds 				result = ERR_PTR(retval);
1531da177e4SLinus Torvalds 			}
1541da177e4SLinus Torvalds 		}
155f52e0c11SAl Viro 	}
1561da177e4SLinus Torvalds 	audit_getname(result);
1571da177e4SLinus Torvalds 	return result;
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
160f52e0c11SAl Viro char *getname(const char __user * filename)
161f52e0c11SAl Viro {
162f52e0c11SAl Viro 	return getname_flags(filename, 0);
163f52e0c11SAl Viro }
164f52e0c11SAl Viro 
1651da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
1661da177e4SLinus Torvalds void putname(const char *name)
1671da177e4SLinus Torvalds {
1685ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
1691da177e4SLinus Torvalds 		audit_putname(name);
1701da177e4SLinus Torvalds 	else
1711da177e4SLinus Torvalds 		__putname(name);
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds EXPORT_SYMBOL(putname);
1741da177e4SLinus Torvalds #endif
1751da177e4SLinus Torvalds 
1765909ccaaSLinus Torvalds /*
1775909ccaaSLinus Torvalds  * This does basic POSIX ACL permission checking
1785909ccaaSLinus Torvalds  */
179b74c79e9SNick Piggin static int acl_permission_check(struct inode *inode, int mask, unsigned int flags,
180b74c79e9SNick Piggin 		int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
1815909ccaaSLinus Torvalds {
18226cf46beSLinus Torvalds 	unsigned int mode = inode->i_mode;
1835909ccaaSLinus Torvalds 
1845909ccaaSLinus Torvalds 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
1855909ccaaSLinus Torvalds 
186e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
187e795b717SSerge E. Hallyn 		goto other_perms;
188e795b717SSerge E. Hallyn 
1895909ccaaSLinus Torvalds 	if (current_fsuid() == inode->i_uid)
1905909ccaaSLinus Torvalds 		mode >>= 6;
1915909ccaaSLinus Torvalds 	else {
1925909ccaaSLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
193b74c79e9SNick Piggin 			int error = check_acl(inode, mask, flags);
1945909ccaaSLinus Torvalds 			if (error != -EAGAIN)
1955909ccaaSLinus Torvalds 				return error;
1965909ccaaSLinus Torvalds 		}
1975909ccaaSLinus Torvalds 
1985909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
1995909ccaaSLinus Torvalds 			mode >>= 3;
2005909ccaaSLinus Torvalds 	}
2015909ccaaSLinus Torvalds 
202e795b717SSerge E. Hallyn other_perms:
2035909ccaaSLinus Torvalds 	/*
2045909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
2055909ccaaSLinus Torvalds 	 */
2065909ccaaSLinus Torvalds 	if ((mask & ~mode) == 0)
2075909ccaaSLinus Torvalds 		return 0;
2085909ccaaSLinus Torvalds 	return -EACCES;
2095909ccaaSLinus Torvalds }
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds /**
2121da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
2131da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2141da177e4SLinus Torvalds  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
2151da177e4SLinus Torvalds  * @check_acl:	optional callback to check for Posix ACLs
21639191628SRandy Dunlap  * @flags:	IPERM_FLAG_ flags.
2171da177e4SLinus Torvalds  *
2181da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
2191da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
2201da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
221b74c79e9SNick Piggin  * are used for other things.
222b74c79e9SNick Piggin  *
223b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
224b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
225b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
2261da177e4SLinus Torvalds  */
227b74c79e9SNick Piggin int generic_permission(struct inode *inode, int mask, unsigned int flags,
228b74c79e9SNick Piggin 	int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
2291da177e4SLinus Torvalds {
2305909ccaaSLinus Torvalds 	int ret;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	/*
2335909ccaaSLinus Torvalds 	 * Do the basic POSIX ACL permission checks.
2341da177e4SLinus Torvalds 	 */
235b74c79e9SNick Piggin 	ret = acl_permission_check(inode, mask, flags, check_acl);
2365909ccaaSLinus Torvalds 	if (ret != -EACCES)
2375909ccaaSLinus Torvalds 		return ret;
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	/*
2401da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
2418e833fd2SAl Viro 	 * Executable DACs are overridable for all directories and
2428e833fd2SAl Viro 	 * for non-directories that have least one exec bit set.
2431da177e4SLinus Torvalds 	 */
244f696a365SMiklos Szeredi 	if (!(mask & MAY_EXEC) || execute_ok(inode))
245e795b717SSerge E. Hallyn 		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
2461da177e4SLinus Torvalds 			return 0;
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	/*
2491da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
2501da177e4SLinus Torvalds 	 */
2517ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
2521da177e4SLinus Torvalds 	if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
253e795b717SSerge E. Hallyn 		if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
2541da177e4SLinus Torvalds 			return 0;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	return -EACCES;
2571da177e4SLinus Torvalds }
2581da177e4SLinus Torvalds 
259cb23beb5SChristoph Hellwig /**
260cb23beb5SChristoph Hellwig  * inode_permission  -  check for access rights to a given inode
261cb23beb5SChristoph Hellwig  * @inode:	inode to check permission on
262cb23beb5SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
263cb23beb5SChristoph Hellwig  *
264cb23beb5SChristoph Hellwig  * Used to check for read/write/execute permissions on an inode.
265cb23beb5SChristoph Hellwig  * We use "fsuid" for this, letting us set arbitrary permissions
266cb23beb5SChristoph Hellwig  * for filesystem access without changing the "normal" uids which
267cb23beb5SChristoph Hellwig  * are used for other things.
268cb23beb5SChristoph Hellwig  */
269f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask)
2701da177e4SLinus Torvalds {
271e6305c43SAl Viro 	int retval;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	if (mask & MAY_WRITE) {
27422590e41SMiklos Szeredi 		umode_t mode = inode->i_mode;
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 		/*
2771da177e4SLinus Torvalds 		 * Nobody gets write access to a read-only fs.
2781da177e4SLinus Torvalds 		 */
2791da177e4SLinus Torvalds 		if (IS_RDONLY(inode) &&
2801da177e4SLinus Torvalds 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
2811da177e4SLinus Torvalds 			return -EROFS;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 		/*
2841da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
2851da177e4SLinus Torvalds 		 */
2861da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
2871da177e4SLinus Torvalds 			return -EACCES;
2881da177e4SLinus Torvalds 	}
2891da177e4SLinus Torvalds 
290acfa4380SAl Viro 	if (inode->i_op->permission)
291b74c79e9SNick Piggin 		retval = inode->i_op->permission(inode, mask, 0);
292f696a365SMiklos Szeredi 	else
293b74c79e9SNick Piggin 		retval = generic_permission(inode, mask, 0,
294b74c79e9SNick Piggin 				inode->i_op->check_acl);
295f696a365SMiklos Szeredi 
2961da177e4SLinus Torvalds 	if (retval)
2971da177e4SLinus Torvalds 		return retval;
2981da177e4SLinus Torvalds 
29908ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
30008ce5f16SSerge E. Hallyn 	if (retval)
30108ce5f16SSerge E. Hallyn 		return retval;
30208ce5f16SSerge E. Hallyn 
303d09ca739SEric Paris 	return security_inode_permission(inode, mask);
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
306f4d6ff89SAl Viro /**
307f4d6ff89SAl Viro  * exec_permission  -  check for right to do lookups in a given directory
308f4d6ff89SAl Viro  * @inode:	inode to check permission on
309f4d6ff89SAl Viro  * @flags:	IPERM_FLAG_ flags.
310f4d6ff89SAl Viro  *
311f4d6ff89SAl Viro  * Short-cut version of inode_permission(), for calling on directories
312f4d6ff89SAl Viro  * during pathname resolution.  Combines parts of inode_permission()
313f4d6ff89SAl Viro  * and generic_permission(), and tests ONLY for MAY_EXEC permission.
314f4d6ff89SAl Viro  *
315f4d6ff89SAl Viro  * If appropriate, check DAC only.  If not appropriate, or
316f4d6ff89SAl Viro  * short-cut DAC fails, then call ->permission() to do more
317f4d6ff89SAl Viro  * complete permission check.
318f4d6ff89SAl Viro  */
319f4d6ff89SAl Viro static inline int exec_permission(struct inode *inode, unsigned int flags)
320f4d6ff89SAl Viro {
321f4d6ff89SAl Viro 	int ret;
322f4d6ff89SAl Viro 	struct user_namespace *ns = inode_userns(inode);
323f4d6ff89SAl Viro 
324f4d6ff89SAl Viro 	if (inode->i_op->permission) {
325f4d6ff89SAl Viro 		ret = inode->i_op->permission(inode, MAY_EXEC, flags);
326f4d6ff89SAl Viro 		if (likely(!ret))
327f4d6ff89SAl Viro 			goto ok;
328f4d6ff89SAl Viro 	} else {
329f4d6ff89SAl Viro 		ret = acl_permission_check(inode, MAY_EXEC, flags,
330f4d6ff89SAl Viro 				inode->i_op->check_acl);
331f4d6ff89SAl Viro 		if (likely(!ret))
332f4d6ff89SAl Viro 			goto ok;
333f4d6ff89SAl Viro 		if (ret != -EACCES)
334f4d6ff89SAl Viro 			return ret;
335f4d6ff89SAl Viro 		if (ns_capable(ns, CAP_DAC_OVERRIDE) ||
336f4d6ff89SAl Viro 				ns_capable(ns, CAP_DAC_READ_SEARCH))
337f4d6ff89SAl Viro 			goto ok;
338f4d6ff89SAl Viro 	}
339f4d6ff89SAl Viro 	return ret;
340f4d6ff89SAl Viro ok:
341f4d6ff89SAl Viro 	return security_inode_exec_permission(inode, flags);
342f4d6ff89SAl Viro }
343f4d6ff89SAl Viro 
3441da177e4SLinus Torvalds /*
3451da177e4SLinus Torvalds  * get_write_access() gets write permission for a file.
3461da177e4SLinus Torvalds  * put_write_access() releases this write permission.
3471da177e4SLinus Torvalds  * This is used for regular files.
3481da177e4SLinus Torvalds  * We cannot support write (and maybe mmap read-write shared) accesses and
3491da177e4SLinus Torvalds  * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
3501da177e4SLinus Torvalds  * can have the following values:
3511da177e4SLinus Torvalds  * 0: no writers, no VM_DENYWRITE mappings
3521da177e4SLinus Torvalds  * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
3531da177e4SLinus Torvalds  * > 0: (i_writecount) users are writing to the file.
3541da177e4SLinus Torvalds  *
3551da177e4SLinus Torvalds  * Normally we operate on that counter with atomic_{inc,dec} and it's safe
3561da177e4SLinus Torvalds  * except for the cases where we don't hold i_writecount yet. Then we need to
3571da177e4SLinus Torvalds  * use {get,deny}_write_access() - these functions check the sign and refuse
3581da177e4SLinus Torvalds  * to do the change if sign is wrong. Exclusion between them is provided by
3591da177e4SLinus Torvalds  * the inode->i_lock spinlock.
3601da177e4SLinus Torvalds  */
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds int get_write_access(struct inode * inode)
3631da177e4SLinus Torvalds {
3641da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
3651da177e4SLinus Torvalds 	if (atomic_read(&inode->i_writecount) < 0) {
3661da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
3671da177e4SLinus Torvalds 		return -ETXTBSY;
3681da177e4SLinus Torvalds 	}
3691da177e4SLinus Torvalds 	atomic_inc(&inode->i_writecount);
3701da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	return 0;
3731da177e4SLinus Torvalds }
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds int deny_write_access(struct file * file)
3761da177e4SLinus Torvalds {
3770f7fc9e4SJosef "Jeff" Sipek 	struct inode *inode = file->f_path.dentry->d_inode;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
3801da177e4SLinus Torvalds 	if (atomic_read(&inode->i_writecount) > 0) {
3811da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
3821da177e4SLinus Torvalds 		return -ETXTBSY;
3831da177e4SLinus Torvalds 	}
3841da177e4SLinus Torvalds 	atomic_dec(&inode->i_writecount);
3851da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	return 0;
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
3901d957f9bSJan Blunck /**
3915dd784d0SJan Blunck  * path_get - get a reference to a path
3925dd784d0SJan Blunck  * @path: path to get the reference to
3935dd784d0SJan Blunck  *
3945dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3955dd784d0SJan Blunck  */
3965dd784d0SJan Blunck void path_get(struct path *path)
3975dd784d0SJan Blunck {
3985dd784d0SJan Blunck 	mntget(path->mnt);
3995dd784d0SJan Blunck 	dget(path->dentry);
4005dd784d0SJan Blunck }
4015dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
4025dd784d0SJan Blunck 
4035dd784d0SJan Blunck /**
4041d957f9bSJan Blunck  * path_put - put a reference to a path
4051d957f9bSJan Blunck  * @path: path to put the reference to
4061d957f9bSJan Blunck  *
4071d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
4081d957f9bSJan Blunck  */
4091d957f9bSJan Blunck void path_put(struct path *path)
4101da177e4SLinus Torvalds {
4111d957f9bSJan Blunck 	dput(path->dentry);
4121d957f9bSJan Blunck 	mntput(path->mnt);
4131da177e4SLinus Torvalds }
4141d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
4151da177e4SLinus Torvalds 
41619660af7SAl Viro /*
41731e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
41819660af7SAl Viro  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
41919660af7SAl Viro  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
42019660af7SAl Viro  * normal reference counts on dentries and vfsmounts to transition to rcu-walk
42119660af7SAl Viro  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
42219660af7SAl Viro  * got stuck, so ref-walk may continue from there. If this is not successful
42319660af7SAl Viro  * (eg. a seqcount has changed), then failure is returned and it's up to caller
42419660af7SAl Viro  * to restart the path walk from the beginning in ref-walk mode.
42531e6b01fSNick Piggin  */
42631e6b01fSNick Piggin 
42731e6b01fSNick Piggin /**
42819660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
42919660af7SAl Viro  * @nd: nameidata pathwalk data
43019660af7SAl Viro  * @dentry: child of nd->path.dentry or NULL
43139191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
43231e6b01fSNick Piggin  *
43319660af7SAl Viro  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
43419660af7SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
43519660af7SAl Viro  * @nd or NULL.  Must be called from rcu-walk context.
43631e6b01fSNick Piggin  */
43719660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
43831e6b01fSNick Piggin {
43931e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
44031e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
4415b6ca027SAl Viro 	int want_root = 0;
44231e6b01fSNick Piggin 
44331e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
4445b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
4455b6ca027SAl Viro 		want_root = 1;
44631e6b01fSNick Piggin 		spin_lock(&fs->lock);
44731e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
44831e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
44931e6b01fSNick Piggin 			goto err_root;
45031e6b01fSNick Piggin 	}
45131e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
45219660af7SAl Viro 	if (!dentry) {
45319660af7SAl Viro 		if (!__d_rcu_to_refcount(parent, nd->seq))
45419660af7SAl Viro 			goto err_parent;
45519660af7SAl Viro 		BUG_ON(nd->inode != parent->d_inode);
45619660af7SAl Viro 	} else {
45794c0d4ecSAl Viro 		if (dentry->d_parent != parent)
45894c0d4ecSAl Viro 			goto err_parent;
45931e6b01fSNick Piggin 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
46031e6b01fSNick Piggin 		if (!__d_rcu_to_refcount(dentry, nd->seq))
46119660af7SAl Viro 			goto err_child;
46231e6b01fSNick Piggin 		/*
46319660af7SAl Viro 		 * If the sequence check on the child dentry passed, then
46419660af7SAl Viro 		 * the child has not been removed from its parent. This
46519660af7SAl Viro 		 * means the parent dentry must be valid and able to take
46619660af7SAl Viro 		 * a reference at this point.
46731e6b01fSNick Piggin 		 */
46831e6b01fSNick Piggin 		BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
46931e6b01fSNick Piggin 		BUG_ON(!parent->d_count);
47031e6b01fSNick Piggin 		parent->d_count++;
47131e6b01fSNick Piggin 		spin_unlock(&dentry->d_lock);
47219660af7SAl Viro 	}
47331e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
4745b6ca027SAl Viro 	if (want_root) {
47531e6b01fSNick Piggin 		path_get(&nd->root);
47631e6b01fSNick Piggin 		spin_unlock(&fs->lock);
47731e6b01fSNick Piggin 	}
47831e6b01fSNick Piggin 	mntget(nd->path.mnt);
47931e6b01fSNick Piggin 
48031e6b01fSNick Piggin 	rcu_read_unlock();
48131e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
48231e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
48331e6b01fSNick Piggin 	return 0;
48419660af7SAl Viro 
48519660af7SAl Viro err_child:
48631e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
48719660af7SAl Viro err_parent:
48831e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
48931e6b01fSNick Piggin err_root:
4905b6ca027SAl Viro 	if (want_root)
49131e6b01fSNick Piggin 		spin_unlock(&fs->lock);
49231e6b01fSNick Piggin 	return -ECHILD;
49331e6b01fSNick Piggin }
49431e6b01fSNick Piggin 
49531e6b01fSNick Piggin /**
496834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
497834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
498834f2a4aSTrond Myklebust  */
499834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
500834f2a4aSTrond Myklebust {
5012dab5974SLinus Torvalds 	struct file *file = nd->intent.open.file;
5022dab5974SLinus Torvalds 
5032dab5974SLinus Torvalds 	if (file && !IS_ERR(file)) {
5042dab5974SLinus Torvalds 		if (file->f_path.dentry == NULL)
5052dab5974SLinus Torvalds 			put_filp(file);
506834f2a4aSTrond Myklebust 		else
5072dab5974SLinus Torvalds 			fput(file);
5082dab5974SLinus Torvalds 	}
509834f2a4aSTrond Myklebust }
510834f2a4aSTrond Myklebust 
511f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
51234286d66SNick Piggin {
513f60aef7eSAl Viro 	return dentry->d_op->d_revalidate(dentry, nd);
51434286d66SNick Piggin }
51534286d66SNick Piggin 
516f5e1c1c1SAl Viro static struct dentry *
517bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd)
518bcdc5e01SIan Kent {
519f5e1c1c1SAl Viro 	int status = d_revalidate(dentry, nd);
520bcdc5e01SIan Kent 	if (unlikely(status <= 0)) {
521bcdc5e01SIan Kent 		/*
522bcdc5e01SIan Kent 		 * The dentry failed validation.
523bcdc5e01SIan Kent 		 * If d_revalidate returned 0 attempt to invalidate
524bcdc5e01SIan Kent 		 * the dentry otherwise d_revalidate is asking us
525bcdc5e01SIan Kent 		 * to return a fail status.
526bcdc5e01SIan Kent 		 */
52734286d66SNick Piggin 		if (status < 0) {
52834286d66SNick Piggin 			dput(dentry);
52934286d66SNick Piggin 			dentry = ERR_PTR(status);
530f5e1c1c1SAl Viro 		} else if (!d_invalidate(dentry)) {
531bcdc5e01SIan Kent 			dput(dentry);
532bcdc5e01SIan Kent 			dentry = NULL;
533bcdc5e01SIan Kent 		}
534bcdc5e01SIan Kent 	}
535f5e1c1c1SAl Viro 	return dentry;
536f5e1c1c1SAl Viro }
537f5e1c1c1SAl Viro 
5389f1fafeeSAl Viro /**
5399f1fafeeSAl Viro  * complete_walk - successful completion of path walk
5409f1fafeeSAl Viro  * @nd:  pointer nameidata
54139159de2SJeff Layton  *
5429f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
5439f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
5449f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
5459f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
5469f1fafeeSAl Viro  * need to drop nd->path.
54739159de2SJeff Layton  */
5489f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
54939159de2SJeff Layton {
55016c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
55139159de2SJeff Layton 	int status;
55239159de2SJeff Layton 
5539f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
5549f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
5559f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
5569f1fafeeSAl Viro 			nd->root.mnt = NULL;
5579f1fafeeSAl Viro 		spin_lock(&dentry->d_lock);
5589f1fafeeSAl Viro 		if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
5599f1fafeeSAl Viro 			spin_unlock(&dentry->d_lock);
5609f1fafeeSAl Viro 			rcu_read_unlock();
5619f1fafeeSAl Viro 			br_read_unlock(vfsmount_lock);
5629f1fafeeSAl Viro 			return -ECHILD;
5639f1fafeeSAl Viro 		}
5649f1fafeeSAl Viro 		BUG_ON(nd->inode != dentry->d_inode);
5659f1fafeeSAl Viro 		spin_unlock(&dentry->d_lock);
5669f1fafeeSAl Viro 		mntget(nd->path.mnt);
5679f1fafeeSAl Viro 		rcu_read_unlock();
5689f1fafeeSAl Viro 		br_read_unlock(vfsmount_lock);
5699f1fafeeSAl Viro 	}
5709f1fafeeSAl Viro 
57116c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
57239159de2SJeff Layton 		return 0;
57339159de2SJeff Layton 
57416c2cd71SAl Viro 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
57516c2cd71SAl Viro 		return 0;
57616c2cd71SAl Viro 
57716c2cd71SAl Viro 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
57816c2cd71SAl Viro 		return 0;
57916c2cd71SAl Viro 
58016c2cd71SAl Viro 	/* Note: we do not d_invalidate() */
58134286d66SNick Piggin 	status = d_revalidate(dentry, nd);
58239159de2SJeff Layton 	if (status > 0)
58339159de2SJeff Layton 		return 0;
58439159de2SJeff Layton 
58516c2cd71SAl Viro 	if (!status)
58639159de2SJeff Layton 		status = -ESTALE;
58716c2cd71SAl Viro 
5889f1fafeeSAl Viro 	path_put(&nd->path);
58939159de2SJeff Layton 	return status;
59039159de2SJeff Layton }
59139159de2SJeff Layton 
5922a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
5932a737871SAl Viro {
594f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
595f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
5962a737871SAl Viro }
5972a737871SAl Viro 
5986de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
5996de88d72SAl Viro 
60031e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
60131e6b01fSNick Piggin {
60231e6b01fSNick Piggin 	if (!nd->root.mnt) {
60331e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
604c28cc364SNick Piggin 		unsigned seq;
605c28cc364SNick Piggin 
606c28cc364SNick Piggin 		do {
607c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
60831e6b01fSNick Piggin 			nd->root = fs->root;
609c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
610c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
61131e6b01fSNick Piggin 	}
61231e6b01fSNick Piggin }
61331e6b01fSNick Piggin 
614f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
6151da177e4SLinus Torvalds {
61631e6b01fSNick Piggin 	int ret;
61731e6b01fSNick Piggin 
6181da177e4SLinus Torvalds 	if (IS_ERR(link))
6191da177e4SLinus Torvalds 		goto fail;
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	if (*link == '/') {
6222a737871SAl Viro 		set_root(nd);
6231d957f9bSJan Blunck 		path_put(&nd->path);
6242a737871SAl Viro 		nd->path = nd->root;
6252a737871SAl Viro 		path_get(&nd->root);
62616c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
6271da177e4SLinus Torvalds 	}
62831e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
629b4091d5fSChristoph Hellwig 
63031e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
63131e6b01fSNick Piggin 	return ret;
6321da177e4SLinus Torvalds fail:
6331d957f9bSJan Blunck 	path_put(&nd->path);
6341da177e4SLinus Torvalds 	return PTR_ERR(link);
6351da177e4SLinus Torvalds }
6361da177e4SLinus Torvalds 
6371d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
638051d3812SIan Kent {
639051d3812SIan Kent 	dput(path->dentry);
6404ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
641051d3812SIan Kent 		mntput(path->mnt);
642051d3812SIan Kent }
643051d3812SIan Kent 
6447b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
6457b9337aaSNick Piggin 					struct nameidata *nd)
646051d3812SIan Kent {
64731e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
6484ac91378SJan Blunck 		dput(nd->path.dentry);
64931e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
6504ac91378SJan Blunck 			mntput(nd->path.mnt);
6519a229683SHuang Shijie 	}
65231e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
6534ac91378SJan Blunck 	nd->path.dentry = path->dentry;
654051d3812SIan Kent }
655051d3812SIan Kent 
656574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
657574197e0SAl Viro {
658574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
659574197e0SAl Viro 	if (!IS_ERR(cookie) && inode->i_op->put_link)
660574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
661574197e0SAl Viro 	path_put(link);
662574197e0SAl Viro }
663574197e0SAl Viro 
664def4af30SAl Viro static __always_inline int
665574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
6661da177e4SLinus Torvalds {
6671da177e4SLinus Torvalds 	int error;
6687b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
6691da177e4SLinus Torvalds 
670844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
671844a3917SAl Viro 
6720e794589SAl Viro 	if (link->mnt == nd->path.mnt)
6730e794589SAl Viro 		mntget(link->mnt);
6740e794589SAl Viro 
675574197e0SAl Viro 	if (unlikely(current->total_link_count >= 40)) {
676574197e0SAl Viro 		*p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
677574197e0SAl Viro 		path_put(&nd->path);
678574197e0SAl Viro 		return -ELOOP;
679574197e0SAl Viro 	}
680574197e0SAl Viro 	cond_resched();
681574197e0SAl Viro 	current->total_link_count++;
682574197e0SAl Viro 
6837b9337aaSNick Piggin 	touch_atime(link->mnt, dentry);
6841da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
685cd4e91d3SAl Viro 
68636f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
68736f3b4f6SAl Viro 	if (error) {
68836f3b4f6SAl Viro 		*p = ERR_PTR(error); /* no ->put_link(), please */
68936f3b4f6SAl Viro 		path_put(&nd->path);
69036f3b4f6SAl Viro 		return error;
69136f3b4f6SAl Viro 	}
69236f3b4f6SAl Viro 
69386acdca1SAl Viro 	nd->last_type = LAST_BIND;
694def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
695def4af30SAl Viro 	error = PTR_ERR(*p);
696def4af30SAl Viro 	if (!IS_ERR(*p)) {
6971da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
698cc314eefSLinus Torvalds 		error = 0;
6991da177e4SLinus Torvalds 		if (s)
7001da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
701bcda7652SAl Viro 		else if (nd->last_type == LAST_BIND) {
70216c2cd71SAl Viro 			nd->flags |= LOOKUP_JUMPED;
703b21041d0SAl Viro 			nd->inode = nd->path.dentry->d_inode;
704b21041d0SAl Viro 			if (nd->inode->i_op->follow_link) {
705bcda7652SAl Viro 				/* stepped on a _really_ weird one */
706bcda7652SAl Viro 				path_put(&nd->path);
707bcda7652SAl Viro 				error = -ELOOP;
708bcda7652SAl Viro 			}
709bcda7652SAl Viro 		}
7101da177e4SLinus Torvalds 	}
7111da177e4SLinus Torvalds 	return error;
7121da177e4SLinus Torvalds }
7131da177e4SLinus Torvalds 
71431e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
71531e6b01fSNick Piggin {
71631e6b01fSNick Piggin 	struct vfsmount *parent;
71731e6b01fSNick Piggin 	struct dentry *mountpoint;
71831e6b01fSNick Piggin 
71931e6b01fSNick Piggin 	parent = path->mnt->mnt_parent;
72031e6b01fSNick Piggin 	if (parent == path->mnt)
72131e6b01fSNick Piggin 		return 0;
72231e6b01fSNick Piggin 	mountpoint = path->mnt->mnt_mountpoint;
72331e6b01fSNick Piggin 	path->dentry = mountpoint;
72431e6b01fSNick Piggin 	path->mnt = parent;
72531e6b01fSNick Piggin 	return 1;
72631e6b01fSNick Piggin }
72731e6b01fSNick Piggin 
728bab77ebfSAl Viro int follow_up(struct path *path)
7291da177e4SLinus Torvalds {
7301da177e4SLinus Torvalds 	struct vfsmount *parent;
7311da177e4SLinus Torvalds 	struct dentry *mountpoint;
73299b7db7bSNick Piggin 
73399b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
734bab77ebfSAl Viro 	parent = path->mnt->mnt_parent;
735bab77ebfSAl Viro 	if (parent == path->mnt) {
73699b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
7371da177e4SLinus Torvalds 		return 0;
7381da177e4SLinus Torvalds 	}
7391da177e4SLinus Torvalds 	mntget(parent);
740bab77ebfSAl Viro 	mountpoint = dget(path->mnt->mnt_mountpoint);
74199b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
742bab77ebfSAl Viro 	dput(path->dentry);
743bab77ebfSAl Viro 	path->dentry = mountpoint;
744bab77ebfSAl Viro 	mntput(path->mnt);
745bab77ebfSAl Viro 	path->mnt = parent;
7461da177e4SLinus Torvalds 	return 1;
7471da177e4SLinus Torvalds }
7481da177e4SLinus Torvalds 
749b5c84bf6SNick Piggin /*
7509875cf80SDavid Howells  * Perform an automount
7519875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
7529875cf80SDavid Howells  *   were called with.
7531da177e4SLinus Torvalds  */
7549875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
7559875cf80SDavid Howells 			    bool *need_mntput)
75631e6b01fSNick Piggin {
7579875cf80SDavid Howells 	struct vfsmount *mnt;
758ea5b778aSDavid Howells 	int err;
7599875cf80SDavid Howells 
7609875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
7619875cf80SDavid Howells 		return -EREMOTE;
7629875cf80SDavid Howells 
7636f45b656SDavid Howells 	/* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
7646f45b656SDavid Howells 	 * and this is the terminal part of the path.
7656f45b656SDavid Howells 	 */
7666f45b656SDavid Howells 	if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
7676f45b656SDavid Howells 		return -EISDIR; /* we actually want to stop here */
7686f45b656SDavid Howells 
7699875cf80SDavid Howells 	/* We want to mount if someone is trying to open/create a file of any
7709875cf80SDavid Howells 	 * type under the mountpoint, wants to traverse through the mountpoint
7719875cf80SDavid Howells 	 * or wants to open the mounted directory.
7729875cf80SDavid Howells 	 *
7739875cf80SDavid Howells 	 * We don't want to mount if someone's just doing a stat and they've
7749875cf80SDavid Howells 	 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
7759875cf80SDavid Howells 	 * appended a '/' to the name.
7769875cf80SDavid Howells 	 */
7779875cf80SDavid Howells 	if (!(flags & LOOKUP_FOLLOW) &&
7789875cf80SDavid Howells 	    !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
7799875cf80SDavid Howells 		       LOOKUP_OPEN | LOOKUP_CREATE)))
7809875cf80SDavid Howells 		return -EISDIR;
7819875cf80SDavid Howells 
7829875cf80SDavid Howells 	current->total_link_count++;
7839875cf80SDavid Howells 	if (current->total_link_count >= 40)
7849875cf80SDavid Howells 		return -ELOOP;
7859875cf80SDavid Howells 
7869875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
7879875cf80SDavid Howells 	if (IS_ERR(mnt)) {
7889875cf80SDavid Howells 		/*
7899875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
7909875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
7919875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
7929875cf80SDavid Howells 		 *
7939875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
7949875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
7959875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
7969875cf80SDavid Howells 		 */
7979875cf80SDavid Howells 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
7989875cf80SDavid Howells 			return -EREMOTE;
7999875cf80SDavid Howells 		return PTR_ERR(mnt);
80031e6b01fSNick Piggin 	}
801ea5b778aSDavid Howells 
8029875cf80SDavid Howells 	if (!mnt) /* mount collision */
8039875cf80SDavid Howells 		return 0;
8049875cf80SDavid Howells 
8058aef1884SAl Viro 	if (!*need_mntput) {
8068aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
8078aef1884SAl Viro 		mntget(path->mnt);
8088aef1884SAl Viro 		*need_mntput = true;
8098aef1884SAl Viro 	}
81019a167afSAl Viro 	err = finish_automount(mnt, path);
811ea5b778aSDavid Howells 
812ea5b778aSDavid Howells 	switch (err) {
813ea5b778aSDavid Howells 	case -EBUSY:
814ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
81519a167afSAl Viro 		return 0;
816ea5b778aSDavid Howells 	case 0:
8178aef1884SAl Viro 		path_put(path);
8189875cf80SDavid Howells 		path->mnt = mnt;
8199875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
8209875cf80SDavid Howells 		return 0;
82119a167afSAl Viro 	default:
82219a167afSAl Viro 		return err;
8239875cf80SDavid Howells 	}
82419a167afSAl Viro 
825ea5b778aSDavid Howells }
8269875cf80SDavid Howells 
8279875cf80SDavid Howells /*
8289875cf80SDavid Howells  * Handle a dentry that is managed in some way.
829cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
8309875cf80SDavid Howells  * - Flagged as mountpoint
8319875cf80SDavid Howells  * - Flagged as automount point
8329875cf80SDavid Howells  *
8339875cf80SDavid Howells  * This may only be called in refwalk mode.
8349875cf80SDavid Howells  *
8359875cf80SDavid Howells  * Serialization is taken care of in namespace.c
8369875cf80SDavid Howells  */
8379875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
8389875cf80SDavid Howells {
8398aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
8409875cf80SDavid Howells 	unsigned managed;
8419875cf80SDavid Howells 	bool need_mntput = false;
8428aef1884SAl Viro 	int ret = 0;
8439875cf80SDavid Howells 
8449875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
8459875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
8469875cf80SDavid Howells 	 * the components of that value change under us */
8479875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
8489875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
8499875cf80SDavid Howells 	       unlikely(managed != 0)) {
850cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
851cc53ce53SDavid Howells 		 * being held. */
852cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
853cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
854cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
8551aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
856cc53ce53SDavid Howells 			if (ret < 0)
8578aef1884SAl Viro 				break;
858cc53ce53SDavid Howells 		}
859cc53ce53SDavid Howells 
8609875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
8619875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
8629875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
8639875cf80SDavid Howells 			if (mounted) {
8649875cf80SDavid Howells 				dput(path->dentry);
8659875cf80SDavid Howells 				if (need_mntput)
866463ffb2eSAl Viro 					mntput(path->mnt);
867463ffb2eSAl Viro 				path->mnt = mounted;
868463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
8699875cf80SDavid Howells 				need_mntput = true;
8709875cf80SDavid Howells 				continue;
871463ffb2eSAl Viro 			}
872463ffb2eSAl Viro 
8739875cf80SDavid Howells 			/* Something is mounted on this dentry in another
8749875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
8759875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
8769875cf80SDavid Howells 			 * vfsmount_lock */
8771da177e4SLinus Torvalds 		}
8789875cf80SDavid Howells 
8799875cf80SDavid Howells 		/* Handle an automount point */
8809875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
8819875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
8829875cf80SDavid Howells 			if (ret < 0)
8838aef1884SAl Viro 				break;
8849875cf80SDavid Howells 			continue;
8859875cf80SDavid Howells 		}
8869875cf80SDavid Howells 
8879875cf80SDavid Howells 		/* We didn't change the current path point */
8889875cf80SDavid Howells 		break;
8899875cf80SDavid Howells 	}
8908aef1884SAl Viro 
8918aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
8928aef1884SAl Viro 		mntput(path->mnt);
8938aef1884SAl Viro 	if (ret == -EISDIR)
8948aef1884SAl Viro 		ret = 0;
8958aef1884SAl Viro 	return ret;
8961da177e4SLinus Torvalds }
8971da177e4SLinus Torvalds 
898cc53ce53SDavid Howells int follow_down_one(struct path *path)
8991da177e4SLinus Torvalds {
9001da177e4SLinus Torvalds 	struct vfsmount *mounted;
9011da177e4SLinus Torvalds 
9021c755af4SAl Viro 	mounted = lookup_mnt(path);
9031da177e4SLinus Torvalds 	if (mounted) {
9049393bd07SAl Viro 		dput(path->dentry);
9059393bd07SAl Viro 		mntput(path->mnt);
9069393bd07SAl Viro 		path->mnt = mounted;
9079393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
9081da177e4SLinus Torvalds 		return 1;
9091da177e4SLinus Torvalds 	}
9101da177e4SLinus Torvalds 	return 0;
9111da177e4SLinus Torvalds }
9121da177e4SLinus Torvalds 
91362a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
91462a7375eSIan Kent {
91562a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
91662a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
91762a7375eSIan Kent }
91862a7375eSIan Kent 
9199875cf80SDavid Howells /*
920287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
921287548e4SAl Viro  * we meet a managed dentry that would need blocking.
9229875cf80SDavid Howells  */
9239875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
924287548e4SAl Viro 			       struct inode **inode)
9259875cf80SDavid Howells {
92662a7375eSIan Kent 	for (;;) {
9279875cf80SDavid Howells 		struct vfsmount *mounted;
92862a7375eSIan Kent 		/*
92962a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
93062a7375eSIan Kent 		 * that wants to block transit.
93162a7375eSIan Kent 		 */
932287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
933ab90911fSDavid Howells 			return false;
93462a7375eSIan Kent 
93562a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
93662a7375eSIan Kent 			break;
93762a7375eSIan Kent 
9389875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
9399875cf80SDavid Howells 		if (!mounted)
9409875cf80SDavid Howells 			break;
9419875cf80SDavid Howells 		path->mnt = mounted;
9429875cf80SDavid Howells 		path->dentry = mounted->mnt_root;
9439875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
94459430262SLinus Torvalds 		/*
94559430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
94659430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
94759430262SLinus Torvalds 		 * because a mount-point is always pinned.
94859430262SLinus Torvalds 		 */
94959430262SLinus Torvalds 		*inode = path->dentry->d_inode;
9509875cf80SDavid Howells 	}
9519875cf80SDavid Howells 	return true;
9529875cf80SDavid Howells }
9539875cf80SDavid Howells 
954dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
955287548e4SAl Viro {
956dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
957287548e4SAl Viro 		struct vfsmount *mounted;
958dea39376SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
959287548e4SAl Viro 		if (!mounted)
960287548e4SAl Viro 			break;
961dea39376SAl Viro 		nd->path.mnt = mounted;
962dea39376SAl Viro 		nd->path.dentry = mounted->mnt_root;
963dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
964287548e4SAl Viro 	}
965287548e4SAl Viro }
966287548e4SAl Viro 
96731e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
96831e6b01fSNick Piggin {
96931e6b01fSNick Piggin 	set_root_rcu(nd);
97031e6b01fSNick Piggin 
97131e6b01fSNick Piggin 	while (1) {
97231e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
97331e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
97431e6b01fSNick Piggin 			break;
97531e6b01fSNick Piggin 		}
97631e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
97731e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
97831e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
97931e6b01fSNick Piggin 			unsigned seq;
98031e6b01fSNick Piggin 
98131e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
98231e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
983ef7562d5SAl Viro 				goto failed;
98431e6b01fSNick Piggin 			nd->path.dentry = parent;
98531e6b01fSNick Piggin 			nd->seq = seq;
98631e6b01fSNick Piggin 			break;
98731e6b01fSNick Piggin 		}
98831e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
98931e6b01fSNick Piggin 			break;
99031e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
99131e6b01fSNick Piggin 	}
992dea39376SAl Viro 	follow_mount_rcu(nd);
993dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
99431e6b01fSNick Piggin 	return 0;
995ef7562d5SAl Viro 
996ef7562d5SAl Viro failed:
997ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
9985b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
999ef7562d5SAl Viro 		nd->root.mnt = NULL;
1000ef7562d5SAl Viro 	rcu_read_unlock();
1001ef7562d5SAl Viro 	br_read_unlock(vfsmount_lock);
1002ef7562d5SAl Viro 	return -ECHILD;
100331e6b01fSNick Piggin }
100431e6b01fSNick Piggin 
10059875cf80SDavid Howells /*
1006cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
1007cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
1008cc53ce53SDavid Howells  * caller is permitted to proceed or not.
1009cc53ce53SDavid Howells  */
10107cc90cc3SAl Viro int follow_down(struct path *path)
1011cc53ce53SDavid Howells {
1012cc53ce53SDavid Howells 	unsigned managed;
1013cc53ce53SDavid Howells 	int ret;
1014cc53ce53SDavid Howells 
1015cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
1016cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1017cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1018cc53ce53SDavid Howells 		 * being held.
1019cc53ce53SDavid Howells 		 *
1020cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
1021cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
1022cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
1023cc53ce53SDavid Howells 		 * superstructure.
1024cc53ce53SDavid Howells 		 *
1025cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
1026cc53ce53SDavid Howells 		 */
1027cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1028cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1029cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
1030ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
10311aed3e42SAl Viro 				path->dentry, false);
1032cc53ce53SDavid Howells 			if (ret < 0)
1033cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
1034cc53ce53SDavid Howells 		}
1035cc53ce53SDavid Howells 
1036cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
1037cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
1038cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
1039cc53ce53SDavid Howells 			if (!mounted)
1040cc53ce53SDavid Howells 				break;
1041cc53ce53SDavid Howells 			dput(path->dentry);
1042cc53ce53SDavid Howells 			mntput(path->mnt);
1043cc53ce53SDavid Howells 			path->mnt = mounted;
1044cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1045cc53ce53SDavid Howells 			continue;
1046cc53ce53SDavid Howells 		}
1047cc53ce53SDavid Howells 
1048cc53ce53SDavid Howells 		/* Don't handle automount points here */
1049cc53ce53SDavid Howells 		break;
1050cc53ce53SDavid Howells 	}
1051cc53ce53SDavid Howells 	return 0;
1052cc53ce53SDavid Howells }
1053cc53ce53SDavid Howells 
1054cc53ce53SDavid Howells /*
10559875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
10569875cf80SDavid Howells  */
10579875cf80SDavid Howells static void follow_mount(struct path *path)
10589875cf80SDavid Howells {
10599875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
10609875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
10619875cf80SDavid Howells 		if (!mounted)
10629875cf80SDavid Howells 			break;
10639875cf80SDavid Howells 		dput(path->dentry);
10649875cf80SDavid Howells 		mntput(path->mnt);
10659875cf80SDavid Howells 		path->mnt = mounted;
10669875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
10679875cf80SDavid Howells 	}
10689875cf80SDavid Howells }
10699875cf80SDavid Howells 
107031e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
10711da177e4SLinus Torvalds {
10722a737871SAl Viro 	set_root(nd);
1073e518ddb7SAndreas Mohr 
10741da177e4SLinus Torvalds 	while(1) {
10754ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
10761da177e4SLinus Torvalds 
10772a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
10782a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
10791da177e4SLinus Torvalds 			break;
10801da177e4SLinus Torvalds 		}
10814ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
10823088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
10833088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
10841da177e4SLinus Torvalds 			dput(old);
10851da177e4SLinus Torvalds 			break;
10861da177e4SLinus Torvalds 		}
10873088dd70SAl Viro 		if (!follow_up(&nd->path))
10881da177e4SLinus Torvalds 			break;
10891da177e4SLinus Torvalds 	}
109079ed0226SAl Viro 	follow_mount(&nd->path);
109131e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
10921da177e4SLinus Torvalds }
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds /*
1095baa03890SNick Piggin  * Allocate a dentry with name and parent, and perform a parent
1096baa03890SNick Piggin  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1097baa03890SNick Piggin  * on error. parent->d_inode->i_mutex must be held. d_lookup must
1098baa03890SNick Piggin  * have verified that no child exists while under i_mutex.
1099baa03890SNick Piggin  */
1100baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1101baa03890SNick Piggin 				struct qstr *name, struct nameidata *nd)
1102baa03890SNick Piggin {
1103baa03890SNick Piggin 	struct inode *inode = parent->d_inode;
1104baa03890SNick Piggin 	struct dentry *dentry;
1105baa03890SNick Piggin 	struct dentry *old;
1106baa03890SNick Piggin 
1107baa03890SNick Piggin 	/* Don't create child dentry for a dead directory. */
1108baa03890SNick Piggin 	if (unlikely(IS_DEADDIR(inode)))
1109baa03890SNick Piggin 		return ERR_PTR(-ENOENT);
1110baa03890SNick Piggin 
1111baa03890SNick Piggin 	dentry = d_alloc(parent, name);
1112baa03890SNick Piggin 	if (unlikely(!dentry))
1113baa03890SNick Piggin 		return ERR_PTR(-ENOMEM);
1114baa03890SNick Piggin 
1115baa03890SNick Piggin 	old = inode->i_op->lookup(inode, dentry, nd);
1116baa03890SNick Piggin 	if (unlikely(old)) {
1117baa03890SNick Piggin 		dput(dentry);
1118baa03890SNick Piggin 		dentry = old;
1119baa03890SNick Piggin 	}
1120baa03890SNick Piggin 	return dentry;
1121baa03890SNick Piggin }
1122baa03890SNick Piggin 
1123baa03890SNick Piggin /*
112444396f4bSJosef Bacik  * We already have a dentry, but require a lookup to be performed on the parent
112544396f4bSJosef Bacik  * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
112644396f4bSJosef Bacik  * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
112744396f4bSJosef Bacik  * child exists while under i_mutex.
112844396f4bSJosef Bacik  */
112944396f4bSJosef Bacik static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
113044396f4bSJosef Bacik 				     struct nameidata *nd)
113144396f4bSJosef Bacik {
113244396f4bSJosef Bacik 	struct inode *inode = parent->d_inode;
113344396f4bSJosef Bacik 	struct dentry *old;
113444396f4bSJosef Bacik 
113544396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
113644396f4bSJosef Bacik 	if (unlikely(IS_DEADDIR(inode)))
113744396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
113844396f4bSJosef Bacik 
113944396f4bSJosef Bacik 	old = inode->i_op->lookup(inode, dentry, nd);
114044396f4bSJosef Bacik 	if (unlikely(old)) {
114144396f4bSJosef Bacik 		dput(dentry);
114244396f4bSJosef Bacik 		dentry = old;
114344396f4bSJosef Bacik 	}
114444396f4bSJosef Bacik 	return dentry;
114544396f4bSJosef Bacik }
114644396f4bSJosef Bacik 
114744396f4bSJosef Bacik /*
11481da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
11491da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
11501da177e4SLinus Torvalds  *  It _is_ time-critical.
11511da177e4SLinus Torvalds  */
11521da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
115331e6b01fSNick Piggin 			struct path *path, struct inode **inode)
11541da177e4SLinus Torvalds {
11554ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
115631e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
11575a18fff2SAl Viro 	int need_reval = 1;
11585a18fff2SAl Viro 	int status = 1;
11599875cf80SDavid Howells 	int err;
11609875cf80SDavid Howells 
11613cac260aSAl Viro 	/*
1162b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1163b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1164b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1165b04f784eSNick Piggin 	 */
116631e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
116731e6b01fSNick Piggin 		unsigned seq;
116831e6b01fSNick Piggin 		*inode = nd->inode;
116931e6b01fSNick Piggin 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
11705a18fff2SAl Viro 		if (!dentry)
11715a18fff2SAl Viro 			goto unlazy;
11725a18fff2SAl Viro 
117331e6b01fSNick Piggin 		/* Memory barrier in read_seqcount_begin of child is enough */
117431e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
117531e6b01fSNick Piggin 			return -ECHILD;
117631e6b01fSNick Piggin 		nd->seq = seq;
11775a18fff2SAl Viro 
117824643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
11795a18fff2SAl Viro 			status = d_revalidate(dentry, nd);
11805a18fff2SAl Viro 			if (unlikely(status <= 0)) {
11815a18fff2SAl Viro 				if (status != -ECHILD)
11825a18fff2SAl Viro 					need_reval = 0;
11835a18fff2SAl Viro 				goto unlazy;
11845a18fff2SAl Viro 			}
118524643087SAl Viro 		}
118644396f4bSJosef Bacik 		if (unlikely(d_need_lookup(dentry)))
118744396f4bSJosef Bacik 			goto unlazy;
118831e6b01fSNick Piggin 		path->mnt = mnt;
118931e6b01fSNick Piggin 		path->dentry = dentry;
1190d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1191d6e9bd25SAl Viro 			goto unlazy;
1192d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1193d6e9bd25SAl Viro 			goto unlazy;
11949875cf80SDavid Howells 		return 0;
11955a18fff2SAl Viro unlazy:
119619660af7SAl Viro 		if (unlazy_walk(nd, dentry))
11975a18fff2SAl Viro 			return -ECHILD;
11985a18fff2SAl Viro 	} else {
119931e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
120024643087SAl Viro 	}
12015a18fff2SAl Viro 
120244396f4bSJosef Bacik 	if (dentry && unlikely(d_need_lookup(dentry))) {
120344396f4bSJosef Bacik 		dput(dentry);
120444396f4bSJosef Bacik 		dentry = NULL;
120544396f4bSJosef Bacik 	}
12065a18fff2SAl Viro retry:
12075a18fff2SAl Viro 	if (unlikely(!dentry)) {
12085a18fff2SAl Viro 		struct inode *dir = parent->d_inode;
12095a18fff2SAl Viro 		BUG_ON(nd->inode != dir);
12105a18fff2SAl Viro 
12115a18fff2SAl Viro 		mutex_lock(&dir->i_mutex);
12125a18fff2SAl Viro 		dentry = d_lookup(parent, name);
12135a18fff2SAl Viro 		if (likely(!dentry)) {
12145a18fff2SAl Viro 			dentry = d_alloc_and_lookup(parent, name, nd);
12155a18fff2SAl Viro 			if (IS_ERR(dentry)) {
12165a18fff2SAl Viro 				mutex_unlock(&dir->i_mutex);
12175a18fff2SAl Viro 				return PTR_ERR(dentry);
12185a18fff2SAl Viro 			}
12195a18fff2SAl Viro 			/* known good */
12205a18fff2SAl Viro 			need_reval = 0;
12215a18fff2SAl Viro 			status = 1;
122244396f4bSJosef Bacik 		} else if (unlikely(d_need_lookup(dentry))) {
122344396f4bSJosef Bacik 			dentry = d_inode_lookup(parent, dentry, nd);
122444396f4bSJosef Bacik 			if (IS_ERR(dentry)) {
122544396f4bSJosef Bacik 				mutex_unlock(&dir->i_mutex);
122644396f4bSJosef Bacik 				return PTR_ERR(dentry);
122744396f4bSJosef Bacik 			}
122844396f4bSJosef Bacik 			/* known good */
122944396f4bSJosef Bacik 			need_reval = 0;
123044396f4bSJosef Bacik 			status = 1;
12315a18fff2SAl Viro 		}
12325a18fff2SAl Viro 		mutex_unlock(&dir->i_mutex);
12335a18fff2SAl Viro 	}
12345a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
12355a18fff2SAl Viro 		status = d_revalidate(dentry, nd);
12365a18fff2SAl Viro 	if (unlikely(status <= 0)) {
12375a18fff2SAl Viro 		if (status < 0) {
12385a18fff2SAl Viro 			dput(dentry);
12395a18fff2SAl Viro 			return status;
12405a18fff2SAl Viro 		}
12415a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
12425a18fff2SAl Viro 			dput(dentry);
12435a18fff2SAl Viro 			dentry = NULL;
12445a18fff2SAl Viro 			need_reval = 1;
12455a18fff2SAl Viro 			goto retry;
12465a18fff2SAl Viro 		}
12475a18fff2SAl Viro 	}
12485a18fff2SAl Viro 
12491da177e4SLinus Torvalds 	path->mnt = mnt;
12501da177e4SLinus Torvalds 	path->dentry = dentry;
12519875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
125289312214SIan Kent 	if (unlikely(err < 0)) {
125389312214SIan Kent 		path_put_conditional(path, nd);
12549875cf80SDavid Howells 		return err;
125589312214SIan Kent 	}
125631e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
12571da177e4SLinus Torvalds 	return 0;
12581da177e4SLinus Torvalds }
12591da177e4SLinus Torvalds 
126052094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
126152094c8aSAl Viro {
126252094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
126352094c8aSAl Viro 		int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
126452094c8aSAl Viro 		if (err != -ECHILD)
126552094c8aSAl Viro 			return err;
126619660af7SAl Viro 		if (unlazy_walk(nd, NULL))
126752094c8aSAl Viro 			return -ECHILD;
126852094c8aSAl Viro 	}
126952094c8aSAl Viro 	return exec_permission(nd->inode, 0);
127052094c8aSAl Viro }
127152094c8aSAl Viro 
12729856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
12739856fa1bSAl Viro {
12749856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
12759856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
12769856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
12779856fa1bSAl Viro 				return -ECHILD;
12789856fa1bSAl Viro 		} else
12799856fa1bSAl Viro 			follow_dotdot(nd);
12809856fa1bSAl Viro 	}
12819856fa1bSAl Viro 	return 0;
12829856fa1bSAl Viro }
12839856fa1bSAl Viro 
1284951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1285951361f9SAl Viro {
1286951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1287951361f9SAl Viro 		path_put(&nd->path);
1288951361f9SAl Viro 	} else {
1289951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
12905b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1291951361f9SAl Viro 			nd->root.mnt = NULL;
1292951361f9SAl Viro 		rcu_read_unlock();
1293951361f9SAl Viro 		br_read_unlock(vfsmount_lock);
1294951361f9SAl Viro 	}
1295951361f9SAl Viro }
1296951361f9SAl Viro 
1297ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
1298ce57dfc1SAl Viro 		struct qstr *name, int type, int follow)
1299ce57dfc1SAl Viro {
1300ce57dfc1SAl Viro 	struct inode *inode;
1301ce57dfc1SAl Viro 	int err;
1302ce57dfc1SAl Viro 	/*
1303ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1304ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1305ce57dfc1SAl Viro 	 * parent relationships.
1306ce57dfc1SAl Viro 	 */
1307ce57dfc1SAl Viro 	if (unlikely(type != LAST_NORM))
1308ce57dfc1SAl Viro 		return handle_dots(nd, type);
1309ce57dfc1SAl Viro 	err = do_lookup(nd, name, path, &inode);
1310ce57dfc1SAl Viro 	if (unlikely(err)) {
1311ce57dfc1SAl Viro 		terminate_walk(nd);
1312ce57dfc1SAl Viro 		return err;
1313ce57dfc1SAl Viro 	}
1314ce57dfc1SAl Viro 	if (!inode) {
1315ce57dfc1SAl Viro 		path_to_nameidata(path, nd);
1316ce57dfc1SAl Viro 		terminate_walk(nd);
1317ce57dfc1SAl Viro 		return -ENOENT;
1318ce57dfc1SAl Viro 	}
1319ce57dfc1SAl Viro 	if (unlikely(inode->i_op->follow_link) && follow) {
132019660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
132119660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
132219660af7SAl Viro 				terminate_walk(nd);
1323ce57dfc1SAl Viro 				return -ECHILD;
132419660af7SAl Viro 			}
132519660af7SAl Viro 		}
1326ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1327ce57dfc1SAl Viro 		return 1;
1328ce57dfc1SAl Viro 	}
1329ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1330ce57dfc1SAl Viro 	nd->inode = inode;
1331ce57dfc1SAl Viro 	return 0;
1332ce57dfc1SAl Viro }
1333ce57dfc1SAl Viro 
13341da177e4SLinus Torvalds /*
1335b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1336b356379aSAl Viro  * limiting consecutive symlinks to 40.
1337b356379aSAl Viro  *
1338b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1339b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1340b356379aSAl Viro  */
1341b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1342b356379aSAl Viro {
1343b356379aSAl Viro 	int res;
1344b356379aSAl Viro 
1345b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1346b356379aSAl Viro 		path_put_conditional(path, nd);
1347b356379aSAl Viro 		path_put(&nd->path);
1348b356379aSAl Viro 		return -ELOOP;
1349b356379aSAl Viro 	}
13501a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1351b356379aSAl Viro 
1352b356379aSAl Viro 	nd->depth++;
1353b356379aSAl Viro 	current->link_count++;
1354b356379aSAl Viro 
1355b356379aSAl Viro 	do {
1356b356379aSAl Viro 		struct path link = *path;
1357b356379aSAl Viro 		void *cookie;
1358574197e0SAl Viro 
1359574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
1360b356379aSAl Viro 		if (!res)
1361b356379aSAl Viro 			res = walk_component(nd, path, &nd->last,
1362b356379aSAl Viro 					     nd->last_type, LOOKUP_FOLLOW);
1363574197e0SAl Viro 		put_link(nd, &link, cookie);
1364b356379aSAl Viro 	} while (res > 0);
1365b356379aSAl Viro 
1366b356379aSAl Viro 	current->link_count--;
1367b356379aSAl Viro 	nd->depth--;
1368b356379aSAl Viro 	return res;
1369b356379aSAl Viro }
1370b356379aSAl Viro 
1371b356379aSAl Viro /*
13721da177e4SLinus Torvalds  * Name resolution.
1373ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1374ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
13751da177e4SLinus Torvalds  *
1376ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1377ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
13781da177e4SLinus Torvalds  */
13796de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
13801da177e4SLinus Torvalds {
13811da177e4SLinus Torvalds 	struct path next;
13821da177e4SLinus Torvalds 	int err;
13831da177e4SLinus Torvalds 	unsigned int lookup_flags = nd->flags;
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds 	while (*name=='/')
13861da177e4SLinus Torvalds 		name++;
13871da177e4SLinus Torvalds 	if (!*name)
1388086e183aSAl Viro 		return 0;
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
13911da177e4SLinus Torvalds 	for(;;) {
13921da177e4SLinus Torvalds 		unsigned long hash;
13931da177e4SLinus Torvalds 		struct qstr this;
13941da177e4SLinus Torvalds 		unsigned int c;
1395fe479a58SAl Viro 		int type;
13961da177e4SLinus Torvalds 
1397cdce5d6bSTrond Myklebust 		nd->flags |= LOOKUP_CONTINUE;
139852094c8aSAl Viro 
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 		}
14531da177e4SLinus Torvalds 		err = -ENOTDIR;
145431e6b01fSNick Piggin 		if (!nd->inode->i_op->lookup)
14551da177e4SLinus Torvalds 			break;
14561da177e4SLinus Torvalds 		continue;
14571da177e4SLinus Torvalds 		/* here ends the main loop */
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds last_component:
1460f55eab82STrond Myklebust 		/* Clear LOOKUP_CONTINUE iff it was previously unset */
1461f55eab82STrond Myklebust 		nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
1462ce57dfc1SAl Viro 		nd->last = this;
1463ce57dfc1SAl Viro 		nd->last_type = type;
1464ce57dfc1SAl Viro 		return 0;
1465ce57dfc1SAl Viro 	}
1466951361f9SAl Viro 	terminate_walk(nd);
14671da177e4SLinus Torvalds 	return err;
14681da177e4SLinus Torvalds }
14691da177e4SLinus Torvalds 
147070e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
147170e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
147231e6b01fSNick Piggin {
147331e6b01fSNick Piggin 	int retval = 0;
147431e6b01fSNick Piggin 	int fput_needed;
147531e6b01fSNick Piggin 	struct file *file;
147631e6b01fSNick Piggin 
147731e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
147816c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
147931e6b01fSNick Piggin 	nd->depth = 0;
14805b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
14815b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
148273d049a4SAl Viro 		if (*name) {
14835b6ca027SAl Viro 			if (!inode->i_op->lookup)
14845b6ca027SAl Viro 				return -ENOTDIR;
14855b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
14865b6ca027SAl Viro 			if (retval)
14875b6ca027SAl Viro 				return retval;
148873d049a4SAl Viro 		}
14895b6ca027SAl Viro 		nd->path = nd->root;
14905b6ca027SAl Viro 		nd->inode = inode;
14915b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
14925b6ca027SAl Viro 			br_read_lock(vfsmount_lock);
14935b6ca027SAl Viro 			rcu_read_lock();
14945b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
14955b6ca027SAl Viro 		} else {
14965b6ca027SAl Viro 			path_get(&nd->path);
14975b6ca027SAl Viro 		}
14985b6ca027SAl Viro 		return 0;
14995b6ca027SAl Viro 	}
15005b6ca027SAl Viro 
150131e6b01fSNick Piggin 	nd->root.mnt = NULL;
150231e6b01fSNick Piggin 
150331e6b01fSNick Piggin 	if (*name=='/') {
1504e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
150531e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
150631e6b01fSNick Piggin 			rcu_read_lock();
1507e41f7d4eSAl Viro 			set_root_rcu(nd);
1508e41f7d4eSAl Viro 		} else {
1509e41f7d4eSAl Viro 			set_root(nd);
1510e41f7d4eSAl Viro 			path_get(&nd->root);
1511e41f7d4eSAl Viro 		}
151231e6b01fSNick Piggin 		nd->path = nd->root;
151331e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1514e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
151531e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1516c28cc364SNick Piggin 			unsigned seq;
151731e6b01fSNick Piggin 
151831e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
151931e6b01fSNick Piggin 			rcu_read_lock();
152031e6b01fSNick Piggin 
1521c28cc364SNick Piggin 			do {
1522c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
152331e6b01fSNick Piggin 				nd->path = fs->pwd;
1524c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1525c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1526e41f7d4eSAl Viro 		} else {
1527e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1528e41f7d4eSAl Viro 		}
152931e6b01fSNick Piggin 	} else {
153031e6b01fSNick Piggin 		struct dentry *dentry;
153131e6b01fSNick Piggin 
15321abf0c71SAl Viro 		file = fget_raw_light(dfd, &fput_needed);
153331e6b01fSNick Piggin 		retval = -EBADF;
153431e6b01fSNick Piggin 		if (!file)
153531e6b01fSNick Piggin 			goto out_fail;
153631e6b01fSNick Piggin 
153731e6b01fSNick Piggin 		dentry = file->f_path.dentry;
153831e6b01fSNick Piggin 
1539f52e0c11SAl Viro 		if (*name) {
154031e6b01fSNick Piggin 			retval = -ENOTDIR;
154131e6b01fSNick Piggin 			if (!S_ISDIR(dentry->d_inode->i_mode))
154231e6b01fSNick Piggin 				goto fput_fail;
154331e6b01fSNick Piggin 
154478f32a9bSAl Viro 			retval = exec_permission(dentry->d_inode, 0);
154531e6b01fSNick Piggin 			if (retval)
154631e6b01fSNick Piggin 				goto fput_fail;
1547f52e0c11SAl Viro 		}
154831e6b01fSNick Piggin 
154931e6b01fSNick Piggin 		nd->path = file->f_path;
1550e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
155131e6b01fSNick Piggin 			if (fput_needed)
155270e9b357SAl Viro 				*fp = file;
1553c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
155431e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
155531e6b01fSNick Piggin 			rcu_read_lock();
15565590ff0dSUlrich Drepper 		} else {
15575dd784d0SJan Blunck 			path_get(&file->f_path);
15585590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
15591da177e4SLinus Torvalds 		}
1560e41f7d4eSAl Viro 	}
1561e41f7d4eSAl Viro 
156231e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
15639b4a9b14SAl Viro 	return 0;
15642dfdd266SJosef 'Jeff' Sipek 
15659b4a9b14SAl Viro fput_fail:
15669b4a9b14SAl Viro 	fput_light(file, fput_needed);
15679b4a9b14SAl Viro out_fail:
15689b4a9b14SAl Viro 	return retval;
15699b4a9b14SAl Viro }
15709b4a9b14SAl Viro 
1571bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1572bd92d7feSAl Viro {
1573bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1574bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1575bd92d7feSAl Viro 
1576bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1577bd92d7feSAl Viro 	return walk_component(nd, path, &nd->last, nd->last_type,
1578bd92d7feSAl Viro 					nd->flags & LOOKUP_FOLLOW);
1579bd92d7feSAl Viro }
1580bd92d7feSAl Viro 
15819b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1582ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
15839b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
15849b4a9b14SAl Viro {
158570e9b357SAl Viro 	struct file *base = NULL;
1586bd92d7feSAl Viro 	struct path path;
1587bd92d7feSAl Viro 	int err;
158831e6b01fSNick Piggin 
158931e6b01fSNick Piggin 	/*
159031e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
159131e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
159231e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
159331e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
159431e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
159531e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
159631e6b01fSNick Piggin 	 * analogue, foo_rcu().
159731e6b01fSNick Piggin 	 *
159831e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
159931e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
160031e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
160131e6b01fSNick Piggin 	 * be able to complete).
160231e6b01fSNick Piggin 	 */
1603bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1604ee0827cdSAl Viro 
1605bd92d7feSAl Viro 	if (unlikely(err))
1606bd92d7feSAl Viro 		return err;
1607ee0827cdSAl Viro 
1608ee0827cdSAl Viro 	current->total_link_count = 0;
1609bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1610bd92d7feSAl Viro 
1611bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1612bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1613bd92d7feSAl Viro 		while (err > 0) {
1614bd92d7feSAl Viro 			void *cookie;
1615bd92d7feSAl Viro 			struct path link = path;
1616bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1617574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
1618bd92d7feSAl Viro 			if (!err)
1619bd92d7feSAl Viro 				err = lookup_last(nd, &path);
1620574197e0SAl Viro 			put_link(nd, &link, cookie);
1621bd92d7feSAl Viro 		}
1622bd92d7feSAl Viro 	}
1623ee0827cdSAl Viro 
16249f1fafeeSAl Viro 	if (!err)
16259f1fafeeSAl Viro 		err = complete_walk(nd);
1626bd92d7feSAl Viro 
1627bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1628bd92d7feSAl Viro 		if (!nd->inode->i_op->lookup) {
1629bd92d7feSAl Viro 			path_put(&nd->path);
1630bd23a539SAl Viro 			err = -ENOTDIR;
1631bd92d7feSAl Viro 		}
1632bd92d7feSAl Viro 	}
163316c2cd71SAl Viro 
163470e9b357SAl Viro 	if (base)
163570e9b357SAl Viro 		fput(base);
1636ee0827cdSAl Viro 
16375b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
163831e6b01fSNick Piggin 		path_put(&nd->root);
163931e6b01fSNick Piggin 		nd->root.mnt = NULL;
164031e6b01fSNick Piggin 	}
1641bd92d7feSAl Viro 	return err;
164231e6b01fSNick Piggin }
164331e6b01fSNick Piggin 
1644ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1645ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1646ee0827cdSAl Viro {
1647ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1648ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1649ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1650ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1651ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1652ee0827cdSAl Viro 
165331e6b01fSNick Piggin 	if (likely(!retval)) {
165431e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
165531e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
165631e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
165731e6b01fSNick Piggin 		}
165831e6b01fSNick Piggin 	}
1659170aa3d0SUlrich Drepper 	return retval;
16601da177e4SLinus Torvalds }
16611da177e4SLinus Torvalds 
1662c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd)
16635590ff0dSUlrich Drepper {
1664c9c6cac0SAl Viro 	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
16655590ff0dSUlrich Drepper }
16665590ff0dSUlrich Drepper 
1667d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1668d1811465SAl Viro {
1669d1811465SAl Viro 	struct nameidata nd;
1670d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1671d1811465SAl Viro 	if (!res)
1672d1811465SAl Viro 		*path = nd.path;
1673d1811465SAl Viro 	return res;
1674d1811465SAl Viro }
1675d1811465SAl Viro 
167616f18200SJosef 'Jeff' Sipek /**
167716f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
167816f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
167916f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
168016f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
168116f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
168216f18200SJosef 'Jeff' Sipek  * @nd: pointer to nameidata
168316f18200SJosef 'Jeff' Sipek  */
168416f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
168516f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
168616f18200SJosef 'Jeff' Sipek 		    struct nameidata *nd)
168716f18200SJosef 'Jeff' Sipek {
16885b6ca027SAl Viro 	nd->root.dentry = dentry;
16895b6ca027SAl Viro 	nd->root.mnt = mnt;
16905b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
16915b6ca027SAl Viro 	return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
169216f18200SJosef 'Jeff' Sipek }
169316f18200SJosef 'Jeff' Sipek 
1694eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1695eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
16961da177e4SLinus Torvalds {
169781fca444SChristoph Hellwig 	struct inode *inode = base->d_inode;
16981da177e4SLinus Torvalds 	struct dentry *dentry;
16991da177e4SLinus Torvalds 	int err;
17001da177e4SLinus Torvalds 
1701b74c79e9SNick Piggin 	err = exec_permission(inode, 0);
170281fca444SChristoph Hellwig 	if (err)
170381fca444SChristoph Hellwig 		return ERR_PTR(err);
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds 	/*
1706b04f784eSNick Piggin 	 * Don't bother with __d_lookup: callers are for creat as
1707b04f784eSNick Piggin 	 * well as unlink, so a lot of the time it would cost
1708b04f784eSNick Piggin 	 * a double lookup.
17096e6b1bd1SAl Viro 	 */
17106e6b1bd1SAl Viro 	dentry = d_lookup(base, name);
17116e6b1bd1SAl Viro 
171244396f4bSJosef Bacik 	if (dentry && d_need_lookup(dentry)) {
171344396f4bSJosef Bacik 		/*
171444396f4bSJosef Bacik 		 * __lookup_hash is called with the parent dir's i_mutex already
171544396f4bSJosef Bacik 		 * held, so we are good to go here.
171644396f4bSJosef Bacik 		 */
171744396f4bSJosef Bacik 		dentry = d_inode_lookup(base, dentry, nd);
171844396f4bSJosef Bacik 		if (IS_ERR(dentry))
171944396f4bSJosef Bacik 			return dentry;
172044396f4bSJosef Bacik 	}
172144396f4bSJosef Bacik 
1722fb045adbSNick Piggin 	if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
17236e6b1bd1SAl Viro 		dentry = do_revalidate(dentry, nd);
17246e6b1bd1SAl Viro 
17251da177e4SLinus Torvalds 	if (!dentry)
1726baa03890SNick Piggin 		dentry = d_alloc_and_lookup(base, name, nd);
17275a202bcdSAl Viro 
17281da177e4SLinus Torvalds 	return dentry;
17291da177e4SLinus Torvalds }
17301da177e4SLinus Torvalds 
1731057f6c01SJames Morris /*
1732057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1733057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1734057f6c01SJames Morris  * SMP-safe.
1735057f6c01SJames Morris  */
1736a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
17371da177e4SLinus Torvalds {
17384ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
17391da177e4SLinus Torvalds }
17401da177e4SLinus Torvalds 
1741eead1911SChristoph Hellwig /**
1742a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1743eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1744eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1745eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1746eead1911SChristoph Hellwig  *
1747a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1748a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1749eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1750eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1751eead1911SChristoph Hellwig  */
1752057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1753057f6c01SJames Morris {
1754057f6c01SJames Morris 	struct qstr this;
17556a96ba54SAl Viro 	unsigned long hash;
17566a96ba54SAl Viro 	unsigned int c;
1757057f6c01SJames Morris 
17582f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
17592f9092e1SDavid Woodhouse 
17606a96ba54SAl Viro 	this.name = name;
17616a96ba54SAl Viro 	this.len = len;
17626a96ba54SAl Viro 	if (!len)
17636a96ba54SAl Viro 		return ERR_PTR(-EACCES);
17646a96ba54SAl Viro 
17656a96ba54SAl Viro 	hash = init_name_hash();
17666a96ba54SAl Viro 	while (len--) {
17676a96ba54SAl Viro 		c = *(const unsigned char *)name++;
17686a96ba54SAl Viro 		if (c == '/' || c == '\0')
17696a96ba54SAl Viro 			return ERR_PTR(-EACCES);
17706a96ba54SAl Viro 		hash = partial_name_hash(c, hash);
17716a96ba54SAl Viro 	}
17726a96ba54SAl Viro 	this.hash = end_name_hash(hash);
17735a202bcdSAl Viro 	/*
17745a202bcdSAl Viro 	 * See if the low-level filesystem might want
17755a202bcdSAl Viro 	 * to use its own hash..
17765a202bcdSAl Viro 	 */
17775a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
17785a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
17795a202bcdSAl Viro 		if (err < 0)
17805a202bcdSAl Viro 			return ERR_PTR(err);
17815a202bcdSAl Viro 	}
1782eead1911SChristoph Hellwig 
178349705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1784057f6c01SJames Morris }
1785057f6c01SJames Morris 
17862d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
17872d8f3038SAl Viro 		 struct path *path)
17881da177e4SLinus Torvalds {
17892d8f3038SAl Viro 	struct nameidata nd;
1790f52e0c11SAl Viro 	char *tmp = getname_flags(name, flags);
17911da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
17921da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
17932d8f3038SAl Viro 
17942d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
17952d8f3038SAl Viro 
17962d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
17971da177e4SLinus Torvalds 		putname(tmp);
17982d8f3038SAl Viro 		if (!err)
17992d8f3038SAl Viro 			*path = nd.path;
18001da177e4SLinus Torvalds 	}
18011da177e4SLinus Torvalds 	return err;
18021da177e4SLinus Torvalds }
18031da177e4SLinus Torvalds 
18042ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
18052ad94ae6SAl Viro 			struct nameidata *nd, char **name)
18062ad94ae6SAl Viro {
18072ad94ae6SAl Viro 	char *s = getname(path);
18082ad94ae6SAl Viro 	int error;
18092ad94ae6SAl Viro 
18102ad94ae6SAl Viro 	if (IS_ERR(s))
18112ad94ae6SAl Viro 		return PTR_ERR(s);
18122ad94ae6SAl Viro 
18132ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
18142ad94ae6SAl Viro 	if (error)
18152ad94ae6SAl Viro 		putname(s);
18162ad94ae6SAl Viro 	else
18172ad94ae6SAl Viro 		*name = s;
18182ad94ae6SAl Viro 
18192ad94ae6SAl Viro 	return error;
18202ad94ae6SAl Viro }
18212ad94ae6SAl Viro 
18221da177e4SLinus Torvalds /*
18231da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
18241da177e4SLinus Torvalds  * minimal.
18251da177e4SLinus Torvalds  */
18261da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
18271da177e4SLinus Torvalds {
1828da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1829da9592edSDavid Howells 
18301da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
18311da177e4SLinus Torvalds 		return 0;
1832e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
1833e795b717SSerge E. Hallyn 		goto other_userns;
1834da9592edSDavid Howells 	if (inode->i_uid == fsuid)
18351da177e4SLinus Torvalds 		return 0;
1836da9592edSDavid Howells 	if (dir->i_uid == fsuid)
18371da177e4SLinus Torvalds 		return 0;
1838e795b717SSerge E. Hallyn 
1839e795b717SSerge E. Hallyn other_userns:
1840e795b717SSerge E. Hallyn 	return !ns_capable(inode_userns(inode), CAP_FOWNER);
18411da177e4SLinus Torvalds }
18421da177e4SLinus Torvalds 
18431da177e4SLinus Torvalds /*
18441da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
18451da177e4SLinus Torvalds  *  whether the type of victim is right.
18461da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
18471da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
18481da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
18491da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
18501da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
18511da177e4SLinus Torvalds  *	a. be owner of dir, or
18521da177e4SLinus Torvalds  *	b. be owner of victim, or
18531da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
18541da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
18551da177e4SLinus Torvalds  *     links pointing to it.
18561da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
18571da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
18581da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
18591da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
18601da177e4SLinus Torvalds  *     nfs_async_unlink().
18611da177e4SLinus Torvalds  */
1862858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
18631da177e4SLinus Torvalds {
18641da177e4SLinus Torvalds 	int error;
18651da177e4SLinus Torvalds 
18661da177e4SLinus Torvalds 	if (!victim->d_inode)
18671da177e4SLinus Torvalds 		return -ENOENT;
18681da177e4SLinus Torvalds 
18691da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1870cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
18711da177e4SLinus Torvalds 
1872f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
18731da177e4SLinus Torvalds 	if (error)
18741da177e4SLinus Torvalds 		return error;
18751da177e4SLinus Torvalds 	if (IS_APPEND(dir))
18761da177e4SLinus Torvalds 		return -EPERM;
18771da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1878f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
18791da177e4SLinus Torvalds 		return -EPERM;
18801da177e4SLinus Torvalds 	if (isdir) {
18811da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
18821da177e4SLinus Torvalds 			return -ENOTDIR;
18831da177e4SLinus Torvalds 		if (IS_ROOT(victim))
18841da177e4SLinus Torvalds 			return -EBUSY;
18851da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
18861da177e4SLinus Torvalds 		return -EISDIR;
18871da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18881da177e4SLinus Torvalds 		return -ENOENT;
18891da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
18901da177e4SLinus Torvalds 		return -EBUSY;
18911da177e4SLinus Torvalds 	return 0;
18921da177e4SLinus Torvalds }
18931da177e4SLinus Torvalds 
18941da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
18951da177e4SLinus Torvalds  *  dir.
18961da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
18971da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
18981da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
18991da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
19001da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
19011da177e4SLinus Torvalds  */
1902a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
19031da177e4SLinus Torvalds {
19041da177e4SLinus Torvalds 	if (child->d_inode)
19051da177e4SLinus Torvalds 		return -EEXIST;
19061da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
19071da177e4SLinus Torvalds 		return -ENOENT;
1908f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
19091da177e4SLinus Torvalds }
19101da177e4SLinus Torvalds 
19111da177e4SLinus Torvalds /*
19121da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
19131da177e4SLinus Torvalds  */
19141da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
19151da177e4SLinus Torvalds {
19161da177e4SLinus Torvalds 	struct dentry *p;
19171da177e4SLinus Torvalds 
19181da177e4SLinus Torvalds 	if (p1 == p2) {
1919f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
19201da177e4SLinus Torvalds 		return NULL;
19211da177e4SLinus Torvalds 	}
19221da177e4SLinus Torvalds 
1923a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19241da177e4SLinus Torvalds 
1925e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1926e2761a11SOGAWA Hirofumi 	if (p) {
1927f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1928f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
19291da177e4SLinus Torvalds 		return p;
19301da177e4SLinus Torvalds 	}
19311da177e4SLinus Torvalds 
1932e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1933e2761a11SOGAWA Hirofumi 	if (p) {
1934f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1935f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19361da177e4SLinus Torvalds 		return p;
19371da177e4SLinus Torvalds 	}
19381da177e4SLinus Torvalds 
1939f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1940f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19411da177e4SLinus Torvalds 	return NULL;
19421da177e4SLinus Torvalds }
19431da177e4SLinus Torvalds 
19441da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
19451da177e4SLinus Torvalds {
19461b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
19471da177e4SLinus Torvalds 	if (p1 != p2) {
19481b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1949a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19501da177e4SLinus Torvalds 	}
19511da177e4SLinus Torvalds }
19521da177e4SLinus Torvalds 
19531da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
19541da177e4SLinus Torvalds 		struct nameidata *nd)
19551da177e4SLinus Torvalds {
1956a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
19571da177e4SLinus Torvalds 
19581da177e4SLinus Torvalds 	if (error)
19591da177e4SLinus Torvalds 		return error;
19601da177e4SLinus Torvalds 
1961acfa4380SAl Viro 	if (!dir->i_op->create)
19621da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
19631da177e4SLinus Torvalds 	mode &= S_IALLUGO;
19641da177e4SLinus Torvalds 	mode |= S_IFREG;
19651da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
19661da177e4SLinus Torvalds 	if (error)
19671da177e4SLinus Torvalds 		return error;
19681da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1969a74574aaSStephen Smalley 	if (!error)
1970f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
19711da177e4SLinus Torvalds 	return error;
19721da177e4SLinus Torvalds }
19731da177e4SLinus Torvalds 
197473d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
19751da177e4SLinus Torvalds {
19763fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
19771da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
19781da177e4SLinus Torvalds 	int error;
19791da177e4SLinus Torvalds 
1980bcda7652SAl Viro 	/* O_PATH? */
1981bcda7652SAl Viro 	if (!acc_mode)
1982bcda7652SAl Viro 		return 0;
1983bcda7652SAl Viro 
19841da177e4SLinus Torvalds 	if (!inode)
19851da177e4SLinus Torvalds 		return -ENOENT;
19861da177e4SLinus Torvalds 
1987c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1988c8fe8f30SChristoph Hellwig 	case S_IFLNK:
19891da177e4SLinus Torvalds 		return -ELOOP;
1990c8fe8f30SChristoph Hellwig 	case S_IFDIR:
1991c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
19921da177e4SLinus Torvalds 			return -EISDIR;
1993c8fe8f30SChristoph Hellwig 		break;
1994c8fe8f30SChristoph Hellwig 	case S_IFBLK:
1995c8fe8f30SChristoph Hellwig 	case S_IFCHR:
19963fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
19971da177e4SLinus Torvalds 			return -EACCES;
1998c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
1999c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2000c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
20011da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2002c8fe8f30SChristoph Hellwig 		break;
20034a3fd211SDave Hansen 	}
2004b41572e9SDave Hansen 
20053fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2006b41572e9SDave Hansen 	if (error)
2007b41572e9SDave Hansen 		return error;
20086146f0d5SMimi Zohar 
20091da177e4SLinus Torvalds 	/*
20101da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
20111da177e4SLinus Torvalds 	 */
20121da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
20138737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
20147715b521SAl Viro 			return -EPERM;
20151da177e4SLinus Torvalds 		if (flag & O_TRUNC)
20167715b521SAl Viro 			return -EPERM;
20171da177e4SLinus Torvalds 	}
20181da177e4SLinus Torvalds 
20191da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
20202e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
20217715b521SAl Viro 		return -EPERM;
20221da177e4SLinus Torvalds 
20231da177e4SLinus Torvalds 	/*
20241da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
20251da177e4SLinus Torvalds 	 */
2026b65a9cfcSAl Viro 	return break_lease(inode, flag);
20277715b521SAl Viro }
20287715b521SAl Viro 
2029e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
20307715b521SAl Viro {
2031e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
20327715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
20337715b521SAl Viro 	int error = get_write_access(inode);
20341da177e4SLinus Torvalds 	if (error)
20357715b521SAl Viro 		return error;
20361da177e4SLinus Torvalds 	/*
20371da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
20381da177e4SLinus Torvalds 	 */
20391da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2040be6d3e56SKentaro Takeda 	if (!error)
2041ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
20421da177e4SLinus Torvalds 	if (!error) {
20437715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2044d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2045e1181ee6SJeff Layton 				    filp);
20461da177e4SLinus Torvalds 	}
20471da177e4SLinus Torvalds 	put_write_access(inode);
2048acd0c935SMimi Zohar 	return error;
20491da177e4SLinus Torvalds }
20501da177e4SLinus Torvalds 
2051d57999e1SDave Hansen /*
2052d57999e1SDave Hansen  * Note that while the flag value (low two bits) for sys_open means:
2053d57999e1SDave Hansen  *	00 - read-only
2054d57999e1SDave Hansen  *	01 - write-only
2055d57999e1SDave Hansen  *	10 - read-write
2056d57999e1SDave Hansen  *	11 - special
2057d57999e1SDave Hansen  * it is changed into
2058d57999e1SDave Hansen  *	00 - no permissions needed
2059d57999e1SDave Hansen  *	01 - read-permission
2060d57999e1SDave Hansen  *	10 - write-permission
2061d57999e1SDave Hansen  *	11 - read-write
2062d57999e1SDave Hansen  * for the internal routines (ie open_namei()/follow_link() etc)
2063d57999e1SDave Hansen  * This is more logical, and also allows the 00 "no perm needed"
2064d57999e1SDave Hansen  * to be used for symlinks (where the permissions are checked
2065d57999e1SDave Hansen  * later).
2066d57999e1SDave Hansen  *
2067d57999e1SDave Hansen */
2068d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2069d57999e1SDave Hansen {
2070d57999e1SDave Hansen 	if ((flag+1) & O_ACCMODE)
2071d57999e1SDave Hansen 		flag++;
2072d57999e1SDave Hansen 	return flag;
2073d57999e1SDave Hansen }
2074d57999e1SDave Hansen 
207531e6b01fSNick Piggin /*
2076fe2d35ffSAl Viro  * Handle the last step of open()
207731e6b01fSNick Piggin  */
2078fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
2079c3e380b0SAl Viro 			    const struct open_flags *op, const char *pathname)
2080fb1cc555SAl Viro {
2081a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
20826c0d46c4SAl Viro 	struct dentry *dentry;
2083ca344a89SAl Viro 	int open_flag = op->open_flag;
20846c0d46c4SAl Viro 	int will_truncate = open_flag & O_TRUNC;
2085ca344a89SAl Viro 	int want_write = 0;
2086bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2087fb1cc555SAl Viro 	struct file *filp;
208816c2cd71SAl Viro 	int error;
2089fb1cc555SAl Viro 
2090c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2091c3e380b0SAl Viro 	nd->flags |= op->intent;
2092c3e380b0SAl Viro 
20931f36f774SAl Viro 	switch (nd->last_type) {
20941f36f774SAl Viro 	case LAST_DOTDOT:
2095176306f5SNeil Brown 	case LAST_DOT:
2096fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2097fe2d35ffSAl Viro 		if (error)
2098fe2d35ffSAl Viro 			return ERR_PTR(error);
20991f36f774SAl Viro 		/* fallthrough */
21001f36f774SAl Viro 	case LAST_ROOT:
21019f1fafeeSAl Viro 		error = complete_walk(nd);
210216c2cd71SAl Viro 		if (error)
21039f1fafeeSAl Viro 			return ERR_PTR(error);
2104fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2105ca344a89SAl Viro 		if (open_flag & O_CREAT) {
210616c2cd71SAl Viro 			error = -EISDIR;
21071f36f774SAl Viro 			goto exit;
2108fe2d35ffSAl Viro 		}
2109fe2d35ffSAl Viro 		goto ok;
21101f36f774SAl Viro 	case LAST_BIND:
21119f1fafeeSAl Viro 		error = complete_walk(nd);
211216c2cd71SAl Viro 		if (error)
21139f1fafeeSAl Viro 			return ERR_PTR(error);
21141f36f774SAl Viro 		audit_inode(pathname, dir);
21151f36f774SAl Viro 		goto ok;
21161f36f774SAl Viro 	}
2117a2c36b45SAl Viro 
2118ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2119bcda7652SAl Viro 		int symlink_ok = 0;
2120fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2121fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2122bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2123bcda7652SAl Viro 			symlink_ok = 1;
2124fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2125ce57dfc1SAl Viro 		error = walk_component(nd, path, &nd->last, LAST_NORM,
2126ce57dfc1SAl Viro 					!symlink_ok);
2127ce57dfc1SAl Viro 		if (error < 0)
2128fe2d35ffSAl Viro 			return ERR_PTR(error);
2129ce57dfc1SAl Viro 		if (error) /* symlink */
2130fe2d35ffSAl Viro 			return NULL;
2131fe2d35ffSAl Viro 		/* sayonara */
21329f1fafeeSAl Viro 		error = complete_walk(nd);
21339f1fafeeSAl Viro 		if (error)
2134fe2d35ffSAl Viro 			return ERR_PTR(-ECHILD);
2135fe2d35ffSAl Viro 
2136fe2d35ffSAl Viro 		error = -ENOTDIR;
2137fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_DIRECTORY) {
2138ce57dfc1SAl Viro 			if (!nd->inode->i_op->lookup)
2139fe2d35ffSAl Viro 				goto exit;
2140fe2d35ffSAl Viro 		}
2141fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2142fe2d35ffSAl Viro 		goto ok;
2143fe2d35ffSAl Viro 	}
2144fe2d35ffSAl Viro 
2145fe2d35ffSAl Viro 	/* create side of things */
21469f1fafeeSAl Viro 	error = complete_walk(nd);
21479f1fafeeSAl Viro 	if (error)
21489f1fafeeSAl Viro 		return ERR_PTR(error);
2149fe2d35ffSAl Viro 
2150fe2d35ffSAl Viro 	audit_inode(pathname, dir);
215116c2cd71SAl Viro 	error = -EISDIR;
21521f36f774SAl Viro 	/* trailing slashes? */
215331e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
21541f36f774SAl Viro 		goto exit;
21551f36f774SAl Viro 
2156a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2157a1e28038SAl Viro 
21586c0d46c4SAl Viro 	dentry = lookup_hash(nd);
21596c0d46c4SAl Viro 	error = PTR_ERR(dentry);
21606c0d46c4SAl Viro 	if (IS_ERR(dentry)) {
2161fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2162fb1cc555SAl Viro 		goto exit;
2163fb1cc555SAl Viro 	}
2164fb1cc555SAl Viro 
21656c0d46c4SAl Viro 	path->dentry = dentry;
21666c0d46c4SAl Viro 	path->mnt = nd->path.mnt;
21676c0d46c4SAl Viro 
2168fb1cc555SAl Viro 	/* Negative dentry, just create the file */
21696c0d46c4SAl Viro 	if (!dentry->d_inode) {
21706c0d46c4SAl Viro 		int mode = op->mode;
21716c0d46c4SAl Viro 		if (!IS_POSIXACL(dir->d_inode))
21726c0d46c4SAl Viro 			mode &= ~current_umask();
2173fb1cc555SAl Viro 		/*
2174fb1cc555SAl Viro 		 * This write is needed to ensure that a
21756c0d46c4SAl Viro 		 * rw->ro transition does not occur between
2176fb1cc555SAl Viro 		 * the time when the file is created and when
2177fb1cc555SAl Viro 		 * a permanent write count is taken through
2178fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2179fb1cc555SAl Viro 		 */
2180fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2181fb1cc555SAl Viro 		if (error)
2182fb1cc555SAl Viro 			goto exit_mutex_unlock;
2183ca344a89SAl Viro 		want_write = 1;
21849b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2185ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
21866c0d46c4SAl Viro 		will_truncate = 0;
2187bcda7652SAl Viro 		acc_mode = MAY_OPEN;
21886c0d46c4SAl Viro 		error = security_path_mknod(&nd->path, dentry, mode, 0);
21896c0d46c4SAl Viro 		if (error)
21906c0d46c4SAl Viro 			goto exit_mutex_unlock;
21916c0d46c4SAl Viro 		error = vfs_create(dir->d_inode, dentry, mode, nd);
21926c0d46c4SAl Viro 		if (error)
21936c0d46c4SAl Viro 			goto exit_mutex_unlock;
21946c0d46c4SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
21956c0d46c4SAl Viro 		dput(nd->path.dentry);
21966c0d46c4SAl Viro 		nd->path.dentry = dentry;
2197ca344a89SAl Viro 		goto common;
2198fb1cc555SAl Viro 	}
2199fb1cc555SAl Viro 
2200fb1cc555SAl Viro 	/*
2201fb1cc555SAl Viro 	 * It already exists.
2202fb1cc555SAl Viro 	 */
2203fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2204fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2205fb1cc555SAl Viro 
2206fb1cc555SAl Viro 	error = -EEXIST;
2207ca344a89SAl Viro 	if (open_flag & O_EXCL)
2208fb1cc555SAl Viro 		goto exit_dput;
2209fb1cc555SAl Viro 
22109875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
22119875cf80SDavid Howells 	if (error < 0)
2212fb1cc555SAl Viro 		goto exit_dput;
2213fb1cc555SAl Viro 
2214fb1cc555SAl Viro 	error = -ENOENT;
2215fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2216fb1cc555SAl Viro 		goto exit_dput;
22179e67f361SAl Viro 
22189e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2219fb1cc555SAl Viro 		return NULL;
2220fb1cc555SAl Viro 
2221fb1cc555SAl Viro 	path_to_nameidata(path, nd);
222231e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2223fb1cc555SAl Viro 	error = -EISDIR;
222431e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2225fb1cc555SAl Viro 		goto exit;
222667ee3ad2SAl Viro ok:
22276c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
22286c0d46c4SAl Viro 		will_truncate = 0;
22296c0d46c4SAl Viro 
22300f9d1a10SAl Viro 	if (will_truncate) {
22310f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
22320f9d1a10SAl Viro 		if (error)
22330f9d1a10SAl Viro 			goto exit;
2234ca344a89SAl Viro 		want_write = 1;
22350f9d1a10SAl Viro 	}
2236ca344a89SAl Viro common:
2237bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2238ca344a89SAl Viro 	if (error)
22390f9d1a10SAl Viro 		goto exit;
22400f9d1a10SAl Viro 	filp = nameidata_to_filp(nd);
22410f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
22420f9d1a10SAl Viro 		error = ima_file_check(filp, op->acc_mode);
22430f9d1a10SAl Viro 		if (error) {
22440f9d1a10SAl Viro 			fput(filp);
22450f9d1a10SAl Viro 			filp = ERR_PTR(error);
22460f9d1a10SAl Viro 		}
22470f9d1a10SAl Viro 	}
22480f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
22490f9d1a10SAl Viro 		if (will_truncate) {
22500f9d1a10SAl Viro 			error = handle_truncate(filp);
22510f9d1a10SAl Viro 			if (error) {
22520f9d1a10SAl Viro 				fput(filp);
22530f9d1a10SAl Viro 				filp = ERR_PTR(error);
22540f9d1a10SAl Viro 			}
22550f9d1a10SAl Viro 		}
22560f9d1a10SAl Viro 	}
2257ca344a89SAl Viro out:
2258ca344a89SAl Viro 	if (want_write)
22590f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
22600f9d1a10SAl Viro 	path_put(&nd->path);
2261fb1cc555SAl Viro 	return filp;
2262fb1cc555SAl Viro 
2263fb1cc555SAl Viro exit_mutex_unlock:
2264fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2265fb1cc555SAl Viro exit_dput:
2266fb1cc555SAl Viro 	path_put_conditional(path, nd);
2267fb1cc555SAl Viro exit:
2268ca344a89SAl Viro 	filp = ERR_PTR(error);
2269ca344a89SAl Viro 	goto out;
2270fb1cc555SAl Viro }
2271fb1cc555SAl Viro 
227213aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
227373d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
22741da177e4SLinus Torvalds {
2275fe2d35ffSAl Viro 	struct file *base = NULL;
22764a3fd211SDave Hansen 	struct file *filp;
22779850c056SAl Viro 	struct path path;
227813aab428SAl Viro 	int error;
227931e6b01fSNick Piggin 
228031e6b01fSNick Piggin 	filp = get_empty_filp();
228131e6b01fSNick Piggin 	if (!filp)
228231e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
228331e6b01fSNick Piggin 
228447c805dcSAl Viro 	filp->f_flags = op->open_flag;
228573d049a4SAl Viro 	nd->intent.open.file = filp;
228673d049a4SAl Viro 	nd->intent.open.flags = open_to_namei_flags(op->open_flag);
228773d049a4SAl Viro 	nd->intent.open.create_mode = op->mode;
228831e6b01fSNick Piggin 
228973d049a4SAl Viro 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
229031e6b01fSNick Piggin 	if (unlikely(error))
229113aab428SAl Viro 		goto out_filp;
229231e6b01fSNick Piggin 
2293fe2d35ffSAl Viro 	current->total_link_count = 0;
229473d049a4SAl Viro 	error = link_path_walk(pathname, nd);
229531e6b01fSNick Piggin 	if (unlikely(error))
229631e6b01fSNick Piggin 		goto out_filp;
22971da177e4SLinus Torvalds 
229873d049a4SAl Viro 	filp = do_last(nd, &path, op, pathname);
2299806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
23007b9337aaSNick Piggin 		struct path link = path;
2301def4af30SAl Viro 		void *cookie;
2302574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
230373d049a4SAl Viro 			path_put_conditional(&path, nd);
230473d049a4SAl Viro 			path_put(&nd->path);
230540b39136SAl Viro 			filp = ERR_PTR(-ELOOP);
230640b39136SAl Viro 			break;
230740b39136SAl Viro 		}
230873d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
230973d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2310574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
2311c3e380b0SAl Viro 		if (unlikely(error))
2312f1afe9efSAl Viro 			filp = ERR_PTR(error);
2313c3e380b0SAl Viro 		else
231473d049a4SAl Viro 			filp = do_last(nd, &path, op, pathname);
2315574197e0SAl Viro 		put_link(nd, &link, cookie);
2316806b681cSAl Viro 	}
231710fa8e62SAl Viro out:
231873d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
231973d049a4SAl Viro 		path_put(&nd->root);
2320fe2d35ffSAl Viro 	if (base)
2321fe2d35ffSAl Viro 		fput(base);
232273d049a4SAl Viro 	release_open_intent(nd);
232310fa8e62SAl Viro 	return filp;
23241da177e4SLinus Torvalds 
232531e6b01fSNick Piggin out_filp:
232610fa8e62SAl Viro 	filp = ERR_PTR(error);
232710fa8e62SAl Viro 	goto out;
2328de459215SKirill Korotaev }
23291da177e4SLinus Torvalds 
233013aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
233113aab428SAl Viro 		const struct open_flags *op, int flags)
233213aab428SAl Viro {
233373d049a4SAl Viro 	struct nameidata nd;
233413aab428SAl Viro 	struct file *filp;
233513aab428SAl Viro 
233673d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
233713aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
233873d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
233913aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
234073d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
234113aab428SAl Viro 	return filp;
234213aab428SAl Viro }
234313aab428SAl Viro 
234473d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
234573d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
234673d049a4SAl Viro {
234773d049a4SAl Viro 	struct nameidata nd;
234873d049a4SAl Viro 	struct file *file;
234973d049a4SAl Viro 
235073d049a4SAl Viro 	nd.root.mnt = mnt;
235173d049a4SAl Viro 	nd.root.dentry = dentry;
235273d049a4SAl Viro 
235373d049a4SAl Viro 	flags |= LOOKUP_ROOT;
235473d049a4SAl Viro 
2355bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
235673d049a4SAl Viro 		return ERR_PTR(-ELOOP);
235773d049a4SAl Viro 
235873d049a4SAl Viro 	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
235973d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
236073d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags);
236173d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
236273d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
236373d049a4SAl Viro 	return file;
236473d049a4SAl Viro }
236573d049a4SAl Viro 
23661da177e4SLinus Torvalds /**
23671da177e4SLinus Torvalds  * lookup_create - lookup a dentry, creating it if it doesn't exist
23681da177e4SLinus Torvalds  * @nd: nameidata info
23691da177e4SLinus Torvalds  * @is_dir: directory flag
23701da177e4SLinus Torvalds  *
23711da177e4SLinus Torvalds  * Simple function to lookup and return a dentry and create it
23721da177e4SLinus Torvalds  * if it doesn't exist.  Is SMP-safe.
2373c663e5d8SChristoph Hellwig  *
23744ac91378SJan Blunck  * Returns with nd->path.dentry->d_inode->i_mutex locked.
23751da177e4SLinus Torvalds  */
23761da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir)
23771da177e4SLinus Torvalds {
2378c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
23791da177e4SLinus Torvalds 
23804ac91378SJan Blunck 	mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2381c663e5d8SChristoph Hellwig 	/*
2382c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2383c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2384c663e5d8SChristoph Hellwig 	 */
23851da177e4SLinus Torvalds 	if (nd->last_type != LAST_NORM)
23861da177e4SLinus Torvalds 		goto fail;
23871da177e4SLinus Torvalds 	nd->flags &= ~LOOKUP_PARENT;
23883516586aSAl Viro 	nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2389a634904aSASANO Masahiro 	nd->intent.open.flags = O_EXCL;
2390c663e5d8SChristoph Hellwig 
2391c663e5d8SChristoph Hellwig 	/*
2392c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2393c663e5d8SChristoph Hellwig 	 */
239449705b77SChristoph Hellwig 	dentry = lookup_hash(nd);
23951da177e4SLinus Torvalds 	if (IS_ERR(dentry))
23961da177e4SLinus Torvalds 		goto fail;
2397c663e5d8SChristoph Hellwig 
2398e9baf6e5SAl Viro 	if (dentry->d_inode)
2399e9baf6e5SAl Viro 		goto eexist;
2400c663e5d8SChristoph Hellwig 	/*
2401c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2402c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2403c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2404c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2405c663e5d8SChristoph Hellwig 	 */
2406e9baf6e5SAl Viro 	if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
24071da177e4SLinus Torvalds 		dput(dentry);
24081da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2409e9baf6e5SAl Viro 	}
2410e9baf6e5SAl Viro 	return dentry;
2411e9baf6e5SAl Viro eexist:
2412e9baf6e5SAl Viro 	dput(dentry);
2413e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
24141da177e4SLinus Torvalds fail:
24151da177e4SLinus Torvalds 	return dentry;
24161da177e4SLinus Torvalds }
2417f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create);
24181da177e4SLinus Torvalds 
24191da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
24201da177e4SLinus Torvalds {
2421a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
24221da177e4SLinus Torvalds 
24231da177e4SLinus Torvalds 	if (error)
24241da177e4SLinus Torvalds 		return error;
24251da177e4SLinus Torvalds 
2426e795b717SSerge E. Hallyn 	if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2427e795b717SSerge E. Hallyn 	    !ns_capable(inode_userns(dir), CAP_MKNOD))
24281da177e4SLinus Torvalds 		return -EPERM;
24291da177e4SLinus Torvalds 
2430acfa4380SAl Viro 	if (!dir->i_op->mknod)
24311da177e4SLinus Torvalds 		return -EPERM;
24321da177e4SLinus Torvalds 
243308ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
243408ce5f16SSerge E. Hallyn 	if (error)
243508ce5f16SSerge E. Hallyn 		return error;
243608ce5f16SSerge E. Hallyn 
24371da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
24381da177e4SLinus Torvalds 	if (error)
24391da177e4SLinus Torvalds 		return error;
24401da177e4SLinus Torvalds 
24411da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2442a74574aaSStephen Smalley 	if (!error)
2443f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
24441da177e4SLinus Torvalds 	return error;
24451da177e4SLinus Torvalds }
24461da177e4SLinus Torvalds 
2447463c3197SDave Hansen static int may_mknod(mode_t mode)
2448463c3197SDave Hansen {
2449463c3197SDave Hansen 	switch (mode & S_IFMT) {
2450463c3197SDave Hansen 	case S_IFREG:
2451463c3197SDave Hansen 	case S_IFCHR:
2452463c3197SDave Hansen 	case S_IFBLK:
2453463c3197SDave Hansen 	case S_IFIFO:
2454463c3197SDave Hansen 	case S_IFSOCK:
2455463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2456463c3197SDave Hansen 		return 0;
2457463c3197SDave Hansen 	case S_IFDIR:
2458463c3197SDave Hansen 		return -EPERM;
2459463c3197SDave Hansen 	default:
2460463c3197SDave Hansen 		return -EINVAL;
2461463c3197SDave Hansen 	}
2462463c3197SDave Hansen }
2463463c3197SDave Hansen 
24642e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
24652e4d0924SHeiko Carstens 		unsigned, dev)
24661da177e4SLinus Torvalds {
24672ad94ae6SAl Viro 	int error;
24681da177e4SLinus Torvalds 	char *tmp;
24691da177e4SLinus Torvalds 	struct dentry *dentry;
24701da177e4SLinus Torvalds 	struct nameidata nd;
24711da177e4SLinus Torvalds 
24721da177e4SLinus Torvalds 	if (S_ISDIR(mode))
24731da177e4SLinus Torvalds 		return -EPERM;
24741da177e4SLinus Torvalds 
24752ad94ae6SAl Viro 	error = user_path_parent(dfd, filename, &nd, &tmp);
24761da177e4SLinus Torvalds 	if (error)
24772ad94ae6SAl Viro 		return error;
24782ad94ae6SAl Viro 
24791da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
2480463c3197SDave Hansen 	if (IS_ERR(dentry)) {
24811da177e4SLinus Torvalds 		error = PTR_ERR(dentry);
2482463c3197SDave Hansen 		goto out_unlock;
2483463c3197SDave Hansen 	}
24844ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2485ce3b0f8dSAl Viro 		mode &= ~current_umask();
2486463c3197SDave Hansen 	error = may_mknod(mode);
2487463c3197SDave Hansen 	if (error)
2488463c3197SDave Hansen 		goto out_dput;
2489463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2490463c3197SDave Hansen 	if (error)
2491463c3197SDave Hansen 		goto out_dput;
2492be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd.path, dentry, mode, dev);
2493be6d3e56SKentaro Takeda 	if (error)
2494be6d3e56SKentaro Takeda 		goto out_drop_write;
24951da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
24961da177e4SLinus Torvalds 		case 0: case S_IFREG:
24974ac91378SJan Blunck 			error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
24981da177e4SLinus Torvalds 			break;
24991da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
25004ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
25011da177e4SLinus Torvalds 					new_decode_dev(dev));
25021da177e4SLinus Torvalds 			break;
25031da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
25044ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
25051da177e4SLinus Torvalds 			break;
25061da177e4SLinus Torvalds 	}
2507be6d3e56SKentaro Takeda out_drop_write:
2508463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2509463c3197SDave Hansen out_dput:
25101da177e4SLinus Torvalds 	dput(dentry);
2511463c3197SDave Hansen out_unlock:
25124ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
25131d957f9bSJan Blunck 	path_put(&nd.path);
25141da177e4SLinus Torvalds 	putname(tmp);
25151da177e4SLinus Torvalds 
25161da177e4SLinus Torvalds 	return error;
25171da177e4SLinus Torvalds }
25181da177e4SLinus Torvalds 
25193480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
25205590ff0dSUlrich Drepper {
25215590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
25225590ff0dSUlrich Drepper }
25235590ff0dSUlrich Drepper 
25241da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
25251da177e4SLinus Torvalds {
2526a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
25271da177e4SLinus Torvalds 
25281da177e4SLinus Torvalds 	if (error)
25291da177e4SLinus Torvalds 		return error;
25301da177e4SLinus Torvalds 
2531acfa4380SAl Viro 	if (!dir->i_op->mkdir)
25321da177e4SLinus Torvalds 		return -EPERM;
25331da177e4SLinus Torvalds 
25341da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
25351da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
25361da177e4SLinus Torvalds 	if (error)
25371da177e4SLinus Torvalds 		return error;
25381da177e4SLinus Torvalds 
25391da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2540a74574aaSStephen Smalley 	if (!error)
2541f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
25421da177e4SLinus Torvalds 	return error;
25431da177e4SLinus Torvalds }
25441da177e4SLinus Torvalds 
25452e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
25461da177e4SLinus Torvalds {
25471da177e4SLinus Torvalds 	int error = 0;
25481da177e4SLinus Torvalds 	char * tmp;
25496902d925SDave Hansen 	struct dentry *dentry;
25506902d925SDave Hansen 	struct nameidata nd;
25511da177e4SLinus Torvalds 
25522ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &tmp);
25532ad94ae6SAl Viro 	if (error)
25546902d925SDave Hansen 		goto out_err;
25551da177e4SLinus Torvalds 
25561da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 1);
25571da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
25586902d925SDave Hansen 	if (IS_ERR(dentry))
25596902d925SDave Hansen 		goto out_unlock;
25606902d925SDave Hansen 
25614ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2562ce3b0f8dSAl Viro 		mode &= ~current_umask();
2563463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2564463c3197SDave Hansen 	if (error)
2565463c3197SDave Hansen 		goto out_dput;
2566be6d3e56SKentaro Takeda 	error = security_path_mkdir(&nd.path, dentry, mode);
2567be6d3e56SKentaro Takeda 	if (error)
2568be6d3e56SKentaro Takeda 		goto out_drop_write;
25694ac91378SJan Blunck 	error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2570be6d3e56SKentaro Takeda out_drop_write:
2571463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2572463c3197SDave Hansen out_dput:
25731da177e4SLinus Torvalds 	dput(dentry);
25746902d925SDave Hansen out_unlock:
25754ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
25761d957f9bSJan Blunck 	path_put(&nd.path);
25771da177e4SLinus Torvalds 	putname(tmp);
25786902d925SDave Hansen out_err:
25791da177e4SLinus Torvalds 	return error;
25801da177e4SLinus Torvalds }
25811da177e4SLinus Torvalds 
25823cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
25835590ff0dSUlrich Drepper {
25845590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
25855590ff0dSUlrich Drepper }
25865590ff0dSUlrich Drepper 
25871da177e4SLinus Torvalds /*
2588a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
2589a71905f0SSage Weil  * should have a usage count of 2 if we're the only user of this
2590a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
2591a71905f0SSage Weil  * then we drop the dentry now.
25921da177e4SLinus Torvalds  *
25931da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
25941da177e4SLinus Torvalds  * do a
25951da177e4SLinus Torvalds  *
25961da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
25971da177e4SLinus Torvalds  *		return -EBUSY;
25981da177e4SLinus Torvalds  *
25991da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
26001da177e4SLinus Torvalds  * that is still in use by something else..
26011da177e4SLinus Torvalds  */
26021da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
26031da177e4SLinus Torvalds {
26041da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
26051da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
260664252c75SSage Weil 	if (dentry->d_count == 1)
26071da177e4SLinus Torvalds 		__d_drop(dentry);
26081da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
26091da177e4SLinus Torvalds }
26101da177e4SLinus Torvalds 
26111da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
26121da177e4SLinus Torvalds {
26131da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
26141da177e4SLinus Torvalds 
26151da177e4SLinus Torvalds 	if (error)
26161da177e4SLinus Torvalds 		return error;
26171da177e4SLinus Torvalds 
2618acfa4380SAl Viro 	if (!dir->i_op->rmdir)
26191da177e4SLinus Torvalds 		return -EPERM;
26201da177e4SLinus Torvalds 
26211b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
2622912dbc15SSage Weil 
26231da177e4SLinus Torvalds 	error = -EBUSY;
2624912dbc15SSage Weil 	if (d_mountpoint(dentry))
2625912dbc15SSage Weil 		goto out;
2626912dbc15SSage Weil 
26271da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
2628912dbc15SSage Weil 	if (error)
2629912dbc15SSage Weil 		goto out;
2630912dbc15SSage Weil 
26313cebde24SSage Weil 	shrink_dcache_parent(dentry);
26321da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
2633912dbc15SSage Weil 	if (error)
2634912dbc15SSage Weil 		goto out;
2635912dbc15SSage Weil 
26361da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
2637d83c49f3SAl Viro 	dont_mount(dentry);
26381da177e4SLinus Torvalds 
2639912dbc15SSage Weil out:
2640912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
2641912dbc15SSage Weil 	if (!error)
2642912dbc15SSage Weil 		d_delete(dentry);
26431da177e4SLinus Torvalds 	return error;
26441da177e4SLinus Torvalds }
26451da177e4SLinus Torvalds 
26465590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
26471da177e4SLinus Torvalds {
26481da177e4SLinus Torvalds 	int error = 0;
26491da177e4SLinus Torvalds 	char * name;
26501da177e4SLinus Torvalds 	struct dentry *dentry;
26511da177e4SLinus Torvalds 	struct nameidata nd;
26521da177e4SLinus Torvalds 
26532ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
26541da177e4SLinus Torvalds 	if (error)
26552ad94ae6SAl Viro 		return error;
26561da177e4SLinus Torvalds 
26571da177e4SLinus Torvalds 	switch(nd.last_type) {
26581da177e4SLinus Torvalds 	case LAST_DOTDOT:
26591da177e4SLinus Torvalds 		error = -ENOTEMPTY;
26601da177e4SLinus Torvalds 		goto exit1;
26611da177e4SLinus Torvalds 	case LAST_DOT:
26621da177e4SLinus Torvalds 		error = -EINVAL;
26631da177e4SLinus Torvalds 		goto exit1;
26641da177e4SLinus Torvalds 	case LAST_ROOT:
26651da177e4SLinus Torvalds 		error = -EBUSY;
26661da177e4SLinus Torvalds 		goto exit1;
26671da177e4SLinus Torvalds 	}
26680612d9fbSOGAWA Hirofumi 
26690612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
26700612d9fbSOGAWA Hirofumi 
26714ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
267249705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
26731da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
26746902d925SDave Hansen 	if (IS_ERR(dentry))
26756902d925SDave Hansen 		goto exit2;
2676e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
2677e6bc45d6STheodore Ts'o 		error = -ENOENT;
2678e6bc45d6STheodore Ts'o 		goto exit3;
2679e6bc45d6STheodore Ts'o 	}
26800622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
26810622753bSDave Hansen 	if (error)
26820622753bSDave Hansen 		goto exit3;
2683be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2684be6d3e56SKentaro Takeda 	if (error)
2685be6d3e56SKentaro Takeda 		goto exit4;
26864ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2687be6d3e56SKentaro Takeda exit4:
26880622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
26890622753bSDave Hansen exit3:
26901da177e4SLinus Torvalds 	dput(dentry);
26916902d925SDave Hansen exit2:
26924ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
26931da177e4SLinus Torvalds exit1:
26941d957f9bSJan Blunck 	path_put(&nd.path);
26951da177e4SLinus Torvalds 	putname(name);
26961da177e4SLinus Torvalds 	return error;
26971da177e4SLinus Torvalds }
26981da177e4SLinus Torvalds 
26993cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
27005590ff0dSUlrich Drepper {
27015590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
27025590ff0dSUlrich Drepper }
27035590ff0dSUlrich Drepper 
27041da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
27051da177e4SLinus Torvalds {
27061da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
27071da177e4SLinus Torvalds 
27081da177e4SLinus Torvalds 	if (error)
27091da177e4SLinus Torvalds 		return error;
27101da177e4SLinus Torvalds 
2711acfa4380SAl Viro 	if (!dir->i_op->unlink)
27121da177e4SLinus Torvalds 		return -EPERM;
27131da177e4SLinus Torvalds 
27141b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
27151da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
27161da177e4SLinus Torvalds 		error = -EBUSY;
27171da177e4SLinus Torvalds 	else {
27181da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2719bec1052eSAl Viro 		if (!error) {
27201da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2721bec1052eSAl Viro 			if (!error)
2722d83c49f3SAl Viro 				dont_mount(dentry);
2723bec1052eSAl Viro 		}
27241da177e4SLinus Torvalds 	}
27251b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
27261da177e4SLinus Torvalds 
27271da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
27281da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2729ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
27301da177e4SLinus Torvalds 		d_delete(dentry);
27311da177e4SLinus Torvalds 	}
27320eeca283SRobert Love 
27331da177e4SLinus Torvalds 	return error;
27341da177e4SLinus Torvalds }
27351da177e4SLinus Torvalds 
27361da177e4SLinus Torvalds /*
27371da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
27381b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
27391da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
27401da177e4SLinus Torvalds  * while waiting on the I/O.
27411da177e4SLinus Torvalds  */
27425590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
27431da177e4SLinus Torvalds {
27442ad94ae6SAl Viro 	int error;
27451da177e4SLinus Torvalds 	char *name;
27461da177e4SLinus Torvalds 	struct dentry *dentry;
27471da177e4SLinus Torvalds 	struct nameidata nd;
27481da177e4SLinus Torvalds 	struct inode *inode = NULL;
27491da177e4SLinus Torvalds 
27502ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
27511da177e4SLinus Torvalds 	if (error)
27522ad94ae6SAl Viro 		return error;
27532ad94ae6SAl Viro 
27541da177e4SLinus Torvalds 	error = -EISDIR;
27551da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
27561da177e4SLinus Torvalds 		goto exit1;
27570612d9fbSOGAWA Hirofumi 
27580612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
27590612d9fbSOGAWA Hirofumi 
27604ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
276149705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
27621da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27631da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
27641da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
276550338b88STörök Edwin 		if (nd.last.name[nd.last.len])
276650338b88STörök Edwin 			goto slashes;
27671da177e4SLinus Torvalds 		inode = dentry->d_inode;
276850338b88STörök Edwin 		if (!inode)
2769e6bc45d6STheodore Ts'o 			goto slashes;
27707de9c6eeSAl Viro 		ihold(inode);
27710622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
27720622753bSDave Hansen 		if (error)
27730622753bSDave Hansen 			goto exit2;
2774be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2775be6d3e56SKentaro Takeda 		if (error)
2776be6d3e56SKentaro Takeda 			goto exit3;
27774ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2778be6d3e56SKentaro Takeda exit3:
27790622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
27801da177e4SLinus Torvalds 	exit2:
27811da177e4SLinus Torvalds 		dput(dentry);
27821da177e4SLinus Torvalds 	}
27834ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27841da177e4SLinus Torvalds 	if (inode)
27851da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
27861da177e4SLinus Torvalds exit1:
27871d957f9bSJan Blunck 	path_put(&nd.path);
27881da177e4SLinus Torvalds 	putname(name);
27891da177e4SLinus Torvalds 	return error;
27901da177e4SLinus Torvalds 
27911da177e4SLinus Torvalds slashes:
27921da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
27931da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
27941da177e4SLinus Torvalds 	goto exit2;
27951da177e4SLinus Torvalds }
27961da177e4SLinus Torvalds 
27972e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
27985590ff0dSUlrich Drepper {
27995590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
28005590ff0dSUlrich Drepper 		return -EINVAL;
28015590ff0dSUlrich Drepper 
28025590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
28035590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
28045590ff0dSUlrich Drepper 
28055590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
28065590ff0dSUlrich Drepper }
28075590ff0dSUlrich Drepper 
28083480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
28095590ff0dSUlrich Drepper {
28105590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
28115590ff0dSUlrich Drepper }
28125590ff0dSUlrich Drepper 
2813db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
28141da177e4SLinus Torvalds {
2815a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
28161da177e4SLinus Torvalds 
28171da177e4SLinus Torvalds 	if (error)
28181da177e4SLinus Torvalds 		return error;
28191da177e4SLinus Torvalds 
2820acfa4380SAl Viro 	if (!dir->i_op->symlink)
28211da177e4SLinus Torvalds 		return -EPERM;
28221da177e4SLinus Torvalds 
28231da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
28241da177e4SLinus Torvalds 	if (error)
28251da177e4SLinus Torvalds 		return error;
28261da177e4SLinus Torvalds 
28271da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2828a74574aaSStephen Smalley 	if (!error)
2829f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
28301da177e4SLinus Torvalds 	return error;
28311da177e4SLinus Torvalds }
28321da177e4SLinus Torvalds 
28332e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
28342e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
28351da177e4SLinus Torvalds {
28362ad94ae6SAl Viro 	int error;
28371da177e4SLinus Torvalds 	char *from;
28381da177e4SLinus Torvalds 	char *to;
28396902d925SDave Hansen 	struct dentry *dentry;
28406902d925SDave Hansen 	struct nameidata nd;
28411da177e4SLinus Torvalds 
28421da177e4SLinus Torvalds 	from = getname(oldname);
28431da177e4SLinus Torvalds 	if (IS_ERR(from))
28441da177e4SLinus Torvalds 		return PTR_ERR(from);
28452ad94ae6SAl Viro 
28462ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
28472ad94ae6SAl Viro 	if (error)
28486902d925SDave Hansen 		goto out_putname;
28491da177e4SLinus Torvalds 
28501da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
28511da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
28526902d925SDave Hansen 	if (IS_ERR(dentry))
28536902d925SDave Hansen 		goto out_unlock;
28546902d925SDave Hansen 
285575c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
285675c3f29dSDave Hansen 	if (error)
285775c3f29dSDave Hansen 		goto out_dput;
2858be6d3e56SKentaro Takeda 	error = security_path_symlink(&nd.path, dentry, from);
2859be6d3e56SKentaro Takeda 	if (error)
2860be6d3e56SKentaro Takeda 		goto out_drop_write;
2861db2e747bSMiklos Szeredi 	error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2862be6d3e56SKentaro Takeda out_drop_write:
286375c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
286475c3f29dSDave Hansen out_dput:
28651da177e4SLinus Torvalds 	dput(dentry);
28666902d925SDave Hansen out_unlock:
28674ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
28681d957f9bSJan Blunck 	path_put(&nd.path);
28691da177e4SLinus Torvalds 	putname(to);
28706902d925SDave Hansen out_putname:
28711da177e4SLinus Torvalds 	putname(from);
28721da177e4SLinus Torvalds 	return error;
28731da177e4SLinus Torvalds }
28741da177e4SLinus Torvalds 
28753480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
28765590ff0dSUlrich Drepper {
28775590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
28785590ff0dSUlrich Drepper }
28795590ff0dSUlrich Drepper 
28801da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
28811da177e4SLinus Torvalds {
28821da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
28831da177e4SLinus Torvalds 	int error;
28841da177e4SLinus Torvalds 
28851da177e4SLinus Torvalds 	if (!inode)
28861da177e4SLinus Torvalds 		return -ENOENT;
28871da177e4SLinus Torvalds 
2888a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
28891da177e4SLinus Torvalds 	if (error)
28901da177e4SLinus Torvalds 		return error;
28911da177e4SLinus Torvalds 
28921da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
28931da177e4SLinus Torvalds 		return -EXDEV;
28941da177e4SLinus Torvalds 
28951da177e4SLinus Torvalds 	/*
28961da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
28971da177e4SLinus Torvalds 	 */
28981da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
28991da177e4SLinus Torvalds 		return -EPERM;
2900acfa4380SAl Viro 	if (!dir->i_op->link)
29011da177e4SLinus Torvalds 		return -EPERM;
29027e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
29031da177e4SLinus Torvalds 		return -EPERM;
29041da177e4SLinus Torvalds 
29051da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
29061da177e4SLinus Torvalds 	if (error)
29071da177e4SLinus Torvalds 		return error;
29081da177e4SLinus Torvalds 
29097e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
2910aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
2911aae8a97dSAneesh Kumar K.V 	if (inode->i_nlink == 0)
2912aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
2913aae8a97dSAneesh Kumar K.V 	else
29141da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
29157e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2916e31e14ecSStephen Smalley 	if (!error)
29177e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
29181da177e4SLinus Torvalds 	return error;
29191da177e4SLinus Torvalds }
29201da177e4SLinus Torvalds 
29211da177e4SLinus Torvalds /*
29221da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
29231da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
29241da177e4SLinus Torvalds  * newname.  --KAB
29251da177e4SLinus Torvalds  *
29261da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
29271da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
29281da177e4SLinus Torvalds  * and other special files.  --ADM
29291da177e4SLinus Torvalds  */
29302e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
29312e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
29321da177e4SLinus Torvalds {
29331da177e4SLinus Torvalds 	struct dentry *new_dentry;
29342d8f3038SAl Viro 	struct nameidata nd;
29352d8f3038SAl Viro 	struct path old_path;
293611a7b371SAneesh Kumar K.V 	int how = 0;
29371da177e4SLinus Torvalds 	int error;
29381da177e4SLinus Torvalds 	char *to;
29391da177e4SLinus Torvalds 
294011a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
2941c04030e1SUlrich Drepper 		return -EINVAL;
294211a7b371SAneesh Kumar K.V 	/*
294311a7b371SAneesh Kumar K.V 	 * To use null names we require CAP_DAC_READ_SEARCH
294411a7b371SAneesh Kumar K.V 	 * This ensures that not everyone will be able to create
294511a7b371SAneesh Kumar K.V 	 * handlink using the passed filedescriptor.
294611a7b371SAneesh Kumar K.V 	 */
294711a7b371SAneesh Kumar K.V 	if (flags & AT_EMPTY_PATH) {
294811a7b371SAneesh Kumar K.V 		if (!capable(CAP_DAC_READ_SEARCH))
294911a7b371SAneesh Kumar K.V 			return -ENOENT;
295011a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
295111a7b371SAneesh Kumar K.V 	}
2952c04030e1SUlrich Drepper 
295311a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
295411a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
295511a7b371SAneesh Kumar K.V 
295611a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
29571da177e4SLinus Torvalds 	if (error)
29582ad94ae6SAl Viro 		return error;
29592ad94ae6SAl Viro 
29602ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
29611da177e4SLinus Torvalds 	if (error)
29621da177e4SLinus Torvalds 		goto out;
29631da177e4SLinus Torvalds 	error = -EXDEV;
29642d8f3038SAl Viro 	if (old_path.mnt != nd.path.mnt)
29651da177e4SLinus Torvalds 		goto out_release;
29661da177e4SLinus Torvalds 	new_dentry = lookup_create(&nd, 0);
29671da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
29686902d925SDave Hansen 	if (IS_ERR(new_dentry))
29696902d925SDave Hansen 		goto out_unlock;
297075c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
297175c3f29dSDave Hansen 	if (error)
297275c3f29dSDave Hansen 		goto out_dput;
2973be6d3e56SKentaro Takeda 	error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2974be6d3e56SKentaro Takeda 	if (error)
2975be6d3e56SKentaro Takeda 		goto out_drop_write;
29762d8f3038SAl Viro 	error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
2977be6d3e56SKentaro Takeda out_drop_write:
297875c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
297975c3f29dSDave Hansen out_dput:
29801da177e4SLinus Torvalds 	dput(new_dentry);
29816902d925SDave Hansen out_unlock:
29824ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
29831da177e4SLinus Torvalds out_release:
29841d957f9bSJan Blunck 	path_put(&nd.path);
29852ad94ae6SAl Viro 	putname(to);
29861da177e4SLinus Torvalds out:
29872d8f3038SAl Viro 	path_put(&old_path);
29881da177e4SLinus Torvalds 
29891da177e4SLinus Torvalds 	return error;
29901da177e4SLinus Torvalds }
29911da177e4SLinus Torvalds 
29923480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
29935590ff0dSUlrich Drepper {
2994c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
29955590ff0dSUlrich Drepper }
29965590ff0dSUlrich Drepper 
29971da177e4SLinus Torvalds /*
29981da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
29991da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
30001da177e4SLinus Torvalds  * Problems:
30011da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
30021da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
30031da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
3004a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
30051da177e4SLinus Torvalds  *	   story.
30061da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
30071b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
30081da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
30091da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
3010a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
30111da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
30121da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
30131da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
3014a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
30151da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
30161da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
30171da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
3018e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
30191b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
30201da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3021c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
30221da177e4SLinus Torvalds  *	   locking].
30231da177e4SLinus Torvalds  */
302475c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
30251da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
30261da177e4SLinus Torvalds {
30271da177e4SLinus Torvalds 	int error = 0;
30289055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
30291da177e4SLinus Torvalds 
30301da177e4SLinus Torvalds 	/*
30311da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
30321da177e4SLinus Torvalds 	 * we'll need to flip '..'.
30331da177e4SLinus Torvalds 	 */
30341da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3035f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
30361da177e4SLinus Torvalds 		if (error)
30371da177e4SLinus Torvalds 			return error;
30381da177e4SLinus Torvalds 	}
30391da177e4SLinus Torvalds 
30401da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30411da177e4SLinus Torvalds 	if (error)
30421da177e4SLinus Torvalds 		return error;
30431da177e4SLinus Torvalds 
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);
3064e31e14ecSStephen Smalley 	if (!error)
3065349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30661da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
30671da177e4SLinus Torvalds 	return error;
30681da177e4SLinus Torvalds }
30691da177e4SLinus Torvalds 
307075c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
30711da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
30721da177e4SLinus Torvalds {
307351892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
30741da177e4SLinus Torvalds 	int error;
30751da177e4SLinus Torvalds 
30761da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30771da177e4SLinus Torvalds 	if (error)
30781da177e4SLinus Torvalds 		return error;
30791da177e4SLinus Torvalds 
30801da177e4SLinus Torvalds 	dget(new_dentry);
30811da177e4SLinus Torvalds 	if (target)
30821b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
308351892bbbSSage Weil 
30841da177e4SLinus Torvalds 	error = -EBUSY;
308551892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
308651892bbbSSage Weil 		goto out;
308751892bbbSSage Weil 
30881da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
308951892bbbSSage Weil 	if (error)
309051892bbbSSage Weil 		goto out;
309151892bbbSSage Weil 
3092bec1052eSAl Viro 	if (target)
3093d83c49f3SAl Viro 		dont_mount(new_dentry);
3094349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30951da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
309651892bbbSSage Weil out:
30971da177e4SLinus Torvalds 	if (target)
30981b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
30991da177e4SLinus Torvalds 	dput(new_dentry);
31001da177e4SLinus Torvalds 	return error;
31011da177e4SLinus Torvalds }
31021da177e4SLinus Torvalds 
31031da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
31041da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
31051da177e4SLinus Torvalds {
31061da177e4SLinus Torvalds 	int error;
31071da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
310859b0df21SEric Paris 	const unsigned char *old_name;
31091da177e4SLinus Torvalds 
31101da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
31111da177e4SLinus Torvalds  		return 0;
31121da177e4SLinus Torvalds 
31131da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
31141da177e4SLinus Torvalds 	if (error)
31151da177e4SLinus Torvalds 		return error;
31161da177e4SLinus Torvalds 
31171da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3118a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
31191da177e4SLinus Torvalds 	else
31201da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
31211da177e4SLinus Torvalds 	if (error)
31221da177e4SLinus Torvalds 		return error;
31231da177e4SLinus Torvalds 
3124acfa4380SAl Viro 	if (!old_dir->i_op->rename)
31251da177e4SLinus Torvalds 		return -EPERM;
31261da177e4SLinus Torvalds 
31270eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
31280eeca283SRobert Love 
31291da177e4SLinus Torvalds 	if (is_dir)
31301da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
31311da177e4SLinus Torvalds 	else
31321da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3133123df294SAl Viro 	if (!error)
3134123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
31355a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
31360eeca283SRobert Love 	fsnotify_oldname_free(old_name);
31370eeca283SRobert Love 
31381da177e4SLinus Torvalds 	return error;
31391da177e4SLinus Torvalds }
31401da177e4SLinus Torvalds 
31412e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
31422e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
31431da177e4SLinus Torvalds {
31441da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
31451da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
31461da177e4SLinus Torvalds 	struct dentry *trap;
31471da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
31482ad94ae6SAl Viro 	char *from;
31492ad94ae6SAl Viro 	char *to;
31502ad94ae6SAl Viro 	int error;
31511da177e4SLinus Torvalds 
31522ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
31531da177e4SLinus Torvalds 	if (error)
31541da177e4SLinus Torvalds 		goto exit;
31551da177e4SLinus Torvalds 
31562ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
31571da177e4SLinus Torvalds 	if (error)
31581da177e4SLinus Torvalds 		goto exit1;
31591da177e4SLinus Torvalds 
31601da177e4SLinus Torvalds 	error = -EXDEV;
31614ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
31621da177e4SLinus Torvalds 		goto exit2;
31631da177e4SLinus Torvalds 
31644ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
31651da177e4SLinus Torvalds 	error = -EBUSY;
31661da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
31671da177e4SLinus Torvalds 		goto exit2;
31681da177e4SLinus Torvalds 
31694ac91378SJan Blunck 	new_dir = newnd.path.dentry;
31701da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
31711da177e4SLinus Torvalds 		goto exit2;
31721da177e4SLinus Torvalds 
31730612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
31740612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
31754e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
31760612d9fbSOGAWA Hirofumi 
31771da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
31781da177e4SLinus Torvalds 
317949705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
31801da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
31811da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
31821da177e4SLinus Torvalds 		goto exit3;
31831da177e4SLinus Torvalds 	/* source must exist */
31841da177e4SLinus Torvalds 	error = -ENOENT;
31851da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
31861da177e4SLinus Torvalds 		goto exit4;
31871da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
31881da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
31891da177e4SLinus Torvalds 		error = -ENOTDIR;
31901da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
31911da177e4SLinus Torvalds 			goto exit4;
31921da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
31931da177e4SLinus Torvalds 			goto exit4;
31941da177e4SLinus Torvalds 	}
31951da177e4SLinus Torvalds 	/* source should not be ancestor of target */
31961da177e4SLinus Torvalds 	error = -EINVAL;
31971da177e4SLinus Torvalds 	if (old_dentry == trap)
31981da177e4SLinus Torvalds 		goto exit4;
319949705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
32001da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
32011da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
32021da177e4SLinus Torvalds 		goto exit4;
32031da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
32041da177e4SLinus Torvalds 	error = -ENOTEMPTY;
32051da177e4SLinus Torvalds 	if (new_dentry == trap)
32061da177e4SLinus Torvalds 		goto exit5;
32071da177e4SLinus Torvalds 
32089079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
32099079b1ebSDave Hansen 	if (error)
32109079b1ebSDave Hansen 		goto exit5;
3211be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3212be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3213be6d3e56SKentaro Takeda 	if (error)
3214be6d3e56SKentaro Takeda 		goto exit6;
32151da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
32161da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3217be6d3e56SKentaro Takeda exit6:
32189079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
32191da177e4SLinus Torvalds exit5:
32201da177e4SLinus Torvalds 	dput(new_dentry);
32211da177e4SLinus Torvalds exit4:
32221da177e4SLinus Torvalds 	dput(old_dentry);
32231da177e4SLinus Torvalds exit3:
32241da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
32251da177e4SLinus Torvalds exit2:
32261d957f9bSJan Blunck 	path_put(&newnd.path);
32272ad94ae6SAl Viro 	putname(to);
32281da177e4SLinus Torvalds exit1:
32291d957f9bSJan Blunck 	path_put(&oldnd.path);
32301da177e4SLinus Torvalds 	putname(from);
32312ad94ae6SAl Viro exit:
32321da177e4SLinus Torvalds 	return error;
32331da177e4SLinus Torvalds }
32341da177e4SLinus Torvalds 
3235a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
32365590ff0dSUlrich Drepper {
32375590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
32385590ff0dSUlrich Drepper }
32395590ff0dSUlrich Drepper 
32401da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
32411da177e4SLinus Torvalds {
32421da177e4SLinus Torvalds 	int len;
32431da177e4SLinus Torvalds 
32441da177e4SLinus Torvalds 	len = PTR_ERR(link);
32451da177e4SLinus Torvalds 	if (IS_ERR(link))
32461da177e4SLinus Torvalds 		goto out;
32471da177e4SLinus Torvalds 
32481da177e4SLinus Torvalds 	len = strlen(link);
32491da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
32501da177e4SLinus Torvalds 		len = buflen;
32511da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
32521da177e4SLinus Torvalds 		len = -EFAULT;
32531da177e4SLinus Torvalds out:
32541da177e4SLinus Torvalds 	return len;
32551da177e4SLinus Torvalds }
32561da177e4SLinus Torvalds 
32571da177e4SLinus Torvalds /*
32581da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
32591da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
32601da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
32611da177e4SLinus Torvalds  */
32621da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32631da177e4SLinus Torvalds {
32641da177e4SLinus Torvalds 	struct nameidata nd;
3265cc314eefSLinus Torvalds 	void *cookie;
3266694a1764SMarcin Slusarz 	int res;
3267cc314eefSLinus Torvalds 
32681da177e4SLinus Torvalds 	nd.depth = 0;
3269cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3270694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3271694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3272694a1764SMarcin Slusarz 
3273694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
32741da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3275cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3276694a1764SMarcin Slusarz 	return res;
32771da177e4SLinus Torvalds }
32781da177e4SLinus Torvalds 
32791da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
32801da177e4SLinus Torvalds {
32811da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
32821da177e4SLinus Torvalds }
32831da177e4SLinus Torvalds 
32841da177e4SLinus Torvalds /* get the link contents into pagecache */
32851da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
32861da177e4SLinus Torvalds {
3287ebd09abbSDuane Griffin 	char *kaddr;
32881da177e4SLinus Torvalds 	struct page *page;
32891da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3290090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
32911da177e4SLinus Torvalds 	if (IS_ERR(page))
32926fe6900eSNick Piggin 		return (char*)page;
32931da177e4SLinus Torvalds 	*ppage = page;
3294ebd09abbSDuane Griffin 	kaddr = kmap(page);
3295ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3296ebd09abbSDuane Griffin 	return kaddr;
32971da177e4SLinus Torvalds }
32981da177e4SLinus Torvalds 
32991da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
33001da177e4SLinus Torvalds {
33011da177e4SLinus Torvalds 	struct page *page = NULL;
33021da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
33031da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
33041da177e4SLinus Torvalds 	if (page) {
33051da177e4SLinus Torvalds 		kunmap(page);
33061da177e4SLinus Torvalds 		page_cache_release(page);
33071da177e4SLinus Torvalds 	}
33081da177e4SLinus Torvalds 	return res;
33091da177e4SLinus Torvalds }
33101da177e4SLinus Torvalds 
3311cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
33121da177e4SLinus Torvalds {
3313cc314eefSLinus Torvalds 	struct page *page = NULL;
33141da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3315cc314eefSLinus Torvalds 	return page;
33161da177e4SLinus Torvalds }
33171da177e4SLinus Torvalds 
3318cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
33191da177e4SLinus Torvalds {
3320cc314eefSLinus Torvalds 	struct page *page = cookie;
3321cc314eefSLinus Torvalds 
3322cc314eefSLinus Torvalds 	if (page) {
33231da177e4SLinus Torvalds 		kunmap(page);
33241da177e4SLinus Torvalds 		page_cache_release(page);
33251da177e4SLinus Torvalds 	}
33261da177e4SLinus Torvalds }
33271da177e4SLinus Torvalds 
332854566b2cSNick Piggin /*
332954566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
333054566b2cSNick Piggin  */
333154566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
33321da177e4SLinus Torvalds {
33331da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
33340adb25d2SKirill Korotaev 	struct page *page;
3335afddba49SNick Piggin 	void *fsdata;
3336beb497abSDmitriy Monakhov 	int err;
33371da177e4SLinus Torvalds 	char *kaddr;
333854566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
333954566b2cSNick Piggin 	if (nofs)
334054566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
33411da177e4SLinus Torvalds 
33427e53cac4SNeilBrown retry:
3343afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
334454566b2cSNick Piggin 				flags, &page, &fsdata);
33451da177e4SLinus Torvalds 	if (err)
3346afddba49SNick Piggin 		goto fail;
3347afddba49SNick Piggin 
33481da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
33491da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
33501da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
3351afddba49SNick Piggin 
3352afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3353afddba49SNick Piggin 							page, fsdata);
33541da177e4SLinus Torvalds 	if (err < 0)
33551da177e4SLinus Torvalds 		goto fail;
3356afddba49SNick Piggin 	if (err < len-1)
3357afddba49SNick Piggin 		goto retry;
3358afddba49SNick Piggin 
33591da177e4SLinus Torvalds 	mark_inode_dirty(inode);
33601da177e4SLinus Torvalds 	return 0;
33611da177e4SLinus Torvalds fail:
33621da177e4SLinus Torvalds 	return err;
33631da177e4SLinus Torvalds }
33641da177e4SLinus Torvalds 
33650adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
33660adb25d2SKirill Korotaev {
33670adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
336854566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
33690adb25d2SKirill Korotaev }
33700adb25d2SKirill Korotaev 
337192e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
33721da177e4SLinus Torvalds 	.readlink	= generic_readlink,
33731da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
33741da177e4SLinus Torvalds 	.put_link	= page_put_link,
33751da177e4SLinus Torvalds };
33761da177e4SLinus Torvalds 
33772d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3378cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
33791da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
33801da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
33811da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
33821da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
33831da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
33841da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
33851da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
33861da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
33871da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
33880adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
33891da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
33901da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3391c9c6cac0SAl Viro EXPORT_SYMBOL(kern_path_parent);
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