xref: /openbmc/linux/fs/namei.c (revision 73d049a4)
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
731da177e4SLinus Torvalds  * the name is a symlink pointing to a non-existant 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 
1391da177e4SLinus Torvalds char * getname(const char __user * filename)
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) {
1501da177e4SLinus Torvalds 			__putname(tmp);
1511da177e4SLinus Torvalds 			result = ERR_PTR(retval);
1521da177e4SLinus Torvalds 		}
1531da177e4SLinus Torvalds 	}
1541da177e4SLinus Torvalds 	audit_getname(result);
1551da177e4SLinus Torvalds 	return result;
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
1591da177e4SLinus Torvalds void putname(const char *name)
1601da177e4SLinus Torvalds {
1615ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
1621da177e4SLinus Torvalds 		audit_putname(name);
1631da177e4SLinus Torvalds 	else
1641da177e4SLinus Torvalds 		__putname(name);
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds EXPORT_SYMBOL(putname);
1671da177e4SLinus Torvalds #endif
1681da177e4SLinus Torvalds 
1695909ccaaSLinus Torvalds /*
1705909ccaaSLinus Torvalds  * This does basic POSIX ACL permission checking
1715909ccaaSLinus Torvalds  */
172b74c79e9SNick Piggin static int acl_permission_check(struct inode *inode, int mask, unsigned int flags,
173b74c79e9SNick Piggin 		int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
1745909ccaaSLinus Torvalds {
1755909ccaaSLinus Torvalds 	umode_t			mode = inode->i_mode;
1765909ccaaSLinus Torvalds 
1775909ccaaSLinus Torvalds 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
1785909ccaaSLinus Torvalds 
1795909ccaaSLinus Torvalds 	if (current_fsuid() == inode->i_uid)
1805909ccaaSLinus Torvalds 		mode >>= 6;
1815909ccaaSLinus Torvalds 	else {
1825909ccaaSLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
183b74c79e9SNick Piggin 			int error = check_acl(inode, mask, flags);
1845909ccaaSLinus Torvalds 			if (error != -EAGAIN)
1855909ccaaSLinus Torvalds 				return error;
1865909ccaaSLinus Torvalds 		}
1875909ccaaSLinus Torvalds 
1885909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
1895909ccaaSLinus Torvalds 			mode >>= 3;
1905909ccaaSLinus Torvalds 	}
1915909ccaaSLinus Torvalds 
1925909ccaaSLinus Torvalds 	/*
1935909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
1945909ccaaSLinus Torvalds 	 */
1955909ccaaSLinus Torvalds 	if ((mask & ~mode) == 0)
1965909ccaaSLinus Torvalds 		return 0;
1975909ccaaSLinus Torvalds 	return -EACCES;
1985909ccaaSLinus Torvalds }
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds /**
2011da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
2021da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2031da177e4SLinus Torvalds  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
2041da177e4SLinus Torvalds  * @check_acl:	optional callback to check for Posix ACLs
20539191628SRandy Dunlap  * @flags:	IPERM_FLAG_ flags.
2061da177e4SLinus Torvalds  *
2071da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
2081da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
2091da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
210b74c79e9SNick Piggin  * are used for other things.
211b74c79e9SNick Piggin  *
212b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
213b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
214b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
2151da177e4SLinus Torvalds  */
216b74c79e9SNick Piggin int generic_permission(struct inode *inode, int mask, unsigned int flags,
217b74c79e9SNick Piggin 	int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
2181da177e4SLinus Torvalds {
2195909ccaaSLinus Torvalds 	int ret;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	/*
2225909ccaaSLinus Torvalds 	 * Do the basic POSIX ACL permission checks.
2231da177e4SLinus Torvalds 	 */
224b74c79e9SNick Piggin 	ret = acl_permission_check(inode, mask, flags, check_acl);
2255909ccaaSLinus Torvalds 	if (ret != -EACCES)
2265909ccaaSLinus Torvalds 		return ret;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	/*
2291da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
2301da177e4SLinus Torvalds 	 * Executable DACs are overridable if at least one exec bit is set.
2311da177e4SLinus Torvalds 	 */
232f696a365SMiklos Szeredi 	if (!(mask & MAY_EXEC) || execute_ok(inode))
2331da177e4SLinus Torvalds 		if (capable(CAP_DAC_OVERRIDE))
2341da177e4SLinus Torvalds 			return 0;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	/*
2371da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
2381da177e4SLinus Torvalds 	 */
2397ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
2401da177e4SLinus Torvalds 	if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
2411da177e4SLinus Torvalds 		if (capable(CAP_DAC_READ_SEARCH))
2421da177e4SLinus Torvalds 			return 0;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	return -EACCES;
2451da177e4SLinus Torvalds }
2461da177e4SLinus Torvalds 
247cb23beb5SChristoph Hellwig /**
248cb23beb5SChristoph Hellwig  * inode_permission  -  check for access rights to a given inode
249cb23beb5SChristoph Hellwig  * @inode:	inode to check permission on
250cb23beb5SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
251cb23beb5SChristoph Hellwig  *
252cb23beb5SChristoph Hellwig  * Used to check for read/write/execute permissions on an inode.
253cb23beb5SChristoph Hellwig  * We use "fsuid" for this, letting us set arbitrary permissions
254cb23beb5SChristoph Hellwig  * for filesystem access without changing the "normal" uids which
255cb23beb5SChristoph Hellwig  * are used for other things.
256cb23beb5SChristoph Hellwig  */
257f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask)
2581da177e4SLinus Torvalds {
259e6305c43SAl Viro 	int retval;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	if (mask & MAY_WRITE) {
26222590e41SMiklos Szeredi 		umode_t mode = inode->i_mode;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 		/*
2651da177e4SLinus Torvalds 		 * Nobody gets write access to a read-only fs.
2661da177e4SLinus Torvalds 		 */
2671da177e4SLinus Torvalds 		if (IS_RDONLY(inode) &&
2681da177e4SLinus Torvalds 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
2691da177e4SLinus Torvalds 			return -EROFS;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 		/*
2721da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
2731da177e4SLinus Torvalds 		 */
2741da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
2751da177e4SLinus Torvalds 			return -EACCES;
2761da177e4SLinus Torvalds 	}
2771da177e4SLinus Torvalds 
278acfa4380SAl Viro 	if (inode->i_op->permission)
279b74c79e9SNick Piggin 		retval = inode->i_op->permission(inode, mask, 0);
280f696a365SMiklos Szeredi 	else
281b74c79e9SNick Piggin 		retval = generic_permission(inode, mask, 0,
282b74c79e9SNick Piggin 				inode->i_op->check_acl);
283f696a365SMiklos Szeredi 
2841da177e4SLinus Torvalds 	if (retval)
2851da177e4SLinus Torvalds 		return retval;
2861da177e4SLinus Torvalds 
28708ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
28808ce5f16SSerge E. Hallyn 	if (retval)
28908ce5f16SSerge E. Hallyn 		return retval;
29008ce5f16SSerge E. Hallyn 
291d09ca739SEric Paris 	return security_inode_permission(inode, mask);
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
294e4543eddSChristoph Hellwig /**
2958c744fb8SChristoph Hellwig  * file_permission  -  check for additional access rights to a given file
2968c744fb8SChristoph Hellwig  * @file:	file to check access rights for
2978c744fb8SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
2988c744fb8SChristoph Hellwig  *
2998c744fb8SChristoph Hellwig  * Used to check for read/write/execute permissions on an already opened
3008c744fb8SChristoph Hellwig  * file.
3018c744fb8SChristoph Hellwig  *
3028c744fb8SChristoph Hellwig  * Note:
3038c744fb8SChristoph Hellwig  *	Do not use this function in new code.  All access checks should
304cb23beb5SChristoph Hellwig  *	be done using inode_permission().
3058c744fb8SChristoph Hellwig  */
3068c744fb8SChristoph Hellwig int file_permission(struct file *file, int mask)
3078c744fb8SChristoph Hellwig {
308f419a2e3SAl Viro 	return inode_permission(file->f_path.dentry->d_inode, mask);
3098c744fb8SChristoph Hellwig }
3108c744fb8SChristoph Hellwig 
3111da177e4SLinus Torvalds /*
3121da177e4SLinus Torvalds  * get_write_access() gets write permission for a file.
3131da177e4SLinus Torvalds  * put_write_access() releases this write permission.
3141da177e4SLinus Torvalds  * This is used for regular files.
3151da177e4SLinus Torvalds  * We cannot support write (and maybe mmap read-write shared) accesses and
3161da177e4SLinus Torvalds  * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
3171da177e4SLinus Torvalds  * can have the following values:
3181da177e4SLinus Torvalds  * 0: no writers, no VM_DENYWRITE mappings
3191da177e4SLinus Torvalds  * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
3201da177e4SLinus Torvalds  * > 0: (i_writecount) users are writing to the file.
3211da177e4SLinus Torvalds  *
3221da177e4SLinus Torvalds  * Normally we operate on that counter with atomic_{inc,dec} and it's safe
3231da177e4SLinus Torvalds  * except for the cases where we don't hold i_writecount yet. Then we need to
3241da177e4SLinus Torvalds  * use {get,deny}_write_access() - these functions check the sign and refuse
3251da177e4SLinus Torvalds  * to do the change if sign is wrong. Exclusion between them is provided by
3261da177e4SLinus Torvalds  * the inode->i_lock spinlock.
3271da177e4SLinus Torvalds  */
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds int get_write_access(struct inode * inode)
3301da177e4SLinus Torvalds {
3311da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
3321da177e4SLinus Torvalds 	if (atomic_read(&inode->i_writecount) < 0) {
3331da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
3341da177e4SLinus Torvalds 		return -ETXTBSY;
3351da177e4SLinus Torvalds 	}
3361da177e4SLinus Torvalds 	atomic_inc(&inode->i_writecount);
3371da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	return 0;
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds int deny_write_access(struct file * file)
3431da177e4SLinus Torvalds {
3440f7fc9e4SJosef "Jeff" Sipek 	struct inode *inode = file->f_path.dentry->d_inode;
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
3471da177e4SLinus Torvalds 	if (atomic_read(&inode->i_writecount) > 0) {
3481da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
3491da177e4SLinus Torvalds 		return -ETXTBSY;
3501da177e4SLinus Torvalds 	}
3511da177e4SLinus Torvalds 	atomic_dec(&inode->i_writecount);
3521da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 	return 0;
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
3571d957f9bSJan Blunck /**
3585dd784d0SJan Blunck  * path_get - get a reference to a path
3595dd784d0SJan Blunck  * @path: path to get the reference to
3605dd784d0SJan Blunck  *
3615dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3625dd784d0SJan Blunck  */
3635dd784d0SJan Blunck void path_get(struct path *path)
3645dd784d0SJan Blunck {
3655dd784d0SJan Blunck 	mntget(path->mnt);
3665dd784d0SJan Blunck 	dget(path->dentry);
3675dd784d0SJan Blunck }
3685dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
3695dd784d0SJan Blunck 
3705dd784d0SJan Blunck /**
3711d957f9bSJan Blunck  * path_put - put a reference to a path
3721d957f9bSJan Blunck  * @path: path to put the reference to
3731d957f9bSJan Blunck  *
3741d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
3751d957f9bSJan Blunck  */
3761d957f9bSJan Blunck void path_put(struct path *path)
3771da177e4SLinus Torvalds {
3781d957f9bSJan Blunck 	dput(path->dentry);
3791d957f9bSJan Blunck 	mntput(path->mnt);
3801da177e4SLinus Torvalds }
3811d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
3821da177e4SLinus Torvalds 
383834f2a4aSTrond Myklebust /**
38431e6b01fSNick Piggin  * nameidata_drop_rcu - drop this nameidata out of rcu-walk
38531e6b01fSNick Piggin  * @nd: nameidata pathwalk data to drop
38639191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
38731e6b01fSNick Piggin  *
38831e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
38931e6b01fSNick Piggin  * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt
39031e6b01fSNick Piggin  * to drop out of rcu-walk mode and take normal reference counts on dentries
39131e6b01fSNick Piggin  * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take
39231e6b01fSNick Piggin  * refcounts at the last known good point before rcu-walk got stuck, so
39331e6b01fSNick Piggin  * ref-walk may continue from there. If this is not successful (eg. a seqcount
39431e6b01fSNick Piggin  * has changed), then failure is returned and path walk restarts from the
39531e6b01fSNick Piggin  * beginning in ref-walk mode.
39631e6b01fSNick Piggin  *
39731e6b01fSNick Piggin  * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into
39831e6b01fSNick Piggin  * ref-walk. Must be called from rcu-walk context.
39931e6b01fSNick Piggin  */
40031e6b01fSNick Piggin static int nameidata_drop_rcu(struct nameidata *nd)
40131e6b01fSNick Piggin {
40231e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
40331e6b01fSNick Piggin 	struct dentry *dentry = nd->path.dentry;
4045b6ca027SAl Viro 	int want_root = 0;
40531e6b01fSNick Piggin 
40631e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
4075b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
4085b6ca027SAl Viro 		want_root = 1;
40931e6b01fSNick Piggin 		spin_lock(&fs->lock);
41031e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
41131e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
41231e6b01fSNick Piggin 			goto err_root;
41331e6b01fSNick Piggin 	}
41431e6b01fSNick Piggin 	spin_lock(&dentry->d_lock);
41531e6b01fSNick Piggin 	if (!__d_rcu_to_refcount(dentry, nd->seq))
41631e6b01fSNick Piggin 		goto err;
41731e6b01fSNick Piggin 	BUG_ON(nd->inode != dentry->d_inode);
41831e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
4195b6ca027SAl Viro 	if (want_root) {
42031e6b01fSNick Piggin 		path_get(&nd->root);
42131e6b01fSNick Piggin 		spin_unlock(&fs->lock);
42231e6b01fSNick Piggin 	}
42331e6b01fSNick Piggin 	mntget(nd->path.mnt);
42431e6b01fSNick Piggin 
42531e6b01fSNick Piggin 	rcu_read_unlock();
42631e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
42731e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
42831e6b01fSNick Piggin 	return 0;
42931e6b01fSNick Piggin err:
43031e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
43131e6b01fSNick Piggin err_root:
4325b6ca027SAl Viro 	if (want_root)
43331e6b01fSNick Piggin 		spin_unlock(&fs->lock);
43431e6b01fSNick Piggin 	return -ECHILD;
43531e6b01fSNick Piggin }
43631e6b01fSNick Piggin 
43731e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing.  */
43831e6b01fSNick Piggin static inline int nameidata_drop_rcu_maybe(struct nameidata *nd)
43931e6b01fSNick Piggin {
44031e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU)
44131e6b01fSNick Piggin 		return nameidata_drop_rcu(nd);
44231e6b01fSNick Piggin 	return 0;
44331e6b01fSNick Piggin }
44431e6b01fSNick Piggin 
44531e6b01fSNick Piggin /**
44631e6b01fSNick Piggin  * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk
44731e6b01fSNick Piggin  * @nd: nameidata pathwalk data to drop
44831e6b01fSNick Piggin  * @dentry: dentry to drop
44939191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
45031e6b01fSNick Piggin  *
45131e6b01fSNick Piggin  * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root,
45231e6b01fSNick Piggin  * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on
45331e6b01fSNick Piggin  * @nd. Must be called from rcu-walk context.
45431e6b01fSNick Piggin  */
45531e6b01fSNick Piggin static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry)
45631e6b01fSNick Piggin {
45731e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
45831e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
4595b6ca027SAl Viro 	int want_root = 0;
46031e6b01fSNick Piggin 
46131e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
4625b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
4635b6ca027SAl Viro 		want_root = 1;
46431e6b01fSNick Piggin 		spin_lock(&fs->lock);
46531e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
46631e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
46731e6b01fSNick Piggin 			goto err_root;
46831e6b01fSNick Piggin 	}
46931e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
47031e6b01fSNick Piggin 	spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
47131e6b01fSNick Piggin 	if (!__d_rcu_to_refcount(dentry, nd->seq))
47231e6b01fSNick Piggin 		goto err;
47331e6b01fSNick Piggin 	/*
47431e6b01fSNick Piggin 	 * If the sequence check on the child dentry passed, then the child has
47531e6b01fSNick Piggin 	 * not been removed from its parent. This means the parent dentry must
47631e6b01fSNick Piggin 	 * be valid and able to take a reference at this point.
47731e6b01fSNick Piggin 	 */
47831e6b01fSNick Piggin 	BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
47931e6b01fSNick Piggin 	BUG_ON(!parent->d_count);
48031e6b01fSNick Piggin 	parent->d_count++;
48131e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
48231e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
4835b6ca027SAl Viro 	if (want_root) {
48431e6b01fSNick Piggin 		path_get(&nd->root);
48531e6b01fSNick Piggin 		spin_unlock(&fs->lock);
48631e6b01fSNick Piggin 	}
48731e6b01fSNick Piggin 	mntget(nd->path.mnt);
48831e6b01fSNick Piggin 
48931e6b01fSNick Piggin 	rcu_read_unlock();
49031e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
49131e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
49231e6b01fSNick Piggin 	return 0;
49331e6b01fSNick Piggin err:
49431e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
49531e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
49631e6b01fSNick Piggin err_root:
4975b6ca027SAl Viro 	if (want_root)
49831e6b01fSNick Piggin 		spin_unlock(&fs->lock);
49931e6b01fSNick Piggin 	return -ECHILD;
50031e6b01fSNick Piggin }
50131e6b01fSNick Piggin 
50231e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing.  */
50331e6b01fSNick Piggin static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry)
50431e6b01fSNick Piggin {
505a7472babSAl Viro 	if (nd->flags & LOOKUP_RCU) {
506a7472babSAl Viro 		if (unlikely(nameidata_dentry_drop_rcu(nd, dentry))) {
507a7472babSAl Viro 			nd->flags &= ~LOOKUP_RCU;
5085b6ca027SAl Viro 			if (!(nd->flags & LOOKUP_ROOT))
509a7472babSAl Viro 				nd->root.mnt = NULL;
510a7472babSAl Viro 			rcu_read_unlock();
511a7472babSAl Viro 			br_read_unlock(vfsmount_lock);
512a7472babSAl Viro 			return -ECHILD;
513a7472babSAl Viro 		}
514a7472babSAl Viro 	}
51531e6b01fSNick Piggin 	return 0;
51631e6b01fSNick Piggin }
51731e6b01fSNick Piggin 
51831e6b01fSNick Piggin /**
51931e6b01fSNick Piggin  * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk
52031e6b01fSNick Piggin  * @nd: nameidata pathwalk data to drop
52139191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
52231e6b01fSNick Piggin  *
52331e6b01fSNick Piggin  * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk.
52431e6b01fSNick Piggin  * nd->path should be the final element of the lookup, so nd->root is discarded.
52531e6b01fSNick Piggin  * Must be called from rcu-walk context.
52631e6b01fSNick Piggin  */
52731e6b01fSNick Piggin static int nameidata_drop_rcu_last(struct nameidata *nd)
52831e6b01fSNick Piggin {
52931e6b01fSNick Piggin 	struct dentry *dentry = nd->path.dentry;
53031e6b01fSNick Piggin 
53131e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
53231e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
5335b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
53431e6b01fSNick Piggin 		nd->root.mnt = NULL;
53531e6b01fSNick Piggin 	spin_lock(&dentry->d_lock);
53631e6b01fSNick Piggin 	if (!__d_rcu_to_refcount(dentry, nd->seq))
53731e6b01fSNick Piggin 		goto err_unlock;
53831e6b01fSNick Piggin 	BUG_ON(nd->inode != dentry->d_inode);
53931e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
54031e6b01fSNick Piggin 
54131e6b01fSNick Piggin 	mntget(nd->path.mnt);
54231e6b01fSNick Piggin 
54331e6b01fSNick Piggin 	rcu_read_unlock();
54431e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
54531e6b01fSNick Piggin 
54631e6b01fSNick Piggin 	return 0;
54731e6b01fSNick Piggin 
54831e6b01fSNick Piggin err_unlock:
54931e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
55031e6b01fSNick Piggin 	rcu_read_unlock();
55131e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
55231e6b01fSNick Piggin 	return -ECHILD;
55331e6b01fSNick Piggin }
55431e6b01fSNick Piggin 
55531e6b01fSNick Piggin /**
556834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
557834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
558834f2a4aSTrond Myklebust  */
559834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
560834f2a4aSTrond Myklebust {
5612dab5974SLinus Torvalds 	struct file *file = nd->intent.open.file;
5622dab5974SLinus Torvalds 
5632dab5974SLinus Torvalds 	if (file && !IS_ERR(file)) {
5642dab5974SLinus Torvalds 		if (file->f_path.dentry == NULL)
5652dab5974SLinus Torvalds 			put_filp(file);
566834f2a4aSTrond Myklebust 		else
5672dab5974SLinus Torvalds 			fput(file);
5682dab5974SLinus Torvalds 	}
569834f2a4aSTrond Myklebust }
570834f2a4aSTrond Myklebust 
571f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
57234286d66SNick Piggin {
573f60aef7eSAl Viro 	return dentry->d_op->d_revalidate(dentry, nd);
57434286d66SNick Piggin }
57534286d66SNick Piggin 
576f5e1c1c1SAl Viro static struct dentry *
577bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd)
578bcdc5e01SIan Kent {
579f5e1c1c1SAl Viro 	int status = d_revalidate(dentry, nd);
580bcdc5e01SIan Kent 	if (unlikely(status <= 0)) {
581bcdc5e01SIan Kent 		/*
582bcdc5e01SIan Kent 		 * The dentry failed validation.
583bcdc5e01SIan Kent 		 * If d_revalidate returned 0 attempt to invalidate
584bcdc5e01SIan Kent 		 * the dentry otherwise d_revalidate is asking us
585bcdc5e01SIan Kent 		 * to return a fail status.
586bcdc5e01SIan Kent 		 */
58734286d66SNick Piggin 		if (status < 0) {
58834286d66SNick Piggin 			dput(dentry);
58934286d66SNick Piggin 			dentry = ERR_PTR(status);
590f5e1c1c1SAl Viro 		} else if (!d_invalidate(dentry)) {
591bcdc5e01SIan Kent 			dput(dentry);
592bcdc5e01SIan Kent 			dentry = NULL;
593bcdc5e01SIan Kent 		}
594bcdc5e01SIan Kent 	}
595f5e1c1c1SAl Viro 	return dentry;
596f5e1c1c1SAl Viro }
597f5e1c1c1SAl Viro 
5981da177e4SLinus Torvalds /*
59916c2cd71SAl Viro  * handle_reval_path - force revalidation of a dentry
60039159de2SJeff Layton  *
60139159de2SJeff Layton  * In some situations the path walking code will trust dentries without
60239159de2SJeff Layton  * revalidating them. This causes problems for filesystems that depend on
60339159de2SJeff Layton  * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
60439159de2SJeff Layton  * (which indicates that it's possible for the dentry to go stale), force
60539159de2SJeff Layton  * a d_revalidate call before proceeding.
60639159de2SJeff Layton  *
60739159de2SJeff Layton  * Returns 0 if the revalidation was successful. If the revalidation fails,
60839159de2SJeff Layton  * either return the error returned by d_revalidate or -ESTALE if the
60939159de2SJeff Layton  * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
61039159de2SJeff Layton  * invalidate the dentry. It's up to the caller to handle putting references
61139159de2SJeff Layton  * to the path if necessary.
61239159de2SJeff Layton  */
61316c2cd71SAl Viro static inline int handle_reval_path(struct nameidata *nd)
61439159de2SJeff Layton {
61516c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
61639159de2SJeff Layton 	int status;
61739159de2SJeff Layton 
61816c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
61939159de2SJeff Layton 		return 0;
62039159de2SJeff Layton 
62116c2cd71SAl Viro 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
62216c2cd71SAl Viro 		return 0;
62316c2cd71SAl Viro 
62416c2cd71SAl Viro 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
62516c2cd71SAl Viro 		return 0;
62616c2cd71SAl Viro 
62716c2cd71SAl Viro 	/* Note: we do not d_invalidate() */
62834286d66SNick Piggin 	status = d_revalidate(dentry, nd);
62939159de2SJeff Layton 	if (status > 0)
63039159de2SJeff Layton 		return 0;
63139159de2SJeff Layton 
63216c2cd71SAl Viro 	if (!status)
63339159de2SJeff Layton 		status = -ESTALE;
63416c2cd71SAl Viro 
63539159de2SJeff Layton 	return status;
63639159de2SJeff Layton }
63739159de2SJeff Layton 
63839159de2SJeff Layton /*
639b75b5086SAl Viro  * Short-cut version of permission(), for calling on directories
640b75b5086SAl Viro  * during pathname resolution.  Combines parts of permission()
641b75b5086SAl Viro  * and generic_permission(), and tests ONLY for MAY_EXEC permission.
6421da177e4SLinus Torvalds  *
6431da177e4SLinus Torvalds  * If appropriate, check DAC only.  If not appropriate, or
644b75b5086SAl Viro  * short-cut DAC fails, then call ->permission() to do more
6451da177e4SLinus Torvalds  * complete permission check.
6461da177e4SLinus Torvalds  */
647b74c79e9SNick Piggin static inline int exec_permission(struct inode *inode, unsigned int flags)
6481da177e4SLinus Torvalds {
6495909ccaaSLinus Torvalds 	int ret;
6501da177e4SLinus Torvalds 
651cb9179eaSLinus Torvalds 	if (inode->i_op->permission) {
652b74c79e9SNick Piggin 		ret = inode->i_op->permission(inode, MAY_EXEC, flags);
653b74c79e9SNick Piggin 	} else {
654b74c79e9SNick Piggin 		ret = acl_permission_check(inode, MAY_EXEC, flags,
655b74c79e9SNick Piggin 				inode->i_op->check_acl);
656cb9179eaSLinus Torvalds 	}
657b74c79e9SNick Piggin 	if (likely(!ret))
6581da177e4SLinus Torvalds 		goto ok;
659b74c79e9SNick Piggin 	if (ret == -ECHILD)
66031e6b01fSNick Piggin 		return ret;
6611da177e4SLinus Torvalds 
662f1ac9f6bSLinus Torvalds 	if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
6631da177e4SLinus Torvalds 		goto ok;
6641da177e4SLinus Torvalds 
6655909ccaaSLinus Torvalds 	return ret;
6661da177e4SLinus Torvalds ok:
667b74c79e9SNick Piggin 	return security_inode_exec_permission(inode, flags);
6681da177e4SLinus Torvalds }
6691da177e4SLinus Torvalds 
6702a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
6712a737871SAl Viro {
672f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
673f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
6742a737871SAl Viro }
6752a737871SAl Viro 
6766de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
6776de88d72SAl Viro 
67831e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
67931e6b01fSNick Piggin {
68031e6b01fSNick Piggin 	if (!nd->root.mnt) {
68131e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
682c28cc364SNick Piggin 		unsigned seq;
683c28cc364SNick Piggin 
684c28cc364SNick Piggin 		do {
685c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
68631e6b01fSNick Piggin 			nd->root = fs->root;
687c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
68831e6b01fSNick Piggin 	}
68931e6b01fSNick Piggin }
69031e6b01fSNick Piggin 
691f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
6921da177e4SLinus Torvalds {
69331e6b01fSNick Piggin 	int ret;
69431e6b01fSNick Piggin 
6951da177e4SLinus Torvalds 	if (IS_ERR(link))
6961da177e4SLinus Torvalds 		goto fail;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	if (*link == '/') {
6992a737871SAl Viro 		set_root(nd);
7001d957f9bSJan Blunck 		path_put(&nd->path);
7012a737871SAl Viro 		nd->path = nd->root;
7022a737871SAl Viro 		path_get(&nd->root);
70316c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
7041da177e4SLinus Torvalds 	}
70531e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
706b4091d5fSChristoph Hellwig 
70731e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
70831e6b01fSNick Piggin 	return ret;
7091da177e4SLinus Torvalds fail:
7101d957f9bSJan Blunck 	path_put(&nd->path);
7111da177e4SLinus Torvalds 	return PTR_ERR(link);
7121da177e4SLinus Torvalds }
7131da177e4SLinus Torvalds 
7141d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
715051d3812SIan Kent {
716051d3812SIan Kent 	dput(path->dentry);
7174ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
718051d3812SIan Kent 		mntput(path->mnt);
719051d3812SIan Kent }
720051d3812SIan Kent 
7217b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
7227b9337aaSNick Piggin 					struct nameidata *nd)
723051d3812SIan Kent {
72431e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
7254ac91378SJan Blunck 		dput(nd->path.dentry);
72631e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
7274ac91378SJan Blunck 			mntput(nd->path.mnt);
7289a229683SHuang Shijie 	}
72931e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
7304ac91378SJan Blunck 	nd->path.dentry = path->dentry;
731051d3812SIan Kent }
732051d3812SIan Kent 
733def4af30SAl Viro static __always_inline int
7347b9337aaSNick Piggin __do_follow_link(const struct path *link, struct nameidata *nd, void **p)
7351da177e4SLinus Torvalds {
7361da177e4SLinus Torvalds 	int error;
7377b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
7381da177e4SLinus Torvalds 
739844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
740844a3917SAl Viro 
7417b9337aaSNick Piggin 	touch_atime(link->mnt, dentry);
7421da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
743cd4e91d3SAl Viro 
74487556ef1SDavid Howells 	if (link->mnt == nd->path.mnt)
7457b9337aaSNick Piggin 		mntget(link->mnt);
74631e6b01fSNick Piggin 
74736f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
74836f3b4f6SAl Viro 	if (error) {
74936f3b4f6SAl Viro 		*p = ERR_PTR(error); /* no ->put_link(), please */
75036f3b4f6SAl Viro 		path_put(&nd->path);
75136f3b4f6SAl Viro 		return error;
75236f3b4f6SAl Viro 	}
75336f3b4f6SAl Viro 
75486acdca1SAl Viro 	nd->last_type = LAST_BIND;
755def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
756def4af30SAl Viro 	error = PTR_ERR(*p);
757def4af30SAl Viro 	if (!IS_ERR(*p)) {
7581da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
759cc314eefSLinus Torvalds 		error = 0;
7601da177e4SLinus Torvalds 		if (s)
7611da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
76216c2cd71SAl Viro 		else if (nd->last_type == LAST_BIND)
76316c2cd71SAl Viro 			nd->flags |= LOOKUP_JUMPED;
7641da177e4SLinus Torvalds 	}
7651da177e4SLinus Torvalds 	return error;
7661da177e4SLinus Torvalds }
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds /*
7691da177e4SLinus Torvalds  * This limits recursive symlink follows to 8, while
7701da177e4SLinus Torvalds  * limiting consecutive symlinks to 40.
7711da177e4SLinus Torvalds  *
7721da177e4SLinus Torvalds  * Without that kind of total limit, nasty chains of consecutive
7731da177e4SLinus Torvalds  * symlinks can cause almost arbitrarily long lookups.
7741da177e4SLinus Torvalds  */
7753abb17e8SLinus Torvalds static inline int do_follow_link(struct inode *inode, struct path *path, struct nameidata *nd)
7761da177e4SLinus Torvalds {
777def4af30SAl Viro 	void *cookie;
7781da177e4SLinus Torvalds 	int err = -ELOOP;
779844a3917SAl Viro 
780844a3917SAl Viro 	/* We drop rcu-walk here */
781844a3917SAl Viro 	if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry))
782844a3917SAl Viro 		return -ECHILD;
7833abb17e8SLinus Torvalds 	BUG_ON(inode != path->dentry->d_inode);
784844a3917SAl Viro 
7851da177e4SLinus Torvalds 	if (current->link_count >= MAX_NESTED_LINKS)
7861da177e4SLinus Torvalds 		goto loop;
7871da177e4SLinus Torvalds 	if (current->total_link_count >= 40)
7881da177e4SLinus Torvalds 		goto loop;
7891da177e4SLinus Torvalds 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
7901da177e4SLinus Torvalds 	cond_resched();
7911da177e4SLinus Torvalds 	current->link_count++;
7921da177e4SLinus Torvalds 	current->total_link_count++;
7931da177e4SLinus Torvalds 	nd->depth++;
794def4af30SAl Viro 	err = __do_follow_link(path, nd, &cookie);
795def4af30SAl Viro 	if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link)
796def4af30SAl Viro 		path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie);
797258fa999SAl Viro 	path_put(path);
7981da177e4SLinus Torvalds 	current->link_count--;
7991da177e4SLinus Torvalds 	nd->depth--;
8001da177e4SLinus Torvalds 	return err;
8011da177e4SLinus Torvalds loop:
8021d957f9bSJan Blunck 	path_put_conditional(path, nd);
8031d957f9bSJan Blunck 	path_put(&nd->path);
8041da177e4SLinus Torvalds 	return err;
8051da177e4SLinus Torvalds }
8061da177e4SLinus Torvalds 
80731e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
80831e6b01fSNick Piggin {
80931e6b01fSNick Piggin 	struct vfsmount *parent;
81031e6b01fSNick Piggin 	struct dentry *mountpoint;
81131e6b01fSNick Piggin 
81231e6b01fSNick Piggin 	parent = path->mnt->mnt_parent;
81331e6b01fSNick Piggin 	if (parent == path->mnt)
81431e6b01fSNick Piggin 		return 0;
81531e6b01fSNick Piggin 	mountpoint = path->mnt->mnt_mountpoint;
81631e6b01fSNick Piggin 	path->dentry = mountpoint;
81731e6b01fSNick Piggin 	path->mnt = parent;
81831e6b01fSNick Piggin 	return 1;
81931e6b01fSNick Piggin }
82031e6b01fSNick Piggin 
821bab77ebfSAl Viro int follow_up(struct path *path)
8221da177e4SLinus Torvalds {
8231da177e4SLinus Torvalds 	struct vfsmount *parent;
8241da177e4SLinus Torvalds 	struct dentry *mountpoint;
82599b7db7bSNick Piggin 
82699b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
827bab77ebfSAl Viro 	parent = path->mnt->mnt_parent;
828bab77ebfSAl Viro 	if (parent == path->mnt) {
82999b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
8301da177e4SLinus Torvalds 		return 0;
8311da177e4SLinus Torvalds 	}
8321da177e4SLinus Torvalds 	mntget(parent);
833bab77ebfSAl Viro 	mountpoint = dget(path->mnt->mnt_mountpoint);
83499b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
835bab77ebfSAl Viro 	dput(path->dentry);
836bab77ebfSAl Viro 	path->dentry = mountpoint;
837bab77ebfSAl Viro 	mntput(path->mnt);
838bab77ebfSAl Viro 	path->mnt = parent;
8391da177e4SLinus Torvalds 	return 1;
8401da177e4SLinus Torvalds }
8411da177e4SLinus Torvalds 
842b5c84bf6SNick Piggin /*
8439875cf80SDavid Howells  * Perform an automount
8449875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
8459875cf80SDavid Howells  *   were called with.
8461da177e4SLinus Torvalds  */
8479875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
8489875cf80SDavid Howells 			    bool *need_mntput)
84931e6b01fSNick Piggin {
8509875cf80SDavid Howells 	struct vfsmount *mnt;
851ea5b778aSDavid Howells 	int err;
8529875cf80SDavid Howells 
8539875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
8549875cf80SDavid Howells 		return -EREMOTE;
8559875cf80SDavid Howells 
8566f45b656SDavid Howells 	/* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
8576f45b656SDavid Howells 	 * and this is the terminal part of the path.
8586f45b656SDavid Howells 	 */
8596f45b656SDavid Howells 	if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
8606f45b656SDavid Howells 		return -EISDIR; /* we actually want to stop here */
8616f45b656SDavid Howells 
8629875cf80SDavid Howells 	/* We want to mount if someone is trying to open/create a file of any
8639875cf80SDavid Howells 	 * type under the mountpoint, wants to traverse through the mountpoint
8649875cf80SDavid Howells 	 * or wants to open the mounted directory.
8659875cf80SDavid Howells 	 *
8669875cf80SDavid Howells 	 * We don't want to mount if someone's just doing a stat and they've
8679875cf80SDavid Howells 	 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
8689875cf80SDavid Howells 	 * appended a '/' to the name.
8699875cf80SDavid Howells 	 */
8709875cf80SDavid Howells 	if (!(flags & LOOKUP_FOLLOW) &&
8719875cf80SDavid Howells 	    !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
8729875cf80SDavid Howells 		       LOOKUP_OPEN | LOOKUP_CREATE)))
8739875cf80SDavid Howells 		return -EISDIR;
8749875cf80SDavid Howells 
8759875cf80SDavid Howells 	current->total_link_count++;
8769875cf80SDavid Howells 	if (current->total_link_count >= 40)
8779875cf80SDavid Howells 		return -ELOOP;
8789875cf80SDavid Howells 
8799875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
8809875cf80SDavid Howells 	if (IS_ERR(mnt)) {
8819875cf80SDavid Howells 		/*
8829875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
8839875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
8849875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
8859875cf80SDavid Howells 		 *
8869875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
8879875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
8889875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
8899875cf80SDavid Howells 		 */
8909875cf80SDavid Howells 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
8919875cf80SDavid Howells 			return -EREMOTE;
8929875cf80SDavid Howells 		return PTR_ERR(mnt);
89331e6b01fSNick Piggin 	}
894ea5b778aSDavid Howells 
8959875cf80SDavid Howells 	if (!mnt) /* mount collision */
8969875cf80SDavid Howells 		return 0;
8979875cf80SDavid Howells 
89819a167afSAl Viro 	err = finish_automount(mnt, path);
899ea5b778aSDavid Howells 
900ea5b778aSDavid Howells 	switch (err) {
901ea5b778aSDavid Howells 	case -EBUSY:
902ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
90319a167afSAl Viro 		return 0;
904ea5b778aSDavid Howells 	case 0:
905463ffb2eSAl Viro 		dput(path->dentry);
9069875cf80SDavid Howells 		if (*need_mntput)
9079875cf80SDavid Howells 			mntput(path->mnt);
9089875cf80SDavid Howells 		path->mnt = mnt;
9099875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
9109875cf80SDavid Howells 		*need_mntput = true;
9119875cf80SDavid Howells 		return 0;
91219a167afSAl Viro 	default:
91319a167afSAl Viro 		return err;
9149875cf80SDavid Howells 	}
91519a167afSAl Viro 
916ea5b778aSDavid Howells }
9179875cf80SDavid Howells 
9189875cf80SDavid Howells /*
9199875cf80SDavid Howells  * Handle a dentry that is managed in some way.
920cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
9219875cf80SDavid Howells  * - Flagged as mountpoint
9229875cf80SDavid Howells  * - Flagged as automount point
9239875cf80SDavid Howells  *
9249875cf80SDavid Howells  * This may only be called in refwalk mode.
9259875cf80SDavid Howells  *
9269875cf80SDavid Howells  * Serialization is taken care of in namespace.c
9279875cf80SDavid Howells  */
9289875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
9299875cf80SDavid Howells {
9309875cf80SDavid Howells 	unsigned managed;
9319875cf80SDavid Howells 	bool need_mntput = false;
9329875cf80SDavid Howells 	int ret;
9339875cf80SDavid Howells 
9349875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
9359875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
9369875cf80SDavid Howells 	 * the components of that value change under us */
9379875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
9389875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
9399875cf80SDavid Howells 	       unlikely(managed != 0)) {
940cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
941cc53ce53SDavid Howells 		 * being held. */
942cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
943cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
944cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
945ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(path->dentry,
946ab90911fSDavid Howells 							   false, false);
947cc53ce53SDavid Howells 			if (ret < 0)
948cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
949cc53ce53SDavid Howells 		}
950cc53ce53SDavid Howells 
9519875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
9529875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
9539875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
9549875cf80SDavid Howells 			if (mounted) {
9559875cf80SDavid Howells 				dput(path->dentry);
9569875cf80SDavid Howells 				if (need_mntput)
957463ffb2eSAl Viro 					mntput(path->mnt);
958463ffb2eSAl Viro 				path->mnt = mounted;
959463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
9609875cf80SDavid Howells 				need_mntput = true;
9619875cf80SDavid Howells 				continue;
962463ffb2eSAl Viro 			}
963463ffb2eSAl Viro 
9649875cf80SDavid Howells 			/* Something is mounted on this dentry in another
9659875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
9669875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
9679875cf80SDavid Howells 			 * vfsmount_lock */
9681da177e4SLinus Torvalds 		}
9699875cf80SDavid Howells 
9709875cf80SDavid Howells 		/* Handle an automount point */
9719875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
9729875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
9739875cf80SDavid Howells 			if (ret < 0)
9749875cf80SDavid Howells 				return ret == -EISDIR ? 0 : ret;
9759875cf80SDavid Howells 			continue;
9769875cf80SDavid Howells 		}
9779875cf80SDavid Howells 
9789875cf80SDavid Howells 		/* We didn't change the current path point */
9799875cf80SDavid Howells 		break;
9809875cf80SDavid Howells 	}
9819875cf80SDavid Howells 	return 0;
9821da177e4SLinus Torvalds }
9831da177e4SLinus Torvalds 
984cc53ce53SDavid Howells int follow_down_one(struct path *path)
9851da177e4SLinus Torvalds {
9861da177e4SLinus Torvalds 	struct vfsmount *mounted;
9871da177e4SLinus Torvalds 
9881c755af4SAl Viro 	mounted = lookup_mnt(path);
9891da177e4SLinus Torvalds 	if (mounted) {
9909393bd07SAl Viro 		dput(path->dentry);
9919393bd07SAl Viro 		mntput(path->mnt);
9929393bd07SAl Viro 		path->mnt = mounted;
9939393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
9941da177e4SLinus Torvalds 		return 1;
9951da177e4SLinus Torvalds 	}
9961da177e4SLinus Torvalds 	return 0;
9971da177e4SLinus Torvalds }
9981da177e4SLinus Torvalds 
9999875cf80SDavid Howells /*
10009875cf80SDavid Howells  * Skip to top of mountpoint pile in rcuwalk mode.  We abort the rcu-walk if we
1001cc53ce53SDavid Howells  * meet a managed dentry and we're not walking to "..".  True is returned to
10029875cf80SDavid Howells  * continue, false to abort.
10039875cf80SDavid Howells  */
10049875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
10059875cf80SDavid Howells 			       struct inode **inode, bool reverse_transit)
10069875cf80SDavid Howells {
10079875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
10089875cf80SDavid Howells 		struct vfsmount *mounted;
1009ab90911fSDavid Howells 		if (unlikely(path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) &&
1010ab90911fSDavid Howells 		    !reverse_transit &&
1011ab90911fSDavid Howells 		    path->dentry->d_op->d_manage(path->dentry, false, true) < 0)
1012ab90911fSDavid Howells 			return false;
10139875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
10149875cf80SDavid Howells 		if (!mounted)
10159875cf80SDavid Howells 			break;
10169875cf80SDavid Howells 		path->mnt = mounted;
10179875cf80SDavid Howells 		path->dentry = mounted->mnt_root;
10189875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
10199875cf80SDavid Howells 		*inode = path->dentry->d_inode;
10209875cf80SDavid Howells 	}
10219875cf80SDavid Howells 
10229875cf80SDavid Howells 	if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
10239875cf80SDavid Howells 		return reverse_transit;
10249875cf80SDavid Howells 	return true;
10259875cf80SDavid Howells }
10269875cf80SDavid Howells 
102731e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
102831e6b01fSNick Piggin {
102931e6b01fSNick Piggin 	struct inode *inode = nd->inode;
103031e6b01fSNick Piggin 
103131e6b01fSNick Piggin 	set_root_rcu(nd);
103231e6b01fSNick Piggin 
103331e6b01fSNick Piggin 	while (1) {
103431e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
103531e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
103631e6b01fSNick Piggin 			break;
103731e6b01fSNick Piggin 		}
103831e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
103931e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
104031e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
104131e6b01fSNick Piggin 			unsigned seq;
104231e6b01fSNick Piggin 
104331e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
104431e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
1045ef7562d5SAl Viro 				goto failed;
104631e6b01fSNick Piggin 			inode = parent->d_inode;
104731e6b01fSNick Piggin 			nd->path.dentry = parent;
104831e6b01fSNick Piggin 			nd->seq = seq;
104931e6b01fSNick Piggin 			break;
105031e6b01fSNick Piggin 		}
105131e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
105231e6b01fSNick Piggin 			break;
105331e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
105431e6b01fSNick Piggin 		inode = nd->path.dentry->d_inode;
105531e6b01fSNick Piggin 	}
10569875cf80SDavid Howells 	__follow_mount_rcu(nd, &nd->path, &inode, true);
105731e6b01fSNick Piggin 	nd->inode = inode;
105831e6b01fSNick Piggin 	return 0;
1059ef7562d5SAl Viro 
1060ef7562d5SAl Viro failed:
1061ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
10625b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
1063ef7562d5SAl Viro 		nd->root.mnt = NULL;
1064ef7562d5SAl Viro 	rcu_read_unlock();
1065ef7562d5SAl Viro 	br_read_unlock(vfsmount_lock);
1066ef7562d5SAl Viro 	return -ECHILD;
106731e6b01fSNick Piggin }
106831e6b01fSNick Piggin 
10699875cf80SDavid Howells /*
1070cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
1071cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
1072cc53ce53SDavid Howells  * caller is permitted to proceed or not.
1073cc53ce53SDavid Howells  *
1074cc53ce53SDavid Howells  * Care must be taken as namespace_sem may be held (indicated by mounting_here
1075cc53ce53SDavid Howells  * being true).
1076cc53ce53SDavid Howells  */
1077cc53ce53SDavid Howells int follow_down(struct path *path, bool mounting_here)
1078cc53ce53SDavid Howells {
1079cc53ce53SDavid Howells 	unsigned managed;
1080cc53ce53SDavid Howells 	int ret;
1081cc53ce53SDavid Howells 
1082cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
1083cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1084cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1085cc53ce53SDavid Howells 		 * being held.
1086cc53ce53SDavid Howells 		 *
1087cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
1088cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
1089cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
1090cc53ce53SDavid Howells 		 * superstructure.
1091cc53ce53SDavid Howells 		 *
1092cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
1093cc53ce53SDavid Howells 		 */
1094cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1095cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1096cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
1097ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
1098ab90911fSDavid Howells 				path->dentry, mounting_here, false);
1099cc53ce53SDavid Howells 			if (ret < 0)
1100cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
1101cc53ce53SDavid Howells 		}
1102cc53ce53SDavid Howells 
1103cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
1104cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
1105cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
1106cc53ce53SDavid Howells 			if (!mounted)
1107cc53ce53SDavid Howells 				break;
1108cc53ce53SDavid Howells 			dput(path->dentry);
1109cc53ce53SDavid Howells 			mntput(path->mnt);
1110cc53ce53SDavid Howells 			path->mnt = mounted;
1111cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1112cc53ce53SDavid Howells 			continue;
1113cc53ce53SDavid Howells 		}
1114cc53ce53SDavid Howells 
1115cc53ce53SDavid Howells 		/* Don't handle automount points here */
1116cc53ce53SDavid Howells 		break;
1117cc53ce53SDavid Howells 	}
1118cc53ce53SDavid Howells 	return 0;
1119cc53ce53SDavid Howells }
1120cc53ce53SDavid Howells 
1121cc53ce53SDavid Howells /*
11229875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
11239875cf80SDavid Howells  */
11249875cf80SDavid Howells static void follow_mount(struct path *path)
11259875cf80SDavid Howells {
11269875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
11279875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
11289875cf80SDavid Howells 		if (!mounted)
11299875cf80SDavid Howells 			break;
11309875cf80SDavid Howells 		dput(path->dentry);
11319875cf80SDavid Howells 		mntput(path->mnt);
11329875cf80SDavid Howells 		path->mnt = mounted;
11339875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
11349875cf80SDavid Howells 	}
11359875cf80SDavid Howells }
11369875cf80SDavid Howells 
113731e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
11381da177e4SLinus Torvalds {
11392a737871SAl Viro 	set_root(nd);
1140e518ddb7SAndreas Mohr 
11411da177e4SLinus Torvalds 	while(1) {
11424ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
11431da177e4SLinus Torvalds 
11442a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
11452a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
11461da177e4SLinus Torvalds 			break;
11471da177e4SLinus Torvalds 		}
11484ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
11493088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
11503088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
11511da177e4SLinus Torvalds 			dput(old);
11521da177e4SLinus Torvalds 			break;
11531da177e4SLinus Torvalds 		}
11543088dd70SAl Viro 		if (!follow_up(&nd->path))
11551da177e4SLinus Torvalds 			break;
11561da177e4SLinus Torvalds 	}
115779ed0226SAl Viro 	follow_mount(&nd->path);
115831e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
11591da177e4SLinus Torvalds }
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds /*
1162baa03890SNick Piggin  * Allocate a dentry with name and parent, and perform a parent
1163baa03890SNick Piggin  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1164baa03890SNick Piggin  * on error. parent->d_inode->i_mutex must be held. d_lookup must
1165baa03890SNick Piggin  * have verified that no child exists while under i_mutex.
1166baa03890SNick Piggin  */
1167baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1168baa03890SNick Piggin 				struct qstr *name, struct nameidata *nd)
1169baa03890SNick Piggin {
1170baa03890SNick Piggin 	struct inode *inode = parent->d_inode;
1171baa03890SNick Piggin 	struct dentry *dentry;
1172baa03890SNick Piggin 	struct dentry *old;
1173baa03890SNick Piggin 
1174baa03890SNick Piggin 	/* Don't create child dentry for a dead directory. */
1175baa03890SNick Piggin 	if (unlikely(IS_DEADDIR(inode)))
1176baa03890SNick Piggin 		return ERR_PTR(-ENOENT);
1177baa03890SNick Piggin 
1178baa03890SNick Piggin 	dentry = d_alloc(parent, name);
1179baa03890SNick Piggin 	if (unlikely(!dentry))
1180baa03890SNick Piggin 		return ERR_PTR(-ENOMEM);
1181baa03890SNick Piggin 
1182baa03890SNick Piggin 	old = inode->i_op->lookup(inode, dentry, nd);
1183baa03890SNick Piggin 	if (unlikely(old)) {
1184baa03890SNick Piggin 		dput(dentry);
1185baa03890SNick Piggin 		dentry = old;
1186baa03890SNick Piggin 	}
1187baa03890SNick Piggin 	return dentry;
1188baa03890SNick Piggin }
1189baa03890SNick Piggin 
1190baa03890SNick Piggin /*
11911da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
11921da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
11931da177e4SLinus Torvalds  *  It _is_ time-critical.
11941da177e4SLinus Torvalds  */
11951da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
119631e6b01fSNick Piggin 			struct path *path, struct inode **inode)
11971da177e4SLinus Torvalds {
11984ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
119931e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
12005a18fff2SAl Viro 	int need_reval = 1;
12015a18fff2SAl Viro 	int status = 1;
12029875cf80SDavid Howells 	int err;
12039875cf80SDavid Howells 
12043cac260aSAl Viro 	/*
1205b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1206b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1207b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1208b04f784eSNick Piggin 	 */
120931e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
121031e6b01fSNick Piggin 		unsigned seq;
121131e6b01fSNick Piggin 		*inode = nd->inode;
121231e6b01fSNick Piggin 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
12135a18fff2SAl Viro 		if (!dentry)
12145a18fff2SAl Viro 			goto unlazy;
12155a18fff2SAl Viro 
121631e6b01fSNick Piggin 		/* Memory barrier in read_seqcount_begin of child is enough */
121731e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
121831e6b01fSNick Piggin 			return -ECHILD;
121931e6b01fSNick Piggin 		nd->seq = seq;
12205a18fff2SAl Viro 
122124643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
12225a18fff2SAl Viro 			status = d_revalidate(dentry, nd);
12235a18fff2SAl Viro 			if (unlikely(status <= 0)) {
12245a18fff2SAl Viro 				if (status != -ECHILD)
12255a18fff2SAl Viro 					need_reval = 0;
12265a18fff2SAl Viro 				goto unlazy;
12275a18fff2SAl Viro 			}
122824643087SAl Viro 		}
122931e6b01fSNick Piggin 		path->mnt = mnt;
123031e6b01fSNick Piggin 		path->dentry = dentry;
12319875cf80SDavid Howells 		if (likely(__follow_mount_rcu(nd, path, inode, false)))
12329875cf80SDavid Howells 			return 0;
12335a18fff2SAl Viro unlazy:
12345a18fff2SAl Viro 		if (dentry) {
12355a18fff2SAl Viro 			if (nameidata_dentry_drop_rcu(nd, dentry))
12365a18fff2SAl Viro 				return -ECHILD;
12375a18fff2SAl Viro 		} else {
12389875cf80SDavid Howells 			if (nameidata_drop_rcu(nd))
12399875cf80SDavid Howells 				return -ECHILD;
12409875cf80SDavid Howells 		}
12415a18fff2SAl Viro 	} else {
124231e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
124324643087SAl Viro 	}
12445a18fff2SAl Viro 
12455a18fff2SAl Viro retry:
12465a18fff2SAl Viro 	if (unlikely(!dentry)) {
12475a18fff2SAl Viro 		struct inode *dir = parent->d_inode;
12485a18fff2SAl Viro 		BUG_ON(nd->inode != dir);
12495a18fff2SAl Viro 
12505a18fff2SAl Viro 		mutex_lock(&dir->i_mutex);
12515a18fff2SAl Viro 		dentry = d_lookup(parent, name);
12525a18fff2SAl Viro 		if (likely(!dentry)) {
12535a18fff2SAl Viro 			dentry = d_alloc_and_lookup(parent, name, nd);
12545a18fff2SAl Viro 			if (IS_ERR(dentry)) {
12555a18fff2SAl Viro 				mutex_unlock(&dir->i_mutex);
12565a18fff2SAl Viro 				return PTR_ERR(dentry);
12575a18fff2SAl Viro 			}
12585a18fff2SAl Viro 			/* known good */
12595a18fff2SAl Viro 			need_reval = 0;
12605a18fff2SAl Viro 			status = 1;
12615a18fff2SAl Viro 		}
12625a18fff2SAl Viro 		mutex_unlock(&dir->i_mutex);
12635a18fff2SAl Viro 	}
12645a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
12655a18fff2SAl Viro 		status = d_revalidate(dentry, nd);
12665a18fff2SAl Viro 	if (unlikely(status <= 0)) {
12675a18fff2SAl Viro 		if (status < 0) {
12685a18fff2SAl Viro 			dput(dentry);
12695a18fff2SAl Viro 			return status;
12705a18fff2SAl Viro 		}
12715a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
12725a18fff2SAl Viro 			dput(dentry);
12735a18fff2SAl Viro 			dentry = NULL;
12745a18fff2SAl Viro 			need_reval = 1;
12755a18fff2SAl Viro 			goto retry;
12765a18fff2SAl Viro 		}
12775a18fff2SAl Viro 	}
12785a18fff2SAl Viro 
12791da177e4SLinus Torvalds 	path->mnt = mnt;
12801da177e4SLinus Torvalds 	path->dentry = dentry;
12819875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
128289312214SIan Kent 	if (unlikely(err < 0)) {
128389312214SIan Kent 		path_put_conditional(path, nd);
12849875cf80SDavid Howells 		return err;
128589312214SIan Kent 	}
128631e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
12871da177e4SLinus Torvalds 	return 0;
12881da177e4SLinus Torvalds }
12891da177e4SLinus Torvalds 
129052094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
129152094c8aSAl Viro {
129252094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
129352094c8aSAl Viro 		int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
129452094c8aSAl Viro 		if (err != -ECHILD)
129552094c8aSAl Viro 			return err;
129652094c8aSAl Viro 		if (nameidata_drop_rcu(nd))
129752094c8aSAl Viro 			return -ECHILD;
129852094c8aSAl Viro 	}
129952094c8aSAl Viro 	return exec_permission(nd->inode, 0);
130052094c8aSAl Viro }
130152094c8aSAl Viro 
13029856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
13039856fa1bSAl Viro {
13049856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
13059856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
13069856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
13079856fa1bSAl Viro 				return -ECHILD;
13089856fa1bSAl Viro 		} else
13099856fa1bSAl Viro 			follow_dotdot(nd);
13109856fa1bSAl Viro 	}
13119856fa1bSAl Viro 	return 0;
13129856fa1bSAl Viro }
13139856fa1bSAl Viro 
1314951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1315951361f9SAl Viro {
1316951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1317951361f9SAl Viro 		path_put(&nd->path);
1318951361f9SAl Viro 	} else {
1319951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
13205b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1321951361f9SAl Viro 			nd->root.mnt = NULL;
1322951361f9SAl Viro 		rcu_read_unlock();
1323951361f9SAl Viro 		br_read_unlock(vfsmount_lock);
1324951361f9SAl Viro 	}
1325951361f9SAl Viro }
1326951361f9SAl Viro 
13271da177e4SLinus Torvalds /*
13281da177e4SLinus Torvalds  * Name resolution.
1329ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1330ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
13311da177e4SLinus Torvalds  *
1332ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1333ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
13341da177e4SLinus Torvalds  */
13356de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
13361da177e4SLinus Torvalds {
13371da177e4SLinus Torvalds 	struct path next;
13381da177e4SLinus Torvalds 	int err;
13391da177e4SLinus Torvalds 	unsigned int lookup_flags = nd->flags;
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds 	while (*name=='/')
13421da177e4SLinus Torvalds 		name++;
13431da177e4SLinus Torvalds 	if (!*name)
1344086e183aSAl Viro 		return 0;
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 	if (nd->depth)
1347f55eab82STrond Myklebust 		lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
13501da177e4SLinus Torvalds 	for(;;) {
135131e6b01fSNick Piggin 		struct inode *inode;
13521da177e4SLinus Torvalds 		unsigned long hash;
13531da177e4SLinus Torvalds 		struct qstr this;
13541da177e4SLinus Torvalds 		unsigned int c;
1355fe479a58SAl Viro 		int type;
13561da177e4SLinus Torvalds 
1357cdce5d6bSTrond Myklebust 		nd->flags |= LOOKUP_CONTINUE;
135852094c8aSAl Viro 
135952094c8aSAl Viro 		err = may_lookup(nd);
13601da177e4SLinus Torvalds  		if (err)
13611da177e4SLinus Torvalds 			break;
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 		this.name = name;
13641da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 		hash = init_name_hash();
13671da177e4SLinus Torvalds 		do {
13681da177e4SLinus Torvalds 			name++;
13691da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
13701da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
13711da177e4SLinus Torvalds 		} while (c && (c != '/'));
13721da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
13731da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
13741da177e4SLinus Torvalds 
1375fe479a58SAl Viro 		type = LAST_NORM;
1376fe479a58SAl Viro 		if (this.name[0] == '.') switch (this.len) {
1377fe479a58SAl Viro 			case 2:
137816c2cd71SAl Viro 				if (this.name[1] == '.') {
1379fe479a58SAl Viro 					type = LAST_DOTDOT;
138016c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
138116c2cd71SAl Viro 				}
1382fe479a58SAl Viro 				break;
1383fe479a58SAl Viro 			case 1:
1384fe479a58SAl Viro 				type = LAST_DOT;
1385fe479a58SAl Viro 		}
13865a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
13875a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
138816c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
13895a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
13905a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
13915a202bcdSAl Viro 							   &this);
13925a202bcdSAl Viro 				if (err < 0)
13935a202bcdSAl Viro 					break;
13945a202bcdSAl Viro 			}
13955a202bcdSAl Viro 		}
1396fe479a58SAl Viro 
13971da177e4SLinus Torvalds 		/* remove trailing slashes? */
13981da177e4SLinus Torvalds 		if (!c)
13991da177e4SLinus Torvalds 			goto last_component;
14001da177e4SLinus Torvalds 		while (*++name == '/');
14011da177e4SLinus Torvalds 		if (!*name)
14021da177e4SLinus Torvalds 			goto last_with_slashes;
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds 		/*
14051da177e4SLinus Torvalds 		 * "." and ".." are special - ".." especially so because it has
14061da177e4SLinus Torvalds 		 * to be able to know about the current root directory and
14071da177e4SLinus Torvalds 		 * parent relationships.
14081da177e4SLinus Torvalds 		 */
1409fe479a58SAl Viro 		if (unlikely(type != LAST_NORM)) {
1410ef7562d5SAl Viro 			if (handle_dots(nd, type))
1411ef7562d5SAl Viro 				return -ECHILD;
14121da177e4SLinus Torvalds 			continue;
14131da177e4SLinus Torvalds 		}
1414fe479a58SAl Viro 
14151da177e4SLinus Torvalds 		/* This does the actual lookups.. */
141631e6b01fSNick Piggin 		err = do_lookup(nd, &this, &next, &inode);
14171da177e4SLinus Torvalds 		if (err)
14181da177e4SLinus Torvalds 			break;
14191da177e4SLinus Torvalds 
14207bc055d1SAl Viro 		if (inode && inode->i_op->follow_link) {
14213abb17e8SLinus Torvalds 			err = do_follow_link(inode, &next, nd);
14221da177e4SLinus Torvalds 			if (err)
1423a7472babSAl Viro 				return err;
142431e6b01fSNick Piggin 			nd->inode = nd->path.dentry->d_inode;
142531e6b01fSNick Piggin 		} else {
142609dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
142731e6b01fSNick Piggin 			nd->inode = inode;
142831e6b01fSNick Piggin 		}
14297bc055d1SAl Viro 		err = -ENOENT;
14307bc055d1SAl Viro 		if (!nd->inode)
14317bc055d1SAl Viro 			break;
14321da177e4SLinus Torvalds 		err = -ENOTDIR;
143331e6b01fSNick Piggin 		if (!nd->inode->i_op->lookup)
14341da177e4SLinus Torvalds 			break;
14351da177e4SLinus Torvalds 		continue;
14361da177e4SLinus Torvalds 		/* here ends the main loop */
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds last_with_slashes:
14391da177e4SLinus Torvalds 		lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
14401da177e4SLinus Torvalds last_component:
1441f55eab82STrond Myklebust 		/* Clear LOOKUP_CONTINUE iff it was previously unset */
1442f55eab82STrond Myklebust 		nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
14431da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_PARENT)
14441da177e4SLinus Torvalds 			goto lookup_parent;
1445ef7562d5SAl Viro 		if (unlikely(type != LAST_NORM))
1446ef7562d5SAl Viro 			return handle_dots(nd, type);
144731e6b01fSNick Piggin 		err = do_lookup(nd, &this, &next, &inode);
14481da177e4SLinus Torvalds 		if (err)
14491da177e4SLinus Torvalds 			break;
1450db372915SDavid Howells 		if (inode && unlikely(inode->i_op->follow_link) &&
1451db372915SDavid Howells 		    (lookup_flags & LOOKUP_FOLLOW)) {
14523abb17e8SLinus Torvalds 			err = do_follow_link(inode, &next, nd);
14531da177e4SLinus Torvalds 			if (err)
1454a7472babSAl Viro 				return err;
145531e6b01fSNick Piggin 			nd->inode = nd->path.dentry->d_inode;
145631e6b01fSNick Piggin 		} else {
145709dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
145831e6b01fSNick Piggin 			nd->inode = inode;
145931e6b01fSNick Piggin 		}
14601da177e4SLinus Torvalds 		err = -ENOENT;
146131e6b01fSNick Piggin 		if (!nd->inode)
14621da177e4SLinus Torvalds 			break;
14631da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_DIRECTORY) {
14641da177e4SLinus Torvalds 			err = -ENOTDIR;
146531e6b01fSNick Piggin 			if (!nd->inode->i_op->lookup)
14661da177e4SLinus Torvalds 				break;
14671da177e4SLinus Torvalds 		}
1468086e183aSAl Viro 		return 0;
14691da177e4SLinus Torvalds lookup_parent:
14701da177e4SLinus Torvalds 		nd->last = this;
1471fe479a58SAl Viro 		nd->last_type = type;
14721da177e4SLinus Torvalds 		return 0;
14731da177e4SLinus Torvalds 	}
1474951361f9SAl Viro 	terminate_walk(nd);
14751da177e4SLinus Torvalds 	return err;
14761da177e4SLinus Torvalds }
14771da177e4SLinus Torvalds 
147870e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
147970e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
148031e6b01fSNick Piggin {
148131e6b01fSNick Piggin 	int retval = 0;
148231e6b01fSNick Piggin 	int fput_needed;
148331e6b01fSNick Piggin 	struct file *file;
148431e6b01fSNick Piggin 
148531e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
148616c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
148731e6b01fSNick Piggin 	nd->depth = 0;
14885b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
14895b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
149073d049a4SAl Viro 		if (*name) {
14915b6ca027SAl Viro 			if (!inode->i_op->lookup)
14925b6ca027SAl Viro 				return -ENOTDIR;
14935b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
14945b6ca027SAl Viro 			if (retval)
14955b6ca027SAl Viro 				return retval;
149673d049a4SAl Viro 		}
14975b6ca027SAl Viro 		nd->path = nd->root;
14985b6ca027SAl Viro 		nd->inode = inode;
14995b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
15005b6ca027SAl Viro 			br_read_lock(vfsmount_lock);
15015b6ca027SAl Viro 			rcu_read_lock();
15025b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
15035b6ca027SAl Viro 		} else {
15045b6ca027SAl Viro 			path_get(&nd->path);
15055b6ca027SAl Viro 		}
15065b6ca027SAl Viro 		return 0;
15075b6ca027SAl Viro 	}
15085b6ca027SAl Viro 
150931e6b01fSNick Piggin 	nd->root.mnt = NULL;
151031e6b01fSNick Piggin 
151131e6b01fSNick Piggin 	if (*name=='/') {
1512e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
151331e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
151431e6b01fSNick Piggin 			rcu_read_lock();
1515e41f7d4eSAl Viro 			set_root_rcu(nd);
1516e41f7d4eSAl Viro 		} else {
1517e41f7d4eSAl Viro 			set_root(nd);
1518e41f7d4eSAl Viro 			path_get(&nd->root);
1519e41f7d4eSAl Viro 		}
152031e6b01fSNick Piggin 		nd->path = nd->root;
152131e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1522e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
152331e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1524c28cc364SNick Piggin 			unsigned seq;
152531e6b01fSNick Piggin 
152631e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
152731e6b01fSNick Piggin 			rcu_read_lock();
152831e6b01fSNick Piggin 
1529c28cc364SNick Piggin 			do {
1530c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
153131e6b01fSNick Piggin 				nd->path = fs->pwd;
1532c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1533c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1534e41f7d4eSAl Viro 		} else {
1535e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1536e41f7d4eSAl Viro 		}
153731e6b01fSNick Piggin 	} else {
153831e6b01fSNick Piggin 		struct dentry *dentry;
153931e6b01fSNick Piggin 
154031e6b01fSNick Piggin 		file = fget_light(dfd, &fput_needed);
154131e6b01fSNick Piggin 		retval = -EBADF;
154231e6b01fSNick Piggin 		if (!file)
154331e6b01fSNick Piggin 			goto out_fail;
154431e6b01fSNick Piggin 
154531e6b01fSNick Piggin 		dentry = file->f_path.dentry;
154631e6b01fSNick Piggin 
154731e6b01fSNick Piggin 		retval = -ENOTDIR;
154831e6b01fSNick Piggin 		if (!S_ISDIR(dentry->d_inode->i_mode))
154931e6b01fSNick Piggin 			goto fput_fail;
155031e6b01fSNick Piggin 
155131e6b01fSNick Piggin 		retval = file_permission(file, MAY_EXEC);
155231e6b01fSNick Piggin 		if (retval)
155331e6b01fSNick Piggin 			goto fput_fail;
155431e6b01fSNick Piggin 
155531e6b01fSNick Piggin 		nd->path = file->f_path;
1556e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
155731e6b01fSNick Piggin 			if (fput_needed)
155870e9b357SAl Viro 				*fp = file;
1559c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
156031e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
156131e6b01fSNick Piggin 			rcu_read_lock();
15625590ff0dSUlrich Drepper 		} else {
15635dd784d0SJan Blunck 			path_get(&file->f_path);
15645590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
15651da177e4SLinus Torvalds 		}
1566e41f7d4eSAl Viro 	}
1567e41f7d4eSAl Viro 
156831e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
15699b4a9b14SAl Viro 	return 0;
15702dfdd266SJosef 'Jeff' Sipek 
15719b4a9b14SAl Viro fput_fail:
15729b4a9b14SAl Viro 	fput_light(file, fput_needed);
15739b4a9b14SAl Viro out_fail:
15749b4a9b14SAl Viro 	return retval;
15759b4a9b14SAl Viro }
15769b4a9b14SAl Viro 
15779b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1578ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
15799b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
15809b4a9b14SAl Viro {
158170e9b357SAl Viro 	struct file *base = NULL;
158231e6b01fSNick Piggin 	int retval;
158331e6b01fSNick Piggin 
158431e6b01fSNick Piggin 	/*
158531e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
158631e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
158731e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
158831e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
158931e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
159031e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
159131e6b01fSNick Piggin 	 * analogue, foo_rcu().
159231e6b01fSNick Piggin 	 *
159331e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
159431e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
159531e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
159631e6b01fSNick Piggin 	 * be able to complete).
159731e6b01fSNick Piggin 	 */
159870e9b357SAl Viro 	retval = path_init(dfd, name, flags, nd, &base);
1599ee0827cdSAl Viro 
160031e6b01fSNick Piggin 	if (unlikely(retval))
160131e6b01fSNick Piggin 		return retval;
1602ee0827cdSAl Viro 
1603ee0827cdSAl Viro 	current->total_link_count = 0;
1604ee0827cdSAl Viro 	retval = link_path_walk(name, nd);
1605ee0827cdSAl Viro 
1606ee0827cdSAl Viro 	if (nd->flags & LOOKUP_RCU) {
16074455ca62SAl Viro 		/* went all way through without dropping RCU */
16084455ca62SAl Viro 		BUG_ON(retval);
1609086e183aSAl Viro 		if (nameidata_drop_rcu_last(nd))
1610086e183aSAl Viro 			retval = -ECHILD;
1611086e183aSAl Viro 	}
161231e6b01fSNick Piggin 
161316c2cd71SAl Viro 	if (!retval)
161416c2cd71SAl Viro 		retval = handle_reval_path(nd);
161516c2cd71SAl Viro 
161670e9b357SAl Viro 	if (base)
161770e9b357SAl Viro 		fput(base);
1618ee0827cdSAl Viro 
16195b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
162031e6b01fSNick Piggin 		path_put(&nd->root);
162131e6b01fSNick Piggin 		nd->root.mnt = NULL;
162231e6b01fSNick Piggin 	}
1623ee0827cdSAl Viro 	return retval;
162431e6b01fSNick Piggin }
162531e6b01fSNick Piggin 
1626ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1627ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1628ee0827cdSAl Viro {
1629ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1630ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1631ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1632ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1633ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1634ee0827cdSAl Viro 
163531e6b01fSNick Piggin 	if (likely(!retval)) {
163631e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
163731e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
163831e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
163931e6b01fSNick Piggin 		}
164031e6b01fSNick Piggin 	}
1641170aa3d0SUlrich Drepper 	return retval;
16421da177e4SLinus Torvalds }
16431da177e4SLinus Torvalds 
1644c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd)
16455590ff0dSUlrich Drepper {
1646c9c6cac0SAl Viro 	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
16475590ff0dSUlrich Drepper }
16485590ff0dSUlrich Drepper 
1649d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1650d1811465SAl Viro {
1651d1811465SAl Viro 	struct nameidata nd;
1652d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1653d1811465SAl Viro 	if (!res)
1654d1811465SAl Viro 		*path = nd.path;
1655d1811465SAl Viro 	return res;
1656d1811465SAl Viro }
1657d1811465SAl Viro 
165816f18200SJosef 'Jeff' Sipek /**
165916f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
166016f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
166116f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
166216f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
166316f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
166416f18200SJosef 'Jeff' Sipek  * @nd: pointer to nameidata
166516f18200SJosef 'Jeff' Sipek  */
166616f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
166716f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
166816f18200SJosef 'Jeff' Sipek 		    struct nameidata *nd)
166916f18200SJosef 'Jeff' Sipek {
16705b6ca027SAl Viro 	nd->root.dentry = dentry;
16715b6ca027SAl Viro 	nd->root.mnt = mnt;
16725b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
16735b6ca027SAl Viro 	return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
167416f18200SJosef 'Jeff' Sipek }
167516f18200SJosef 'Jeff' Sipek 
1676eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1677eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
16781da177e4SLinus Torvalds {
167981fca444SChristoph Hellwig 	struct inode *inode = base->d_inode;
16801da177e4SLinus Torvalds 	struct dentry *dentry;
16811da177e4SLinus Torvalds 	int err;
16821da177e4SLinus Torvalds 
1683b74c79e9SNick Piggin 	err = exec_permission(inode, 0);
168481fca444SChristoph Hellwig 	if (err)
168581fca444SChristoph Hellwig 		return ERR_PTR(err);
16861da177e4SLinus Torvalds 
16871da177e4SLinus Torvalds 	/*
1688b04f784eSNick Piggin 	 * Don't bother with __d_lookup: callers are for creat as
1689b04f784eSNick Piggin 	 * well as unlink, so a lot of the time it would cost
1690b04f784eSNick Piggin 	 * a double lookup.
16916e6b1bd1SAl Viro 	 */
16926e6b1bd1SAl Viro 	dentry = d_lookup(base, name);
16936e6b1bd1SAl Viro 
1694fb045adbSNick Piggin 	if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
16956e6b1bd1SAl Viro 		dentry = do_revalidate(dentry, nd);
16966e6b1bd1SAl Viro 
16971da177e4SLinus Torvalds 	if (!dentry)
1698baa03890SNick Piggin 		dentry = d_alloc_and_lookup(base, name, nd);
16995a202bcdSAl Viro 
17001da177e4SLinus Torvalds 	return dentry;
17011da177e4SLinus Torvalds }
17021da177e4SLinus Torvalds 
1703057f6c01SJames Morris /*
1704057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1705057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1706057f6c01SJames Morris  * SMP-safe.
1707057f6c01SJames Morris  */
1708a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
17091da177e4SLinus Torvalds {
17104ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
17111da177e4SLinus Torvalds }
17121da177e4SLinus Torvalds 
1713eead1911SChristoph Hellwig /**
1714a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1715eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1716eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1717eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1718eead1911SChristoph Hellwig  *
1719a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1720a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1721eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1722eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1723eead1911SChristoph Hellwig  */
1724057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1725057f6c01SJames Morris {
1726057f6c01SJames Morris 	struct qstr this;
17276a96ba54SAl Viro 	unsigned long hash;
17286a96ba54SAl Viro 	unsigned int c;
1729057f6c01SJames Morris 
17302f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
17312f9092e1SDavid Woodhouse 
17326a96ba54SAl Viro 	this.name = name;
17336a96ba54SAl Viro 	this.len = len;
17346a96ba54SAl Viro 	if (!len)
17356a96ba54SAl Viro 		return ERR_PTR(-EACCES);
17366a96ba54SAl Viro 
17376a96ba54SAl Viro 	hash = init_name_hash();
17386a96ba54SAl Viro 	while (len--) {
17396a96ba54SAl Viro 		c = *(const unsigned char *)name++;
17406a96ba54SAl Viro 		if (c == '/' || c == '\0')
17416a96ba54SAl Viro 			return ERR_PTR(-EACCES);
17426a96ba54SAl Viro 		hash = partial_name_hash(c, hash);
17436a96ba54SAl Viro 	}
17446a96ba54SAl Viro 	this.hash = end_name_hash(hash);
17455a202bcdSAl Viro 	/*
17465a202bcdSAl Viro 	 * See if the low-level filesystem might want
17475a202bcdSAl Viro 	 * to use its own hash..
17485a202bcdSAl Viro 	 */
17495a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
17505a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
17515a202bcdSAl Viro 		if (err < 0)
17525a202bcdSAl Viro 			return ERR_PTR(err);
17535a202bcdSAl Viro 	}
1754eead1911SChristoph Hellwig 
175549705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1756057f6c01SJames Morris }
1757057f6c01SJames Morris 
17582d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
17592d8f3038SAl Viro 		 struct path *path)
17601da177e4SLinus Torvalds {
17612d8f3038SAl Viro 	struct nameidata nd;
17621da177e4SLinus Torvalds 	char *tmp = getname(name);
17631da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
17641da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
17652d8f3038SAl Viro 
17662d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
17672d8f3038SAl Viro 
17682d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
17691da177e4SLinus Torvalds 		putname(tmp);
17702d8f3038SAl Viro 		if (!err)
17712d8f3038SAl Viro 			*path = nd.path;
17721da177e4SLinus Torvalds 	}
17731da177e4SLinus Torvalds 	return err;
17741da177e4SLinus Torvalds }
17751da177e4SLinus Torvalds 
17762ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
17772ad94ae6SAl Viro 			struct nameidata *nd, char **name)
17782ad94ae6SAl Viro {
17792ad94ae6SAl Viro 	char *s = getname(path);
17802ad94ae6SAl Viro 	int error;
17812ad94ae6SAl Viro 
17822ad94ae6SAl Viro 	if (IS_ERR(s))
17832ad94ae6SAl Viro 		return PTR_ERR(s);
17842ad94ae6SAl Viro 
17852ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
17862ad94ae6SAl Viro 	if (error)
17872ad94ae6SAl Viro 		putname(s);
17882ad94ae6SAl Viro 	else
17892ad94ae6SAl Viro 		*name = s;
17902ad94ae6SAl Viro 
17912ad94ae6SAl Viro 	return error;
17922ad94ae6SAl Viro }
17932ad94ae6SAl Viro 
17941da177e4SLinus Torvalds /*
17951da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
17961da177e4SLinus Torvalds  * minimal.
17971da177e4SLinus Torvalds  */
17981da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
17991da177e4SLinus Torvalds {
1800da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1801da9592edSDavid Howells 
18021da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
18031da177e4SLinus Torvalds 		return 0;
1804da9592edSDavid Howells 	if (inode->i_uid == fsuid)
18051da177e4SLinus Torvalds 		return 0;
1806da9592edSDavid Howells 	if (dir->i_uid == fsuid)
18071da177e4SLinus Torvalds 		return 0;
18081da177e4SLinus Torvalds 	return !capable(CAP_FOWNER);
18091da177e4SLinus Torvalds }
18101da177e4SLinus Torvalds 
18111da177e4SLinus Torvalds /*
18121da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
18131da177e4SLinus Torvalds  *  whether the type of victim is right.
18141da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
18151da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
18161da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
18171da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
18181da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
18191da177e4SLinus Torvalds  *	a. be owner of dir, or
18201da177e4SLinus Torvalds  *	b. be owner of victim, or
18211da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
18221da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
18231da177e4SLinus Torvalds  *     links pointing to it.
18241da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
18251da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
18261da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
18271da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
18281da177e4SLinus Torvalds  *     nfs_async_unlink().
18291da177e4SLinus Torvalds  */
1830858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
18311da177e4SLinus Torvalds {
18321da177e4SLinus Torvalds 	int error;
18331da177e4SLinus Torvalds 
18341da177e4SLinus Torvalds 	if (!victim->d_inode)
18351da177e4SLinus Torvalds 		return -ENOENT;
18361da177e4SLinus Torvalds 
18371da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1838cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
18391da177e4SLinus Torvalds 
1840f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
18411da177e4SLinus Torvalds 	if (error)
18421da177e4SLinus Torvalds 		return error;
18431da177e4SLinus Torvalds 	if (IS_APPEND(dir))
18441da177e4SLinus Torvalds 		return -EPERM;
18451da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1846f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
18471da177e4SLinus Torvalds 		return -EPERM;
18481da177e4SLinus Torvalds 	if (isdir) {
18491da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
18501da177e4SLinus Torvalds 			return -ENOTDIR;
18511da177e4SLinus Torvalds 		if (IS_ROOT(victim))
18521da177e4SLinus Torvalds 			return -EBUSY;
18531da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
18541da177e4SLinus Torvalds 		return -EISDIR;
18551da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18561da177e4SLinus Torvalds 		return -ENOENT;
18571da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
18581da177e4SLinus Torvalds 		return -EBUSY;
18591da177e4SLinus Torvalds 	return 0;
18601da177e4SLinus Torvalds }
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
18631da177e4SLinus Torvalds  *  dir.
18641da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
18651da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
18661da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
18671da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
18681da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
18691da177e4SLinus Torvalds  */
1870a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
18711da177e4SLinus Torvalds {
18721da177e4SLinus Torvalds 	if (child->d_inode)
18731da177e4SLinus Torvalds 		return -EEXIST;
18741da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18751da177e4SLinus Torvalds 		return -ENOENT;
1876f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
18771da177e4SLinus Torvalds }
18781da177e4SLinus Torvalds 
18791da177e4SLinus Torvalds /*
18801da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
18811da177e4SLinus Torvalds  */
18821da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
18831da177e4SLinus Torvalds {
18841da177e4SLinus Torvalds 	struct dentry *p;
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds 	if (p1 == p2) {
1887f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
18881da177e4SLinus Torvalds 		return NULL;
18891da177e4SLinus Torvalds 	}
18901da177e4SLinus Torvalds 
1891a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
18921da177e4SLinus Torvalds 
1893e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1894e2761a11SOGAWA Hirofumi 	if (p) {
1895f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1896f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
18971da177e4SLinus Torvalds 		return p;
18981da177e4SLinus Torvalds 	}
18991da177e4SLinus Torvalds 
1900e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1901e2761a11SOGAWA Hirofumi 	if (p) {
1902f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1903f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19041da177e4SLinus Torvalds 		return p;
19051da177e4SLinus Torvalds 	}
19061da177e4SLinus Torvalds 
1907f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1908f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19091da177e4SLinus Torvalds 	return NULL;
19101da177e4SLinus Torvalds }
19111da177e4SLinus Torvalds 
19121da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
19131da177e4SLinus Torvalds {
19141b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
19151da177e4SLinus Torvalds 	if (p1 != p2) {
19161b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1917a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19181da177e4SLinus Torvalds 	}
19191da177e4SLinus Torvalds }
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
19221da177e4SLinus Torvalds 		struct nameidata *nd)
19231da177e4SLinus Torvalds {
1924a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 	if (error)
19271da177e4SLinus Torvalds 		return error;
19281da177e4SLinus Torvalds 
1929acfa4380SAl Viro 	if (!dir->i_op->create)
19301da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
19311da177e4SLinus Torvalds 	mode &= S_IALLUGO;
19321da177e4SLinus Torvalds 	mode |= S_IFREG;
19331da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
19341da177e4SLinus Torvalds 	if (error)
19351da177e4SLinus Torvalds 		return error;
19361da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1937a74574aaSStephen Smalley 	if (!error)
1938f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
19391da177e4SLinus Torvalds 	return error;
19401da177e4SLinus Torvalds }
19411da177e4SLinus Torvalds 
194273d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
19431da177e4SLinus Torvalds {
19443fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
19451da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
19461da177e4SLinus Torvalds 	int error;
19471da177e4SLinus Torvalds 
19481da177e4SLinus Torvalds 	if (!inode)
19491da177e4SLinus Torvalds 		return -ENOENT;
19501da177e4SLinus Torvalds 
1951c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1952c8fe8f30SChristoph Hellwig 	case S_IFLNK:
19531da177e4SLinus Torvalds 		return -ELOOP;
1954c8fe8f30SChristoph Hellwig 	case S_IFDIR:
1955c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
19561da177e4SLinus Torvalds 			return -EISDIR;
1957c8fe8f30SChristoph Hellwig 		break;
1958c8fe8f30SChristoph Hellwig 	case S_IFBLK:
1959c8fe8f30SChristoph Hellwig 	case S_IFCHR:
19603fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
19611da177e4SLinus Torvalds 			return -EACCES;
1962c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
1963c8fe8f30SChristoph Hellwig 	case S_IFIFO:
1964c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
19651da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
1966c8fe8f30SChristoph Hellwig 		break;
19674a3fd211SDave Hansen 	}
1968b41572e9SDave Hansen 
19693fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
1970b41572e9SDave Hansen 	if (error)
1971b41572e9SDave Hansen 		return error;
19726146f0d5SMimi Zohar 
19731da177e4SLinus Torvalds 	/*
19741da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
19751da177e4SLinus Torvalds 	 */
19761da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
19778737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
19787715b521SAl Viro 			return -EPERM;
19791da177e4SLinus Torvalds 		if (flag & O_TRUNC)
19807715b521SAl Viro 			return -EPERM;
19811da177e4SLinus Torvalds 	}
19821da177e4SLinus Torvalds 
19831da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
19847715b521SAl Viro 	if (flag & O_NOATIME && !is_owner_or_cap(inode))
19857715b521SAl Viro 		return -EPERM;
19861da177e4SLinus Torvalds 
19871da177e4SLinus Torvalds 	/*
19881da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
19891da177e4SLinus Torvalds 	 */
1990b65a9cfcSAl Viro 	return break_lease(inode, flag);
19917715b521SAl Viro }
19927715b521SAl Viro 
1993e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
19947715b521SAl Viro {
1995e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
19967715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
19977715b521SAl Viro 	int error = get_write_access(inode);
19981da177e4SLinus Torvalds 	if (error)
19997715b521SAl Viro 		return error;
20001da177e4SLinus Torvalds 	/*
20011da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
20021da177e4SLinus Torvalds 	 */
20031da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2004be6d3e56SKentaro Takeda 	if (!error)
2005ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
20061da177e4SLinus Torvalds 	if (!error) {
20077715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2008d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2009e1181ee6SJeff Layton 				    filp);
20101da177e4SLinus Torvalds 	}
20111da177e4SLinus Torvalds 	put_write_access(inode);
2012acd0c935SMimi Zohar 	return error;
20131da177e4SLinus Torvalds }
20141da177e4SLinus Torvalds 
2015d57999e1SDave Hansen /*
2016d57999e1SDave Hansen  * Note that while the flag value (low two bits) for sys_open means:
2017d57999e1SDave Hansen  *	00 - read-only
2018d57999e1SDave Hansen  *	01 - write-only
2019d57999e1SDave Hansen  *	10 - read-write
2020d57999e1SDave Hansen  *	11 - special
2021d57999e1SDave Hansen  * it is changed into
2022d57999e1SDave Hansen  *	00 - no permissions needed
2023d57999e1SDave Hansen  *	01 - read-permission
2024d57999e1SDave Hansen  *	10 - write-permission
2025d57999e1SDave Hansen  *	11 - read-write
2026d57999e1SDave Hansen  * for the internal routines (ie open_namei()/follow_link() etc)
2027d57999e1SDave Hansen  * This is more logical, and also allows the 00 "no perm needed"
2028d57999e1SDave Hansen  * to be used for symlinks (where the permissions are checked
2029d57999e1SDave Hansen  * later).
2030d57999e1SDave Hansen  *
2031d57999e1SDave Hansen */
2032d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2033d57999e1SDave Hansen {
2034d57999e1SDave Hansen 	if ((flag+1) & O_ACCMODE)
2035d57999e1SDave Hansen 		flag++;
2036d57999e1SDave Hansen 	return flag;
2037d57999e1SDave Hansen }
2038d57999e1SDave Hansen 
203931e6b01fSNick Piggin /*
2040fe2d35ffSAl Viro  * Handle the last step of open()
204131e6b01fSNick Piggin  */
2042fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
2043c3e380b0SAl Viro 			    const struct open_flags *op, const char *pathname)
2044fb1cc555SAl Viro {
2045a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
20466c0d46c4SAl Viro 	struct dentry *dentry;
2047ca344a89SAl Viro 	int open_flag = op->open_flag;
20486c0d46c4SAl Viro 	int will_truncate = open_flag & O_TRUNC;
2049ca344a89SAl Viro 	int want_write = 0;
2050ca344a89SAl Viro 	int skip_perm = 0;
2051fb1cc555SAl Viro 	struct file *filp;
2052fe2d35ffSAl Viro 	struct inode *inode;
205316c2cd71SAl Viro 	int error;
2054fb1cc555SAl Viro 
2055c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2056c3e380b0SAl Viro 	nd->flags |= op->intent;
2057c3e380b0SAl Viro 
20581f36f774SAl Viro 	switch (nd->last_type) {
20591f36f774SAl Viro 	case LAST_DOTDOT:
2060176306f5SNeil Brown 	case LAST_DOT:
2061fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2062fe2d35ffSAl Viro 		if (error)
2063fe2d35ffSAl Viro 			return ERR_PTR(error);
20641f36f774SAl Viro 		/* fallthrough */
20651f36f774SAl Viro 	case LAST_ROOT:
2066fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_RCU) {
2067fe2d35ffSAl Viro 			if (nameidata_drop_rcu_last(nd))
2068fe2d35ffSAl Viro 				return ERR_PTR(-ECHILD);
2069fe2d35ffSAl Viro 		}
207016c2cd71SAl Viro 		error = handle_reval_path(nd);
207116c2cd71SAl Viro 		if (error)
207216c2cd71SAl Viro 			goto exit;
2073fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2074ca344a89SAl Viro 		if (open_flag & O_CREAT) {
207516c2cd71SAl Viro 			error = -EISDIR;
20761f36f774SAl Viro 			goto exit;
2077fe2d35ffSAl Viro 		}
2078fe2d35ffSAl Viro 		goto ok;
20791f36f774SAl Viro 	case LAST_BIND:
2080fe2d35ffSAl Viro 		/* can't be RCU mode here */
208116c2cd71SAl Viro 		error = handle_reval_path(nd);
208216c2cd71SAl Viro 		if (error)
208316c2cd71SAl Viro 			goto exit;
20841f36f774SAl Viro 		audit_inode(pathname, dir);
20851f36f774SAl Viro 		goto ok;
20861f36f774SAl Viro 	}
2087a2c36b45SAl Viro 
2088ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2089fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2090fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2091fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2092fe2d35ffSAl Viro 		error = do_lookup(nd, &nd->last, path, &inode);
2093fe2d35ffSAl Viro 		if (error) {
2094fe2d35ffSAl Viro 			terminate_walk(nd);
2095fe2d35ffSAl Viro 			return ERR_PTR(error);
2096fe2d35ffSAl Viro 		}
2097fe2d35ffSAl Viro 		if (!inode) {
2098fe2d35ffSAl Viro 			path_to_nameidata(path, nd);
2099fe2d35ffSAl Viro 			terminate_walk(nd);
2100fe2d35ffSAl Viro 			return ERR_PTR(-ENOENT);
2101fe2d35ffSAl Viro 		}
2102fe2d35ffSAl Viro 		if (unlikely(inode->i_op->follow_link)) {
2103fe2d35ffSAl Viro 			/* We drop rcu-walk here */
2104fe2d35ffSAl Viro 			if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry))
2105fe2d35ffSAl Viro 				return ERR_PTR(-ECHILD);
2106fe2d35ffSAl Viro 			return NULL;
2107fe2d35ffSAl Viro 		}
2108fe2d35ffSAl Viro 		path_to_nameidata(path, nd);
2109fe2d35ffSAl Viro 		nd->inode = inode;
2110fe2d35ffSAl Viro 		/* sayonara */
2111fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_RCU) {
2112fe2d35ffSAl Viro 			if (nameidata_drop_rcu_last(nd))
2113fe2d35ffSAl Viro 				return ERR_PTR(-ECHILD);
2114fe2d35ffSAl Viro 		}
2115fe2d35ffSAl Viro 
2116fe2d35ffSAl Viro 		error = -ENOTDIR;
2117fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_DIRECTORY) {
2118fe2d35ffSAl Viro 			if (!inode->i_op->lookup)
2119fe2d35ffSAl Viro 				goto exit;
2120fe2d35ffSAl Viro 		}
2121fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2122fe2d35ffSAl Viro 		goto ok;
2123fe2d35ffSAl Viro 	}
2124fe2d35ffSAl Viro 
2125fe2d35ffSAl Viro 	/* create side of things */
2126fe2d35ffSAl Viro 
2127fe2d35ffSAl Viro 	if (nd->flags & LOOKUP_RCU) {
2128fe2d35ffSAl Viro 		if (nameidata_drop_rcu_last(nd))
2129fe2d35ffSAl Viro 			return ERR_PTR(-ECHILD);
2130fe2d35ffSAl Viro 	}
2131fe2d35ffSAl Viro 
2132fe2d35ffSAl Viro 	audit_inode(pathname, dir);
213316c2cd71SAl Viro 	error = -EISDIR;
21341f36f774SAl Viro 	/* trailing slashes? */
213531e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
21361f36f774SAl Viro 		goto exit;
21371f36f774SAl Viro 
2138a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2139a1e28038SAl Viro 
21406c0d46c4SAl Viro 	dentry = lookup_hash(nd);
21416c0d46c4SAl Viro 	error = PTR_ERR(dentry);
21426c0d46c4SAl Viro 	if (IS_ERR(dentry)) {
2143fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2144fb1cc555SAl Viro 		goto exit;
2145fb1cc555SAl Viro 	}
2146fb1cc555SAl Viro 
21476c0d46c4SAl Viro 	path->dentry = dentry;
21486c0d46c4SAl Viro 	path->mnt = nd->path.mnt;
21496c0d46c4SAl Viro 
2150fb1cc555SAl Viro 	/* Negative dentry, just create the file */
21516c0d46c4SAl Viro 	if (!dentry->d_inode) {
21526c0d46c4SAl Viro 		int mode = op->mode;
21536c0d46c4SAl Viro 		if (!IS_POSIXACL(dir->d_inode))
21546c0d46c4SAl Viro 			mode &= ~current_umask();
2155fb1cc555SAl Viro 		/*
2156fb1cc555SAl Viro 		 * This write is needed to ensure that a
21576c0d46c4SAl Viro 		 * rw->ro transition does not occur between
2158fb1cc555SAl Viro 		 * the time when the file is created and when
2159fb1cc555SAl Viro 		 * a permanent write count is taken through
2160fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2161fb1cc555SAl Viro 		 */
2162fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2163fb1cc555SAl Viro 		if (error)
2164fb1cc555SAl Viro 			goto exit_mutex_unlock;
2165ca344a89SAl Viro 		want_write = 1;
21669b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2167ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
21686c0d46c4SAl Viro 		will_truncate = 0;
2169ca344a89SAl Viro 		skip_perm = 1;
21706c0d46c4SAl Viro 		error = security_path_mknod(&nd->path, dentry, mode, 0);
21716c0d46c4SAl Viro 		if (error)
21726c0d46c4SAl Viro 			goto exit_mutex_unlock;
21736c0d46c4SAl Viro 		error = vfs_create(dir->d_inode, dentry, mode, nd);
21746c0d46c4SAl Viro 		if (error)
21756c0d46c4SAl Viro 			goto exit_mutex_unlock;
21766c0d46c4SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
21776c0d46c4SAl Viro 		dput(nd->path.dentry);
21786c0d46c4SAl Viro 		nd->path.dentry = dentry;
2179ca344a89SAl Viro 		goto common;
2180fb1cc555SAl Viro 	}
2181fb1cc555SAl Viro 
2182fb1cc555SAl Viro 	/*
2183fb1cc555SAl Viro 	 * It already exists.
2184fb1cc555SAl Viro 	 */
2185fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2186fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2187fb1cc555SAl Viro 
2188fb1cc555SAl Viro 	error = -EEXIST;
2189ca344a89SAl Viro 	if (open_flag & O_EXCL)
2190fb1cc555SAl Viro 		goto exit_dput;
2191fb1cc555SAl Viro 
21929875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
21939875cf80SDavid Howells 	if (error < 0)
2194fb1cc555SAl Viro 		goto exit_dput;
2195fb1cc555SAl Viro 
2196fb1cc555SAl Viro 	error = -ENOENT;
2197fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2198fb1cc555SAl Viro 		goto exit_dput;
21999e67f361SAl Viro 
22009e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2201fb1cc555SAl Viro 		return NULL;
2202fb1cc555SAl Viro 
2203fb1cc555SAl Viro 	path_to_nameidata(path, nd);
220431e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2205fb1cc555SAl Viro 	error = -EISDIR;
220631e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2207fb1cc555SAl Viro 		goto exit;
220867ee3ad2SAl Viro ok:
22096c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
22106c0d46c4SAl Viro 		will_truncate = 0;
22116c0d46c4SAl Viro 
22120f9d1a10SAl Viro 	if (will_truncate) {
22130f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
22140f9d1a10SAl Viro 		if (error)
22150f9d1a10SAl Viro 			goto exit;
2216ca344a89SAl Viro 		want_write = 1;
22170f9d1a10SAl Viro 	}
2218ca344a89SAl Viro common:
2219ca344a89SAl Viro 	error = may_open(&nd->path, skip_perm ? 0 : op->acc_mode, open_flag);
2220ca344a89SAl Viro 	if (error)
22210f9d1a10SAl Viro 		goto exit;
22220f9d1a10SAl Viro 	filp = nameidata_to_filp(nd);
22230f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
22240f9d1a10SAl Viro 		error = ima_file_check(filp, op->acc_mode);
22250f9d1a10SAl Viro 		if (error) {
22260f9d1a10SAl Viro 			fput(filp);
22270f9d1a10SAl Viro 			filp = ERR_PTR(error);
22280f9d1a10SAl Viro 		}
22290f9d1a10SAl Viro 	}
22300f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
22310f9d1a10SAl Viro 		if (will_truncate) {
22320f9d1a10SAl Viro 			error = handle_truncate(filp);
22330f9d1a10SAl Viro 			if (error) {
22340f9d1a10SAl Viro 				fput(filp);
22350f9d1a10SAl Viro 				filp = ERR_PTR(error);
22360f9d1a10SAl Viro 			}
22370f9d1a10SAl Viro 		}
22380f9d1a10SAl Viro 	}
2239ca344a89SAl Viro out:
2240ca344a89SAl Viro 	if (want_write)
22410f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
22420f9d1a10SAl Viro 	path_put(&nd->path);
2243fb1cc555SAl Viro 	return filp;
2244fb1cc555SAl Viro 
2245fb1cc555SAl Viro exit_mutex_unlock:
2246fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2247fb1cc555SAl Viro exit_dput:
2248fb1cc555SAl Viro 	path_put_conditional(path, nd);
2249fb1cc555SAl Viro exit:
2250ca344a89SAl Viro 	filp = ERR_PTR(error);
2251ca344a89SAl Viro 	goto out;
2252fb1cc555SAl Viro }
2253fb1cc555SAl Viro 
225413aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
225573d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
22561da177e4SLinus Torvalds {
2257fe2d35ffSAl Viro 	struct file *base = NULL;
22584a3fd211SDave Hansen 	struct file *filp;
22599850c056SAl Viro 	struct path path;
22601da177e4SLinus Torvalds 	int count = 0;
226113aab428SAl Viro 	int error;
226231e6b01fSNick Piggin 
226331e6b01fSNick Piggin 	filp = get_empty_filp();
226431e6b01fSNick Piggin 	if (!filp)
226531e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
226631e6b01fSNick Piggin 
226747c805dcSAl Viro 	filp->f_flags = op->open_flag;
226873d049a4SAl Viro 	nd->intent.open.file = filp;
226973d049a4SAl Viro 	nd->intent.open.flags = open_to_namei_flags(op->open_flag);
227073d049a4SAl Viro 	nd->intent.open.create_mode = op->mode;
227131e6b01fSNick Piggin 
227273d049a4SAl Viro 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
227331e6b01fSNick Piggin 	if (unlikely(error))
227413aab428SAl Viro 		goto out_filp;
227531e6b01fSNick Piggin 
2276fe2d35ffSAl Viro 	current->total_link_count = 0;
227773d049a4SAl Viro 	error = link_path_walk(pathname, nd);
227831e6b01fSNick Piggin 	if (unlikely(error))
227931e6b01fSNick Piggin 		goto out_filp;
22801da177e4SLinus Torvalds 
228173d049a4SAl Viro 	filp = do_last(nd, &path, op, pathname);
2282806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
22837b9337aaSNick Piggin 		struct path link = path;
22847b9337aaSNick Piggin 		struct inode *linki = link.dentry->d_inode;
2285def4af30SAl Viro 		void *cookie;
228673d049a4SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW) || count++ == 32) {
228773d049a4SAl Viro 			path_put_conditional(&path, nd);
228873d049a4SAl Viro 			path_put(&nd->path);
228940b39136SAl Viro 			filp = ERR_PTR(-ELOOP);
229040b39136SAl Viro 			break;
229140b39136SAl Viro 		}
2292806b681cSAl Viro 		/*
2293806b681cSAl Viro 		 * This is subtle. Instead of calling do_follow_link() we do
2294806b681cSAl Viro 		 * the thing by hands. The reason is that this way we have zero
2295806b681cSAl Viro 		 * link_count and path_walk() (called from ->follow_link)
2296806b681cSAl Viro 		 * honoring LOOKUP_PARENT.  After that we have the parent and
2297806b681cSAl Viro 		 * last component, i.e. we are in the same situation as after
2298806b681cSAl Viro 		 * the first path_walk().  Well, almost - if the last component
2299806b681cSAl Viro 		 * is normal we get its copy stored in nd->last.name and we will
2300806b681cSAl Viro 		 * have to putname() it when we are done. Procfs-like symlinks
2301806b681cSAl Viro 		 * just set LAST_BIND.
2302806b681cSAl Viro 		 */
230373d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
230473d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
230573d049a4SAl Viro 		error = __do_follow_link(&link, nd, &cookie);
2306c3e380b0SAl Viro 		if (unlikely(error))
2307f1afe9efSAl Viro 			filp = ERR_PTR(error);
2308c3e380b0SAl Viro 		else
230973d049a4SAl Viro 			filp = do_last(nd, &path, op, pathname);
2310f1afe9efSAl Viro 		if (!IS_ERR(cookie) && linki->i_op->put_link)
231173d049a4SAl Viro 			linki->i_op->put_link(link.dentry, nd, cookie);
23127b9337aaSNick Piggin 		path_put(&link);
2313806b681cSAl Viro 	}
231410fa8e62SAl Viro out:
231573d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
231673d049a4SAl Viro 		path_put(&nd->root);
2317fe2d35ffSAl Viro 	if (base)
2318fe2d35ffSAl Viro 		fput(base);
231973d049a4SAl Viro 	release_open_intent(nd);
232010fa8e62SAl Viro 	return filp;
23211da177e4SLinus Torvalds 
232231e6b01fSNick Piggin out_filp:
232310fa8e62SAl Viro 	filp = ERR_PTR(error);
232410fa8e62SAl Viro 	goto out;
2325de459215SKirill Korotaev }
23261da177e4SLinus Torvalds 
232713aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
232813aab428SAl Viro 		const struct open_flags *op, int flags)
232913aab428SAl Viro {
233073d049a4SAl Viro 	struct nameidata nd;
233113aab428SAl Viro 	struct file *filp;
233213aab428SAl Viro 
233373d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
233413aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
233573d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
233613aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
233773d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
233813aab428SAl Viro 	return filp;
233913aab428SAl Viro }
234013aab428SAl Viro 
234173d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
234273d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
234373d049a4SAl Viro {
234473d049a4SAl Viro 	struct nameidata nd;
234573d049a4SAl Viro 	struct file *file;
234673d049a4SAl Viro 
234773d049a4SAl Viro 	nd.root.mnt = mnt;
234873d049a4SAl Viro 	nd.root.dentry = dentry;
234973d049a4SAl Viro 
235073d049a4SAl Viro 	flags |= LOOKUP_ROOT;
235173d049a4SAl Viro 
235273d049a4SAl Viro 	if (dentry->d_inode->i_op->follow_link)
235373d049a4SAl Viro 		return ERR_PTR(-ELOOP);
235473d049a4SAl Viro 
235573d049a4SAl Viro 	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
235673d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
235773d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags);
235873d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
235973d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
236073d049a4SAl Viro 	return file;
236173d049a4SAl Viro }
236273d049a4SAl Viro 
23631da177e4SLinus Torvalds /**
23641da177e4SLinus Torvalds  * lookup_create - lookup a dentry, creating it if it doesn't exist
23651da177e4SLinus Torvalds  * @nd: nameidata info
23661da177e4SLinus Torvalds  * @is_dir: directory flag
23671da177e4SLinus Torvalds  *
23681da177e4SLinus Torvalds  * Simple function to lookup and return a dentry and create it
23691da177e4SLinus Torvalds  * if it doesn't exist.  Is SMP-safe.
2370c663e5d8SChristoph Hellwig  *
23714ac91378SJan Blunck  * Returns with nd->path.dentry->d_inode->i_mutex locked.
23721da177e4SLinus Torvalds  */
23731da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir)
23741da177e4SLinus Torvalds {
2375c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
23761da177e4SLinus Torvalds 
23774ac91378SJan Blunck 	mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2378c663e5d8SChristoph Hellwig 	/*
2379c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2380c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2381c663e5d8SChristoph Hellwig 	 */
23821da177e4SLinus Torvalds 	if (nd->last_type != LAST_NORM)
23831da177e4SLinus Torvalds 		goto fail;
23841da177e4SLinus Torvalds 	nd->flags &= ~LOOKUP_PARENT;
23853516586aSAl Viro 	nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2386a634904aSASANO Masahiro 	nd->intent.open.flags = O_EXCL;
2387c663e5d8SChristoph Hellwig 
2388c663e5d8SChristoph Hellwig 	/*
2389c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2390c663e5d8SChristoph Hellwig 	 */
239149705b77SChristoph Hellwig 	dentry = lookup_hash(nd);
23921da177e4SLinus Torvalds 	if (IS_ERR(dentry))
23931da177e4SLinus Torvalds 		goto fail;
2394c663e5d8SChristoph Hellwig 
2395e9baf6e5SAl Viro 	if (dentry->d_inode)
2396e9baf6e5SAl Viro 		goto eexist;
2397c663e5d8SChristoph Hellwig 	/*
2398c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2399c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2400c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2401c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2402c663e5d8SChristoph Hellwig 	 */
2403e9baf6e5SAl Viro 	if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
24041da177e4SLinus Torvalds 		dput(dentry);
24051da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2406e9baf6e5SAl Viro 	}
2407e9baf6e5SAl Viro 	return dentry;
2408e9baf6e5SAl Viro eexist:
2409e9baf6e5SAl Viro 	dput(dentry);
2410e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
24111da177e4SLinus Torvalds fail:
24121da177e4SLinus Torvalds 	return dentry;
24131da177e4SLinus Torvalds }
2414f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create);
24151da177e4SLinus Torvalds 
24161da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
24171da177e4SLinus Torvalds {
2418a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
24191da177e4SLinus Torvalds 
24201da177e4SLinus Torvalds 	if (error)
24211da177e4SLinus Torvalds 		return error;
24221da177e4SLinus Torvalds 
24231da177e4SLinus Torvalds 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
24241da177e4SLinus Torvalds 		return -EPERM;
24251da177e4SLinus Torvalds 
2426acfa4380SAl Viro 	if (!dir->i_op->mknod)
24271da177e4SLinus Torvalds 		return -EPERM;
24281da177e4SLinus Torvalds 
242908ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
243008ce5f16SSerge E. Hallyn 	if (error)
243108ce5f16SSerge E. Hallyn 		return error;
243208ce5f16SSerge E. Hallyn 
24331da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
24341da177e4SLinus Torvalds 	if (error)
24351da177e4SLinus Torvalds 		return error;
24361da177e4SLinus Torvalds 
24371da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2438a74574aaSStephen Smalley 	if (!error)
2439f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
24401da177e4SLinus Torvalds 	return error;
24411da177e4SLinus Torvalds }
24421da177e4SLinus Torvalds 
2443463c3197SDave Hansen static int may_mknod(mode_t mode)
2444463c3197SDave Hansen {
2445463c3197SDave Hansen 	switch (mode & S_IFMT) {
2446463c3197SDave Hansen 	case S_IFREG:
2447463c3197SDave Hansen 	case S_IFCHR:
2448463c3197SDave Hansen 	case S_IFBLK:
2449463c3197SDave Hansen 	case S_IFIFO:
2450463c3197SDave Hansen 	case S_IFSOCK:
2451463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2452463c3197SDave Hansen 		return 0;
2453463c3197SDave Hansen 	case S_IFDIR:
2454463c3197SDave Hansen 		return -EPERM;
2455463c3197SDave Hansen 	default:
2456463c3197SDave Hansen 		return -EINVAL;
2457463c3197SDave Hansen 	}
2458463c3197SDave Hansen }
2459463c3197SDave Hansen 
24602e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
24612e4d0924SHeiko Carstens 		unsigned, dev)
24621da177e4SLinus Torvalds {
24632ad94ae6SAl Viro 	int error;
24641da177e4SLinus Torvalds 	char *tmp;
24651da177e4SLinus Torvalds 	struct dentry *dentry;
24661da177e4SLinus Torvalds 	struct nameidata nd;
24671da177e4SLinus Torvalds 
24681da177e4SLinus Torvalds 	if (S_ISDIR(mode))
24691da177e4SLinus Torvalds 		return -EPERM;
24701da177e4SLinus Torvalds 
24712ad94ae6SAl Viro 	error = user_path_parent(dfd, filename, &nd, &tmp);
24721da177e4SLinus Torvalds 	if (error)
24732ad94ae6SAl Viro 		return error;
24742ad94ae6SAl Viro 
24751da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
2476463c3197SDave Hansen 	if (IS_ERR(dentry)) {
24771da177e4SLinus Torvalds 		error = PTR_ERR(dentry);
2478463c3197SDave Hansen 		goto out_unlock;
2479463c3197SDave Hansen 	}
24804ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2481ce3b0f8dSAl Viro 		mode &= ~current_umask();
2482463c3197SDave Hansen 	error = may_mknod(mode);
2483463c3197SDave Hansen 	if (error)
2484463c3197SDave Hansen 		goto out_dput;
2485463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2486463c3197SDave Hansen 	if (error)
2487463c3197SDave Hansen 		goto out_dput;
2488be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd.path, dentry, mode, dev);
2489be6d3e56SKentaro Takeda 	if (error)
2490be6d3e56SKentaro Takeda 		goto out_drop_write;
24911da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
24921da177e4SLinus Torvalds 		case 0: case S_IFREG:
24934ac91378SJan Blunck 			error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
24941da177e4SLinus Torvalds 			break;
24951da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
24964ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
24971da177e4SLinus Torvalds 					new_decode_dev(dev));
24981da177e4SLinus Torvalds 			break;
24991da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
25004ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
25011da177e4SLinus Torvalds 			break;
25021da177e4SLinus Torvalds 	}
2503be6d3e56SKentaro Takeda out_drop_write:
2504463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2505463c3197SDave Hansen out_dput:
25061da177e4SLinus Torvalds 	dput(dentry);
2507463c3197SDave Hansen out_unlock:
25084ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
25091d957f9bSJan Blunck 	path_put(&nd.path);
25101da177e4SLinus Torvalds 	putname(tmp);
25111da177e4SLinus Torvalds 
25121da177e4SLinus Torvalds 	return error;
25131da177e4SLinus Torvalds }
25141da177e4SLinus Torvalds 
25153480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
25165590ff0dSUlrich Drepper {
25175590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
25185590ff0dSUlrich Drepper }
25195590ff0dSUlrich Drepper 
25201da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
25211da177e4SLinus Torvalds {
2522a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
25231da177e4SLinus Torvalds 
25241da177e4SLinus Torvalds 	if (error)
25251da177e4SLinus Torvalds 		return error;
25261da177e4SLinus Torvalds 
2527acfa4380SAl Viro 	if (!dir->i_op->mkdir)
25281da177e4SLinus Torvalds 		return -EPERM;
25291da177e4SLinus Torvalds 
25301da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
25311da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
25321da177e4SLinus Torvalds 	if (error)
25331da177e4SLinus Torvalds 		return error;
25341da177e4SLinus Torvalds 
25351da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2536a74574aaSStephen Smalley 	if (!error)
2537f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
25381da177e4SLinus Torvalds 	return error;
25391da177e4SLinus Torvalds }
25401da177e4SLinus Torvalds 
25412e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
25421da177e4SLinus Torvalds {
25431da177e4SLinus Torvalds 	int error = 0;
25441da177e4SLinus Torvalds 	char * tmp;
25456902d925SDave Hansen 	struct dentry *dentry;
25466902d925SDave Hansen 	struct nameidata nd;
25471da177e4SLinus Torvalds 
25482ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &tmp);
25492ad94ae6SAl Viro 	if (error)
25506902d925SDave Hansen 		goto out_err;
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 1);
25531da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
25546902d925SDave Hansen 	if (IS_ERR(dentry))
25556902d925SDave Hansen 		goto out_unlock;
25566902d925SDave Hansen 
25574ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2558ce3b0f8dSAl Viro 		mode &= ~current_umask();
2559463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2560463c3197SDave Hansen 	if (error)
2561463c3197SDave Hansen 		goto out_dput;
2562be6d3e56SKentaro Takeda 	error = security_path_mkdir(&nd.path, dentry, mode);
2563be6d3e56SKentaro Takeda 	if (error)
2564be6d3e56SKentaro Takeda 		goto out_drop_write;
25654ac91378SJan Blunck 	error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2566be6d3e56SKentaro Takeda out_drop_write:
2567463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2568463c3197SDave Hansen out_dput:
25691da177e4SLinus Torvalds 	dput(dentry);
25706902d925SDave Hansen out_unlock:
25714ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
25721d957f9bSJan Blunck 	path_put(&nd.path);
25731da177e4SLinus Torvalds 	putname(tmp);
25746902d925SDave Hansen out_err:
25751da177e4SLinus Torvalds 	return error;
25761da177e4SLinus Torvalds }
25771da177e4SLinus Torvalds 
25783cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
25795590ff0dSUlrich Drepper {
25805590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
25815590ff0dSUlrich Drepper }
25825590ff0dSUlrich Drepper 
25831da177e4SLinus Torvalds /*
25841da177e4SLinus Torvalds  * We try to drop the dentry early: we should have
25851da177e4SLinus Torvalds  * a usage count of 2 if we're the only user of this
25861da177e4SLinus Torvalds  * dentry, and if that is true (possibly after pruning
25871da177e4SLinus Torvalds  * the dcache), then we drop the dentry now.
25881da177e4SLinus Torvalds  *
25891da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
25901da177e4SLinus Torvalds  * do a
25911da177e4SLinus Torvalds  *
25921da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
25931da177e4SLinus Torvalds  *		return -EBUSY;
25941da177e4SLinus Torvalds  *
25951da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
25961da177e4SLinus Torvalds  * that is still in use by something else..
25971da177e4SLinus Torvalds  */
25981da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
25991da177e4SLinus Torvalds {
26001da177e4SLinus Torvalds 	dget(dentry);
26011da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
26021da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
2603b7ab39f6SNick Piggin 	if (dentry->d_count == 2)
26041da177e4SLinus Torvalds 		__d_drop(dentry);
26051da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
26061da177e4SLinus Torvalds }
26071da177e4SLinus Torvalds 
26081da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
26091da177e4SLinus Torvalds {
26101da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
26111da177e4SLinus Torvalds 
26121da177e4SLinus Torvalds 	if (error)
26131da177e4SLinus Torvalds 		return error;
26141da177e4SLinus Torvalds 
2615acfa4380SAl Viro 	if (!dir->i_op->rmdir)
26161da177e4SLinus Torvalds 		return -EPERM;
26171da177e4SLinus Torvalds 
26181b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
26191da177e4SLinus Torvalds 	dentry_unhash(dentry);
26201da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
26211da177e4SLinus Torvalds 		error = -EBUSY;
26221da177e4SLinus Torvalds 	else {
26231da177e4SLinus Torvalds 		error = security_inode_rmdir(dir, dentry);
26241da177e4SLinus Torvalds 		if (!error) {
26251da177e4SLinus Torvalds 			error = dir->i_op->rmdir(dir, dentry);
2626d83c49f3SAl Viro 			if (!error) {
26271da177e4SLinus Torvalds 				dentry->d_inode->i_flags |= S_DEAD;
2628d83c49f3SAl Viro 				dont_mount(dentry);
2629d83c49f3SAl Viro 			}
26301da177e4SLinus Torvalds 		}
26311da177e4SLinus Torvalds 	}
26321b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
26331da177e4SLinus Torvalds 	if (!error) {
26341da177e4SLinus Torvalds 		d_delete(dentry);
26351da177e4SLinus Torvalds 	}
26361da177e4SLinus Torvalds 	dput(dentry);
26371da177e4SLinus Torvalds 
26381da177e4SLinus Torvalds 	return error;
26391da177e4SLinus Torvalds }
26401da177e4SLinus Torvalds 
26415590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
26421da177e4SLinus Torvalds {
26431da177e4SLinus Torvalds 	int error = 0;
26441da177e4SLinus Torvalds 	char * name;
26451da177e4SLinus Torvalds 	struct dentry *dentry;
26461da177e4SLinus Torvalds 	struct nameidata nd;
26471da177e4SLinus Torvalds 
26482ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
26491da177e4SLinus Torvalds 	if (error)
26502ad94ae6SAl Viro 		return error;
26511da177e4SLinus Torvalds 
26521da177e4SLinus Torvalds 	switch(nd.last_type) {
26531da177e4SLinus Torvalds 	case LAST_DOTDOT:
26541da177e4SLinus Torvalds 		error = -ENOTEMPTY;
26551da177e4SLinus Torvalds 		goto exit1;
26561da177e4SLinus Torvalds 	case LAST_DOT:
26571da177e4SLinus Torvalds 		error = -EINVAL;
26581da177e4SLinus Torvalds 		goto exit1;
26591da177e4SLinus Torvalds 	case LAST_ROOT:
26601da177e4SLinus Torvalds 		error = -EBUSY;
26611da177e4SLinus Torvalds 		goto exit1;
26621da177e4SLinus Torvalds 	}
26630612d9fbSOGAWA Hirofumi 
26640612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
26650612d9fbSOGAWA Hirofumi 
26664ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
266749705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
26681da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
26696902d925SDave Hansen 	if (IS_ERR(dentry))
26706902d925SDave Hansen 		goto exit2;
26710622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
26720622753bSDave Hansen 	if (error)
26730622753bSDave Hansen 		goto exit3;
2674be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2675be6d3e56SKentaro Takeda 	if (error)
2676be6d3e56SKentaro Takeda 		goto exit4;
26774ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2678be6d3e56SKentaro Takeda exit4:
26790622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
26800622753bSDave Hansen exit3:
26811da177e4SLinus Torvalds 	dput(dentry);
26826902d925SDave Hansen exit2:
26834ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
26841da177e4SLinus Torvalds exit1:
26851d957f9bSJan Blunck 	path_put(&nd.path);
26861da177e4SLinus Torvalds 	putname(name);
26871da177e4SLinus Torvalds 	return error;
26881da177e4SLinus Torvalds }
26891da177e4SLinus Torvalds 
26903cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
26915590ff0dSUlrich Drepper {
26925590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
26935590ff0dSUlrich Drepper }
26945590ff0dSUlrich Drepper 
26951da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
26961da177e4SLinus Torvalds {
26971da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
26981da177e4SLinus Torvalds 
26991da177e4SLinus Torvalds 	if (error)
27001da177e4SLinus Torvalds 		return error;
27011da177e4SLinus Torvalds 
2702acfa4380SAl Viro 	if (!dir->i_op->unlink)
27031da177e4SLinus Torvalds 		return -EPERM;
27041da177e4SLinus Torvalds 
27051b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
27061da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
27071da177e4SLinus Torvalds 		error = -EBUSY;
27081da177e4SLinus Torvalds 	else {
27091da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2710bec1052eSAl Viro 		if (!error) {
27111da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2712bec1052eSAl Viro 			if (!error)
2713d83c49f3SAl Viro 				dont_mount(dentry);
2714bec1052eSAl Viro 		}
27151da177e4SLinus Torvalds 	}
27161b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
27171da177e4SLinus Torvalds 
27181da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
27191da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2720ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
27211da177e4SLinus Torvalds 		d_delete(dentry);
27221da177e4SLinus Torvalds 	}
27230eeca283SRobert Love 
27241da177e4SLinus Torvalds 	return error;
27251da177e4SLinus Torvalds }
27261da177e4SLinus Torvalds 
27271da177e4SLinus Torvalds /*
27281da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
27291b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
27301da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
27311da177e4SLinus Torvalds  * while waiting on the I/O.
27321da177e4SLinus Torvalds  */
27335590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
27341da177e4SLinus Torvalds {
27352ad94ae6SAl Viro 	int error;
27361da177e4SLinus Torvalds 	char *name;
27371da177e4SLinus Torvalds 	struct dentry *dentry;
27381da177e4SLinus Torvalds 	struct nameidata nd;
27391da177e4SLinus Torvalds 	struct inode *inode = NULL;
27401da177e4SLinus Torvalds 
27412ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
27421da177e4SLinus Torvalds 	if (error)
27432ad94ae6SAl Viro 		return error;
27442ad94ae6SAl Viro 
27451da177e4SLinus Torvalds 	error = -EISDIR;
27461da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
27471da177e4SLinus Torvalds 		goto exit1;
27480612d9fbSOGAWA Hirofumi 
27490612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
27500612d9fbSOGAWA Hirofumi 
27514ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
275249705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
27531da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27541da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
27551da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
27561da177e4SLinus Torvalds 		if (nd.last.name[nd.last.len])
27571da177e4SLinus Torvalds 			goto slashes;
27581da177e4SLinus Torvalds 		inode = dentry->d_inode;
27591da177e4SLinus Torvalds 		if (inode)
27607de9c6eeSAl Viro 			ihold(inode);
27610622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
27620622753bSDave Hansen 		if (error)
27630622753bSDave Hansen 			goto exit2;
2764be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2765be6d3e56SKentaro Takeda 		if (error)
2766be6d3e56SKentaro Takeda 			goto exit3;
27674ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2768be6d3e56SKentaro Takeda exit3:
27690622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
27701da177e4SLinus Torvalds 	exit2:
27711da177e4SLinus Torvalds 		dput(dentry);
27721da177e4SLinus Torvalds 	}
27734ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27741da177e4SLinus Torvalds 	if (inode)
27751da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
27761da177e4SLinus Torvalds exit1:
27771d957f9bSJan Blunck 	path_put(&nd.path);
27781da177e4SLinus Torvalds 	putname(name);
27791da177e4SLinus Torvalds 	return error;
27801da177e4SLinus Torvalds 
27811da177e4SLinus Torvalds slashes:
27821da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
27831da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
27841da177e4SLinus Torvalds 	goto exit2;
27851da177e4SLinus Torvalds }
27861da177e4SLinus Torvalds 
27872e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
27885590ff0dSUlrich Drepper {
27895590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
27905590ff0dSUlrich Drepper 		return -EINVAL;
27915590ff0dSUlrich Drepper 
27925590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
27935590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
27945590ff0dSUlrich Drepper 
27955590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
27965590ff0dSUlrich Drepper }
27975590ff0dSUlrich Drepper 
27983480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
27995590ff0dSUlrich Drepper {
28005590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
28015590ff0dSUlrich Drepper }
28025590ff0dSUlrich Drepper 
2803db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
28041da177e4SLinus Torvalds {
2805a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
28061da177e4SLinus Torvalds 
28071da177e4SLinus Torvalds 	if (error)
28081da177e4SLinus Torvalds 		return error;
28091da177e4SLinus Torvalds 
2810acfa4380SAl Viro 	if (!dir->i_op->symlink)
28111da177e4SLinus Torvalds 		return -EPERM;
28121da177e4SLinus Torvalds 
28131da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
28141da177e4SLinus Torvalds 	if (error)
28151da177e4SLinus Torvalds 		return error;
28161da177e4SLinus Torvalds 
28171da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2818a74574aaSStephen Smalley 	if (!error)
2819f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
28201da177e4SLinus Torvalds 	return error;
28211da177e4SLinus Torvalds }
28221da177e4SLinus Torvalds 
28232e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
28242e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
28251da177e4SLinus Torvalds {
28262ad94ae6SAl Viro 	int error;
28271da177e4SLinus Torvalds 	char *from;
28281da177e4SLinus Torvalds 	char *to;
28296902d925SDave Hansen 	struct dentry *dentry;
28306902d925SDave Hansen 	struct nameidata nd;
28311da177e4SLinus Torvalds 
28321da177e4SLinus Torvalds 	from = getname(oldname);
28331da177e4SLinus Torvalds 	if (IS_ERR(from))
28341da177e4SLinus Torvalds 		return PTR_ERR(from);
28352ad94ae6SAl Viro 
28362ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
28372ad94ae6SAl Viro 	if (error)
28386902d925SDave Hansen 		goto out_putname;
28391da177e4SLinus Torvalds 
28401da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
28411da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
28426902d925SDave Hansen 	if (IS_ERR(dentry))
28436902d925SDave Hansen 		goto out_unlock;
28446902d925SDave Hansen 
284575c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
284675c3f29dSDave Hansen 	if (error)
284775c3f29dSDave Hansen 		goto out_dput;
2848be6d3e56SKentaro Takeda 	error = security_path_symlink(&nd.path, dentry, from);
2849be6d3e56SKentaro Takeda 	if (error)
2850be6d3e56SKentaro Takeda 		goto out_drop_write;
2851db2e747bSMiklos Szeredi 	error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2852be6d3e56SKentaro Takeda out_drop_write:
285375c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
285475c3f29dSDave Hansen out_dput:
28551da177e4SLinus Torvalds 	dput(dentry);
28566902d925SDave Hansen out_unlock:
28574ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
28581d957f9bSJan Blunck 	path_put(&nd.path);
28591da177e4SLinus Torvalds 	putname(to);
28606902d925SDave Hansen out_putname:
28611da177e4SLinus Torvalds 	putname(from);
28621da177e4SLinus Torvalds 	return error;
28631da177e4SLinus Torvalds }
28641da177e4SLinus Torvalds 
28653480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
28665590ff0dSUlrich Drepper {
28675590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
28685590ff0dSUlrich Drepper }
28695590ff0dSUlrich Drepper 
28701da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
28711da177e4SLinus Torvalds {
28721da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
28731da177e4SLinus Torvalds 	int error;
28741da177e4SLinus Torvalds 
28751da177e4SLinus Torvalds 	if (!inode)
28761da177e4SLinus Torvalds 		return -ENOENT;
28771da177e4SLinus Torvalds 
2878a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
28791da177e4SLinus Torvalds 	if (error)
28801da177e4SLinus Torvalds 		return error;
28811da177e4SLinus Torvalds 
28821da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
28831da177e4SLinus Torvalds 		return -EXDEV;
28841da177e4SLinus Torvalds 
28851da177e4SLinus Torvalds 	/*
28861da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
28871da177e4SLinus Torvalds 	 */
28881da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
28891da177e4SLinus Torvalds 		return -EPERM;
2890acfa4380SAl Viro 	if (!dir->i_op->link)
28911da177e4SLinus Torvalds 		return -EPERM;
28927e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
28931da177e4SLinus Torvalds 		return -EPERM;
28941da177e4SLinus Torvalds 
28951da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
28961da177e4SLinus Torvalds 	if (error)
28971da177e4SLinus Torvalds 		return error;
28981da177e4SLinus Torvalds 
28997e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
29001da177e4SLinus Torvalds 	error = dir->i_op->link(old_dentry, dir, new_dentry);
29017e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2902e31e14ecSStephen Smalley 	if (!error)
29037e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
29041da177e4SLinus Torvalds 	return error;
29051da177e4SLinus Torvalds }
29061da177e4SLinus Torvalds 
29071da177e4SLinus Torvalds /*
29081da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
29091da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
29101da177e4SLinus Torvalds  * newname.  --KAB
29111da177e4SLinus Torvalds  *
29121da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
29131da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
29141da177e4SLinus Torvalds  * and other special files.  --ADM
29151da177e4SLinus Torvalds  */
29162e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
29172e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
29181da177e4SLinus Torvalds {
29191da177e4SLinus Torvalds 	struct dentry *new_dentry;
29202d8f3038SAl Viro 	struct nameidata nd;
29212d8f3038SAl Viro 	struct path old_path;
29221da177e4SLinus Torvalds 	int error;
29231da177e4SLinus Torvalds 	char *to;
29241da177e4SLinus Torvalds 
292545c9b11aSUlrich Drepper 	if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
2926c04030e1SUlrich Drepper 		return -EINVAL;
2927c04030e1SUlrich Drepper 
29282d8f3038SAl Viro 	error = user_path_at(olddfd, oldname,
292945c9b11aSUlrich Drepper 			     flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
29302d8f3038SAl Viro 			     &old_path);
29311da177e4SLinus Torvalds 	if (error)
29322ad94ae6SAl Viro 		return error;
29332ad94ae6SAl Viro 
29342ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
29351da177e4SLinus Torvalds 	if (error)
29361da177e4SLinus Torvalds 		goto out;
29371da177e4SLinus Torvalds 	error = -EXDEV;
29382d8f3038SAl Viro 	if (old_path.mnt != nd.path.mnt)
29391da177e4SLinus Torvalds 		goto out_release;
29401da177e4SLinus Torvalds 	new_dentry = lookup_create(&nd, 0);
29411da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
29426902d925SDave Hansen 	if (IS_ERR(new_dentry))
29436902d925SDave Hansen 		goto out_unlock;
294475c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
294575c3f29dSDave Hansen 	if (error)
294675c3f29dSDave Hansen 		goto out_dput;
2947be6d3e56SKentaro Takeda 	error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2948be6d3e56SKentaro Takeda 	if (error)
2949be6d3e56SKentaro Takeda 		goto out_drop_write;
29502d8f3038SAl Viro 	error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
2951be6d3e56SKentaro Takeda out_drop_write:
295275c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
295375c3f29dSDave Hansen out_dput:
29541da177e4SLinus Torvalds 	dput(new_dentry);
29556902d925SDave Hansen out_unlock:
29564ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
29571da177e4SLinus Torvalds out_release:
29581d957f9bSJan Blunck 	path_put(&nd.path);
29592ad94ae6SAl Viro 	putname(to);
29601da177e4SLinus Torvalds out:
29612d8f3038SAl Viro 	path_put(&old_path);
29621da177e4SLinus Torvalds 
29631da177e4SLinus Torvalds 	return error;
29641da177e4SLinus Torvalds }
29651da177e4SLinus Torvalds 
29663480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
29675590ff0dSUlrich Drepper {
2968c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
29695590ff0dSUlrich Drepper }
29705590ff0dSUlrich Drepper 
29711da177e4SLinus Torvalds /*
29721da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
29731da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
29741da177e4SLinus Torvalds  * Problems:
29751da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
29761da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
29771da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
2978a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
29791da177e4SLinus Torvalds  *	   story.
29801da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
29811b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
29821da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
29831da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
2984a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
29851da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
29861da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
29871da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
2988a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
29891da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
29901da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
29911da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
29921da177e4SLinus Torvalds  *	d) some filesystems don't support opened-but-unlinked directories,
29931da177e4SLinus Torvalds  *	   either because of layout or because they are not ready to deal with
29941da177e4SLinus Torvalds  *	   all cases correctly. The latter will be fixed (taking this sort of
29951da177e4SLinus Torvalds  *	   stuff into VFS), but the former is not going away. Solution: the same
29961da177e4SLinus Torvalds  *	   trick as in rmdir().
29971da177e4SLinus Torvalds  *	e) conversion from fhandle to dentry may come in the wrong moment - when
29981b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
29991da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3000c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
30011da177e4SLinus Torvalds  *	   locking].
30021da177e4SLinus Torvalds  */
300375c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
30041da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
30051da177e4SLinus Torvalds {
30061da177e4SLinus Torvalds 	int error = 0;
30071da177e4SLinus Torvalds 	struct inode *target;
30081da177e4SLinus Torvalds 
30091da177e4SLinus Torvalds 	/*
30101da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
30111da177e4SLinus Torvalds 	 * we'll need to flip '..'.
30121da177e4SLinus Torvalds 	 */
30131da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3014f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
30151da177e4SLinus Torvalds 		if (error)
30161da177e4SLinus Torvalds 			return error;
30171da177e4SLinus Torvalds 	}
30181da177e4SLinus Torvalds 
30191da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30201da177e4SLinus Torvalds 	if (error)
30211da177e4SLinus Torvalds 		return error;
30221da177e4SLinus Torvalds 
30231da177e4SLinus Torvalds 	target = new_dentry->d_inode;
3024d83c49f3SAl Viro 	if (target)
30251b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
30261da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
30271da177e4SLinus Torvalds 		error = -EBUSY;
3028d83c49f3SAl Viro 	else {
3029d83c49f3SAl Viro 		if (target)
3030d83c49f3SAl Viro 			dentry_unhash(new_dentry);
30311da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3032d83c49f3SAl Viro 	}
30331da177e4SLinus Torvalds 	if (target) {
3034d83c49f3SAl Viro 		if (!error) {
30351da177e4SLinus Torvalds 			target->i_flags |= S_DEAD;
3036d83c49f3SAl Viro 			dont_mount(new_dentry);
3037d83c49f3SAl Viro 		}
30381b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
30391da177e4SLinus Torvalds 		if (d_unhashed(new_dentry))
30401da177e4SLinus Torvalds 			d_rehash(new_dentry);
30411da177e4SLinus Torvalds 		dput(new_dentry);
30421da177e4SLinus Torvalds 	}
3043e31e14ecSStephen Smalley 	if (!error)
3044349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30451da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
30461da177e4SLinus Torvalds 	return error;
30471da177e4SLinus Torvalds }
30481da177e4SLinus Torvalds 
304975c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
30501da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
30511da177e4SLinus Torvalds {
30521da177e4SLinus Torvalds 	struct inode *target;
30531da177e4SLinus Torvalds 	int error;
30541da177e4SLinus Torvalds 
30551da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30561da177e4SLinus Torvalds 	if (error)
30571da177e4SLinus Torvalds 		return error;
30581da177e4SLinus Torvalds 
30591da177e4SLinus Torvalds 	dget(new_dentry);
30601da177e4SLinus Torvalds 	target = new_dentry->d_inode;
30611da177e4SLinus Torvalds 	if (target)
30621b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
30631da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
30641da177e4SLinus Torvalds 		error = -EBUSY;
30651da177e4SLinus Torvalds 	else
30661da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
30671da177e4SLinus Torvalds 	if (!error) {
3068bec1052eSAl Viro 		if (target)
3069d83c49f3SAl Viro 			dont_mount(new_dentry);
3070349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30711da177e4SLinus Torvalds 			d_move(old_dentry, new_dentry);
30721da177e4SLinus Torvalds 	}
30731da177e4SLinus Torvalds 	if (target)
30741b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
30751da177e4SLinus Torvalds 	dput(new_dentry);
30761da177e4SLinus Torvalds 	return error;
30771da177e4SLinus Torvalds }
30781da177e4SLinus Torvalds 
30791da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
30801da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
30811da177e4SLinus Torvalds {
30821da177e4SLinus Torvalds 	int error;
30831da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
308459b0df21SEric Paris 	const unsigned char *old_name;
30851da177e4SLinus Torvalds 
30861da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
30871da177e4SLinus Torvalds  		return 0;
30881da177e4SLinus Torvalds 
30891da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
30901da177e4SLinus Torvalds 	if (error)
30911da177e4SLinus Torvalds 		return error;
30921da177e4SLinus Torvalds 
30931da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3094a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
30951da177e4SLinus Torvalds 	else
30961da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
30971da177e4SLinus Torvalds 	if (error)
30981da177e4SLinus Torvalds 		return error;
30991da177e4SLinus Torvalds 
3100acfa4380SAl Viro 	if (!old_dir->i_op->rename)
31011da177e4SLinus Torvalds 		return -EPERM;
31021da177e4SLinus Torvalds 
31030eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
31040eeca283SRobert Love 
31051da177e4SLinus Torvalds 	if (is_dir)
31061da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
31071da177e4SLinus Torvalds 	else
31081da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3109123df294SAl Viro 	if (!error)
3110123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
31115a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
31120eeca283SRobert Love 	fsnotify_oldname_free(old_name);
31130eeca283SRobert Love 
31141da177e4SLinus Torvalds 	return error;
31151da177e4SLinus Torvalds }
31161da177e4SLinus Torvalds 
31172e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
31182e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
31191da177e4SLinus Torvalds {
31201da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
31211da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
31221da177e4SLinus Torvalds 	struct dentry *trap;
31231da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
31242ad94ae6SAl Viro 	char *from;
31252ad94ae6SAl Viro 	char *to;
31262ad94ae6SAl Viro 	int error;
31271da177e4SLinus Torvalds 
31282ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
31291da177e4SLinus Torvalds 	if (error)
31301da177e4SLinus Torvalds 		goto exit;
31311da177e4SLinus Torvalds 
31322ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
31331da177e4SLinus Torvalds 	if (error)
31341da177e4SLinus Torvalds 		goto exit1;
31351da177e4SLinus Torvalds 
31361da177e4SLinus Torvalds 	error = -EXDEV;
31374ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
31381da177e4SLinus Torvalds 		goto exit2;
31391da177e4SLinus Torvalds 
31404ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
31411da177e4SLinus Torvalds 	error = -EBUSY;
31421da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
31431da177e4SLinus Torvalds 		goto exit2;
31441da177e4SLinus Torvalds 
31454ac91378SJan Blunck 	new_dir = newnd.path.dentry;
31461da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
31471da177e4SLinus Torvalds 		goto exit2;
31481da177e4SLinus Torvalds 
31490612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
31500612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
31514e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
31520612d9fbSOGAWA Hirofumi 
31531da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
31541da177e4SLinus Torvalds 
315549705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
31561da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
31571da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
31581da177e4SLinus Torvalds 		goto exit3;
31591da177e4SLinus Torvalds 	/* source must exist */
31601da177e4SLinus Torvalds 	error = -ENOENT;
31611da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
31621da177e4SLinus Torvalds 		goto exit4;
31631da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
31641da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
31651da177e4SLinus Torvalds 		error = -ENOTDIR;
31661da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
31671da177e4SLinus Torvalds 			goto exit4;
31681da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
31691da177e4SLinus Torvalds 			goto exit4;
31701da177e4SLinus Torvalds 	}
31711da177e4SLinus Torvalds 	/* source should not be ancestor of target */
31721da177e4SLinus Torvalds 	error = -EINVAL;
31731da177e4SLinus Torvalds 	if (old_dentry == trap)
31741da177e4SLinus Torvalds 		goto exit4;
317549705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
31761da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
31771da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
31781da177e4SLinus Torvalds 		goto exit4;
31791da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
31801da177e4SLinus Torvalds 	error = -ENOTEMPTY;
31811da177e4SLinus Torvalds 	if (new_dentry == trap)
31821da177e4SLinus Torvalds 		goto exit5;
31831da177e4SLinus Torvalds 
31849079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
31859079b1ebSDave Hansen 	if (error)
31869079b1ebSDave Hansen 		goto exit5;
3187be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3188be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3189be6d3e56SKentaro Takeda 	if (error)
3190be6d3e56SKentaro Takeda 		goto exit6;
31911da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
31921da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3193be6d3e56SKentaro Takeda exit6:
31949079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
31951da177e4SLinus Torvalds exit5:
31961da177e4SLinus Torvalds 	dput(new_dentry);
31971da177e4SLinus Torvalds exit4:
31981da177e4SLinus Torvalds 	dput(old_dentry);
31991da177e4SLinus Torvalds exit3:
32001da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
32011da177e4SLinus Torvalds exit2:
32021d957f9bSJan Blunck 	path_put(&newnd.path);
32032ad94ae6SAl Viro 	putname(to);
32041da177e4SLinus Torvalds exit1:
32051d957f9bSJan Blunck 	path_put(&oldnd.path);
32061da177e4SLinus Torvalds 	putname(from);
32072ad94ae6SAl Viro exit:
32081da177e4SLinus Torvalds 	return error;
32091da177e4SLinus Torvalds }
32101da177e4SLinus Torvalds 
3211a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
32125590ff0dSUlrich Drepper {
32135590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
32145590ff0dSUlrich Drepper }
32155590ff0dSUlrich Drepper 
32161da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
32171da177e4SLinus Torvalds {
32181da177e4SLinus Torvalds 	int len;
32191da177e4SLinus Torvalds 
32201da177e4SLinus Torvalds 	len = PTR_ERR(link);
32211da177e4SLinus Torvalds 	if (IS_ERR(link))
32221da177e4SLinus Torvalds 		goto out;
32231da177e4SLinus Torvalds 
32241da177e4SLinus Torvalds 	len = strlen(link);
32251da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
32261da177e4SLinus Torvalds 		len = buflen;
32271da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
32281da177e4SLinus Torvalds 		len = -EFAULT;
32291da177e4SLinus Torvalds out:
32301da177e4SLinus Torvalds 	return len;
32311da177e4SLinus Torvalds }
32321da177e4SLinus Torvalds 
32331da177e4SLinus Torvalds /*
32341da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
32351da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
32361da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
32371da177e4SLinus Torvalds  */
32381da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32391da177e4SLinus Torvalds {
32401da177e4SLinus Torvalds 	struct nameidata nd;
3241cc314eefSLinus Torvalds 	void *cookie;
3242694a1764SMarcin Slusarz 	int res;
3243cc314eefSLinus Torvalds 
32441da177e4SLinus Torvalds 	nd.depth = 0;
3245cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3246694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3247694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3248694a1764SMarcin Slusarz 
3249694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
32501da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3251cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3252694a1764SMarcin Slusarz 	return res;
32531da177e4SLinus Torvalds }
32541da177e4SLinus Torvalds 
32551da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
32561da177e4SLinus Torvalds {
32571da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
32581da177e4SLinus Torvalds }
32591da177e4SLinus Torvalds 
32601da177e4SLinus Torvalds /* get the link contents into pagecache */
32611da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
32621da177e4SLinus Torvalds {
3263ebd09abbSDuane Griffin 	char *kaddr;
32641da177e4SLinus Torvalds 	struct page *page;
32651da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3266090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
32671da177e4SLinus Torvalds 	if (IS_ERR(page))
32686fe6900eSNick Piggin 		return (char*)page;
32691da177e4SLinus Torvalds 	*ppage = page;
3270ebd09abbSDuane Griffin 	kaddr = kmap(page);
3271ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3272ebd09abbSDuane Griffin 	return kaddr;
32731da177e4SLinus Torvalds }
32741da177e4SLinus Torvalds 
32751da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32761da177e4SLinus Torvalds {
32771da177e4SLinus Torvalds 	struct page *page = NULL;
32781da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
32791da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
32801da177e4SLinus Torvalds 	if (page) {
32811da177e4SLinus Torvalds 		kunmap(page);
32821da177e4SLinus Torvalds 		page_cache_release(page);
32831da177e4SLinus Torvalds 	}
32841da177e4SLinus Torvalds 	return res;
32851da177e4SLinus Torvalds }
32861da177e4SLinus Torvalds 
3287cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
32881da177e4SLinus Torvalds {
3289cc314eefSLinus Torvalds 	struct page *page = NULL;
32901da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3291cc314eefSLinus Torvalds 	return page;
32921da177e4SLinus Torvalds }
32931da177e4SLinus Torvalds 
3294cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
32951da177e4SLinus Torvalds {
3296cc314eefSLinus Torvalds 	struct page *page = cookie;
3297cc314eefSLinus Torvalds 
3298cc314eefSLinus Torvalds 	if (page) {
32991da177e4SLinus Torvalds 		kunmap(page);
33001da177e4SLinus Torvalds 		page_cache_release(page);
33011da177e4SLinus Torvalds 	}
33021da177e4SLinus Torvalds }
33031da177e4SLinus Torvalds 
330454566b2cSNick Piggin /*
330554566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
330654566b2cSNick Piggin  */
330754566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
33081da177e4SLinus Torvalds {
33091da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
33100adb25d2SKirill Korotaev 	struct page *page;
3311afddba49SNick Piggin 	void *fsdata;
3312beb497abSDmitriy Monakhov 	int err;
33131da177e4SLinus Torvalds 	char *kaddr;
331454566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
331554566b2cSNick Piggin 	if (nofs)
331654566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
33171da177e4SLinus Torvalds 
33187e53cac4SNeilBrown retry:
3319afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
332054566b2cSNick Piggin 				flags, &page, &fsdata);
33211da177e4SLinus Torvalds 	if (err)
3322afddba49SNick Piggin 		goto fail;
3323afddba49SNick Piggin 
33241da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
33251da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
33261da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
3327afddba49SNick Piggin 
3328afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3329afddba49SNick Piggin 							page, fsdata);
33301da177e4SLinus Torvalds 	if (err < 0)
33311da177e4SLinus Torvalds 		goto fail;
3332afddba49SNick Piggin 	if (err < len-1)
3333afddba49SNick Piggin 		goto retry;
3334afddba49SNick Piggin 
33351da177e4SLinus Torvalds 	mark_inode_dirty(inode);
33361da177e4SLinus Torvalds 	return 0;
33371da177e4SLinus Torvalds fail:
33381da177e4SLinus Torvalds 	return err;
33391da177e4SLinus Torvalds }
33401da177e4SLinus Torvalds 
33410adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
33420adb25d2SKirill Korotaev {
33430adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
334454566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
33450adb25d2SKirill Korotaev }
33460adb25d2SKirill Korotaev 
334792e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
33481da177e4SLinus Torvalds 	.readlink	= generic_readlink,
33491da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
33501da177e4SLinus Torvalds 	.put_link	= page_put_link,
33511da177e4SLinus Torvalds };
33521da177e4SLinus Torvalds 
33532d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3354cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
33551da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
33561da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
33571da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
33581da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
33591da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
33601da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
33611da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
33621da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
33631da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
33640adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
33651da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
33661da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3367c9c6cac0SAl Viro EXPORT_SYMBOL(kern_path_parent);
3368d1811465SAl Viro EXPORT_SYMBOL(kern_path);
336916f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3370f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
33718c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission);
33721da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
33731da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
33741da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
33751da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
33761da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
33771da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
33781da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
33791da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
33801da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
33811da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
33821da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
33831da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
33841da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
33851da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3386