xref: /openbmc/linux/fs/namei.c (revision 8a5e929d)
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 	 */
6676f45b656SDavid Howells 	if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
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) &&
6799875cf80SDavid Howells 	    !(flags & (LOOKUP_CONTINUE | 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 		 */
6989875cf80SDavid Howells 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
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 	unsigned int lookup_flags = nd->flags;
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 	while (*name=='/')
12871da177e4SLinus Torvalds 		name++;
12881da177e4SLinus Torvalds 	if (!*name)
1289086e183aSAl Viro 		return 0;
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
12921da177e4SLinus Torvalds 	for(;;) {
12931da177e4SLinus Torvalds 		unsigned long hash;
12941da177e4SLinus Torvalds 		struct qstr this;
12951da177e4SLinus Torvalds 		unsigned int c;
1296fe479a58SAl Viro 		int type;
12971da177e4SLinus Torvalds 
1298cdce5d6bSTrond Myklebust 		nd->flags |= LOOKUP_CONTINUE;
129952094c8aSAl Viro 
130052094c8aSAl Viro 		err = may_lookup(nd);
13011da177e4SLinus Torvalds  		if (err)
13021da177e4SLinus Torvalds 			break;
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 		this.name = name;
13051da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 		hash = init_name_hash();
13081da177e4SLinus Torvalds 		do {
13091da177e4SLinus Torvalds 			name++;
13101da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
13111da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
13121da177e4SLinus Torvalds 		} while (c && (c != '/'));
13131da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
13141da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
13151da177e4SLinus Torvalds 
1316fe479a58SAl Viro 		type = LAST_NORM;
1317fe479a58SAl Viro 		if (this.name[0] == '.') switch (this.len) {
1318fe479a58SAl Viro 			case 2:
131916c2cd71SAl Viro 				if (this.name[1] == '.') {
1320fe479a58SAl Viro 					type = LAST_DOTDOT;
132116c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
132216c2cd71SAl Viro 				}
1323fe479a58SAl Viro 				break;
1324fe479a58SAl Viro 			case 1:
1325fe479a58SAl Viro 				type = LAST_DOT;
1326fe479a58SAl Viro 		}
13275a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
13285a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
132916c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
13305a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
13315a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
13325a202bcdSAl Viro 							   &this);
13335a202bcdSAl Viro 				if (err < 0)
13345a202bcdSAl Viro 					break;
13355a202bcdSAl Viro 			}
13365a202bcdSAl Viro 		}
1337fe479a58SAl Viro 
13381da177e4SLinus Torvalds 		/* remove trailing slashes? */
13391da177e4SLinus Torvalds 		if (!c)
13401da177e4SLinus Torvalds 			goto last_component;
13411da177e4SLinus Torvalds 		while (*++name == '/');
13421da177e4SLinus Torvalds 		if (!*name)
1343b356379aSAl Viro 			goto last_component;
13441da177e4SLinus Torvalds 
1345ce57dfc1SAl Viro 		err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1346ce57dfc1SAl Viro 		if (err < 0)
1347ce57dfc1SAl Viro 			return err;
1348fe479a58SAl Viro 
1349ce57dfc1SAl Viro 		if (err) {
1350b356379aSAl Viro 			err = nested_symlink(&next, nd);
13511da177e4SLinus Torvalds 			if (err)
1352a7472babSAl Viro 				return err;
135331e6b01fSNick Piggin 		}
13541da177e4SLinus Torvalds 		err = -ENOTDIR;
135531e6b01fSNick Piggin 		if (!nd->inode->i_op->lookup)
13561da177e4SLinus Torvalds 			break;
13571da177e4SLinus Torvalds 		continue;
13581da177e4SLinus Torvalds 		/* here ends the main loop */
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds last_component:
1361f55eab82STrond Myklebust 		/* Clear LOOKUP_CONTINUE iff it was previously unset */
1362f55eab82STrond Myklebust 		nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
1363ce57dfc1SAl Viro 		nd->last = this;
1364ce57dfc1SAl Viro 		nd->last_type = type;
1365ce57dfc1SAl Viro 		return 0;
1366ce57dfc1SAl Viro 	}
1367951361f9SAl Viro 	terminate_walk(nd);
13681da177e4SLinus Torvalds 	return err;
13691da177e4SLinus Torvalds }
13701da177e4SLinus Torvalds 
137170e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
137270e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
137331e6b01fSNick Piggin {
137431e6b01fSNick Piggin 	int retval = 0;
137531e6b01fSNick Piggin 	int fput_needed;
137631e6b01fSNick Piggin 	struct file *file;
137731e6b01fSNick Piggin 
137831e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
137916c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
138031e6b01fSNick Piggin 	nd->depth = 0;
13815b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
13825b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
138373d049a4SAl Viro 		if (*name) {
13845b6ca027SAl Viro 			if (!inode->i_op->lookup)
13855b6ca027SAl Viro 				return -ENOTDIR;
13865b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
13875b6ca027SAl Viro 			if (retval)
13885b6ca027SAl Viro 				return retval;
138973d049a4SAl Viro 		}
13905b6ca027SAl Viro 		nd->path = nd->root;
13915b6ca027SAl Viro 		nd->inode = inode;
13925b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
13935b6ca027SAl Viro 			br_read_lock(vfsmount_lock);
13945b6ca027SAl Viro 			rcu_read_lock();
13955b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
13965b6ca027SAl Viro 		} else {
13975b6ca027SAl Viro 			path_get(&nd->path);
13985b6ca027SAl Viro 		}
13995b6ca027SAl Viro 		return 0;
14005b6ca027SAl Viro 	}
14015b6ca027SAl Viro 
140231e6b01fSNick Piggin 	nd->root.mnt = NULL;
140331e6b01fSNick Piggin 
140431e6b01fSNick Piggin 	if (*name=='/') {
1405e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
140631e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
140731e6b01fSNick Piggin 			rcu_read_lock();
1408e41f7d4eSAl Viro 			set_root_rcu(nd);
1409e41f7d4eSAl Viro 		} else {
1410e41f7d4eSAl Viro 			set_root(nd);
1411e41f7d4eSAl Viro 			path_get(&nd->root);
1412e41f7d4eSAl Viro 		}
141331e6b01fSNick Piggin 		nd->path = nd->root;
141431e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1415e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
141631e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1417c28cc364SNick Piggin 			unsigned seq;
141831e6b01fSNick Piggin 
141931e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
142031e6b01fSNick Piggin 			rcu_read_lock();
142131e6b01fSNick Piggin 
1422c28cc364SNick Piggin 			do {
1423c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
142431e6b01fSNick Piggin 				nd->path = fs->pwd;
1425c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1426c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1427e41f7d4eSAl Viro 		} else {
1428e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1429e41f7d4eSAl Viro 		}
143031e6b01fSNick Piggin 	} else {
143131e6b01fSNick Piggin 		struct dentry *dentry;
143231e6b01fSNick Piggin 
14331abf0c71SAl Viro 		file = fget_raw_light(dfd, &fput_needed);
143431e6b01fSNick Piggin 		retval = -EBADF;
143531e6b01fSNick Piggin 		if (!file)
143631e6b01fSNick Piggin 			goto out_fail;
143731e6b01fSNick Piggin 
143831e6b01fSNick Piggin 		dentry = file->f_path.dentry;
143931e6b01fSNick Piggin 
1440f52e0c11SAl Viro 		if (*name) {
144131e6b01fSNick Piggin 			retval = -ENOTDIR;
144231e6b01fSNick Piggin 			if (!S_ISDIR(dentry->d_inode->i_mode))
144331e6b01fSNick Piggin 				goto fput_fail;
144431e6b01fSNick Piggin 
14454ad5abb3SAl Viro 			retval = inode_permission(dentry->d_inode, MAY_EXEC);
144631e6b01fSNick Piggin 			if (retval)
144731e6b01fSNick Piggin 				goto fput_fail;
1448f52e0c11SAl Viro 		}
144931e6b01fSNick Piggin 
145031e6b01fSNick Piggin 		nd->path = file->f_path;
1451e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
145231e6b01fSNick Piggin 			if (fput_needed)
145370e9b357SAl Viro 				*fp = file;
1454c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
145531e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
145631e6b01fSNick Piggin 			rcu_read_lock();
14575590ff0dSUlrich Drepper 		} else {
14585dd784d0SJan Blunck 			path_get(&file->f_path);
14595590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
14601da177e4SLinus Torvalds 		}
1461e41f7d4eSAl Viro 	}
1462e41f7d4eSAl Viro 
146331e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
14649b4a9b14SAl Viro 	return 0;
14652dfdd266SJosef 'Jeff' Sipek 
14669b4a9b14SAl Viro fput_fail:
14679b4a9b14SAl Viro 	fput_light(file, fput_needed);
14689b4a9b14SAl Viro out_fail:
14699b4a9b14SAl Viro 	return retval;
14709b4a9b14SAl Viro }
14719b4a9b14SAl Viro 
1472bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1473bd92d7feSAl Viro {
1474bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1475bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1476bd92d7feSAl Viro 
1477bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1478bd92d7feSAl Viro 	return walk_component(nd, path, &nd->last, nd->last_type,
1479bd92d7feSAl Viro 					nd->flags & LOOKUP_FOLLOW);
1480bd92d7feSAl Viro }
1481bd92d7feSAl Viro 
14829b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1483ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
14849b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
14859b4a9b14SAl Viro {
148670e9b357SAl Viro 	struct file *base = NULL;
1487bd92d7feSAl Viro 	struct path path;
1488bd92d7feSAl Viro 	int err;
148931e6b01fSNick Piggin 
149031e6b01fSNick Piggin 	/*
149131e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
149231e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
149331e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
149431e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
149531e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
149631e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
149731e6b01fSNick Piggin 	 * analogue, foo_rcu().
149831e6b01fSNick Piggin 	 *
149931e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
150031e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
150131e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
150231e6b01fSNick Piggin 	 * be able to complete).
150331e6b01fSNick Piggin 	 */
1504bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1505ee0827cdSAl Viro 
1506bd92d7feSAl Viro 	if (unlikely(err))
1507bd92d7feSAl Viro 		return err;
1508ee0827cdSAl Viro 
1509ee0827cdSAl Viro 	current->total_link_count = 0;
1510bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1511bd92d7feSAl Viro 
1512bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1513bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1514bd92d7feSAl Viro 		while (err > 0) {
1515bd92d7feSAl Viro 			void *cookie;
1516bd92d7feSAl Viro 			struct path link = path;
1517bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1518574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
1519bd92d7feSAl Viro 			if (!err)
1520bd92d7feSAl Viro 				err = lookup_last(nd, &path);
1521574197e0SAl Viro 			put_link(nd, &link, cookie);
1522bd92d7feSAl Viro 		}
1523bd92d7feSAl Viro 	}
1524ee0827cdSAl Viro 
15259f1fafeeSAl Viro 	if (!err)
15269f1fafeeSAl Viro 		err = complete_walk(nd);
1527bd92d7feSAl Viro 
1528bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1529bd92d7feSAl Viro 		if (!nd->inode->i_op->lookup) {
1530bd92d7feSAl Viro 			path_put(&nd->path);
1531bd23a539SAl Viro 			err = -ENOTDIR;
1532bd92d7feSAl Viro 		}
1533bd92d7feSAl Viro 	}
153416c2cd71SAl Viro 
153570e9b357SAl Viro 	if (base)
153670e9b357SAl Viro 		fput(base);
1537ee0827cdSAl Viro 
15385b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
153931e6b01fSNick Piggin 		path_put(&nd->root);
154031e6b01fSNick Piggin 		nd->root.mnt = NULL;
154131e6b01fSNick Piggin 	}
1542bd92d7feSAl Viro 	return err;
154331e6b01fSNick Piggin }
154431e6b01fSNick Piggin 
1545ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1546ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1547ee0827cdSAl Viro {
1548ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1549ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1550ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1551ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1552ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1553ee0827cdSAl Viro 
155431e6b01fSNick Piggin 	if (likely(!retval)) {
155531e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
155631e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
155731e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
155831e6b01fSNick Piggin 		}
155931e6b01fSNick Piggin 	}
1560170aa3d0SUlrich Drepper 	return retval;
15611da177e4SLinus Torvalds }
15621da177e4SLinus Torvalds 
1563c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd)
15645590ff0dSUlrich Drepper {
1565c9c6cac0SAl Viro 	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
15665590ff0dSUlrich Drepper }
15675590ff0dSUlrich Drepper 
1568d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1569d1811465SAl Viro {
1570d1811465SAl Viro 	struct nameidata nd;
1571d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1572d1811465SAl Viro 	if (!res)
1573d1811465SAl Viro 		*path = nd.path;
1574d1811465SAl Viro 	return res;
1575d1811465SAl Viro }
1576d1811465SAl Viro 
157716f18200SJosef 'Jeff' Sipek /**
157816f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
157916f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
158016f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
158116f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
158216f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
158316f18200SJosef 'Jeff' Sipek  * @nd: pointer to nameidata
158416f18200SJosef 'Jeff' Sipek  */
158516f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
158616f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
158716f18200SJosef 'Jeff' Sipek 		    struct nameidata *nd)
158816f18200SJosef 'Jeff' Sipek {
15895b6ca027SAl Viro 	nd->root.dentry = dentry;
15905b6ca027SAl Viro 	nd->root.mnt = mnt;
15915b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
15925b6ca027SAl Viro 	return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
159316f18200SJosef 'Jeff' Sipek }
159416f18200SJosef 'Jeff' Sipek 
1595eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1596eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
15971da177e4SLinus Torvalds {
159881fca444SChristoph Hellwig 	struct inode *inode = base->d_inode;
15991da177e4SLinus Torvalds 	struct dentry *dentry;
16001da177e4SLinus Torvalds 	int err;
16011da177e4SLinus Torvalds 
16024ad5abb3SAl Viro 	err = inode_permission(inode, MAY_EXEC);
160381fca444SChristoph Hellwig 	if (err)
160481fca444SChristoph Hellwig 		return ERR_PTR(err);
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds 	/*
1607b04f784eSNick Piggin 	 * Don't bother with __d_lookup: callers are for creat as
1608b04f784eSNick Piggin 	 * well as unlink, so a lot of the time it would cost
1609b04f784eSNick Piggin 	 * a double lookup.
16106e6b1bd1SAl Viro 	 */
16116e6b1bd1SAl Viro 	dentry = d_lookup(base, name);
16126e6b1bd1SAl Viro 
161344396f4bSJosef Bacik 	if (dentry && d_need_lookup(dentry)) {
161444396f4bSJosef Bacik 		/*
161544396f4bSJosef Bacik 		 * __lookup_hash is called with the parent dir's i_mutex already
161644396f4bSJosef Bacik 		 * held, so we are good to go here.
161744396f4bSJosef Bacik 		 */
161844396f4bSJosef Bacik 		dentry = d_inode_lookup(base, dentry, nd);
161944396f4bSJosef Bacik 		if (IS_ERR(dentry))
162044396f4bSJosef Bacik 			return dentry;
162144396f4bSJosef Bacik 	}
162244396f4bSJosef Bacik 
1623d2d9e9fbSAl Viro 	if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1624d2d9e9fbSAl Viro 		int status = d_revalidate(dentry, nd);
1625d2d9e9fbSAl Viro 		if (unlikely(status <= 0)) {
1626d2d9e9fbSAl Viro 			/*
1627d2d9e9fbSAl Viro 			 * The dentry failed validation.
1628d2d9e9fbSAl Viro 			 * If d_revalidate returned 0 attempt to invalidate
1629d2d9e9fbSAl Viro 			 * the dentry otherwise d_revalidate is asking us
1630d2d9e9fbSAl Viro 			 * to return a fail status.
1631d2d9e9fbSAl Viro 			 */
1632d2d9e9fbSAl Viro 			if (status < 0) {
1633d2d9e9fbSAl Viro 				dput(dentry);
1634d2d9e9fbSAl Viro 				return ERR_PTR(status);
1635d2d9e9fbSAl Viro 			} else if (!d_invalidate(dentry)) {
1636d2d9e9fbSAl Viro 				dput(dentry);
1637d2d9e9fbSAl Viro 				dentry = NULL;
1638d2d9e9fbSAl Viro 			}
1639d2d9e9fbSAl Viro 		}
1640d2d9e9fbSAl Viro 	}
16416e6b1bd1SAl Viro 
16421da177e4SLinus Torvalds 	if (!dentry)
1643baa03890SNick Piggin 		dentry = d_alloc_and_lookup(base, name, nd);
16445a202bcdSAl Viro 
16451da177e4SLinus Torvalds 	return dentry;
16461da177e4SLinus Torvalds }
16471da177e4SLinus Torvalds 
1648057f6c01SJames Morris /*
1649057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1650057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1651057f6c01SJames Morris  * SMP-safe.
1652057f6c01SJames Morris  */
1653a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
16541da177e4SLinus Torvalds {
16554ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
16561da177e4SLinus Torvalds }
16571da177e4SLinus Torvalds 
1658eead1911SChristoph Hellwig /**
1659a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1660eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1661eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1662eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1663eead1911SChristoph Hellwig  *
1664a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1665a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1666eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1667eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1668eead1911SChristoph Hellwig  */
1669057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1670057f6c01SJames Morris {
1671057f6c01SJames Morris 	struct qstr this;
16726a96ba54SAl Viro 	unsigned long hash;
16736a96ba54SAl Viro 	unsigned int c;
1674057f6c01SJames Morris 
16752f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
16762f9092e1SDavid Woodhouse 
16776a96ba54SAl Viro 	this.name = name;
16786a96ba54SAl Viro 	this.len = len;
16796a96ba54SAl Viro 	if (!len)
16806a96ba54SAl Viro 		return ERR_PTR(-EACCES);
16816a96ba54SAl Viro 
16826a96ba54SAl Viro 	hash = init_name_hash();
16836a96ba54SAl Viro 	while (len--) {
16846a96ba54SAl Viro 		c = *(const unsigned char *)name++;
16856a96ba54SAl Viro 		if (c == '/' || c == '\0')
16866a96ba54SAl Viro 			return ERR_PTR(-EACCES);
16876a96ba54SAl Viro 		hash = partial_name_hash(c, hash);
16886a96ba54SAl Viro 	}
16896a96ba54SAl Viro 	this.hash = end_name_hash(hash);
16905a202bcdSAl Viro 	/*
16915a202bcdSAl Viro 	 * See if the low-level filesystem might want
16925a202bcdSAl Viro 	 * to use its own hash..
16935a202bcdSAl Viro 	 */
16945a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
16955a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
16965a202bcdSAl Viro 		if (err < 0)
16975a202bcdSAl Viro 			return ERR_PTR(err);
16985a202bcdSAl Viro 	}
1699eead1911SChristoph Hellwig 
170049705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1701057f6c01SJames Morris }
1702057f6c01SJames Morris 
17032d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
17042d8f3038SAl Viro 		 struct path *path)
17051da177e4SLinus Torvalds {
17062d8f3038SAl Viro 	struct nameidata nd;
1707f52e0c11SAl Viro 	char *tmp = getname_flags(name, flags);
17081da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
17091da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
17102d8f3038SAl Viro 
17112d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
17122d8f3038SAl Viro 
17132d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
17141da177e4SLinus Torvalds 		putname(tmp);
17152d8f3038SAl Viro 		if (!err)
17162d8f3038SAl Viro 			*path = nd.path;
17171da177e4SLinus Torvalds 	}
17181da177e4SLinus Torvalds 	return err;
17191da177e4SLinus Torvalds }
17201da177e4SLinus Torvalds 
17212ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
17222ad94ae6SAl Viro 			struct nameidata *nd, char **name)
17232ad94ae6SAl Viro {
17242ad94ae6SAl Viro 	char *s = getname(path);
17252ad94ae6SAl Viro 	int error;
17262ad94ae6SAl Viro 
17272ad94ae6SAl Viro 	if (IS_ERR(s))
17282ad94ae6SAl Viro 		return PTR_ERR(s);
17292ad94ae6SAl Viro 
17302ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
17312ad94ae6SAl Viro 	if (error)
17322ad94ae6SAl Viro 		putname(s);
17332ad94ae6SAl Viro 	else
17342ad94ae6SAl Viro 		*name = s;
17352ad94ae6SAl Viro 
17362ad94ae6SAl Viro 	return error;
17372ad94ae6SAl Viro }
17382ad94ae6SAl Viro 
17391da177e4SLinus Torvalds /*
17401da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
17411da177e4SLinus Torvalds  * minimal.
17421da177e4SLinus Torvalds  */
17431da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
17441da177e4SLinus Torvalds {
1745da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1746da9592edSDavid Howells 
17471da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
17481da177e4SLinus Torvalds 		return 0;
1749e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
1750e795b717SSerge E. Hallyn 		goto other_userns;
1751da9592edSDavid Howells 	if (inode->i_uid == fsuid)
17521da177e4SLinus Torvalds 		return 0;
1753da9592edSDavid Howells 	if (dir->i_uid == fsuid)
17541da177e4SLinus Torvalds 		return 0;
1755e795b717SSerge E. Hallyn 
1756e795b717SSerge E. Hallyn other_userns:
1757e795b717SSerge E. Hallyn 	return !ns_capable(inode_userns(inode), CAP_FOWNER);
17581da177e4SLinus Torvalds }
17591da177e4SLinus Torvalds 
17601da177e4SLinus Torvalds /*
17611da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
17621da177e4SLinus Torvalds  *  whether the type of victim is right.
17631da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
17641da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
17651da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
17661da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
17671da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
17681da177e4SLinus Torvalds  *	a. be owner of dir, or
17691da177e4SLinus Torvalds  *	b. be owner of victim, or
17701da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
17711da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
17721da177e4SLinus Torvalds  *     links pointing to it.
17731da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
17741da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
17751da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
17761da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
17771da177e4SLinus Torvalds  *     nfs_async_unlink().
17781da177e4SLinus Torvalds  */
1779858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
17801da177e4SLinus Torvalds {
17811da177e4SLinus Torvalds 	int error;
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds 	if (!victim->d_inode)
17841da177e4SLinus Torvalds 		return -ENOENT;
17851da177e4SLinus Torvalds 
17861da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1787cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
17881da177e4SLinus Torvalds 
1789f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
17901da177e4SLinus Torvalds 	if (error)
17911da177e4SLinus Torvalds 		return error;
17921da177e4SLinus Torvalds 	if (IS_APPEND(dir))
17931da177e4SLinus Torvalds 		return -EPERM;
17941da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1795f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
17961da177e4SLinus Torvalds 		return -EPERM;
17971da177e4SLinus Torvalds 	if (isdir) {
17981da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
17991da177e4SLinus Torvalds 			return -ENOTDIR;
18001da177e4SLinus Torvalds 		if (IS_ROOT(victim))
18011da177e4SLinus Torvalds 			return -EBUSY;
18021da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
18031da177e4SLinus Torvalds 		return -EISDIR;
18041da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18051da177e4SLinus Torvalds 		return -ENOENT;
18061da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
18071da177e4SLinus Torvalds 		return -EBUSY;
18081da177e4SLinus Torvalds 	return 0;
18091da177e4SLinus Torvalds }
18101da177e4SLinus Torvalds 
18111da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
18121da177e4SLinus Torvalds  *  dir.
18131da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
18141da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
18151da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
18161da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
18171da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
18181da177e4SLinus Torvalds  */
1819a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
18201da177e4SLinus Torvalds {
18211da177e4SLinus Torvalds 	if (child->d_inode)
18221da177e4SLinus Torvalds 		return -EEXIST;
18231da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18241da177e4SLinus Torvalds 		return -ENOENT;
1825f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
18261da177e4SLinus Torvalds }
18271da177e4SLinus Torvalds 
18281da177e4SLinus Torvalds /*
18291da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
18301da177e4SLinus Torvalds  */
18311da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
18321da177e4SLinus Torvalds {
18331da177e4SLinus Torvalds 	struct dentry *p;
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds 	if (p1 == p2) {
1836f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
18371da177e4SLinus Torvalds 		return NULL;
18381da177e4SLinus Torvalds 	}
18391da177e4SLinus Torvalds 
1840a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
18411da177e4SLinus Torvalds 
1842e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1843e2761a11SOGAWA Hirofumi 	if (p) {
1844f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1845f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
18461da177e4SLinus Torvalds 		return p;
18471da177e4SLinus Torvalds 	}
18481da177e4SLinus Torvalds 
1849e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1850e2761a11SOGAWA Hirofumi 	if (p) {
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 p;
18541da177e4SLinus Torvalds 	}
18551da177e4SLinus Torvalds 
1856f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1857f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
18581da177e4SLinus Torvalds 	return NULL;
18591da177e4SLinus Torvalds }
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
18621da177e4SLinus Torvalds {
18631b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
18641da177e4SLinus Torvalds 	if (p1 != p2) {
18651b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1866a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
18671da177e4SLinus Torvalds 	}
18681da177e4SLinus Torvalds }
18691da177e4SLinus Torvalds 
18701da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
18711da177e4SLinus Torvalds 		struct nameidata *nd)
18721da177e4SLinus Torvalds {
1873a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
18741da177e4SLinus Torvalds 
18751da177e4SLinus Torvalds 	if (error)
18761da177e4SLinus Torvalds 		return error;
18771da177e4SLinus Torvalds 
1878acfa4380SAl Viro 	if (!dir->i_op->create)
18791da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
18801da177e4SLinus Torvalds 	mode &= S_IALLUGO;
18811da177e4SLinus Torvalds 	mode |= S_IFREG;
18821da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
18831da177e4SLinus Torvalds 	if (error)
18841da177e4SLinus Torvalds 		return error;
18851da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1886a74574aaSStephen Smalley 	if (!error)
1887f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
18881da177e4SLinus Torvalds 	return error;
18891da177e4SLinus Torvalds }
18901da177e4SLinus Torvalds 
189173d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
18921da177e4SLinus Torvalds {
18933fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
18941da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
18951da177e4SLinus Torvalds 	int error;
18961da177e4SLinus Torvalds 
1897bcda7652SAl Viro 	/* O_PATH? */
1898bcda7652SAl Viro 	if (!acc_mode)
1899bcda7652SAl Viro 		return 0;
1900bcda7652SAl Viro 
19011da177e4SLinus Torvalds 	if (!inode)
19021da177e4SLinus Torvalds 		return -ENOENT;
19031da177e4SLinus Torvalds 
1904c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1905c8fe8f30SChristoph Hellwig 	case S_IFLNK:
19061da177e4SLinus Torvalds 		return -ELOOP;
1907c8fe8f30SChristoph Hellwig 	case S_IFDIR:
1908c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
19091da177e4SLinus Torvalds 			return -EISDIR;
1910c8fe8f30SChristoph Hellwig 		break;
1911c8fe8f30SChristoph Hellwig 	case S_IFBLK:
1912c8fe8f30SChristoph Hellwig 	case S_IFCHR:
19133fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
19141da177e4SLinus Torvalds 			return -EACCES;
1915c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
1916c8fe8f30SChristoph Hellwig 	case S_IFIFO:
1917c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
19181da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
1919c8fe8f30SChristoph Hellwig 		break;
19204a3fd211SDave Hansen 	}
1921b41572e9SDave Hansen 
19223fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
1923b41572e9SDave Hansen 	if (error)
1924b41572e9SDave Hansen 		return error;
19256146f0d5SMimi Zohar 
19261da177e4SLinus Torvalds 	/*
19271da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
19281da177e4SLinus Torvalds 	 */
19291da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
19308737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
19317715b521SAl Viro 			return -EPERM;
19321da177e4SLinus Torvalds 		if (flag & O_TRUNC)
19337715b521SAl Viro 			return -EPERM;
19341da177e4SLinus Torvalds 	}
19351da177e4SLinus Torvalds 
19361da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
19372e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
19387715b521SAl Viro 		return -EPERM;
19391da177e4SLinus Torvalds 
19401da177e4SLinus Torvalds 	/*
19411da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
19421da177e4SLinus Torvalds 	 */
1943b65a9cfcSAl Viro 	return break_lease(inode, flag);
19447715b521SAl Viro }
19457715b521SAl Viro 
1946e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
19477715b521SAl Viro {
1948e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
19497715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
19507715b521SAl Viro 	int error = get_write_access(inode);
19511da177e4SLinus Torvalds 	if (error)
19527715b521SAl Viro 		return error;
19531da177e4SLinus Torvalds 	/*
19541da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
19551da177e4SLinus Torvalds 	 */
19561da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
1957be6d3e56SKentaro Takeda 	if (!error)
1958ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
19591da177e4SLinus Torvalds 	if (!error) {
19607715b521SAl Viro 		error = do_truncate(path->dentry, 0,
1961d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1962e1181ee6SJeff Layton 				    filp);
19631da177e4SLinus Torvalds 	}
19641da177e4SLinus Torvalds 	put_write_access(inode);
1965acd0c935SMimi Zohar 	return error;
19661da177e4SLinus Torvalds }
19671da177e4SLinus Torvalds 
1968d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
1969d57999e1SDave Hansen {
19708a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
19718a5e929dSAl Viro 		flag--;
1972d57999e1SDave Hansen 	return flag;
1973d57999e1SDave Hansen }
1974d57999e1SDave Hansen 
197531e6b01fSNick Piggin /*
1976fe2d35ffSAl Viro  * Handle the last step of open()
197731e6b01fSNick Piggin  */
1978fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
1979c3e380b0SAl Viro 			    const struct open_flags *op, const char *pathname)
1980fb1cc555SAl Viro {
1981a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
19826c0d46c4SAl Viro 	struct dentry *dentry;
1983ca344a89SAl Viro 	int open_flag = op->open_flag;
19846c0d46c4SAl Viro 	int will_truncate = open_flag & O_TRUNC;
1985ca344a89SAl Viro 	int want_write = 0;
1986bcda7652SAl Viro 	int acc_mode = op->acc_mode;
1987fb1cc555SAl Viro 	struct file *filp;
198816c2cd71SAl Viro 	int error;
1989fb1cc555SAl Viro 
1990c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1991c3e380b0SAl Viro 	nd->flags |= op->intent;
1992c3e380b0SAl Viro 
19931f36f774SAl Viro 	switch (nd->last_type) {
19941f36f774SAl Viro 	case LAST_DOTDOT:
1995176306f5SNeil Brown 	case LAST_DOT:
1996fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
1997fe2d35ffSAl Viro 		if (error)
1998fe2d35ffSAl Viro 			return ERR_PTR(error);
19991f36f774SAl Viro 		/* fallthrough */
20001f36f774SAl Viro 	case LAST_ROOT:
20019f1fafeeSAl Viro 		error = complete_walk(nd);
200216c2cd71SAl Viro 		if (error)
20039f1fafeeSAl Viro 			return ERR_PTR(error);
2004fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2005ca344a89SAl Viro 		if (open_flag & O_CREAT) {
200616c2cd71SAl Viro 			error = -EISDIR;
20071f36f774SAl Viro 			goto exit;
2008fe2d35ffSAl Viro 		}
2009fe2d35ffSAl Viro 		goto ok;
20101f36f774SAl Viro 	case LAST_BIND:
20119f1fafeeSAl Viro 		error = complete_walk(nd);
201216c2cd71SAl Viro 		if (error)
20139f1fafeeSAl Viro 			return ERR_PTR(error);
20141f36f774SAl Viro 		audit_inode(pathname, dir);
20151f36f774SAl Viro 		goto ok;
20161f36f774SAl Viro 	}
2017a2c36b45SAl Viro 
2018ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2019bcda7652SAl Viro 		int symlink_ok = 0;
2020fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2021fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2022bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2023bcda7652SAl Viro 			symlink_ok = 1;
2024fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2025ce57dfc1SAl Viro 		error = walk_component(nd, path, &nd->last, LAST_NORM,
2026ce57dfc1SAl Viro 					!symlink_ok);
2027ce57dfc1SAl Viro 		if (error < 0)
2028fe2d35ffSAl Viro 			return ERR_PTR(error);
2029ce57dfc1SAl Viro 		if (error) /* symlink */
2030fe2d35ffSAl Viro 			return NULL;
2031fe2d35ffSAl Viro 		/* sayonara */
20329f1fafeeSAl Viro 		error = complete_walk(nd);
20339f1fafeeSAl Viro 		if (error)
2034fe2d35ffSAl Viro 			return ERR_PTR(-ECHILD);
2035fe2d35ffSAl Viro 
2036fe2d35ffSAl Viro 		error = -ENOTDIR;
2037fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_DIRECTORY) {
2038ce57dfc1SAl Viro 			if (!nd->inode->i_op->lookup)
2039fe2d35ffSAl Viro 				goto exit;
2040fe2d35ffSAl Viro 		}
2041fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2042fe2d35ffSAl Viro 		goto ok;
2043fe2d35ffSAl Viro 	}
2044fe2d35ffSAl Viro 
2045fe2d35ffSAl Viro 	/* create side of things */
20469f1fafeeSAl Viro 	error = complete_walk(nd);
20479f1fafeeSAl Viro 	if (error)
20489f1fafeeSAl Viro 		return ERR_PTR(error);
2049fe2d35ffSAl Viro 
2050fe2d35ffSAl Viro 	audit_inode(pathname, dir);
205116c2cd71SAl Viro 	error = -EISDIR;
20521f36f774SAl Viro 	/* trailing slashes? */
205331e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
20541f36f774SAl Viro 		goto exit;
20551f36f774SAl Viro 
2056a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2057a1e28038SAl Viro 
20586c0d46c4SAl Viro 	dentry = lookup_hash(nd);
20596c0d46c4SAl Viro 	error = PTR_ERR(dentry);
20606c0d46c4SAl Viro 	if (IS_ERR(dentry)) {
2061fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2062fb1cc555SAl Viro 		goto exit;
2063fb1cc555SAl Viro 	}
2064fb1cc555SAl Viro 
20656c0d46c4SAl Viro 	path->dentry = dentry;
20666c0d46c4SAl Viro 	path->mnt = nd->path.mnt;
20676c0d46c4SAl Viro 
2068fb1cc555SAl Viro 	/* Negative dentry, just create the file */
20696c0d46c4SAl Viro 	if (!dentry->d_inode) {
20706c0d46c4SAl Viro 		int mode = op->mode;
20716c0d46c4SAl Viro 		if (!IS_POSIXACL(dir->d_inode))
20726c0d46c4SAl Viro 			mode &= ~current_umask();
2073fb1cc555SAl Viro 		/*
2074fb1cc555SAl Viro 		 * This write is needed to ensure that a
20756c0d46c4SAl Viro 		 * rw->ro transition does not occur between
2076fb1cc555SAl Viro 		 * the time when the file is created and when
2077fb1cc555SAl Viro 		 * a permanent write count is taken through
2078fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2079fb1cc555SAl Viro 		 */
2080fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2081fb1cc555SAl Viro 		if (error)
2082fb1cc555SAl Viro 			goto exit_mutex_unlock;
2083ca344a89SAl Viro 		want_write = 1;
20849b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2085ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
20866c0d46c4SAl Viro 		will_truncate = 0;
2087bcda7652SAl Viro 		acc_mode = MAY_OPEN;
20886c0d46c4SAl Viro 		error = security_path_mknod(&nd->path, dentry, mode, 0);
20896c0d46c4SAl Viro 		if (error)
20906c0d46c4SAl Viro 			goto exit_mutex_unlock;
20916c0d46c4SAl Viro 		error = vfs_create(dir->d_inode, dentry, mode, nd);
20926c0d46c4SAl Viro 		if (error)
20936c0d46c4SAl Viro 			goto exit_mutex_unlock;
20946c0d46c4SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
20956c0d46c4SAl Viro 		dput(nd->path.dentry);
20966c0d46c4SAl Viro 		nd->path.dentry = dentry;
2097ca344a89SAl Viro 		goto common;
2098fb1cc555SAl Viro 	}
2099fb1cc555SAl Viro 
2100fb1cc555SAl Viro 	/*
2101fb1cc555SAl Viro 	 * It already exists.
2102fb1cc555SAl Viro 	 */
2103fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2104fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2105fb1cc555SAl Viro 
2106fb1cc555SAl Viro 	error = -EEXIST;
2107ca344a89SAl Viro 	if (open_flag & O_EXCL)
2108fb1cc555SAl Viro 		goto exit_dput;
2109fb1cc555SAl Viro 
21109875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
21119875cf80SDavid Howells 	if (error < 0)
2112fb1cc555SAl Viro 		goto exit_dput;
2113fb1cc555SAl Viro 
2114fb1cc555SAl Viro 	error = -ENOENT;
2115fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2116fb1cc555SAl Viro 		goto exit_dput;
21179e67f361SAl Viro 
21189e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2119fb1cc555SAl Viro 		return NULL;
2120fb1cc555SAl Viro 
2121fb1cc555SAl Viro 	path_to_nameidata(path, nd);
212231e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2123fb1cc555SAl Viro 	error = -EISDIR;
212431e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2125fb1cc555SAl Viro 		goto exit;
212667ee3ad2SAl Viro ok:
21276c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
21286c0d46c4SAl Viro 		will_truncate = 0;
21296c0d46c4SAl Viro 
21300f9d1a10SAl Viro 	if (will_truncate) {
21310f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
21320f9d1a10SAl Viro 		if (error)
21330f9d1a10SAl Viro 			goto exit;
2134ca344a89SAl Viro 		want_write = 1;
21350f9d1a10SAl Viro 	}
2136ca344a89SAl Viro common:
2137bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2138ca344a89SAl Viro 	if (error)
21390f9d1a10SAl Viro 		goto exit;
21400f9d1a10SAl Viro 	filp = nameidata_to_filp(nd);
21410f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
21420f9d1a10SAl Viro 		error = ima_file_check(filp, op->acc_mode);
21430f9d1a10SAl Viro 		if (error) {
21440f9d1a10SAl Viro 			fput(filp);
21450f9d1a10SAl Viro 			filp = ERR_PTR(error);
21460f9d1a10SAl Viro 		}
21470f9d1a10SAl Viro 	}
21480f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
21490f9d1a10SAl Viro 		if (will_truncate) {
21500f9d1a10SAl Viro 			error = handle_truncate(filp);
21510f9d1a10SAl Viro 			if (error) {
21520f9d1a10SAl Viro 				fput(filp);
21530f9d1a10SAl Viro 				filp = ERR_PTR(error);
21540f9d1a10SAl Viro 			}
21550f9d1a10SAl Viro 		}
21560f9d1a10SAl Viro 	}
2157ca344a89SAl Viro out:
2158ca344a89SAl Viro 	if (want_write)
21590f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
21600f9d1a10SAl Viro 	path_put(&nd->path);
2161fb1cc555SAl Viro 	return filp;
2162fb1cc555SAl Viro 
2163fb1cc555SAl Viro exit_mutex_unlock:
2164fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2165fb1cc555SAl Viro exit_dput:
2166fb1cc555SAl Viro 	path_put_conditional(path, nd);
2167fb1cc555SAl Viro exit:
2168ca344a89SAl Viro 	filp = ERR_PTR(error);
2169ca344a89SAl Viro 	goto out;
2170fb1cc555SAl Viro }
2171fb1cc555SAl Viro 
217213aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
217373d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
21741da177e4SLinus Torvalds {
2175fe2d35ffSAl Viro 	struct file *base = NULL;
21764a3fd211SDave Hansen 	struct file *filp;
21779850c056SAl Viro 	struct path path;
217813aab428SAl Viro 	int error;
217931e6b01fSNick Piggin 
218031e6b01fSNick Piggin 	filp = get_empty_filp();
218131e6b01fSNick Piggin 	if (!filp)
218231e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
218331e6b01fSNick Piggin 
218447c805dcSAl Viro 	filp->f_flags = op->open_flag;
218573d049a4SAl Viro 	nd->intent.open.file = filp;
218673d049a4SAl Viro 	nd->intent.open.flags = open_to_namei_flags(op->open_flag);
218773d049a4SAl Viro 	nd->intent.open.create_mode = op->mode;
218831e6b01fSNick Piggin 
218973d049a4SAl Viro 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
219031e6b01fSNick Piggin 	if (unlikely(error))
219113aab428SAl Viro 		goto out_filp;
219231e6b01fSNick Piggin 
2193fe2d35ffSAl Viro 	current->total_link_count = 0;
219473d049a4SAl Viro 	error = link_path_walk(pathname, nd);
219531e6b01fSNick Piggin 	if (unlikely(error))
219631e6b01fSNick Piggin 		goto out_filp;
21971da177e4SLinus Torvalds 
219873d049a4SAl Viro 	filp = do_last(nd, &path, op, pathname);
2199806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
22007b9337aaSNick Piggin 		struct path link = path;
2201def4af30SAl Viro 		void *cookie;
2202574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
220373d049a4SAl Viro 			path_put_conditional(&path, nd);
220473d049a4SAl Viro 			path_put(&nd->path);
220540b39136SAl Viro 			filp = ERR_PTR(-ELOOP);
220640b39136SAl Viro 			break;
220740b39136SAl Viro 		}
220873d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
220973d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2210574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
2211c3e380b0SAl Viro 		if (unlikely(error))
2212f1afe9efSAl Viro 			filp = ERR_PTR(error);
2213c3e380b0SAl Viro 		else
221473d049a4SAl Viro 			filp = do_last(nd, &path, op, pathname);
2215574197e0SAl Viro 		put_link(nd, &link, cookie);
2216806b681cSAl Viro 	}
221710fa8e62SAl Viro out:
221873d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
221973d049a4SAl Viro 		path_put(&nd->root);
2220fe2d35ffSAl Viro 	if (base)
2221fe2d35ffSAl Viro 		fput(base);
222273d049a4SAl Viro 	release_open_intent(nd);
222310fa8e62SAl Viro 	return filp;
22241da177e4SLinus Torvalds 
222531e6b01fSNick Piggin out_filp:
222610fa8e62SAl Viro 	filp = ERR_PTR(error);
222710fa8e62SAl Viro 	goto out;
2228de459215SKirill Korotaev }
22291da177e4SLinus Torvalds 
223013aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
223113aab428SAl Viro 		const struct open_flags *op, int flags)
223213aab428SAl Viro {
223373d049a4SAl Viro 	struct nameidata nd;
223413aab428SAl Viro 	struct file *filp;
223513aab428SAl Viro 
223673d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
223713aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
223873d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
223913aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
224073d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
224113aab428SAl Viro 	return filp;
224213aab428SAl Viro }
224313aab428SAl Viro 
224473d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
224573d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
224673d049a4SAl Viro {
224773d049a4SAl Viro 	struct nameidata nd;
224873d049a4SAl Viro 	struct file *file;
224973d049a4SAl Viro 
225073d049a4SAl Viro 	nd.root.mnt = mnt;
225173d049a4SAl Viro 	nd.root.dentry = dentry;
225273d049a4SAl Viro 
225373d049a4SAl Viro 	flags |= LOOKUP_ROOT;
225473d049a4SAl Viro 
2255bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
225673d049a4SAl Viro 		return ERR_PTR(-ELOOP);
225773d049a4SAl Viro 
225873d049a4SAl Viro 	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
225973d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
226073d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags);
226173d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
226273d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
226373d049a4SAl Viro 	return file;
226473d049a4SAl Viro }
226573d049a4SAl Viro 
22661da177e4SLinus Torvalds /**
22671da177e4SLinus Torvalds  * lookup_create - lookup a dentry, creating it if it doesn't exist
22681da177e4SLinus Torvalds  * @nd: nameidata info
22691da177e4SLinus Torvalds  * @is_dir: directory flag
22701da177e4SLinus Torvalds  *
22711da177e4SLinus Torvalds  * Simple function to lookup and return a dentry and create it
22721da177e4SLinus Torvalds  * if it doesn't exist.  Is SMP-safe.
2273c663e5d8SChristoph Hellwig  *
22744ac91378SJan Blunck  * Returns with nd->path.dentry->d_inode->i_mutex locked.
22751da177e4SLinus Torvalds  */
22761da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir)
22771da177e4SLinus Torvalds {
2278c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
22791da177e4SLinus Torvalds 
22804ac91378SJan Blunck 	mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2281c663e5d8SChristoph Hellwig 	/*
2282c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2283c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2284c663e5d8SChristoph Hellwig 	 */
22851da177e4SLinus Torvalds 	if (nd->last_type != LAST_NORM)
22861da177e4SLinus Torvalds 		goto fail;
22871da177e4SLinus Torvalds 	nd->flags &= ~LOOKUP_PARENT;
22883516586aSAl Viro 	nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2289a634904aSASANO Masahiro 	nd->intent.open.flags = O_EXCL;
2290c663e5d8SChristoph Hellwig 
2291c663e5d8SChristoph Hellwig 	/*
2292c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2293c663e5d8SChristoph Hellwig 	 */
229449705b77SChristoph Hellwig 	dentry = lookup_hash(nd);
22951da177e4SLinus Torvalds 	if (IS_ERR(dentry))
22961da177e4SLinus Torvalds 		goto fail;
2297c663e5d8SChristoph Hellwig 
2298e9baf6e5SAl Viro 	if (dentry->d_inode)
2299e9baf6e5SAl Viro 		goto eexist;
2300c663e5d8SChristoph Hellwig 	/*
2301c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2302c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2303c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2304c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2305c663e5d8SChristoph Hellwig 	 */
2306e9baf6e5SAl Viro 	if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
23071da177e4SLinus Torvalds 		dput(dentry);
23081da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2309e9baf6e5SAl Viro 	}
2310e9baf6e5SAl Viro 	return dentry;
2311e9baf6e5SAl Viro eexist:
2312e9baf6e5SAl Viro 	dput(dentry);
2313e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
23141da177e4SLinus Torvalds fail:
23151da177e4SLinus Torvalds 	return dentry;
23161da177e4SLinus Torvalds }
2317f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create);
23181da177e4SLinus Torvalds 
23191da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
23201da177e4SLinus Torvalds {
2321a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
23221da177e4SLinus Torvalds 
23231da177e4SLinus Torvalds 	if (error)
23241da177e4SLinus Torvalds 		return error;
23251da177e4SLinus Torvalds 
2326e795b717SSerge E. Hallyn 	if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2327e795b717SSerge E. Hallyn 	    !ns_capable(inode_userns(dir), CAP_MKNOD))
23281da177e4SLinus Torvalds 		return -EPERM;
23291da177e4SLinus Torvalds 
2330acfa4380SAl Viro 	if (!dir->i_op->mknod)
23311da177e4SLinus Torvalds 		return -EPERM;
23321da177e4SLinus Torvalds 
233308ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
233408ce5f16SSerge E. Hallyn 	if (error)
233508ce5f16SSerge E. Hallyn 		return error;
233608ce5f16SSerge E. Hallyn 
23371da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
23381da177e4SLinus Torvalds 	if (error)
23391da177e4SLinus Torvalds 		return error;
23401da177e4SLinus Torvalds 
23411da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2342a74574aaSStephen Smalley 	if (!error)
2343f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
23441da177e4SLinus Torvalds 	return error;
23451da177e4SLinus Torvalds }
23461da177e4SLinus Torvalds 
2347463c3197SDave Hansen static int may_mknod(mode_t mode)
2348463c3197SDave Hansen {
2349463c3197SDave Hansen 	switch (mode & S_IFMT) {
2350463c3197SDave Hansen 	case S_IFREG:
2351463c3197SDave Hansen 	case S_IFCHR:
2352463c3197SDave Hansen 	case S_IFBLK:
2353463c3197SDave Hansen 	case S_IFIFO:
2354463c3197SDave Hansen 	case S_IFSOCK:
2355463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2356463c3197SDave Hansen 		return 0;
2357463c3197SDave Hansen 	case S_IFDIR:
2358463c3197SDave Hansen 		return -EPERM;
2359463c3197SDave Hansen 	default:
2360463c3197SDave Hansen 		return -EINVAL;
2361463c3197SDave Hansen 	}
2362463c3197SDave Hansen }
2363463c3197SDave Hansen 
23642e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
23652e4d0924SHeiko Carstens 		unsigned, dev)
23661da177e4SLinus Torvalds {
23672ad94ae6SAl Viro 	int error;
23681da177e4SLinus Torvalds 	char *tmp;
23691da177e4SLinus Torvalds 	struct dentry *dentry;
23701da177e4SLinus Torvalds 	struct nameidata nd;
23711da177e4SLinus Torvalds 
23721da177e4SLinus Torvalds 	if (S_ISDIR(mode))
23731da177e4SLinus Torvalds 		return -EPERM;
23741da177e4SLinus Torvalds 
23752ad94ae6SAl Viro 	error = user_path_parent(dfd, filename, &nd, &tmp);
23761da177e4SLinus Torvalds 	if (error)
23772ad94ae6SAl Viro 		return error;
23782ad94ae6SAl Viro 
23791da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
2380463c3197SDave Hansen 	if (IS_ERR(dentry)) {
23811da177e4SLinus Torvalds 		error = PTR_ERR(dentry);
2382463c3197SDave Hansen 		goto out_unlock;
2383463c3197SDave Hansen 	}
23844ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2385ce3b0f8dSAl Viro 		mode &= ~current_umask();
2386463c3197SDave Hansen 	error = may_mknod(mode);
2387463c3197SDave Hansen 	if (error)
2388463c3197SDave Hansen 		goto out_dput;
2389463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2390463c3197SDave Hansen 	if (error)
2391463c3197SDave Hansen 		goto out_dput;
2392be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd.path, dentry, mode, dev);
2393be6d3e56SKentaro Takeda 	if (error)
2394be6d3e56SKentaro Takeda 		goto out_drop_write;
23951da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
23961da177e4SLinus Torvalds 		case 0: case S_IFREG:
2397554a8b9fSAl Viro 			error = vfs_create(nd.path.dentry->d_inode,dentry,mode,NULL);
23981da177e4SLinus Torvalds 			break;
23991da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
24004ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
24011da177e4SLinus Torvalds 					new_decode_dev(dev));
24021da177e4SLinus Torvalds 			break;
24031da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
24044ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
24051da177e4SLinus Torvalds 			break;
24061da177e4SLinus Torvalds 	}
2407be6d3e56SKentaro Takeda out_drop_write:
2408463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2409463c3197SDave Hansen out_dput:
24101da177e4SLinus Torvalds 	dput(dentry);
2411463c3197SDave Hansen out_unlock:
24124ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
24131d957f9bSJan Blunck 	path_put(&nd.path);
24141da177e4SLinus Torvalds 	putname(tmp);
24151da177e4SLinus Torvalds 
24161da177e4SLinus Torvalds 	return error;
24171da177e4SLinus Torvalds }
24181da177e4SLinus Torvalds 
24193480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
24205590ff0dSUlrich Drepper {
24215590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
24225590ff0dSUlrich Drepper }
24235590ff0dSUlrich Drepper 
24241da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
24251da177e4SLinus Torvalds {
2426a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
24271da177e4SLinus Torvalds 
24281da177e4SLinus Torvalds 	if (error)
24291da177e4SLinus Torvalds 		return error;
24301da177e4SLinus Torvalds 
2431acfa4380SAl Viro 	if (!dir->i_op->mkdir)
24321da177e4SLinus Torvalds 		return -EPERM;
24331da177e4SLinus Torvalds 
24341da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
24351da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
24361da177e4SLinus Torvalds 	if (error)
24371da177e4SLinus Torvalds 		return error;
24381da177e4SLinus Torvalds 
24391da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2440a74574aaSStephen Smalley 	if (!error)
2441f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
24421da177e4SLinus Torvalds 	return error;
24431da177e4SLinus Torvalds }
24441da177e4SLinus Torvalds 
24452e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
24461da177e4SLinus Torvalds {
24471da177e4SLinus Torvalds 	int error = 0;
24481da177e4SLinus Torvalds 	char * tmp;
24496902d925SDave Hansen 	struct dentry *dentry;
24506902d925SDave Hansen 	struct nameidata nd;
24511da177e4SLinus Torvalds 
24522ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &tmp);
24532ad94ae6SAl Viro 	if (error)
24546902d925SDave Hansen 		goto out_err;
24551da177e4SLinus Torvalds 
24561da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 1);
24571da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
24586902d925SDave Hansen 	if (IS_ERR(dentry))
24596902d925SDave Hansen 		goto out_unlock;
24606902d925SDave Hansen 
24614ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2462ce3b0f8dSAl Viro 		mode &= ~current_umask();
2463463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2464463c3197SDave Hansen 	if (error)
2465463c3197SDave Hansen 		goto out_dput;
2466be6d3e56SKentaro Takeda 	error = security_path_mkdir(&nd.path, dentry, mode);
2467be6d3e56SKentaro Takeda 	if (error)
2468be6d3e56SKentaro Takeda 		goto out_drop_write;
24694ac91378SJan Blunck 	error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2470be6d3e56SKentaro Takeda out_drop_write:
2471463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2472463c3197SDave Hansen out_dput:
24731da177e4SLinus Torvalds 	dput(dentry);
24746902d925SDave Hansen out_unlock:
24754ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
24761d957f9bSJan Blunck 	path_put(&nd.path);
24771da177e4SLinus Torvalds 	putname(tmp);
24786902d925SDave Hansen out_err:
24791da177e4SLinus Torvalds 	return error;
24801da177e4SLinus Torvalds }
24811da177e4SLinus Torvalds 
24823cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
24835590ff0dSUlrich Drepper {
24845590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
24855590ff0dSUlrich Drepper }
24865590ff0dSUlrich Drepper 
24871da177e4SLinus Torvalds /*
2488a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
2489a71905f0SSage Weil  * should have a usage count of 2 if we're the only user of this
2490a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
2491a71905f0SSage Weil  * then we drop the dentry now.
24921da177e4SLinus Torvalds  *
24931da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
24941da177e4SLinus Torvalds  * do a
24951da177e4SLinus Torvalds  *
24961da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
24971da177e4SLinus Torvalds  *		return -EBUSY;
24981da177e4SLinus Torvalds  *
24991da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
25001da177e4SLinus Torvalds  * that is still in use by something else..
25011da177e4SLinus Torvalds  */
25021da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
25031da177e4SLinus Torvalds {
25041da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
25051da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
250664252c75SSage Weil 	if (dentry->d_count == 1)
25071da177e4SLinus Torvalds 		__d_drop(dentry);
25081da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
25091da177e4SLinus Torvalds }
25101da177e4SLinus Torvalds 
25111da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
25121da177e4SLinus Torvalds {
25131da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
25141da177e4SLinus Torvalds 
25151da177e4SLinus Torvalds 	if (error)
25161da177e4SLinus Torvalds 		return error;
25171da177e4SLinus Torvalds 
2518acfa4380SAl Viro 	if (!dir->i_op->rmdir)
25191da177e4SLinus Torvalds 		return -EPERM;
25201da177e4SLinus Torvalds 
25211b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
2522912dbc15SSage Weil 
25231da177e4SLinus Torvalds 	error = -EBUSY;
2524912dbc15SSage Weil 	if (d_mountpoint(dentry))
2525912dbc15SSage Weil 		goto out;
2526912dbc15SSage Weil 
25271da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
2528912dbc15SSage Weil 	if (error)
2529912dbc15SSage Weil 		goto out;
2530912dbc15SSage Weil 
25313cebde24SSage Weil 	shrink_dcache_parent(dentry);
25321da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
2533912dbc15SSage Weil 	if (error)
2534912dbc15SSage Weil 		goto out;
2535912dbc15SSage Weil 
25361da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
2537d83c49f3SAl Viro 	dont_mount(dentry);
25381da177e4SLinus Torvalds 
2539912dbc15SSage Weil out:
2540912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
2541912dbc15SSage Weil 	if (!error)
2542912dbc15SSage Weil 		d_delete(dentry);
25431da177e4SLinus Torvalds 	return error;
25441da177e4SLinus Torvalds }
25451da177e4SLinus Torvalds 
25465590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
25471da177e4SLinus Torvalds {
25481da177e4SLinus Torvalds 	int error = 0;
25491da177e4SLinus Torvalds 	char * name;
25501da177e4SLinus Torvalds 	struct dentry *dentry;
25511da177e4SLinus Torvalds 	struct nameidata nd;
25521da177e4SLinus Torvalds 
25532ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
25541da177e4SLinus Torvalds 	if (error)
25552ad94ae6SAl Viro 		return error;
25561da177e4SLinus Torvalds 
25571da177e4SLinus Torvalds 	switch(nd.last_type) {
25581da177e4SLinus Torvalds 	case LAST_DOTDOT:
25591da177e4SLinus Torvalds 		error = -ENOTEMPTY;
25601da177e4SLinus Torvalds 		goto exit1;
25611da177e4SLinus Torvalds 	case LAST_DOT:
25621da177e4SLinus Torvalds 		error = -EINVAL;
25631da177e4SLinus Torvalds 		goto exit1;
25641da177e4SLinus Torvalds 	case LAST_ROOT:
25651da177e4SLinus Torvalds 		error = -EBUSY;
25661da177e4SLinus Torvalds 		goto exit1;
25671da177e4SLinus Torvalds 	}
25680612d9fbSOGAWA Hirofumi 
25690612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
25700612d9fbSOGAWA Hirofumi 
25714ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
257249705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
25731da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
25746902d925SDave Hansen 	if (IS_ERR(dentry))
25756902d925SDave Hansen 		goto exit2;
2576e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
2577e6bc45d6STheodore Ts'o 		error = -ENOENT;
2578e6bc45d6STheodore Ts'o 		goto exit3;
2579e6bc45d6STheodore Ts'o 	}
25800622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
25810622753bSDave Hansen 	if (error)
25820622753bSDave Hansen 		goto exit3;
2583be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2584be6d3e56SKentaro Takeda 	if (error)
2585be6d3e56SKentaro Takeda 		goto exit4;
25864ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2587be6d3e56SKentaro Takeda exit4:
25880622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
25890622753bSDave Hansen exit3:
25901da177e4SLinus Torvalds 	dput(dentry);
25916902d925SDave Hansen exit2:
25924ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
25931da177e4SLinus Torvalds exit1:
25941d957f9bSJan Blunck 	path_put(&nd.path);
25951da177e4SLinus Torvalds 	putname(name);
25961da177e4SLinus Torvalds 	return error;
25971da177e4SLinus Torvalds }
25981da177e4SLinus Torvalds 
25993cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
26005590ff0dSUlrich Drepper {
26015590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
26025590ff0dSUlrich Drepper }
26035590ff0dSUlrich Drepper 
26041da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
26051da177e4SLinus Torvalds {
26061da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
26071da177e4SLinus Torvalds 
26081da177e4SLinus Torvalds 	if (error)
26091da177e4SLinus Torvalds 		return error;
26101da177e4SLinus Torvalds 
2611acfa4380SAl Viro 	if (!dir->i_op->unlink)
26121da177e4SLinus Torvalds 		return -EPERM;
26131da177e4SLinus Torvalds 
26141b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
26151da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
26161da177e4SLinus Torvalds 		error = -EBUSY;
26171da177e4SLinus Torvalds 	else {
26181da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2619bec1052eSAl Viro 		if (!error) {
26201da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2621bec1052eSAl Viro 			if (!error)
2622d83c49f3SAl Viro 				dont_mount(dentry);
2623bec1052eSAl Viro 		}
26241da177e4SLinus Torvalds 	}
26251b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
26261da177e4SLinus Torvalds 
26271da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
26281da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2629ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
26301da177e4SLinus Torvalds 		d_delete(dentry);
26311da177e4SLinus Torvalds 	}
26320eeca283SRobert Love 
26331da177e4SLinus Torvalds 	return error;
26341da177e4SLinus Torvalds }
26351da177e4SLinus Torvalds 
26361da177e4SLinus Torvalds /*
26371da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
26381b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
26391da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
26401da177e4SLinus Torvalds  * while waiting on the I/O.
26411da177e4SLinus Torvalds  */
26425590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
26431da177e4SLinus Torvalds {
26442ad94ae6SAl Viro 	int error;
26451da177e4SLinus Torvalds 	char *name;
26461da177e4SLinus Torvalds 	struct dentry *dentry;
26471da177e4SLinus Torvalds 	struct nameidata nd;
26481da177e4SLinus Torvalds 	struct inode *inode = NULL;
26491da177e4SLinus Torvalds 
26502ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
26511da177e4SLinus Torvalds 	if (error)
26522ad94ae6SAl Viro 		return error;
26532ad94ae6SAl Viro 
26541da177e4SLinus Torvalds 	error = -EISDIR;
26551da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
26561da177e4SLinus Torvalds 		goto exit1;
26570612d9fbSOGAWA Hirofumi 
26580612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
26590612d9fbSOGAWA Hirofumi 
26604ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
266149705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
26621da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
26631da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
26641da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
266550338b88STörök Edwin 		if (nd.last.name[nd.last.len])
266650338b88STörök Edwin 			goto slashes;
26671da177e4SLinus Torvalds 		inode = dentry->d_inode;
266850338b88STörök Edwin 		if (!inode)
2669e6bc45d6STheodore Ts'o 			goto slashes;
26707de9c6eeSAl Viro 		ihold(inode);
26710622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
26720622753bSDave Hansen 		if (error)
26730622753bSDave Hansen 			goto exit2;
2674be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2675be6d3e56SKentaro Takeda 		if (error)
2676be6d3e56SKentaro Takeda 			goto exit3;
26774ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2678be6d3e56SKentaro Takeda exit3:
26790622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
26801da177e4SLinus Torvalds 	exit2:
26811da177e4SLinus Torvalds 		dput(dentry);
26821da177e4SLinus Torvalds 	}
26834ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
26841da177e4SLinus Torvalds 	if (inode)
26851da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
26861da177e4SLinus Torvalds exit1:
26871d957f9bSJan Blunck 	path_put(&nd.path);
26881da177e4SLinus Torvalds 	putname(name);
26891da177e4SLinus Torvalds 	return error;
26901da177e4SLinus Torvalds 
26911da177e4SLinus Torvalds slashes:
26921da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
26931da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
26941da177e4SLinus Torvalds 	goto exit2;
26951da177e4SLinus Torvalds }
26961da177e4SLinus Torvalds 
26972e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
26985590ff0dSUlrich Drepper {
26995590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
27005590ff0dSUlrich Drepper 		return -EINVAL;
27015590ff0dSUlrich Drepper 
27025590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
27035590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
27045590ff0dSUlrich Drepper 
27055590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
27065590ff0dSUlrich Drepper }
27075590ff0dSUlrich Drepper 
27083480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
27095590ff0dSUlrich Drepper {
27105590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
27115590ff0dSUlrich Drepper }
27125590ff0dSUlrich Drepper 
2713db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
27141da177e4SLinus Torvalds {
2715a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
27161da177e4SLinus Torvalds 
27171da177e4SLinus Torvalds 	if (error)
27181da177e4SLinus Torvalds 		return error;
27191da177e4SLinus Torvalds 
2720acfa4380SAl Viro 	if (!dir->i_op->symlink)
27211da177e4SLinus Torvalds 		return -EPERM;
27221da177e4SLinus Torvalds 
27231da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
27241da177e4SLinus Torvalds 	if (error)
27251da177e4SLinus Torvalds 		return error;
27261da177e4SLinus Torvalds 
27271da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2728a74574aaSStephen Smalley 	if (!error)
2729f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
27301da177e4SLinus Torvalds 	return error;
27311da177e4SLinus Torvalds }
27321da177e4SLinus Torvalds 
27332e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
27342e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
27351da177e4SLinus Torvalds {
27362ad94ae6SAl Viro 	int error;
27371da177e4SLinus Torvalds 	char *from;
27381da177e4SLinus Torvalds 	char *to;
27396902d925SDave Hansen 	struct dentry *dentry;
27406902d925SDave Hansen 	struct nameidata nd;
27411da177e4SLinus Torvalds 
27421da177e4SLinus Torvalds 	from = getname(oldname);
27431da177e4SLinus Torvalds 	if (IS_ERR(from))
27441da177e4SLinus Torvalds 		return PTR_ERR(from);
27452ad94ae6SAl Viro 
27462ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
27472ad94ae6SAl Viro 	if (error)
27486902d925SDave Hansen 		goto out_putname;
27491da177e4SLinus Torvalds 
27501da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
27511da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27526902d925SDave Hansen 	if (IS_ERR(dentry))
27536902d925SDave Hansen 		goto out_unlock;
27546902d925SDave Hansen 
275575c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
275675c3f29dSDave Hansen 	if (error)
275775c3f29dSDave Hansen 		goto out_dput;
2758be6d3e56SKentaro Takeda 	error = security_path_symlink(&nd.path, dentry, from);
2759be6d3e56SKentaro Takeda 	if (error)
2760be6d3e56SKentaro Takeda 		goto out_drop_write;
2761db2e747bSMiklos Szeredi 	error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2762be6d3e56SKentaro Takeda out_drop_write:
276375c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
276475c3f29dSDave Hansen out_dput:
27651da177e4SLinus Torvalds 	dput(dentry);
27666902d925SDave Hansen out_unlock:
27674ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27681d957f9bSJan Blunck 	path_put(&nd.path);
27691da177e4SLinus Torvalds 	putname(to);
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;
28342d8f3038SAl Viro 	struct nameidata nd;
28352d8f3038SAl Viro 	struct path old_path;
283611a7b371SAneesh Kumar K.V 	int how = 0;
28371da177e4SLinus Torvalds 	int error;
28381da177e4SLinus Torvalds 	char *to;
28391da177e4SLinus Torvalds 
284011a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
2841c04030e1SUlrich Drepper 		return -EINVAL;
284211a7b371SAneesh Kumar K.V 	/*
284311a7b371SAneesh Kumar K.V 	 * To use null names we require CAP_DAC_READ_SEARCH
284411a7b371SAneesh Kumar K.V 	 * This ensures that not everyone will be able to create
284511a7b371SAneesh Kumar K.V 	 * handlink using the passed filedescriptor.
284611a7b371SAneesh Kumar K.V 	 */
284711a7b371SAneesh Kumar K.V 	if (flags & AT_EMPTY_PATH) {
284811a7b371SAneesh Kumar K.V 		if (!capable(CAP_DAC_READ_SEARCH))
284911a7b371SAneesh Kumar K.V 			return -ENOENT;
285011a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
285111a7b371SAneesh Kumar K.V 	}
2852c04030e1SUlrich Drepper 
285311a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
285411a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
285511a7b371SAneesh Kumar K.V 
285611a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
28571da177e4SLinus Torvalds 	if (error)
28582ad94ae6SAl Viro 		return error;
28592ad94ae6SAl Viro 
28602ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
28611da177e4SLinus Torvalds 	if (error)
28621da177e4SLinus Torvalds 		goto out;
28631da177e4SLinus Torvalds 	error = -EXDEV;
28642d8f3038SAl Viro 	if (old_path.mnt != nd.path.mnt)
28651da177e4SLinus Torvalds 		goto out_release;
28661da177e4SLinus Torvalds 	new_dentry = lookup_create(&nd, 0);
28671da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
28686902d925SDave Hansen 	if (IS_ERR(new_dentry))
28696902d925SDave Hansen 		goto out_unlock;
287075c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
287175c3f29dSDave Hansen 	if (error)
287275c3f29dSDave Hansen 		goto out_dput;
2873be6d3e56SKentaro Takeda 	error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2874be6d3e56SKentaro Takeda 	if (error)
2875be6d3e56SKentaro Takeda 		goto out_drop_write;
28762d8f3038SAl Viro 	error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
2877be6d3e56SKentaro Takeda out_drop_write:
287875c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
287975c3f29dSDave Hansen out_dput:
28801da177e4SLinus Torvalds 	dput(new_dentry);
28816902d925SDave Hansen out_unlock:
28824ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
28831da177e4SLinus Torvalds out_release:
28841d957f9bSJan Blunck 	path_put(&nd.path);
28852ad94ae6SAl Viro 	putname(to);
28861da177e4SLinus Torvalds out:
28872d8f3038SAl Viro 	path_put(&old_path);
28881da177e4SLinus Torvalds 
28891da177e4SLinus Torvalds 	return error;
28901da177e4SLinus Torvalds }
28911da177e4SLinus Torvalds 
28923480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
28935590ff0dSUlrich Drepper {
2894c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
28955590ff0dSUlrich Drepper }
28965590ff0dSUlrich Drepper 
28971da177e4SLinus Torvalds /*
28981da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
28991da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
29001da177e4SLinus Torvalds  * Problems:
29011da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
29021da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
29031da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
2904a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
29051da177e4SLinus Torvalds  *	   story.
29061da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
29071b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
29081da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
29091da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
2910a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
29111da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
29121da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
29131da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
2914a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
29151da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
29161da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
29171da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
2918e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
29191b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
29201da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2921c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
29221da177e4SLinus Torvalds  *	   locking].
29231da177e4SLinus Torvalds  */
292475c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
29251da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
29261da177e4SLinus Torvalds {
29271da177e4SLinus Torvalds 	int error = 0;
29289055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
29291da177e4SLinus Torvalds 
29301da177e4SLinus Torvalds 	/*
29311da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
29321da177e4SLinus Torvalds 	 * we'll need to flip '..'.
29331da177e4SLinus Torvalds 	 */
29341da177e4SLinus Torvalds 	if (new_dir != old_dir) {
2935f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
29361da177e4SLinus Torvalds 		if (error)
29371da177e4SLinus Torvalds 			return error;
29381da177e4SLinus Torvalds 	}
29391da177e4SLinus Torvalds 
29401da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
29411da177e4SLinus Torvalds 	if (error)
29421da177e4SLinus Torvalds 		return error;
29431da177e4SLinus Torvalds 
2944d83c49f3SAl Viro 	if (target)
29451b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
29469055cba7SSage Weil 
29471da177e4SLinus Torvalds 	error = -EBUSY;
29489055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
29499055cba7SSage Weil 		goto out;
29509055cba7SSage Weil 
29513cebde24SSage Weil 	if (target)
29523cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
29531da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
29549055cba7SSage Weil 	if (error)
29559055cba7SSage Weil 		goto out;
29569055cba7SSage Weil 
29571da177e4SLinus Torvalds 	if (target) {
29581da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
2959d83c49f3SAl Viro 		dont_mount(new_dentry);
2960d83c49f3SAl Viro 	}
29619055cba7SSage Weil out:
29629055cba7SSage Weil 	if (target)
29631b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
2964e31e14ecSStephen Smalley 	if (!error)
2965349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
29661da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
29671da177e4SLinus Torvalds 	return error;
29681da177e4SLinus Torvalds }
29691da177e4SLinus Torvalds 
297075c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
29711da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
29721da177e4SLinus Torvalds {
297351892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
29741da177e4SLinus Torvalds 	int error;
29751da177e4SLinus Torvalds 
29761da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
29771da177e4SLinus Torvalds 	if (error)
29781da177e4SLinus Torvalds 		return error;
29791da177e4SLinus Torvalds 
29801da177e4SLinus Torvalds 	dget(new_dentry);
29811da177e4SLinus Torvalds 	if (target)
29821b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
298351892bbbSSage Weil 
29841da177e4SLinus Torvalds 	error = -EBUSY;
298551892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
298651892bbbSSage Weil 		goto out;
298751892bbbSSage Weil 
29881da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
298951892bbbSSage Weil 	if (error)
299051892bbbSSage Weil 		goto out;
299151892bbbSSage Weil 
2992bec1052eSAl Viro 	if (target)
2993d83c49f3SAl Viro 		dont_mount(new_dentry);
2994349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
29951da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
299651892bbbSSage Weil out:
29971da177e4SLinus Torvalds 	if (target)
29981b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
29991da177e4SLinus Torvalds 	dput(new_dentry);
30001da177e4SLinus Torvalds 	return error;
30011da177e4SLinus Torvalds }
30021da177e4SLinus Torvalds 
30031da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
30041da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
30051da177e4SLinus Torvalds {
30061da177e4SLinus Torvalds 	int error;
30071da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
300859b0df21SEric Paris 	const unsigned char *old_name;
30091da177e4SLinus Torvalds 
30101da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
30111da177e4SLinus Torvalds  		return 0;
30121da177e4SLinus Torvalds 
30131da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
30141da177e4SLinus Torvalds 	if (error)
30151da177e4SLinus Torvalds 		return error;
30161da177e4SLinus Torvalds 
30171da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3018a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
30191da177e4SLinus Torvalds 	else
30201da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
30211da177e4SLinus Torvalds 	if (error)
30221da177e4SLinus Torvalds 		return error;
30231da177e4SLinus Torvalds 
3024acfa4380SAl Viro 	if (!old_dir->i_op->rename)
30251da177e4SLinus Torvalds 		return -EPERM;
30261da177e4SLinus Torvalds 
30270eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
30280eeca283SRobert Love 
30291da177e4SLinus Torvalds 	if (is_dir)
30301da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
30311da177e4SLinus Torvalds 	else
30321da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3033123df294SAl Viro 	if (!error)
3034123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
30355a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
30360eeca283SRobert Love 	fsnotify_oldname_free(old_name);
30370eeca283SRobert Love 
30381da177e4SLinus Torvalds 	return error;
30391da177e4SLinus Torvalds }
30401da177e4SLinus Torvalds 
30412e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
30422e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
30431da177e4SLinus Torvalds {
30441da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
30451da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
30461da177e4SLinus Torvalds 	struct dentry *trap;
30471da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
30482ad94ae6SAl Viro 	char *from;
30492ad94ae6SAl Viro 	char *to;
30502ad94ae6SAl Viro 	int error;
30511da177e4SLinus Torvalds 
30522ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
30531da177e4SLinus Torvalds 	if (error)
30541da177e4SLinus Torvalds 		goto exit;
30551da177e4SLinus Torvalds 
30562ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
30571da177e4SLinus Torvalds 	if (error)
30581da177e4SLinus Torvalds 		goto exit1;
30591da177e4SLinus Torvalds 
30601da177e4SLinus Torvalds 	error = -EXDEV;
30614ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
30621da177e4SLinus Torvalds 		goto exit2;
30631da177e4SLinus Torvalds 
30644ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
30651da177e4SLinus Torvalds 	error = -EBUSY;
30661da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
30671da177e4SLinus Torvalds 		goto exit2;
30681da177e4SLinus Torvalds 
30694ac91378SJan Blunck 	new_dir = newnd.path.dentry;
30701da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
30711da177e4SLinus Torvalds 		goto exit2;
30721da177e4SLinus Torvalds 
30730612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
30740612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
30754e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
30760612d9fbSOGAWA Hirofumi 
30771da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
30781da177e4SLinus Torvalds 
307949705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
30801da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
30811da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
30821da177e4SLinus Torvalds 		goto exit3;
30831da177e4SLinus Torvalds 	/* source must exist */
30841da177e4SLinus Torvalds 	error = -ENOENT;
30851da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
30861da177e4SLinus Torvalds 		goto exit4;
30871da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
30881da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
30891da177e4SLinus Torvalds 		error = -ENOTDIR;
30901da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
30911da177e4SLinus Torvalds 			goto exit4;
30921da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
30931da177e4SLinus Torvalds 			goto exit4;
30941da177e4SLinus Torvalds 	}
30951da177e4SLinus Torvalds 	/* source should not be ancestor of target */
30961da177e4SLinus Torvalds 	error = -EINVAL;
30971da177e4SLinus Torvalds 	if (old_dentry == trap)
30981da177e4SLinus Torvalds 		goto exit4;
309949705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
31001da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
31011da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
31021da177e4SLinus Torvalds 		goto exit4;
31031da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
31041da177e4SLinus Torvalds 	error = -ENOTEMPTY;
31051da177e4SLinus Torvalds 	if (new_dentry == trap)
31061da177e4SLinus Torvalds 		goto exit5;
31071da177e4SLinus Torvalds 
31089079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
31099079b1ebSDave Hansen 	if (error)
31109079b1ebSDave Hansen 		goto exit5;
3111be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3112be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3113be6d3e56SKentaro Takeda 	if (error)
3114be6d3e56SKentaro Takeda 		goto exit6;
31151da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
31161da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3117be6d3e56SKentaro Takeda exit6:
31189079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
31191da177e4SLinus Torvalds exit5:
31201da177e4SLinus Torvalds 	dput(new_dentry);
31211da177e4SLinus Torvalds exit4:
31221da177e4SLinus Torvalds 	dput(old_dentry);
31231da177e4SLinus Torvalds exit3:
31241da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
31251da177e4SLinus Torvalds exit2:
31261d957f9bSJan Blunck 	path_put(&newnd.path);
31272ad94ae6SAl Viro 	putname(to);
31281da177e4SLinus Torvalds exit1:
31291d957f9bSJan Blunck 	path_put(&oldnd.path);
31301da177e4SLinus Torvalds 	putname(from);
31312ad94ae6SAl Viro exit:
31321da177e4SLinus Torvalds 	return error;
31331da177e4SLinus Torvalds }
31341da177e4SLinus Torvalds 
3135a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
31365590ff0dSUlrich Drepper {
31375590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
31385590ff0dSUlrich Drepper }
31395590ff0dSUlrich Drepper 
31401da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
31411da177e4SLinus Torvalds {
31421da177e4SLinus Torvalds 	int len;
31431da177e4SLinus Torvalds 
31441da177e4SLinus Torvalds 	len = PTR_ERR(link);
31451da177e4SLinus Torvalds 	if (IS_ERR(link))
31461da177e4SLinus Torvalds 		goto out;
31471da177e4SLinus Torvalds 
31481da177e4SLinus Torvalds 	len = strlen(link);
31491da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
31501da177e4SLinus Torvalds 		len = buflen;
31511da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
31521da177e4SLinus Torvalds 		len = -EFAULT;
31531da177e4SLinus Torvalds out:
31541da177e4SLinus Torvalds 	return len;
31551da177e4SLinus Torvalds }
31561da177e4SLinus Torvalds 
31571da177e4SLinus Torvalds /*
31581da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
31591da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
31601da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
31611da177e4SLinus Torvalds  */
31621da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
31631da177e4SLinus Torvalds {
31641da177e4SLinus Torvalds 	struct nameidata nd;
3165cc314eefSLinus Torvalds 	void *cookie;
3166694a1764SMarcin Slusarz 	int res;
3167cc314eefSLinus Torvalds 
31681da177e4SLinus Torvalds 	nd.depth = 0;
3169cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3170694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3171694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3172694a1764SMarcin Slusarz 
3173694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
31741da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3175cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3176694a1764SMarcin Slusarz 	return res;
31771da177e4SLinus Torvalds }
31781da177e4SLinus Torvalds 
31791da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
31801da177e4SLinus Torvalds {
31811da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
31821da177e4SLinus Torvalds }
31831da177e4SLinus Torvalds 
31841da177e4SLinus Torvalds /* get the link contents into pagecache */
31851da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
31861da177e4SLinus Torvalds {
3187ebd09abbSDuane Griffin 	char *kaddr;
31881da177e4SLinus Torvalds 	struct page *page;
31891da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3190090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
31911da177e4SLinus Torvalds 	if (IS_ERR(page))
31926fe6900eSNick Piggin 		return (char*)page;
31931da177e4SLinus Torvalds 	*ppage = page;
3194ebd09abbSDuane Griffin 	kaddr = kmap(page);
3195ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3196ebd09abbSDuane Griffin 	return kaddr;
31971da177e4SLinus Torvalds }
31981da177e4SLinus Torvalds 
31991da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32001da177e4SLinus Torvalds {
32011da177e4SLinus Torvalds 	struct page *page = NULL;
32021da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
32031da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
32041da177e4SLinus Torvalds 	if (page) {
32051da177e4SLinus Torvalds 		kunmap(page);
32061da177e4SLinus Torvalds 		page_cache_release(page);
32071da177e4SLinus Torvalds 	}
32081da177e4SLinus Torvalds 	return res;
32091da177e4SLinus Torvalds }
32101da177e4SLinus Torvalds 
3211cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
32121da177e4SLinus Torvalds {
3213cc314eefSLinus Torvalds 	struct page *page = NULL;
32141da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3215cc314eefSLinus Torvalds 	return page;
32161da177e4SLinus Torvalds }
32171da177e4SLinus Torvalds 
3218cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
32191da177e4SLinus Torvalds {
3220cc314eefSLinus Torvalds 	struct page *page = cookie;
3221cc314eefSLinus Torvalds 
3222cc314eefSLinus Torvalds 	if (page) {
32231da177e4SLinus Torvalds 		kunmap(page);
32241da177e4SLinus Torvalds 		page_cache_release(page);
32251da177e4SLinus Torvalds 	}
32261da177e4SLinus Torvalds }
32271da177e4SLinus Torvalds 
322854566b2cSNick Piggin /*
322954566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
323054566b2cSNick Piggin  */
323154566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
32321da177e4SLinus Torvalds {
32331da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
32340adb25d2SKirill Korotaev 	struct page *page;
3235afddba49SNick Piggin 	void *fsdata;
3236beb497abSDmitriy Monakhov 	int err;
32371da177e4SLinus Torvalds 	char *kaddr;
323854566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
323954566b2cSNick Piggin 	if (nofs)
324054566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
32411da177e4SLinus Torvalds 
32427e53cac4SNeilBrown retry:
3243afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
324454566b2cSNick Piggin 				flags, &page, &fsdata);
32451da177e4SLinus Torvalds 	if (err)
3246afddba49SNick Piggin 		goto fail;
3247afddba49SNick Piggin 
32481da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
32491da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
32501da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
3251afddba49SNick Piggin 
3252afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3253afddba49SNick Piggin 							page, fsdata);
32541da177e4SLinus Torvalds 	if (err < 0)
32551da177e4SLinus Torvalds 		goto fail;
3256afddba49SNick Piggin 	if (err < len-1)
3257afddba49SNick Piggin 		goto retry;
3258afddba49SNick Piggin 
32591da177e4SLinus Torvalds 	mark_inode_dirty(inode);
32601da177e4SLinus Torvalds 	return 0;
32611da177e4SLinus Torvalds fail:
32621da177e4SLinus Torvalds 	return err;
32631da177e4SLinus Torvalds }
32641da177e4SLinus Torvalds 
32650adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
32660adb25d2SKirill Korotaev {
32670adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
326854566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
32690adb25d2SKirill Korotaev }
32700adb25d2SKirill Korotaev 
327192e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
32721da177e4SLinus Torvalds 	.readlink	= generic_readlink,
32731da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
32741da177e4SLinus Torvalds 	.put_link	= page_put_link,
32751da177e4SLinus Torvalds };
32761da177e4SLinus Torvalds 
32772d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3278cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
32791da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
32801da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
32811da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
32821da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
32831da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
32841da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
32851da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
32861da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
32871da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
32880adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
32891da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
32901da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3291c9c6cac0SAl Viro EXPORT_SYMBOL(kern_path_parent);
3292d1811465SAl Viro EXPORT_SYMBOL(kern_path);
329316f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3294f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
32951da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
32961da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
32971da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
32981da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
32991da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
33001da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
33011da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
33021da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
33031da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
33041da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
33051da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
33061da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
33071da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
33081da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3309