xref: /openbmc/linux/fs/namei.c (revision 4ad5abb3)
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 
439f5e1c1c1SAl Viro static struct dentry *
440bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd)
441bcdc5e01SIan Kent {
442f5e1c1c1SAl Viro 	int status = d_revalidate(dentry, nd);
443bcdc5e01SIan Kent 	if (unlikely(status <= 0)) {
444bcdc5e01SIan Kent 		/*
445bcdc5e01SIan Kent 		 * The dentry failed validation.
446bcdc5e01SIan Kent 		 * If d_revalidate returned 0 attempt to invalidate
447bcdc5e01SIan Kent 		 * the dentry otherwise d_revalidate is asking us
448bcdc5e01SIan Kent 		 * to return a fail status.
449bcdc5e01SIan Kent 		 */
45034286d66SNick Piggin 		if (status < 0) {
45134286d66SNick Piggin 			dput(dentry);
45234286d66SNick Piggin 			dentry = ERR_PTR(status);
453f5e1c1c1SAl Viro 		} else if (!d_invalidate(dentry)) {
454bcdc5e01SIan Kent 			dput(dentry);
455bcdc5e01SIan Kent 			dentry = NULL;
456bcdc5e01SIan Kent 		}
457bcdc5e01SIan Kent 	}
458f5e1c1c1SAl Viro 	return dentry;
459f5e1c1c1SAl Viro }
460f5e1c1c1SAl Viro 
4619f1fafeeSAl Viro /**
4629f1fafeeSAl Viro  * complete_walk - successful completion of path walk
4639f1fafeeSAl Viro  * @nd:  pointer nameidata
46439159de2SJeff Layton  *
4659f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
4669f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
4679f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
4689f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
4699f1fafeeSAl Viro  * need to drop nd->path.
47039159de2SJeff Layton  */
4719f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
47239159de2SJeff Layton {
47316c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
47439159de2SJeff Layton 	int status;
47539159de2SJeff Layton 
4769f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
4779f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
4789f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
4799f1fafeeSAl Viro 			nd->root.mnt = NULL;
4809f1fafeeSAl Viro 		spin_lock(&dentry->d_lock);
4819f1fafeeSAl Viro 		if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
4829f1fafeeSAl Viro 			spin_unlock(&dentry->d_lock);
4839f1fafeeSAl Viro 			rcu_read_unlock();
4849f1fafeeSAl Viro 			br_read_unlock(vfsmount_lock);
4859f1fafeeSAl Viro 			return -ECHILD;
4869f1fafeeSAl Viro 		}
4879f1fafeeSAl Viro 		BUG_ON(nd->inode != dentry->d_inode);
4889f1fafeeSAl Viro 		spin_unlock(&dentry->d_lock);
4899f1fafeeSAl Viro 		mntget(nd->path.mnt);
4909f1fafeeSAl Viro 		rcu_read_unlock();
4919f1fafeeSAl Viro 		br_read_unlock(vfsmount_lock);
4929f1fafeeSAl Viro 	}
4939f1fafeeSAl Viro 
49416c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
49539159de2SJeff Layton 		return 0;
49639159de2SJeff Layton 
49716c2cd71SAl Viro 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
49816c2cd71SAl Viro 		return 0;
49916c2cd71SAl Viro 
50016c2cd71SAl Viro 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
50116c2cd71SAl Viro 		return 0;
50216c2cd71SAl Viro 
50316c2cd71SAl Viro 	/* Note: we do not d_invalidate() */
50434286d66SNick Piggin 	status = d_revalidate(dentry, nd);
50539159de2SJeff Layton 	if (status > 0)
50639159de2SJeff Layton 		return 0;
50739159de2SJeff Layton 
50816c2cd71SAl Viro 	if (!status)
50939159de2SJeff Layton 		status = -ESTALE;
51016c2cd71SAl Viro 
5119f1fafeeSAl Viro 	path_put(&nd->path);
51239159de2SJeff Layton 	return status;
51339159de2SJeff Layton }
51439159de2SJeff Layton 
5152a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
5162a737871SAl Viro {
517f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
518f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
5192a737871SAl Viro }
5202a737871SAl Viro 
5216de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
5226de88d72SAl Viro 
52331e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
52431e6b01fSNick Piggin {
52531e6b01fSNick Piggin 	if (!nd->root.mnt) {
52631e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
527c28cc364SNick Piggin 		unsigned seq;
528c28cc364SNick Piggin 
529c28cc364SNick Piggin 		do {
530c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
53131e6b01fSNick Piggin 			nd->root = fs->root;
532c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
533c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
53431e6b01fSNick Piggin 	}
53531e6b01fSNick Piggin }
53631e6b01fSNick Piggin 
537f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
5381da177e4SLinus Torvalds {
53931e6b01fSNick Piggin 	int ret;
54031e6b01fSNick Piggin 
5411da177e4SLinus Torvalds 	if (IS_ERR(link))
5421da177e4SLinus Torvalds 		goto fail;
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 	if (*link == '/') {
5452a737871SAl Viro 		set_root(nd);
5461d957f9bSJan Blunck 		path_put(&nd->path);
5472a737871SAl Viro 		nd->path = nd->root;
5482a737871SAl Viro 		path_get(&nd->root);
54916c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
5501da177e4SLinus Torvalds 	}
55131e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
552b4091d5fSChristoph Hellwig 
55331e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
55431e6b01fSNick Piggin 	return ret;
5551da177e4SLinus Torvalds fail:
5561d957f9bSJan Blunck 	path_put(&nd->path);
5571da177e4SLinus Torvalds 	return PTR_ERR(link);
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds 
5601d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
561051d3812SIan Kent {
562051d3812SIan Kent 	dput(path->dentry);
5634ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
564051d3812SIan Kent 		mntput(path->mnt);
565051d3812SIan Kent }
566051d3812SIan Kent 
5677b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
5687b9337aaSNick Piggin 					struct nameidata *nd)
569051d3812SIan Kent {
57031e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
5714ac91378SJan Blunck 		dput(nd->path.dentry);
57231e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
5734ac91378SJan Blunck 			mntput(nd->path.mnt);
5749a229683SHuang Shijie 	}
57531e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
5764ac91378SJan Blunck 	nd->path.dentry = path->dentry;
577051d3812SIan Kent }
578051d3812SIan Kent 
579574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
580574197e0SAl Viro {
581574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
582574197e0SAl Viro 	if (!IS_ERR(cookie) && inode->i_op->put_link)
583574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
584574197e0SAl Viro 	path_put(link);
585574197e0SAl Viro }
586574197e0SAl Viro 
587def4af30SAl Viro static __always_inline int
588574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
5891da177e4SLinus Torvalds {
5901da177e4SLinus Torvalds 	int error;
5917b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
5921da177e4SLinus Torvalds 
593844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
594844a3917SAl Viro 
5950e794589SAl Viro 	if (link->mnt == nd->path.mnt)
5960e794589SAl Viro 		mntget(link->mnt);
5970e794589SAl Viro 
598574197e0SAl Viro 	if (unlikely(current->total_link_count >= 40)) {
599574197e0SAl Viro 		*p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
600574197e0SAl Viro 		path_put(&nd->path);
601574197e0SAl Viro 		return -ELOOP;
602574197e0SAl Viro 	}
603574197e0SAl Viro 	cond_resched();
604574197e0SAl Viro 	current->total_link_count++;
605574197e0SAl Viro 
6067b9337aaSNick Piggin 	touch_atime(link->mnt, dentry);
6071da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
608cd4e91d3SAl Viro 
60936f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
61036f3b4f6SAl Viro 	if (error) {
61136f3b4f6SAl Viro 		*p = ERR_PTR(error); /* no ->put_link(), please */
61236f3b4f6SAl Viro 		path_put(&nd->path);
61336f3b4f6SAl Viro 		return error;
61436f3b4f6SAl Viro 	}
61536f3b4f6SAl Viro 
61686acdca1SAl Viro 	nd->last_type = LAST_BIND;
617def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
618def4af30SAl Viro 	error = PTR_ERR(*p);
619def4af30SAl Viro 	if (!IS_ERR(*p)) {
6201da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
621cc314eefSLinus Torvalds 		error = 0;
6221da177e4SLinus Torvalds 		if (s)
6231da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
624bcda7652SAl Viro 		else if (nd->last_type == LAST_BIND) {
62516c2cd71SAl Viro 			nd->flags |= LOOKUP_JUMPED;
626b21041d0SAl Viro 			nd->inode = nd->path.dentry->d_inode;
627b21041d0SAl Viro 			if (nd->inode->i_op->follow_link) {
628bcda7652SAl Viro 				/* stepped on a _really_ weird one */
629bcda7652SAl Viro 				path_put(&nd->path);
630bcda7652SAl Viro 				error = -ELOOP;
631bcda7652SAl Viro 			}
632bcda7652SAl Viro 		}
6331da177e4SLinus Torvalds 	}
6341da177e4SLinus Torvalds 	return error;
6351da177e4SLinus Torvalds }
6361da177e4SLinus Torvalds 
63731e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
63831e6b01fSNick Piggin {
63931e6b01fSNick Piggin 	struct vfsmount *parent;
64031e6b01fSNick Piggin 	struct dentry *mountpoint;
64131e6b01fSNick Piggin 
64231e6b01fSNick Piggin 	parent = path->mnt->mnt_parent;
64331e6b01fSNick Piggin 	if (parent == path->mnt)
64431e6b01fSNick Piggin 		return 0;
64531e6b01fSNick Piggin 	mountpoint = path->mnt->mnt_mountpoint;
64631e6b01fSNick Piggin 	path->dentry = mountpoint;
64731e6b01fSNick Piggin 	path->mnt = parent;
64831e6b01fSNick Piggin 	return 1;
64931e6b01fSNick Piggin }
65031e6b01fSNick Piggin 
651bab77ebfSAl Viro int follow_up(struct path *path)
6521da177e4SLinus Torvalds {
6531da177e4SLinus Torvalds 	struct vfsmount *parent;
6541da177e4SLinus Torvalds 	struct dentry *mountpoint;
65599b7db7bSNick Piggin 
65699b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
657bab77ebfSAl Viro 	parent = path->mnt->mnt_parent;
658bab77ebfSAl Viro 	if (parent == path->mnt) {
65999b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
6601da177e4SLinus Torvalds 		return 0;
6611da177e4SLinus Torvalds 	}
6621da177e4SLinus Torvalds 	mntget(parent);
663bab77ebfSAl Viro 	mountpoint = dget(path->mnt->mnt_mountpoint);
66499b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
665bab77ebfSAl Viro 	dput(path->dentry);
666bab77ebfSAl Viro 	path->dentry = mountpoint;
667bab77ebfSAl Viro 	mntput(path->mnt);
668bab77ebfSAl Viro 	path->mnt = parent;
6691da177e4SLinus Torvalds 	return 1;
6701da177e4SLinus Torvalds }
6711da177e4SLinus Torvalds 
672b5c84bf6SNick Piggin /*
6739875cf80SDavid Howells  * Perform an automount
6749875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
6759875cf80SDavid Howells  *   were called with.
6761da177e4SLinus Torvalds  */
6779875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
6789875cf80SDavid Howells 			    bool *need_mntput)
67931e6b01fSNick Piggin {
6809875cf80SDavid Howells 	struct vfsmount *mnt;
681ea5b778aSDavid Howells 	int err;
6829875cf80SDavid Howells 
6839875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
6849875cf80SDavid Howells 		return -EREMOTE;
6859875cf80SDavid Howells 
6866f45b656SDavid Howells 	/* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
6876f45b656SDavid Howells 	 * and this is the terminal part of the path.
6886f45b656SDavid Howells 	 */
6896f45b656SDavid Howells 	if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
6906f45b656SDavid Howells 		return -EISDIR; /* we actually want to stop here */
6916f45b656SDavid Howells 
6929875cf80SDavid Howells 	/* We want to mount if someone is trying to open/create a file of any
6939875cf80SDavid Howells 	 * type under the mountpoint, wants to traverse through the mountpoint
6949875cf80SDavid Howells 	 * or wants to open the mounted directory.
6959875cf80SDavid Howells 	 *
6969875cf80SDavid Howells 	 * We don't want to mount if someone's just doing a stat and they've
6979875cf80SDavid Howells 	 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
6989875cf80SDavid Howells 	 * appended a '/' to the name.
6999875cf80SDavid Howells 	 */
7009875cf80SDavid Howells 	if (!(flags & LOOKUP_FOLLOW) &&
7019875cf80SDavid Howells 	    !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
7029875cf80SDavid Howells 		       LOOKUP_OPEN | LOOKUP_CREATE)))
7039875cf80SDavid Howells 		return -EISDIR;
7049875cf80SDavid Howells 
7059875cf80SDavid Howells 	current->total_link_count++;
7069875cf80SDavid Howells 	if (current->total_link_count >= 40)
7079875cf80SDavid Howells 		return -ELOOP;
7089875cf80SDavid Howells 
7099875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
7109875cf80SDavid Howells 	if (IS_ERR(mnt)) {
7119875cf80SDavid Howells 		/*
7129875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
7139875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
7149875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
7159875cf80SDavid Howells 		 *
7169875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
7179875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
7189875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
7199875cf80SDavid Howells 		 */
7209875cf80SDavid Howells 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
7219875cf80SDavid Howells 			return -EREMOTE;
7229875cf80SDavid Howells 		return PTR_ERR(mnt);
72331e6b01fSNick Piggin 	}
724ea5b778aSDavid Howells 
7259875cf80SDavid Howells 	if (!mnt) /* mount collision */
7269875cf80SDavid Howells 		return 0;
7279875cf80SDavid Howells 
7288aef1884SAl Viro 	if (!*need_mntput) {
7298aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
7308aef1884SAl Viro 		mntget(path->mnt);
7318aef1884SAl Viro 		*need_mntput = true;
7328aef1884SAl Viro 	}
73319a167afSAl Viro 	err = finish_automount(mnt, path);
734ea5b778aSDavid Howells 
735ea5b778aSDavid Howells 	switch (err) {
736ea5b778aSDavid Howells 	case -EBUSY:
737ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
73819a167afSAl Viro 		return 0;
739ea5b778aSDavid Howells 	case 0:
7408aef1884SAl Viro 		path_put(path);
7419875cf80SDavid Howells 		path->mnt = mnt;
7429875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
7439875cf80SDavid Howells 		return 0;
74419a167afSAl Viro 	default:
74519a167afSAl Viro 		return err;
7469875cf80SDavid Howells 	}
74719a167afSAl Viro 
748ea5b778aSDavid Howells }
7499875cf80SDavid Howells 
7509875cf80SDavid Howells /*
7519875cf80SDavid Howells  * Handle a dentry that is managed in some way.
752cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
7539875cf80SDavid Howells  * - Flagged as mountpoint
7549875cf80SDavid Howells  * - Flagged as automount point
7559875cf80SDavid Howells  *
7569875cf80SDavid Howells  * This may only be called in refwalk mode.
7579875cf80SDavid Howells  *
7589875cf80SDavid Howells  * Serialization is taken care of in namespace.c
7599875cf80SDavid Howells  */
7609875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
7619875cf80SDavid Howells {
7628aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
7639875cf80SDavid Howells 	unsigned managed;
7649875cf80SDavid Howells 	bool need_mntput = false;
7658aef1884SAl Viro 	int ret = 0;
7669875cf80SDavid Howells 
7679875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
7689875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
7699875cf80SDavid Howells 	 * the components of that value change under us */
7709875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
7719875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
7729875cf80SDavid Howells 	       unlikely(managed != 0)) {
773cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
774cc53ce53SDavid Howells 		 * being held. */
775cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
776cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
777cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
7781aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
779cc53ce53SDavid Howells 			if (ret < 0)
7808aef1884SAl Viro 				break;
781cc53ce53SDavid Howells 		}
782cc53ce53SDavid Howells 
7839875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
7849875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
7859875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
7869875cf80SDavid Howells 			if (mounted) {
7879875cf80SDavid Howells 				dput(path->dentry);
7889875cf80SDavid Howells 				if (need_mntput)
789463ffb2eSAl Viro 					mntput(path->mnt);
790463ffb2eSAl Viro 				path->mnt = mounted;
791463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
7929875cf80SDavid Howells 				need_mntput = true;
7939875cf80SDavid Howells 				continue;
794463ffb2eSAl Viro 			}
795463ffb2eSAl Viro 
7969875cf80SDavid Howells 			/* Something is mounted on this dentry in another
7979875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
7989875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
7999875cf80SDavid Howells 			 * vfsmount_lock */
8001da177e4SLinus Torvalds 		}
8019875cf80SDavid Howells 
8029875cf80SDavid Howells 		/* Handle an automount point */
8039875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
8049875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
8059875cf80SDavid Howells 			if (ret < 0)
8068aef1884SAl Viro 				break;
8079875cf80SDavid Howells 			continue;
8089875cf80SDavid Howells 		}
8099875cf80SDavid Howells 
8109875cf80SDavid Howells 		/* We didn't change the current path point */
8119875cf80SDavid Howells 		break;
8129875cf80SDavid Howells 	}
8138aef1884SAl Viro 
8148aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
8158aef1884SAl Viro 		mntput(path->mnt);
8168aef1884SAl Viro 	if (ret == -EISDIR)
8178aef1884SAl Viro 		ret = 0;
8188aef1884SAl Viro 	return ret;
8191da177e4SLinus Torvalds }
8201da177e4SLinus Torvalds 
821cc53ce53SDavid Howells int follow_down_one(struct path *path)
8221da177e4SLinus Torvalds {
8231da177e4SLinus Torvalds 	struct vfsmount *mounted;
8241da177e4SLinus Torvalds 
8251c755af4SAl Viro 	mounted = lookup_mnt(path);
8261da177e4SLinus Torvalds 	if (mounted) {
8279393bd07SAl Viro 		dput(path->dentry);
8289393bd07SAl Viro 		mntput(path->mnt);
8299393bd07SAl Viro 		path->mnt = mounted;
8309393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
8311da177e4SLinus Torvalds 		return 1;
8321da177e4SLinus Torvalds 	}
8331da177e4SLinus Torvalds 	return 0;
8341da177e4SLinus Torvalds }
8351da177e4SLinus Torvalds 
83662a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
83762a7375eSIan Kent {
83862a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
83962a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
84062a7375eSIan Kent }
84162a7375eSIan Kent 
8429875cf80SDavid Howells /*
843287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
844287548e4SAl Viro  * we meet a managed dentry that would need blocking.
8459875cf80SDavid Howells  */
8469875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
847287548e4SAl Viro 			       struct inode **inode)
8489875cf80SDavid Howells {
84962a7375eSIan Kent 	for (;;) {
8509875cf80SDavid Howells 		struct vfsmount *mounted;
85162a7375eSIan Kent 		/*
85262a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
85362a7375eSIan Kent 		 * that wants to block transit.
85462a7375eSIan Kent 		 */
855287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
856ab90911fSDavid Howells 			return false;
85762a7375eSIan Kent 
85862a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
85962a7375eSIan Kent 			break;
86062a7375eSIan Kent 
8619875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
8629875cf80SDavid Howells 		if (!mounted)
8639875cf80SDavid Howells 			break;
8649875cf80SDavid Howells 		path->mnt = mounted;
8659875cf80SDavid Howells 		path->dentry = mounted->mnt_root;
8669875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
86759430262SLinus Torvalds 		/*
86859430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
86959430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
87059430262SLinus Torvalds 		 * because a mount-point is always pinned.
87159430262SLinus Torvalds 		 */
87259430262SLinus Torvalds 		*inode = path->dentry->d_inode;
8739875cf80SDavid Howells 	}
8749875cf80SDavid Howells 	return true;
8759875cf80SDavid Howells }
8769875cf80SDavid Howells 
877dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
878287548e4SAl Viro {
879dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
880287548e4SAl Viro 		struct vfsmount *mounted;
881dea39376SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
882287548e4SAl Viro 		if (!mounted)
883287548e4SAl Viro 			break;
884dea39376SAl Viro 		nd->path.mnt = mounted;
885dea39376SAl Viro 		nd->path.dentry = mounted->mnt_root;
886dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
887287548e4SAl Viro 	}
888287548e4SAl Viro }
889287548e4SAl Viro 
89031e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
89131e6b01fSNick Piggin {
89231e6b01fSNick Piggin 	set_root_rcu(nd);
89331e6b01fSNick Piggin 
89431e6b01fSNick Piggin 	while (1) {
89531e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
89631e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
89731e6b01fSNick Piggin 			break;
89831e6b01fSNick Piggin 		}
89931e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
90031e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
90131e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
90231e6b01fSNick Piggin 			unsigned seq;
90331e6b01fSNick Piggin 
90431e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
90531e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
906ef7562d5SAl Viro 				goto failed;
90731e6b01fSNick Piggin 			nd->path.dentry = parent;
90831e6b01fSNick Piggin 			nd->seq = seq;
90931e6b01fSNick Piggin 			break;
91031e6b01fSNick Piggin 		}
91131e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
91231e6b01fSNick Piggin 			break;
91331e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
91431e6b01fSNick Piggin 	}
915dea39376SAl Viro 	follow_mount_rcu(nd);
916dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
91731e6b01fSNick Piggin 	return 0;
918ef7562d5SAl Viro 
919ef7562d5SAl Viro failed:
920ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
9215b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
922ef7562d5SAl Viro 		nd->root.mnt = NULL;
923ef7562d5SAl Viro 	rcu_read_unlock();
924ef7562d5SAl Viro 	br_read_unlock(vfsmount_lock);
925ef7562d5SAl Viro 	return -ECHILD;
92631e6b01fSNick Piggin }
92731e6b01fSNick Piggin 
9289875cf80SDavid Howells /*
929cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
930cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
931cc53ce53SDavid Howells  * caller is permitted to proceed or not.
932cc53ce53SDavid Howells  */
9337cc90cc3SAl Viro int follow_down(struct path *path)
934cc53ce53SDavid Howells {
935cc53ce53SDavid Howells 	unsigned managed;
936cc53ce53SDavid Howells 	int ret;
937cc53ce53SDavid Howells 
938cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
939cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
940cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
941cc53ce53SDavid Howells 		 * being held.
942cc53ce53SDavid Howells 		 *
943cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
944cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
945cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
946cc53ce53SDavid Howells 		 * superstructure.
947cc53ce53SDavid Howells 		 *
948cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
949cc53ce53SDavid Howells 		 */
950cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
951cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
952cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
953ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
9541aed3e42SAl Viro 				path->dentry, false);
955cc53ce53SDavid Howells 			if (ret < 0)
956cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
957cc53ce53SDavid Howells 		}
958cc53ce53SDavid Howells 
959cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
960cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
961cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
962cc53ce53SDavid Howells 			if (!mounted)
963cc53ce53SDavid Howells 				break;
964cc53ce53SDavid Howells 			dput(path->dentry);
965cc53ce53SDavid Howells 			mntput(path->mnt);
966cc53ce53SDavid Howells 			path->mnt = mounted;
967cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
968cc53ce53SDavid Howells 			continue;
969cc53ce53SDavid Howells 		}
970cc53ce53SDavid Howells 
971cc53ce53SDavid Howells 		/* Don't handle automount points here */
972cc53ce53SDavid Howells 		break;
973cc53ce53SDavid Howells 	}
974cc53ce53SDavid Howells 	return 0;
975cc53ce53SDavid Howells }
976cc53ce53SDavid Howells 
977cc53ce53SDavid Howells /*
9789875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
9799875cf80SDavid Howells  */
9809875cf80SDavid Howells static void follow_mount(struct path *path)
9819875cf80SDavid Howells {
9829875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
9839875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
9849875cf80SDavid Howells 		if (!mounted)
9859875cf80SDavid Howells 			break;
9869875cf80SDavid Howells 		dput(path->dentry);
9879875cf80SDavid Howells 		mntput(path->mnt);
9889875cf80SDavid Howells 		path->mnt = mounted;
9899875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
9909875cf80SDavid Howells 	}
9919875cf80SDavid Howells }
9929875cf80SDavid Howells 
99331e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
9941da177e4SLinus Torvalds {
9952a737871SAl Viro 	set_root(nd);
996e518ddb7SAndreas Mohr 
9971da177e4SLinus Torvalds 	while(1) {
9984ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
9991da177e4SLinus Torvalds 
10002a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
10012a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
10021da177e4SLinus Torvalds 			break;
10031da177e4SLinus Torvalds 		}
10044ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
10053088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
10063088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
10071da177e4SLinus Torvalds 			dput(old);
10081da177e4SLinus Torvalds 			break;
10091da177e4SLinus Torvalds 		}
10103088dd70SAl Viro 		if (!follow_up(&nd->path))
10111da177e4SLinus Torvalds 			break;
10121da177e4SLinus Torvalds 	}
101379ed0226SAl Viro 	follow_mount(&nd->path);
101431e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
10151da177e4SLinus Torvalds }
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds /*
1018baa03890SNick Piggin  * Allocate a dentry with name and parent, and perform a parent
1019baa03890SNick Piggin  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1020baa03890SNick Piggin  * on error. parent->d_inode->i_mutex must be held. d_lookup must
1021baa03890SNick Piggin  * have verified that no child exists while under i_mutex.
1022baa03890SNick Piggin  */
1023baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1024baa03890SNick Piggin 				struct qstr *name, struct nameidata *nd)
1025baa03890SNick Piggin {
1026baa03890SNick Piggin 	struct inode *inode = parent->d_inode;
1027baa03890SNick Piggin 	struct dentry *dentry;
1028baa03890SNick Piggin 	struct dentry *old;
1029baa03890SNick Piggin 
1030baa03890SNick Piggin 	/* Don't create child dentry for a dead directory. */
1031baa03890SNick Piggin 	if (unlikely(IS_DEADDIR(inode)))
1032baa03890SNick Piggin 		return ERR_PTR(-ENOENT);
1033baa03890SNick Piggin 
1034baa03890SNick Piggin 	dentry = d_alloc(parent, name);
1035baa03890SNick Piggin 	if (unlikely(!dentry))
1036baa03890SNick Piggin 		return ERR_PTR(-ENOMEM);
1037baa03890SNick Piggin 
1038baa03890SNick Piggin 	old = inode->i_op->lookup(inode, dentry, nd);
1039baa03890SNick Piggin 	if (unlikely(old)) {
1040baa03890SNick Piggin 		dput(dentry);
1041baa03890SNick Piggin 		dentry = old;
1042baa03890SNick Piggin 	}
1043baa03890SNick Piggin 	return dentry;
1044baa03890SNick Piggin }
1045baa03890SNick Piggin 
1046baa03890SNick Piggin /*
104744396f4bSJosef Bacik  * We already have a dentry, but require a lookup to be performed on the parent
104844396f4bSJosef Bacik  * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
104944396f4bSJosef Bacik  * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
105044396f4bSJosef Bacik  * child exists while under i_mutex.
105144396f4bSJosef Bacik  */
105244396f4bSJosef Bacik static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
105344396f4bSJosef Bacik 				     struct nameidata *nd)
105444396f4bSJosef Bacik {
105544396f4bSJosef Bacik 	struct inode *inode = parent->d_inode;
105644396f4bSJosef Bacik 	struct dentry *old;
105744396f4bSJosef Bacik 
105844396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
105944396f4bSJosef Bacik 	if (unlikely(IS_DEADDIR(inode)))
106044396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
106144396f4bSJosef Bacik 
106244396f4bSJosef Bacik 	old = inode->i_op->lookup(inode, dentry, nd);
106344396f4bSJosef Bacik 	if (unlikely(old)) {
106444396f4bSJosef Bacik 		dput(dentry);
106544396f4bSJosef Bacik 		dentry = old;
106644396f4bSJosef Bacik 	}
106744396f4bSJosef Bacik 	return dentry;
106844396f4bSJosef Bacik }
106944396f4bSJosef Bacik 
107044396f4bSJosef Bacik /*
10711da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
10721da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
10731da177e4SLinus Torvalds  *  It _is_ time-critical.
10741da177e4SLinus Torvalds  */
10751da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
107631e6b01fSNick Piggin 			struct path *path, struct inode **inode)
10771da177e4SLinus Torvalds {
10784ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
107931e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
10805a18fff2SAl Viro 	int need_reval = 1;
10815a18fff2SAl Viro 	int status = 1;
10829875cf80SDavid Howells 	int err;
10839875cf80SDavid Howells 
10843cac260aSAl Viro 	/*
1085b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1086b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1087b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1088b04f784eSNick Piggin 	 */
108931e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
109031e6b01fSNick Piggin 		unsigned seq;
109131e6b01fSNick Piggin 		*inode = nd->inode;
109231e6b01fSNick Piggin 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
10935a18fff2SAl Viro 		if (!dentry)
10945a18fff2SAl Viro 			goto unlazy;
10955a18fff2SAl Viro 
109631e6b01fSNick Piggin 		/* Memory barrier in read_seqcount_begin of child is enough */
109731e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
109831e6b01fSNick Piggin 			return -ECHILD;
109931e6b01fSNick Piggin 		nd->seq = seq;
11005a18fff2SAl Viro 
110124643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
11025a18fff2SAl Viro 			status = d_revalidate(dentry, nd);
11035a18fff2SAl Viro 			if (unlikely(status <= 0)) {
11045a18fff2SAl Viro 				if (status != -ECHILD)
11055a18fff2SAl Viro 					need_reval = 0;
11065a18fff2SAl Viro 				goto unlazy;
11075a18fff2SAl Viro 			}
110824643087SAl Viro 		}
110944396f4bSJosef Bacik 		if (unlikely(d_need_lookup(dentry)))
111044396f4bSJosef Bacik 			goto unlazy;
111131e6b01fSNick Piggin 		path->mnt = mnt;
111231e6b01fSNick Piggin 		path->dentry = dentry;
1113d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1114d6e9bd25SAl Viro 			goto unlazy;
1115d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1116d6e9bd25SAl Viro 			goto unlazy;
11179875cf80SDavid Howells 		return 0;
11185a18fff2SAl Viro unlazy:
111919660af7SAl Viro 		if (unlazy_walk(nd, dentry))
11205a18fff2SAl Viro 			return -ECHILD;
11215a18fff2SAl Viro 	} else {
112231e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
112324643087SAl Viro 	}
11245a18fff2SAl Viro 
112544396f4bSJosef Bacik 	if (dentry && unlikely(d_need_lookup(dentry))) {
112644396f4bSJosef Bacik 		dput(dentry);
112744396f4bSJosef Bacik 		dentry = NULL;
112844396f4bSJosef Bacik 	}
11295a18fff2SAl Viro retry:
11305a18fff2SAl Viro 	if (unlikely(!dentry)) {
11315a18fff2SAl Viro 		struct inode *dir = parent->d_inode;
11325a18fff2SAl Viro 		BUG_ON(nd->inode != dir);
11335a18fff2SAl Viro 
11345a18fff2SAl Viro 		mutex_lock(&dir->i_mutex);
11355a18fff2SAl Viro 		dentry = d_lookup(parent, name);
11365a18fff2SAl Viro 		if (likely(!dentry)) {
11375a18fff2SAl Viro 			dentry = d_alloc_and_lookup(parent, name, nd);
11385a18fff2SAl Viro 			if (IS_ERR(dentry)) {
11395a18fff2SAl Viro 				mutex_unlock(&dir->i_mutex);
11405a18fff2SAl Viro 				return PTR_ERR(dentry);
11415a18fff2SAl Viro 			}
11425a18fff2SAl Viro 			/* known good */
11435a18fff2SAl Viro 			need_reval = 0;
11445a18fff2SAl Viro 			status = 1;
114544396f4bSJosef Bacik 		} else if (unlikely(d_need_lookup(dentry))) {
114644396f4bSJosef Bacik 			dentry = d_inode_lookup(parent, dentry, nd);
114744396f4bSJosef Bacik 			if (IS_ERR(dentry)) {
114844396f4bSJosef Bacik 				mutex_unlock(&dir->i_mutex);
114944396f4bSJosef Bacik 				return PTR_ERR(dentry);
115044396f4bSJosef Bacik 			}
115144396f4bSJosef Bacik 			/* known good */
115244396f4bSJosef Bacik 			need_reval = 0;
115344396f4bSJosef Bacik 			status = 1;
11545a18fff2SAl Viro 		}
11555a18fff2SAl Viro 		mutex_unlock(&dir->i_mutex);
11565a18fff2SAl Viro 	}
11575a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
11585a18fff2SAl Viro 		status = d_revalidate(dentry, nd);
11595a18fff2SAl Viro 	if (unlikely(status <= 0)) {
11605a18fff2SAl Viro 		if (status < 0) {
11615a18fff2SAl Viro 			dput(dentry);
11625a18fff2SAl Viro 			return status;
11635a18fff2SAl Viro 		}
11645a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
11655a18fff2SAl Viro 			dput(dentry);
11665a18fff2SAl Viro 			dentry = NULL;
11675a18fff2SAl Viro 			need_reval = 1;
11685a18fff2SAl Viro 			goto retry;
11695a18fff2SAl Viro 		}
11705a18fff2SAl Viro 	}
11715a18fff2SAl Viro 
11721da177e4SLinus Torvalds 	path->mnt = mnt;
11731da177e4SLinus Torvalds 	path->dentry = dentry;
11749875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
117589312214SIan Kent 	if (unlikely(err < 0)) {
117689312214SIan Kent 		path_put_conditional(path, nd);
11779875cf80SDavid Howells 		return err;
117889312214SIan Kent 	}
117931e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
11801da177e4SLinus Torvalds 	return 0;
11811da177e4SLinus Torvalds }
11821da177e4SLinus Torvalds 
118352094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
118452094c8aSAl Viro {
118552094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
11864ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
118752094c8aSAl Viro 		if (err != -ECHILD)
118852094c8aSAl Viro 			return err;
118919660af7SAl Viro 		if (unlazy_walk(nd, NULL))
119052094c8aSAl Viro 			return -ECHILD;
119152094c8aSAl Viro 	}
11924ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
119352094c8aSAl Viro }
119452094c8aSAl Viro 
11959856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
11969856fa1bSAl Viro {
11979856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
11989856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
11999856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
12009856fa1bSAl Viro 				return -ECHILD;
12019856fa1bSAl Viro 		} else
12029856fa1bSAl Viro 			follow_dotdot(nd);
12039856fa1bSAl Viro 	}
12049856fa1bSAl Viro 	return 0;
12059856fa1bSAl Viro }
12069856fa1bSAl Viro 
1207951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1208951361f9SAl Viro {
1209951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1210951361f9SAl Viro 		path_put(&nd->path);
1211951361f9SAl Viro 	} else {
1212951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
12135b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1214951361f9SAl Viro 			nd->root.mnt = NULL;
1215951361f9SAl Viro 		rcu_read_unlock();
1216951361f9SAl Viro 		br_read_unlock(vfsmount_lock);
1217951361f9SAl Viro 	}
1218951361f9SAl Viro }
1219951361f9SAl Viro 
1220ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
1221ce57dfc1SAl Viro 		struct qstr *name, int type, int follow)
1222ce57dfc1SAl Viro {
1223ce57dfc1SAl Viro 	struct inode *inode;
1224ce57dfc1SAl Viro 	int err;
1225ce57dfc1SAl Viro 	/*
1226ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1227ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1228ce57dfc1SAl Viro 	 * parent relationships.
1229ce57dfc1SAl Viro 	 */
1230ce57dfc1SAl Viro 	if (unlikely(type != LAST_NORM))
1231ce57dfc1SAl Viro 		return handle_dots(nd, type);
1232ce57dfc1SAl Viro 	err = do_lookup(nd, name, path, &inode);
1233ce57dfc1SAl Viro 	if (unlikely(err)) {
1234ce57dfc1SAl Viro 		terminate_walk(nd);
1235ce57dfc1SAl Viro 		return err;
1236ce57dfc1SAl Viro 	}
1237ce57dfc1SAl Viro 	if (!inode) {
1238ce57dfc1SAl Viro 		path_to_nameidata(path, nd);
1239ce57dfc1SAl Viro 		terminate_walk(nd);
1240ce57dfc1SAl Viro 		return -ENOENT;
1241ce57dfc1SAl Viro 	}
1242ce57dfc1SAl Viro 	if (unlikely(inode->i_op->follow_link) && follow) {
124319660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
124419660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
124519660af7SAl Viro 				terminate_walk(nd);
1246ce57dfc1SAl Viro 				return -ECHILD;
124719660af7SAl Viro 			}
124819660af7SAl Viro 		}
1249ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1250ce57dfc1SAl Viro 		return 1;
1251ce57dfc1SAl Viro 	}
1252ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1253ce57dfc1SAl Viro 	nd->inode = inode;
1254ce57dfc1SAl Viro 	return 0;
1255ce57dfc1SAl Viro }
1256ce57dfc1SAl Viro 
12571da177e4SLinus Torvalds /*
1258b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1259b356379aSAl Viro  * limiting consecutive symlinks to 40.
1260b356379aSAl Viro  *
1261b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1262b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1263b356379aSAl Viro  */
1264b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1265b356379aSAl Viro {
1266b356379aSAl Viro 	int res;
1267b356379aSAl Viro 
1268b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1269b356379aSAl Viro 		path_put_conditional(path, nd);
1270b356379aSAl Viro 		path_put(&nd->path);
1271b356379aSAl Viro 		return -ELOOP;
1272b356379aSAl Viro 	}
12731a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1274b356379aSAl Viro 
1275b356379aSAl Viro 	nd->depth++;
1276b356379aSAl Viro 	current->link_count++;
1277b356379aSAl Viro 
1278b356379aSAl Viro 	do {
1279b356379aSAl Viro 		struct path link = *path;
1280b356379aSAl Viro 		void *cookie;
1281574197e0SAl Viro 
1282574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
1283b356379aSAl Viro 		if (!res)
1284b356379aSAl Viro 			res = walk_component(nd, path, &nd->last,
1285b356379aSAl Viro 					     nd->last_type, LOOKUP_FOLLOW);
1286574197e0SAl Viro 		put_link(nd, &link, cookie);
1287b356379aSAl Viro 	} while (res > 0);
1288b356379aSAl Viro 
1289b356379aSAl Viro 	current->link_count--;
1290b356379aSAl Viro 	nd->depth--;
1291b356379aSAl Viro 	return res;
1292b356379aSAl Viro }
1293b356379aSAl Viro 
1294b356379aSAl Viro /*
12951da177e4SLinus Torvalds  * Name resolution.
1296ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1297ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
12981da177e4SLinus Torvalds  *
1299ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1300ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
13011da177e4SLinus Torvalds  */
13026de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
13031da177e4SLinus Torvalds {
13041da177e4SLinus Torvalds 	struct path next;
13051da177e4SLinus Torvalds 	int err;
13061da177e4SLinus Torvalds 	unsigned int lookup_flags = nd->flags;
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 	while (*name=='/')
13091da177e4SLinus Torvalds 		name++;
13101da177e4SLinus Torvalds 	if (!*name)
1311086e183aSAl Viro 		return 0;
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
13141da177e4SLinus Torvalds 	for(;;) {
13151da177e4SLinus Torvalds 		unsigned long hash;
13161da177e4SLinus Torvalds 		struct qstr this;
13171da177e4SLinus Torvalds 		unsigned int c;
1318fe479a58SAl Viro 		int type;
13191da177e4SLinus Torvalds 
1320cdce5d6bSTrond Myklebust 		nd->flags |= LOOKUP_CONTINUE;
132152094c8aSAl Viro 
132252094c8aSAl Viro 		err = may_lookup(nd);
13231da177e4SLinus Torvalds  		if (err)
13241da177e4SLinus Torvalds 			break;
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 		this.name = name;
13271da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 		hash = init_name_hash();
13301da177e4SLinus Torvalds 		do {
13311da177e4SLinus Torvalds 			name++;
13321da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
13331da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
13341da177e4SLinus Torvalds 		} while (c && (c != '/'));
13351da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
13361da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
13371da177e4SLinus Torvalds 
1338fe479a58SAl Viro 		type = LAST_NORM;
1339fe479a58SAl Viro 		if (this.name[0] == '.') switch (this.len) {
1340fe479a58SAl Viro 			case 2:
134116c2cd71SAl Viro 				if (this.name[1] == '.') {
1342fe479a58SAl Viro 					type = LAST_DOTDOT;
134316c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
134416c2cd71SAl Viro 				}
1345fe479a58SAl Viro 				break;
1346fe479a58SAl Viro 			case 1:
1347fe479a58SAl Viro 				type = LAST_DOT;
1348fe479a58SAl Viro 		}
13495a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
13505a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
135116c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
13525a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
13535a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
13545a202bcdSAl Viro 							   &this);
13555a202bcdSAl Viro 				if (err < 0)
13565a202bcdSAl Viro 					break;
13575a202bcdSAl Viro 			}
13585a202bcdSAl Viro 		}
1359fe479a58SAl Viro 
13601da177e4SLinus Torvalds 		/* remove trailing slashes? */
13611da177e4SLinus Torvalds 		if (!c)
13621da177e4SLinus Torvalds 			goto last_component;
13631da177e4SLinus Torvalds 		while (*++name == '/');
13641da177e4SLinus Torvalds 		if (!*name)
1365b356379aSAl Viro 			goto last_component;
13661da177e4SLinus Torvalds 
1367ce57dfc1SAl Viro 		err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1368ce57dfc1SAl Viro 		if (err < 0)
1369ce57dfc1SAl Viro 			return err;
1370fe479a58SAl Viro 
1371ce57dfc1SAl Viro 		if (err) {
1372b356379aSAl Viro 			err = nested_symlink(&next, nd);
13731da177e4SLinus Torvalds 			if (err)
1374a7472babSAl Viro 				return err;
137531e6b01fSNick Piggin 		}
13761da177e4SLinus Torvalds 		err = -ENOTDIR;
137731e6b01fSNick Piggin 		if (!nd->inode->i_op->lookup)
13781da177e4SLinus Torvalds 			break;
13791da177e4SLinus Torvalds 		continue;
13801da177e4SLinus Torvalds 		/* here ends the main loop */
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds last_component:
1383f55eab82STrond Myklebust 		/* Clear LOOKUP_CONTINUE iff it was previously unset */
1384f55eab82STrond Myklebust 		nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
1385ce57dfc1SAl Viro 		nd->last = this;
1386ce57dfc1SAl Viro 		nd->last_type = type;
1387ce57dfc1SAl Viro 		return 0;
1388ce57dfc1SAl Viro 	}
1389951361f9SAl Viro 	terminate_walk(nd);
13901da177e4SLinus Torvalds 	return err;
13911da177e4SLinus Torvalds }
13921da177e4SLinus Torvalds 
139370e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
139470e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
139531e6b01fSNick Piggin {
139631e6b01fSNick Piggin 	int retval = 0;
139731e6b01fSNick Piggin 	int fput_needed;
139831e6b01fSNick Piggin 	struct file *file;
139931e6b01fSNick Piggin 
140031e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
140116c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
140231e6b01fSNick Piggin 	nd->depth = 0;
14035b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
14045b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
140573d049a4SAl Viro 		if (*name) {
14065b6ca027SAl Viro 			if (!inode->i_op->lookup)
14075b6ca027SAl Viro 				return -ENOTDIR;
14085b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
14095b6ca027SAl Viro 			if (retval)
14105b6ca027SAl Viro 				return retval;
141173d049a4SAl Viro 		}
14125b6ca027SAl Viro 		nd->path = nd->root;
14135b6ca027SAl Viro 		nd->inode = inode;
14145b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
14155b6ca027SAl Viro 			br_read_lock(vfsmount_lock);
14165b6ca027SAl Viro 			rcu_read_lock();
14175b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
14185b6ca027SAl Viro 		} else {
14195b6ca027SAl Viro 			path_get(&nd->path);
14205b6ca027SAl Viro 		}
14215b6ca027SAl Viro 		return 0;
14225b6ca027SAl Viro 	}
14235b6ca027SAl Viro 
142431e6b01fSNick Piggin 	nd->root.mnt = NULL;
142531e6b01fSNick Piggin 
142631e6b01fSNick Piggin 	if (*name=='/') {
1427e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
142831e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
142931e6b01fSNick Piggin 			rcu_read_lock();
1430e41f7d4eSAl Viro 			set_root_rcu(nd);
1431e41f7d4eSAl Viro 		} else {
1432e41f7d4eSAl Viro 			set_root(nd);
1433e41f7d4eSAl Viro 			path_get(&nd->root);
1434e41f7d4eSAl Viro 		}
143531e6b01fSNick Piggin 		nd->path = nd->root;
143631e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1437e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
143831e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1439c28cc364SNick Piggin 			unsigned seq;
144031e6b01fSNick Piggin 
144131e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
144231e6b01fSNick Piggin 			rcu_read_lock();
144331e6b01fSNick Piggin 
1444c28cc364SNick Piggin 			do {
1445c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
144631e6b01fSNick Piggin 				nd->path = fs->pwd;
1447c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1448c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1449e41f7d4eSAl Viro 		} else {
1450e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1451e41f7d4eSAl Viro 		}
145231e6b01fSNick Piggin 	} else {
145331e6b01fSNick Piggin 		struct dentry *dentry;
145431e6b01fSNick Piggin 
14551abf0c71SAl Viro 		file = fget_raw_light(dfd, &fput_needed);
145631e6b01fSNick Piggin 		retval = -EBADF;
145731e6b01fSNick Piggin 		if (!file)
145831e6b01fSNick Piggin 			goto out_fail;
145931e6b01fSNick Piggin 
146031e6b01fSNick Piggin 		dentry = file->f_path.dentry;
146131e6b01fSNick Piggin 
1462f52e0c11SAl Viro 		if (*name) {
146331e6b01fSNick Piggin 			retval = -ENOTDIR;
146431e6b01fSNick Piggin 			if (!S_ISDIR(dentry->d_inode->i_mode))
146531e6b01fSNick Piggin 				goto fput_fail;
146631e6b01fSNick Piggin 
14674ad5abb3SAl Viro 			retval = inode_permission(dentry->d_inode, MAY_EXEC);
146831e6b01fSNick Piggin 			if (retval)
146931e6b01fSNick Piggin 				goto fput_fail;
1470f52e0c11SAl Viro 		}
147131e6b01fSNick Piggin 
147231e6b01fSNick Piggin 		nd->path = file->f_path;
1473e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
147431e6b01fSNick Piggin 			if (fput_needed)
147570e9b357SAl Viro 				*fp = file;
1476c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
147731e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
147831e6b01fSNick Piggin 			rcu_read_lock();
14795590ff0dSUlrich Drepper 		} else {
14805dd784d0SJan Blunck 			path_get(&file->f_path);
14815590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
14821da177e4SLinus Torvalds 		}
1483e41f7d4eSAl Viro 	}
1484e41f7d4eSAl Viro 
148531e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
14869b4a9b14SAl Viro 	return 0;
14872dfdd266SJosef 'Jeff' Sipek 
14889b4a9b14SAl Viro fput_fail:
14899b4a9b14SAl Viro 	fput_light(file, fput_needed);
14909b4a9b14SAl Viro out_fail:
14919b4a9b14SAl Viro 	return retval;
14929b4a9b14SAl Viro }
14939b4a9b14SAl Viro 
1494bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1495bd92d7feSAl Viro {
1496bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1497bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1498bd92d7feSAl Viro 
1499bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1500bd92d7feSAl Viro 	return walk_component(nd, path, &nd->last, nd->last_type,
1501bd92d7feSAl Viro 					nd->flags & LOOKUP_FOLLOW);
1502bd92d7feSAl Viro }
1503bd92d7feSAl Viro 
15049b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1505ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
15069b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
15079b4a9b14SAl Viro {
150870e9b357SAl Viro 	struct file *base = NULL;
1509bd92d7feSAl Viro 	struct path path;
1510bd92d7feSAl Viro 	int err;
151131e6b01fSNick Piggin 
151231e6b01fSNick Piggin 	/*
151331e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
151431e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
151531e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
151631e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
151731e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
151831e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
151931e6b01fSNick Piggin 	 * analogue, foo_rcu().
152031e6b01fSNick Piggin 	 *
152131e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
152231e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
152331e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
152431e6b01fSNick Piggin 	 * be able to complete).
152531e6b01fSNick Piggin 	 */
1526bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1527ee0827cdSAl Viro 
1528bd92d7feSAl Viro 	if (unlikely(err))
1529bd92d7feSAl Viro 		return err;
1530ee0827cdSAl Viro 
1531ee0827cdSAl Viro 	current->total_link_count = 0;
1532bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1533bd92d7feSAl Viro 
1534bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1535bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1536bd92d7feSAl Viro 		while (err > 0) {
1537bd92d7feSAl Viro 			void *cookie;
1538bd92d7feSAl Viro 			struct path link = path;
1539bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1540574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
1541bd92d7feSAl Viro 			if (!err)
1542bd92d7feSAl Viro 				err = lookup_last(nd, &path);
1543574197e0SAl Viro 			put_link(nd, &link, cookie);
1544bd92d7feSAl Viro 		}
1545bd92d7feSAl Viro 	}
1546ee0827cdSAl Viro 
15479f1fafeeSAl Viro 	if (!err)
15489f1fafeeSAl Viro 		err = complete_walk(nd);
1549bd92d7feSAl Viro 
1550bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1551bd92d7feSAl Viro 		if (!nd->inode->i_op->lookup) {
1552bd92d7feSAl Viro 			path_put(&nd->path);
1553bd23a539SAl Viro 			err = -ENOTDIR;
1554bd92d7feSAl Viro 		}
1555bd92d7feSAl Viro 	}
155616c2cd71SAl Viro 
155770e9b357SAl Viro 	if (base)
155870e9b357SAl Viro 		fput(base);
1559ee0827cdSAl Viro 
15605b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
156131e6b01fSNick Piggin 		path_put(&nd->root);
156231e6b01fSNick Piggin 		nd->root.mnt = NULL;
156331e6b01fSNick Piggin 	}
1564bd92d7feSAl Viro 	return err;
156531e6b01fSNick Piggin }
156631e6b01fSNick Piggin 
1567ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1568ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1569ee0827cdSAl Viro {
1570ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1571ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1572ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1573ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1574ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1575ee0827cdSAl Viro 
157631e6b01fSNick Piggin 	if (likely(!retval)) {
157731e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
157831e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
157931e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
158031e6b01fSNick Piggin 		}
158131e6b01fSNick Piggin 	}
1582170aa3d0SUlrich Drepper 	return retval;
15831da177e4SLinus Torvalds }
15841da177e4SLinus Torvalds 
1585c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd)
15865590ff0dSUlrich Drepper {
1587c9c6cac0SAl Viro 	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
15885590ff0dSUlrich Drepper }
15895590ff0dSUlrich Drepper 
1590d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1591d1811465SAl Viro {
1592d1811465SAl Viro 	struct nameidata nd;
1593d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1594d1811465SAl Viro 	if (!res)
1595d1811465SAl Viro 		*path = nd.path;
1596d1811465SAl Viro 	return res;
1597d1811465SAl Viro }
1598d1811465SAl Viro 
159916f18200SJosef 'Jeff' Sipek /**
160016f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
160116f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
160216f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
160316f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
160416f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
160516f18200SJosef 'Jeff' Sipek  * @nd: pointer to nameidata
160616f18200SJosef 'Jeff' Sipek  */
160716f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
160816f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
160916f18200SJosef 'Jeff' Sipek 		    struct nameidata *nd)
161016f18200SJosef 'Jeff' Sipek {
16115b6ca027SAl Viro 	nd->root.dentry = dentry;
16125b6ca027SAl Viro 	nd->root.mnt = mnt;
16135b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
16145b6ca027SAl Viro 	return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
161516f18200SJosef 'Jeff' Sipek }
161616f18200SJosef 'Jeff' Sipek 
1617eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1618eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
16191da177e4SLinus Torvalds {
162081fca444SChristoph Hellwig 	struct inode *inode = base->d_inode;
16211da177e4SLinus Torvalds 	struct dentry *dentry;
16221da177e4SLinus Torvalds 	int err;
16231da177e4SLinus Torvalds 
16244ad5abb3SAl Viro 	err = inode_permission(inode, MAY_EXEC);
162581fca444SChristoph Hellwig 	if (err)
162681fca444SChristoph Hellwig 		return ERR_PTR(err);
16271da177e4SLinus Torvalds 
16281da177e4SLinus Torvalds 	/*
1629b04f784eSNick Piggin 	 * Don't bother with __d_lookup: callers are for creat as
1630b04f784eSNick Piggin 	 * well as unlink, so a lot of the time it would cost
1631b04f784eSNick Piggin 	 * a double lookup.
16326e6b1bd1SAl Viro 	 */
16336e6b1bd1SAl Viro 	dentry = d_lookup(base, name);
16346e6b1bd1SAl Viro 
163544396f4bSJosef Bacik 	if (dentry && d_need_lookup(dentry)) {
163644396f4bSJosef Bacik 		/*
163744396f4bSJosef Bacik 		 * __lookup_hash is called with the parent dir's i_mutex already
163844396f4bSJosef Bacik 		 * held, so we are good to go here.
163944396f4bSJosef Bacik 		 */
164044396f4bSJosef Bacik 		dentry = d_inode_lookup(base, dentry, nd);
164144396f4bSJosef Bacik 		if (IS_ERR(dentry))
164244396f4bSJosef Bacik 			return dentry;
164344396f4bSJosef Bacik 	}
164444396f4bSJosef Bacik 
1645fb045adbSNick Piggin 	if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
16466e6b1bd1SAl Viro 		dentry = do_revalidate(dentry, nd);
16476e6b1bd1SAl Viro 
16481da177e4SLinus Torvalds 	if (!dentry)
1649baa03890SNick Piggin 		dentry = d_alloc_and_lookup(base, name, nd);
16505a202bcdSAl Viro 
16511da177e4SLinus Torvalds 	return dentry;
16521da177e4SLinus Torvalds }
16531da177e4SLinus Torvalds 
1654057f6c01SJames Morris /*
1655057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1656057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1657057f6c01SJames Morris  * SMP-safe.
1658057f6c01SJames Morris  */
1659a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
16601da177e4SLinus Torvalds {
16614ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
16621da177e4SLinus Torvalds }
16631da177e4SLinus Torvalds 
1664eead1911SChristoph Hellwig /**
1665a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1666eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1667eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1668eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1669eead1911SChristoph Hellwig  *
1670a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1671a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1672eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1673eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1674eead1911SChristoph Hellwig  */
1675057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1676057f6c01SJames Morris {
1677057f6c01SJames Morris 	struct qstr this;
16786a96ba54SAl Viro 	unsigned long hash;
16796a96ba54SAl Viro 	unsigned int c;
1680057f6c01SJames Morris 
16812f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
16822f9092e1SDavid Woodhouse 
16836a96ba54SAl Viro 	this.name = name;
16846a96ba54SAl Viro 	this.len = len;
16856a96ba54SAl Viro 	if (!len)
16866a96ba54SAl Viro 		return ERR_PTR(-EACCES);
16876a96ba54SAl Viro 
16886a96ba54SAl Viro 	hash = init_name_hash();
16896a96ba54SAl Viro 	while (len--) {
16906a96ba54SAl Viro 		c = *(const unsigned char *)name++;
16916a96ba54SAl Viro 		if (c == '/' || c == '\0')
16926a96ba54SAl Viro 			return ERR_PTR(-EACCES);
16936a96ba54SAl Viro 		hash = partial_name_hash(c, hash);
16946a96ba54SAl Viro 	}
16956a96ba54SAl Viro 	this.hash = end_name_hash(hash);
16965a202bcdSAl Viro 	/*
16975a202bcdSAl Viro 	 * See if the low-level filesystem might want
16985a202bcdSAl Viro 	 * to use its own hash..
16995a202bcdSAl Viro 	 */
17005a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
17015a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
17025a202bcdSAl Viro 		if (err < 0)
17035a202bcdSAl Viro 			return ERR_PTR(err);
17045a202bcdSAl Viro 	}
1705eead1911SChristoph Hellwig 
170649705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1707057f6c01SJames Morris }
1708057f6c01SJames Morris 
17092d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
17102d8f3038SAl Viro 		 struct path *path)
17111da177e4SLinus Torvalds {
17122d8f3038SAl Viro 	struct nameidata nd;
1713f52e0c11SAl Viro 	char *tmp = getname_flags(name, flags);
17141da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
17151da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
17162d8f3038SAl Viro 
17172d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
17182d8f3038SAl Viro 
17192d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
17201da177e4SLinus Torvalds 		putname(tmp);
17212d8f3038SAl Viro 		if (!err)
17222d8f3038SAl Viro 			*path = nd.path;
17231da177e4SLinus Torvalds 	}
17241da177e4SLinus Torvalds 	return err;
17251da177e4SLinus Torvalds }
17261da177e4SLinus Torvalds 
17272ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
17282ad94ae6SAl Viro 			struct nameidata *nd, char **name)
17292ad94ae6SAl Viro {
17302ad94ae6SAl Viro 	char *s = getname(path);
17312ad94ae6SAl Viro 	int error;
17322ad94ae6SAl Viro 
17332ad94ae6SAl Viro 	if (IS_ERR(s))
17342ad94ae6SAl Viro 		return PTR_ERR(s);
17352ad94ae6SAl Viro 
17362ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
17372ad94ae6SAl Viro 	if (error)
17382ad94ae6SAl Viro 		putname(s);
17392ad94ae6SAl Viro 	else
17402ad94ae6SAl Viro 		*name = s;
17412ad94ae6SAl Viro 
17422ad94ae6SAl Viro 	return error;
17432ad94ae6SAl Viro }
17442ad94ae6SAl Viro 
17451da177e4SLinus Torvalds /*
17461da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
17471da177e4SLinus Torvalds  * minimal.
17481da177e4SLinus Torvalds  */
17491da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
17501da177e4SLinus Torvalds {
1751da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1752da9592edSDavid Howells 
17531da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
17541da177e4SLinus Torvalds 		return 0;
1755e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
1756e795b717SSerge E. Hallyn 		goto other_userns;
1757da9592edSDavid Howells 	if (inode->i_uid == fsuid)
17581da177e4SLinus Torvalds 		return 0;
1759da9592edSDavid Howells 	if (dir->i_uid == fsuid)
17601da177e4SLinus Torvalds 		return 0;
1761e795b717SSerge E. Hallyn 
1762e795b717SSerge E. Hallyn other_userns:
1763e795b717SSerge E. Hallyn 	return !ns_capable(inode_userns(inode), CAP_FOWNER);
17641da177e4SLinus Torvalds }
17651da177e4SLinus Torvalds 
17661da177e4SLinus Torvalds /*
17671da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
17681da177e4SLinus Torvalds  *  whether the type of victim is right.
17691da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
17701da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
17711da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
17721da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
17731da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
17741da177e4SLinus Torvalds  *	a. be owner of dir, or
17751da177e4SLinus Torvalds  *	b. be owner of victim, or
17761da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
17771da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
17781da177e4SLinus Torvalds  *     links pointing to it.
17791da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
17801da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
17811da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
17821da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
17831da177e4SLinus Torvalds  *     nfs_async_unlink().
17841da177e4SLinus Torvalds  */
1785858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
17861da177e4SLinus Torvalds {
17871da177e4SLinus Torvalds 	int error;
17881da177e4SLinus Torvalds 
17891da177e4SLinus Torvalds 	if (!victim->d_inode)
17901da177e4SLinus Torvalds 		return -ENOENT;
17911da177e4SLinus Torvalds 
17921da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1793cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
17941da177e4SLinus Torvalds 
1795f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
17961da177e4SLinus Torvalds 	if (error)
17971da177e4SLinus Torvalds 		return error;
17981da177e4SLinus Torvalds 	if (IS_APPEND(dir))
17991da177e4SLinus Torvalds 		return -EPERM;
18001da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1801f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
18021da177e4SLinus Torvalds 		return -EPERM;
18031da177e4SLinus Torvalds 	if (isdir) {
18041da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
18051da177e4SLinus Torvalds 			return -ENOTDIR;
18061da177e4SLinus Torvalds 		if (IS_ROOT(victim))
18071da177e4SLinus Torvalds 			return -EBUSY;
18081da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
18091da177e4SLinus Torvalds 		return -EISDIR;
18101da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18111da177e4SLinus Torvalds 		return -ENOENT;
18121da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
18131da177e4SLinus Torvalds 		return -EBUSY;
18141da177e4SLinus Torvalds 	return 0;
18151da177e4SLinus Torvalds }
18161da177e4SLinus Torvalds 
18171da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
18181da177e4SLinus Torvalds  *  dir.
18191da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
18201da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
18211da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
18221da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
18231da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
18241da177e4SLinus Torvalds  */
1825a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
18261da177e4SLinus Torvalds {
18271da177e4SLinus Torvalds 	if (child->d_inode)
18281da177e4SLinus Torvalds 		return -EEXIST;
18291da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18301da177e4SLinus Torvalds 		return -ENOENT;
1831f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
18321da177e4SLinus Torvalds }
18331da177e4SLinus Torvalds 
18341da177e4SLinus Torvalds /*
18351da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
18361da177e4SLinus Torvalds  */
18371da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
18381da177e4SLinus Torvalds {
18391da177e4SLinus Torvalds 	struct dentry *p;
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds 	if (p1 == p2) {
1842f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
18431da177e4SLinus Torvalds 		return NULL;
18441da177e4SLinus Torvalds 	}
18451da177e4SLinus Torvalds 
1846a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
18471da177e4SLinus Torvalds 
1848e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1849e2761a11SOGAWA Hirofumi 	if (p) {
1850f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1851f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
18521da177e4SLinus Torvalds 		return p;
18531da177e4SLinus Torvalds 	}
18541da177e4SLinus Torvalds 
1855e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1856e2761a11SOGAWA Hirofumi 	if (p) {
1857f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1858f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
18591da177e4SLinus Torvalds 		return p;
18601da177e4SLinus Torvalds 	}
18611da177e4SLinus Torvalds 
1862f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1863f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
18641da177e4SLinus Torvalds 	return NULL;
18651da177e4SLinus Torvalds }
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
18681da177e4SLinus Torvalds {
18691b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
18701da177e4SLinus Torvalds 	if (p1 != p2) {
18711b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1872a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
18731da177e4SLinus Torvalds 	}
18741da177e4SLinus Torvalds }
18751da177e4SLinus Torvalds 
18761da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
18771da177e4SLinus Torvalds 		struct nameidata *nd)
18781da177e4SLinus Torvalds {
1879a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
18801da177e4SLinus Torvalds 
18811da177e4SLinus Torvalds 	if (error)
18821da177e4SLinus Torvalds 		return error;
18831da177e4SLinus Torvalds 
1884acfa4380SAl Viro 	if (!dir->i_op->create)
18851da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
18861da177e4SLinus Torvalds 	mode &= S_IALLUGO;
18871da177e4SLinus Torvalds 	mode |= S_IFREG;
18881da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
18891da177e4SLinus Torvalds 	if (error)
18901da177e4SLinus Torvalds 		return error;
18911da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1892a74574aaSStephen Smalley 	if (!error)
1893f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
18941da177e4SLinus Torvalds 	return error;
18951da177e4SLinus Torvalds }
18961da177e4SLinus Torvalds 
189773d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
18981da177e4SLinus Torvalds {
18993fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
19001da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
19011da177e4SLinus Torvalds 	int error;
19021da177e4SLinus Torvalds 
1903bcda7652SAl Viro 	/* O_PATH? */
1904bcda7652SAl Viro 	if (!acc_mode)
1905bcda7652SAl Viro 		return 0;
1906bcda7652SAl Viro 
19071da177e4SLinus Torvalds 	if (!inode)
19081da177e4SLinus Torvalds 		return -ENOENT;
19091da177e4SLinus Torvalds 
1910c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1911c8fe8f30SChristoph Hellwig 	case S_IFLNK:
19121da177e4SLinus Torvalds 		return -ELOOP;
1913c8fe8f30SChristoph Hellwig 	case S_IFDIR:
1914c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
19151da177e4SLinus Torvalds 			return -EISDIR;
1916c8fe8f30SChristoph Hellwig 		break;
1917c8fe8f30SChristoph Hellwig 	case S_IFBLK:
1918c8fe8f30SChristoph Hellwig 	case S_IFCHR:
19193fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
19201da177e4SLinus Torvalds 			return -EACCES;
1921c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
1922c8fe8f30SChristoph Hellwig 	case S_IFIFO:
1923c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
19241da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
1925c8fe8f30SChristoph Hellwig 		break;
19264a3fd211SDave Hansen 	}
1927b41572e9SDave Hansen 
19283fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
1929b41572e9SDave Hansen 	if (error)
1930b41572e9SDave Hansen 		return error;
19316146f0d5SMimi Zohar 
19321da177e4SLinus Torvalds 	/*
19331da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
19341da177e4SLinus Torvalds 	 */
19351da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
19368737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
19377715b521SAl Viro 			return -EPERM;
19381da177e4SLinus Torvalds 		if (flag & O_TRUNC)
19397715b521SAl Viro 			return -EPERM;
19401da177e4SLinus Torvalds 	}
19411da177e4SLinus Torvalds 
19421da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
19432e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
19447715b521SAl Viro 		return -EPERM;
19451da177e4SLinus Torvalds 
19461da177e4SLinus Torvalds 	/*
19471da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
19481da177e4SLinus Torvalds 	 */
1949b65a9cfcSAl Viro 	return break_lease(inode, flag);
19507715b521SAl Viro }
19517715b521SAl Viro 
1952e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
19537715b521SAl Viro {
1954e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
19557715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
19567715b521SAl Viro 	int error = get_write_access(inode);
19571da177e4SLinus Torvalds 	if (error)
19587715b521SAl Viro 		return error;
19591da177e4SLinus Torvalds 	/*
19601da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
19611da177e4SLinus Torvalds 	 */
19621da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
1963be6d3e56SKentaro Takeda 	if (!error)
1964ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
19651da177e4SLinus Torvalds 	if (!error) {
19667715b521SAl Viro 		error = do_truncate(path->dentry, 0,
1967d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1968e1181ee6SJeff Layton 				    filp);
19691da177e4SLinus Torvalds 	}
19701da177e4SLinus Torvalds 	put_write_access(inode);
1971acd0c935SMimi Zohar 	return error;
19721da177e4SLinus Torvalds }
19731da177e4SLinus Torvalds 
1974d57999e1SDave Hansen /*
1975d57999e1SDave Hansen  * Note that while the flag value (low two bits) for sys_open means:
1976d57999e1SDave Hansen  *	00 - read-only
1977d57999e1SDave Hansen  *	01 - write-only
1978d57999e1SDave Hansen  *	10 - read-write
1979d57999e1SDave Hansen  *	11 - special
1980d57999e1SDave Hansen  * it is changed into
1981d57999e1SDave Hansen  *	00 - no permissions needed
1982d57999e1SDave Hansen  *	01 - read-permission
1983d57999e1SDave Hansen  *	10 - write-permission
1984d57999e1SDave Hansen  *	11 - read-write
1985d57999e1SDave Hansen  * for the internal routines (ie open_namei()/follow_link() etc)
1986d57999e1SDave Hansen  * This is more logical, and also allows the 00 "no perm needed"
1987d57999e1SDave Hansen  * to be used for symlinks (where the permissions are checked
1988d57999e1SDave Hansen  * later).
1989d57999e1SDave Hansen  *
1990d57999e1SDave Hansen */
1991d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
1992d57999e1SDave Hansen {
1993d57999e1SDave Hansen 	if ((flag+1) & O_ACCMODE)
1994d57999e1SDave Hansen 		flag++;
1995d57999e1SDave Hansen 	return flag;
1996d57999e1SDave Hansen }
1997d57999e1SDave Hansen 
199831e6b01fSNick Piggin /*
1999fe2d35ffSAl Viro  * Handle the last step of open()
200031e6b01fSNick Piggin  */
2001fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
2002c3e380b0SAl Viro 			    const struct open_flags *op, const char *pathname)
2003fb1cc555SAl Viro {
2004a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
20056c0d46c4SAl Viro 	struct dentry *dentry;
2006ca344a89SAl Viro 	int open_flag = op->open_flag;
20076c0d46c4SAl Viro 	int will_truncate = open_flag & O_TRUNC;
2008ca344a89SAl Viro 	int want_write = 0;
2009bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2010fb1cc555SAl Viro 	struct file *filp;
201116c2cd71SAl Viro 	int error;
2012fb1cc555SAl Viro 
2013c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2014c3e380b0SAl Viro 	nd->flags |= op->intent;
2015c3e380b0SAl Viro 
20161f36f774SAl Viro 	switch (nd->last_type) {
20171f36f774SAl Viro 	case LAST_DOTDOT:
2018176306f5SNeil Brown 	case LAST_DOT:
2019fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2020fe2d35ffSAl Viro 		if (error)
2021fe2d35ffSAl Viro 			return ERR_PTR(error);
20221f36f774SAl Viro 		/* fallthrough */
20231f36f774SAl Viro 	case LAST_ROOT:
20249f1fafeeSAl Viro 		error = complete_walk(nd);
202516c2cd71SAl Viro 		if (error)
20269f1fafeeSAl Viro 			return ERR_PTR(error);
2027fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2028ca344a89SAl Viro 		if (open_flag & O_CREAT) {
202916c2cd71SAl Viro 			error = -EISDIR;
20301f36f774SAl Viro 			goto exit;
2031fe2d35ffSAl Viro 		}
2032fe2d35ffSAl Viro 		goto ok;
20331f36f774SAl Viro 	case LAST_BIND:
20349f1fafeeSAl Viro 		error = complete_walk(nd);
203516c2cd71SAl Viro 		if (error)
20369f1fafeeSAl Viro 			return ERR_PTR(error);
20371f36f774SAl Viro 		audit_inode(pathname, dir);
20381f36f774SAl Viro 		goto ok;
20391f36f774SAl Viro 	}
2040a2c36b45SAl Viro 
2041ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2042bcda7652SAl Viro 		int symlink_ok = 0;
2043fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2044fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2045bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2046bcda7652SAl Viro 			symlink_ok = 1;
2047fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2048ce57dfc1SAl Viro 		error = walk_component(nd, path, &nd->last, LAST_NORM,
2049ce57dfc1SAl Viro 					!symlink_ok);
2050ce57dfc1SAl Viro 		if (error < 0)
2051fe2d35ffSAl Viro 			return ERR_PTR(error);
2052ce57dfc1SAl Viro 		if (error) /* symlink */
2053fe2d35ffSAl Viro 			return NULL;
2054fe2d35ffSAl Viro 		/* sayonara */
20559f1fafeeSAl Viro 		error = complete_walk(nd);
20569f1fafeeSAl Viro 		if (error)
2057fe2d35ffSAl Viro 			return ERR_PTR(-ECHILD);
2058fe2d35ffSAl Viro 
2059fe2d35ffSAl Viro 		error = -ENOTDIR;
2060fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_DIRECTORY) {
2061ce57dfc1SAl Viro 			if (!nd->inode->i_op->lookup)
2062fe2d35ffSAl Viro 				goto exit;
2063fe2d35ffSAl Viro 		}
2064fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2065fe2d35ffSAl Viro 		goto ok;
2066fe2d35ffSAl Viro 	}
2067fe2d35ffSAl Viro 
2068fe2d35ffSAl Viro 	/* create side of things */
20699f1fafeeSAl Viro 	error = complete_walk(nd);
20709f1fafeeSAl Viro 	if (error)
20719f1fafeeSAl Viro 		return ERR_PTR(error);
2072fe2d35ffSAl Viro 
2073fe2d35ffSAl Viro 	audit_inode(pathname, dir);
207416c2cd71SAl Viro 	error = -EISDIR;
20751f36f774SAl Viro 	/* trailing slashes? */
207631e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
20771f36f774SAl Viro 		goto exit;
20781f36f774SAl Viro 
2079a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2080a1e28038SAl Viro 
20816c0d46c4SAl Viro 	dentry = lookup_hash(nd);
20826c0d46c4SAl Viro 	error = PTR_ERR(dentry);
20836c0d46c4SAl Viro 	if (IS_ERR(dentry)) {
2084fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2085fb1cc555SAl Viro 		goto exit;
2086fb1cc555SAl Viro 	}
2087fb1cc555SAl Viro 
20886c0d46c4SAl Viro 	path->dentry = dentry;
20896c0d46c4SAl Viro 	path->mnt = nd->path.mnt;
20906c0d46c4SAl Viro 
2091fb1cc555SAl Viro 	/* Negative dentry, just create the file */
20926c0d46c4SAl Viro 	if (!dentry->d_inode) {
20936c0d46c4SAl Viro 		int mode = op->mode;
20946c0d46c4SAl Viro 		if (!IS_POSIXACL(dir->d_inode))
20956c0d46c4SAl Viro 			mode &= ~current_umask();
2096fb1cc555SAl Viro 		/*
2097fb1cc555SAl Viro 		 * This write is needed to ensure that a
20986c0d46c4SAl Viro 		 * rw->ro transition does not occur between
2099fb1cc555SAl Viro 		 * the time when the file is created and when
2100fb1cc555SAl Viro 		 * a permanent write count is taken through
2101fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2102fb1cc555SAl Viro 		 */
2103fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2104fb1cc555SAl Viro 		if (error)
2105fb1cc555SAl Viro 			goto exit_mutex_unlock;
2106ca344a89SAl Viro 		want_write = 1;
21079b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2108ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
21096c0d46c4SAl Viro 		will_truncate = 0;
2110bcda7652SAl Viro 		acc_mode = MAY_OPEN;
21116c0d46c4SAl Viro 		error = security_path_mknod(&nd->path, dentry, mode, 0);
21126c0d46c4SAl Viro 		if (error)
21136c0d46c4SAl Viro 			goto exit_mutex_unlock;
21146c0d46c4SAl Viro 		error = vfs_create(dir->d_inode, dentry, mode, nd);
21156c0d46c4SAl Viro 		if (error)
21166c0d46c4SAl Viro 			goto exit_mutex_unlock;
21176c0d46c4SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
21186c0d46c4SAl Viro 		dput(nd->path.dentry);
21196c0d46c4SAl Viro 		nd->path.dentry = dentry;
2120ca344a89SAl Viro 		goto common;
2121fb1cc555SAl Viro 	}
2122fb1cc555SAl Viro 
2123fb1cc555SAl Viro 	/*
2124fb1cc555SAl Viro 	 * It already exists.
2125fb1cc555SAl Viro 	 */
2126fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2127fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2128fb1cc555SAl Viro 
2129fb1cc555SAl Viro 	error = -EEXIST;
2130ca344a89SAl Viro 	if (open_flag & O_EXCL)
2131fb1cc555SAl Viro 		goto exit_dput;
2132fb1cc555SAl Viro 
21339875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
21349875cf80SDavid Howells 	if (error < 0)
2135fb1cc555SAl Viro 		goto exit_dput;
2136fb1cc555SAl Viro 
2137fb1cc555SAl Viro 	error = -ENOENT;
2138fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2139fb1cc555SAl Viro 		goto exit_dput;
21409e67f361SAl Viro 
21419e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2142fb1cc555SAl Viro 		return NULL;
2143fb1cc555SAl Viro 
2144fb1cc555SAl Viro 	path_to_nameidata(path, nd);
214531e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2146fb1cc555SAl Viro 	error = -EISDIR;
214731e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2148fb1cc555SAl Viro 		goto exit;
214967ee3ad2SAl Viro ok:
21506c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
21516c0d46c4SAl Viro 		will_truncate = 0;
21526c0d46c4SAl Viro 
21530f9d1a10SAl Viro 	if (will_truncate) {
21540f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
21550f9d1a10SAl Viro 		if (error)
21560f9d1a10SAl Viro 			goto exit;
2157ca344a89SAl Viro 		want_write = 1;
21580f9d1a10SAl Viro 	}
2159ca344a89SAl Viro common:
2160bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2161ca344a89SAl Viro 	if (error)
21620f9d1a10SAl Viro 		goto exit;
21630f9d1a10SAl Viro 	filp = nameidata_to_filp(nd);
21640f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
21650f9d1a10SAl Viro 		error = ima_file_check(filp, op->acc_mode);
21660f9d1a10SAl Viro 		if (error) {
21670f9d1a10SAl Viro 			fput(filp);
21680f9d1a10SAl Viro 			filp = ERR_PTR(error);
21690f9d1a10SAl Viro 		}
21700f9d1a10SAl Viro 	}
21710f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
21720f9d1a10SAl Viro 		if (will_truncate) {
21730f9d1a10SAl Viro 			error = handle_truncate(filp);
21740f9d1a10SAl Viro 			if (error) {
21750f9d1a10SAl Viro 				fput(filp);
21760f9d1a10SAl Viro 				filp = ERR_PTR(error);
21770f9d1a10SAl Viro 			}
21780f9d1a10SAl Viro 		}
21790f9d1a10SAl Viro 	}
2180ca344a89SAl Viro out:
2181ca344a89SAl Viro 	if (want_write)
21820f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
21830f9d1a10SAl Viro 	path_put(&nd->path);
2184fb1cc555SAl Viro 	return filp;
2185fb1cc555SAl Viro 
2186fb1cc555SAl Viro exit_mutex_unlock:
2187fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2188fb1cc555SAl Viro exit_dput:
2189fb1cc555SAl Viro 	path_put_conditional(path, nd);
2190fb1cc555SAl Viro exit:
2191ca344a89SAl Viro 	filp = ERR_PTR(error);
2192ca344a89SAl Viro 	goto out;
2193fb1cc555SAl Viro }
2194fb1cc555SAl Viro 
219513aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
219673d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
21971da177e4SLinus Torvalds {
2198fe2d35ffSAl Viro 	struct file *base = NULL;
21994a3fd211SDave Hansen 	struct file *filp;
22009850c056SAl Viro 	struct path path;
220113aab428SAl Viro 	int error;
220231e6b01fSNick Piggin 
220331e6b01fSNick Piggin 	filp = get_empty_filp();
220431e6b01fSNick Piggin 	if (!filp)
220531e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
220631e6b01fSNick Piggin 
220747c805dcSAl Viro 	filp->f_flags = op->open_flag;
220873d049a4SAl Viro 	nd->intent.open.file = filp;
220973d049a4SAl Viro 	nd->intent.open.flags = open_to_namei_flags(op->open_flag);
221073d049a4SAl Viro 	nd->intent.open.create_mode = op->mode;
221131e6b01fSNick Piggin 
221273d049a4SAl Viro 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
221331e6b01fSNick Piggin 	if (unlikely(error))
221413aab428SAl Viro 		goto out_filp;
221531e6b01fSNick Piggin 
2216fe2d35ffSAl Viro 	current->total_link_count = 0;
221773d049a4SAl Viro 	error = link_path_walk(pathname, nd);
221831e6b01fSNick Piggin 	if (unlikely(error))
221931e6b01fSNick Piggin 		goto out_filp;
22201da177e4SLinus Torvalds 
222173d049a4SAl Viro 	filp = do_last(nd, &path, op, pathname);
2222806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
22237b9337aaSNick Piggin 		struct path link = path;
2224def4af30SAl Viro 		void *cookie;
2225574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
222673d049a4SAl Viro 			path_put_conditional(&path, nd);
222773d049a4SAl Viro 			path_put(&nd->path);
222840b39136SAl Viro 			filp = ERR_PTR(-ELOOP);
222940b39136SAl Viro 			break;
223040b39136SAl Viro 		}
223173d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
223273d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2233574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
2234c3e380b0SAl Viro 		if (unlikely(error))
2235f1afe9efSAl Viro 			filp = ERR_PTR(error);
2236c3e380b0SAl Viro 		else
223773d049a4SAl Viro 			filp = do_last(nd, &path, op, pathname);
2238574197e0SAl Viro 		put_link(nd, &link, cookie);
2239806b681cSAl Viro 	}
224010fa8e62SAl Viro out:
224173d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
224273d049a4SAl Viro 		path_put(&nd->root);
2243fe2d35ffSAl Viro 	if (base)
2244fe2d35ffSAl Viro 		fput(base);
224573d049a4SAl Viro 	release_open_intent(nd);
224610fa8e62SAl Viro 	return filp;
22471da177e4SLinus Torvalds 
224831e6b01fSNick Piggin out_filp:
224910fa8e62SAl Viro 	filp = ERR_PTR(error);
225010fa8e62SAl Viro 	goto out;
2251de459215SKirill Korotaev }
22521da177e4SLinus Torvalds 
225313aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
225413aab428SAl Viro 		const struct open_flags *op, int flags)
225513aab428SAl Viro {
225673d049a4SAl Viro 	struct nameidata nd;
225713aab428SAl Viro 	struct file *filp;
225813aab428SAl Viro 
225973d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
226013aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
226173d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
226213aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
226373d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
226413aab428SAl Viro 	return filp;
226513aab428SAl Viro }
226613aab428SAl Viro 
226773d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
226873d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
226973d049a4SAl Viro {
227073d049a4SAl Viro 	struct nameidata nd;
227173d049a4SAl Viro 	struct file *file;
227273d049a4SAl Viro 
227373d049a4SAl Viro 	nd.root.mnt = mnt;
227473d049a4SAl Viro 	nd.root.dentry = dentry;
227573d049a4SAl Viro 
227673d049a4SAl Viro 	flags |= LOOKUP_ROOT;
227773d049a4SAl Viro 
2278bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
227973d049a4SAl Viro 		return ERR_PTR(-ELOOP);
228073d049a4SAl Viro 
228173d049a4SAl Viro 	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
228273d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
228373d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags);
228473d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
228573d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
228673d049a4SAl Viro 	return file;
228773d049a4SAl Viro }
228873d049a4SAl Viro 
22891da177e4SLinus Torvalds /**
22901da177e4SLinus Torvalds  * lookup_create - lookup a dentry, creating it if it doesn't exist
22911da177e4SLinus Torvalds  * @nd: nameidata info
22921da177e4SLinus Torvalds  * @is_dir: directory flag
22931da177e4SLinus Torvalds  *
22941da177e4SLinus Torvalds  * Simple function to lookup and return a dentry and create it
22951da177e4SLinus Torvalds  * if it doesn't exist.  Is SMP-safe.
2296c663e5d8SChristoph Hellwig  *
22974ac91378SJan Blunck  * Returns with nd->path.dentry->d_inode->i_mutex locked.
22981da177e4SLinus Torvalds  */
22991da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir)
23001da177e4SLinus Torvalds {
2301c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
23021da177e4SLinus Torvalds 
23034ac91378SJan Blunck 	mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2304c663e5d8SChristoph Hellwig 	/*
2305c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2306c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2307c663e5d8SChristoph Hellwig 	 */
23081da177e4SLinus Torvalds 	if (nd->last_type != LAST_NORM)
23091da177e4SLinus Torvalds 		goto fail;
23101da177e4SLinus Torvalds 	nd->flags &= ~LOOKUP_PARENT;
23113516586aSAl Viro 	nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2312a634904aSASANO Masahiro 	nd->intent.open.flags = O_EXCL;
2313c663e5d8SChristoph Hellwig 
2314c663e5d8SChristoph Hellwig 	/*
2315c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2316c663e5d8SChristoph Hellwig 	 */
231749705b77SChristoph Hellwig 	dentry = lookup_hash(nd);
23181da177e4SLinus Torvalds 	if (IS_ERR(dentry))
23191da177e4SLinus Torvalds 		goto fail;
2320c663e5d8SChristoph Hellwig 
2321e9baf6e5SAl Viro 	if (dentry->d_inode)
2322e9baf6e5SAl Viro 		goto eexist;
2323c663e5d8SChristoph Hellwig 	/*
2324c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2325c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2326c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2327c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2328c663e5d8SChristoph Hellwig 	 */
2329e9baf6e5SAl Viro 	if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
23301da177e4SLinus Torvalds 		dput(dentry);
23311da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2332e9baf6e5SAl Viro 	}
2333e9baf6e5SAl Viro 	return dentry;
2334e9baf6e5SAl Viro eexist:
2335e9baf6e5SAl Viro 	dput(dentry);
2336e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
23371da177e4SLinus Torvalds fail:
23381da177e4SLinus Torvalds 	return dentry;
23391da177e4SLinus Torvalds }
2340f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create);
23411da177e4SLinus Torvalds 
23421da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
23431da177e4SLinus Torvalds {
2344a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds 	if (error)
23471da177e4SLinus Torvalds 		return error;
23481da177e4SLinus Torvalds 
2349e795b717SSerge E. Hallyn 	if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2350e795b717SSerge E. Hallyn 	    !ns_capable(inode_userns(dir), CAP_MKNOD))
23511da177e4SLinus Torvalds 		return -EPERM;
23521da177e4SLinus Torvalds 
2353acfa4380SAl Viro 	if (!dir->i_op->mknod)
23541da177e4SLinus Torvalds 		return -EPERM;
23551da177e4SLinus Torvalds 
235608ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
235708ce5f16SSerge E. Hallyn 	if (error)
235808ce5f16SSerge E. Hallyn 		return error;
235908ce5f16SSerge E. Hallyn 
23601da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
23611da177e4SLinus Torvalds 	if (error)
23621da177e4SLinus Torvalds 		return error;
23631da177e4SLinus Torvalds 
23641da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2365a74574aaSStephen Smalley 	if (!error)
2366f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
23671da177e4SLinus Torvalds 	return error;
23681da177e4SLinus Torvalds }
23691da177e4SLinus Torvalds 
2370463c3197SDave Hansen static int may_mknod(mode_t mode)
2371463c3197SDave Hansen {
2372463c3197SDave Hansen 	switch (mode & S_IFMT) {
2373463c3197SDave Hansen 	case S_IFREG:
2374463c3197SDave Hansen 	case S_IFCHR:
2375463c3197SDave Hansen 	case S_IFBLK:
2376463c3197SDave Hansen 	case S_IFIFO:
2377463c3197SDave Hansen 	case S_IFSOCK:
2378463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2379463c3197SDave Hansen 		return 0;
2380463c3197SDave Hansen 	case S_IFDIR:
2381463c3197SDave Hansen 		return -EPERM;
2382463c3197SDave Hansen 	default:
2383463c3197SDave Hansen 		return -EINVAL;
2384463c3197SDave Hansen 	}
2385463c3197SDave Hansen }
2386463c3197SDave Hansen 
23872e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
23882e4d0924SHeiko Carstens 		unsigned, dev)
23891da177e4SLinus Torvalds {
23902ad94ae6SAl Viro 	int error;
23911da177e4SLinus Torvalds 	char *tmp;
23921da177e4SLinus Torvalds 	struct dentry *dentry;
23931da177e4SLinus Torvalds 	struct nameidata nd;
23941da177e4SLinus Torvalds 
23951da177e4SLinus Torvalds 	if (S_ISDIR(mode))
23961da177e4SLinus Torvalds 		return -EPERM;
23971da177e4SLinus Torvalds 
23982ad94ae6SAl Viro 	error = user_path_parent(dfd, filename, &nd, &tmp);
23991da177e4SLinus Torvalds 	if (error)
24002ad94ae6SAl Viro 		return error;
24012ad94ae6SAl Viro 
24021da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
2403463c3197SDave Hansen 	if (IS_ERR(dentry)) {
24041da177e4SLinus Torvalds 		error = PTR_ERR(dentry);
2405463c3197SDave Hansen 		goto out_unlock;
2406463c3197SDave Hansen 	}
24074ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2408ce3b0f8dSAl Viro 		mode &= ~current_umask();
2409463c3197SDave Hansen 	error = may_mknod(mode);
2410463c3197SDave Hansen 	if (error)
2411463c3197SDave Hansen 		goto out_dput;
2412463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2413463c3197SDave Hansen 	if (error)
2414463c3197SDave Hansen 		goto out_dput;
2415be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd.path, dentry, mode, dev);
2416be6d3e56SKentaro Takeda 	if (error)
2417be6d3e56SKentaro Takeda 		goto out_drop_write;
24181da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
24191da177e4SLinus Torvalds 		case 0: case S_IFREG:
24204ac91378SJan Blunck 			error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
24211da177e4SLinus Torvalds 			break;
24221da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
24234ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
24241da177e4SLinus Torvalds 					new_decode_dev(dev));
24251da177e4SLinus Torvalds 			break;
24261da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
24274ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
24281da177e4SLinus Torvalds 			break;
24291da177e4SLinus Torvalds 	}
2430be6d3e56SKentaro Takeda out_drop_write:
2431463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2432463c3197SDave Hansen out_dput:
24331da177e4SLinus Torvalds 	dput(dentry);
2434463c3197SDave Hansen out_unlock:
24354ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
24361d957f9bSJan Blunck 	path_put(&nd.path);
24371da177e4SLinus Torvalds 	putname(tmp);
24381da177e4SLinus Torvalds 
24391da177e4SLinus Torvalds 	return error;
24401da177e4SLinus Torvalds }
24411da177e4SLinus Torvalds 
24423480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
24435590ff0dSUlrich Drepper {
24445590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
24455590ff0dSUlrich Drepper }
24465590ff0dSUlrich Drepper 
24471da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
24481da177e4SLinus Torvalds {
2449a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
24501da177e4SLinus Torvalds 
24511da177e4SLinus Torvalds 	if (error)
24521da177e4SLinus Torvalds 		return error;
24531da177e4SLinus Torvalds 
2454acfa4380SAl Viro 	if (!dir->i_op->mkdir)
24551da177e4SLinus Torvalds 		return -EPERM;
24561da177e4SLinus Torvalds 
24571da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
24581da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
24591da177e4SLinus Torvalds 	if (error)
24601da177e4SLinus Torvalds 		return error;
24611da177e4SLinus Torvalds 
24621da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2463a74574aaSStephen Smalley 	if (!error)
2464f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
24651da177e4SLinus Torvalds 	return error;
24661da177e4SLinus Torvalds }
24671da177e4SLinus Torvalds 
24682e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
24691da177e4SLinus Torvalds {
24701da177e4SLinus Torvalds 	int error = 0;
24711da177e4SLinus Torvalds 	char * tmp;
24726902d925SDave Hansen 	struct dentry *dentry;
24736902d925SDave Hansen 	struct nameidata nd;
24741da177e4SLinus Torvalds 
24752ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &tmp);
24762ad94ae6SAl Viro 	if (error)
24776902d925SDave Hansen 		goto out_err;
24781da177e4SLinus Torvalds 
24791da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 1);
24801da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
24816902d925SDave Hansen 	if (IS_ERR(dentry))
24826902d925SDave Hansen 		goto out_unlock;
24836902d925SDave Hansen 
24844ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2485ce3b0f8dSAl Viro 		mode &= ~current_umask();
2486463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2487463c3197SDave Hansen 	if (error)
2488463c3197SDave Hansen 		goto out_dput;
2489be6d3e56SKentaro Takeda 	error = security_path_mkdir(&nd.path, dentry, mode);
2490be6d3e56SKentaro Takeda 	if (error)
2491be6d3e56SKentaro Takeda 		goto out_drop_write;
24924ac91378SJan Blunck 	error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2493be6d3e56SKentaro Takeda out_drop_write:
2494463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2495463c3197SDave Hansen out_dput:
24961da177e4SLinus Torvalds 	dput(dentry);
24976902d925SDave Hansen out_unlock:
24984ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
24991d957f9bSJan Blunck 	path_put(&nd.path);
25001da177e4SLinus Torvalds 	putname(tmp);
25016902d925SDave Hansen out_err:
25021da177e4SLinus Torvalds 	return error;
25031da177e4SLinus Torvalds }
25041da177e4SLinus Torvalds 
25053cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
25065590ff0dSUlrich Drepper {
25075590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
25085590ff0dSUlrich Drepper }
25095590ff0dSUlrich Drepper 
25101da177e4SLinus Torvalds /*
2511a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
2512a71905f0SSage Weil  * should have a usage count of 2 if we're the only user of this
2513a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
2514a71905f0SSage Weil  * then we drop the dentry now.
25151da177e4SLinus Torvalds  *
25161da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
25171da177e4SLinus Torvalds  * do a
25181da177e4SLinus Torvalds  *
25191da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
25201da177e4SLinus Torvalds  *		return -EBUSY;
25211da177e4SLinus Torvalds  *
25221da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
25231da177e4SLinus Torvalds  * that is still in use by something else..
25241da177e4SLinus Torvalds  */
25251da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
25261da177e4SLinus Torvalds {
25271da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
25281da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
252964252c75SSage Weil 	if (dentry->d_count == 1)
25301da177e4SLinus Torvalds 		__d_drop(dentry);
25311da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
25321da177e4SLinus Torvalds }
25331da177e4SLinus Torvalds 
25341da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
25351da177e4SLinus Torvalds {
25361da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
25371da177e4SLinus Torvalds 
25381da177e4SLinus Torvalds 	if (error)
25391da177e4SLinus Torvalds 		return error;
25401da177e4SLinus Torvalds 
2541acfa4380SAl Viro 	if (!dir->i_op->rmdir)
25421da177e4SLinus Torvalds 		return -EPERM;
25431da177e4SLinus Torvalds 
25441b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
2545912dbc15SSage Weil 
25461da177e4SLinus Torvalds 	error = -EBUSY;
2547912dbc15SSage Weil 	if (d_mountpoint(dentry))
2548912dbc15SSage Weil 		goto out;
2549912dbc15SSage Weil 
25501da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
2551912dbc15SSage Weil 	if (error)
2552912dbc15SSage Weil 		goto out;
2553912dbc15SSage Weil 
25543cebde24SSage Weil 	shrink_dcache_parent(dentry);
25551da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
2556912dbc15SSage Weil 	if (error)
2557912dbc15SSage Weil 		goto out;
2558912dbc15SSage Weil 
25591da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
2560d83c49f3SAl Viro 	dont_mount(dentry);
25611da177e4SLinus Torvalds 
2562912dbc15SSage Weil out:
2563912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
2564912dbc15SSage Weil 	if (!error)
2565912dbc15SSage Weil 		d_delete(dentry);
25661da177e4SLinus Torvalds 	return error;
25671da177e4SLinus Torvalds }
25681da177e4SLinus Torvalds 
25695590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
25701da177e4SLinus Torvalds {
25711da177e4SLinus Torvalds 	int error = 0;
25721da177e4SLinus Torvalds 	char * name;
25731da177e4SLinus Torvalds 	struct dentry *dentry;
25741da177e4SLinus Torvalds 	struct nameidata nd;
25751da177e4SLinus Torvalds 
25762ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
25771da177e4SLinus Torvalds 	if (error)
25782ad94ae6SAl Viro 		return error;
25791da177e4SLinus Torvalds 
25801da177e4SLinus Torvalds 	switch(nd.last_type) {
25811da177e4SLinus Torvalds 	case LAST_DOTDOT:
25821da177e4SLinus Torvalds 		error = -ENOTEMPTY;
25831da177e4SLinus Torvalds 		goto exit1;
25841da177e4SLinus Torvalds 	case LAST_DOT:
25851da177e4SLinus Torvalds 		error = -EINVAL;
25861da177e4SLinus Torvalds 		goto exit1;
25871da177e4SLinus Torvalds 	case LAST_ROOT:
25881da177e4SLinus Torvalds 		error = -EBUSY;
25891da177e4SLinus Torvalds 		goto exit1;
25901da177e4SLinus Torvalds 	}
25910612d9fbSOGAWA Hirofumi 
25920612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
25930612d9fbSOGAWA Hirofumi 
25944ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
259549705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
25961da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
25976902d925SDave Hansen 	if (IS_ERR(dentry))
25986902d925SDave Hansen 		goto exit2;
2599e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
2600e6bc45d6STheodore Ts'o 		error = -ENOENT;
2601e6bc45d6STheodore Ts'o 		goto exit3;
2602e6bc45d6STheodore Ts'o 	}
26030622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
26040622753bSDave Hansen 	if (error)
26050622753bSDave Hansen 		goto exit3;
2606be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2607be6d3e56SKentaro Takeda 	if (error)
2608be6d3e56SKentaro Takeda 		goto exit4;
26094ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2610be6d3e56SKentaro Takeda exit4:
26110622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
26120622753bSDave Hansen exit3:
26131da177e4SLinus Torvalds 	dput(dentry);
26146902d925SDave Hansen exit2:
26154ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
26161da177e4SLinus Torvalds exit1:
26171d957f9bSJan Blunck 	path_put(&nd.path);
26181da177e4SLinus Torvalds 	putname(name);
26191da177e4SLinus Torvalds 	return error;
26201da177e4SLinus Torvalds }
26211da177e4SLinus Torvalds 
26223cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
26235590ff0dSUlrich Drepper {
26245590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
26255590ff0dSUlrich Drepper }
26265590ff0dSUlrich Drepper 
26271da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
26281da177e4SLinus Torvalds {
26291da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
26301da177e4SLinus Torvalds 
26311da177e4SLinus Torvalds 	if (error)
26321da177e4SLinus Torvalds 		return error;
26331da177e4SLinus Torvalds 
2634acfa4380SAl Viro 	if (!dir->i_op->unlink)
26351da177e4SLinus Torvalds 		return -EPERM;
26361da177e4SLinus Torvalds 
26371b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
26381da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
26391da177e4SLinus Torvalds 		error = -EBUSY;
26401da177e4SLinus Torvalds 	else {
26411da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2642bec1052eSAl Viro 		if (!error) {
26431da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2644bec1052eSAl Viro 			if (!error)
2645d83c49f3SAl Viro 				dont_mount(dentry);
2646bec1052eSAl Viro 		}
26471da177e4SLinus Torvalds 	}
26481b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
26491da177e4SLinus Torvalds 
26501da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
26511da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2652ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
26531da177e4SLinus Torvalds 		d_delete(dentry);
26541da177e4SLinus Torvalds 	}
26550eeca283SRobert Love 
26561da177e4SLinus Torvalds 	return error;
26571da177e4SLinus Torvalds }
26581da177e4SLinus Torvalds 
26591da177e4SLinus Torvalds /*
26601da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
26611b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
26621da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
26631da177e4SLinus Torvalds  * while waiting on the I/O.
26641da177e4SLinus Torvalds  */
26655590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
26661da177e4SLinus Torvalds {
26672ad94ae6SAl Viro 	int error;
26681da177e4SLinus Torvalds 	char *name;
26691da177e4SLinus Torvalds 	struct dentry *dentry;
26701da177e4SLinus Torvalds 	struct nameidata nd;
26711da177e4SLinus Torvalds 	struct inode *inode = NULL;
26721da177e4SLinus Torvalds 
26732ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
26741da177e4SLinus Torvalds 	if (error)
26752ad94ae6SAl Viro 		return error;
26762ad94ae6SAl Viro 
26771da177e4SLinus Torvalds 	error = -EISDIR;
26781da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
26791da177e4SLinus Torvalds 		goto exit1;
26800612d9fbSOGAWA Hirofumi 
26810612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
26820612d9fbSOGAWA Hirofumi 
26834ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
268449705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
26851da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
26861da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
26871da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
268850338b88STörök Edwin 		if (nd.last.name[nd.last.len])
268950338b88STörök Edwin 			goto slashes;
26901da177e4SLinus Torvalds 		inode = dentry->d_inode;
269150338b88STörök Edwin 		if (!inode)
2692e6bc45d6STheodore Ts'o 			goto slashes;
26937de9c6eeSAl Viro 		ihold(inode);
26940622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
26950622753bSDave Hansen 		if (error)
26960622753bSDave Hansen 			goto exit2;
2697be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2698be6d3e56SKentaro Takeda 		if (error)
2699be6d3e56SKentaro Takeda 			goto exit3;
27004ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2701be6d3e56SKentaro Takeda exit3:
27020622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
27031da177e4SLinus Torvalds 	exit2:
27041da177e4SLinus Torvalds 		dput(dentry);
27051da177e4SLinus Torvalds 	}
27064ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27071da177e4SLinus Torvalds 	if (inode)
27081da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
27091da177e4SLinus Torvalds exit1:
27101d957f9bSJan Blunck 	path_put(&nd.path);
27111da177e4SLinus Torvalds 	putname(name);
27121da177e4SLinus Torvalds 	return error;
27131da177e4SLinus Torvalds 
27141da177e4SLinus Torvalds slashes:
27151da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
27161da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
27171da177e4SLinus Torvalds 	goto exit2;
27181da177e4SLinus Torvalds }
27191da177e4SLinus Torvalds 
27202e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
27215590ff0dSUlrich Drepper {
27225590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
27235590ff0dSUlrich Drepper 		return -EINVAL;
27245590ff0dSUlrich Drepper 
27255590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
27265590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
27275590ff0dSUlrich Drepper 
27285590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
27295590ff0dSUlrich Drepper }
27305590ff0dSUlrich Drepper 
27313480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
27325590ff0dSUlrich Drepper {
27335590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
27345590ff0dSUlrich Drepper }
27355590ff0dSUlrich Drepper 
2736db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
27371da177e4SLinus Torvalds {
2738a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
27391da177e4SLinus Torvalds 
27401da177e4SLinus Torvalds 	if (error)
27411da177e4SLinus Torvalds 		return error;
27421da177e4SLinus Torvalds 
2743acfa4380SAl Viro 	if (!dir->i_op->symlink)
27441da177e4SLinus Torvalds 		return -EPERM;
27451da177e4SLinus Torvalds 
27461da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
27471da177e4SLinus Torvalds 	if (error)
27481da177e4SLinus Torvalds 		return error;
27491da177e4SLinus Torvalds 
27501da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2751a74574aaSStephen Smalley 	if (!error)
2752f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
27531da177e4SLinus Torvalds 	return error;
27541da177e4SLinus Torvalds }
27551da177e4SLinus Torvalds 
27562e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
27572e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
27581da177e4SLinus Torvalds {
27592ad94ae6SAl Viro 	int error;
27601da177e4SLinus Torvalds 	char *from;
27611da177e4SLinus Torvalds 	char *to;
27626902d925SDave Hansen 	struct dentry *dentry;
27636902d925SDave Hansen 	struct nameidata nd;
27641da177e4SLinus Torvalds 
27651da177e4SLinus Torvalds 	from = getname(oldname);
27661da177e4SLinus Torvalds 	if (IS_ERR(from))
27671da177e4SLinus Torvalds 		return PTR_ERR(from);
27682ad94ae6SAl Viro 
27692ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
27702ad94ae6SAl Viro 	if (error)
27716902d925SDave Hansen 		goto out_putname;
27721da177e4SLinus Torvalds 
27731da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
27741da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27756902d925SDave Hansen 	if (IS_ERR(dentry))
27766902d925SDave Hansen 		goto out_unlock;
27776902d925SDave Hansen 
277875c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
277975c3f29dSDave Hansen 	if (error)
278075c3f29dSDave Hansen 		goto out_dput;
2781be6d3e56SKentaro Takeda 	error = security_path_symlink(&nd.path, dentry, from);
2782be6d3e56SKentaro Takeda 	if (error)
2783be6d3e56SKentaro Takeda 		goto out_drop_write;
2784db2e747bSMiklos Szeredi 	error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2785be6d3e56SKentaro Takeda out_drop_write:
278675c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
278775c3f29dSDave Hansen out_dput:
27881da177e4SLinus Torvalds 	dput(dentry);
27896902d925SDave Hansen out_unlock:
27904ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27911d957f9bSJan Blunck 	path_put(&nd.path);
27921da177e4SLinus Torvalds 	putname(to);
27936902d925SDave Hansen out_putname:
27941da177e4SLinus Torvalds 	putname(from);
27951da177e4SLinus Torvalds 	return error;
27961da177e4SLinus Torvalds }
27971da177e4SLinus Torvalds 
27983480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
27995590ff0dSUlrich Drepper {
28005590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
28015590ff0dSUlrich Drepper }
28025590ff0dSUlrich Drepper 
28031da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
28041da177e4SLinus Torvalds {
28051da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
28061da177e4SLinus Torvalds 	int error;
28071da177e4SLinus Torvalds 
28081da177e4SLinus Torvalds 	if (!inode)
28091da177e4SLinus Torvalds 		return -ENOENT;
28101da177e4SLinus Torvalds 
2811a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
28121da177e4SLinus Torvalds 	if (error)
28131da177e4SLinus Torvalds 		return error;
28141da177e4SLinus Torvalds 
28151da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
28161da177e4SLinus Torvalds 		return -EXDEV;
28171da177e4SLinus Torvalds 
28181da177e4SLinus Torvalds 	/*
28191da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
28201da177e4SLinus Torvalds 	 */
28211da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
28221da177e4SLinus Torvalds 		return -EPERM;
2823acfa4380SAl Viro 	if (!dir->i_op->link)
28241da177e4SLinus Torvalds 		return -EPERM;
28257e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
28261da177e4SLinus Torvalds 		return -EPERM;
28271da177e4SLinus Torvalds 
28281da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
28291da177e4SLinus Torvalds 	if (error)
28301da177e4SLinus Torvalds 		return error;
28311da177e4SLinus Torvalds 
28327e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
2833aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
2834aae8a97dSAneesh Kumar K.V 	if (inode->i_nlink == 0)
2835aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
2836aae8a97dSAneesh Kumar K.V 	else
28371da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
28387e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2839e31e14ecSStephen Smalley 	if (!error)
28407e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
28411da177e4SLinus Torvalds 	return error;
28421da177e4SLinus Torvalds }
28431da177e4SLinus Torvalds 
28441da177e4SLinus Torvalds /*
28451da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
28461da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
28471da177e4SLinus Torvalds  * newname.  --KAB
28481da177e4SLinus Torvalds  *
28491da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
28501da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
28511da177e4SLinus Torvalds  * and other special files.  --ADM
28521da177e4SLinus Torvalds  */
28532e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
28542e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
28551da177e4SLinus Torvalds {
28561da177e4SLinus Torvalds 	struct dentry *new_dentry;
28572d8f3038SAl Viro 	struct nameidata nd;
28582d8f3038SAl Viro 	struct path old_path;
285911a7b371SAneesh Kumar K.V 	int how = 0;
28601da177e4SLinus Torvalds 	int error;
28611da177e4SLinus Torvalds 	char *to;
28621da177e4SLinus Torvalds 
286311a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
2864c04030e1SUlrich Drepper 		return -EINVAL;
286511a7b371SAneesh Kumar K.V 	/*
286611a7b371SAneesh Kumar K.V 	 * To use null names we require CAP_DAC_READ_SEARCH
286711a7b371SAneesh Kumar K.V 	 * This ensures that not everyone will be able to create
286811a7b371SAneesh Kumar K.V 	 * handlink using the passed filedescriptor.
286911a7b371SAneesh Kumar K.V 	 */
287011a7b371SAneesh Kumar K.V 	if (flags & AT_EMPTY_PATH) {
287111a7b371SAneesh Kumar K.V 		if (!capable(CAP_DAC_READ_SEARCH))
287211a7b371SAneesh Kumar K.V 			return -ENOENT;
287311a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
287411a7b371SAneesh Kumar K.V 	}
2875c04030e1SUlrich Drepper 
287611a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
287711a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
287811a7b371SAneesh Kumar K.V 
287911a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
28801da177e4SLinus Torvalds 	if (error)
28812ad94ae6SAl Viro 		return error;
28822ad94ae6SAl Viro 
28832ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
28841da177e4SLinus Torvalds 	if (error)
28851da177e4SLinus Torvalds 		goto out;
28861da177e4SLinus Torvalds 	error = -EXDEV;
28872d8f3038SAl Viro 	if (old_path.mnt != nd.path.mnt)
28881da177e4SLinus Torvalds 		goto out_release;
28891da177e4SLinus Torvalds 	new_dentry = lookup_create(&nd, 0);
28901da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
28916902d925SDave Hansen 	if (IS_ERR(new_dentry))
28926902d925SDave Hansen 		goto out_unlock;
289375c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
289475c3f29dSDave Hansen 	if (error)
289575c3f29dSDave Hansen 		goto out_dput;
2896be6d3e56SKentaro Takeda 	error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2897be6d3e56SKentaro Takeda 	if (error)
2898be6d3e56SKentaro Takeda 		goto out_drop_write;
28992d8f3038SAl Viro 	error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
2900be6d3e56SKentaro Takeda out_drop_write:
290175c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
290275c3f29dSDave Hansen out_dput:
29031da177e4SLinus Torvalds 	dput(new_dentry);
29046902d925SDave Hansen out_unlock:
29054ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
29061da177e4SLinus Torvalds out_release:
29071d957f9bSJan Blunck 	path_put(&nd.path);
29082ad94ae6SAl Viro 	putname(to);
29091da177e4SLinus Torvalds out:
29102d8f3038SAl Viro 	path_put(&old_path);
29111da177e4SLinus Torvalds 
29121da177e4SLinus Torvalds 	return error;
29131da177e4SLinus Torvalds }
29141da177e4SLinus Torvalds 
29153480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
29165590ff0dSUlrich Drepper {
2917c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
29185590ff0dSUlrich Drepper }
29195590ff0dSUlrich Drepper 
29201da177e4SLinus Torvalds /*
29211da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
29221da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
29231da177e4SLinus Torvalds  * Problems:
29241da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
29251da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
29261da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
2927a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
29281da177e4SLinus Torvalds  *	   story.
29291da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
29301b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
29311da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
29321da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
2933a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
29341da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
29351da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
29361da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
2937a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
29381da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
29391da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
29401da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
2941e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
29421b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
29431da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2944c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
29451da177e4SLinus Torvalds  *	   locking].
29461da177e4SLinus Torvalds  */
294775c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
29481da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
29491da177e4SLinus Torvalds {
29501da177e4SLinus Torvalds 	int error = 0;
29519055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
29521da177e4SLinus Torvalds 
29531da177e4SLinus Torvalds 	/*
29541da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
29551da177e4SLinus Torvalds 	 * we'll need to flip '..'.
29561da177e4SLinus Torvalds 	 */
29571da177e4SLinus Torvalds 	if (new_dir != old_dir) {
2958f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
29591da177e4SLinus Torvalds 		if (error)
29601da177e4SLinus Torvalds 			return error;
29611da177e4SLinus Torvalds 	}
29621da177e4SLinus Torvalds 
29631da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
29641da177e4SLinus Torvalds 	if (error)
29651da177e4SLinus Torvalds 		return error;
29661da177e4SLinus Torvalds 
2967d83c49f3SAl Viro 	if (target)
29681b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
29699055cba7SSage Weil 
29701da177e4SLinus Torvalds 	error = -EBUSY;
29719055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
29729055cba7SSage Weil 		goto out;
29739055cba7SSage Weil 
29743cebde24SSage Weil 	if (target)
29753cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
29761da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
29779055cba7SSage Weil 	if (error)
29789055cba7SSage Weil 		goto out;
29799055cba7SSage Weil 
29801da177e4SLinus Torvalds 	if (target) {
29811da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
2982d83c49f3SAl Viro 		dont_mount(new_dentry);
2983d83c49f3SAl Viro 	}
29849055cba7SSage Weil out:
29859055cba7SSage Weil 	if (target)
29861b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
2987e31e14ecSStephen Smalley 	if (!error)
2988349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
29891da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
29901da177e4SLinus Torvalds 	return error;
29911da177e4SLinus Torvalds }
29921da177e4SLinus Torvalds 
299375c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
29941da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
29951da177e4SLinus Torvalds {
299651892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
29971da177e4SLinus Torvalds 	int error;
29981da177e4SLinus Torvalds 
29991da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30001da177e4SLinus Torvalds 	if (error)
30011da177e4SLinus Torvalds 		return error;
30021da177e4SLinus Torvalds 
30031da177e4SLinus Torvalds 	dget(new_dentry);
30041da177e4SLinus Torvalds 	if (target)
30051b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
300651892bbbSSage Weil 
30071da177e4SLinus Torvalds 	error = -EBUSY;
300851892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
300951892bbbSSage Weil 		goto out;
301051892bbbSSage Weil 
30111da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
301251892bbbSSage Weil 	if (error)
301351892bbbSSage Weil 		goto out;
301451892bbbSSage Weil 
3015bec1052eSAl Viro 	if (target)
3016d83c49f3SAl Viro 		dont_mount(new_dentry);
3017349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30181da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
301951892bbbSSage Weil out:
30201da177e4SLinus Torvalds 	if (target)
30211b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
30221da177e4SLinus Torvalds 	dput(new_dentry);
30231da177e4SLinus Torvalds 	return error;
30241da177e4SLinus Torvalds }
30251da177e4SLinus Torvalds 
30261da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
30271da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
30281da177e4SLinus Torvalds {
30291da177e4SLinus Torvalds 	int error;
30301da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
303159b0df21SEric Paris 	const unsigned char *old_name;
30321da177e4SLinus Torvalds 
30331da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
30341da177e4SLinus Torvalds  		return 0;
30351da177e4SLinus Torvalds 
30361da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
30371da177e4SLinus Torvalds 	if (error)
30381da177e4SLinus Torvalds 		return error;
30391da177e4SLinus Torvalds 
30401da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3041a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
30421da177e4SLinus Torvalds 	else
30431da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
30441da177e4SLinus Torvalds 	if (error)
30451da177e4SLinus Torvalds 		return error;
30461da177e4SLinus Torvalds 
3047acfa4380SAl Viro 	if (!old_dir->i_op->rename)
30481da177e4SLinus Torvalds 		return -EPERM;
30491da177e4SLinus Torvalds 
30500eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
30510eeca283SRobert Love 
30521da177e4SLinus Torvalds 	if (is_dir)
30531da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
30541da177e4SLinus Torvalds 	else
30551da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3056123df294SAl Viro 	if (!error)
3057123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
30585a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
30590eeca283SRobert Love 	fsnotify_oldname_free(old_name);
30600eeca283SRobert Love 
30611da177e4SLinus Torvalds 	return error;
30621da177e4SLinus Torvalds }
30631da177e4SLinus Torvalds 
30642e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
30652e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
30661da177e4SLinus Torvalds {
30671da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
30681da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
30691da177e4SLinus Torvalds 	struct dentry *trap;
30701da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
30712ad94ae6SAl Viro 	char *from;
30722ad94ae6SAl Viro 	char *to;
30732ad94ae6SAl Viro 	int error;
30741da177e4SLinus Torvalds 
30752ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
30761da177e4SLinus Torvalds 	if (error)
30771da177e4SLinus Torvalds 		goto exit;
30781da177e4SLinus Torvalds 
30792ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
30801da177e4SLinus Torvalds 	if (error)
30811da177e4SLinus Torvalds 		goto exit1;
30821da177e4SLinus Torvalds 
30831da177e4SLinus Torvalds 	error = -EXDEV;
30844ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
30851da177e4SLinus Torvalds 		goto exit2;
30861da177e4SLinus Torvalds 
30874ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
30881da177e4SLinus Torvalds 	error = -EBUSY;
30891da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
30901da177e4SLinus Torvalds 		goto exit2;
30911da177e4SLinus Torvalds 
30924ac91378SJan Blunck 	new_dir = newnd.path.dentry;
30931da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
30941da177e4SLinus Torvalds 		goto exit2;
30951da177e4SLinus Torvalds 
30960612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
30970612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
30984e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
30990612d9fbSOGAWA Hirofumi 
31001da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
31011da177e4SLinus Torvalds 
310249705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
31031da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
31041da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
31051da177e4SLinus Torvalds 		goto exit3;
31061da177e4SLinus Torvalds 	/* source must exist */
31071da177e4SLinus Torvalds 	error = -ENOENT;
31081da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
31091da177e4SLinus Torvalds 		goto exit4;
31101da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
31111da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
31121da177e4SLinus Torvalds 		error = -ENOTDIR;
31131da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
31141da177e4SLinus Torvalds 			goto exit4;
31151da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
31161da177e4SLinus Torvalds 			goto exit4;
31171da177e4SLinus Torvalds 	}
31181da177e4SLinus Torvalds 	/* source should not be ancestor of target */
31191da177e4SLinus Torvalds 	error = -EINVAL;
31201da177e4SLinus Torvalds 	if (old_dentry == trap)
31211da177e4SLinus Torvalds 		goto exit4;
312249705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
31231da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
31241da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
31251da177e4SLinus Torvalds 		goto exit4;
31261da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
31271da177e4SLinus Torvalds 	error = -ENOTEMPTY;
31281da177e4SLinus Torvalds 	if (new_dentry == trap)
31291da177e4SLinus Torvalds 		goto exit5;
31301da177e4SLinus Torvalds 
31319079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
31329079b1ebSDave Hansen 	if (error)
31339079b1ebSDave Hansen 		goto exit5;
3134be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3135be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3136be6d3e56SKentaro Takeda 	if (error)
3137be6d3e56SKentaro Takeda 		goto exit6;
31381da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
31391da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3140be6d3e56SKentaro Takeda exit6:
31419079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
31421da177e4SLinus Torvalds exit5:
31431da177e4SLinus Torvalds 	dput(new_dentry);
31441da177e4SLinus Torvalds exit4:
31451da177e4SLinus Torvalds 	dput(old_dentry);
31461da177e4SLinus Torvalds exit3:
31471da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
31481da177e4SLinus Torvalds exit2:
31491d957f9bSJan Blunck 	path_put(&newnd.path);
31502ad94ae6SAl Viro 	putname(to);
31511da177e4SLinus Torvalds exit1:
31521d957f9bSJan Blunck 	path_put(&oldnd.path);
31531da177e4SLinus Torvalds 	putname(from);
31542ad94ae6SAl Viro exit:
31551da177e4SLinus Torvalds 	return error;
31561da177e4SLinus Torvalds }
31571da177e4SLinus Torvalds 
3158a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
31595590ff0dSUlrich Drepper {
31605590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
31615590ff0dSUlrich Drepper }
31625590ff0dSUlrich Drepper 
31631da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
31641da177e4SLinus Torvalds {
31651da177e4SLinus Torvalds 	int len;
31661da177e4SLinus Torvalds 
31671da177e4SLinus Torvalds 	len = PTR_ERR(link);
31681da177e4SLinus Torvalds 	if (IS_ERR(link))
31691da177e4SLinus Torvalds 		goto out;
31701da177e4SLinus Torvalds 
31711da177e4SLinus Torvalds 	len = strlen(link);
31721da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
31731da177e4SLinus Torvalds 		len = buflen;
31741da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
31751da177e4SLinus Torvalds 		len = -EFAULT;
31761da177e4SLinus Torvalds out:
31771da177e4SLinus Torvalds 	return len;
31781da177e4SLinus Torvalds }
31791da177e4SLinus Torvalds 
31801da177e4SLinus Torvalds /*
31811da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
31821da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
31831da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
31841da177e4SLinus Torvalds  */
31851da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
31861da177e4SLinus Torvalds {
31871da177e4SLinus Torvalds 	struct nameidata nd;
3188cc314eefSLinus Torvalds 	void *cookie;
3189694a1764SMarcin Slusarz 	int res;
3190cc314eefSLinus Torvalds 
31911da177e4SLinus Torvalds 	nd.depth = 0;
3192cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3193694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3194694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3195694a1764SMarcin Slusarz 
3196694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
31971da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3198cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3199694a1764SMarcin Slusarz 	return res;
32001da177e4SLinus Torvalds }
32011da177e4SLinus Torvalds 
32021da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
32031da177e4SLinus Torvalds {
32041da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
32051da177e4SLinus Torvalds }
32061da177e4SLinus Torvalds 
32071da177e4SLinus Torvalds /* get the link contents into pagecache */
32081da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
32091da177e4SLinus Torvalds {
3210ebd09abbSDuane Griffin 	char *kaddr;
32111da177e4SLinus Torvalds 	struct page *page;
32121da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3213090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
32141da177e4SLinus Torvalds 	if (IS_ERR(page))
32156fe6900eSNick Piggin 		return (char*)page;
32161da177e4SLinus Torvalds 	*ppage = page;
3217ebd09abbSDuane Griffin 	kaddr = kmap(page);
3218ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3219ebd09abbSDuane Griffin 	return kaddr;
32201da177e4SLinus Torvalds }
32211da177e4SLinus Torvalds 
32221da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32231da177e4SLinus Torvalds {
32241da177e4SLinus Torvalds 	struct page *page = NULL;
32251da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
32261da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
32271da177e4SLinus Torvalds 	if (page) {
32281da177e4SLinus Torvalds 		kunmap(page);
32291da177e4SLinus Torvalds 		page_cache_release(page);
32301da177e4SLinus Torvalds 	}
32311da177e4SLinus Torvalds 	return res;
32321da177e4SLinus Torvalds }
32331da177e4SLinus Torvalds 
3234cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
32351da177e4SLinus Torvalds {
3236cc314eefSLinus Torvalds 	struct page *page = NULL;
32371da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3238cc314eefSLinus Torvalds 	return page;
32391da177e4SLinus Torvalds }
32401da177e4SLinus Torvalds 
3241cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
32421da177e4SLinus Torvalds {
3243cc314eefSLinus Torvalds 	struct page *page = cookie;
3244cc314eefSLinus Torvalds 
3245cc314eefSLinus Torvalds 	if (page) {
32461da177e4SLinus Torvalds 		kunmap(page);
32471da177e4SLinus Torvalds 		page_cache_release(page);
32481da177e4SLinus Torvalds 	}
32491da177e4SLinus Torvalds }
32501da177e4SLinus Torvalds 
325154566b2cSNick Piggin /*
325254566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
325354566b2cSNick Piggin  */
325454566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
32551da177e4SLinus Torvalds {
32561da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
32570adb25d2SKirill Korotaev 	struct page *page;
3258afddba49SNick Piggin 	void *fsdata;
3259beb497abSDmitriy Monakhov 	int err;
32601da177e4SLinus Torvalds 	char *kaddr;
326154566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
326254566b2cSNick Piggin 	if (nofs)
326354566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
32641da177e4SLinus Torvalds 
32657e53cac4SNeilBrown retry:
3266afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
326754566b2cSNick Piggin 				flags, &page, &fsdata);
32681da177e4SLinus Torvalds 	if (err)
3269afddba49SNick Piggin 		goto fail;
3270afddba49SNick Piggin 
32711da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
32721da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
32731da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
3274afddba49SNick Piggin 
3275afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3276afddba49SNick Piggin 							page, fsdata);
32771da177e4SLinus Torvalds 	if (err < 0)
32781da177e4SLinus Torvalds 		goto fail;
3279afddba49SNick Piggin 	if (err < len-1)
3280afddba49SNick Piggin 		goto retry;
3281afddba49SNick Piggin 
32821da177e4SLinus Torvalds 	mark_inode_dirty(inode);
32831da177e4SLinus Torvalds 	return 0;
32841da177e4SLinus Torvalds fail:
32851da177e4SLinus Torvalds 	return err;
32861da177e4SLinus Torvalds }
32871da177e4SLinus Torvalds 
32880adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
32890adb25d2SKirill Korotaev {
32900adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
329154566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
32920adb25d2SKirill Korotaev }
32930adb25d2SKirill Korotaev 
329492e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
32951da177e4SLinus Torvalds 	.readlink	= generic_readlink,
32961da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
32971da177e4SLinus Torvalds 	.put_link	= page_put_link,
32981da177e4SLinus Torvalds };
32991da177e4SLinus Torvalds 
33002d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3301cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
33021da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
33031da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
33041da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
33051da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
33061da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
33071da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
33081da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
33091da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
33101da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
33110adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
33121da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
33131da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3314c9c6cac0SAl Viro EXPORT_SYMBOL(kern_path_parent);
3315d1811465SAl Viro EXPORT_SYMBOL(kern_path);
331616f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3317f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
33181da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
33191da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
33201da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
33211da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
33221da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
33231da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
33241da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
33251da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
33261da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
33271da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
33281da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
33291da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
33301da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
33311da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3332