xref: /openbmc/linux/fs/namei.c (revision dae6ad8f)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/namei.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  * Some corrections by tytso.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
121da177e4SLinus Torvalds  * lookup logic.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/init.h>
181da177e4SLinus Torvalds #include <linux/module.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/fs.h>
211da177e4SLinus Torvalds #include <linux/namei.h>
221da177e4SLinus Torvalds #include <linux/pagemap.h>
230eeca283SRobert Love #include <linux/fsnotify.h>
241da177e4SLinus Torvalds #include <linux/personality.h>
251da177e4SLinus Torvalds #include <linux/security.h>
266146f0d5SMimi Zohar #include <linux/ima.h>
271da177e4SLinus Torvalds #include <linux/syscalls.h>
281da177e4SLinus Torvalds #include <linux/mount.h>
291da177e4SLinus Torvalds #include <linux/audit.h>
3016f7e0feSRandy Dunlap #include <linux/capability.h>
31834f2a4aSTrond Myklebust #include <linux/file.h>
325590ff0dSUlrich Drepper #include <linux/fcntl.h>
3308ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h>
345ad4e53bSAl Viro #include <linux/fs_struct.h>
351da177e4SLinus Torvalds #include <asm/uaccess.h>
361da177e4SLinus Torvalds 
37e81e3f4dSEric Paris #include "internal.h"
38e81e3f4dSEric Paris 
391da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer]
401da177e4SLinus Torvalds  * Fundamental changes in the pathname lookup mechanisms (namei)
411da177e4SLinus Torvalds  * were necessary because of omirr.  The reason is that omirr needs
421da177e4SLinus Torvalds  * to know the _real_ pathname, not the user-supplied one, in case
431da177e4SLinus Torvalds  * of symlinks (and also when transname replacements occur).
441da177e4SLinus Torvalds  *
451da177e4SLinus Torvalds  * The new code replaces the old recursive symlink resolution with
461da177e4SLinus Torvalds  * an iterative one (in case of non-nested symlink chains).  It does
471da177e4SLinus Torvalds  * this with calls to <fs>_follow_link().
481da177e4SLinus Torvalds  * As a side effect, dir_namei(), _namei() and follow_link() are now
491da177e4SLinus Torvalds  * replaced with a single function lookup_dentry() that can handle all
501da177e4SLinus Torvalds  * the special cases of the former code.
511da177e4SLinus Torvalds  *
521da177e4SLinus Torvalds  * With the new dcache, the pathname is stored at each inode, at least as
531da177e4SLinus Torvalds  * long as the refcount of the inode is positive.  As a side effect, the
541da177e4SLinus Torvalds  * size of the dcache depends on the inode cache and thus is dynamic.
551da177e4SLinus Torvalds  *
561da177e4SLinus Torvalds  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
571da177e4SLinus Torvalds  * resolution to correspond with current state of the code.
581da177e4SLinus Torvalds  *
591da177e4SLinus Torvalds  * Note that the symlink resolution is not *completely* iterative.
601da177e4SLinus Torvalds  * There is still a significant amount of tail- and mid- recursion in
611da177e4SLinus Torvalds  * the algorithm.  Also, note that <fs>_readlink() is not used in
621da177e4SLinus Torvalds  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
631da177e4SLinus Torvalds  * may return different results than <fs>_follow_link().  Many virtual
641da177e4SLinus Torvalds  * filesystems (including /proc) exhibit this behavior.
651da177e4SLinus Torvalds  */
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
681da177e4SLinus Torvalds  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
691da177e4SLinus Torvalds  * and the name already exists in form of a symlink, try to create the new
701da177e4SLinus Torvalds  * name indicated by the symlink. The old code always complained that the
711da177e4SLinus Torvalds  * name already exists, due to not following the symlink even if its target
721da177e4SLinus Torvalds  * is nonexistent.  The new semantics affects also mknod() and link() when
7325985edcSLucas De Marchi  * the name is a symlink pointing to a non-existent name.
741da177e4SLinus Torvalds  *
751da177e4SLinus Torvalds  * I don't know which semantics is the right one, since I have no access
761da177e4SLinus Torvalds  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
771da177e4SLinus Torvalds  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
781da177e4SLinus Torvalds  * "old" one. Personally, I think the new semantics is much more logical.
791da177e4SLinus Torvalds  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
801da177e4SLinus Torvalds  * file does succeed in both HP-UX and SunOs, but not in Solaris
811da177e4SLinus Torvalds  * and in the old Linux semantics.
821da177e4SLinus Torvalds  */
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
851da177e4SLinus Torvalds  * semantics.  See the comments in "open_namei" and "do_link" below.
861da177e4SLinus Torvalds  *
871da177e4SLinus Torvalds  * [10-Sep-98 Alan Modra] Another symlink change.
881da177e4SLinus Torvalds  */
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
911da177e4SLinus Torvalds  *	inside the path - always follow.
921da177e4SLinus Torvalds  *	in the last component in creation/removal/renaming - never follow.
931da177e4SLinus Torvalds  *	if LOOKUP_FOLLOW passed - follow.
941da177e4SLinus Torvalds  *	if the pathname has trailing slashes - follow.
951da177e4SLinus Torvalds  *	otherwise - don't follow.
961da177e4SLinus Torvalds  * (applied in that order).
971da177e4SLinus Torvalds  *
981da177e4SLinus Torvalds  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
991da177e4SLinus Torvalds  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
1001da177e4SLinus Torvalds  * During the 2.4 we need to fix the userland stuff depending on it -
1011da177e4SLinus Torvalds  * hopefully we will be able to get rid of that wart in 2.5. So far only
1021da177e4SLinus Torvalds  * XEmacs seems to be relying on it...
1031da177e4SLinus Torvalds  */
1041da177e4SLinus Torvalds /*
1051da177e4SLinus Torvalds  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
106a11f3a05SArjan van de Ven  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1071da177e4SLinus Torvalds  * any extra contention...
1081da177e4SLinus Torvalds  */
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional
1111da177e4SLinus Torvalds  * checking and hopefully speeding things up, we copy filenames to the
1121da177e4SLinus Torvalds  * kernel data space before using them..
1131da177e4SLinus Torvalds  *
1141da177e4SLinus Torvalds  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
1151da177e4SLinus Torvalds  * PATH_MAX includes the nul terminator --RR.
1161da177e4SLinus Torvalds  */
117858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page)
1181da177e4SLinus Torvalds {
1191da177e4SLinus Torvalds 	int retval;
1201da177e4SLinus Torvalds 	unsigned long len = PATH_MAX;
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	if (!segment_eq(get_fs(), KERNEL_DS)) {
1231da177e4SLinus Torvalds 		if ((unsigned long) filename >= TASK_SIZE)
1241da177e4SLinus Torvalds 			return -EFAULT;
1251da177e4SLinus Torvalds 		if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
1261da177e4SLinus Torvalds 			len = TASK_SIZE - (unsigned long) filename;
1271da177e4SLinus Torvalds 	}
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	retval = strncpy_from_user(page, filename, len);
1301da177e4SLinus Torvalds 	if (retval > 0) {
1311da177e4SLinus Torvalds 		if (retval < len)
1321da177e4SLinus Torvalds 			return 0;
1331da177e4SLinus Torvalds 		return -ENAMETOOLONG;
1341da177e4SLinus Torvalds 	} else if (!retval)
1351da177e4SLinus Torvalds 		retval = -ENOENT;
1361da177e4SLinus Torvalds 	return retval;
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
139f52e0c11SAl Viro static char *getname_flags(const char __user * filename, int flags)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	char *tmp, *result;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	result = ERR_PTR(-ENOMEM);
1441da177e4SLinus Torvalds 	tmp = __getname();
1451da177e4SLinus Torvalds 	if (tmp)  {
1461da177e4SLinus Torvalds 		int retval = do_getname(filename, tmp);
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 		result = tmp;
1491da177e4SLinus Torvalds 		if (retval < 0) {
150f52e0c11SAl Viro 			if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
1511da177e4SLinus Torvalds 				__putname(tmp);
1521da177e4SLinus Torvalds 				result = ERR_PTR(retval);
1531da177e4SLinus Torvalds 			}
1541da177e4SLinus Torvalds 		}
155f52e0c11SAl Viro 	}
1561da177e4SLinus Torvalds 	audit_getname(result);
1571da177e4SLinus Torvalds 	return result;
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
160f52e0c11SAl Viro char *getname(const char __user * filename)
161f52e0c11SAl Viro {
162f52e0c11SAl Viro 	return getname_flags(filename, 0);
163f52e0c11SAl Viro }
164f52e0c11SAl Viro 
1651da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
1661da177e4SLinus Torvalds void putname(const char *name)
1671da177e4SLinus Torvalds {
1685ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
1691da177e4SLinus Torvalds 		audit_putname(name);
1701da177e4SLinus Torvalds 	else
1711da177e4SLinus Torvalds 		__putname(name);
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds EXPORT_SYMBOL(putname);
1741da177e4SLinus Torvalds #endif
1751da177e4SLinus Torvalds 
1765909ccaaSLinus Torvalds /*
1775909ccaaSLinus Torvalds  * This does basic POSIX ACL permission checking
1785909ccaaSLinus Torvalds  */
1797e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask)
1805909ccaaSLinus Torvalds {
1817e40145eSAl Viro 	int (*check_acl)(struct inode *inode, int mask);
18226cf46beSLinus Torvalds 	unsigned int mode = inode->i_mode;
1835909ccaaSLinus Torvalds 
1849c2c7039SAl Viro 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
1855909ccaaSLinus Torvalds 
186e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
187e795b717SSerge E. Hallyn 		goto other_perms;
188e795b717SSerge E. Hallyn 
1895909ccaaSLinus Torvalds 	if (current_fsuid() == inode->i_uid)
1905909ccaaSLinus Torvalds 		mode >>= 6;
1915909ccaaSLinus Torvalds 	else {
192178ea735SAl Viro 		check_acl = inode->i_op->check_acl;
1935909ccaaSLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
1947e40145eSAl Viro 			int error = check_acl(inode, mask);
1955909ccaaSLinus Torvalds 			if (error != -EAGAIN)
1965909ccaaSLinus Torvalds 				return error;
1975909ccaaSLinus Torvalds 		}
1985909ccaaSLinus Torvalds 
1995909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
2005909ccaaSLinus Torvalds 			mode >>= 3;
2015909ccaaSLinus Torvalds 	}
2025909ccaaSLinus Torvalds 
203e795b717SSerge E. Hallyn other_perms:
2045909ccaaSLinus Torvalds 	/*
2055909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
2065909ccaaSLinus Torvalds 	 */
2079c2c7039SAl Viro 	if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
2085909ccaaSLinus Torvalds 		return 0;
2095909ccaaSLinus Torvalds 	return -EACCES;
2105909ccaaSLinus Torvalds }
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds /**
2131da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
2141da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2151da177e4SLinus Torvalds  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
21639191628SRandy Dunlap  * @flags:	IPERM_FLAG_ flags.
2171da177e4SLinus Torvalds  *
2181da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
2191da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
2201da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
221b74c79e9SNick Piggin  * are used for other things.
222b74c79e9SNick Piggin  *
223b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
224b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
225b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
2261da177e4SLinus Torvalds  */
2272830ba7fSAl Viro int generic_permission(struct inode *inode, int mask)
2281da177e4SLinus Torvalds {
2295909ccaaSLinus Torvalds 	int ret;
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	/*
2325909ccaaSLinus Torvalds 	 * Do the basic POSIX ACL permission checks.
2331da177e4SLinus Torvalds 	 */
2347e40145eSAl Viro 	ret = acl_permission_check(inode, mask);
2355909ccaaSLinus Torvalds 	if (ret != -EACCES)
2365909ccaaSLinus Torvalds 		return ret;
2371da177e4SLinus Torvalds 
238d594e7ecSAl Viro 	if (S_ISDIR(inode->i_mode)) {
239d594e7ecSAl Viro 		/* DACs are overridable for directories */
240d594e7ecSAl Viro 		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
241d594e7ecSAl Viro 			return 0;
242d594e7ecSAl Viro 		if (!(mask & MAY_WRITE))
243d594e7ecSAl Viro 			if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
244d594e7ecSAl Viro 				return 0;
245d594e7ecSAl Viro 		return -EACCES;
246d594e7ecSAl Viro 	}
2471da177e4SLinus Torvalds 	/*
2481da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
249d594e7ecSAl Viro 	 * Executable DACs are overridable when there is
250d594e7ecSAl Viro 	 * at least one exec bit set.
2511da177e4SLinus Torvalds 	 */
252d594e7ecSAl Viro 	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
253e795b717SSerge E. Hallyn 		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
2541da177e4SLinus Torvalds 			return 0;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	/*
2571da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
2581da177e4SLinus Torvalds 	 */
2597ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
260d594e7ecSAl Viro 	if (mask == MAY_READ)
261e795b717SSerge E. Hallyn 		if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
2621da177e4SLinus Torvalds 			return 0;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	return -EACCES;
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds 
267cb23beb5SChristoph Hellwig /**
268cb23beb5SChristoph Hellwig  * inode_permission  -  check for access rights to a given inode
269cb23beb5SChristoph Hellwig  * @inode:	inode to check permission on
270cb23beb5SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
271cb23beb5SChristoph Hellwig  *
272cb23beb5SChristoph Hellwig  * Used to check for read/write/execute permissions on an inode.
273cb23beb5SChristoph Hellwig  * We use "fsuid" for this, letting us set arbitrary permissions
274cb23beb5SChristoph Hellwig  * for filesystem access without changing the "normal" uids which
275cb23beb5SChristoph Hellwig  * are used for other things.
276cb23beb5SChristoph Hellwig  */
277f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask)
2781da177e4SLinus Torvalds {
279e6305c43SAl Viro 	int retval;
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	if (mask & MAY_WRITE) {
28222590e41SMiklos Szeredi 		umode_t mode = inode->i_mode;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 		/*
2851da177e4SLinus Torvalds 		 * Nobody gets write access to a read-only fs.
2861da177e4SLinus Torvalds 		 */
2871da177e4SLinus Torvalds 		if (IS_RDONLY(inode) &&
2881da177e4SLinus Torvalds 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
2891da177e4SLinus Torvalds 			return -EROFS;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 		/*
2921da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
2931da177e4SLinus Torvalds 		 */
2941da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
2951da177e4SLinus Torvalds 			return -EACCES;
2961da177e4SLinus Torvalds 	}
2971da177e4SLinus Torvalds 
298acfa4380SAl Viro 	if (inode->i_op->permission)
29910556cb2SAl Viro 		retval = inode->i_op->permission(inode, mask);
300f696a365SMiklos Szeredi 	else
3012830ba7fSAl Viro 		retval = generic_permission(inode, mask);
302f696a365SMiklos Szeredi 
3031da177e4SLinus Torvalds 	if (retval)
3041da177e4SLinus Torvalds 		return retval;
3051da177e4SLinus Torvalds 
30608ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
30708ce5f16SSerge E. Hallyn 	if (retval)
30808ce5f16SSerge E. Hallyn 		return retval;
30908ce5f16SSerge E. Hallyn 
310d09ca739SEric Paris 	return security_inode_permission(inode, mask);
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds 
313f4d6ff89SAl Viro /**
3145dd784d0SJan Blunck  * path_get - get a reference to a path
3155dd784d0SJan Blunck  * @path: path to get the reference to
3165dd784d0SJan Blunck  *
3175dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3185dd784d0SJan Blunck  */
3195dd784d0SJan Blunck void path_get(struct path *path)
3205dd784d0SJan Blunck {
3215dd784d0SJan Blunck 	mntget(path->mnt);
3225dd784d0SJan Blunck 	dget(path->dentry);
3235dd784d0SJan Blunck }
3245dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
3255dd784d0SJan Blunck 
3265dd784d0SJan Blunck /**
3271d957f9bSJan Blunck  * path_put - put a reference to a path
3281d957f9bSJan Blunck  * @path: path to put the reference to
3291d957f9bSJan Blunck  *
3301d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
3311d957f9bSJan Blunck  */
3321d957f9bSJan Blunck void path_put(struct path *path)
3331da177e4SLinus Torvalds {
3341d957f9bSJan Blunck 	dput(path->dentry);
3351d957f9bSJan Blunck 	mntput(path->mnt);
3361da177e4SLinus Torvalds }
3371d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
3381da177e4SLinus Torvalds 
33919660af7SAl Viro /*
34031e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
34119660af7SAl Viro  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
34219660af7SAl Viro  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
34319660af7SAl Viro  * normal reference counts on dentries and vfsmounts to transition to rcu-walk
34419660af7SAl Viro  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
34519660af7SAl Viro  * got stuck, so ref-walk may continue from there. If this is not successful
34619660af7SAl Viro  * (eg. a seqcount has changed), then failure is returned and it's up to caller
34719660af7SAl Viro  * to restart the path walk from the beginning in ref-walk mode.
34831e6b01fSNick Piggin  */
34931e6b01fSNick Piggin 
35031e6b01fSNick Piggin /**
35119660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
35219660af7SAl Viro  * @nd: nameidata pathwalk data
35319660af7SAl Viro  * @dentry: child of nd->path.dentry or NULL
35439191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
35531e6b01fSNick Piggin  *
35619660af7SAl Viro  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
35719660af7SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
35819660af7SAl Viro  * @nd or NULL.  Must be called from rcu-walk context.
35931e6b01fSNick Piggin  */
36019660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
36131e6b01fSNick Piggin {
36231e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
36331e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
3645b6ca027SAl Viro 	int want_root = 0;
36531e6b01fSNick Piggin 
36631e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
3675b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
3685b6ca027SAl Viro 		want_root = 1;
36931e6b01fSNick Piggin 		spin_lock(&fs->lock);
37031e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
37131e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
37231e6b01fSNick Piggin 			goto err_root;
37331e6b01fSNick Piggin 	}
37431e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
37519660af7SAl Viro 	if (!dentry) {
37619660af7SAl Viro 		if (!__d_rcu_to_refcount(parent, nd->seq))
37719660af7SAl Viro 			goto err_parent;
37819660af7SAl Viro 		BUG_ON(nd->inode != parent->d_inode);
37919660af7SAl Viro 	} else {
38094c0d4ecSAl Viro 		if (dentry->d_parent != parent)
38194c0d4ecSAl Viro 			goto err_parent;
38231e6b01fSNick Piggin 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
38331e6b01fSNick Piggin 		if (!__d_rcu_to_refcount(dentry, nd->seq))
38419660af7SAl Viro 			goto err_child;
38531e6b01fSNick Piggin 		/*
38619660af7SAl Viro 		 * If the sequence check on the child dentry passed, then
38719660af7SAl Viro 		 * the child has not been removed from its parent. This
38819660af7SAl Viro 		 * means the parent dentry must be valid and able to take
38919660af7SAl Viro 		 * a reference at this point.
39031e6b01fSNick Piggin 		 */
39131e6b01fSNick Piggin 		BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
39231e6b01fSNick Piggin 		BUG_ON(!parent->d_count);
39331e6b01fSNick Piggin 		parent->d_count++;
39431e6b01fSNick Piggin 		spin_unlock(&dentry->d_lock);
39519660af7SAl Viro 	}
39631e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
3975b6ca027SAl Viro 	if (want_root) {
39831e6b01fSNick Piggin 		path_get(&nd->root);
39931e6b01fSNick Piggin 		spin_unlock(&fs->lock);
40031e6b01fSNick Piggin 	}
40131e6b01fSNick Piggin 	mntget(nd->path.mnt);
40231e6b01fSNick Piggin 
40331e6b01fSNick Piggin 	rcu_read_unlock();
40431e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
40531e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
40631e6b01fSNick Piggin 	return 0;
40719660af7SAl Viro 
40819660af7SAl Viro err_child:
40931e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
41019660af7SAl Viro err_parent:
41131e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
41231e6b01fSNick Piggin err_root:
4135b6ca027SAl Viro 	if (want_root)
41431e6b01fSNick Piggin 		spin_unlock(&fs->lock);
41531e6b01fSNick Piggin 	return -ECHILD;
41631e6b01fSNick Piggin }
41731e6b01fSNick Piggin 
41831e6b01fSNick Piggin /**
419834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
420834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
421834f2a4aSTrond Myklebust  */
422834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
423834f2a4aSTrond Myklebust {
4242dab5974SLinus Torvalds 	struct file *file = nd->intent.open.file;
4252dab5974SLinus Torvalds 
4262dab5974SLinus Torvalds 	if (file && !IS_ERR(file)) {
4272dab5974SLinus Torvalds 		if (file->f_path.dentry == NULL)
4282dab5974SLinus Torvalds 			put_filp(file);
429834f2a4aSTrond Myklebust 		else
4302dab5974SLinus Torvalds 			fput(file);
4312dab5974SLinus Torvalds 	}
432834f2a4aSTrond Myklebust }
433834f2a4aSTrond Myklebust 
434f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
43534286d66SNick Piggin {
436f60aef7eSAl Viro 	return dentry->d_op->d_revalidate(dentry, nd);
43734286d66SNick Piggin }
43834286d66SNick Piggin 
4399f1fafeeSAl Viro /**
4409f1fafeeSAl Viro  * complete_walk - successful completion of path walk
4419f1fafeeSAl Viro  * @nd:  pointer nameidata
44239159de2SJeff Layton  *
4439f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
4449f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
4459f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
4469f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
4479f1fafeeSAl Viro  * need to drop nd->path.
44839159de2SJeff Layton  */
4499f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
45039159de2SJeff Layton {
45116c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
45239159de2SJeff Layton 	int status;
45339159de2SJeff Layton 
4549f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
4559f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
4569f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
4579f1fafeeSAl Viro 			nd->root.mnt = NULL;
4589f1fafeeSAl Viro 		spin_lock(&dentry->d_lock);
4599f1fafeeSAl Viro 		if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
4609f1fafeeSAl Viro 			spin_unlock(&dentry->d_lock);
4619f1fafeeSAl Viro 			rcu_read_unlock();
4629f1fafeeSAl Viro 			br_read_unlock(vfsmount_lock);
4639f1fafeeSAl Viro 			return -ECHILD;
4649f1fafeeSAl Viro 		}
4659f1fafeeSAl Viro 		BUG_ON(nd->inode != dentry->d_inode);
4669f1fafeeSAl Viro 		spin_unlock(&dentry->d_lock);
4679f1fafeeSAl Viro 		mntget(nd->path.mnt);
4689f1fafeeSAl Viro 		rcu_read_unlock();
4699f1fafeeSAl Viro 		br_read_unlock(vfsmount_lock);
4709f1fafeeSAl Viro 	}
4719f1fafeeSAl Viro 
47216c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
47339159de2SJeff Layton 		return 0;
47439159de2SJeff Layton 
47516c2cd71SAl Viro 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
47616c2cd71SAl Viro 		return 0;
47716c2cd71SAl Viro 
47816c2cd71SAl Viro 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
47916c2cd71SAl Viro 		return 0;
48016c2cd71SAl Viro 
48116c2cd71SAl Viro 	/* Note: we do not d_invalidate() */
48234286d66SNick Piggin 	status = d_revalidate(dentry, nd);
48339159de2SJeff Layton 	if (status > 0)
48439159de2SJeff Layton 		return 0;
48539159de2SJeff Layton 
48616c2cd71SAl Viro 	if (!status)
48739159de2SJeff Layton 		status = -ESTALE;
48816c2cd71SAl Viro 
4899f1fafeeSAl Viro 	path_put(&nd->path);
49039159de2SJeff Layton 	return status;
49139159de2SJeff Layton }
49239159de2SJeff Layton 
4932a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
4942a737871SAl Viro {
495f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
496f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
4972a737871SAl Viro }
4982a737871SAl Viro 
4996de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
5006de88d72SAl Viro 
50131e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
50231e6b01fSNick Piggin {
50331e6b01fSNick Piggin 	if (!nd->root.mnt) {
50431e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
505c28cc364SNick Piggin 		unsigned seq;
506c28cc364SNick Piggin 
507c28cc364SNick Piggin 		do {
508c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
50931e6b01fSNick Piggin 			nd->root = fs->root;
510c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
511c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
51231e6b01fSNick Piggin 	}
51331e6b01fSNick Piggin }
51431e6b01fSNick Piggin 
515f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
5161da177e4SLinus Torvalds {
51731e6b01fSNick Piggin 	int ret;
51831e6b01fSNick Piggin 
5191da177e4SLinus Torvalds 	if (IS_ERR(link))
5201da177e4SLinus Torvalds 		goto fail;
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	if (*link == '/') {
5232a737871SAl Viro 		set_root(nd);
5241d957f9bSJan Blunck 		path_put(&nd->path);
5252a737871SAl Viro 		nd->path = nd->root;
5262a737871SAl Viro 		path_get(&nd->root);
52716c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
5281da177e4SLinus Torvalds 	}
52931e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
530b4091d5fSChristoph Hellwig 
53131e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
53231e6b01fSNick Piggin 	return ret;
5331da177e4SLinus Torvalds fail:
5341d957f9bSJan Blunck 	path_put(&nd->path);
5351da177e4SLinus Torvalds 	return PTR_ERR(link);
5361da177e4SLinus Torvalds }
5371da177e4SLinus Torvalds 
5381d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
539051d3812SIan Kent {
540051d3812SIan Kent 	dput(path->dentry);
5414ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
542051d3812SIan Kent 		mntput(path->mnt);
543051d3812SIan Kent }
544051d3812SIan Kent 
5457b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
5467b9337aaSNick Piggin 					struct nameidata *nd)
547051d3812SIan Kent {
54831e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
5494ac91378SJan Blunck 		dput(nd->path.dentry);
55031e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
5514ac91378SJan Blunck 			mntput(nd->path.mnt);
5529a229683SHuang Shijie 	}
55331e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
5544ac91378SJan Blunck 	nd->path.dentry = path->dentry;
555051d3812SIan Kent }
556051d3812SIan Kent 
557574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
558574197e0SAl Viro {
559574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
560574197e0SAl Viro 	if (!IS_ERR(cookie) && inode->i_op->put_link)
561574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
562574197e0SAl Viro 	path_put(link);
563574197e0SAl Viro }
564574197e0SAl Viro 
565def4af30SAl Viro static __always_inline int
566574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
5671da177e4SLinus Torvalds {
5681da177e4SLinus Torvalds 	int error;
5697b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
5701da177e4SLinus Torvalds 
571844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
572844a3917SAl Viro 
5730e794589SAl Viro 	if (link->mnt == nd->path.mnt)
5740e794589SAl Viro 		mntget(link->mnt);
5750e794589SAl Viro 
576574197e0SAl Viro 	if (unlikely(current->total_link_count >= 40)) {
577574197e0SAl Viro 		*p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
578574197e0SAl Viro 		path_put(&nd->path);
579574197e0SAl Viro 		return -ELOOP;
580574197e0SAl Viro 	}
581574197e0SAl Viro 	cond_resched();
582574197e0SAl Viro 	current->total_link_count++;
583574197e0SAl Viro 
5847b9337aaSNick Piggin 	touch_atime(link->mnt, dentry);
5851da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
586cd4e91d3SAl Viro 
58736f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
58836f3b4f6SAl Viro 	if (error) {
58936f3b4f6SAl Viro 		*p = ERR_PTR(error); /* no ->put_link(), please */
59036f3b4f6SAl Viro 		path_put(&nd->path);
59136f3b4f6SAl Viro 		return error;
59236f3b4f6SAl Viro 	}
59336f3b4f6SAl Viro 
59486acdca1SAl Viro 	nd->last_type = LAST_BIND;
595def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
596def4af30SAl Viro 	error = PTR_ERR(*p);
597def4af30SAl Viro 	if (!IS_ERR(*p)) {
5981da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
599cc314eefSLinus Torvalds 		error = 0;
6001da177e4SLinus Torvalds 		if (s)
6011da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
602bcda7652SAl Viro 		else if (nd->last_type == LAST_BIND) {
60316c2cd71SAl Viro 			nd->flags |= LOOKUP_JUMPED;
604b21041d0SAl Viro 			nd->inode = nd->path.dentry->d_inode;
605b21041d0SAl Viro 			if (nd->inode->i_op->follow_link) {
606bcda7652SAl Viro 				/* stepped on a _really_ weird one */
607bcda7652SAl Viro 				path_put(&nd->path);
608bcda7652SAl Viro 				error = -ELOOP;
609bcda7652SAl Viro 			}
610bcda7652SAl Viro 		}
6111da177e4SLinus Torvalds 	}
6121da177e4SLinus Torvalds 	return error;
6131da177e4SLinus Torvalds }
6141da177e4SLinus Torvalds 
61531e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
61631e6b01fSNick Piggin {
61731e6b01fSNick Piggin 	struct vfsmount *parent;
61831e6b01fSNick Piggin 	struct dentry *mountpoint;
61931e6b01fSNick Piggin 
62031e6b01fSNick Piggin 	parent = path->mnt->mnt_parent;
62131e6b01fSNick Piggin 	if (parent == path->mnt)
62231e6b01fSNick Piggin 		return 0;
62331e6b01fSNick Piggin 	mountpoint = path->mnt->mnt_mountpoint;
62431e6b01fSNick Piggin 	path->dentry = mountpoint;
62531e6b01fSNick Piggin 	path->mnt = parent;
62631e6b01fSNick Piggin 	return 1;
62731e6b01fSNick Piggin }
62831e6b01fSNick Piggin 
629bab77ebfSAl Viro int follow_up(struct path *path)
6301da177e4SLinus Torvalds {
6311da177e4SLinus Torvalds 	struct vfsmount *parent;
6321da177e4SLinus Torvalds 	struct dentry *mountpoint;
63399b7db7bSNick Piggin 
63499b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
635bab77ebfSAl Viro 	parent = path->mnt->mnt_parent;
636bab77ebfSAl Viro 	if (parent == path->mnt) {
63799b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
6381da177e4SLinus Torvalds 		return 0;
6391da177e4SLinus Torvalds 	}
6401da177e4SLinus Torvalds 	mntget(parent);
641bab77ebfSAl Viro 	mountpoint = dget(path->mnt->mnt_mountpoint);
64299b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
643bab77ebfSAl Viro 	dput(path->dentry);
644bab77ebfSAl Viro 	path->dentry = mountpoint;
645bab77ebfSAl Viro 	mntput(path->mnt);
646bab77ebfSAl Viro 	path->mnt = parent;
6471da177e4SLinus Torvalds 	return 1;
6481da177e4SLinus Torvalds }
6491da177e4SLinus Torvalds 
650b5c84bf6SNick Piggin /*
6519875cf80SDavid Howells  * Perform an automount
6529875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
6539875cf80SDavid Howells  *   were called with.
6541da177e4SLinus Torvalds  */
6559875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
6569875cf80SDavid Howells 			    bool *need_mntput)
65731e6b01fSNick Piggin {
6589875cf80SDavid Howells 	struct vfsmount *mnt;
659ea5b778aSDavid Howells 	int err;
6609875cf80SDavid Howells 
6619875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
6629875cf80SDavid Howells 		return -EREMOTE;
6639875cf80SDavid Howells 
6646f45b656SDavid Howells 	/* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
6656f45b656SDavid Howells 	 * and this is the terminal part of the path.
6666f45b656SDavid Howells 	 */
66749084c3bSAl Viro 	if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_PARENT))
6686f45b656SDavid Howells 		return -EISDIR; /* we actually want to stop here */
6696f45b656SDavid Howells 
6709875cf80SDavid Howells 	/* We want to mount if someone is trying to open/create a file of any
6719875cf80SDavid Howells 	 * type under the mountpoint, wants to traverse through the mountpoint
6729875cf80SDavid Howells 	 * or wants to open the mounted directory.
6739875cf80SDavid Howells 	 *
6749875cf80SDavid Howells 	 * We don't want to mount if someone's just doing a stat and they've
6759875cf80SDavid Howells 	 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
6769875cf80SDavid Howells 	 * appended a '/' to the name.
6779875cf80SDavid Howells 	 */
6789875cf80SDavid Howells 	if (!(flags & LOOKUP_FOLLOW) &&
67949084c3bSAl Viro 	    !(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
6809875cf80SDavid Howells 		       LOOKUP_OPEN | LOOKUP_CREATE)))
6819875cf80SDavid Howells 		return -EISDIR;
6829875cf80SDavid Howells 
6839875cf80SDavid Howells 	current->total_link_count++;
6849875cf80SDavid Howells 	if (current->total_link_count >= 40)
6859875cf80SDavid Howells 		return -ELOOP;
6869875cf80SDavid Howells 
6879875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
6889875cf80SDavid Howells 	if (IS_ERR(mnt)) {
6899875cf80SDavid Howells 		/*
6909875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
6919875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
6929875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
6939875cf80SDavid Howells 		 *
6949875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
6959875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
6969875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
6979875cf80SDavid Howells 		 */
69849084c3bSAl Viro 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
6999875cf80SDavid Howells 			return -EREMOTE;
7009875cf80SDavid Howells 		return PTR_ERR(mnt);
70131e6b01fSNick Piggin 	}
702ea5b778aSDavid Howells 
7039875cf80SDavid Howells 	if (!mnt) /* mount collision */
7049875cf80SDavid Howells 		return 0;
7059875cf80SDavid Howells 
7068aef1884SAl Viro 	if (!*need_mntput) {
7078aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
7088aef1884SAl Viro 		mntget(path->mnt);
7098aef1884SAl Viro 		*need_mntput = true;
7108aef1884SAl Viro 	}
71119a167afSAl Viro 	err = finish_automount(mnt, path);
712ea5b778aSDavid Howells 
713ea5b778aSDavid Howells 	switch (err) {
714ea5b778aSDavid Howells 	case -EBUSY:
715ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
71619a167afSAl Viro 		return 0;
717ea5b778aSDavid Howells 	case 0:
7188aef1884SAl Viro 		path_put(path);
7199875cf80SDavid Howells 		path->mnt = mnt;
7209875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
7219875cf80SDavid Howells 		return 0;
72219a167afSAl Viro 	default:
72319a167afSAl Viro 		return err;
7249875cf80SDavid Howells 	}
72519a167afSAl Viro 
726ea5b778aSDavid Howells }
7279875cf80SDavid Howells 
7289875cf80SDavid Howells /*
7299875cf80SDavid Howells  * Handle a dentry that is managed in some way.
730cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
7319875cf80SDavid Howells  * - Flagged as mountpoint
7329875cf80SDavid Howells  * - Flagged as automount point
7339875cf80SDavid Howells  *
7349875cf80SDavid Howells  * This may only be called in refwalk mode.
7359875cf80SDavid Howells  *
7369875cf80SDavid Howells  * Serialization is taken care of in namespace.c
7379875cf80SDavid Howells  */
7389875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
7399875cf80SDavid Howells {
7408aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
7419875cf80SDavid Howells 	unsigned managed;
7429875cf80SDavid Howells 	bool need_mntput = false;
7438aef1884SAl Viro 	int ret = 0;
7449875cf80SDavid Howells 
7459875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
7469875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
7479875cf80SDavid Howells 	 * the components of that value change under us */
7489875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
7499875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
7509875cf80SDavid Howells 	       unlikely(managed != 0)) {
751cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
752cc53ce53SDavid Howells 		 * being held. */
753cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
754cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
755cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
7561aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
757cc53ce53SDavid Howells 			if (ret < 0)
7588aef1884SAl Viro 				break;
759cc53ce53SDavid Howells 		}
760cc53ce53SDavid Howells 
7619875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
7629875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
7639875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
7649875cf80SDavid Howells 			if (mounted) {
7659875cf80SDavid Howells 				dput(path->dentry);
7669875cf80SDavid Howells 				if (need_mntput)
767463ffb2eSAl Viro 					mntput(path->mnt);
768463ffb2eSAl Viro 				path->mnt = mounted;
769463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
7709875cf80SDavid Howells 				need_mntput = true;
7719875cf80SDavid Howells 				continue;
772463ffb2eSAl Viro 			}
773463ffb2eSAl Viro 
7749875cf80SDavid Howells 			/* Something is mounted on this dentry in another
7759875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
7769875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
7779875cf80SDavid Howells 			 * vfsmount_lock */
7781da177e4SLinus Torvalds 		}
7799875cf80SDavid Howells 
7809875cf80SDavid Howells 		/* Handle an automount point */
7819875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
7829875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
7839875cf80SDavid Howells 			if (ret < 0)
7848aef1884SAl Viro 				break;
7859875cf80SDavid Howells 			continue;
7869875cf80SDavid Howells 		}
7879875cf80SDavid Howells 
7889875cf80SDavid Howells 		/* We didn't change the current path point */
7899875cf80SDavid Howells 		break;
7909875cf80SDavid Howells 	}
7918aef1884SAl Viro 
7928aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
7938aef1884SAl Viro 		mntput(path->mnt);
7948aef1884SAl Viro 	if (ret == -EISDIR)
7958aef1884SAl Viro 		ret = 0;
7968aef1884SAl Viro 	return ret;
7971da177e4SLinus Torvalds }
7981da177e4SLinus Torvalds 
799cc53ce53SDavid Howells int follow_down_one(struct path *path)
8001da177e4SLinus Torvalds {
8011da177e4SLinus Torvalds 	struct vfsmount *mounted;
8021da177e4SLinus Torvalds 
8031c755af4SAl Viro 	mounted = lookup_mnt(path);
8041da177e4SLinus Torvalds 	if (mounted) {
8059393bd07SAl Viro 		dput(path->dentry);
8069393bd07SAl Viro 		mntput(path->mnt);
8079393bd07SAl Viro 		path->mnt = mounted;
8089393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
8091da177e4SLinus Torvalds 		return 1;
8101da177e4SLinus Torvalds 	}
8111da177e4SLinus Torvalds 	return 0;
8121da177e4SLinus Torvalds }
8131da177e4SLinus Torvalds 
81462a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
81562a7375eSIan Kent {
81662a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
81762a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
81862a7375eSIan Kent }
81962a7375eSIan Kent 
8209875cf80SDavid Howells /*
821287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
822287548e4SAl Viro  * we meet a managed dentry that would need blocking.
8239875cf80SDavid Howells  */
8249875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
825287548e4SAl Viro 			       struct inode **inode)
8269875cf80SDavid Howells {
82762a7375eSIan Kent 	for (;;) {
8289875cf80SDavid Howells 		struct vfsmount *mounted;
82962a7375eSIan Kent 		/*
83062a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
83162a7375eSIan Kent 		 * that wants to block transit.
83262a7375eSIan Kent 		 */
833287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
834ab90911fSDavid Howells 			return false;
83562a7375eSIan Kent 
83662a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
83762a7375eSIan Kent 			break;
83862a7375eSIan Kent 
8399875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
8409875cf80SDavid Howells 		if (!mounted)
8419875cf80SDavid Howells 			break;
8429875cf80SDavid Howells 		path->mnt = mounted;
8439875cf80SDavid Howells 		path->dentry = mounted->mnt_root;
8449875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
84559430262SLinus Torvalds 		/*
84659430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
84759430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
84859430262SLinus Torvalds 		 * because a mount-point is always pinned.
84959430262SLinus Torvalds 		 */
85059430262SLinus Torvalds 		*inode = path->dentry->d_inode;
8519875cf80SDavid Howells 	}
8529875cf80SDavid Howells 	return true;
8539875cf80SDavid Howells }
8549875cf80SDavid Howells 
855dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
856287548e4SAl Viro {
857dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
858287548e4SAl Viro 		struct vfsmount *mounted;
859dea39376SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
860287548e4SAl Viro 		if (!mounted)
861287548e4SAl Viro 			break;
862dea39376SAl Viro 		nd->path.mnt = mounted;
863dea39376SAl Viro 		nd->path.dentry = mounted->mnt_root;
864dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
865287548e4SAl Viro 	}
866287548e4SAl Viro }
867287548e4SAl Viro 
86831e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
86931e6b01fSNick Piggin {
87031e6b01fSNick Piggin 	set_root_rcu(nd);
87131e6b01fSNick Piggin 
87231e6b01fSNick Piggin 	while (1) {
87331e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
87431e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
87531e6b01fSNick Piggin 			break;
87631e6b01fSNick Piggin 		}
87731e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
87831e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
87931e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
88031e6b01fSNick Piggin 			unsigned seq;
88131e6b01fSNick Piggin 
88231e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
88331e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
884ef7562d5SAl Viro 				goto failed;
88531e6b01fSNick Piggin 			nd->path.dentry = parent;
88631e6b01fSNick Piggin 			nd->seq = seq;
88731e6b01fSNick Piggin 			break;
88831e6b01fSNick Piggin 		}
88931e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
89031e6b01fSNick Piggin 			break;
89131e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
89231e6b01fSNick Piggin 	}
893dea39376SAl Viro 	follow_mount_rcu(nd);
894dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
89531e6b01fSNick Piggin 	return 0;
896ef7562d5SAl Viro 
897ef7562d5SAl Viro failed:
898ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
8995b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
900ef7562d5SAl Viro 		nd->root.mnt = NULL;
901ef7562d5SAl Viro 	rcu_read_unlock();
902ef7562d5SAl Viro 	br_read_unlock(vfsmount_lock);
903ef7562d5SAl Viro 	return -ECHILD;
90431e6b01fSNick Piggin }
90531e6b01fSNick Piggin 
9069875cf80SDavid Howells /*
907cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
908cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
909cc53ce53SDavid Howells  * caller is permitted to proceed or not.
910cc53ce53SDavid Howells  */
9117cc90cc3SAl Viro int follow_down(struct path *path)
912cc53ce53SDavid Howells {
913cc53ce53SDavid Howells 	unsigned managed;
914cc53ce53SDavid Howells 	int ret;
915cc53ce53SDavid Howells 
916cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
917cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
918cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
919cc53ce53SDavid Howells 		 * being held.
920cc53ce53SDavid Howells 		 *
921cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
922cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
923cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
924cc53ce53SDavid Howells 		 * superstructure.
925cc53ce53SDavid Howells 		 *
926cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
927cc53ce53SDavid Howells 		 */
928cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
929cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
930cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
931ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
9321aed3e42SAl Viro 				path->dentry, false);
933cc53ce53SDavid Howells 			if (ret < 0)
934cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
935cc53ce53SDavid Howells 		}
936cc53ce53SDavid Howells 
937cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
938cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
939cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
940cc53ce53SDavid Howells 			if (!mounted)
941cc53ce53SDavid Howells 				break;
942cc53ce53SDavid Howells 			dput(path->dentry);
943cc53ce53SDavid Howells 			mntput(path->mnt);
944cc53ce53SDavid Howells 			path->mnt = mounted;
945cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
946cc53ce53SDavid Howells 			continue;
947cc53ce53SDavid Howells 		}
948cc53ce53SDavid Howells 
949cc53ce53SDavid Howells 		/* Don't handle automount points here */
950cc53ce53SDavid Howells 		break;
951cc53ce53SDavid Howells 	}
952cc53ce53SDavid Howells 	return 0;
953cc53ce53SDavid Howells }
954cc53ce53SDavid Howells 
955cc53ce53SDavid Howells /*
9569875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
9579875cf80SDavid Howells  */
9589875cf80SDavid Howells static void follow_mount(struct path *path)
9599875cf80SDavid Howells {
9609875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
9619875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
9629875cf80SDavid Howells 		if (!mounted)
9639875cf80SDavid Howells 			break;
9649875cf80SDavid Howells 		dput(path->dentry);
9659875cf80SDavid Howells 		mntput(path->mnt);
9669875cf80SDavid Howells 		path->mnt = mounted;
9679875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
9689875cf80SDavid Howells 	}
9699875cf80SDavid Howells }
9709875cf80SDavid Howells 
97131e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
9721da177e4SLinus Torvalds {
9732a737871SAl Viro 	set_root(nd);
974e518ddb7SAndreas Mohr 
9751da177e4SLinus Torvalds 	while(1) {
9764ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
9771da177e4SLinus Torvalds 
9782a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
9792a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
9801da177e4SLinus Torvalds 			break;
9811da177e4SLinus Torvalds 		}
9824ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
9833088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
9843088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
9851da177e4SLinus Torvalds 			dput(old);
9861da177e4SLinus Torvalds 			break;
9871da177e4SLinus Torvalds 		}
9883088dd70SAl Viro 		if (!follow_up(&nd->path))
9891da177e4SLinus Torvalds 			break;
9901da177e4SLinus Torvalds 	}
99179ed0226SAl Viro 	follow_mount(&nd->path);
99231e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
9931da177e4SLinus Torvalds }
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds /*
996baa03890SNick Piggin  * Allocate a dentry with name and parent, and perform a parent
997baa03890SNick Piggin  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
998baa03890SNick Piggin  * on error. parent->d_inode->i_mutex must be held. d_lookup must
999baa03890SNick Piggin  * have verified that no child exists while under i_mutex.
1000baa03890SNick Piggin  */
1001baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1002baa03890SNick Piggin 				struct qstr *name, struct nameidata *nd)
1003baa03890SNick Piggin {
1004baa03890SNick Piggin 	struct inode *inode = parent->d_inode;
1005baa03890SNick Piggin 	struct dentry *dentry;
1006baa03890SNick Piggin 	struct dentry *old;
1007baa03890SNick Piggin 
1008baa03890SNick Piggin 	/* Don't create child dentry for a dead directory. */
1009baa03890SNick Piggin 	if (unlikely(IS_DEADDIR(inode)))
1010baa03890SNick Piggin 		return ERR_PTR(-ENOENT);
1011baa03890SNick Piggin 
1012baa03890SNick Piggin 	dentry = d_alloc(parent, name);
1013baa03890SNick Piggin 	if (unlikely(!dentry))
1014baa03890SNick Piggin 		return ERR_PTR(-ENOMEM);
1015baa03890SNick Piggin 
1016baa03890SNick Piggin 	old = inode->i_op->lookup(inode, dentry, nd);
1017baa03890SNick Piggin 	if (unlikely(old)) {
1018baa03890SNick Piggin 		dput(dentry);
1019baa03890SNick Piggin 		dentry = old;
1020baa03890SNick Piggin 	}
1021baa03890SNick Piggin 	return dentry;
1022baa03890SNick Piggin }
1023baa03890SNick Piggin 
1024baa03890SNick Piggin /*
102544396f4bSJosef Bacik  * We already have a dentry, but require a lookup to be performed on the parent
102644396f4bSJosef Bacik  * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
102744396f4bSJosef Bacik  * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
102844396f4bSJosef Bacik  * child exists while under i_mutex.
102944396f4bSJosef Bacik  */
103044396f4bSJosef Bacik static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
103144396f4bSJosef Bacik 				     struct nameidata *nd)
103244396f4bSJosef Bacik {
103344396f4bSJosef Bacik 	struct inode *inode = parent->d_inode;
103444396f4bSJosef Bacik 	struct dentry *old;
103544396f4bSJosef Bacik 
103644396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
103744396f4bSJosef Bacik 	if (unlikely(IS_DEADDIR(inode)))
103844396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
103944396f4bSJosef Bacik 
104044396f4bSJosef Bacik 	old = inode->i_op->lookup(inode, dentry, nd);
104144396f4bSJosef Bacik 	if (unlikely(old)) {
104244396f4bSJosef Bacik 		dput(dentry);
104344396f4bSJosef Bacik 		dentry = old;
104444396f4bSJosef Bacik 	}
104544396f4bSJosef Bacik 	return dentry;
104644396f4bSJosef Bacik }
104744396f4bSJosef Bacik 
104844396f4bSJosef Bacik /*
10491da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
10501da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
10511da177e4SLinus Torvalds  *  It _is_ time-critical.
10521da177e4SLinus Torvalds  */
10531da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
105431e6b01fSNick Piggin 			struct path *path, struct inode **inode)
10551da177e4SLinus Torvalds {
10564ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
105731e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
10585a18fff2SAl Viro 	int need_reval = 1;
10595a18fff2SAl Viro 	int status = 1;
10609875cf80SDavid Howells 	int err;
10619875cf80SDavid Howells 
10623cac260aSAl Viro 	/*
1063b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1064b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1065b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1066b04f784eSNick Piggin 	 */
106731e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
106831e6b01fSNick Piggin 		unsigned seq;
106931e6b01fSNick Piggin 		*inode = nd->inode;
107031e6b01fSNick Piggin 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
10715a18fff2SAl Viro 		if (!dentry)
10725a18fff2SAl Viro 			goto unlazy;
10735a18fff2SAl Viro 
107431e6b01fSNick Piggin 		/* Memory barrier in read_seqcount_begin of child is enough */
107531e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
107631e6b01fSNick Piggin 			return -ECHILD;
107731e6b01fSNick Piggin 		nd->seq = seq;
10785a18fff2SAl Viro 
107924643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
10805a18fff2SAl Viro 			status = d_revalidate(dentry, nd);
10815a18fff2SAl Viro 			if (unlikely(status <= 0)) {
10825a18fff2SAl Viro 				if (status != -ECHILD)
10835a18fff2SAl Viro 					need_reval = 0;
10845a18fff2SAl Viro 				goto unlazy;
10855a18fff2SAl Viro 			}
108624643087SAl Viro 		}
108744396f4bSJosef Bacik 		if (unlikely(d_need_lookup(dentry)))
108844396f4bSJosef Bacik 			goto unlazy;
108931e6b01fSNick Piggin 		path->mnt = mnt;
109031e6b01fSNick Piggin 		path->dentry = dentry;
1091d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1092d6e9bd25SAl Viro 			goto unlazy;
1093d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1094d6e9bd25SAl Viro 			goto unlazy;
10959875cf80SDavid Howells 		return 0;
10965a18fff2SAl Viro unlazy:
109719660af7SAl Viro 		if (unlazy_walk(nd, dentry))
10985a18fff2SAl Viro 			return -ECHILD;
10995a18fff2SAl Viro 	} else {
110031e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
110124643087SAl Viro 	}
11025a18fff2SAl Viro 
110344396f4bSJosef Bacik 	if (dentry && unlikely(d_need_lookup(dentry))) {
110444396f4bSJosef Bacik 		dput(dentry);
110544396f4bSJosef Bacik 		dentry = NULL;
110644396f4bSJosef Bacik 	}
11075a18fff2SAl Viro retry:
11085a18fff2SAl Viro 	if (unlikely(!dentry)) {
11095a18fff2SAl Viro 		struct inode *dir = parent->d_inode;
11105a18fff2SAl Viro 		BUG_ON(nd->inode != dir);
11115a18fff2SAl Viro 
11125a18fff2SAl Viro 		mutex_lock(&dir->i_mutex);
11135a18fff2SAl Viro 		dentry = d_lookup(parent, name);
11145a18fff2SAl Viro 		if (likely(!dentry)) {
11155a18fff2SAl Viro 			dentry = d_alloc_and_lookup(parent, name, nd);
11165a18fff2SAl Viro 			if (IS_ERR(dentry)) {
11175a18fff2SAl Viro 				mutex_unlock(&dir->i_mutex);
11185a18fff2SAl Viro 				return PTR_ERR(dentry);
11195a18fff2SAl Viro 			}
11205a18fff2SAl Viro 			/* known good */
11215a18fff2SAl Viro 			need_reval = 0;
11225a18fff2SAl Viro 			status = 1;
112344396f4bSJosef Bacik 		} else if (unlikely(d_need_lookup(dentry))) {
112444396f4bSJosef Bacik 			dentry = d_inode_lookup(parent, dentry, nd);
112544396f4bSJosef Bacik 			if (IS_ERR(dentry)) {
112644396f4bSJosef Bacik 				mutex_unlock(&dir->i_mutex);
112744396f4bSJosef Bacik 				return PTR_ERR(dentry);
112844396f4bSJosef Bacik 			}
112944396f4bSJosef Bacik 			/* known good */
113044396f4bSJosef Bacik 			need_reval = 0;
113144396f4bSJosef Bacik 			status = 1;
11325a18fff2SAl Viro 		}
11335a18fff2SAl Viro 		mutex_unlock(&dir->i_mutex);
11345a18fff2SAl Viro 	}
11355a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
11365a18fff2SAl Viro 		status = d_revalidate(dentry, nd);
11375a18fff2SAl Viro 	if (unlikely(status <= 0)) {
11385a18fff2SAl Viro 		if (status < 0) {
11395a18fff2SAl Viro 			dput(dentry);
11405a18fff2SAl Viro 			return status;
11415a18fff2SAl Viro 		}
11425a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
11435a18fff2SAl Viro 			dput(dentry);
11445a18fff2SAl Viro 			dentry = NULL;
11455a18fff2SAl Viro 			need_reval = 1;
11465a18fff2SAl Viro 			goto retry;
11475a18fff2SAl Viro 		}
11485a18fff2SAl Viro 	}
11495a18fff2SAl Viro 
11501da177e4SLinus Torvalds 	path->mnt = mnt;
11511da177e4SLinus Torvalds 	path->dentry = dentry;
11529875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
115389312214SIan Kent 	if (unlikely(err < 0)) {
115489312214SIan Kent 		path_put_conditional(path, nd);
11559875cf80SDavid Howells 		return err;
115689312214SIan Kent 	}
115731e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
11581da177e4SLinus Torvalds 	return 0;
11591da177e4SLinus Torvalds }
11601da177e4SLinus Torvalds 
116152094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
116252094c8aSAl Viro {
116352094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
11644ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
116552094c8aSAl Viro 		if (err != -ECHILD)
116652094c8aSAl Viro 			return err;
116719660af7SAl Viro 		if (unlazy_walk(nd, NULL))
116852094c8aSAl Viro 			return -ECHILD;
116952094c8aSAl Viro 	}
11704ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
117152094c8aSAl Viro }
117252094c8aSAl Viro 
11739856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
11749856fa1bSAl Viro {
11759856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
11769856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
11779856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
11789856fa1bSAl Viro 				return -ECHILD;
11799856fa1bSAl Viro 		} else
11809856fa1bSAl Viro 			follow_dotdot(nd);
11819856fa1bSAl Viro 	}
11829856fa1bSAl Viro 	return 0;
11839856fa1bSAl Viro }
11849856fa1bSAl Viro 
1185951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1186951361f9SAl Viro {
1187951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1188951361f9SAl Viro 		path_put(&nd->path);
1189951361f9SAl Viro 	} else {
1190951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
11915b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1192951361f9SAl Viro 			nd->root.mnt = NULL;
1193951361f9SAl Viro 		rcu_read_unlock();
1194951361f9SAl Viro 		br_read_unlock(vfsmount_lock);
1195951361f9SAl Viro 	}
1196951361f9SAl Viro }
1197951361f9SAl Viro 
1198ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
1199ce57dfc1SAl Viro 		struct qstr *name, int type, int follow)
1200ce57dfc1SAl Viro {
1201ce57dfc1SAl Viro 	struct inode *inode;
1202ce57dfc1SAl Viro 	int err;
1203ce57dfc1SAl Viro 	/*
1204ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1205ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1206ce57dfc1SAl Viro 	 * parent relationships.
1207ce57dfc1SAl Viro 	 */
1208ce57dfc1SAl Viro 	if (unlikely(type != LAST_NORM))
1209ce57dfc1SAl Viro 		return handle_dots(nd, type);
1210ce57dfc1SAl Viro 	err = do_lookup(nd, name, path, &inode);
1211ce57dfc1SAl Viro 	if (unlikely(err)) {
1212ce57dfc1SAl Viro 		terminate_walk(nd);
1213ce57dfc1SAl Viro 		return err;
1214ce57dfc1SAl Viro 	}
1215ce57dfc1SAl Viro 	if (!inode) {
1216ce57dfc1SAl Viro 		path_to_nameidata(path, nd);
1217ce57dfc1SAl Viro 		terminate_walk(nd);
1218ce57dfc1SAl Viro 		return -ENOENT;
1219ce57dfc1SAl Viro 	}
1220ce57dfc1SAl Viro 	if (unlikely(inode->i_op->follow_link) && follow) {
122119660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
122219660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
122319660af7SAl Viro 				terminate_walk(nd);
1224ce57dfc1SAl Viro 				return -ECHILD;
122519660af7SAl Viro 			}
122619660af7SAl Viro 		}
1227ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1228ce57dfc1SAl Viro 		return 1;
1229ce57dfc1SAl Viro 	}
1230ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1231ce57dfc1SAl Viro 	nd->inode = inode;
1232ce57dfc1SAl Viro 	return 0;
1233ce57dfc1SAl Viro }
1234ce57dfc1SAl Viro 
12351da177e4SLinus Torvalds /*
1236b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1237b356379aSAl Viro  * limiting consecutive symlinks to 40.
1238b356379aSAl Viro  *
1239b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1240b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1241b356379aSAl Viro  */
1242b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1243b356379aSAl Viro {
1244b356379aSAl Viro 	int res;
1245b356379aSAl Viro 
1246b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1247b356379aSAl Viro 		path_put_conditional(path, nd);
1248b356379aSAl Viro 		path_put(&nd->path);
1249b356379aSAl Viro 		return -ELOOP;
1250b356379aSAl Viro 	}
12511a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1252b356379aSAl Viro 
1253b356379aSAl Viro 	nd->depth++;
1254b356379aSAl Viro 	current->link_count++;
1255b356379aSAl Viro 
1256b356379aSAl Viro 	do {
1257b356379aSAl Viro 		struct path link = *path;
1258b356379aSAl Viro 		void *cookie;
1259574197e0SAl Viro 
1260574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
1261b356379aSAl Viro 		if (!res)
1262b356379aSAl Viro 			res = walk_component(nd, path, &nd->last,
1263b356379aSAl Viro 					     nd->last_type, LOOKUP_FOLLOW);
1264574197e0SAl Viro 		put_link(nd, &link, cookie);
1265b356379aSAl Viro 	} while (res > 0);
1266b356379aSAl Viro 
1267b356379aSAl Viro 	current->link_count--;
1268b356379aSAl Viro 	nd->depth--;
1269b356379aSAl Viro 	return res;
1270b356379aSAl Viro }
1271b356379aSAl Viro 
1272b356379aSAl Viro /*
12731da177e4SLinus Torvalds  * Name resolution.
1274ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1275ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
12761da177e4SLinus Torvalds  *
1277ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1278ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
12791da177e4SLinus Torvalds  */
12806de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
12811da177e4SLinus Torvalds {
12821da177e4SLinus Torvalds 	struct path next;
12831da177e4SLinus Torvalds 	int err;
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 	while (*name=='/')
12861da177e4SLinus Torvalds 		name++;
12871da177e4SLinus Torvalds 	if (!*name)
1288086e183aSAl Viro 		return 0;
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
12911da177e4SLinus Torvalds 	for(;;) {
12921da177e4SLinus Torvalds 		unsigned long hash;
12931da177e4SLinus Torvalds 		struct qstr this;
12941da177e4SLinus Torvalds 		unsigned int c;
1295fe479a58SAl Viro 		int type;
12961da177e4SLinus Torvalds 
129752094c8aSAl Viro 		err = may_lookup(nd);
12981da177e4SLinus Torvalds  		if (err)
12991da177e4SLinus Torvalds 			break;
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 		this.name = name;
13021da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 		hash = init_name_hash();
13051da177e4SLinus Torvalds 		do {
13061da177e4SLinus Torvalds 			name++;
13071da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
13081da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
13091da177e4SLinus Torvalds 		} while (c && (c != '/'));
13101da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
13111da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
13121da177e4SLinus Torvalds 
1313fe479a58SAl Viro 		type = LAST_NORM;
1314fe479a58SAl Viro 		if (this.name[0] == '.') switch (this.len) {
1315fe479a58SAl Viro 			case 2:
131616c2cd71SAl Viro 				if (this.name[1] == '.') {
1317fe479a58SAl Viro 					type = LAST_DOTDOT;
131816c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
131916c2cd71SAl Viro 				}
1320fe479a58SAl Viro 				break;
1321fe479a58SAl Viro 			case 1:
1322fe479a58SAl Viro 				type = LAST_DOT;
1323fe479a58SAl Viro 		}
13245a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
13255a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
132616c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
13275a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
13285a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
13295a202bcdSAl Viro 							   &this);
13305a202bcdSAl Viro 				if (err < 0)
13315a202bcdSAl Viro 					break;
13325a202bcdSAl Viro 			}
13335a202bcdSAl Viro 		}
1334fe479a58SAl Viro 
13351da177e4SLinus Torvalds 		/* remove trailing slashes? */
13361da177e4SLinus Torvalds 		if (!c)
13371da177e4SLinus Torvalds 			goto last_component;
13381da177e4SLinus Torvalds 		while (*++name == '/');
13391da177e4SLinus Torvalds 		if (!*name)
1340b356379aSAl Viro 			goto last_component;
13411da177e4SLinus Torvalds 
1342ce57dfc1SAl Viro 		err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1343ce57dfc1SAl Viro 		if (err < 0)
1344ce57dfc1SAl Viro 			return err;
1345fe479a58SAl Viro 
1346ce57dfc1SAl Viro 		if (err) {
1347b356379aSAl Viro 			err = nested_symlink(&next, nd);
13481da177e4SLinus Torvalds 			if (err)
1349a7472babSAl Viro 				return err;
135031e6b01fSNick Piggin 		}
13511da177e4SLinus Torvalds 		err = -ENOTDIR;
135231e6b01fSNick Piggin 		if (!nd->inode->i_op->lookup)
13531da177e4SLinus Torvalds 			break;
13541da177e4SLinus Torvalds 		continue;
13551da177e4SLinus Torvalds 		/* here ends the main loop */
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds last_component:
1358ce57dfc1SAl Viro 		nd->last = this;
1359ce57dfc1SAl Viro 		nd->last_type = type;
1360ce57dfc1SAl Viro 		return 0;
1361ce57dfc1SAl Viro 	}
1362951361f9SAl Viro 	terminate_walk(nd);
13631da177e4SLinus Torvalds 	return err;
13641da177e4SLinus Torvalds }
13651da177e4SLinus Torvalds 
136670e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
136770e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
136831e6b01fSNick Piggin {
136931e6b01fSNick Piggin 	int retval = 0;
137031e6b01fSNick Piggin 	int fput_needed;
137131e6b01fSNick Piggin 	struct file *file;
137231e6b01fSNick Piggin 
137331e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
137416c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
137531e6b01fSNick Piggin 	nd->depth = 0;
13765b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
13775b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
137873d049a4SAl Viro 		if (*name) {
13795b6ca027SAl Viro 			if (!inode->i_op->lookup)
13805b6ca027SAl Viro 				return -ENOTDIR;
13815b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
13825b6ca027SAl Viro 			if (retval)
13835b6ca027SAl Viro 				return retval;
138473d049a4SAl Viro 		}
13855b6ca027SAl Viro 		nd->path = nd->root;
13865b6ca027SAl Viro 		nd->inode = inode;
13875b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
13885b6ca027SAl Viro 			br_read_lock(vfsmount_lock);
13895b6ca027SAl Viro 			rcu_read_lock();
13905b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
13915b6ca027SAl Viro 		} else {
13925b6ca027SAl Viro 			path_get(&nd->path);
13935b6ca027SAl Viro 		}
13945b6ca027SAl Viro 		return 0;
13955b6ca027SAl Viro 	}
13965b6ca027SAl Viro 
139731e6b01fSNick Piggin 	nd->root.mnt = NULL;
139831e6b01fSNick Piggin 
139931e6b01fSNick Piggin 	if (*name=='/') {
1400e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
140131e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
140231e6b01fSNick Piggin 			rcu_read_lock();
1403e41f7d4eSAl Viro 			set_root_rcu(nd);
1404e41f7d4eSAl Viro 		} else {
1405e41f7d4eSAl Viro 			set_root(nd);
1406e41f7d4eSAl Viro 			path_get(&nd->root);
1407e41f7d4eSAl Viro 		}
140831e6b01fSNick Piggin 		nd->path = nd->root;
140931e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1410e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
141131e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1412c28cc364SNick Piggin 			unsigned seq;
141331e6b01fSNick Piggin 
141431e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
141531e6b01fSNick Piggin 			rcu_read_lock();
141631e6b01fSNick Piggin 
1417c28cc364SNick Piggin 			do {
1418c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
141931e6b01fSNick Piggin 				nd->path = fs->pwd;
1420c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1421c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1422e41f7d4eSAl Viro 		} else {
1423e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1424e41f7d4eSAl Viro 		}
142531e6b01fSNick Piggin 	} else {
142631e6b01fSNick Piggin 		struct dentry *dentry;
142731e6b01fSNick Piggin 
14281abf0c71SAl Viro 		file = fget_raw_light(dfd, &fput_needed);
142931e6b01fSNick Piggin 		retval = -EBADF;
143031e6b01fSNick Piggin 		if (!file)
143131e6b01fSNick Piggin 			goto out_fail;
143231e6b01fSNick Piggin 
143331e6b01fSNick Piggin 		dentry = file->f_path.dentry;
143431e6b01fSNick Piggin 
1435f52e0c11SAl Viro 		if (*name) {
143631e6b01fSNick Piggin 			retval = -ENOTDIR;
143731e6b01fSNick Piggin 			if (!S_ISDIR(dentry->d_inode->i_mode))
143831e6b01fSNick Piggin 				goto fput_fail;
143931e6b01fSNick Piggin 
14404ad5abb3SAl Viro 			retval = inode_permission(dentry->d_inode, MAY_EXEC);
144131e6b01fSNick Piggin 			if (retval)
144231e6b01fSNick Piggin 				goto fput_fail;
1443f52e0c11SAl Viro 		}
144431e6b01fSNick Piggin 
144531e6b01fSNick Piggin 		nd->path = file->f_path;
1446e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
144731e6b01fSNick Piggin 			if (fput_needed)
144870e9b357SAl Viro 				*fp = file;
1449c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
145031e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
145131e6b01fSNick Piggin 			rcu_read_lock();
14525590ff0dSUlrich Drepper 		} else {
14535dd784d0SJan Blunck 			path_get(&file->f_path);
14545590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
14551da177e4SLinus Torvalds 		}
1456e41f7d4eSAl Viro 	}
1457e41f7d4eSAl Viro 
145831e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
14599b4a9b14SAl Viro 	return 0;
14602dfdd266SJosef 'Jeff' Sipek 
14619b4a9b14SAl Viro fput_fail:
14629b4a9b14SAl Viro 	fput_light(file, fput_needed);
14639b4a9b14SAl Viro out_fail:
14649b4a9b14SAl Viro 	return retval;
14659b4a9b14SAl Viro }
14669b4a9b14SAl Viro 
1467bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1468bd92d7feSAl Viro {
1469bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1470bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1471bd92d7feSAl Viro 
1472bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1473bd92d7feSAl Viro 	return walk_component(nd, path, &nd->last, nd->last_type,
1474bd92d7feSAl Viro 					nd->flags & LOOKUP_FOLLOW);
1475bd92d7feSAl Viro }
1476bd92d7feSAl Viro 
14779b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1478ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
14799b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
14809b4a9b14SAl Viro {
148170e9b357SAl Viro 	struct file *base = NULL;
1482bd92d7feSAl Viro 	struct path path;
1483bd92d7feSAl Viro 	int err;
148431e6b01fSNick Piggin 
148531e6b01fSNick Piggin 	/*
148631e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
148731e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
148831e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
148931e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
149031e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
149131e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
149231e6b01fSNick Piggin 	 * analogue, foo_rcu().
149331e6b01fSNick Piggin 	 *
149431e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
149531e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
149631e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
149731e6b01fSNick Piggin 	 * be able to complete).
149831e6b01fSNick Piggin 	 */
1499bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1500ee0827cdSAl Viro 
1501bd92d7feSAl Viro 	if (unlikely(err))
1502bd92d7feSAl Viro 		return err;
1503ee0827cdSAl Viro 
1504ee0827cdSAl Viro 	current->total_link_count = 0;
1505bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1506bd92d7feSAl Viro 
1507bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1508bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1509bd92d7feSAl Viro 		while (err > 0) {
1510bd92d7feSAl Viro 			void *cookie;
1511bd92d7feSAl Viro 			struct path link = path;
1512bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1513574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
1514bd92d7feSAl Viro 			if (!err)
1515bd92d7feSAl Viro 				err = lookup_last(nd, &path);
1516574197e0SAl Viro 			put_link(nd, &link, cookie);
1517bd92d7feSAl Viro 		}
1518bd92d7feSAl Viro 	}
1519ee0827cdSAl Viro 
15209f1fafeeSAl Viro 	if (!err)
15219f1fafeeSAl Viro 		err = complete_walk(nd);
1522bd92d7feSAl Viro 
1523bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1524bd92d7feSAl Viro 		if (!nd->inode->i_op->lookup) {
1525bd92d7feSAl Viro 			path_put(&nd->path);
1526bd23a539SAl Viro 			err = -ENOTDIR;
1527bd92d7feSAl Viro 		}
1528bd92d7feSAl Viro 	}
152916c2cd71SAl Viro 
153070e9b357SAl Viro 	if (base)
153170e9b357SAl Viro 		fput(base);
1532ee0827cdSAl Viro 
15335b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
153431e6b01fSNick Piggin 		path_put(&nd->root);
153531e6b01fSNick Piggin 		nd->root.mnt = NULL;
153631e6b01fSNick Piggin 	}
1537bd92d7feSAl Viro 	return err;
153831e6b01fSNick Piggin }
153931e6b01fSNick Piggin 
1540ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1541ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1542ee0827cdSAl Viro {
1543ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1544ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1545ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1546ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1547ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1548ee0827cdSAl Viro 
154931e6b01fSNick Piggin 	if (likely(!retval)) {
155031e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
155131e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
155231e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
155331e6b01fSNick Piggin 		}
155431e6b01fSNick Piggin 	}
1555170aa3d0SUlrich Drepper 	return retval;
15561da177e4SLinus Torvalds }
15571da177e4SLinus Torvalds 
1558c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd)
15595590ff0dSUlrich Drepper {
1560c9c6cac0SAl Viro 	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
15615590ff0dSUlrich Drepper }
15625590ff0dSUlrich Drepper 
1563d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1564d1811465SAl Viro {
1565d1811465SAl Viro 	struct nameidata nd;
1566d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1567d1811465SAl Viro 	if (!res)
1568d1811465SAl Viro 		*path = nd.path;
1569d1811465SAl Viro 	return res;
1570d1811465SAl Viro }
1571d1811465SAl Viro 
157216f18200SJosef 'Jeff' Sipek /**
157316f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
157416f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
157516f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
157616f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
157716f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
157816f18200SJosef 'Jeff' Sipek  * @nd: pointer to nameidata
157916f18200SJosef 'Jeff' Sipek  */
158016f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
158116f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
158216f18200SJosef 'Jeff' Sipek 		    struct nameidata *nd)
158316f18200SJosef 'Jeff' Sipek {
15845b6ca027SAl Viro 	nd->root.dentry = dentry;
15855b6ca027SAl Viro 	nd->root.mnt = mnt;
15865b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
15875b6ca027SAl Viro 	return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
158816f18200SJosef 'Jeff' Sipek }
158916f18200SJosef 'Jeff' Sipek 
1590eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1591eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
15921da177e4SLinus Torvalds {
159381fca444SChristoph Hellwig 	struct inode *inode = base->d_inode;
15941da177e4SLinus Torvalds 	struct dentry *dentry;
15951da177e4SLinus Torvalds 	int err;
15961da177e4SLinus Torvalds 
15974ad5abb3SAl Viro 	err = inode_permission(inode, MAY_EXEC);
159881fca444SChristoph Hellwig 	if (err)
159981fca444SChristoph Hellwig 		return ERR_PTR(err);
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 	/*
1602b04f784eSNick Piggin 	 * Don't bother with __d_lookup: callers are for creat as
1603b04f784eSNick Piggin 	 * well as unlink, so a lot of the time it would cost
1604b04f784eSNick Piggin 	 * a double lookup.
16056e6b1bd1SAl Viro 	 */
16066e6b1bd1SAl Viro 	dentry = d_lookup(base, name);
16076e6b1bd1SAl Viro 
160844396f4bSJosef Bacik 	if (dentry && d_need_lookup(dentry)) {
160944396f4bSJosef Bacik 		/*
161044396f4bSJosef Bacik 		 * __lookup_hash is called with the parent dir's i_mutex already
161144396f4bSJosef Bacik 		 * held, so we are good to go here.
161244396f4bSJosef Bacik 		 */
161344396f4bSJosef Bacik 		dentry = d_inode_lookup(base, dentry, nd);
161444396f4bSJosef Bacik 		if (IS_ERR(dentry))
161544396f4bSJosef Bacik 			return dentry;
161644396f4bSJosef Bacik 	}
161744396f4bSJosef Bacik 
1618d2d9e9fbSAl Viro 	if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1619d2d9e9fbSAl Viro 		int status = d_revalidate(dentry, nd);
1620d2d9e9fbSAl Viro 		if (unlikely(status <= 0)) {
1621d2d9e9fbSAl Viro 			/*
1622d2d9e9fbSAl Viro 			 * The dentry failed validation.
1623d2d9e9fbSAl Viro 			 * If d_revalidate returned 0 attempt to invalidate
1624d2d9e9fbSAl Viro 			 * the dentry otherwise d_revalidate is asking us
1625d2d9e9fbSAl Viro 			 * to return a fail status.
1626d2d9e9fbSAl Viro 			 */
1627d2d9e9fbSAl Viro 			if (status < 0) {
1628d2d9e9fbSAl Viro 				dput(dentry);
1629d2d9e9fbSAl Viro 				return ERR_PTR(status);
1630d2d9e9fbSAl Viro 			} else if (!d_invalidate(dentry)) {
1631d2d9e9fbSAl Viro 				dput(dentry);
1632d2d9e9fbSAl Viro 				dentry = NULL;
1633d2d9e9fbSAl Viro 			}
1634d2d9e9fbSAl Viro 		}
1635d2d9e9fbSAl Viro 	}
16366e6b1bd1SAl Viro 
16371da177e4SLinus Torvalds 	if (!dentry)
1638baa03890SNick Piggin 		dentry = d_alloc_and_lookup(base, name, nd);
16395a202bcdSAl Viro 
16401da177e4SLinus Torvalds 	return dentry;
16411da177e4SLinus Torvalds }
16421da177e4SLinus Torvalds 
1643057f6c01SJames Morris /*
1644057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1645057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1646057f6c01SJames Morris  * SMP-safe.
1647057f6c01SJames Morris  */
1648a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
16491da177e4SLinus Torvalds {
16504ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
16511da177e4SLinus Torvalds }
16521da177e4SLinus Torvalds 
1653eead1911SChristoph Hellwig /**
1654a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1655eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1656eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1657eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1658eead1911SChristoph Hellwig  *
1659a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1660a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1661eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1662eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1663eead1911SChristoph Hellwig  */
1664057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1665057f6c01SJames Morris {
1666057f6c01SJames Morris 	struct qstr this;
16676a96ba54SAl Viro 	unsigned long hash;
16686a96ba54SAl Viro 	unsigned int c;
1669057f6c01SJames Morris 
16702f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
16712f9092e1SDavid Woodhouse 
16726a96ba54SAl Viro 	this.name = name;
16736a96ba54SAl Viro 	this.len = len;
16746a96ba54SAl Viro 	if (!len)
16756a96ba54SAl Viro 		return ERR_PTR(-EACCES);
16766a96ba54SAl Viro 
16776a96ba54SAl Viro 	hash = init_name_hash();
16786a96ba54SAl Viro 	while (len--) {
16796a96ba54SAl Viro 		c = *(const unsigned char *)name++;
16806a96ba54SAl Viro 		if (c == '/' || c == '\0')
16816a96ba54SAl Viro 			return ERR_PTR(-EACCES);
16826a96ba54SAl Viro 		hash = partial_name_hash(c, hash);
16836a96ba54SAl Viro 	}
16846a96ba54SAl Viro 	this.hash = end_name_hash(hash);
16855a202bcdSAl Viro 	/*
16865a202bcdSAl Viro 	 * See if the low-level filesystem might want
16875a202bcdSAl Viro 	 * to use its own hash..
16885a202bcdSAl Viro 	 */
16895a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
16905a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
16915a202bcdSAl Viro 		if (err < 0)
16925a202bcdSAl Viro 			return ERR_PTR(err);
16935a202bcdSAl Viro 	}
1694eead1911SChristoph Hellwig 
169549705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1696057f6c01SJames Morris }
1697057f6c01SJames Morris 
16982d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
16992d8f3038SAl Viro 		 struct path *path)
17001da177e4SLinus Torvalds {
17012d8f3038SAl Viro 	struct nameidata nd;
1702f52e0c11SAl Viro 	char *tmp = getname_flags(name, flags);
17031da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
17041da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
17052d8f3038SAl Viro 
17062d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
17072d8f3038SAl Viro 
17082d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
17091da177e4SLinus Torvalds 		putname(tmp);
17102d8f3038SAl Viro 		if (!err)
17112d8f3038SAl Viro 			*path = nd.path;
17121da177e4SLinus Torvalds 	}
17131da177e4SLinus Torvalds 	return err;
17141da177e4SLinus Torvalds }
17151da177e4SLinus Torvalds 
17162ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
17172ad94ae6SAl Viro 			struct nameidata *nd, char **name)
17182ad94ae6SAl Viro {
17192ad94ae6SAl Viro 	char *s = getname(path);
17202ad94ae6SAl Viro 	int error;
17212ad94ae6SAl Viro 
17222ad94ae6SAl Viro 	if (IS_ERR(s))
17232ad94ae6SAl Viro 		return PTR_ERR(s);
17242ad94ae6SAl Viro 
17252ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
17262ad94ae6SAl Viro 	if (error)
17272ad94ae6SAl Viro 		putname(s);
17282ad94ae6SAl Viro 	else
17292ad94ae6SAl Viro 		*name = s;
17302ad94ae6SAl Viro 
17312ad94ae6SAl Viro 	return error;
17322ad94ae6SAl Viro }
17332ad94ae6SAl Viro 
17341da177e4SLinus Torvalds /*
17351da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
17361da177e4SLinus Torvalds  * minimal.
17371da177e4SLinus Torvalds  */
17381da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
17391da177e4SLinus Torvalds {
1740da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1741da9592edSDavid Howells 
17421da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
17431da177e4SLinus Torvalds 		return 0;
1744e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
1745e795b717SSerge E. Hallyn 		goto other_userns;
1746da9592edSDavid Howells 	if (inode->i_uid == fsuid)
17471da177e4SLinus Torvalds 		return 0;
1748da9592edSDavid Howells 	if (dir->i_uid == fsuid)
17491da177e4SLinus Torvalds 		return 0;
1750e795b717SSerge E. Hallyn 
1751e795b717SSerge E. Hallyn other_userns:
1752e795b717SSerge E. Hallyn 	return !ns_capable(inode_userns(inode), CAP_FOWNER);
17531da177e4SLinus Torvalds }
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds /*
17561da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
17571da177e4SLinus Torvalds  *  whether the type of victim is right.
17581da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
17591da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
17601da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
17611da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
17621da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
17631da177e4SLinus Torvalds  *	a. be owner of dir, or
17641da177e4SLinus Torvalds  *	b. be owner of victim, or
17651da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
17661da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
17671da177e4SLinus Torvalds  *     links pointing to it.
17681da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
17691da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
17701da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
17711da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
17721da177e4SLinus Torvalds  *     nfs_async_unlink().
17731da177e4SLinus Torvalds  */
1774858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
17751da177e4SLinus Torvalds {
17761da177e4SLinus Torvalds 	int error;
17771da177e4SLinus Torvalds 
17781da177e4SLinus Torvalds 	if (!victim->d_inode)
17791da177e4SLinus Torvalds 		return -ENOENT;
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1782cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
17831da177e4SLinus Torvalds 
1784f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
17851da177e4SLinus Torvalds 	if (error)
17861da177e4SLinus Torvalds 		return error;
17871da177e4SLinus Torvalds 	if (IS_APPEND(dir))
17881da177e4SLinus Torvalds 		return -EPERM;
17891da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1790f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
17911da177e4SLinus Torvalds 		return -EPERM;
17921da177e4SLinus Torvalds 	if (isdir) {
17931da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
17941da177e4SLinus Torvalds 			return -ENOTDIR;
17951da177e4SLinus Torvalds 		if (IS_ROOT(victim))
17961da177e4SLinus Torvalds 			return -EBUSY;
17971da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
17981da177e4SLinus Torvalds 		return -EISDIR;
17991da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18001da177e4SLinus Torvalds 		return -ENOENT;
18011da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
18021da177e4SLinus Torvalds 		return -EBUSY;
18031da177e4SLinus Torvalds 	return 0;
18041da177e4SLinus Torvalds }
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
18071da177e4SLinus Torvalds  *  dir.
18081da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
18091da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
18101da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
18111da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
18121da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
18131da177e4SLinus Torvalds  */
1814a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
18151da177e4SLinus Torvalds {
18161da177e4SLinus Torvalds 	if (child->d_inode)
18171da177e4SLinus Torvalds 		return -EEXIST;
18181da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18191da177e4SLinus Torvalds 		return -ENOENT;
1820f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
18211da177e4SLinus Torvalds }
18221da177e4SLinus Torvalds 
18231da177e4SLinus Torvalds /*
18241da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
18251da177e4SLinus Torvalds  */
18261da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
18271da177e4SLinus Torvalds {
18281da177e4SLinus Torvalds 	struct dentry *p;
18291da177e4SLinus Torvalds 
18301da177e4SLinus Torvalds 	if (p1 == p2) {
1831f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
18321da177e4SLinus Torvalds 		return NULL;
18331da177e4SLinus Torvalds 	}
18341da177e4SLinus Torvalds 
1835a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
18361da177e4SLinus Torvalds 
1837e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1838e2761a11SOGAWA Hirofumi 	if (p) {
1839f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1840f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
18411da177e4SLinus Torvalds 		return p;
18421da177e4SLinus Torvalds 	}
18431da177e4SLinus Torvalds 
1844e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1845e2761a11SOGAWA Hirofumi 	if (p) {
1846f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1847f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
18481da177e4SLinus Torvalds 		return p;
18491da177e4SLinus Torvalds 	}
18501da177e4SLinus Torvalds 
1851f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1852f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
18531da177e4SLinus Torvalds 	return NULL;
18541da177e4SLinus Torvalds }
18551da177e4SLinus Torvalds 
18561da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
18571da177e4SLinus Torvalds {
18581b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
18591da177e4SLinus Torvalds 	if (p1 != p2) {
18601b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1861a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
18621da177e4SLinus Torvalds 	}
18631da177e4SLinus Torvalds }
18641da177e4SLinus Torvalds 
18651da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
18661da177e4SLinus Torvalds 		struct nameidata *nd)
18671da177e4SLinus Torvalds {
1868a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
18691da177e4SLinus Torvalds 
18701da177e4SLinus Torvalds 	if (error)
18711da177e4SLinus Torvalds 		return error;
18721da177e4SLinus Torvalds 
1873acfa4380SAl Viro 	if (!dir->i_op->create)
18741da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
18751da177e4SLinus Torvalds 	mode &= S_IALLUGO;
18761da177e4SLinus Torvalds 	mode |= S_IFREG;
18771da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
18781da177e4SLinus Torvalds 	if (error)
18791da177e4SLinus Torvalds 		return error;
18801da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1881a74574aaSStephen Smalley 	if (!error)
1882f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
18831da177e4SLinus Torvalds 	return error;
18841da177e4SLinus Torvalds }
18851da177e4SLinus Torvalds 
188673d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
18871da177e4SLinus Torvalds {
18883fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
18891da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
18901da177e4SLinus Torvalds 	int error;
18911da177e4SLinus Torvalds 
1892bcda7652SAl Viro 	/* O_PATH? */
1893bcda7652SAl Viro 	if (!acc_mode)
1894bcda7652SAl Viro 		return 0;
1895bcda7652SAl Viro 
18961da177e4SLinus Torvalds 	if (!inode)
18971da177e4SLinus Torvalds 		return -ENOENT;
18981da177e4SLinus Torvalds 
1899c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1900c8fe8f30SChristoph Hellwig 	case S_IFLNK:
19011da177e4SLinus Torvalds 		return -ELOOP;
1902c8fe8f30SChristoph Hellwig 	case S_IFDIR:
1903c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
19041da177e4SLinus Torvalds 			return -EISDIR;
1905c8fe8f30SChristoph Hellwig 		break;
1906c8fe8f30SChristoph Hellwig 	case S_IFBLK:
1907c8fe8f30SChristoph Hellwig 	case S_IFCHR:
19083fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
19091da177e4SLinus Torvalds 			return -EACCES;
1910c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
1911c8fe8f30SChristoph Hellwig 	case S_IFIFO:
1912c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
19131da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
1914c8fe8f30SChristoph Hellwig 		break;
19154a3fd211SDave Hansen 	}
1916b41572e9SDave Hansen 
19173fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
1918b41572e9SDave Hansen 	if (error)
1919b41572e9SDave Hansen 		return error;
19206146f0d5SMimi Zohar 
19211da177e4SLinus Torvalds 	/*
19221da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
19231da177e4SLinus Torvalds 	 */
19241da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
19258737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
19267715b521SAl Viro 			return -EPERM;
19271da177e4SLinus Torvalds 		if (flag & O_TRUNC)
19287715b521SAl Viro 			return -EPERM;
19291da177e4SLinus Torvalds 	}
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
19322e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
19337715b521SAl Viro 		return -EPERM;
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds 	/*
19361da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
19371da177e4SLinus Torvalds 	 */
1938b65a9cfcSAl Viro 	return break_lease(inode, flag);
19397715b521SAl Viro }
19407715b521SAl Viro 
1941e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
19427715b521SAl Viro {
1943e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
19447715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
19457715b521SAl Viro 	int error = get_write_access(inode);
19461da177e4SLinus Torvalds 	if (error)
19477715b521SAl Viro 		return error;
19481da177e4SLinus Torvalds 	/*
19491da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
19501da177e4SLinus Torvalds 	 */
19511da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
1952be6d3e56SKentaro Takeda 	if (!error)
1953ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
19541da177e4SLinus Torvalds 	if (!error) {
19557715b521SAl Viro 		error = do_truncate(path->dentry, 0,
1956d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1957e1181ee6SJeff Layton 				    filp);
19581da177e4SLinus Torvalds 	}
19591da177e4SLinus Torvalds 	put_write_access(inode);
1960acd0c935SMimi Zohar 	return error;
19611da177e4SLinus Torvalds }
19621da177e4SLinus Torvalds 
1963d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
1964d57999e1SDave Hansen {
19658a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
19668a5e929dSAl Viro 		flag--;
1967d57999e1SDave Hansen 	return flag;
1968d57999e1SDave Hansen }
1969d57999e1SDave Hansen 
197031e6b01fSNick Piggin /*
1971fe2d35ffSAl Viro  * Handle the last step of open()
197231e6b01fSNick Piggin  */
1973fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
1974c3e380b0SAl Viro 			    const struct open_flags *op, const char *pathname)
1975fb1cc555SAl Viro {
1976a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
19776c0d46c4SAl Viro 	struct dentry *dentry;
1978ca344a89SAl Viro 	int open_flag = op->open_flag;
19796c0d46c4SAl Viro 	int will_truncate = open_flag & O_TRUNC;
1980ca344a89SAl Viro 	int want_write = 0;
1981bcda7652SAl Viro 	int acc_mode = op->acc_mode;
1982fb1cc555SAl Viro 	struct file *filp;
198316c2cd71SAl Viro 	int error;
1984fb1cc555SAl Viro 
1985c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1986c3e380b0SAl Viro 	nd->flags |= op->intent;
1987c3e380b0SAl Viro 
19881f36f774SAl Viro 	switch (nd->last_type) {
19891f36f774SAl Viro 	case LAST_DOTDOT:
1990176306f5SNeil Brown 	case LAST_DOT:
1991fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
1992fe2d35ffSAl Viro 		if (error)
1993fe2d35ffSAl Viro 			return ERR_PTR(error);
19941f36f774SAl Viro 		/* fallthrough */
19951f36f774SAl Viro 	case LAST_ROOT:
19969f1fafeeSAl Viro 		error = complete_walk(nd);
199716c2cd71SAl Viro 		if (error)
19989f1fafeeSAl Viro 			return ERR_PTR(error);
1999fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2000ca344a89SAl Viro 		if (open_flag & O_CREAT) {
200116c2cd71SAl Viro 			error = -EISDIR;
20021f36f774SAl Viro 			goto exit;
2003fe2d35ffSAl Viro 		}
2004fe2d35ffSAl Viro 		goto ok;
20051f36f774SAl Viro 	case LAST_BIND:
20069f1fafeeSAl Viro 		error = complete_walk(nd);
200716c2cd71SAl Viro 		if (error)
20089f1fafeeSAl Viro 			return ERR_PTR(error);
20091f36f774SAl Viro 		audit_inode(pathname, dir);
20101f36f774SAl Viro 		goto ok;
20111f36f774SAl Viro 	}
2012a2c36b45SAl Viro 
2013ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2014bcda7652SAl Viro 		int symlink_ok = 0;
2015fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2016fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2017bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2018bcda7652SAl Viro 			symlink_ok = 1;
2019fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2020ce57dfc1SAl Viro 		error = walk_component(nd, path, &nd->last, LAST_NORM,
2021ce57dfc1SAl Viro 					!symlink_ok);
2022ce57dfc1SAl Viro 		if (error < 0)
2023fe2d35ffSAl Viro 			return ERR_PTR(error);
2024ce57dfc1SAl Viro 		if (error) /* symlink */
2025fe2d35ffSAl Viro 			return NULL;
2026fe2d35ffSAl Viro 		/* sayonara */
20279f1fafeeSAl Viro 		error = complete_walk(nd);
20289f1fafeeSAl Viro 		if (error)
2029fe2d35ffSAl Viro 			return ERR_PTR(-ECHILD);
2030fe2d35ffSAl Viro 
2031fe2d35ffSAl Viro 		error = -ENOTDIR;
2032fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_DIRECTORY) {
2033ce57dfc1SAl Viro 			if (!nd->inode->i_op->lookup)
2034fe2d35ffSAl Viro 				goto exit;
2035fe2d35ffSAl Viro 		}
2036fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2037fe2d35ffSAl Viro 		goto ok;
2038fe2d35ffSAl Viro 	}
2039fe2d35ffSAl Viro 
2040fe2d35ffSAl Viro 	/* create side of things */
20419f1fafeeSAl Viro 	error = complete_walk(nd);
20429f1fafeeSAl Viro 	if (error)
20439f1fafeeSAl Viro 		return ERR_PTR(error);
2044fe2d35ffSAl Viro 
2045fe2d35ffSAl Viro 	audit_inode(pathname, dir);
204616c2cd71SAl Viro 	error = -EISDIR;
20471f36f774SAl Viro 	/* trailing slashes? */
204831e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
20491f36f774SAl Viro 		goto exit;
20501f36f774SAl Viro 
2051a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2052a1e28038SAl Viro 
20536c0d46c4SAl Viro 	dentry = lookup_hash(nd);
20546c0d46c4SAl Viro 	error = PTR_ERR(dentry);
20556c0d46c4SAl Viro 	if (IS_ERR(dentry)) {
2056fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2057fb1cc555SAl Viro 		goto exit;
2058fb1cc555SAl Viro 	}
2059fb1cc555SAl Viro 
20606c0d46c4SAl Viro 	path->dentry = dentry;
20616c0d46c4SAl Viro 	path->mnt = nd->path.mnt;
20626c0d46c4SAl Viro 
2063fb1cc555SAl Viro 	/* Negative dentry, just create the file */
20646c0d46c4SAl Viro 	if (!dentry->d_inode) {
20656c0d46c4SAl Viro 		int mode = op->mode;
20666c0d46c4SAl Viro 		if (!IS_POSIXACL(dir->d_inode))
20676c0d46c4SAl Viro 			mode &= ~current_umask();
2068fb1cc555SAl Viro 		/*
2069fb1cc555SAl Viro 		 * This write is needed to ensure that a
20706c0d46c4SAl Viro 		 * rw->ro transition does not occur between
2071fb1cc555SAl Viro 		 * the time when the file is created and when
2072fb1cc555SAl Viro 		 * a permanent write count is taken through
2073fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2074fb1cc555SAl Viro 		 */
2075fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2076fb1cc555SAl Viro 		if (error)
2077fb1cc555SAl Viro 			goto exit_mutex_unlock;
2078ca344a89SAl Viro 		want_write = 1;
20799b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2080ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
20816c0d46c4SAl Viro 		will_truncate = 0;
2082bcda7652SAl Viro 		acc_mode = MAY_OPEN;
20836c0d46c4SAl Viro 		error = security_path_mknod(&nd->path, dentry, mode, 0);
20846c0d46c4SAl Viro 		if (error)
20856c0d46c4SAl Viro 			goto exit_mutex_unlock;
20866c0d46c4SAl Viro 		error = vfs_create(dir->d_inode, dentry, mode, nd);
20876c0d46c4SAl Viro 		if (error)
20886c0d46c4SAl Viro 			goto exit_mutex_unlock;
20896c0d46c4SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
20906c0d46c4SAl Viro 		dput(nd->path.dentry);
20916c0d46c4SAl Viro 		nd->path.dentry = dentry;
2092ca344a89SAl Viro 		goto common;
2093fb1cc555SAl Viro 	}
2094fb1cc555SAl Viro 
2095fb1cc555SAl Viro 	/*
2096fb1cc555SAl Viro 	 * It already exists.
2097fb1cc555SAl Viro 	 */
2098fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2099fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2100fb1cc555SAl Viro 
2101fb1cc555SAl Viro 	error = -EEXIST;
2102ca344a89SAl Viro 	if (open_flag & O_EXCL)
2103fb1cc555SAl Viro 		goto exit_dput;
2104fb1cc555SAl Viro 
21059875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
21069875cf80SDavid Howells 	if (error < 0)
2107fb1cc555SAl Viro 		goto exit_dput;
2108fb1cc555SAl Viro 
2109fb1cc555SAl Viro 	error = -ENOENT;
2110fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2111fb1cc555SAl Viro 		goto exit_dput;
21129e67f361SAl Viro 
21139e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2114fb1cc555SAl Viro 		return NULL;
2115fb1cc555SAl Viro 
2116fb1cc555SAl Viro 	path_to_nameidata(path, nd);
211731e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2118fb1cc555SAl Viro 	error = -EISDIR;
211931e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2120fb1cc555SAl Viro 		goto exit;
212167ee3ad2SAl Viro ok:
21226c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
21236c0d46c4SAl Viro 		will_truncate = 0;
21246c0d46c4SAl Viro 
21250f9d1a10SAl Viro 	if (will_truncate) {
21260f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
21270f9d1a10SAl Viro 		if (error)
21280f9d1a10SAl Viro 			goto exit;
2129ca344a89SAl Viro 		want_write = 1;
21300f9d1a10SAl Viro 	}
2131ca344a89SAl Viro common:
2132bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2133ca344a89SAl Viro 	if (error)
21340f9d1a10SAl Viro 		goto exit;
21350f9d1a10SAl Viro 	filp = nameidata_to_filp(nd);
21360f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
21370f9d1a10SAl Viro 		error = ima_file_check(filp, op->acc_mode);
21380f9d1a10SAl Viro 		if (error) {
21390f9d1a10SAl Viro 			fput(filp);
21400f9d1a10SAl Viro 			filp = ERR_PTR(error);
21410f9d1a10SAl Viro 		}
21420f9d1a10SAl Viro 	}
21430f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
21440f9d1a10SAl Viro 		if (will_truncate) {
21450f9d1a10SAl Viro 			error = handle_truncate(filp);
21460f9d1a10SAl Viro 			if (error) {
21470f9d1a10SAl Viro 				fput(filp);
21480f9d1a10SAl Viro 				filp = ERR_PTR(error);
21490f9d1a10SAl Viro 			}
21500f9d1a10SAl Viro 		}
21510f9d1a10SAl Viro 	}
2152ca344a89SAl Viro out:
2153ca344a89SAl Viro 	if (want_write)
21540f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
21550f9d1a10SAl Viro 	path_put(&nd->path);
2156fb1cc555SAl Viro 	return filp;
2157fb1cc555SAl Viro 
2158fb1cc555SAl Viro exit_mutex_unlock:
2159fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2160fb1cc555SAl Viro exit_dput:
2161fb1cc555SAl Viro 	path_put_conditional(path, nd);
2162fb1cc555SAl Viro exit:
2163ca344a89SAl Viro 	filp = ERR_PTR(error);
2164ca344a89SAl Viro 	goto out;
2165fb1cc555SAl Viro }
2166fb1cc555SAl Viro 
216713aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
216873d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
21691da177e4SLinus Torvalds {
2170fe2d35ffSAl Viro 	struct file *base = NULL;
21714a3fd211SDave Hansen 	struct file *filp;
21729850c056SAl Viro 	struct path path;
217313aab428SAl Viro 	int error;
217431e6b01fSNick Piggin 
217531e6b01fSNick Piggin 	filp = get_empty_filp();
217631e6b01fSNick Piggin 	if (!filp)
217731e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
217831e6b01fSNick Piggin 
217947c805dcSAl Viro 	filp->f_flags = op->open_flag;
218073d049a4SAl Viro 	nd->intent.open.file = filp;
218173d049a4SAl Viro 	nd->intent.open.flags = open_to_namei_flags(op->open_flag);
218273d049a4SAl Viro 	nd->intent.open.create_mode = op->mode;
218331e6b01fSNick Piggin 
218473d049a4SAl Viro 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
218531e6b01fSNick Piggin 	if (unlikely(error))
218613aab428SAl Viro 		goto out_filp;
218731e6b01fSNick Piggin 
2188fe2d35ffSAl Viro 	current->total_link_count = 0;
218973d049a4SAl Viro 	error = link_path_walk(pathname, nd);
219031e6b01fSNick Piggin 	if (unlikely(error))
219131e6b01fSNick Piggin 		goto out_filp;
21921da177e4SLinus Torvalds 
219373d049a4SAl Viro 	filp = do_last(nd, &path, op, pathname);
2194806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
21957b9337aaSNick Piggin 		struct path link = path;
2196def4af30SAl Viro 		void *cookie;
2197574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
219873d049a4SAl Viro 			path_put_conditional(&path, nd);
219973d049a4SAl Viro 			path_put(&nd->path);
220040b39136SAl Viro 			filp = ERR_PTR(-ELOOP);
220140b39136SAl Viro 			break;
220240b39136SAl Viro 		}
220373d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
220473d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2205574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
2206c3e380b0SAl Viro 		if (unlikely(error))
2207f1afe9efSAl Viro 			filp = ERR_PTR(error);
2208c3e380b0SAl Viro 		else
220973d049a4SAl Viro 			filp = do_last(nd, &path, op, pathname);
2210574197e0SAl Viro 		put_link(nd, &link, cookie);
2211806b681cSAl Viro 	}
221210fa8e62SAl Viro out:
221373d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
221473d049a4SAl Viro 		path_put(&nd->root);
2215fe2d35ffSAl Viro 	if (base)
2216fe2d35ffSAl Viro 		fput(base);
221773d049a4SAl Viro 	release_open_intent(nd);
221810fa8e62SAl Viro 	return filp;
22191da177e4SLinus Torvalds 
222031e6b01fSNick Piggin out_filp:
222110fa8e62SAl Viro 	filp = ERR_PTR(error);
222210fa8e62SAl Viro 	goto out;
2223de459215SKirill Korotaev }
22241da177e4SLinus Torvalds 
222513aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
222613aab428SAl Viro 		const struct open_flags *op, int flags)
222713aab428SAl Viro {
222873d049a4SAl Viro 	struct nameidata nd;
222913aab428SAl Viro 	struct file *filp;
223013aab428SAl Viro 
223173d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
223213aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
223373d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
223413aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
223573d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
223613aab428SAl Viro 	return filp;
223713aab428SAl Viro }
223813aab428SAl Viro 
223973d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
224073d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
224173d049a4SAl Viro {
224273d049a4SAl Viro 	struct nameidata nd;
224373d049a4SAl Viro 	struct file *file;
224473d049a4SAl Viro 
224573d049a4SAl Viro 	nd.root.mnt = mnt;
224673d049a4SAl Viro 	nd.root.dentry = dentry;
224773d049a4SAl Viro 
224873d049a4SAl Viro 	flags |= LOOKUP_ROOT;
224973d049a4SAl Viro 
2250bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
225173d049a4SAl Viro 		return ERR_PTR(-ELOOP);
225273d049a4SAl Viro 
225373d049a4SAl Viro 	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
225473d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
225573d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags);
225673d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
225773d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
225873d049a4SAl Viro 	return file;
225973d049a4SAl Viro }
226073d049a4SAl Viro 
22611da177e4SLinus Torvalds /**
22621da177e4SLinus Torvalds  * lookup_create - lookup a dentry, creating it if it doesn't exist
22631da177e4SLinus Torvalds  * @nd: nameidata info
22641da177e4SLinus Torvalds  * @is_dir: directory flag
22651da177e4SLinus Torvalds  *
22661da177e4SLinus Torvalds  * Simple function to lookup and return a dentry and create it
22671da177e4SLinus Torvalds  * if it doesn't exist.  Is SMP-safe.
2268c663e5d8SChristoph Hellwig  *
22694ac91378SJan Blunck  * Returns with nd->path.dentry->d_inode->i_mutex locked.
22701da177e4SLinus Torvalds  */
22711da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir)
22721da177e4SLinus Torvalds {
2273c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
22741da177e4SLinus Torvalds 
22754ac91378SJan Blunck 	mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2276c663e5d8SChristoph Hellwig 	/*
2277c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2278c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2279c663e5d8SChristoph Hellwig 	 */
22801da177e4SLinus Torvalds 	if (nd->last_type != LAST_NORM)
22811da177e4SLinus Torvalds 		goto fail;
22821da177e4SLinus Torvalds 	nd->flags &= ~LOOKUP_PARENT;
22833516586aSAl Viro 	nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2284a634904aSASANO Masahiro 	nd->intent.open.flags = O_EXCL;
2285c663e5d8SChristoph Hellwig 
2286c663e5d8SChristoph Hellwig 	/*
2287c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2288c663e5d8SChristoph Hellwig 	 */
228949705b77SChristoph Hellwig 	dentry = lookup_hash(nd);
22901da177e4SLinus Torvalds 	if (IS_ERR(dentry))
22911da177e4SLinus Torvalds 		goto fail;
2292c663e5d8SChristoph Hellwig 
2293e9baf6e5SAl Viro 	if (dentry->d_inode)
2294e9baf6e5SAl Viro 		goto eexist;
2295c663e5d8SChristoph Hellwig 	/*
2296c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2297c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2298c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2299c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2300c663e5d8SChristoph Hellwig 	 */
2301e9baf6e5SAl Viro 	if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
23021da177e4SLinus Torvalds 		dput(dentry);
23031da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2304e9baf6e5SAl Viro 	}
2305e9baf6e5SAl Viro 	return dentry;
2306e9baf6e5SAl Viro eexist:
2307e9baf6e5SAl Viro 	dput(dentry);
2308e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
23091da177e4SLinus Torvalds fail:
23101da177e4SLinus Torvalds 	return dentry;
23111da177e4SLinus Torvalds }
2312f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create);
23131da177e4SLinus Torvalds 
2314dae6ad8fSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
2315dae6ad8fSAl Viro {
2316dae6ad8fSAl Viro 	struct nameidata nd;
2317dae6ad8fSAl Viro 	struct dentry *res;
2318dae6ad8fSAl Viro 	int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2319dae6ad8fSAl Viro 	if (error)
2320dae6ad8fSAl Viro 		return ERR_PTR(error);
2321dae6ad8fSAl Viro 	res = lookup_create(&nd, is_dir);
2322dae6ad8fSAl Viro 	if (IS_ERR(res)) {
2323dae6ad8fSAl Viro 		mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2324dae6ad8fSAl Viro 		path_put(&nd.path);
2325dae6ad8fSAl Viro 	}
2326dae6ad8fSAl Viro 	*path = nd.path;
2327dae6ad8fSAl Viro 	return res;
2328dae6ad8fSAl Viro }
2329dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
2330dae6ad8fSAl Viro 
2331dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2332dae6ad8fSAl Viro {
2333dae6ad8fSAl Viro 	char *tmp = getname(pathname);
2334dae6ad8fSAl Viro 	struct dentry *res;
2335dae6ad8fSAl Viro 	if (IS_ERR(tmp))
2336dae6ad8fSAl Viro 		return ERR_CAST(tmp);
2337dae6ad8fSAl Viro 	res = kern_path_create(dfd, tmp, path, is_dir);
2338dae6ad8fSAl Viro 	putname(tmp);
2339dae6ad8fSAl Viro 	return res;
2340dae6ad8fSAl Viro }
2341dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
2342dae6ad8fSAl Viro 
23431da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
23441da177e4SLinus Torvalds {
2345a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
23461da177e4SLinus Torvalds 
23471da177e4SLinus Torvalds 	if (error)
23481da177e4SLinus Torvalds 		return error;
23491da177e4SLinus Torvalds 
2350e795b717SSerge E. Hallyn 	if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2351e795b717SSerge E. Hallyn 	    !ns_capable(inode_userns(dir), CAP_MKNOD))
23521da177e4SLinus Torvalds 		return -EPERM;
23531da177e4SLinus Torvalds 
2354acfa4380SAl Viro 	if (!dir->i_op->mknod)
23551da177e4SLinus Torvalds 		return -EPERM;
23561da177e4SLinus Torvalds 
235708ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
235808ce5f16SSerge E. Hallyn 	if (error)
235908ce5f16SSerge E. Hallyn 		return error;
236008ce5f16SSerge E. Hallyn 
23611da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
23621da177e4SLinus Torvalds 	if (error)
23631da177e4SLinus Torvalds 		return error;
23641da177e4SLinus Torvalds 
23651da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2366a74574aaSStephen Smalley 	if (!error)
2367f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
23681da177e4SLinus Torvalds 	return error;
23691da177e4SLinus Torvalds }
23701da177e4SLinus Torvalds 
2371463c3197SDave Hansen static int may_mknod(mode_t mode)
2372463c3197SDave Hansen {
2373463c3197SDave Hansen 	switch (mode & S_IFMT) {
2374463c3197SDave Hansen 	case S_IFREG:
2375463c3197SDave Hansen 	case S_IFCHR:
2376463c3197SDave Hansen 	case S_IFBLK:
2377463c3197SDave Hansen 	case S_IFIFO:
2378463c3197SDave Hansen 	case S_IFSOCK:
2379463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2380463c3197SDave Hansen 		return 0;
2381463c3197SDave Hansen 	case S_IFDIR:
2382463c3197SDave Hansen 		return -EPERM;
2383463c3197SDave Hansen 	default:
2384463c3197SDave Hansen 		return -EINVAL;
2385463c3197SDave Hansen 	}
2386463c3197SDave Hansen }
2387463c3197SDave Hansen 
23882e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
23892e4d0924SHeiko Carstens 		unsigned, dev)
23901da177e4SLinus Torvalds {
23911da177e4SLinus Torvalds 	struct dentry *dentry;
2392dae6ad8fSAl Viro 	struct path path;
2393dae6ad8fSAl Viro 	int error;
23941da177e4SLinus Torvalds 
23951da177e4SLinus Torvalds 	if (S_ISDIR(mode))
23961da177e4SLinus Torvalds 		return -EPERM;
23971da177e4SLinus Torvalds 
2398dae6ad8fSAl Viro 	dentry = user_path_create(dfd, filename, &path, 0);
2399dae6ad8fSAl Viro 	if (IS_ERR(dentry))
2400dae6ad8fSAl Viro 		return PTR_ERR(dentry);
24012ad94ae6SAl Viro 
2402dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
2403ce3b0f8dSAl Viro 		mode &= ~current_umask();
2404463c3197SDave Hansen 	error = may_mknod(mode);
2405463c3197SDave Hansen 	if (error)
2406463c3197SDave Hansen 		goto out_dput;
2407dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
2408463c3197SDave Hansen 	if (error)
2409463c3197SDave Hansen 		goto out_dput;
2410dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
2411be6d3e56SKentaro Takeda 	if (error)
2412be6d3e56SKentaro Takeda 		goto out_drop_write;
24131da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
24141da177e4SLinus Torvalds 		case 0: case S_IFREG:
2415dae6ad8fSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
24161da177e4SLinus Torvalds 			break;
24171da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
2418dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
24191da177e4SLinus Torvalds 					new_decode_dev(dev));
24201da177e4SLinus Torvalds 			break;
24211da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
2422dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
24231da177e4SLinus Torvalds 			break;
24241da177e4SLinus Torvalds 	}
2425be6d3e56SKentaro Takeda out_drop_write:
2426dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
2427463c3197SDave Hansen out_dput:
24281da177e4SLinus Torvalds 	dput(dentry);
2429dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2430dae6ad8fSAl Viro 	path_put(&path);
24311da177e4SLinus Torvalds 
24321da177e4SLinus Torvalds 	return error;
24331da177e4SLinus Torvalds }
24341da177e4SLinus Torvalds 
24353480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
24365590ff0dSUlrich Drepper {
24375590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
24385590ff0dSUlrich Drepper }
24395590ff0dSUlrich Drepper 
24401da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
24411da177e4SLinus Torvalds {
2442a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
24431da177e4SLinus Torvalds 
24441da177e4SLinus Torvalds 	if (error)
24451da177e4SLinus Torvalds 		return error;
24461da177e4SLinus Torvalds 
2447acfa4380SAl Viro 	if (!dir->i_op->mkdir)
24481da177e4SLinus Torvalds 		return -EPERM;
24491da177e4SLinus Torvalds 
24501da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
24511da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
24521da177e4SLinus Torvalds 	if (error)
24531da177e4SLinus Torvalds 		return error;
24541da177e4SLinus Torvalds 
24551da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2456a74574aaSStephen Smalley 	if (!error)
2457f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
24581da177e4SLinus Torvalds 	return error;
24591da177e4SLinus Torvalds }
24601da177e4SLinus Torvalds 
24612e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
24621da177e4SLinus Torvalds {
24636902d925SDave Hansen 	struct dentry *dentry;
2464dae6ad8fSAl Viro 	struct path path;
2465dae6ad8fSAl Viro 	int error;
24661da177e4SLinus Torvalds 
2467dae6ad8fSAl Viro 	dentry = user_path_create(dfd, pathname, &path, 1);
24686902d925SDave Hansen 	if (IS_ERR(dentry))
2469dae6ad8fSAl Viro 		return PTR_ERR(dentry);
24706902d925SDave Hansen 
2471dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
2472ce3b0f8dSAl Viro 		mode &= ~current_umask();
2473dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
2474463c3197SDave Hansen 	if (error)
2475463c3197SDave Hansen 		goto out_dput;
2476dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
2477be6d3e56SKentaro Takeda 	if (error)
2478be6d3e56SKentaro Takeda 		goto out_drop_write;
2479dae6ad8fSAl Viro 	error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
2480be6d3e56SKentaro Takeda out_drop_write:
2481dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
2482463c3197SDave Hansen out_dput:
24831da177e4SLinus Torvalds 	dput(dentry);
2484dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2485dae6ad8fSAl Viro 	path_put(&path);
24861da177e4SLinus Torvalds 	return error;
24871da177e4SLinus Torvalds }
24881da177e4SLinus Torvalds 
24893cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
24905590ff0dSUlrich Drepper {
24915590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
24925590ff0dSUlrich Drepper }
24935590ff0dSUlrich Drepper 
24941da177e4SLinus Torvalds /*
2495a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
2496a71905f0SSage Weil  * should have a usage count of 2 if we're the only user of this
2497a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
2498a71905f0SSage Weil  * then we drop the dentry now.
24991da177e4SLinus Torvalds  *
25001da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
25011da177e4SLinus Torvalds  * do a
25021da177e4SLinus Torvalds  *
25031da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
25041da177e4SLinus Torvalds  *		return -EBUSY;
25051da177e4SLinus Torvalds  *
25061da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
25071da177e4SLinus Torvalds  * that is still in use by something else..
25081da177e4SLinus Torvalds  */
25091da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
25101da177e4SLinus Torvalds {
25111da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
25121da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
251364252c75SSage Weil 	if (dentry->d_count == 1)
25141da177e4SLinus Torvalds 		__d_drop(dentry);
25151da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
25161da177e4SLinus Torvalds }
25171da177e4SLinus Torvalds 
25181da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
25191da177e4SLinus Torvalds {
25201da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
25211da177e4SLinus Torvalds 
25221da177e4SLinus Torvalds 	if (error)
25231da177e4SLinus Torvalds 		return error;
25241da177e4SLinus Torvalds 
2525acfa4380SAl Viro 	if (!dir->i_op->rmdir)
25261da177e4SLinus Torvalds 		return -EPERM;
25271da177e4SLinus Torvalds 
25281b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
2529912dbc15SSage Weil 
25301da177e4SLinus Torvalds 	error = -EBUSY;
2531912dbc15SSage Weil 	if (d_mountpoint(dentry))
2532912dbc15SSage Weil 		goto out;
2533912dbc15SSage Weil 
25341da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
2535912dbc15SSage Weil 	if (error)
2536912dbc15SSage Weil 		goto out;
2537912dbc15SSage Weil 
25383cebde24SSage Weil 	shrink_dcache_parent(dentry);
25391da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
2540912dbc15SSage Weil 	if (error)
2541912dbc15SSage Weil 		goto out;
2542912dbc15SSage Weil 
25431da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
2544d83c49f3SAl Viro 	dont_mount(dentry);
25451da177e4SLinus Torvalds 
2546912dbc15SSage Weil out:
2547912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
2548912dbc15SSage Weil 	if (!error)
2549912dbc15SSage Weil 		d_delete(dentry);
25501da177e4SLinus Torvalds 	return error;
25511da177e4SLinus Torvalds }
25521da177e4SLinus Torvalds 
25535590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
25541da177e4SLinus Torvalds {
25551da177e4SLinus Torvalds 	int error = 0;
25561da177e4SLinus Torvalds 	char * name;
25571da177e4SLinus Torvalds 	struct dentry *dentry;
25581da177e4SLinus Torvalds 	struct nameidata nd;
25591da177e4SLinus Torvalds 
25602ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
25611da177e4SLinus Torvalds 	if (error)
25622ad94ae6SAl Viro 		return error;
25631da177e4SLinus Torvalds 
25641da177e4SLinus Torvalds 	switch(nd.last_type) {
25651da177e4SLinus Torvalds 	case LAST_DOTDOT:
25661da177e4SLinus Torvalds 		error = -ENOTEMPTY;
25671da177e4SLinus Torvalds 		goto exit1;
25681da177e4SLinus Torvalds 	case LAST_DOT:
25691da177e4SLinus Torvalds 		error = -EINVAL;
25701da177e4SLinus Torvalds 		goto exit1;
25711da177e4SLinus Torvalds 	case LAST_ROOT:
25721da177e4SLinus Torvalds 		error = -EBUSY;
25731da177e4SLinus Torvalds 		goto exit1;
25741da177e4SLinus Torvalds 	}
25750612d9fbSOGAWA Hirofumi 
25760612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
25770612d9fbSOGAWA Hirofumi 
25784ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
257949705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
25801da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
25816902d925SDave Hansen 	if (IS_ERR(dentry))
25826902d925SDave Hansen 		goto exit2;
2583e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
2584e6bc45d6STheodore Ts'o 		error = -ENOENT;
2585e6bc45d6STheodore Ts'o 		goto exit3;
2586e6bc45d6STheodore Ts'o 	}
25870622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
25880622753bSDave Hansen 	if (error)
25890622753bSDave Hansen 		goto exit3;
2590be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2591be6d3e56SKentaro Takeda 	if (error)
2592be6d3e56SKentaro Takeda 		goto exit4;
25934ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2594be6d3e56SKentaro Takeda exit4:
25950622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
25960622753bSDave Hansen exit3:
25971da177e4SLinus Torvalds 	dput(dentry);
25986902d925SDave Hansen exit2:
25994ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
26001da177e4SLinus Torvalds exit1:
26011d957f9bSJan Blunck 	path_put(&nd.path);
26021da177e4SLinus Torvalds 	putname(name);
26031da177e4SLinus Torvalds 	return error;
26041da177e4SLinus Torvalds }
26051da177e4SLinus Torvalds 
26063cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
26075590ff0dSUlrich Drepper {
26085590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
26095590ff0dSUlrich Drepper }
26105590ff0dSUlrich Drepper 
26111da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
26121da177e4SLinus Torvalds {
26131da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
26141da177e4SLinus Torvalds 
26151da177e4SLinus Torvalds 	if (error)
26161da177e4SLinus Torvalds 		return error;
26171da177e4SLinus Torvalds 
2618acfa4380SAl Viro 	if (!dir->i_op->unlink)
26191da177e4SLinus Torvalds 		return -EPERM;
26201da177e4SLinus Torvalds 
26211b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
26221da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
26231da177e4SLinus Torvalds 		error = -EBUSY;
26241da177e4SLinus Torvalds 	else {
26251da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2626bec1052eSAl Viro 		if (!error) {
26271da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2628bec1052eSAl Viro 			if (!error)
2629d83c49f3SAl Viro 				dont_mount(dentry);
2630bec1052eSAl Viro 		}
26311da177e4SLinus Torvalds 	}
26321b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
26331da177e4SLinus Torvalds 
26341da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
26351da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2636ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
26371da177e4SLinus Torvalds 		d_delete(dentry);
26381da177e4SLinus Torvalds 	}
26390eeca283SRobert Love 
26401da177e4SLinus Torvalds 	return error;
26411da177e4SLinus Torvalds }
26421da177e4SLinus Torvalds 
26431da177e4SLinus Torvalds /*
26441da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
26451b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
26461da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
26471da177e4SLinus Torvalds  * while waiting on the I/O.
26481da177e4SLinus Torvalds  */
26495590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
26501da177e4SLinus Torvalds {
26512ad94ae6SAl Viro 	int error;
26521da177e4SLinus Torvalds 	char *name;
26531da177e4SLinus Torvalds 	struct dentry *dentry;
26541da177e4SLinus Torvalds 	struct nameidata nd;
26551da177e4SLinus Torvalds 	struct inode *inode = NULL;
26561da177e4SLinus Torvalds 
26572ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
26581da177e4SLinus Torvalds 	if (error)
26592ad94ae6SAl Viro 		return error;
26602ad94ae6SAl Viro 
26611da177e4SLinus Torvalds 	error = -EISDIR;
26621da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
26631da177e4SLinus Torvalds 		goto exit1;
26640612d9fbSOGAWA Hirofumi 
26650612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
26660612d9fbSOGAWA Hirofumi 
26674ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
266849705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
26691da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
26701da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
26711da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
267250338b88STörök Edwin 		if (nd.last.name[nd.last.len])
267350338b88STörök Edwin 			goto slashes;
26741da177e4SLinus Torvalds 		inode = dentry->d_inode;
267550338b88STörök Edwin 		if (!inode)
2676e6bc45d6STheodore Ts'o 			goto slashes;
26777de9c6eeSAl Viro 		ihold(inode);
26780622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
26790622753bSDave Hansen 		if (error)
26800622753bSDave Hansen 			goto exit2;
2681be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2682be6d3e56SKentaro Takeda 		if (error)
2683be6d3e56SKentaro Takeda 			goto exit3;
26844ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2685be6d3e56SKentaro Takeda exit3:
26860622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
26871da177e4SLinus Torvalds 	exit2:
26881da177e4SLinus Torvalds 		dput(dentry);
26891da177e4SLinus Torvalds 	}
26904ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
26911da177e4SLinus Torvalds 	if (inode)
26921da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
26931da177e4SLinus Torvalds exit1:
26941d957f9bSJan Blunck 	path_put(&nd.path);
26951da177e4SLinus Torvalds 	putname(name);
26961da177e4SLinus Torvalds 	return error;
26971da177e4SLinus Torvalds 
26981da177e4SLinus Torvalds slashes:
26991da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
27001da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
27011da177e4SLinus Torvalds 	goto exit2;
27021da177e4SLinus Torvalds }
27031da177e4SLinus Torvalds 
27042e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
27055590ff0dSUlrich Drepper {
27065590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
27075590ff0dSUlrich Drepper 		return -EINVAL;
27085590ff0dSUlrich Drepper 
27095590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
27105590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
27115590ff0dSUlrich Drepper 
27125590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
27135590ff0dSUlrich Drepper }
27145590ff0dSUlrich Drepper 
27153480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
27165590ff0dSUlrich Drepper {
27175590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
27185590ff0dSUlrich Drepper }
27195590ff0dSUlrich Drepper 
2720db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
27211da177e4SLinus Torvalds {
2722a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
27231da177e4SLinus Torvalds 
27241da177e4SLinus Torvalds 	if (error)
27251da177e4SLinus Torvalds 		return error;
27261da177e4SLinus Torvalds 
2727acfa4380SAl Viro 	if (!dir->i_op->symlink)
27281da177e4SLinus Torvalds 		return -EPERM;
27291da177e4SLinus Torvalds 
27301da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
27311da177e4SLinus Torvalds 	if (error)
27321da177e4SLinus Torvalds 		return error;
27331da177e4SLinus Torvalds 
27341da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2735a74574aaSStephen Smalley 	if (!error)
2736f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
27371da177e4SLinus Torvalds 	return error;
27381da177e4SLinus Torvalds }
27391da177e4SLinus Torvalds 
27402e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
27412e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
27421da177e4SLinus Torvalds {
27432ad94ae6SAl Viro 	int error;
27441da177e4SLinus Torvalds 	char *from;
27456902d925SDave Hansen 	struct dentry *dentry;
2746dae6ad8fSAl Viro 	struct path path;
27471da177e4SLinus Torvalds 
27481da177e4SLinus Torvalds 	from = getname(oldname);
27491da177e4SLinus Torvalds 	if (IS_ERR(from))
27501da177e4SLinus Torvalds 		return PTR_ERR(from);
27512ad94ae6SAl Viro 
2752dae6ad8fSAl Viro 	dentry = user_path_create(newdfd, newname, &path, 0);
27531da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27546902d925SDave Hansen 	if (IS_ERR(dentry))
2755dae6ad8fSAl Viro 		goto out_putname;
27566902d925SDave Hansen 
2757dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
275875c3f29dSDave Hansen 	if (error)
275975c3f29dSDave Hansen 		goto out_dput;
2760dae6ad8fSAl Viro 	error = security_path_symlink(&path, dentry, from);
2761be6d3e56SKentaro Takeda 	if (error)
2762be6d3e56SKentaro Takeda 		goto out_drop_write;
2763dae6ad8fSAl Viro 	error = vfs_symlink(path.dentry->d_inode, dentry, from);
2764be6d3e56SKentaro Takeda out_drop_write:
2765dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
276675c3f29dSDave Hansen out_dput:
27671da177e4SLinus Torvalds 	dput(dentry);
2768dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2769dae6ad8fSAl Viro 	path_put(&path);
27706902d925SDave Hansen out_putname:
27711da177e4SLinus Torvalds 	putname(from);
27721da177e4SLinus Torvalds 	return error;
27731da177e4SLinus Torvalds }
27741da177e4SLinus Torvalds 
27753480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
27765590ff0dSUlrich Drepper {
27775590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
27785590ff0dSUlrich Drepper }
27795590ff0dSUlrich Drepper 
27801da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
27811da177e4SLinus Torvalds {
27821da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
27831da177e4SLinus Torvalds 	int error;
27841da177e4SLinus Torvalds 
27851da177e4SLinus Torvalds 	if (!inode)
27861da177e4SLinus Torvalds 		return -ENOENT;
27871da177e4SLinus Torvalds 
2788a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
27891da177e4SLinus Torvalds 	if (error)
27901da177e4SLinus Torvalds 		return error;
27911da177e4SLinus Torvalds 
27921da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
27931da177e4SLinus Torvalds 		return -EXDEV;
27941da177e4SLinus Torvalds 
27951da177e4SLinus Torvalds 	/*
27961da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
27971da177e4SLinus Torvalds 	 */
27981da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
27991da177e4SLinus Torvalds 		return -EPERM;
2800acfa4380SAl Viro 	if (!dir->i_op->link)
28011da177e4SLinus Torvalds 		return -EPERM;
28027e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
28031da177e4SLinus Torvalds 		return -EPERM;
28041da177e4SLinus Torvalds 
28051da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
28061da177e4SLinus Torvalds 	if (error)
28071da177e4SLinus Torvalds 		return error;
28081da177e4SLinus Torvalds 
28097e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
2810aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
2811aae8a97dSAneesh Kumar K.V 	if (inode->i_nlink == 0)
2812aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
2813aae8a97dSAneesh Kumar K.V 	else
28141da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
28157e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2816e31e14ecSStephen Smalley 	if (!error)
28177e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
28181da177e4SLinus Torvalds 	return error;
28191da177e4SLinus Torvalds }
28201da177e4SLinus Torvalds 
28211da177e4SLinus Torvalds /*
28221da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
28231da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
28241da177e4SLinus Torvalds  * newname.  --KAB
28251da177e4SLinus Torvalds  *
28261da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
28271da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
28281da177e4SLinus Torvalds  * and other special files.  --ADM
28291da177e4SLinus Torvalds  */
28302e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
28312e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
28321da177e4SLinus Torvalds {
28331da177e4SLinus Torvalds 	struct dentry *new_dentry;
2834dae6ad8fSAl Viro 	struct path old_path, new_path;
283511a7b371SAneesh Kumar K.V 	int how = 0;
28361da177e4SLinus Torvalds 	int error;
28371da177e4SLinus Torvalds 
283811a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
2839c04030e1SUlrich Drepper 		return -EINVAL;
284011a7b371SAneesh Kumar K.V 	/*
284111a7b371SAneesh Kumar K.V 	 * To use null names we require CAP_DAC_READ_SEARCH
284211a7b371SAneesh Kumar K.V 	 * This ensures that not everyone will be able to create
284311a7b371SAneesh Kumar K.V 	 * handlink using the passed filedescriptor.
284411a7b371SAneesh Kumar K.V 	 */
284511a7b371SAneesh Kumar K.V 	if (flags & AT_EMPTY_PATH) {
284611a7b371SAneesh Kumar K.V 		if (!capable(CAP_DAC_READ_SEARCH))
284711a7b371SAneesh Kumar K.V 			return -ENOENT;
284811a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
284911a7b371SAneesh Kumar K.V 	}
2850c04030e1SUlrich Drepper 
285111a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
285211a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
285311a7b371SAneesh Kumar K.V 
285411a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
28551da177e4SLinus Torvalds 	if (error)
28562ad94ae6SAl Viro 		return error;
28572ad94ae6SAl Viro 
2858dae6ad8fSAl Viro 	new_dentry = user_path_create(newdfd, newname, &new_path, 0);
28591da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
28606902d925SDave Hansen 	if (IS_ERR(new_dentry))
2861dae6ad8fSAl Viro 		goto out;
2862dae6ad8fSAl Viro 
2863dae6ad8fSAl Viro 	error = -EXDEV;
2864dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
2865dae6ad8fSAl Viro 		goto out_dput;
2866dae6ad8fSAl Viro 	error = mnt_want_write(new_path.mnt);
286775c3f29dSDave Hansen 	if (error)
286875c3f29dSDave Hansen 		goto out_dput;
2869dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
2870be6d3e56SKentaro Takeda 	if (error)
2871be6d3e56SKentaro Takeda 		goto out_drop_write;
2872dae6ad8fSAl Viro 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
2873be6d3e56SKentaro Takeda out_drop_write:
2874dae6ad8fSAl Viro 	mnt_drop_write(new_path.mnt);
287575c3f29dSDave Hansen out_dput:
28761da177e4SLinus Torvalds 	dput(new_dentry);
2877dae6ad8fSAl Viro 	mutex_unlock(&new_path.dentry->d_inode->i_mutex);
2878dae6ad8fSAl Viro 	path_put(&new_path);
28791da177e4SLinus Torvalds out:
28802d8f3038SAl Viro 	path_put(&old_path);
28811da177e4SLinus Torvalds 
28821da177e4SLinus Torvalds 	return error;
28831da177e4SLinus Torvalds }
28841da177e4SLinus Torvalds 
28853480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
28865590ff0dSUlrich Drepper {
2887c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
28885590ff0dSUlrich Drepper }
28895590ff0dSUlrich Drepper 
28901da177e4SLinus Torvalds /*
28911da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
28921da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
28931da177e4SLinus Torvalds  * Problems:
28941da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
28951da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
28961da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
2897a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
28981da177e4SLinus Torvalds  *	   story.
28991da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
29001b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
29011da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
29021da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
2903a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
29041da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
29051da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
29061da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
2907a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
29081da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
29091da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
29101da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
2911e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
29121b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
29131da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2914c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
29151da177e4SLinus Torvalds  *	   locking].
29161da177e4SLinus Torvalds  */
291775c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
29181da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
29191da177e4SLinus Torvalds {
29201da177e4SLinus Torvalds 	int error = 0;
29219055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
29221da177e4SLinus Torvalds 
29231da177e4SLinus Torvalds 	/*
29241da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
29251da177e4SLinus Torvalds 	 * we'll need to flip '..'.
29261da177e4SLinus Torvalds 	 */
29271da177e4SLinus Torvalds 	if (new_dir != old_dir) {
2928f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
29291da177e4SLinus Torvalds 		if (error)
29301da177e4SLinus Torvalds 			return error;
29311da177e4SLinus Torvalds 	}
29321da177e4SLinus Torvalds 
29331da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
29341da177e4SLinus Torvalds 	if (error)
29351da177e4SLinus Torvalds 		return error;
29361da177e4SLinus Torvalds 
2937d83c49f3SAl Viro 	if (target)
29381b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
29399055cba7SSage Weil 
29401da177e4SLinus Torvalds 	error = -EBUSY;
29419055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
29429055cba7SSage Weil 		goto out;
29439055cba7SSage Weil 
29443cebde24SSage Weil 	if (target)
29453cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
29461da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
29479055cba7SSage Weil 	if (error)
29489055cba7SSage Weil 		goto out;
29499055cba7SSage Weil 
29501da177e4SLinus Torvalds 	if (target) {
29511da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
2952d83c49f3SAl Viro 		dont_mount(new_dentry);
2953d83c49f3SAl Viro 	}
29549055cba7SSage Weil out:
29559055cba7SSage Weil 	if (target)
29561b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
2957e31e14ecSStephen Smalley 	if (!error)
2958349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
29591da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
29601da177e4SLinus Torvalds 	return error;
29611da177e4SLinus Torvalds }
29621da177e4SLinus Torvalds 
296375c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
29641da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
29651da177e4SLinus Torvalds {
296651892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
29671da177e4SLinus Torvalds 	int error;
29681da177e4SLinus Torvalds 
29691da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
29701da177e4SLinus Torvalds 	if (error)
29711da177e4SLinus Torvalds 		return error;
29721da177e4SLinus Torvalds 
29731da177e4SLinus Torvalds 	dget(new_dentry);
29741da177e4SLinus Torvalds 	if (target)
29751b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
297651892bbbSSage Weil 
29771da177e4SLinus Torvalds 	error = -EBUSY;
297851892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
297951892bbbSSage Weil 		goto out;
298051892bbbSSage Weil 
29811da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
298251892bbbSSage Weil 	if (error)
298351892bbbSSage Weil 		goto out;
298451892bbbSSage Weil 
2985bec1052eSAl Viro 	if (target)
2986d83c49f3SAl Viro 		dont_mount(new_dentry);
2987349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
29881da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
298951892bbbSSage Weil out:
29901da177e4SLinus Torvalds 	if (target)
29911b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
29921da177e4SLinus Torvalds 	dput(new_dentry);
29931da177e4SLinus Torvalds 	return error;
29941da177e4SLinus Torvalds }
29951da177e4SLinus Torvalds 
29961da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
29971da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
29981da177e4SLinus Torvalds {
29991da177e4SLinus Torvalds 	int error;
30001da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
300159b0df21SEric Paris 	const unsigned char *old_name;
30021da177e4SLinus Torvalds 
30031da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
30041da177e4SLinus Torvalds  		return 0;
30051da177e4SLinus Torvalds 
30061da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
30071da177e4SLinus Torvalds 	if (error)
30081da177e4SLinus Torvalds 		return error;
30091da177e4SLinus Torvalds 
30101da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3011a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
30121da177e4SLinus Torvalds 	else
30131da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
30141da177e4SLinus Torvalds 	if (error)
30151da177e4SLinus Torvalds 		return error;
30161da177e4SLinus Torvalds 
3017acfa4380SAl Viro 	if (!old_dir->i_op->rename)
30181da177e4SLinus Torvalds 		return -EPERM;
30191da177e4SLinus Torvalds 
30200eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
30210eeca283SRobert Love 
30221da177e4SLinus Torvalds 	if (is_dir)
30231da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
30241da177e4SLinus Torvalds 	else
30251da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3026123df294SAl Viro 	if (!error)
3027123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
30285a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
30290eeca283SRobert Love 	fsnotify_oldname_free(old_name);
30300eeca283SRobert Love 
30311da177e4SLinus Torvalds 	return error;
30321da177e4SLinus Torvalds }
30331da177e4SLinus Torvalds 
30342e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
30352e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
30361da177e4SLinus Torvalds {
30371da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
30381da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
30391da177e4SLinus Torvalds 	struct dentry *trap;
30401da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
30412ad94ae6SAl Viro 	char *from;
30422ad94ae6SAl Viro 	char *to;
30432ad94ae6SAl Viro 	int error;
30441da177e4SLinus Torvalds 
30452ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
30461da177e4SLinus Torvalds 	if (error)
30471da177e4SLinus Torvalds 		goto exit;
30481da177e4SLinus Torvalds 
30492ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
30501da177e4SLinus Torvalds 	if (error)
30511da177e4SLinus Torvalds 		goto exit1;
30521da177e4SLinus Torvalds 
30531da177e4SLinus Torvalds 	error = -EXDEV;
30544ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
30551da177e4SLinus Torvalds 		goto exit2;
30561da177e4SLinus Torvalds 
30574ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
30581da177e4SLinus Torvalds 	error = -EBUSY;
30591da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
30601da177e4SLinus Torvalds 		goto exit2;
30611da177e4SLinus Torvalds 
30624ac91378SJan Blunck 	new_dir = newnd.path.dentry;
30631da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
30641da177e4SLinus Torvalds 		goto exit2;
30651da177e4SLinus Torvalds 
30660612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
30670612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
30684e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
30690612d9fbSOGAWA Hirofumi 
30701da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
30711da177e4SLinus Torvalds 
307249705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
30731da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
30741da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
30751da177e4SLinus Torvalds 		goto exit3;
30761da177e4SLinus Torvalds 	/* source must exist */
30771da177e4SLinus Torvalds 	error = -ENOENT;
30781da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
30791da177e4SLinus Torvalds 		goto exit4;
30801da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
30811da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
30821da177e4SLinus Torvalds 		error = -ENOTDIR;
30831da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
30841da177e4SLinus Torvalds 			goto exit4;
30851da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
30861da177e4SLinus Torvalds 			goto exit4;
30871da177e4SLinus Torvalds 	}
30881da177e4SLinus Torvalds 	/* source should not be ancestor of target */
30891da177e4SLinus Torvalds 	error = -EINVAL;
30901da177e4SLinus Torvalds 	if (old_dentry == trap)
30911da177e4SLinus Torvalds 		goto exit4;
309249705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
30931da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
30941da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
30951da177e4SLinus Torvalds 		goto exit4;
30961da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
30971da177e4SLinus Torvalds 	error = -ENOTEMPTY;
30981da177e4SLinus Torvalds 	if (new_dentry == trap)
30991da177e4SLinus Torvalds 		goto exit5;
31001da177e4SLinus Torvalds 
31019079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
31029079b1ebSDave Hansen 	if (error)
31039079b1ebSDave Hansen 		goto exit5;
3104be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3105be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3106be6d3e56SKentaro Takeda 	if (error)
3107be6d3e56SKentaro Takeda 		goto exit6;
31081da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
31091da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3110be6d3e56SKentaro Takeda exit6:
31119079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
31121da177e4SLinus Torvalds exit5:
31131da177e4SLinus Torvalds 	dput(new_dentry);
31141da177e4SLinus Torvalds exit4:
31151da177e4SLinus Torvalds 	dput(old_dentry);
31161da177e4SLinus Torvalds exit3:
31171da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
31181da177e4SLinus Torvalds exit2:
31191d957f9bSJan Blunck 	path_put(&newnd.path);
31202ad94ae6SAl Viro 	putname(to);
31211da177e4SLinus Torvalds exit1:
31221d957f9bSJan Blunck 	path_put(&oldnd.path);
31231da177e4SLinus Torvalds 	putname(from);
31242ad94ae6SAl Viro exit:
31251da177e4SLinus Torvalds 	return error;
31261da177e4SLinus Torvalds }
31271da177e4SLinus Torvalds 
3128a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
31295590ff0dSUlrich Drepper {
31305590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
31315590ff0dSUlrich Drepper }
31325590ff0dSUlrich Drepper 
31331da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
31341da177e4SLinus Torvalds {
31351da177e4SLinus Torvalds 	int len;
31361da177e4SLinus Torvalds 
31371da177e4SLinus Torvalds 	len = PTR_ERR(link);
31381da177e4SLinus Torvalds 	if (IS_ERR(link))
31391da177e4SLinus Torvalds 		goto out;
31401da177e4SLinus Torvalds 
31411da177e4SLinus Torvalds 	len = strlen(link);
31421da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
31431da177e4SLinus Torvalds 		len = buflen;
31441da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
31451da177e4SLinus Torvalds 		len = -EFAULT;
31461da177e4SLinus Torvalds out:
31471da177e4SLinus Torvalds 	return len;
31481da177e4SLinus Torvalds }
31491da177e4SLinus Torvalds 
31501da177e4SLinus Torvalds /*
31511da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
31521da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
31531da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
31541da177e4SLinus Torvalds  */
31551da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
31561da177e4SLinus Torvalds {
31571da177e4SLinus Torvalds 	struct nameidata nd;
3158cc314eefSLinus Torvalds 	void *cookie;
3159694a1764SMarcin Slusarz 	int res;
3160cc314eefSLinus Torvalds 
31611da177e4SLinus Torvalds 	nd.depth = 0;
3162cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3163694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3164694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3165694a1764SMarcin Slusarz 
3166694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
31671da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3168cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3169694a1764SMarcin Slusarz 	return res;
31701da177e4SLinus Torvalds }
31711da177e4SLinus Torvalds 
31721da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
31731da177e4SLinus Torvalds {
31741da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
31751da177e4SLinus Torvalds }
31761da177e4SLinus Torvalds 
31771da177e4SLinus Torvalds /* get the link contents into pagecache */
31781da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
31791da177e4SLinus Torvalds {
3180ebd09abbSDuane Griffin 	char *kaddr;
31811da177e4SLinus Torvalds 	struct page *page;
31821da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3183090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
31841da177e4SLinus Torvalds 	if (IS_ERR(page))
31856fe6900eSNick Piggin 		return (char*)page;
31861da177e4SLinus Torvalds 	*ppage = page;
3187ebd09abbSDuane Griffin 	kaddr = kmap(page);
3188ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3189ebd09abbSDuane Griffin 	return kaddr;
31901da177e4SLinus Torvalds }
31911da177e4SLinus Torvalds 
31921da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
31931da177e4SLinus Torvalds {
31941da177e4SLinus Torvalds 	struct page *page = NULL;
31951da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
31961da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
31971da177e4SLinus Torvalds 	if (page) {
31981da177e4SLinus Torvalds 		kunmap(page);
31991da177e4SLinus Torvalds 		page_cache_release(page);
32001da177e4SLinus Torvalds 	}
32011da177e4SLinus Torvalds 	return res;
32021da177e4SLinus Torvalds }
32031da177e4SLinus Torvalds 
3204cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
32051da177e4SLinus Torvalds {
3206cc314eefSLinus Torvalds 	struct page *page = NULL;
32071da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3208cc314eefSLinus Torvalds 	return page;
32091da177e4SLinus Torvalds }
32101da177e4SLinus Torvalds 
3211cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
32121da177e4SLinus Torvalds {
3213cc314eefSLinus Torvalds 	struct page *page = cookie;
3214cc314eefSLinus Torvalds 
3215cc314eefSLinus Torvalds 	if (page) {
32161da177e4SLinus Torvalds 		kunmap(page);
32171da177e4SLinus Torvalds 		page_cache_release(page);
32181da177e4SLinus Torvalds 	}
32191da177e4SLinus Torvalds }
32201da177e4SLinus Torvalds 
322154566b2cSNick Piggin /*
322254566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
322354566b2cSNick Piggin  */
322454566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
32251da177e4SLinus Torvalds {
32261da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
32270adb25d2SKirill Korotaev 	struct page *page;
3228afddba49SNick Piggin 	void *fsdata;
3229beb497abSDmitriy Monakhov 	int err;
32301da177e4SLinus Torvalds 	char *kaddr;
323154566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
323254566b2cSNick Piggin 	if (nofs)
323354566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
32341da177e4SLinus Torvalds 
32357e53cac4SNeilBrown retry:
3236afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
323754566b2cSNick Piggin 				flags, &page, &fsdata);
32381da177e4SLinus Torvalds 	if (err)
3239afddba49SNick Piggin 		goto fail;
3240afddba49SNick Piggin 
32411da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
32421da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
32431da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
3244afddba49SNick Piggin 
3245afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3246afddba49SNick Piggin 							page, fsdata);
32471da177e4SLinus Torvalds 	if (err < 0)
32481da177e4SLinus Torvalds 		goto fail;
3249afddba49SNick Piggin 	if (err < len-1)
3250afddba49SNick Piggin 		goto retry;
3251afddba49SNick Piggin 
32521da177e4SLinus Torvalds 	mark_inode_dirty(inode);
32531da177e4SLinus Torvalds 	return 0;
32541da177e4SLinus Torvalds fail:
32551da177e4SLinus Torvalds 	return err;
32561da177e4SLinus Torvalds }
32571da177e4SLinus Torvalds 
32580adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
32590adb25d2SKirill Korotaev {
32600adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
326154566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
32620adb25d2SKirill Korotaev }
32630adb25d2SKirill Korotaev 
326492e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
32651da177e4SLinus Torvalds 	.readlink	= generic_readlink,
32661da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
32671da177e4SLinus Torvalds 	.put_link	= page_put_link,
32681da177e4SLinus Torvalds };
32691da177e4SLinus Torvalds 
32702d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3271cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
32721da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
32731da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
32741da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
32751da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
32761da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
32771da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
32781da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
32791da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
32801da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
32810adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
32821da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
32831da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3284c9c6cac0SAl Viro EXPORT_SYMBOL(kern_path_parent);
3285d1811465SAl Viro EXPORT_SYMBOL(kern_path);
328616f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3287f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
32881da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
32891da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
32901da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
32911da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
32921da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
32931da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
32941da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
32951da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
32961da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
32971da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
32981da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
32991da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
33001da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
33011da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3302