xref: /openbmc/linux/fs/namei.c (revision fe479a58)
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;
40431e6b01fSNick Piggin 
40531e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
40631e6b01fSNick Piggin 	if (nd->root.mnt) {
40731e6b01fSNick Piggin 		spin_lock(&fs->lock);
40831e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
40931e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
41031e6b01fSNick Piggin 			goto err_root;
41131e6b01fSNick Piggin 	}
41231e6b01fSNick Piggin 	spin_lock(&dentry->d_lock);
41331e6b01fSNick Piggin 	if (!__d_rcu_to_refcount(dentry, nd->seq))
41431e6b01fSNick Piggin 		goto err;
41531e6b01fSNick Piggin 	BUG_ON(nd->inode != dentry->d_inode);
41631e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
41731e6b01fSNick Piggin 	if (nd->root.mnt) {
41831e6b01fSNick Piggin 		path_get(&nd->root);
41931e6b01fSNick Piggin 		spin_unlock(&fs->lock);
42031e6b01fSNick Piggin 	}
42131e6b01fSNick Piggin 	mntget(nd->path.mnt);
42231e6b01fSNick Piggin 
42331e6b01fSNick Piggin 	rcu_read_unlock();
42431e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
42531e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
42631e6b01fSNick Piggin 	return 0;
42731e6b01fSNick Piggin err:
42831e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
42931e6b01fSNick Piggin err_root:
43031e6b01fSNick Piggin 	if (nd->root.mnt)
43131e6b01fSNick Piggin 		spin_unlock(&fs->lock);
43231e6b01fSNick Piggin 	return -ECHILD;
43331e6b01fSNick Piggin }
43431e6b01fSNick Piggin 
43531e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing.  */
43631e6b01fSNick Piggin static inline int nameidata_drop_rcu_maybe(struct nameidata *nd)
43731e6b01fSNick Piggin {
43831e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU)
43931e6b01fSNick Piggin 		return nameidata_drop_rcu(nd);
44031e6b01fSNick Piggin 	return 0;
44131e6b01fSNick Piggin }
44231e6b01fSNick Piggin 
44331e6b01fSNick Piggin /**
44431e6b01fSNick Piggin  * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk
44531e6b01fSNick Piggin  * @nd: nameidata pathwalk data to drop
44631e6b01fSNick Piggin  * @dentry: dentry to drop
44739191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
44831e6b01fSNick Piggin  *
44931e6b01fSNick Piggin  * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root,
45031e6b01fSNick Piggin  * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on
45131e6b01fSNick Piggin  * @nd. Must be called from rcu-walk context.
45231e6b01fSNick Piggin  */
45331e6b01fSNick Piggin static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry)
45431e6b01fSNick Piggin {
45531e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
45631e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
45731e6b01fSNick Piggin 
45831e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
45931e6b01fSNick Piggin 	if (nd->root.mnt) {
46031e6b01fSNick Piggin 		spin_lock(&fs->lock);
46131e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
46231e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
46331e6b01fSNick Piggin 			goto err_root;
46431e6b01fSNick Piggin 	}
46531e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
46631e6b01fSNick Piggin 	spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
46731e6b01fSNick Piggin 	if (!__d_rcu_to_refcount(dentry, nd->seq))
46831e6b01fSNick Piggin 		goto err;
46931e6b01fSNick Piggin 	/*
47031e6b01fSNick Piggin 	 * If the sequence check on the child dentry passed, then the child has
47131e6b01fSNick Piggin 	 * not been removed from its parent. This means the parent dentry must
47231e6b01fSNick Piggin 	 * be valid and able to take a reference at this point.
47331e6b01fSNick Piggin 	 */
47431e6b01fSNick Piggin 	BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
47531e6b01fSNick Piggin 	BUG_ON(!parent->d_count);
47631e6b01fSNick Piggin 	parent->d_count++;
47731e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
47831e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
47931e6b01fSNick Piggin 	if (nd->root.mnt) {
48031e6b01fSNick Piggin 		path_get(&nd->root);
48131e6b01fSNick Piggin 		spin_unlock(&fs->lock);
48231e6b01fSNick Piggin 	}
48331e6b01fSNick Piggin 	mntget(nd->path.mnt);
48431e6b01fSNick Piggin 
48531e6b01fSNick Piggin 	rcu_read_unlock();
48631e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
48731e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
48831e6b01fSNick Piggin 	return 0;
48931e6b01fSNick Piggin err:
49031e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
49131e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
49231e6b01fSNick Piggin err_root:
49331e6b01fSNick Piggin 	if (nd->root.mnt)
49431e6b01fSNick Piggin 		spin_unlock(&fs->lock);
49531e6b01fSNick Piggin 	return -ECHILD;
49631e6b01fSNick Piggin }
49731e6b01fSNick Piggin 
49831e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing.  */
49931e6b01fSNick Piggin static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry)
50031e6b01fSNick Piggin {
50131e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU)
50231e6b01fSNick Piggin 		return nameidata_dentry_drop_rcu(nd, dentry);
50331e6b01fSNick Piggin 	return 0;
50431e6b01fSNick Piggin }
50531e6b01fSNick Piggin 
50631e6b01fSNick Piggin /**
50731e6b01fSNick Piggin  * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk
50831e6b01fSNick Piggin  * @nd: nameidata pathwalk data to drop
50939191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
51031e6b01fSNick Piggin  *
51131e6b01fSNick Piggin  * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk.
51231e6b01fSNick Piggin  * nd->path should be the final element of the lookup, so nd->root is discarded.
51331e6b01fSNick Piggin  * Must be called from rcu-walk context.
51431e6b01fSNick Piggin  */
51531e6b01fSNick Piggin static int nameidata_drop_rcu_last(struct nameidata *nd)
51631e6b01fSNick Piggin {
51731e6b01fSNick Piggin 	struct dentry *dentry = nd->path.dentry;
51831e6b01fSNick Piggin 
51931e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
52031e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
52131e6b01fSNick Piggin 	nd->root.mnt = NULL;
52231e6b01fSNick Piggin 	spin_lock(&dentry->d_lock);
52331e6b01fSNick Piggin 	if (!__d_rcu_to_refcount(dentry, nd->seq))
52431e6b01fSNick Piggin 		goto err_unlock;
52531e6b01fSNick Piggin 	BUG_ON(nd->inode != dentry->d_inode);
52631e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
52731e6b01fSNick Piggin 
52831e6b01fSNick Piggin 	mntget(nd->path.mnt);
52931e6b01fSNick Piggin 
53031e6b01fSNick Piggin 	rcu_read_unlock();
53131e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
53231e6b01fSNick Piggin 
53331e6b01fSNick Piggin 	return 0;
53431e6b01fSNick Piggin 
53531e6b01fSNick Piggin err_unlock:
53631e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
53731e6b01fSNick Piggin 	rcu_read_unlock();
53831e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
53931e6b01fSNick Piggin 	return -ECHILD;
54031e6b01fSNick Piggin }
54131e6b01fSNick Piggin 
54231e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing.  */
54331e6b01fSNick Piggin static inline int nameidata_drop_rcu_last_maybe(struct nameidata *nd)
54431e6b01fSNick Piggin {
54531e6b01fSNick Piggin 	if (likely(nd->flags & LOOKUP_RCU))
54631e6b01fSNick Piggin 		return nameidata_drop_rcu_last(nd);
54731e6b01fSNick Piggin 	return 0;
54831e6b01fSNick Piggin }
54931e6b01fSNick Piggin 
55031e6b01fSNick Piggin /**
551834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
552834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
553834f2a4aSTrond Myklebust  */
554834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
555834f2a4aSTrond Myklebust {
5562dab5974SLinus Torvalds 	struct file *file = nd->intent.open.file;
5572dab5974SLinus Torvalds 
5582dab5974SLinus Torvalds 	if (file && !IS_ERR(file)) {
5592dab5974SLinus Torvalds 		if (file->f_path.dentry == NULL)
5602dab5974SLinus Torvalds 			put_filp(file);
561834f2a4aSTrond Myklebust 		else
5622dab5974SLinus Torvalds 			fput(file);
5632dab5974SLinus Torvalds 	}
564834f2a4aSTrond Myklebust }
565834f2a4aSTrond Myklebust 
566f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
56734286d66SNick Piggin {
568f60aef7eSAl Viro 	return dentry->d_op->d_revalidate(dentry, nd);
56934286d66SNick Piggin }
57034286d66SNick Piggin 
571f5e1c1c1SAl Viro static struct dentry *
572bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd)
573bcdc5e01SIan Kent {
574f5e1c1c1SAl Viro 	int status = d_revalidate(dentry, nd);
575bcdc5e01SIan Kent 	if (unlikely(status <= 0)) {
576bcdc5e01SIan Kent 		/*
577bcdc5e01SIan Kent 		 * The dentry failed validation.
578bcdc5e01SIan Kent 		 * If d_revalidate returned 0 attempt to invalidate
579bcdc5e01SIan Kent 		 * the dentry otherwise d_revalidate is asking us
580bcdc5e01SIan Kent 		 * to return a fail status.
581bcdc5e01SIan Kent 		 */
58234286d66SNick Piggin 		if (status < 0) {
58334286d66SNick Piggin 			dput(dentry);
58434286d66SNick Piggin 			dentry = ERR_PTR(status);
585f5e1c1c1SAl Viro 		} else if (!d_invalidate(dentry)) {
586bcdc5e01SIan Kent 			dput(dentry);
587bcdc5e01SIan Kent 			dentry = NULL;
588bcdc5e01SIan Kent 		}
589bcdc5e01SIan Kent 	}
590f5e1c1c1SAl Viro 	return dentry;
591f5e1c1c1SAl Viro }
592f5e1c1c1SAl Viro 
593f5e1c1c1SAl Viro static inline struct dentry *
594f5e1c1c1SAl Viro do_revalidate_rcu(struct dentry *dentry, struct nameidata *nd)
595f5e1c1c1SAl Viro {
596f60aef7eSAl Viro 	int status = d_revalidate(dentry, nd);
597f5e1c1c1SAl Viro 	if (likely(status > 0))
598f5e1c1c1SAl Viro 		return dentry;
599f5e1c1c1SAl Viro 	if (status == -ECHILD) {
600f5e1c1c1SAl Viro 		if (nameidata_dentry_drop_rcu(nd, dentry))
601f5e1c1c1SAl Viro 			return ERR_PTR(-ECHILD);
602f5e1c1c1SAl Viro 		return do_revalidate(dentry, nd);
603f5e1c1c1SAl Viro 	}
604f5e1c1c1SAl Viro 	if (status < 0)
605f5e1c1c1SAl Viro 		return ERR_PTR(status);
606f5e1c1c1SAl Viro 	/* Don't d_invalidate in rcu-walk mode */
607f5e1c1c1SAl Viro 	if (nameidata_dentry_drop_rcu(nd, dentry))
608f5e1c1c1SAl Viro 		return ERR_PTR(-ECHILD);
609f5e1c1c1SAl Viro 	if (!d_invalidate(dentry)) {
610f5e1c1c1SAl Viro 		dput(dentry);
611f5e1c1c1SAl Viro 		dentry = NULL;
612bcdc5e01SIan Kent 	}
613bcdc5e01SIan Kent 	return dentry;
614bcdc5e01SIan Kent }
615bcdc5e01SIan Kent 
616fb045adbSNick Piggin static inline int need_reval_dot(struct dentry *dentry)
617fb045adbSNick Piggin {
618fb045adbSNick Piggin 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
619fb045adbSNick Piggin 		return 0;
620fb045adbSNick Piggin 
621fb045adbSNick Piggin 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
622fb045adbSNick Piggin 		return 0;
623fb045adbSNick Piggin 
624fb045adbSNick Piggin 	return 1;
625fb045adbSNick Piggin }
626fb045adbSNick Piggin 
6271da177e4SLinus Torvalds /*
62839159de2SJeff Layton  * force_reval_path - force revalidation of a dentry
62939159de2SJeff Layton  *
63039159de2SJeff Layton  * In some situations the path walking code will trust dentries without
63139159de2SJeff Layton  * revalidating them. This causes problems for filesystems that depend on
63239159de2SJeff Layton  * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
63339159de2SJeff Layton  * (which indicates that it's possible for the dentry to go stale), force
63439159de2SJeff Layton  * a d_revalidate call before proceeding.
63539159de2SJeff Layton  *
63639159de2SJeff Layton  * Returns 0 if the revalidation was successful. If the revalidation fails,
63739159de2SJeff Layton  * either return the error returned by d_revalidate or -ESTALE if the
63839159de2SJeff Layton  * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
63939159de2SJeff Layton  * invalidate the dentry. It's up to the caller to handle putting references
64039159de2SJeff Layton  * to the path if necessary.
64139159de2SJeff Layton  */
64239159de2SJeff Layton static int
64339159de2SJeff Layton force_reval_path(struct path *path, struct nameidata *nd)
64439159de2SJeff Layton {
64539159de2SJeff Layton 	int status;
64639159de2SJeff Layton 	struct dentry *dentry = path->dentry;
64739159de2SJeff Layton 
64839159de2SJeff Layton 	/*
64939159de2SJeff Layton 	 * only check on filesystems where it's possible for the dentry to
650fb045adbSNick Piggin 	 * become stale.
65139159de2SJeff Layton 	 */
652fb045adbSNick Piggin 	if (!need_reval_dot(dentry))
65339159de2SJeff Layton 		return 0;
65439159de2SJeff Layton 
65534286d66SNick Piggin 	status = d_revalidate(dentry, nd);
65639159de2SJeff Layton 	if (status > 0)
65739159de2SJeff Layton 		return 0;
65839159de2SJeff Layton 
65939159de2SJeff Layton 	if (!status) {
66039159de2SJeff Layton 		d_invalidate(dentry);
66139159de2SJeff Layton 		status = -ESTALE;
66239159de2SJeff Layton 	}
66339159de2SJeff Layton 	return status;
66439159de2SJeff Layton }
66539159de2SJeff Layton 
66639159de2SJeff Layton /*
667b75b5086SAl Viro  * Short-cut version of permission(), for calling on directories
668b75b5086SAl Viro  * during pathname resolution.  Combines parts of permission()
669b75b5086SAl Viro  * and generic_permission(), and tests ONLY for MAY_EXEC permission.
6701da177e4SLinus Torvalds  *
6711da177e4SLinus Torvalds  * If appropriate, check DAC only.  If not appropriate, or
672b75b5086SAl Viro  * short-cut DAC fails, then call ->permission() to do more
6731da177e4SLinus Torvalds  * complete permission check.
6741da177e4SLinus Torvalds  */
675b74c79e9SNick Piggin static inline int exec_permission(struct inode *inode, unsigned int flags)
6761da177e4SLinus Torvalds {
6775909ccaaSLinus Torvalds 	int ret;
6781da177e4SLinus Torvalds 
679cb9179eaSLinus Torvalds 	if (inode->i_op->permission) {
680b74c79e9SNick Piggin 		ret = inode->i_op->permission(inode, MAY_EXEC, flags);
681b74c79e9SNick Piggin 	} else {
682b74c79e9SNick Piggin 		ret = acl_permission_check(inode, MAY_EXEC, flags,
683b74c79e9SNick Piggin 				inode->i_op->check_acl);
684cb9179eaSLinus Torvalds 	}
685b74c79e9SNick Piggin 	if (likely(!ret))
6861da177e4SLinus Torvalds 		goto ok;
687b74c79e9SNick Piggin 	if (ret == -ECHILD)
68831e6b01fSNick Piggin 		return ret;
6891da177e4SLinus Torvalds 
690f1ac9f6bSLinus Torvalds 	if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
6911da177e4SLinus Torvalds 		goto ok;
6921da177e4SLinus Torvalds 
6935909ccaaSLinus Torvalds 	return ret;
6941da177e4SLinus Torvalds ok:
695b74c79e9SNick Piggin 	return security_inode_exec_permission(inode, flags);
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
6982a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
6992a737871SAl Viro {
700f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
701f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
7022a737871SAl Viro }
7032a737871SAl Viro 
7046de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
7056de88d72SAl Viro 
70631e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
70731e6b01fSNick Piggin {
70831e6b01fSNick Piggin 	if (!nd->root.mnt) {
70931e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
710c28cc364SNick Piggin 		unsigned seq;
711c28cc364SNick Piggin 
712c28cc364SNick Piggin 		do {
713c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
71431e6b01fSNick Piggin 			nd->root = fs->root;
715c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
71631e6b01fSNick Piggin 	}
71731e6b01fSNick Piggin }
71831e6b01fSNick Piggin 
719f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
7201da177e4SLinus Torvalds {
72131e6b01fSNick Piggin 	int ret;
72231e6b01fSNick Piggin 
7231da177e4SLinus Torvalds 	if (IS_ERR(link))
7241da177e4SLinus Torvalds 		goto fail;
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	if (*link == '/') {
7272a737871SAl Viro 		set_root(nd);
7281d957f9bSJan Blunck 		path_put(&nd->path);
7292a737871SAl Viro 		nd->path = nd->root;
7302a737871SAl Viro 		path_get(&nd->root);
7311da177e4SLinus Torvalds 	}
73231e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
733b4091d5fSChristoph Hellwig 
73431e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
73531e6b01fSNick Piggin 	return ret;
7361da177e4SLinus Torvalds fail:
7371d957f9bSJan Blunck 	path_put(&nd->path);
7381da177e4SLinus Torvalds 	return PTR_ERR(link);
7391da177e4SLinus Torvalds }
7401da177e4SLinus Torvalds 
7411d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
742051d3812SIan Kent {
743051d3812SIan Kent 	dput(path->dentry);
7444ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
745051d3812SIan Kent 		mntput(path->mnt);
746051d3812SIan Kent }
747051d3812SIan Kent 
7487b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
7497b9337aaSNick Piggin 					struct nameidata *nd)
750051d3812SIan Kent {
75131e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
7524ac91378SJan Blunck 		dput(nd->path.dentry);
75331e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
7544ac91378SJan Blunck 			mntput(nd->path.mnt);
7559a229683SHuang Shijie 	}
75631e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
7574ac91378SJan Blunck 	nd->path.dentry = path->dentry;
758051d3812SIan Kent }
759051d3812SIan Kent 
760def4af30SAl Viro static __always_inline int
7617b9337aaSNick Piggin __do_follow_link(const struct path *link, struct nameidata *nd, void **p)
7621da177e4SLinus Torvalds {
7631da177e4SLinus Torvalds 	int error;
7647b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
7651da177e4SLinus Torvalds 
766844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
767844a3917SAl Viro 
7687b9337aaSNick Piggin 	touch_atime(link->mnt, dentry);
7691da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
770cd4e91d3SAl Viro 
77187556ef1SDavid Howells 	if (link->mnt == nd->path.mnt)
7727b9337aaSNick Piggin 		mntget(link->mnt);
77331e6b01fSNick Piggin 
77486acdca1SAl Viro 	nd->last_type = LAST_BIND;
775def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
776def4af30SAl Viro 	error = PTR_ERR(*p);
777def4af30SAl Viro 	if (!IS_ERR(*p)) {
7781da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
779cc314eefSLinus Torvalds 		error = 0;
7801da177e4SLinus Torvalds 		if (s)
7811da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
78239159de2SJeff Layton 		else if (nd->last_type == LAST_BIND) {
78339159de2SJeff Layton 			error = force_reval_path(&nd->path, nd);
78439159de2SJeff Layton 			if (error)
78539159de2SJeff Layton 				path_put(&nd->path);
78639159de2SJeff Layton 		}
7871da177e4SLinus Torvalds 	}
7881da177e4SLinus Torvalds 	return error;
7891da177e4SLinus Torvalds }
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds /*
7921da177e4SLinus Torvalds  * This limits recursive symlink follows to 8, while
7931da177e4SLinus Torvalds  * limiting consecutive symlinks to 40.
7941da177e4SLinus Torvalds  *
7951da177e4SLinus Torvalds  * Without that kind of total limit, nasty chains of consecutive
7961da177e4SLinus Torvalds  * symlinks can cause almost arbitrarily long lookups.
7971da177e4SLinus Torvalds  */
7983abb17e8SLinus Torvalds static inline int do_follow_link(struct inode *inode, struct path *path, struct nameidata *nd)
7991da177e4SLinus Torvalds {
800def4af30SAl Viro 	void *cookie;
8011da177e4SLinus Torvalds 	int err = -ELOOP;
802844a3917SAl Viro 
803844a3917SAl Viro 	/* We drop rcu-walk here */
804844a3917SAl Viro 	if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry))
805844a3917SAl Viro 		return -ECHILD;
8063abb17e8SLinus Torvalds 	BUG_ON(inode != path->dentry->d_inode);
807844a3917SAl Viro 
8081da177e4SLinus Torvalds 	if (current->link_count >= MAX_NESTED_LINKS)
8091da177e4SLinus Torvalds 		goto loop;
8101da177e4SLinus Torvalds 	if (current->total_link_count >= 40)
8111da177e4SLinus Torvalds 		goto loop;
8121da177e4SLinus Torvalds 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
8131da177e4SLinus Torvalds 	cond_resched();
81490ebe565SAl Viro 	err = security_inode_follow_link(path->dentry, nd);
8151da177e4SLinus Torvalds 	if (err)
8161da177e4SLinus Torvalds 		goto loop;
8171da177e4SLinus Torvalds 	current->link_count++;
8181da177e4SLinus Torvalds 	current->total_link_count++;
8191da177e4SLinus Torvalds 	nd->depth++;
820def4af30SAl Viro 	err = __do_follow_link(path, nd, &cookie);
821def4af30SAl Viro 	if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link)
822def4af30SAl Viro 		path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie);
823258fa999SAl Viro 	path_put(path);
8241da177e4SLinus Torvalds 	current->link_count--;
8251da177e4SLinus Torvalds 	nd->depth--;
8261da177e4SLinus Torvalds 	return err;
8271da177e4SLinus Torvalds loop:
8281d957f9bSJan Blunck 	path_put_conditional(path, nd);
8291d957f9bSJan Blunck 	path_put(&nd->path);
8301da177e4SLinus Torvalds 	return err;
8311da177e4SLinus Torvalds }
8321da177e4SLinus Torvalds 
83331e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
83431e6b01fSNick Piggin {
83531e6b01fSNick Piggin 	struct vfsmount *parent;
83631e6b01fSNick Piggin 	struct dentry *mountpoint;
83731e6b01fSNick Piggin 
83831e6b01fSNick Piggin 	parent = path->mnt->mnt_parent;
83931e6b01fSNick Piggin 	if (parent == path->mnt)
84031e6b01fSNick Piggin 		return 0;
84131e6b01fSNick Piggin 	mountpoint = path->mnt->mnt_mountpoint;
84231e6b01fSNick Piggin 	path->dentry = mountpoint;
84331e6b01fSNick Piggin 	path->mnt = parent;
84431e6b01fSNick Piggin 	return 1;
84531e6b01fSNick Piggin }
84631e6b01fSNick Piggin 
847bab77ebfSAl Viro int follow_up(struct path *path)
8481da177e4SLinus Torvalds {
8491da177e4SLinus Torvalds 	struct vfsmount *parent;
8501da177e4SLinus Torvalds 	struct dentry *mountpoint;
85199b7db7bSNick Piggin 
85299b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
853bab77ebfSAl Viro 	parent = path->mnt->mnt_parent;
854bab77ebfSAl Viro 	if (parent == path->mnt) {
85599b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
8561da177e4SLinus Torvalds 		return 0;
8571da177e4SLinus Torvalds 	}
8581da177e4SLinus Torvalds 	mntget(parent);
859bab77ebfSAl Viro 	mountpoint = dget(path->mnt->mnt_mountpoint);
86099b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
861bab77ebfSAl Viro 	dput(path->dentry);
862bab77ebfSAl Viro 	path->dentry = mountpoint;
863bab77ebfSAl Viro 	mntput(path->mnt);
864bab77ebfSAl Viro 	path->mnt = parent;
8651da177e4SLinus Torvalds 	return 1;
8661da177e4SLinus Torvalds }
8671da177e4SLinus Torvalds 
868b5c84bf6SNick Piggin /*
8699875cf80SDavid Howells  * Perform an automount
8709875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
8719875cf80SDavid Howells  *   were called with.
8721da177e4SLinus Torvalds  */
8739875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
8749875cf80SDavid Howells 			    bool *need_mntput)
87531e6b01fSNick Piggin {
8769875cf80SDavid Howells 	struct vfsmount *mnt;
877ea5b778aSDavid Howells 	int err;
8789875cf80SDavid Howells 
8799875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
8809875cf80SDavid Howells 		return -EREMOTE;
8819875cf80SDavid Howells 
8826f45b656SDavid Howells 	/* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
8836f45b656SDavid Howells 	 * and this is the terminal part of the path.
8846f45b656SDavid Howells 	 */
8856f45b656SDavid Howells 	if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
8866f45b656SDavid Howells 		return -EISDIR; /* we actually want to stop here */
8876f45b656SDavid Howells 
8889875cf80SDavid Howells 	/* We want to mount if someone is trying to open/create a file of any
8899875cf80SDavid Howells 	 * type under the mountpoint, wants to traverse through the mountpoint
8909875cf80SDavid Howells 	 * or wants to open the mounted directory.
8919875cf80SDavid Howells 	 *
8929875cf80SDavid Howells 	 * We don't want to mount if someone's just doing a stat and they've
8939875cf80SDavid Howells 	 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
8949875cf80SDavid Howells 	 * appended a '/' to the name.
8959875cf80SDavid Howells 	 */
8969875cf80SDavid Howells 	if (!(flags & LOOKUP_FOLLOW) &&
8979875cf80SDavid Howells 	    !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
8989875cf80SDavid Howells 		       LOOKUP_OPEN | LOOKUP_CREATE)))
8999875cf80SDavid Howells 		return -EISDIR;
9009875cf80SDavid Howells 
9019875cf80SDavid Howells 	current->total_link_count++;
9029875cf80SDavid Howells 	if (current->total_link_count >= 40)
9039875cf80SDavid Howells 		return -ELOOP;
9049875cf80SDavid Howells 
9059875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
9069875cf80SDavid Howells 	if (IS_ERR(mnt)) {
9079875cf80SDavid Howells 		/*
9089875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
9099875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
9109875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
9119875cf80SDavid Howells 		 *
9129875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
9139875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
9149875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
9159875cf80SDavid Howells 		 */
9169875cf80SDavid Howells 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
9179875cf80SDavid Howells 			return -EREMOTE;
9189875cf80SDavid Howells 		return PTR_ERR(mnt);
91931e6b01fSNick Piggin 	}
920ea5b778aSDavid Howells 
9219875cf80SDavid Howells 	if (!mnt) /* mount collision */
9229875cf80SDavid Howells 		return 0;
9239875cf80SDavid Howells 
92419a167afSAl Viro 	err = finish_automount(mnt, path);
925ea5b778aSDavid Howells 
926ea5b778aSDavid Howells 	switch (err) {
927ea5b778aSDavid Howells 	case -EBUSY:
928ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
92919a167afSAl Viro 		return 0;
930ea5b778aSDavid Howells 	case 0:
931463ffb2eSAl Viro 		dput(path->dentry);
9329875cf80SDavid Howells 		if (*need_mntput)
9339875cf80SDavid Howells 			mntput(path->mnt);
9349875cf80SDavid Howells 		path->mnt = mnt;
9359875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
9369875cf80SDavid Howells 		*need_mntput = true;
9379875cf80SDavid Howells 		return 0;
93819a167afSAl Viro 	default:
93919a167afSAl Viro 		return err;
9409875cf80SDavid Howells 	}
94119a167afSAl Viro 
942ea5b778aSDavid Howells }
9439875cf80SDavid Howells 
9449875cf80SDavid Howells /*
9459875cf80SDavid Howells  * Handle a dentry that is managed in some way.
946cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
9479875cf80SDavid Howells  * - Flagged as mountpoint
9489875cf80SDavid Howells  * - Flagged as automount point
9499875cf80SDavid Howells  *
9509875cf80SDavid Howells  * This may only be called in refwalk mode.
9519875cf80SDavid Howells  *
9529875cf80SDavid Howells  * Serialization is taken care of in namespace.c
9539875cf80SDavid Howells  */
9549875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
9559875cf80SDavid Howells {
9569875cf80SDavid Howells 	unsigned managed;
9579875cf80SDavid Howells 	bool need_mntput = false;
9589875cf80SDavid Howells 	int ret;
9599875cf80SDavid Howells 
9609875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
9619875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
9629875cf80SDavid Howells 	 * the components of that value change under us */
9639875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
9649875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
9659875cf80SDavid Howells 	       unlikely(managed != 0)) {
966cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
967cc53ce53SDavid Howells 		 * being held. */
968cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
969cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
970cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
971ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(path->dentry,
972ab90911fSDavid Howells 							   false, false);
973cc53ce53SDavid Howells 			if (ret < 0)
974cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
975cc53ce53SDavid Howells 		}
976cc53ce53SDavid Howells 
9779875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
9789875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
9799875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
9809875cf80SDavid Howells 			if (mounted) {
9819875cf80SDavid Howells 				dput(path->dentry);
9829875cf80SDavid Howells 				if (need_mntput)
983463ffb2eSAl Viro 					mntput(path->mnt);
984463ffb2eSAl Viro 				path->mnt = mounted;
985463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
9869875cf80SDavid Howells 				need_mntput = true;
9879875cf80SDavid Howells 				continue;
988463ffb2eSAl Viro 			}
989463ffb2eSAl Viro 
9909875cf80SDavid Howells 			/* Something is mounted on this dentry in another
9919875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
9929875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
9939875cf80SDavid Howells 			 * vfsmount_lock */
9941da177e4SLinus Torvalds 		}
9959875cf80SDavid Howells 
9969875cf80SDavid Howells 		/* Handle an automount point */
9979875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
9989875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
9999875cf80SDavid Howells 			if (ret < 0)
10009875cf80SDavid Howells 				return ret == -EISDIR ? 0 : ret;
10019875cf80SDavid Howells 			continue;
10029875cf80SDavid Howells 		}
10039875cf80SDavid Howells 
10049875cf80SDavid Howells 		/* We didn't change the current path point */
10059875cf80SDavid Howells 		break;
10069875cf80SDavid Howells 	}
10079875cf80SDavid Howells 	return 0;
10081da177e4SLinus Torvalds }
10091da177e4SLinus Torvalds 
1010cc53ce53SDavid Howells int follow_down_one(struct path *path)
10111da177e4SLinus Torvalds {
10121da177e4SLinus Torvalds 	struct vfsmount *mounted;
10131da177e4SLinus Torvalds 
10141c755af4SAl Viro 	mounted = lookup_mnt(path);
10151da177e4SLinus Torvalds 	if (mounted) {
10169393bd07SAl Viro 		dput(path->dentry);
10179393bd07SAl Viro 		mntput(path->mnt);
10189393bd07SAl Viro 		path->mnt = mounted;
10199393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
10201da177e4SLinus Torvalds 		return 1;
10211da177e4SLinus Torvalds 	}
10221da177e4SLinus Torvalds 	return 0;
10231da177e4SLinus Torvalds }
10241da177e4SLinus Torvalds 
10259875cf80SDavid Howells /*
10269875cf80SDavid Howells  * Skip to top of mountpoint pile in rcuwalk mode.  We abort the rcu-walk if we
1027cc53ce53SDavid Howells  * meet a managed dentry and we're not walking to "..".  True is returned to
10289875cf80SDavid Howells  * continue, false to abort.
10299875cf80SDavid Howells  */
10309875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
10319875cf80SDavid Howells 			       struct inode **inode, bool reverse_transit)
10329875cf80SDavid Howells {
10339875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
10349875cf80SDavid Howells 		struct vfsmount *mounted;
1035ab90911fSDavid Howells 		if (unlikely(path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) &&
1036ab90911fSDavid Howells 		    !reverse_transit &&
1037ab90911fSDavid Howells 		    path->dentry->d_op->d_manage(path->dentry, false, true) < 0)
1038ab90911fSDavid Howells 			return false;
10399875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
10409875cf80SDavid Howells 		if (!mounted)
10419875cf80SDavid Howells 			break;
10429875cf80SDavid Howells 		path->mnt = mounted;
10439875cf80SDavid Howells 		path->dentry = mounted->mnt_root;
10449875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
10459875cf80SDavid Howells 		*inode = path->dentry->d_inode;
10469875cf80SDavid Howells 	}
10479875cf80SDavid Howells 
10489875cf80SDavid Howells 	if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
10499875cf80SDavid Howells 		return reverse_transit;
10509875cf80SDavid Howells 	return true;
10519875cf80SDavid Howells }
10529875cf80SDavid Howells 
105331e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
105431e6b01fSNick Piggin {
105531e6b01fSNick Piggin 	struct inode *inode = nd->inode;
105631e6b01fSNick Piggin 
105731e6b01fSNick Piggin 	set_root_rcu(nd);
105831e6b01fSNick Piggin 
105931e6b01fSNick Piggin 	while (1) {
106031e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
106131e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
106231e6b01fSNick Piggin 			break;
106331e6b01fSNick Piggin 		}
106431e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
106531e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
106631e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
106731e6b01fSNick Piggin 			unsigned seq;
106831e6b01fSNick Piggin 
106931e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
107031e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
107131e6b01fSNick Piggin 				return -ECHILD;
107231e6b01fSNick Piggin 			inode = parent->d_inode;
107331e6b01fSNick Piggin 			nd->path.dentry = parent;
107431e6b01fSNick Piggin 			nd->seq = seq;
107531e6b01fSNick Piggin 			break;
107631e6b01fSNick Piggin 		}
107731e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
107831e6b01fSNick Piggin 			break;
107931e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
108031e6b01fSNick Piggin 		inode = nd->path.dentry->d_inode;
108131e6b01fSNick Piggin 	}
10829875cf80SDavid Howells 	__follow_mount_rcu(nd, &nd->path, &inode, true);
108331e6b01fSNick Piggin 	nd->inode = inode;
108431e6b01fSNick Piggin 
108531e6b01fSNick Piggin 	return 0;
108631e6b01fSNick Piggin }
108731e6b01fSNick Piggin 
10889875cf80SDavid Howells /*
1089cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
1090cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
1091cc53ce53SDavid Howells  * caller is permitted to proceed or not.
1092cc53ce53SDavid Howells  *
1093cc53ce53SDavid Howells  * Care must be taken as namespace_sem may be held (indicated by mounting_here
1094cc53ce53SDavid Howells  * being true).
1095cc53ce53SDavid Howells  */
1096cc53ce53SDavid Howells int follow_down(struct path *path, bool mounting_here)
1097cc53ce53SDavid Howells {
1098cc53ce53SDavid Howells 	unsigned managed;
1099cc53ce53SDavid Howells 	int ret;
1100cc53ce53SDavid Howells 
1101cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
1102cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1103cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1104cc53ce53SDavid Howells 		 * being held.
1105cc53ce53SDavid Howells 		 *
1106cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
1107cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
1108cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
1109cc53ce53SDavid Howells 		 * superstructure.
1110cc53ce53SDavid Howells 		 *
1111cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
1112cc53ce53SDavid Howells 		 */
1113cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1114cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1115cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
1116ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
1117ab90911fSDavid Howells 				path->dentry, mounting_here, false);
1118cc53ce53SDavid Howells 			if (ret < 0)
1119cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
1120cc53ce53SDavid Howells 		}
1121cc53ce53SDavid Howells 
1122cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
1123cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
1124cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
1125cc53ce53SDavid Howells 			if (!mounted)
1126cc53ce53SDavid Howells 				break;
1127cc53ce53SDavid Howells 			dput(path->dentry);
1128cc53ce53SDavid Howells 			mntput(path->mnt);
1129cc53ce53SDavid Howells 			path->mnt = mounted;
1130cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1131cc53ce53SDavid Howells 			continue;
1132cc53ce53SDavid Howells 		}
1133cc53ce53SDavid Howells 
1134cc53ce53SDavid Howells 		/* Don't handle automount points here */
1135cc53ce53SDavid Howells 		break;
1136cc53ce53SDavid Howells 	}
1137cc53ce53SDavid Howells 	return 0;
1138cc53ce53SDavid Howells }
1139cc53ce53SDavid Howells 
1140cc53ce53SDavid Howells /*
11419875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
11429875cf80SDavid Howells  */
11439875cf80SDavid Howells static void follow_mount(struct path *path)
11449875cf80SDavid Howells {
11459875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
11469875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
11479875cf80SDavid Howells 		if (!mounted)
11489875cf80SDavid Howells 			break;
11499875cf80SDavid Howells 		dput(path->dentry);
11509875cf80SDavid Howells 		mntput(path->mnt);
11519875cf80SDavid Howells 		path->mnt = mounted;
11529875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
11539875cf80SDavid Howells 	}
11549875cf80SDavid Howells }
11559875cf80SDavid Howells 
115631e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
11571da177e4SLinus Torvalds {
11582a737871SAl Viro 	set_root(nd);
1159e518ddb7SAndreas Mohr 
11601da177e4SLinus Torvalds 	while(1) {
11614ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
11621da177e4SLinus Torvalds 
11632a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
11642a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
11651da177e4SLinus Torvalds 			break;
11661da177e4SLinus Torvalds 		}
11674ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
11683088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
11693088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
11701da177e4SLinus Torvalds 			dput(old);
11711da177e4SLinus Torvalds 			break;
11721da177e4SLinus Torvalds 		}
11733088dd70SAl Viro 		if (!follow_up(&nd->path))
11741da177e4SLinus Torvalds 			break;
11751da177e4SLinus Torvalds 	}
117679ed0226SAl Viro 	follow_mount(&nd->path);
117731e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
11781da177e4SLinus Torvalds }
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds /*
1181baa03890SNick Piggin  * Allocate a dentry with name and parent, and perform a parent
1182baa03890SNick Piggin  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1183baa03890SNick Piggin  * on error. parent->d_inode->i_mutex must be held. d_lookup must
1184baa03890SNick Piggin  * have verified that no child exists while under i_mutex.
1185baa03890SNick Piggin  */
1186baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1187baa03890SNick Piggin 				struct qstr *name, struct nameidata *nd)
1188baa03890SNick Piggin {
1189baa03890SNick Piggin 	struct inode *inode = parent->d_inode;
1190baa03890SNick Piggin 	struct dentry *dentry;
1191baa03890SNick Piggin 	struct dentry *old;
1192baa03890SNick Piggin 
1193baa03890SNick Piggin 	/* Don't create child dentry for a dead directory. */
1194baa03890SNick Piggin 	if (unlikely(IS_DEADDIR(inode)))
1195baa03890SNick Piggin 		return ERR_PTR(-ENOENT);
1196baa03890SNick Piggin 
1197baa03890SNick Piggin 	dentry = d_alloc(parent, name);
1198baa03890SNick Piggin 	if (unlikely(!dentry))
1199baa03890SNick Piggin 		return ERR_PTR(-ENOMEM);
1200baa03890SNick Piggin 
1201baa03890SNick Piggin 	old = inode->i_op->lookup(inode, dentry, nd);
1202baa03890SNick Piggin 	if (unlikely(old)) {
1203baa03890SNick Piggin 		dput(dentry);
1204baa03890SNick Piggin 		dentry = old;
1205baa03890SNick Piggin 	}
1206baa03890SNick Piggin 	return dentry;
1207baa03890SNick Piggin }
1208baa03890SNick Piggin 
1209baa03890SNick Piggin /*
12101da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
12111da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
12121da177e4SLinus Torvalds  *  It _is_ time-critical.
12131da177e4SLinus Torvalds  */
12141da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
121531e6b01fSNick Piggin 			struct path *path, struct inode **inode)
12161da177e4SLinus Torvalds {
12174ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
121831e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
12196e6b1bd1SAl Viro 	struct inode *dir;
12209875cf80SDavid Howells 	int err;
12219875cf80SDavid Howells 
12223cac260aSAl Viro 	/*
12233cac260aSAl Viro 	 * See if the low-level filesystem might want
12243cac260aSAl Viro 	 * to use its own hash..
12253cac260aSAl Viro 	 */
1226fb045adbSNick Piggin 	if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
12279875cf80SDavid Howells 		err = parent->d_op->d_hash(parent, nd->inode, name);
12283cac260aSAl Viro 		if (err < 0)
12293cac260aSAl Viro 			return err;
12303cac260aSAl Viro 	}
12311da177e4SLinus Torvalds 
1232b04f784eSNick Piggin 	/*
1233b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1234b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1235b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1236b04f784eSNick Piggin 	 */
123731e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
123831e6b01fSNick Piggin 		unsigned seq;
123931e6b01fSNick Piggin 
124031e6b01fSNick Piggin 		*inode = nd->inode;
124131e6b01fSNick Piggin 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
124231e6b01fSNick Piggin 		if (!dentry) {
124331e6b01fSNick Piggin 			if (nameidata_drop_rcu(nd))
124431e6b01fSNick Piggin 				return -ECHILD;
124531e6b01fSNick Piggin 			goto need_lookup;
124631e6b01fSNick Piggin 		}
124731e6b01fSNick Piggin 		/* Memory barrier in read_seqcount_begin of child is enough */
124831e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
124931e6b01fSNick Piggin 			return -ECHILD;
125031e6b01fSNick Piggin 
125131e6b01fSNick Piggin 		nd->seq = seq;
125224643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1253f5e1c1c1SAl Viro 			dentry = do_revalidate_rcu(dentry, nd);
125424643087SAl Viro 			if (!dentry)
125524643087SAl Viro 				goto need_lookup;
125624643087SAl Viro 			if (IS_ERR(dentry))
125724643087SAl Viro 				goto fail;
125824643087SAl Viro 			if (!(nd->flags & LOOKUP_RCU))
125924643087SAl Viro 				goto done;
126024643087SAl Viro 		}
126131e6b01fSNick Piggin 		path->mnt = mnt;
126231e6b01fSNick Piggin 		path->dentry = dentry;
12639875cf80SDavid Howells 		if (likely(__follow_mount_rcu(nd, path, inode, false)))
12649875cf80SDavid Howells 			return 0;
12659875cf80SDavid Howells 		if (nameidata_drop_rcu(nd))
12669875cf80SDavid Howells 			return -ECHILD;
12679875cf80SDavid Howells 		/* fallthru */
12689875cf80SDavid Howells 	}
126931e6b01fSNick Piggin 	dentry = __d_lookup(parent, name);
12701da177e4SLinus Torvalds 	if (!dentry)
12711da177e4SLinus Torvalds 		goto need_lookup;
12722e2e88eaSNick Piggin found:
127324643087SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
127424643087SAl Viro 		dentry = do_revalidate(dentry, nd);
127524643087SAl Viro 		if (!dentry)
127624643087SAl Viro 			goto need_lookup;
127724643087SAl Viro 		if (IS_ERR(dentry))
127824643087SAl Viro 			goto fail;
127924643087SAl Viro 	}
12801da177e4SLinus Torvalds done:
12811da177e4SLinus Torvalds 	path->mnt = mnt;
12821da177e4SLinus Torvalds 	path->dentry = dentry;
12839875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
128489312214SIan Kent 	if (unlikely(err < 0)) {
128589312214SIan Kent 		path_put_conditional(path, nd);
12869875cf80SDavid Howells 		return err;
128789312214SIan Kent 	}
128831e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
12891da177e4SLinus Torvalds 	return 0;
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds need_lookup:
12926e6b1bd1SAl Viro 	dir = parent->d_inode;
129331e6b01fSNick Piggin 	BUG_ON(nd->inode != dir);
12946e6b1bd1SAl Viro 
12956e6b1bd1SAl Viro 	mutex_lock(&dir->i_mutex);
12966e6b1bd1SAl Viro 	/*
12976e6b1bd1SAl Viro 	 * First re-do the cached lookup just in case it was created
1298b04f784eSNick Piggin 	 * while we waited for the directory semaphore, or the first
1299b04f784eSNick Piggin 	 * lookup failed due to an unrelated rename.
13006e6b1bd1SAl Viro 	 *
1301b04f784eSNick Piggin 	 * This could use version numbering or similar to avoid unnecessary
1302b04f784eSNick Piggin 	 * cache lookups, but then we'd have to do the first lookup in the
1303b04f784eSNick Piggin 	 * non-racy way. However in the common case here, everything should
1304b04f784eSNick Piggin 	 * be hot in cache, so would it be a big win?
13056e6b1bd1SAl Viro 	 */
13066e6b1bd1SAl Viro 	dentry = d_lookup(parent, name);
1307baa03890SNick Piggin 	if (likely(!dentry)) {
1308baa03890SNick Piggin 		dentry = d_alloc_and_lookup(parent, name, nd);
13096e6b1bd1SAl Viro 		mutex_unlock(&dir->i_mutex);
13106e6b1bd1SAl Viro 		if (IS_ERR(dentry))
13116e6b1bd1SAl Viro 			goto fail;
13126e6b1bd1SAl Viro 		goto done;
13136e6b1bd1SAl Viro 	}
13146e6b1bd1SAl Viro 	/*
13156e6b1bd1SAl Viro 	 * Uhhuh! Nasty case: the cache was re-populated while
13166e6b1bd1SAl Viro 	 * we waited on the semaphore. Need to revalidate.
13176e6b1bd1SAl Viro 	 */
13186e6b1bd1SAl Viro 	mutex_unlock(&dir->i_mutex);
13192e2e88eaSNick Piggin 	goto found;
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds fail:
13221da177e4SLinus Torvalds 	return PTR_ERR(dentry);
13231da177e4SLinus Torvalds }
13241da177e4SLinus Torvalds 
132552094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
132652094c8aSAl Viro {
132752094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
132852094c8aSAl Viro 		int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
132952094c8aSAl Viro 		if (err != -ECHILD)
133052094c8aSAl Viro 			return err;
133152094c8aSAl Viro 		if (nameidata_drop_rcu(nd))
133252094c8aSAl Viro 			return -ECHILD;
133352094c8aSAl Viro 	}
133452094c8aSAl Viro 	return exec_permission(nd->inode, 0);
133552094c8aSAl Viro }
133652094c8aSAl Viro 
13371da177e4SLinus Torvalds /*
13381da177e4SLinus Torvalds  * Name resolution.
1339ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1340ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
13411da177e4SLinus Torvalds  *
1342ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1343ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
13441da177e4SLinus Torvalds  */
13456de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
13461da177e4SLinus Torvalds {
13471da177e4SLinus Torvalds 	struct path next;
13481da177e4SLinus Torvalds 	int err;
13491da177e4SLinus Torvalds 	unsigned int lookup_flags = nd->flags;
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 	while (*name=='/')
13521da177e4SLinus Torvalds 		name++;
13531da177e4SLinus Torvalds 	if (!*name)
13541da177e4SLinus Torvalds 		goto return_reval;
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds 	if (nd->depth)
1357f55eab82STrond Myklebust 		lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
13601da177e4SLinus Torvalds 	for(;;) {
136131e6b01fSNick Piggin 		struct inode *inode;
13621da177e4SLinus Torvalds 		unsigned long hash;
13631da177e4SLinus Torvalds 		struct qstr this;
13641da177e4SLinus Torvalds 		unsigned int c;
1365fe479a58SAl Viro 		int type;
13661da177e4SLinus Torvalds 
1367cdce5d6bSTrond Myklebust 		nd->flags |= LOOKUP_CONTINUE;
136852094c8aSAl Viro 
136952094c8aSAl Viro 		err = may_lookup(nd);
13701da177e4SLinus Torvalds  		if (err)
13711da177e4SLinus Torvalds 			break;
13721da177e4SLinus Torvalds 
13731da177e4SLinus Torvalds 		this.name = name;
13741da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 		hash = init_name_hash();
13771da177e4SLinus Torvalds 		do {
13781da177e4SLinus Torvalds 			name++;
13791da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
13801da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
13811da177e4SLinus Torvalds 		} while (c && (c != '/'));
13821da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
13831da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
13841da177e4SLinus Torvalds 
1385fe479a58SAl Viro 		type = LAST_NORM;
1386fe479a58SAl Viro 		if (this.name[0] == '.') switch (this.len) {
1387fe479a58SAl Viro 			case 2:
1388fe479a58SAl Viro 				if (this.name[1] == '.')
1389fe479a58SAl Viro 					type = LAST_DOTDOT;
1390fe479a58SAl Viro 				break;
1391fe479a58SAl Viro 			case 1:
1392fe479a58SAl Viro 				type = LAST_DOT;
1393fe479a58SAl Viro 		}
1394fe479a58SAl Viro 
13951da177e4SLinus Torvalds 		/* remove trailing slashes? */
13961da177e4SLinus Torvalds 		if (!c)
13971da177e4SLinus Torvalds 			goto last_component;
13981da177e4SLinus Torvalds 		while (*++name == '/');
13991da177e4SLinus Torvalds 		if (!*name)
14001da177e4SLinus Torvalds 			goto last_with_slashes;
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds 		/*
14031da177e4SLinus Torvalds 		 * "." and ".." are special - ".." especially so because it has
14041da177e4SLinus Torvalds 		 * to be able to know about the current root directory and
14051da177e4SLinus Torvalds 		 * parent relationships.
14061da177e4SLinus Torvalds 		 */
1407fe479a58SAl Viro 		if (unlikely(type != LAST_NORM)) {
1408fe479a58SAl Viro 			if (type == LAST_DOTDOT) {
140931e6b01fSNick Piggin 				if (nd->flags & LOOKUP_RCU) {
141031e6b01fSNick Piggin 					if (follow_dotdot_rcu(nd))
141131e6b01fSNick Piggin 						return -ECHILD;
141231e6b01fSNick Piggin 				} else
141358c465ebSAl Viro 					follow_dotdot(nd);
1414fe479a58SAl Viro 			}
14151da177e4SLinus Torvalds 			continue;
14161da177e4SLinus Torvalds 		}
1417fe479a58SAl Viro 
14181da177e4SLinus Torvalds 		/* This does the actual lookups.. */
141931e6b01fSNick Piggin 		err = do_lookup(nd, &this, &next, &inode);
14201da177e4SLinus Torvalds 		if (err)
14211da177e4SLinus Torvalds 			break;
14221da177e4SLinus Torvalds 		err = -ENOENT;
14231da177e4SLinus Torvalds 		if (!inode)
14241da177e4SLinus Torvalds 			goto out_dput;
14251da177e4SLinus Torvalds 
14261da177e4SLinus Torvalds 		if (inode->i_op->follow_link) {
14273abb17e8SLinus Torvalds 			err = do_follow_link(inode, &next, nd);
14281da177e4SLinus Torvalds 			if (err)
14291da177e4SLinus Torvalds 				goto return_err;
143031e6b01fSNick Piggin 			nd->inode = nd->path.dentry->d_inode;
14311da177e4SLinus Torvalds 			err = -ENOENT;
143231e6b01fSNick Piggin 			if (!nd->inode)
14331da177e4SLinus Torvalds 				break;
143431e6b01fSNick Piggin 		} else {
143509dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
143631e6b01fSNick Piggin 			nd->inode = inode;
143731e6b01fSNick Piggin 		}
14381da177e4SLinus Torvalds 		err = -ENOTDIR;
143931e6b01fSNick Piggin 		if (!nd->inode->i_op->lookup)
14401da177e4SLinus Torvalds 			break;
14411da177e4SLinus Torvalds 		continue;
14421da177e4SLinus Torvalds 		/* here ends the main loop */
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds last_with_slashes:
14451da177e4SLinus Torvalds 		lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
14461da177e4SLinus Torvalds last_component:
1447f55eab82STrond Myklebust 		/* Clear LOOKUP_CONTINUE iff it was previously unset */
1448f55eab82STrond Myklebust 		nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
14491da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_PARENT)
14501da177e4SLinus Torvalds 			goto lookup_parent;
1451fe479a58SAl Viro 		if (unlikely(type != LAST_NORM)) {
1452fe479a58SAl Viro 			if (type == LAST_DOTDOT) {
145331e6b01fSNick Piggin 				if (nd->flags & LOOKUP_RCU) {
145431e6b01fSNick Piggin 					if (follow_dotdot_rcu(nd))
145531e6b01fSNick Piggin 						return -ECHILD;
145631e6b01fSNick Piggin 				} else
145758c465ebSAl Viro 					follow_dotdot(nd);
1458fe479a58SAl Viro 			}
14591da177e4SLinus Torvalds 			goto return_reval;
14601da177e4SLinus Torvalds 		}
146131e6b01fSNick Piggin 		err = do_lookup(nd, &this, &next, &inode);
14621da177e4SLinus Torvalds 		if (err)
14631da177e4SLinus Torvalds 			break;
1464db372915SDavid Howells 		if (inode && unlikely(inode->i_op->follow_link) &&
1465db372915SDavid Howells 		    (lookup_flags & LOOKUP_FOLLOW)) {
14663abb17e8SLinus Torvalds 			err = do_follow_link(inode, &next, nd);
14671da177e4SLinus Torvalds 			if (err)
14681da177e4SLinus Torvalds 				goto return_err;
146931e6b01fSNick Piggin 			nd->inode = nd->path.dentry->d_inode;
147031e6b01fSNick Piggin 		} else {
147109dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
147231e6b01fSNick Piggin 			nd->inode = inode;
147331e6b01fSNick Piggin 		}
14741da177e4SLinus Torvalds 		err = -ENOENT;
147531e6b01fSNick Piggin 		if (!nd->inode)
14761da177e4SLinus Torvalds 			break;
14771da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_DIRECTORY) {
14781da177e4SLinus Torvalds 			err = -ENOTDIR;
147931e6b01fSNick Piggin 			if (!nd->inode->i_op->lookup)
14801da177e4SLinus Torvalds 				break;
14811da177e4SLinus Torvalds 		}
14821da177e4SLinus Torvalds 		goto return_base;
14831da177e4SLinus Torvalds lookup_parent:
14841da177e4SLinus Torvalds 		nd->last = this;
1485fe479a58SAl Viro 		nd->last_type = type;
1486fe479a58SAl Viro 		if (type == LAST_NORM)
14871da177e4SLinus Torvalds 			goto return_base;
14881da177e4SLinus Torvalds return_reval:
14891da177e4SLinus Torvalds 		/*
14901da177e4SLinus Torvalds 		 * We bypassed the ordinary revalidation routines.
14911da177e4SLinus Torvalds 		 * We may need to check the cached dentry for staleness.
14921da177e4SLinus Torvalds 		 */
1493fb045adbSNick Piggin 		if (need_reval_dot(nd->path.dentry)) {
1494f60aef7eSAl Viro 			if (nameidata_drop_rcu_last_maybe(nd))
1495f60aef7eSAl Viro 				return -ECHILD;
14961da177e4SLinus Torvalds 			/* Note: we do not d_invalidate() */
149734286d66SNick Piggin 			err = d_revalidate(nd->path.dentry, nd);
149834286d66SNick Piggin 			if (!err)
149934286d66SNick Piggin 				err = -ESTALE;
150034286d66SNick Piggin 			if (err < 0)
15011da177e4SLinus Torvalds 				break;
1502f60aef7eSAl Viro 			return 0;
15031da177e4SLinus Torvalds 		}
15041da177e4SLinus Torvalds return_base:
150531e6b01fSNick Piggin 		if (nameidata_drop_rcu_last_maybe(nd))
150631e6b01fSNick Piggin 			return -ECHILD;
15071da177e4SLinus Torvalds 		return 0;
15081da177e4SLinus Torvalds out_dput:
150931e6b01fSNick Piggin 		if (!(nd->flags & LOOKUP_RCU))
15101d957f9bSJan Blunck 			path_put_conditional(&next, nd);
15111da177e4SLinus Torvalds 		break;
15121da177e4SLinus Torvalds 	}
151331e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU))
15141d957f9bSJan Blunck 		path_put(&nd->path);
15151da177e4SLinus Torvalds return_err:
15161da177e4SLinus Torvalds 	return err;
15171da177e4SLinus Torvalds }
15181da177e4SLinus Torvalds 
1519e41f7d4eSAl Viro static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
152031e6b01fSNick Piggin {
152131e6b01fSNick Piggin 	int retval = 0;
152231e6b01fSNick Piggin 	int fput_needed;
152331e6b01fSNick Piggin 	struct file *file;
152431e6b01fSNick Piggin 
152531e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
1526e41f7d4eSAl Viro 	nd->flags = flags;
152731e6b01fSNick Piggin 	nd->depth = 0;
152831e6b01fSNick Piggin 	nd->root.mnt = NULL;
152931e6b01fSNick Piggin 	nd->file = NULL;
153031e6b01fSNick Piggin 
153131e6b01fSNick Piggin 	if (*name=='/') {
1532e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
153331e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
153431e6b01fSNick Piggin 			rcu_read_lock();
1535e41f7d4eSAl Viro 			set_root_rcu(nd);
1536e41f7d4eSAl Viro 		} else {
1537e41f7d4eSAl Viro 			set_root(nd);
1538e41f7d4eSAl Viro 			path_get(&nd->root);
1539e41f7d4eSAl Viro 		}
154031e6b01fSNick Piggin 		nd->path = nd->root;
154131e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1542e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
154331e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1544c28cc364SNick Piggin 			unsigned seq;
154531e6b01fSNick Piggin 
154631e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
154731e6b01fSNick Piggin 			rcu_read_lock();
154831e6b01fSNick Piggin 
1549c28cc364SNick Piggin 			do {
1550c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
155131e6b01fSNick Piggin 				nd->path = fs->pwd;
1552c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1553c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1554e41f7d4eSAl Viro 		} else {
1555e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1556e41f7d4eSAl Viro 		}
155731e6b01fSNick Piggin 	} else {
155831e6b01fSNick Piggin 		struct dentry *dentry;
155931e6b01fSNick Piggin 
156031e6b01fSNick Piggin 		file = fget_light(dfd, &fput_needed);
156131e6b01fSNick Piggin 		retval = -EBADF;
156231e6b01fSNick Piggin 		if (!file)
156331e6b01fSNick Piggin 			goto out_fail;
156431e6b01fSNick Piggin 
156531e6b01fSNick Piggin 		dentry = file->f_path.dentry;
156631e6b01fSNick Piggin 
156731e6b01fSNick Piggin 		retval = -ENOTDIR;
156831e6b01fSNick Piggin 		if (!S_ISDIR(dentry->d_inode->i_mode))
156931e6b01fSNick Piggin 			goto fput_fail;
157031e6b01fSNick Piggin 
157131e6b01fSNick Piggin 		retval = file_permission(file, MAY_EXEC);
157231e6b01fSNick Piggin 		if (retval)
157331e6b01fSNick Piggin 			goto fput_fail;
157431e6b01fSNick Piggin 
157531e6b01fSNick Piggin 		nd->path = file->f_path;
1576e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
157731e6b01fSNick Piggin 			if (fput_needed)
157831e6b01fSNick Piggin 				nd->file = file;
1579c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
158031e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
158131e6b01fSNick Piggin 			rcu_read_lock();
15825590ff0dSUlrich Drepper 		} else {
15835dd784d0SJan Blunck 			path_get(&file->f_path);
15845590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
15851da177e4SLinus Torvalds 		}
1586e41f7d4eSAl Viro 	}
1587e41f7d4eSAl Viro 
158831e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
15899b4a9b14SAl Viro 	return 0;
15902dfdd266SJosef 'Jeff' Sipek 
15919b4a9b14SAl Viro fput_fail:
15929b4a9b14SAl Viro 	fput_light(file, fput_needed);
15939b4a9b14SAl Viro out_fail:
15949b4a9b14SAl Viro 	return retval;
15959b4a9b14SAl Viro }
15969b4a9b14SAl Viro 
15979b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1598ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
15999b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
16009b4a9b14SAl Viro {
160131e6b01fSNick Piggin 	int retval;
160231e6b01fSNick Piggin 
160331e6b01fSNick Piggin 	/*
160431e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
160531e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
160631e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
160731e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
160831e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
160931e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
161031e6b01fSNick Piggin 	 * analogue, foo_rcu().
161131e6b01fSNick Piggin 	 *
161231e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
161331e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
161431e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
161531e6b01fSNick Piggin 	 * be able to complete).
161631e6b01fSNick Piggin 	 */
1617ee0827cdSAl Viro 	retval = path_init(dfd, name, flags, nd);
1618ee0827cdSAl Viro 
161931e6b01fSNick Piggin 	if (unlikely(retval))
162031e6b01fSNick Piggin 		return retval;
1621ee0827cdSAl Viro 
1622ee0827cdSAl Viro 	current->total_link_count = 0;
1623ee0827cdSAl Viro 	retval = link_path_walk(name, nd);
1624ee0827cdSAl Viro 
1625ee0827cdSAl Viro 	if (nd->flags & LOOKUP_RCU) {
1626ee0827cdSAl Viro 		/* RCU dangling. Cancel it. */
1627ee0827cdSAl Viro 		nd->flags &= ~LOOKUP_RCU;
16282a737871SAl Viro 		nd->root.mnt = NULL;
1629ee0827cdSAl Viro 		rcu_read_unlock();
1630ee0827cdSAl Viro 		br_read_unlock(vfsmount_lock);
16312a737871SAl Viro 	}
163231e6b01fSNick Piggin 
1633ee0827cdSAl Viro 	if (nd->file) {
1634ee0827cdSAl Viro 		fput(nd->file);
1635ee0827cdSAl Viro 		nd->file = NULL;
1636ee0827cdSAl Viro 	}
1637ee0827cdSAl Viro 
163831e6b01fSNick Piggin 	if (nd->root.mnt) {
163931e6b01fSNick Piggin 		path_put(&nd->root);
164031e6b01fSNick Piggin 		nd->root.mnt = NULL;
164131e6b01fSNick Piggin 	}
1642ee0827cdSAl Viro 	return retval;
164331e6b01fSNick Piggin }
164431e6b01fSNick Piggin 
1645ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1646ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1647ee0827cdSAl Viro {
1648ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1649ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1650ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1651ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1652ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1653ee0827cdSAl Viro 
165431e6b01fSNick Piggin 	if (likely(!retval)) {
165531e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
165631e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
165731e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
165831e6b01fSNick Piggin 		}
165931e6b01fSNick Piggin 	}
1660170aa3d0SUlrich Drepper 	return retval;
16611da177e4SLinus Torvalds }
16621da177e4SLinus Torvalds 
1663c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd)
16645590ff0dSUlrich Drepper {
1665c9c6cac0SAl Viro 	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
16665590ff0dSUlrich Drepper }
16675590ff0dSUlrich Drepper 
1668d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1669d1811465SAl Viro {
1670d1811465SAl Viro 	struct nameidata nd;
1671d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1672d1811465SAl Viro 	if (!res)
1673d1811465SAl Viro 		*path = nd.path;
1674d1811465SAl Viro 	return res;
1675d1811465SAl Viro }
1676d1811465SAl Viro 
167716f18200SJosef 'Jeff' Sipek /**
167816f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
167916f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
168016f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
168116f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
168216f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
168316f18200SJosef 'Jeff' Sipek  * @nd: pointer to nameidata
168416f18200SJosef 'Jeff' Sipek  */
168516f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
168616f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
168716f18200SJosef 'Jeff' Sipek 		    struct nameidata *nd)
168816f18200SJosef 'Jeff' Sipek {
1689ee0827cdSAl Viro 	int result;
169016f18200SJosef 'Jeff' Sipek 
169116f18200SJosef 'Jeff' Sipek 	/* same as do_path_lookup */
169216f18200SJosef 'Jeff' Sipek 	nd->last_type = LAST_ROOT;
169316f18200SJosef 'Jeff' Sipek 	nd->flags = flags;
169416f18200SJosef 'Jeff' Sipek 	nd->depth = 0;
169516f18200SJosef 'Jeff' Sipek 
1696c8e7f449SJan Blunck 	nd->path.dentry = dentry;
1697c8e7f449SJan Blunck 	nd->path.mnt = mnt;
1698c8e7f449SJan Blunck 	path_get(&nd->path);
16995b857119SAl Viro 	nd->root = nd->path;
17005b857119SAl Viro 	path_get(&nd->root);
170131e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
170216f18200SJosef 'Jeff' Sipek 
1703ee0827cdSAl Viro 	current->total_link_count = 0;
1704ee0827cdSAl Viro 
1705ee0827cdSAl Viro 	result = link_path_walk(name, nd);
1706ee0827cdSAl Viro 	if (result == -ESTALE) {
1707ee0827cdSAl Viro 		/* nd->path had been dropped */
1708ee0827cdSAl Viro 		current->total_link_count = 0;
1709ee0827cdSAl Viro 		nd->path.dentry = dentry;
1710ee0827cdSAl Viro 		nd->path.mnt = mnt;
1711ee0827cdSAl Viro 		nd->inode = dentry->d_inode;
1712ee0827cdSAl Viro 		path_get(&nd->path);
1713ee0827cdSAl Viro 		nd->flags |= LOOKUP_REVAL;
1714ee0827cdSAl Viro 		result = link_path_walk(name, nd);
1715ee0827cdSAl Viro 	}
1716ee0827cdSAl Viro 	if (unlikely(!result && !audit_dummy_context() && nd->path.dentry &&
171731e6b01fSNick Piggin 				nd->inode))
17184ac91378SJan Blunck 		audit_inode(name, nd->path.dentry);
171916f18200SJosef 'Jeff' Sipek 
17202a737871SAl Viro 	path_put(&nd->root);
17212a737871SAl Viro 	nd->root.mnt = NULL;
172216f18200SJosef 'Jeff' Sipek 
1723ee0827cdSAl Viro 	return result;
172416f18200SJosef 'Jeff' Sipek }
172516f18200SJosef 'Jeff' Sipek 
1726eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1727eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
17281da177e4SLinus Torvalds {
172981fca444SChristoph Hellwig 	struct inode *inode = base->d_inode;
17301da177e4SLinus Torvalds 	struct dentry *dentry;
17311da177e4SLinus Torvalds 	int err;
17321da177e4SLinus Torvalds 
1733b74c79e9SNick Piggin 	err = exec_permission(inode, 0);
173481fca444SChristoph Hellwig 	if (err)
173581fca444SChristoph Hellwig 		return ERR_PTR(err);
17361da177e4SLinus Torvalds 
17371da177e4SLinus Torvalds 	/*
17381da177e4SLinus Torvalds 	 * See if the low-level filesystem might want
17391da177e4SLinus Torvalds 	 * to use its own hash..
17401da177e4SLinus Torvalds 	 */
1741fb045adbSNick Piggin 	if (base->d_flags & DCACHE_OP_HASH) {
1742b1e6a015SNick Piggin 		err = base->d_op->d_hash(base, inode, name);
17431da177e4SLinus Torvalds 		dentry = ERR_PTR(err);
17441da177e4SLinus Torvalds 		if (err < 0)
17451da177e4SLinus Torvalds 			goto out;
17461da177e4SLinus Torvalds 	}
17471da177e4SLinus Torvalds 
1748b04f784eSNick Piggin 	/*
1749b04f784eSNick Piggin 	 * Don't bother with __d_lookup: callers are for creat as
1750b04f784eSNick Piggin 	 * well as unlink, so a lot of the time it would cost
1751b04f784eSNick Piggin 	 * a double lookup.
17526e6b1bd1SAl Viro 	 */
17536e6b1bd1SAl Viro 	dentry = d_lookup(base, name);
17546e6b1bd1SAl Viro 
1755fb045adbSNick Piggin 	if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
17566e6b1bd1SAl Viro 		dentry = do_revalidate(dentry, nd);
17576e6b1bd1SAl Viro 
17581da177e4SLinus Torvalds 	if (!dentry)
1759baa03890SNick Piggin 		dentry = d_alloc_and_lookup(base, name, nd);
17601da177e4SLinus Torvalds out:
17611da177e4SLinus Torvalds 	return dentry;
17621da177e4SLinus Torvalds }
17631da177e4SLinus Torvalds 
1764057f6c01SJames Morris /*
1765057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1766057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1767057f6c01SJames Morris  * SMP-safe.
1768057f6c01SJames Morris  */
1769a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
17701da177e4SLinus Torvalds {
17714ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
17721da177e4SLinus Torvalds }
17731da177e4SLinus Torvalds 
1774eead1911SChristoph Hellwig static int __lookup_one_len(const char *name, struct qstr *this,
1775eead1911SChristoph Hellwig 		struct dentry *base, int len)
17761da177e4SLinus Torvalds {
17771da177e4SLinus Torvalds 	unsigned long hash;
17781da177e4SLinus Torvalds 	unsigned int c;
17791da177e4SLinus Torvalds 
1780057f6c01SJames Morris 	this->name = name;
1781057f6c01SJames Morris 	this->len = len;
17821da177e4SLinus Torvalds 	if (!len)
1783057f6c01SJames Morris 		return -EACCES;
17841da177e4SLinus Torvalds 
17851da177e4SLinus Torvalds 	hash = init_name_hash();
17861da177e4SLinus Torvalds 	while (len--) {
17871da177e4SLinus Torvalds 		c = *(const unsigned char *)name++;
17881da177e4SLinus Torvalds 		if (c == '/' || c == '\0')
1789057f6c01SJames Morris 			return -EACCES;
17901da177e4SLinus Torvalds 		hash = partial_name_hash(c, hash);
17911da177e4SLinus Torvalds 	}
1792057f6c01SJames Morris 	this->hash = end_name_hash(hash);
1793057f6c01SJames Morris 	return 0;
1794057f6c01SJames Morris }
17951da177e4SLinus Torvalds 
1796eead1911SChristoph Hellwig /**
1797a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1798eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1799eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1800eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1801eead1911SChristoph Hellwig  *
1802a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1803a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1804eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1805eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1806eead1911SChristoph Hellwig  */
1807057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1808057f6c01SJames Morris {
1809057f6c01SJames Morris 	int err;
1810057f6c01SJames Morris 	struct qstr this;
1811057f6c01SJames Morris 
18122f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
18132f9092e1SDavid Woodhouse 
1814057f6c01SJames Morris 	err = __lookup_one_len(name, &this, base, len);
1815057f6c01SJames Morris 	if (err)
1816057f6c01SJames Morris 		return ERR_PTR(err);
1817eead1911SChristoph Hellwig 
181849705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1819057f6c01SJames Morris }
1820057f6c01SJames Morris 
18212d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
18222d8f3038SAl Viro 		 struct path *path)
18231da177e4SLinus Torvalds {
18242d8f3038SAl Viro 	struct nameidata nd;
18251da177e4SLinus Torvalds 	char *tmp = getname(name);
18261da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
18271da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
18282d8f3038SAl Viro 
18292d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
18302d8f3038SAl Viro 
18312d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
18321da177e4SLinus Torvalds 		putname(tmp);
18332d8f3038SAl Viro 		if (!err)
18342d8f3038SAl Viro 			*path = nd.path;
18351da177e4SLinus Torvalds 	}
18361da177e4SLinus Torvalds 	return err;
18371da177e4SLinus Torvalds }
18381da177e4SLinus Torvalds 
18392ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
18402ad94ae6SAl Viro 			struct nameidata *nd, char **name)
18412ad94ae6SAl Viro {
18422ad94ae6SAl Viro 	char *s = getname(path);
18432ad94ae6SAl Viro 	int error;
18442ad94ae6SAl Viro 
18452ad94ae6SAl Viro 	if (IS_ERR(s))
18462ad94ae6SAl Viro 		return PTR_ERR(s);
18472ad94ae6SAl Viro 
18482ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
18492ad94ae6SAl Viro 	if (error)
18502ad94ae6SAl Viro 		putname(s);
18512ad94ae6SAl Viro 	else
18522ad94ae6SAl Viro 		*name = s;
18532ad94ae6SAl Viro 
18542ad94ae6SAl Viro 	return error;
18552ad94ae6SAl Viro }
18562ad94ae6SAl Viro 
18571da177e4SLinus Torvalds /*
18581da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
18591da177e4SLinus Torvalds  * minimal.
18601da177e4SLinus Torvalds  */
18611da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
18621da177e4SLinus Torvalds {
1863da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1864da9592edSDavid Howells 
18651da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
18661da177e4SLinus Torvalds 		return 0;
1867da9592edSDavid Howells 	if (inode->i_uid == fsuid)
18681da177e4SLinus Torvalds 		return 0;
1869da9592edSDavid Howells 	if (dir->i_uid == fsuid)
18701da177e4SLinus Torvalds 		return 0;
18711da177e4SLinus Torvalds 	return !capable(CAP_FOWNER);
18721da177e4SLinus Torvalds }
18731da177e4SLinus Torvalds 
18741da177e4SLinus Torvalds /*
18751da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
18761da177e4SLinus Torvalds  *  whether the type of victim is right.
18771da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
18781da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
18791da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
18801da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
18811da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
18821da177e4SLinus Torvalds  *	a. be owner of dir, or
18831da177e4SLinus Torvalds  *	b. be owner of victim, or
18841da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
18851da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
18861da177e4SLinus Torvalds  *     links pointing to it.
18871da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
18881da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
18891da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
18901da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
18911da177e4SLinus Torvalds  *     nfs_async_unlink().
18921da177e4SLinus Torvalds  */
1893858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
18941da177e4SLinus Torvalds {
18951da177e4SLinus Torvalds 	int error;
18961da177e4SLinus Torvalds 
18971da177e4SLinus Torvalds 	if (!victim->d_inode)
18981da177e4SLinus Torvalds 		return -ENOENT;
18991da177e4SLinus Torvalds 
19001da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1901cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
19021da177e4SLinus Torvalds 
1903f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
19041da177e4SLinus Torvalds 	if (error)
19051da177e4SLinus Torvalds 		return error;
19061da177e4SLinus Torvalds 	if (IS_APPEND(dir))
19071da177e4SLinus Torvalds 		return -EPERM;
19081da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1909f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
19101da177e4SLinus Torvalds 		return -EPERM;
19111da177e4SLinus Torvalds 	if (isdir) {
19121da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
19131da177e4SLinus Torvalds 			return -ENOTDIR;
19141da177e4SLinus Torvalds 		if (IS_ROOT(victim))
19151da177e4SLinus Torvalds 			return -EBUSY;
19161da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
19171da177e4SLinus Torvalds 		return -EISDIR;
19181da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
19191da177e4SLinus Torvalds 		return -ENOENT;
19201da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
19211da177e4SLinus Torvalds 		return -EBUSY;
19221da177e4SLinus Torvalds 	return 0;
19231da177e4SLinus Torvalds }
19241da177e4SLinus Torvalds 
19251da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
19261da177e4SLinus Torvalds  *  dir.
19271da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
19281da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
19291da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
19301da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
19311da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
19321da177e4SLinus Torvalds  */
1933a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
19341da177e4SLinus Torvalds {
19351da177e4SLinus Torvalds 	if (child->d_inode)
19361da177e4SLinus Torvalds 		return -EEXIST;
19371da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
19381da177e4SLinus Torvalds 		return -ENOENT;
1939f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
19401da177e4SLinus Torvalds }
19411da177e4SLinus Torvalds 
19421da177e4SLinus Torvalds /*
19431da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
19441da177e4SLinus Torvalds  */
19451da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
19461da177e4SLinus Torvalds {
19471da177e4SLinus Torvalds 	struct dentry *p;
19481da177e4SLinus Torvalds 
19491da177e4SLinus Torvalds 	if (p1 == p2) {
1950f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
19511da177e4SLinus Torvalds 		return NULL;
19521da177e4SLinus Torvalds 	}
19531da177e4SLinus Torvalds 
1954a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19551da177e4SLinus Torvalds 
1956e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1957e2761a11SOGAWA Hirofumi 	if (p) {
1958f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1959f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
19601da177e4SLinus Torvalds 		return p;
19611da177e4SLinus Torvalds 	}
19621da177e4SLinus Torvalds 
1963e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1964e2761a11SOGAWA Hirofumi 	if (p) {
1965f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1966f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19671da177e4SLinus Torvalds 		return p;
19681da177e4SLinus Torvalds 	}
19691da177e4SLinus Torvalds 
1970f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1971f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19721da177e4SLinus Torvalds 	return NULL;
19731da177e4SLinus Torvalds }
19741da177e4SLinus Torvalds 
19751da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
19761da177e4SLinus Torvalds {
19771b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
19781da177e4SLinus Torvalds 	if (p1 != p2) {
19791b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1980a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19811da177e4SLinus Torvalds 	}
19821da177e4SLinus Torvalds }
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
19851da177e4SLinus Torvalds 		struct nameidata *nd)
19861da177e4SLinus Torvalds {
1987a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
19881da177e4SLinus Torvalds 
19891da177e4SLinus Torvalds 	if (error)
19901da177e4SLinus Torvalds 		return error;
19911da177e4SLinus Torvalds 
1992acfa4380SAl Viro 	if (!dir->i_op->create)
19931da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
19941da177e4SLinus Torvalds 	mode &= S_IALLUGO;
19951da177e4SLinus Torvalds 	mode |= S_IFREG;
19961da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
19971da177e4SLinus Torvalds 	if (error)
19981da177e4SLinus Torvalds 		return error;
19991da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
2000a74574aaSStephen Smalley 	if (!error)
2001f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
20021da177e4SLinus Torvalds 	return error;
20031da177e4SLinus Torvalds }
20041da177e4SLinus Torvalds 
20053fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag)
20061da177e4SLinus Torvalds {
20073fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
20081da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
20091da177e4SLinus Torvalds 	int error;
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds 	if (!inode)
20121da177e4SLinus Torvalds 		return -ENOENT;
20131da177e4SLinus Torvalds 
2014c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2015c8fe8f30SChristoph Hellwig 	case S_IFLNK:
20161da177e4SLinus Torvalds 		return -ELOOP;
2017c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2018c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
20191da177e4SLinus Torvalds 			return -EISDIR;
2020c8fe8f30SChristoph Hellwig 		break;
2021c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2022c8fe8f30SChristoph Hellwig 	case S_IFCHR:
20233fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
20241da177e4SLinus Torvalds 			return -EACCES;
2025c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
2026c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2027c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
20281da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2029c8fe8f30SChristoph Hellwig 		break;
20304a3fd211SDave Hansen 	}
2031b41572e9SDave Hansen 
20323fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2033b41572e9SDave Hansen 	if (error)
2034b41572e9SDave Hansen 		return error;
20356146f0d5SMimi Zohar 
20361da177e4SLinus Torvalds 	/*
20371da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
20381da177e4SLinus Torvalds 	 */
20391da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
20408737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
20417715b521SAl Viro 			return -EPERM;
20421da177e4SLinus Torvalds 		if (flag & O_TRUNC)
20437715b521SAl Viro 			return -EPERM;
20441da177e4SLinus Torvalds 	}
20451da177e4SLinus Torvalds 
20461da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
20477715b521SAl Viro 	if (flag & O_NOATIME && !is_owner_or_cap(inode))
20487715b521SAl Viro 		return -EPERM;
20491da177e4SLinus Torvalds 
20501da177e4SLinus Torvalds 	/*
20511da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
20521da177e4SLinus Torvalds 	 */
2053b65a9cfcSAl Viro 	return break_lease(inode, flag);
20547715b521SAl Viro }
20557715b521SAl Viro 
2056e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
20577715b521SAl Viro {
2058e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
20597715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
20607715b521SAl Viro 	int error = get_write_access(inode);
20611da177e4SLinus Torvalds 	if (error)
20627715b521SAl Viro 		return error;
20631da177e4SLinus Torvalds 	/*
20641da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
20651da177e4SLinus Torvalds 	 */
20661da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2067be6d3e56SKentaro Takeda 	if (!error)
2068ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
20691da177e4SLinus Torvalds 	if (!error) {
20707715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2071d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2072e1181ee6SJeff Layton 				    filp);
20731da177e4SLinus Torvalds 	}
20741da177e4SLinus Torvalds 	put_write_access(inode);
2075acd0c935SMimi Zohar 	return error;
20761da177e4SLinus Torvalds }
20771da177e4SLinus Torvalds 
2078d57999e1SDave Hansen /*
2079d57999e1SDave Hansen  * Be careful about ever adding any more callers of this
2080d57999e1SDave Hansen  * function.  Its flags must be in the namei format, not
2081d57999e1SDave Hansen  * what get passed to sys_open().
2082d57999e1SDave Hansen  */
2083d57999e1SDave Hansen static int __open_namei_create(struct nameidata *nd, struct path *path,
20848737c930SAl Viro 				int open_flag, int mode)
2085aab520e2SDave Hansen {
2086aab520e2SDave Hansen 	int error;
20874ac91378SJan Blunck 	struct dentry *dir = nd->path.dentry;
2088aab520e2SDave Hansen 
2089aab520e2SDave Hansen 	if (!IS_POSIXACL(dir->d_inode))
2090ce3b0f8dSAl Viro 		mode &= ~current_umask();
2091be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd->path, path->dentry, mode, 0);
2092be6d3e56SKentaro Takeda 	if (error)
2093be6d3e56SKentaro Takeda 		goto out_unlock;
2094aab520e2SDave Hansen 	error = vfs_create(dir->d_inode, path->dentry, mode, nd);
2095be6d3e56SKentaro Takeda out_unlock:
2096aab520e2SDave Hansen 	mutex_unlock(&dir->d_inode->i_mutex);
20974ac91378SJan Blunck 	dput(nd->path.dentry);
20984ac91378SJan Blunck 	nd->path.dentry = path->dentry;
209931e6b01fSNick Piggin 
2100aab520e2SDave Hansen 	if (error)
2101aab520e2SDave Hansen 		return error;
2102aab520e2SDave Hansen 	/* Don't check for write permission, don't truncate */
21038737c930SAl Viro 	return may_open(&nd->path, 0, open_flag & ~O_TRUNC);
2104aab520e2SDave Hansen }
2105aab520e2SDave Hansen 
21061da177e4SLinus Torvalds /*
2107d57999e1SDave Hansen  * Note that while the flag value (low two bits) for sys_open means:
2108d57999e1SDave Hansen  *	00 - read-only
2109d57999e1SDave Hansen  *	01 - write-only
2110d57999e1SDave Hansen  *	10 - read-write
2111d57999e1SDave Hansen  *	11 - special
2112d57999e1SDave Hansen  * it is changed into
2113d57999e1SDave Hansen  *	00 - no permissions needed
2114d57999e1SDave Hansen  *	01 - read-permission
2115d57999e1SDave Hansen  *	10 - write-permission
2116d57999e1SDave Hansen  *	11 - read-write
2117d57999e1SDave Hansen  * for the internal routines (ie open_namei()/follow_link() etc)
2118d57999e1SDave Hansen  * This is more logical, and also allows the 00 "no perm needed"
2119d57999e1SDave Hansen  * to be used for symlinks (where the permissions are checked
2120d57999e1SDave Hansen  * later).
2121d57999e1SDave Hansen  *
2122d57999e1SDave Hansen */
2123d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2124d57999e1SDave Hansen {
2125d57999e1SDave Hansen 	if ((flag+1) & O_ACCMODE)
2126d57999e1SDave Hansen 		flag++;
2127d57999e1SDave Hansen 	return flag;
2128d57999e1SDave Hansen }
2129d57999e1SDave Hansen 
21307715b521SAl Viro static int open_will_truncate(int flag, struct inode *inode)
21314a3fd211SDave Hansen {
2132d57999e1SDave Hansen 	/*
21334a3fd211SDave Hansen 	 * We'll never write to the fs underlying
21344a3fd211SDave Hansen 	 * a device file.
21354a3fd211SDave Hansen 	 */
21364a3fd211SDave Hansen 	if (special_file(inode->i_mode))
21374a3fd211SDave Hansen 		return 0;
21384a3fd211SDave Hansen 	return (flag & O_TRUNC);
21394a3fd211SDave Hansen }
21404a3fd211SDave Hansen 
2141648fa861SAl Viro static struct file *finish_open(struct nameidata *nd,
21429a66179eSAl Viro 				int open_flag, int acc_mode)
2143648fa861SAl Viro {
2144648fa861SAl Viro 	struct file *filp;
2145648fa861SAl Viro 	int will_truncate;
2146648fa861SAl Viro 	int error;
2147648fa861SAl Viro 
21489a66179eSAl Viro 	will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode);
2149648fa861SAl Viro 	if (will_truncate) {
2150648fa861SAl Viro 		error = mnt_want_write(nd->path.mnt);
2151648fa861SAl Viro 		if (error)
2152648fa861SAl Viro 			goto exit;
2153648fa861SAl Viro 	}
2154648fa861SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2155648fa861SAl Viro 	if (error) {
2156648fa861SAl Viro 		if (will_truncate)
2157648fa861SAl Viro 			mnt_drop_write(nd->path.mnt);
2158648fa861SAl Viro 		goto exit;
2159648fa861SAl Viro 	}
2160648fa861SAl Viro 	filp = nameidata_to_filp(nd);
2161648fa861SAl Viro 	if (!IS_ERR(filp)) {
2162648fa861SAl Viro 		error = ima_file_check(filp, acc_mode);
2163648fa861SAl Viro 		if (error) {
2164648fa861SAl Viro 			fput(filp);
2165648fa861SAl Viro 			filp = ERR_PTR(error);
2166648fa861SAl Viro 		}
2167648fa861SAl Viro 	}
2168648fa861SAl Viro 	if (!IS_ERR(filp)) {
2169648fa861SAl Viro 		if (will_truncate) {
2170e1181ee6SJeff Layton 			error = handle_truncate(filp);
2171648fa861SAl Viro 			if (error) {
2172648fa861SAl Viro 				fput(filp);
2173648fa861SAl Viro 				filp = ERR_PTR(error);
2174648fa861SAl Viro 			}
2175648fa861SAl Viro 		}
2176648fa861SAl Viro 	}
2177648fa861SAl Viro 	/*
2178648fa861SAl Viro 	 * It is now safe to drop the mnt write
2179648fa861SAl Viro 	 * because the filp has had a write taken
2180648fa861SAl Viro 	 * on its behalf.
2181648fa861SAl Viro 	 */
2182648fa861SAl Viro 	if (will_truncate)
2183648fa861SAl Viro 		mnt_drop_write(nd->path.mnt);
2184d893f1bcSAl Viro 	path_put(&nd->path);
2185648fa861SAl Viro 	return filp;
2186648fa861SAl Viro 
2187648fa861SAl Viro exit:
2188648fa861SAl Viro 	path_put(&nd->path);
2189648fa861SAl Viro 	return ERR_PTR(error);
2190648fa861SAl Viro }
2191648fa861SAl Viro 
219231e6b01fSNick Piggin /*
219331e6b01fSNick Piggin  * Handle O_CREAT case for do_filp_open
219431e6b01fSNick Piggin  */
2195fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
21965b369df8SAl Viro 			    int open_flag, int acc_mode,
21973e297b61SAl Viro 			    int mode, const char *pathname)
2198fb1cc555SAl Viro {
2199a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
2200fb1cc555SAl Viro 	struct file *filp;
22011f36f774SAl Viro 	int error = -EISDIR;
2202fb1cc555SAl Viro 
22031f36f774SAl Viro 	switch (nd->last_type) {
22041f36f774SAl Viro 	case LAST_DOTDOT:
22051f36f774SAl Viro 		follow_dotdot(nd);
22061f36f774SAl Viro 		dir = nd->path.dentry;
2207176306f5SNeil Brown 	case LAST_DOT:
2208fb045adbSNick Piggin 		if (need_reval_dot(dir)) {
2209f20877d9SJ. R. Okajima 			int status = d_revalidate(nd->path.dentry, nd);
2210f20877d9SJ. R. Okajima 			if (!status)
2211f20877d9SJ. R. Okajima 				status = -ESTALE;
2212f20877d9SJ. R. Okajima 			if (status < 0) {
2213f20877d9SJ. R. Okajima 				error = status;
2214a2c36b45SAl Viro 				goto exit;
22151f36f774SAl Viro 			}
2216f20877d9SJ. R. Okajima 		}
22171f36f774SAl Viro 		/* fallthrough */
22181f36f774SAl Viro 	case LAST_ROOT:
22191f36f774SAl Viro 		goto exit;
22201f36f774SAl Viro 	case LAST_BIND:
22211f36f774SAl Viro 		audit_inode(pathname, dir);
22221f36f774SAl Viro 		goto ok;
22231f36f774SAl Viro 	}
2224a2c36b45SAl Viro 
22251f36f774SAl Viro 	/* trailing slashes? */
222631e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
22271f36f774SAl Viro 		goto exit;
22281f36f774SAl Viro 
2229a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2230a1e28038SAl Viro 
2231a1e28038SAl Viro 	path->dentry = lookup_hash(nd);
2232a1e28038SAl Viro 	path->mnt = nd->path.mnt;
2233a1e28038SAl Viro 
2234fb1cc555SAl Viro 	error = PTR_ERR(path->dentry);
2235fb1cc555SAl Viro 	if (IS_ERR(path->dentry)) {
2236fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2237fb1cc555SAl Viro 		goto exit;
2238fb1cc555SAl Viro 	}
2239fb1cc555SAl Viro 
2240fb1cc555SAl Viro 	if (IS_ERR(nd->intent.open.file)) {
2241fb1cc555SAl Viro 		error = PTR_ERR(nd->intent.open.file);
2242fb1cc555SAl Viro 		goto exit_mutex_unlock;
2243fb1cc555SAl Viro 	}
2244fb1cc555SAl Viro 
2245fb1cc555SAl Viro 	/* Negative dentry, just create the file */
2246fb1cc555SAl Viro 	if (!path->dentry->d_inode) {
2247fb1cc555SAl Viro 		/*
2248fb1cc555SAl Viro 		 * This write is needed to ensure that a
2249fb1cc555SAl Viro 		 * ro->rw transition does not occur between
2250fb1cc555SAl Viro 		 * the time when the file is created and when
2251fb1cc555SAl Viro 		 * a permanent write count is taken through
2252fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2253fb1cc555SAl Viro 		 */
2254fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2255fb1cc555SAl Viro 		if (error)
2256fb1cc555SAl Viro 			goto exit_mutex_unlock;
2257fb1cc555SAl Viro 		error = __open_namei_create(nd, path, open_flag, mode);
2258fb1cc555SAl Viro 		if (error) {
2259fb1cc555SAl Viro 			mnt_drop_write(nd->path.mnt);
2260fb1cc555SAl Viro 			goto exit;
2261fb1cc555SAl Viro 		}
2262fb1cc555SAl Viro 		filp = nameidata_to_filp(nd);
2263fb1cc555SAl Viro 		mnt_drop_write(nd->path.mnt);
2264d893f1bcSAl Viro 		path_put(&nd->path);
2265fb1cc555SAl Viro 		if (!IS_ERR(filp)) {
2266fb1cc555SAl Viro 			error = ima_file_check(filp, acc_mode);
2267fb1cc555SAl Viro 			if (error) {
2268fb1cc555SAl Viro 				fput(filp);
2269fb1cc555SAl Viro 				filp = ERR_PTR(error);
2270fb1cc555SAl Viro 			}
2271fb1cc555SAl Viro 		}
2272fb1cc555SAl Viro 		return filp;
2273fb1cc555SAl Viro 	}
2274fb1cc555SAl Viro 
2275fb1cc555SAl Viro 	/*
2276fb1cc555SAl Viro 	 * It already exists.
2277fb1cc555SAl Viro 	 */
2278fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2279fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2280fb1cc555SAl Viro 
2281fb1cc555SAl Viro 	error = -EEXIST;
22825b369df8SAl Viro 	if (open_flag & O_EXCL)
2283fb1cc555SAl Viro 		goto exit_dput;
2284fb1cc555SAl Viro 
22859875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
22869875cf80SDavid Howells 	if (error < 0)
2287fb1cc555SAl Viro 		goto exit_dput;
2288fb1cc555SAl Viro 
2289fb1cc555SAl Viro 	error = -ENOENT;
2290fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2291fb1cc555SAl Viro 		goto exit_dput;
22929e67f361SAl Viro 
22939e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2294fb1cc555SAl Viro 		return NULL;
2295fb1cc555SAl Viro 
2296fb1cc555SAl Viro 	path_to_nameidata(path, nd);
229731e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2298fb1cc555SAl Viro 	error = -EISDIR;
229931e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2300fb1cc555SAl Viro 		goto exit;
230167ee3ad2SAl Viro ok:
23029a66179eSAl Viro 	filp = finish_open(nd, open_flag, acc_mode);
2303fb1cc555SAl Viro 	return filp;
2304fb1cc555SAl Viro 
2305fb1cc555SAl Viro exit_mutex_unlock:
2306fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2307fb1cc555SAl Viro exit_dput:
2308fb1cc555SAl Viro 	path_put_conditional(path, nd);
2309fb1cc555SAl Viro exit:
2310fb1cc555SAl Viro 	path_put(&nd->path);
2311fb1cc555SAl Viro 	return ERR_PTR(error);
2312fb1cc555SAl Viro }
2313fb1cc555SAl Viro 
23144a3fd211SDave Hansen /*
23154a3fd211SDave Hansen  * Note that the low bits of the passed in "open_flag"
23164a3fd211SDave Hansen  * are not the same as in the local variable "flag". See
23174a3fd211SDave Hansen  * open_to_namei_flags() for more details.
23181da177e4SLinus Torvalds  */
2319a70e65dfSChristoph Hellwig struct file *do_filp_open(int dfd, const char *pathname,
23206e8341a1SAl Viro 		int open_flag, int mode, int acc_mode)
23211da177e4SLinus Torvalds {
23224a3fd211SDave Hansen 	struct file *filp;
2323a70e65dfSChristoph Hellwig 	struct nameidata nd;
23246e8341a1SAl Viro 	int error;
23259850c056SAl Viro 	struct path path;
23261da177e4SLinus Torvalds 	int count = 0;
2327d57999e1SDave Hansen 	int flag = open_to_namei_flags(open_flag);
232831e6b01fSNick Piggin 	int flags;
23291f36f774SAl Viro 
23301f36f774SAl Viro 	if (!(open_flag & O_CREAT))
23311f36f774SAl Viro 		mode = 0;
23321da177e4SLinus Torvalds 
2333b1085ba8SLino Sanfilippo 	/* Must never be set by userspace */
2334b1085ba8SLino Sanfilippo 	open_flag &= ~FMODE_NONOTIFY;
2335b1085ba8SLino Sanfilippo 
23366b2f3d1fSChristoph Hellwig 	/*
23376b2f3d1fSChristoph Hellwig 	 * O_SYNC is implemented as __O_SYNC|O_DSYNC.  As many places only
23386b2f3d1fSChristoph Hellwig 	 * check for O_DSYNC if the need any syncing at all we enforce it's
23396b2f3d1fSChristoph Hellwig 	 * always set instead of having to deal with possibly weird behaviour
23406b2f3d1fSChristoph Hellwig 	 * for malicious applications setting only __O_SYNC.
23416b2f3d1fSChristoph Hellwig 	 */
23426b2f3d1fSChristoph Hellwig 	if (open_flag & __O_SYNC)
23436b2f3d1fSChristoph Hellwig 		open_flag |= O_DSYNC;
23446b2f3d1fSChristoph Hellwig 
23456e8341a1SAl Viro 	if (!acc_mode)
23466d125529SAl Viro 		acc_mode = MAY_OPEN | ACC_MODE(open_flag);
23471da177e4SLinus Torvalds 
2348834f2a4aSTrond Myklebust 	/* O_TRUNC implies we need access checks for write permissions */
23494296e2cbSAl Viro 	if (open_flag & O_TRUNC)
2350834f2a4aSTrond Myklebust 		acc_mode |= MAY_WRITE;
2351834f2a4aSTrond Myklebust 
23521da177e4SLinus Torvalds 	/* Allow the LSM permission hook to distinguish append
23531da177e4SLinus Torvalds 	   access from general write access. */
23544296e2cbSAl Viro 	if (open_flag & O_APPEND)
23551da177e4SLinus Torvalds 		acc_mode |= MAY_APPEND;
23561da177e4SLinus Torvalds 
235731e6b01fSNick Piggin 	flags = LOOKUP_OPEN;
235831e6b01fSNick Piggin 	if (open_flag & O_CREAT) {
235931e6b01fSNick Piggin 		flags |= LOOKUP_CREATE;
236031e6b01fSNick Piggin 		if (open_flag & O_EXCL)
236131e6b01fSNick Piggin 			flags |= LOOKUP_EXCL;
2362654f562cSJ. R. Okajima 	}
236331e6b01fSNick Piggin 	if (open_flag & O_DIRECTORY)
236431e6b01fSNick Piggin 		flags |= LOOKUP_DIRECTORY;
236531e6b01fSNick Piggin 	if (!(open_flag & O_NOFOLLOW))
236631e6b01fSNick Piggin 		flags |= LOOKUP_FOLLOW;
236731e6b01fSNick Piggin 
236831e6b01fSNick Piggin 	filp = get_empty_filp();
236931e6b01fSNick Piggin 	if (!filp)
237031e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
237131e6b01fSNick Piggin 
237231e6b01fSNick Piggin 	filp->f_flags = open_flag;
237331e6b01fSNick Piggin 	nd.intent.open.file = filp;
237431e6b01fSNick Piggin 	nd.intent.open.flags = flag;
237531e6b01fSNick Piggin 	nd.intent.open.create_mode = mode;
237631e6b01fSNick Piggin 
237731e6b01fSNick Piggin 	if (open_flag & O_CREAT)
237831e6b01fSNick Piggin 		goto creat;
237931e6b01fSNick Piggin 
238031e6b01fSNick Piggin 	/* !O_CREAT, simple open */
238131e6b01fSNick Piggin 	error = do_path_lookup(dfd, pathname, flags, &nd);
238231e6b01fSNick Piggin 	if (unlikely(error))
23831858efd4SAl Viro 		goto out_filp2;
238431e6b01fSNick Piggin 	error = -ELOOP;
238531e6b01fSNick Piggin 	if (!(nd.flags & LOOKUP_FOLLOW)) {
238631e6b01fSNick Piggin 		if (nd.inode->i_op->follow_link)
23871858efd4SAl Viro 			goto out_path2;
238831e6b01fSNick Piggin 	}
238931e6b01fSNick Piggin 	error = -ENOTDIR;
239031e6b01fSNick Piggin 	if (nd.flags & LOOKUP_DIRECTORY) {
239131e6b01fSNick Piggin 		if (!nd.inode->i_op->lookup)
23921858efd4SAl Viro 			goto out_path2;
239331e6b01fSNick Piggin 	}
239431e6b01fSNick Piggin 	audit_inode(pathname, nd.path.dentry);
239531e6b01fSNick Piggin 	filp = finish_open(&nd, open_flag, acc_mode);
23961858efd4SAl Viro out2:
23972dab5974SLinus Torvalds 	release_open_intent(&nd);
239831e6b01fSNick Piggin 	return filp;
239931e6b01fSNick Piggin 
24001858efd4SAl Viro out_path2:
24011858efd4SAl Viro 	path_put(&nd.path);
24021858efd4SAl Viro out_filp2:
24031858efd4SAl Viro 	filp = ERR_PTR(error);
24041858efd4SAl Viro 	goto out2;
24051858efd4SAl Viro 
240631e6b01fSNick Piggin creat:
240731e6b01fSNick Piggin 	/* OK, have to create the file. Find the parent. */
2408ee0827cdSAl Viro 	error = path_lookupat(dfd, pathname, LOOKUP_PARENT | LOOKUP_RCU, &nd);
2409ee0827cdSAl Viro 	if (unlikely(error == -ECHILD))
2410ee0827cdSAl Viro 		error = path_lookupat(dfd, pathname, LOOKUP_PARENT, &nd);
2411ee0827cdSAl Viro 	if (unlikely(error == -ESTALE)) {
241231e6b01fSNick Piggin reval:
241331e6b01fSNick Piggin 		flags |= LOOKUP_REVAL;
2414ee0827cdSAl Viro 		error = path_lookupat(dfd, pathname,
2415ee0827cdSAl Viro 				LOOKUP_PARENT | LOOKUP_REVAL, &nd);
241631e6b01fSNick Piggin 	}
241731e6b01fSNick Piggin 	if (unlikely(error))
241831e6b01fSNick Piggin 		goto out_filp;
241931e6b01fSNick Piggin 	if (unlikely(!audit_dummy_context()))
24209b4a9b14SAl Viro 		audit_inode(pathname, nd.path.dentry);
24211da177e4SLinus Torvalds 
24221da177e4SLinus Torvalds 	/*
2423a2c36b45SAl Viro 	 * We have the parent and last component.
24241da177e4SLinus Torvalds 	 */
242531e6b01fSNick Piggin 	nd.flags = flags;
24263e297b61SAl Viro 	filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
2427806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
24287b9337aaSNick Piggin 		struct path link = path;
24297b9337aaSNick Piggin 		struct inode *linki = link.dentry->d_inode;
2430def4af30SAl Viro 		void *cookie;
2431806b681cSAl Viro 		error = -ELOOP;
2432db372915SDavid Howells 		if (!(nd.flags & LOOKUP_FOLLOW))
24331f36f774SAl Viro 			goto exit_dput;
24341f36f774SAl Viro 		if (count++ == 32)
2435806b681cSAl Viro 			goto exit_dput;
2436806b681cSAl Viro 		/*
2437806b681cSAl Viro 		 * This is subtle. Instead of calling do_follow_link() we do
2438806b681cSAl Viro 		 * the thing by hands. The reason is that this way we have zero
2439806b681cSAl Viro 		 * link_count and path_walk() (called from ->follow_link)
2440806b681cSAl Viro 		 * honoring LOOKUP_PARENT.  After that we have the parent and
2441806b681cSAl Viro 		 * last component, i.e. we are in the same situation as after
2442806b681cSAl Viro 		 * the first path_walk().  Well, almost - if the last component
2443806b681cSAl Viro 		 * is normal we get its copy stored in nd->last.name and we will
2444806b681cSAl Viro 		 * have to putname() it when we are done. Procfs-like symlinks
2445806b681cSAl Viro 		 * just set LAST_BIND.
2446806b681cSAl Viro 		 */
2447806b681cSAl Viro 		nd.flags |= LOOKUP_PARENT;
24487b9337aaSNick Piggin 		error = security_inode_follow_link(link.dentry, &nd);
2449806b681cSAl Viro 		if (error)
2450806b681cSAl Viro 			goto exit_dput;
24517b9337aaSNick Piggin 		error = __do_follow_link(&link, &nd, &cookie);
2452def4af30SAl Viro 		if (unlikely(error)) {
24537b9337aaSNick Piggin 			if (!IS_ERR(cookie) && linki->i_op->put_link)
24547b9337aaSNick Piggin 				linki->i_op->put_link(link.dentry, &nd, cookie);
2455806b681cSAl Viro 			/* nd.path had been dropped */
24567b9337aaSNick Piggin 			nd.path = link;
245731e6b01fSNick Piggin 			goto out_path;
2458806b681cSAl Viro 		}
2459806b681cSAl Viro 		nd.flags &= ~LOOKUP_PARENT;
24603e297b61SAl Viro 		filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
24617b9337aaSNick Piggin 		if (linki->i_op->put_link)
24627b9337aaSNick Piggin 			linki->i_op->put_link(link.dentry, &nd, cookie);
24637b9337aaSNick Piggin 		path_put(&link);
2464806b681cSAl Viro 	}
246510fa8e62SAl Viro out:
24662a737871SAl Viro 	if (nd.root.mnt)
24672a737871SAl Viro 		path_put(&nd.root);
246831e6b01fSNick Piggin 	if (filp == ERR_PTR(-ESTALE) && !(flags & LOOKUP_REVAL))
246910fa8e62SAl Viro 		goto reval;
24702dab5974SLinus Torvalds 	release_open_intent(&nd);
247110fa8e62SAl Viro 	return filp;
24721da177e4SLinus Torvalds 
2473806b681cSAl Viro exit_dput:
2474806b681cSAl Viro 	path_put_conditional(&path, &nd);
247531e6b01fSNick Piggin out_path:
247631e6b01fSNick Piggin 	path_put(&nd.path);
247731e6b01fSNick Piggin out_filp:
247810fa8e62SAl Viro 	filp = ERR_PTR(error);
247910fa8e62SAl Viro 	goto out;
2480de459215SKirill Korotaev }
24811da177e4SLinus Torvalds 
24821da177e4SLinus Torvalds /**
2483a70e65dfSChristoph Hellwig  * filp_open - open file and return file pointer
2484a70e65dfSChristoph Hellwig  *
2485a70e65dfSChristoph Hellwig  * @filename:	path to open
2486a70e65dfSChristoph Hellwig  * @flags:	open flags as per the open(2) second argument
2487a70e65dfSChristoph Hellwig  * @mode:	mode for the new file if O_CREAT is set, else ignored
2488a70e65dfSChristoph Hellwig  *
2489a70e65dfSChristoph Hellwig  * This is the helper to open a file from kernelspace if you really
2490a70e65dfSChristoph Hellwig  * have to.  But in generally you should not do this, so please move
2491a70e65dfSChristoph Hellwig  * along, nothing to see here..
2492a70e65dfSChristoph Hellwig  */
2493a70e65dfSChristoph Hellwig struct file *filp_open(const char *filename, int flags, int mode)
2494a70e65dfSChristoph Hellwig {
24956e8341a1SAl Viro 	return do_filp_open(AT_FDCWD, filename, flags, mode, 0);
2496a70e65dfSChristoph Hellwig }
2497a70e65dfSChristoph Hellwig EXPORT_SYMBOL(filp_open);
2498a70e65dfSChristoph Hellwig 
2499a70e65dfSChristoph Hellwig /**
25001da177e4SLinus Torvalds  * lookup_create - lookup a dentry, creating it if it doesn't exist
25011da177e4SLinus Torvalds  * @nd: nameidata info
25021da177e4SLinus Torvalds  * @is_dir: directory flag
25031da177e4SLinus Torvalds  *
25041da177e4SLinus Torvalds  * Simple function to lookup and return a dentry and create it
25051da177e4SLinus Torvalds  * if it doesn't exist.  Is SMP-safe.
2506c663e5d8SChristoph Hellwig  *
25074ac91378SJan Blunck  * Returns with nd->path.dentry->d_inode->i_mutex locked.
25081da177e4SLinus Torvalds  */
25091da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir)
25101da177e4SLinus Torvalds {
2511c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
25121da177e4SLinus Torvalds 
25134ac91378SJan Blunck 	mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2514c663e5d8SChristoph Hellwig 	/*
2515c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2516c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2517c663e5d8SChristoph Hellwig 	 */
25181da177e4SLinus Torvalds 	if (nd->last_type != LAST_NORM)
25191da177e4SLinus Torvalds 		goto fail;
25201da177e4SLinus Torvalds 	nd->flags &= ~LOOKUP_PARENT;
25213516586aSAl Viro 	nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2522a634904aSASANO Masahiro 	nd->intent.open.flags = O_EXCL;
2523c663e5d8SChristoph Hellwig 
2524c663e5d8SChristoph Hellwig 	/*
2525c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2526c663e5d8SChristoph Hellwig 	 */
252749705b77SChristoph Hellwig 	dentry = lookup_hash(nd);
25281da177e4SLinus Torvalds 	if (IS_ERR(dentry))
25291da177e4SLinus Torvalds 		goto fail;
2530c663e5d8SChristoph Hellwig 
2531e9baf6e5SAl Viro 	if (dentry->d_inode)
2532e9baf6e5SAl Viro 		goto eexist;
2533c663e5d8SChristoph Hellwig 	/*
2534c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2535c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2536c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2537c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2538c663e5d8SChristoph Hellwig 	 */
2539e9baf6e5SAl Viro 	if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
25401da177e4SLinus Torvalds 		dput(dentry);
25411da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2542e9baf6e5SAl Viro 	}
2543e9baf6e5SAl Viro 	return dentry;
2544e9baf6e5SAl Viro eexist:
2545e9baf6e5SAl Viro 	dput(dentry);
2546e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
25471da177e4SLinus Torvalds fail:
25481da177e4SLinus Torvalds 	return dentry;
25491da177e4SLinus Torvalds }
2550f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create);
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
25531da177e4SLinus Torvalds {
2554a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
25551da177e4SLinus Torvalds 
25561da177e4SLinus Torvalds 	if (error)
25571da177e4SLinus Torvalds 		return error;
25581da177e4SLinus Torvalds 
25591da177e4SLinus Torvalds 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
25601da177e4SLinus Torvalds 		return -EPERM;
25611da177e4SLinus Torvalds 
2562acfa4380SAl Viro 	if (!dir->i_op->mknod)
25631da177e4SLinus Torvalds 		return -EPERM;
25641da177e4SLinus Torvalds 
256508ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
256608ce5f16SSerge E. Hallyn 	if (error)
256708ce5f16SSerge E. Hallyn 		return error;
256808ce5f16SSerge E. Hallyn 
25691da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
25701da177e4SLinus Torvalds 	if (error)
25711da177e4SLinus Torvalds 		return error;
25721da177e4SLinus Torvalds 
25731da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2574a74574aaSStephen Smalley 	if (!error)
2575f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
25761da177e4SLinus Torvalds 	return error;
25771da177e4SLinus Torvalds }
25781da177e4SLinus Torvalds 
2579463c3197SDave Hansen static int may_mknod(mode_t mode)
2580463c3197SDave Hansen {
2581463c3197SDave Hansen 	switch (mode & S_IFMT) {
2582463c3197SDave Hansen 	case S_IFREG:
2583463c3197SDave Hansen 	case S_IFCHR:
2584463c3197SDave Hansen 	case S_IFBLK:
2585463c3197SDave Hansen 	case S_IFIFO:
2586463c3197SDave Hansen 	case S_IFSOCK:
2587463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2588463c3197SDave Hansen 		return 0;
2589463c3197SDave Hansen 	case S_IFDIR:
2590463c3197SDave Hansen 		return -EPERM;
2591463c3197SDave Hansen 	default:
2592463c3197SDave Hansen 		return -EINVAL;
2593463c3197SDave Hansen 	}
2594463c3197SDave Hansen }
2595463c3197SDave Hansen 
25962e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
25972e4d0924SHeiko Carstens 		unsigned, dev)
25981da177e4SLinus Torvalds {
25992ad94ae6SAl Viro 	int error;
26001da177e4SLinus Torvalds 	char *tmp;
26011da177e4SLinus Torvalds 	struct dentry *dentry;
26021da177e4SLinus Torvalds 	struct nameidata nd;
26031da177e4SLinus Torvalds 
26041da177e4SLinus Torvalds 	if (S_ISDIR(mode))
26051da177e4SLinus Torvalds 		return -EPERM;
26061da177e4SLinus Torvalds 
26072ad94ae6SAl Viro 	error = user_path_parent(dfd, filename, &nd, &tmp);
26081da177e4SLinus Torvalds 	if (error)
26092ad94ae6SAl Viro 		return error;
26102ad94ae6SAl Viro 
26111da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
2612463c3197SDave Hansen 	if (IS_ERR(dentry)) {
26131da177e4SLinus Torvalds 		error = PTR_ERR(dentry);
2614463c3197SDave Hansen 		goto out_unlock;
2615463c3197SDave Hansen 	}
26164ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2617ce3b0f8dSAl Viro 		mode &= ~current_umask();
2618463c3197SDave Hansen 	error = may_mknod(mode);
2619463c3197SDave Hansen 	if (error)
2620463c3197SDave Hansen 		goto out_dput;
2621463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2622463c3197SDave Hansen 	if (error)
2623463c3197SDave Hansen 		goto out_dput;
2624be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd.path, dentry, mode, dev);
2625be6d3e56SKentaro Takeda 	if (error)
2626be6d3e56SKentaro Takeda 		goto out_drop_write;
26271da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
26281da177e4SLinus Torvalds 		case 0: case S_IFREG:
26294ac91378SJan Blunck 			error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
26301da177e4SLinus Torvalds 			break;
26311da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
26324ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
26331da177e4SLinus Torvalds 					new_decode_dev(dev));
26341da177e4SLinus Torvalds 			break;
26351da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
26364ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
26371da177e4SLinus Torvalds 			break;
26381da177e4SLinus Torvalds 	}
2639be6d3e56SKentaro Takeda out_drop_write:
2640463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2641463c3197SDave Hansen out_dput:
26421da177e4SLinus Torvalds 	dput(dentry);
2643463c3197SDave Hansen out_unlock:
26444ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
26451d957f9bSJan Blunck 	path_put(&nd.path);
26461da177e4SLinus Torvalds 	putname(tmp);
26471da177e4SLinus Torvalds 
26481da177e4SLinus Torvalds 	return error;
26491da177e4SLinus Torvalds }
26501da177e4SLinus Torvalds 
26513480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
26525590ff0dSUlrich Drepper {
26535590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
26545590ff0dSUlrich Drepper }
26555590ff0dSUlrich Drepper 
26561da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
26571da177e4SLinus Torvalds {
2658a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
26591da177e4SLinus Torvalds 
26601da177e4SLinus Torvalds 	if (error)
26611da177e4SLinus Torvalds 		return error;
26621da177e4SLinus Torvalds 
2663acfa4380SAl Viro 	if (!dir->i_op->mkdir)
26641da177e4SLinus Torvalds 		return -EPERM;
26651da177e4SLinus Torvalds 
26661da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
26671da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
26681da177e4SLinus Torvalds 	if (error)
26691da177e4SLinus Torvalds 		return error;
26701da177e4SLinus Torvalds 
26711da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2672a74574aaSStephen Smalley 	if (!error)
2673f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
26741da177e4SLinus Torvalds 	return error;
26751da177e4SLinus Torvalds }
26761da177e4SLinus Torvalds 
26772e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
26781da177e4SLinus Torvalds {
26791da177e4SLinus Torvalds 	int error = 0;
26801da177e4SLinus Torvalds 	char * tmp;
26816902d925SDave Hansen 	struct dentry *dentry;
26826902d925SDave Hansen 	struct nameidata nd;
26831da177e4SLinus Torvalds 
26842ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &tmp);
26852ad94ae6SAl Viro 	if (error)
26866902d925SDave Hansen 		goto out_err;
26871da177e4SLinus Torvalds 
26881da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 1);
26891da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
26906902d925SDave Hansen 	if (IS_ERR(dentry))
26916902d925SDave Hansen 		goto out_unlock;
26926902d925SDave Hansen 
26934ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2694ce3b0f8dSAl Viro 		mode &= ~current_umask();
2695463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2696463c3197SDave Hansen 	if (error)
2697463c3197SDave Hansen 		goto out_dput;
2698be6d3e56SKentaro Takeda 	error = security_path_mkdir(&nd.path, dentry, mode);
2699be6d3e56SKentaro Takeda 	if (error)
2700be6d3e56SKentaro Takeda 		goto out_drop_write;
27014ac91378SJan Blunck 	error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2702be6d3e56SKentaro Takeda out_drop_write:
2703463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2704463c3197SDave Hansen out_dput:
27051da177e4SLinus Torvalds 	dput(dentry);
27066902d925SDave Hansen out_unlock:
27074ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27081d957f9bSJan Blunck 	path_put(&nd.path);
27091da177e4SLinus Torvalds 	putname(tmp);
27106902d925SDave Hansen out_err:
27111da177e4SLinus Torvalds 	return error;
27121da177e4SLinus Torvalds }
27131da177e4SLinus Torvalds 
27143cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
27155590ff0dSUlrich Drepper {
27165590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
27175590ff0dSUlrich Drepper }
27185590ff0dSUlrich Drepper 
27191da177e4SLinus Torvalds /*
27201da177e4SLinus Torvalds  * We try to drop the dentry early: we should have
27211da177e4SLinus Torvalds  * a usage count of 2 if we're the only user of this
27221da177e4SLinus Torvalds  * dentry, and if that is true (possibly after pruning
27231da177e4SLinus Torvalds  * the dcache), then we drop the dentry now.
27241da177e4SLinus Torvalds  *
27251da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
27261da177e4SLinus Torvalds  * do a
27271da177e4SLinus Torvalds  *
27281da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
27291da177e4SLinus Torvalds  *		return -EBUSY;
27301da177e4SLinus Torvalds  *
27311da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
27321da177e4SLinus Torvalds  * that is still in use by something else..
27331da177e4SLinus Torvalds  */
27341da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
27351da177e4SLinus Torvalds {
27361da177e4SLinus Torvalds 	dget(dentry);
27371da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
27381da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
2739b7ab39f6SNick Piggin 	if (dentry->d_count == 2)
27401da177e4SLinus Torvalds 		__d_drop(dentry);
27411da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
27421da177e4SLinus Torvalds }
27431da177e4SLinus Torvalds 
27441da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
27451da177e4SLinus Torvalds {
27461da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
27471da177e4SLinus Torvalds 
27481da177e4SLinus Torvalds 	if (error)
27491da177e4SLinus Torvalds 		return error;
27501da177e4SLinus Torvalds 
2751acfa4380SAl Viro 	if (!dir->i_op->rmdir)
27521da177e4SLinus Torvalds 		return -EPERM;
27531da177e4SLinus Torvalds 
27541b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
27551da177e4SLinus Torvalds 	dentry_unhash(dentry);
27561da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
27571da177e4SLinus Torvalds 		error = -EBUSY;
27581da177e4SLinus Torvalds 	else {
27591da177e4SLinus Torvalds 		error = security_inode_rmdir(dir, dentry);
27601da177e4SLinus Torvalds 		if (!error) {
27611da177e4SLinus Torvalds 			error = dir->i_op->rmdir(dir, dentry);
2762d83c49f3SAl Viro 			if (!error) {
27631da177e4SLinus Torvalds 				dentry->d_inode->i_flags |= S_DEAD;
2764d83c49f3SAl Viro 				dont_mount(dentry);
2765d83c49f3SAl Viro 			}
27661da177e4SLinus Torvalds 		}
27671da177e4SLinus Torvalds 	}
27681b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
27691da177e4SLinus Torvalds 	if (!error) {
27701da177e4SLinus Torvalds 		d_delete(dentry);
27711da177e4SLinus Torvalds 	}
27721da177e4SLinus Torvalds 	dput(dentry);
27731da177e4SLinus Torvalds 
27741da177e4SLinus Torvalds 	return error;
27751da177e4SLinus Torvalds }
27761da177e4SLinus Torvalds 
27775590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
27781da177e4SLinus Torvalds {
27791da177e4SLinus Torvalds 	int error = 0;
27801da177e4SLinus Torvalds 	char * name;
27811da177e4SLinus Torvalds 	struct dentry *dentry;
27821da177e4SLinus Torvalds 	struct nameidata nd;
27831da177e4SLinus Torvalds 
27842ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
27851da177e4SLinus Torvalds 	if (error)
27862ad94ae6SAl Viro 		return error;
27871da177e4SLinus Torvalds 
27881da177e4SLinus Torvalds 	switch(nd.last_type) {
27891da177e4SLinus Torvalds 	case LAST_DOTDOT:
27901da177e4SLinus Torvalds 		error = -ENOTEMPTY;
27911da177e4SLinus Torvalds 		goto exit1;
27921da177e4SLinus Torvalds 	case LAST_DOT:
27931da177e4SLinus Torvalds 		error = -EINVAL;
27941da177e4SLinus Torvalds 		goto exit1;
27951da177e4SLinus Torvalds 	case LAST_ROOT:
27961da177e4SLinus Torvalds 		error = -EBUSY;
27971da177e4SLinus Torvalds 		goto exit1;
27981da177e4SLinus Torvalds 	}
27990612d9fbSOGAWA Hirofumi 
28000612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
28010612d9fbSOGAWA Hirofumi 
28024ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
280349705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
28041da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
28056902d925SDave Hansen 	if (IS_ERR(dentry))
28066902d925SDave Hansen 		goto exit2;
28070622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
28080622753bSDave Hansen 	if (error)
28090622753bSDave Hansen 		goto exit3;
2810be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2811be6d3e56SKentaro Takeda 	if (error)
2812be6d3e56SKentaro Takeda 		goto exit4;
28134ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2814be6d3e56SKentaro Takeda exit4:
28150622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
28160622753bSDave Hansen exit3:
28171da177e4SLinus Torvalds 	dput(dentry);
28186902d925SDave Hansen exit2:
28194ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
28201da177e4SLinus Torvalds exit1:
28211d957f9bSJan Blunck 	path_put(&nd.path);
28221da177e4SLinus Torvalds 	putname(name);
28231da177e4SLinus Torvalds 	return error;
28241da177e4SLinus Torvalds }
28251da177e4SLinus Torvalds 
28263cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
28275590ff0dSUlrich Drepper {
28285590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
28295590ff0dSUlrich Drepper }
28305590ff0dSUlrich Drepper 
28311da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
28321da177e4SLinus Torvalds {
28331da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
28341da177e4SLinus Torvalds 
28351da177e4SLinus Torvalds 	if (error)
28361da177e4SLinus Torvalds 		return error;
28371da177e4SLinus Torvalds 
2838acfa4380SAl Viro 	if (!dir->i_op->unlink)
28391da177e4SLinus Torvalds 		return -EPERM;
28401da177e4SLinus Torvalds 
28411b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
28421da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
28431da177e4SLinus Torvalds 		error = -EBUSY;
28441da177e4SLinus Torvalds 	else {
28451da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2846bec1052eSAl Viro 		if (!error) {
28471da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2848bec1052eSAl Viro 			if (!error)
2849d83c49f3SAl Viro 				dont_mount(dentry);
2850bec1052eSAl Viro 		}
28511da177e4SLinus Torvalds 	}
28521b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
28531da177e4SLinus Torvalds 
28541da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
28551da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2856ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
28571da177e4SLinus Torvalds 		d_delete(dentry);
28581da177e4SLinus Torvalds 	}
28590eeca283SRobert Love 
28601da177e4SLinus Torvalds 	return error;
28611da177e4SLinus Torvalds }
28621da177e4SLinus Torvalds 
28631da177e4SLinus Torvalds /*
28641da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
28651b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
28661da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
28671da177e4SLinus Torvalds  * while waiting on the I/O.
28681da177e4SLinus Torvalds  */
28695590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
28701da177e4SLinus Torvalds {
28712ad94ae6SAl Viro 	int error;
28721da177e4SLinus Torvalds 	char *name;
28731da177e4SLinus Torvalds 	struct dentry *dentry;
28741da177e4SLinus Torvalds 	struct nameidata nd;
28751da177e4SLinus Torvalds 	struct inode *inode = NULL;
28761da177e4SLinus Torvalds 
28772ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
28781da177e4SLinus Torvalds 	if (error)
28792ad94ae6SAl Viro 		return error;
28802ad94ae6SAl Viro 
28811da177e4SLinus Torvalds 	error = -EISDIR;
28821da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
28831da177e4SLinus Torvalds 		goto exit1;
28840612d9fbSOGAWA Hirofumi 
28850612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
28860612d9fbSOGAWA Hirofumi 
28874ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
288849705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
28891da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
28901da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
28911da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
28921da177e4SLinus Torvalds 		if (nd.last.name[nd.last.len])
28931da177e4SLinus Torvalds 			goto slashes;
28941da177e4SLinus Torvalds 		inode = dentry->d_inode;
28951da177e4SLinus Torvalds 		if (inode)
28967de9c6eeSAl Viro 			ihold(inode);
28970622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
28980622753bSDave Hansen 		if (error)
28990622753bSDave Hansen 			goto exit2;
2900be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2901be6d3e56SKentaro Takeda 		if (error)
2902be6d3e56SKentaro Takeda 			goto exit3;
29034ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2904be6d3e56SKentaro Takeda exit3:
29050622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
29061da177e4SLinus Torvalds 	exit2:
29071da177e4SLinus Torvalds 		dput(dentry);
29081da177e4SLinus Torvalds 	}
29094ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
29101da177e4SLinus Torvalds 	if (inode)
29111da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
29121da177e4SLinus Torvalds exit1:
29131d957f9bSJan Blunck 	path_put(&nd.path);
29141da177e4SLinus Torvalds 	putname(name);
29151da177e4SLinus Torvalds 	return error;
29161da177e4SLinus Torvalds 
29171da177e4SLinus Torvalds slashes:
29181da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
29191da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
29201da177e4SLinus Torvalds 	goto exit2;
29211da177e4SLinus Torvalds }
29221da177e4SLinus Torvalds 
29232e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
29245590ff0dSUlrich Drepper {
29255590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
29265590ff0dSUlrich Drepper 		return -EINVAL;
29275590ff0dSUlrich Drepper 
29285590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
29295590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
29305590ff0dSUlrich Drepper 
29315590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
29325590ff0dSUlrich Drepper }
29335590ff0dSUlrich Drepper 
29343480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
29355590ff0dSUlrich Drepper {
29365590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
29375590ff0dSUlrich Drepper }
29385590ff0dSUlrich Drepper 
2939db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
29401da177e4SLinus Torvalds {
2941a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
29421da177e4SLinus Torvalds 
29431da177e4SLinus Torvalds 	if (error)
29441da177e4SLinus Torvalds 		return error;
29451da177e4SLinus Torvalds 
2946acfa4380SAl Viro 	if (!dir->i_op->symlink)
29471da177e4SLinus Torvalds 		return -EPERM;
29481da177e4SLinus Torvalds 
29491da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
29501da177e4SLinus Torvalds 	if (error)
29511da177e4SLinus Torvalds 		return error;
29521da177e4SLinus Torvalds 
29531da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2954a74574aaSStephen Smalley 	if (!error)
2955f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
29561da177e4SLinus Torvalds 	return error;
29571da177e4SLinus Torvalds }
29581da177e4SLinus Torvalds 
29592e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
29602e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
29611da177e4SLinus Torvalds {
29622ad94ae6SAl Viro 	int error;
29631da177e4SLinus Torvalds 	char *from;
29641da177e4SLinus Torvalds 	char *to;
29656902d925SDave Hansen 	struct dentry *dentry;
29666902d925SDave Hansen 	struct nameidata nd;
29671da177e4SLinus Torvalds 
29681da177e4SLinus Torvalds 	from = getname(oldname);
29691da177e4SLinus Torvalds 	if (IS_ERR(from))
29701da177e4SLinus Torvalds 		return PTR_ERR(from);
29712ad94ae6SAl Viro 
29722ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
29732ad94ae6SAl Viro 	if (error)
29746902d925SDave Hansen 		goto out_putname;
29751da177e4SLinus Torvalds 
29761da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
29771da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
29786902d925SDave Hansen 	if (IS_ERR(dentry))
29796902d925SDave Hansen 		goto out_unlock;
29806902d925SDave Hansen 
298175c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
298275c3f29dSDave Hansen 	if (error)
298375c3f29dSDave Hansen 		goto out_dput;
2984be6d3e56SKentaro Takeda 	error = security_path_symlink(&nd.path, dentry, from);
2985be6d3e56SKentaro Takeda 	if (error)
2986be6d3e56SKentaro Takeda 		goto out_drop_write;
2987db2e747bSMiklos Szeredi 	error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2988be6d3e56SKentaro Takeda out_drop_write:
298975c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
299075c3f29dSDave Hansen out_dput:
29911da177e4SLinus Torvalds 	dput(dentry);
29926902d925SDave Hansen out_unlock:
29934ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
29941d957f9bSJan Blunck 	path_put(&nd.path);
29951da177e4SLinus Torvalds 	putname(to);
29966902d925SDave Hansen out_putname:
29971da177e4SLinus Torvalds 	putname(from);
29981da177e4SLinus Torvalds 	return error;
29991da177e4SLinus Torvalds }
30001da177e4SLinus Torvalds 
30013480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
30025590ff0dSUlrich Drepper {
30035590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
30045590ff0dSUlrich Drepper }
30055590ff0dSUlrich Drepper 
30061da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
30071da177e4SLinus Torvalds {
30081da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
30091da177e4SLinus Torvalds 	int error;
30101da177e4SLinus Torvalds 
30111da177e4SLinus Torvalds 	if (!inode)
30121da177e4SLinus Torvalds 		return -ENOENT;
30131da177e4SLinus Torvalds 
3014a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
30151da177e4SLinus Torvalds 	if (error)
30161da177e4SLinus Torvalds 		return error;
30171da177e4SLinus Torvalds 
30181da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
30191da177e4SLinus Torvalds 		return -EXDEV;
30201da177e4SLinus Torvalds 
30211da177e4SLinus Torvalds 	/*
30221da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
30231da177e4SLinus Torvalds 	 */
30241da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
30251da177e4SLinus Torvalds 		return -EPERM;
3026acfa4380SAl Viro 	if (!dir->i_op->link)
30271da177e4SLinus Torvalds 		return -EPERM;
30287e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
30291da177e4SLinus Torvalds 		return -EPERM;
30301da177e4SLinus Torvalds 
30311da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
30321da177e4SLinus Torvalds 	if (error)
30331da177e4SLinus Torvalds 		return error;
30341da177e4SLinus Torvalds 
30357e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
30361da177e4SLinus Torvalds 	error = dir->i_op->link(old_dentry, dir, new_dentry);
30377e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
3038e31e14ecSStephen Smalley 	if (!error)
30397e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
30401da177e4SLinus Torvalds 	return error;
30411da177e4SLinus Torvalds }
30421da177e4SLinus Torvalds 
30431da177e4SLinus Torvalds /*
30441da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
30451da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
30461da177e4SLinus Torvalds  * newname.  --KAB
30471da177e4SLinus Torvalds  *
30481da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
30491da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
30501da177e4SLinus Torvalds  * and other special files.  --ADM
30511da177e4SLinus Torvalds  */
30522e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
30532e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
30541da177e4SLinus Torvalds {
30551da177e4SLinus Torvalds 	struct dentry *new_dentry;
30562d8f3038SAl Viro 	struct nameidata nd;
30572d8f3038SAl Viro 	struct path old_path;
30581da177e4SLinus Torvalds 	int error;
30591da177e4SLinus Torvalds 	char *to;
30601da177e4SLinus Torvalds 
306145c9b11aSUlrich Drepper 	if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
3062c04030e1SUlrich Drepper 		return -EINVAL;
3063c04030e1SUlrich Drepper 
30642d8f3038SAl Viro 	error = user_path_at(olddfd, oldname,
306545c9b11aSUlrich Drepper 			     flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
30662d8f3038SAl Viro 			     &old_path);
30671da177e4SLinus Torvalds 	if (error)
30682ad94ae6SAl Viro 		return error;
30692ad94ae6SAl Viro 
30702ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
30711da177e4SLinus Torvalds 	if (error)
30721da177e4SLinus Torvalds 		goto out;
30731da177e4SLinus Torvalds 	error = -EXDEV;
30742d8f3038SAl Viro 	if (old_path.mnt != nd.path.mnt)
30751da177e4SLinus Torvalds 		goto out_release;
30761da177e4SLinus Torvalds 	new_dentry = lookup_create(&nd, 0);
30771da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
30786902d925SDave Hansen 	if (IS_ERR(new_dentry))
30796902d925SDave Hansen 		goto out_unlock;
308075c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
308175c3f29dSDave Hansen 	if (error)
308275c3f29dSDave Hansen 		goto out_dput;
3083be6d3e56SKentaro Takeda 	error = security_path_link(old_path.dentry, &nd.path, new_dentry);
3084be6d3e56SKentaro Takeda 	if (error)
3085be6d3e56SKentaro Takeda 		goto out_drop_write;
30862d8f3038SAl Viro 	error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
3087be6d3e56SKentaro Takeda out_drop_write:
308875c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
308975c3f29dSDave Hansen out_dput:
30901da177e4SLinus Torvalds 	dput(new_dentry);
30916902d925SDave Hansen out_unlock:
30924ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
30931da177e4SLinus Torvalds out_release:
30941d957f9bSJan Blunck 	path_put(&nd.path);
30952ad94ae6SAl Viro 	putname(to);
30961da177e4SLinus Torvalds out:
30972d8f3038SAl Viro 	path_put(&old_path);
30981da177e4SLinus Torvalds 
30991da177e4SLinus Torvalds 	return error;
31001da177e4SLinus Torvalds }
31011da177e4SLinus Torvalds 
31023480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
31035590ff0dSUlrich Drepper {
3104c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
31055590ff0dSUlrich Drepper }
31065590ff0dSUlrich Drepper 
31071da177e4SLinus Torvalds /*
31081da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
31091da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
31101da177e4SLinus Torvalds  * Problems:
31111da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
31121da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
31131da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
3114a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
31151da177e4SLinus Torvalds  *	   story.
31161da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
31171b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
31181da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
31191da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
3120a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
31211da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
31221da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
31231da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
3124a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
31251da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
31261da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
31271da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
31281da177e4SLinus Torvalds  *	d) some filesystems don't support opened-but-unlinked directories,
31291da177e4SLinus Torvalds  *	   either because of layout or because they are not ready to deal with
31301da177e4SLinus Torvalds  *	   all cases correctly. The latter will be fixed (taking this sort of
31311da177e4SLinus Torvalds  *	   stuff into VFS), but the former is not going away. Solution: the same
31321da177e4SLinus Torvalds  *	   trick as in rmdir().
31331da177e4SLinus Torvalds  *	e) conversion from fhandle to dentry may come in the wrong moment - when
31341b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
31351da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3136c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
31371da177e4SLinus Torvalds  *	   locking].
31381da177e4SLinus Torvalds  */
313975c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
31401da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
31411da177e4SLinus Torvalds {
31421da177e4SLinus Torvalds 	int error = 0;
31431da177e4SLinus Torvalds 	struct inode *target;
31441da177e4SLinus Torvalds 
31451da177e4SLinus Torvalds 	/*
31461da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
31471da177e4SLinus Torvalds 	 * we'll need to flip '..'.
31481da177e4SLinus Torvalds 	 */
31491da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3150f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
31511da177e4SLinus Torvalds 		if (error)
31521da177e4SLinus Torvalds 			return error;
31531da177e4SLinus Torvalds 	}
31541da177e4SLinus Torvalds 
31551da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
31561da177e4SLinus Torvalds 	if (error)
31571da177e4SLinus Torvalds 		return error;
31581da177e4SLinus Torvalds 
31591da177e4SLinus Torvalds 	target = new_dentry->d_inode;
3160d83c49f3SAl Viro 	if (target)
31611b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
31621da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
31631da177e4SLinus Torvalds 		error = -EBUSY;
3164d83c49f3SAl Viro 	else {
3165d83c49f3SAl Viro 		if (target)
3166d83c49f3SAl Viro 			dentry_unhash(new_dentry);
31671da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3168d83c49f3SAl Viro 	}
31691da177e4SLinus Torvalds 	if (target) {
3170d83c49f3SAl Viro 		if (!error) {
31711da177e4SLinus Torvalds 			target->i_flags |= S_DEAD;
3172d83c49f3SAl Viro 			dont_mount(new_dentry);
3173d83c49f3SAl Viro 		}
31741b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
31751da177e4SLinus Torvalds 		if (d_unhashed(new_dentry))
31761da177e4SLinus Torvalds 			d_rehash(new_dentry);
31771da177e4SLinus Torvalds 		dput(new_dentry);
31781da177e4SLinus Torvalds 	}
3179e31e14ecSStephen Smalley 	if (!error)
3180349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
31811da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
31821da177e4SLinus Torvalds 	return error;
31831da177e4SLinus Torvalds }
31841da177e4SLinus Torvalds 
318575c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
31861da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
31871da177e4SLinus Torvalds {
31881da177e4SLinus Torvalds 	struct inode *target;
31891da177e4SLinus Torvalds 	int error;
31901da177e4SLinus Torvalds 
31911da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
31921da177e4SLinus Torvalds 	if (error)
31931da177e4SLinus Torvalds 		return error;
31941da177e4SLinus Torvalds 
31951da177e4SLinus Torvalds 	dget(new_dentry);
31961da177e4SLinus Torvalds 	target = new_dentry->d_inode;
31971da177e4SLinus Torvalds 	if (target)
31981b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
31991da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
32001da177e4SLinus Torvalds 		error = -EBUSY;
32011da177e4SLinus Torvalds 	else
32021da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
32031da177e4SLinus Torvalds 	if (!error) {
3204bec1052eSAl Viro 		if (target)
3205d83c49f3SAl Viro 			dont_mount(new_dentry);
3206349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
32071da177e4SLinus Torvalds 			d_move(old_dentry, new_dentry);
32081da177e4SLinus Torvalds 	}
32091da177e4SLinus Torvalds 	if (target)
32101b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
32111da177e4SLinus Torvalds 	dput(new_dentry);
32121da177e4SLinus Torvalds 	return error;
32131da177e4SLinus Torvalds }
32141da177e4SLinus Torvalds 
32151da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
32161da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
32171da177e4SLinus Torvalds {
32181da177e4SLinus Torvalds 	int error;
32191da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
322059b0df21SEric Paris 	const unsigned char *old_name;
32211da177e4SLinus Torvalds 
32221da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
32231da177e4SLinus Torvalds  		return 0;
32241da177e4SLinus Torvalds 
32251da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
32261da177e4SLinus Torvalds 	if (error)
32271da177e4SLinus Torvalds 		return error;
32281da177e4SLinus Torvalds 
32291da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3230a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
32311da177e4SLinus Torvalds 	else
32321da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
32331da177e4SLinus Torvalds 	if (error)
32341da177e4SLinus Torvalds 		return error;
32351da177e4SLinus Torvalds 
3236acfa4380SAl Viro 	if (!old_dir->i_op->rename)
32371da177e4SLinus Torvalds 		return -EPERM;
32381da177e4SLinus Torvalds 
32390eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
32400eeca283SRobert Love 
32411da177e4SLinus Torvalds 	if (is_dir)
32421da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
32431da177e4SLinus Torvalds 	else
32441da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3245123df294SAl Viro 	if (!error)
3246123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
32475a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
32480eeca283SRobert Love 	fsnotify_oldname_free(old_name);
32490eeca283SRobert Love 
32501da177e4SLinus Torvalds 	return error;
32511da177e4SLinus Torvalds }
32521da177e4SLinus Torvalds 
32532e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
32542e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
32551da177e4SLinus Torvalds {
32561da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
32571da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
32581da177e4SLinus Torvalds 	struct dentry *trap;
32591da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
32602ad94ae6SAl Viro 	char *from;
32612ad94ae6SAl Viro 	char *to;
32622ad94ae6SAl Viro 	int error;
32631da177e4SLinus Torvalds 
32642ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
32651da177e4SLinus Torvalds 	if (error)
32661da177e4SLinus Torvalds 		goto exit;
32671da177e4SLinus Torvalds 
32682ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
32691da177e4SLinus Torvalds 	if (error)
32701da177e4SLinus Torvalds 		goto exit1;
32711da177e4SLinus Torvalds 
32721da177e4SLinus Torvalds 	error = -EXDEV;
32734ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
32741da177e4SLinus Torvalds 		goto exit2;
32751da177e4SLinus Torvalds 
32764ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
32771da177e4SLinus Torvalds 	error = -EBUSY;
32781da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
32791da177e4SLinus Torvalds 		goto exit2;
32801da177e4SLinus Torvalds 
32814ac91378SJan Blunck 	new_dir = newnd.path.dentry;
32821da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
32831da177e4SLinus Torvalds 		goto exit2;
32841da177e4SLinus Torvalds 
32850612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
32860612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
32874e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
32880612d9fbSOGAWA Hirofumi 
32891da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
32901da177e4SLinus Torvalds 
329149705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
32921da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
32931da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
32941da177e4SLinus Torvalds 		goto exit3;
32951da177e4SLinus Torvalds 	/* source must exist */
32961da177e4SLinus Torvalds 	error = -ENOENT;
32971da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
32981da177e4SLinus Torvalds 		goto exit4;
32991da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
33001da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
33011da177e4SLinus Torvalds 		error = -ENOTDIR;
33021da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
33031da177e4SLinus Torvalds 			goto exit4;
33041da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
33051da177e4SLinus Torvalds 			goto exit4;
33061da177e4SLinus Torvalds 	}
33071da177e4SLinus Torvalds 	/* source should not be ancestor of target */
33081da177e4SLinus Torvalds 	error = -EINVAL;
33091da177e4SLinus Torvalds 	if (old_dentry == trap)
33101da177e4SLinus Torvalds 		goto exit4;
331149705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
33121da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
33131da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
33141da177e4SLinus Torvalds 		goto exit4;
33151da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
33161da177e4SLinus Torvalds 	error = -ENOTEMPTY;
33171da177e4SLinus Torvalds 	if (new_dentry == trap)
33181da177e4SLinus Torvalds 		goto exit5;
33191da177e4SLinus Torvalds 
33209079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
33219079b1ebSDave Hansen 	if (error)
33229079b1ebSDave Hansen 		goto exit5;
3323be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3324be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3325be6d3e56SKentaro Takeda 	if (error)
3326be6d3e56SKentaro Takeda 		goto exit6;
33271da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
33281da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3329be6d3e56SKentaro Takeda exit6:
33309079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
33311da177e4SLinus Torvalds exit5:
33321da177e4SLinus Torvalds 	dput(new_dentry);
33331da177e4SLinus Torvalds exit4:
33341da177e4SLinus Torvalds 	dput(old_dentry);
33351da177e4SLinus Torvalds exit3:
33361da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
33371da177e4SLinus Torvalds exit2:
33381d957f9bSJan Blunck 	path_put(&newnd.path);
33392ad94ae6SAl Viro 	putname(to);
33401da177e4SLinus Torvalds exit1:
33411d957f9bSJan Blunck 	path_put(&oldnd.path);
33421da177e4SLinus Torvalds 	putname(from);
33432ad94ae6SAl Viro exit:
33441da177e4SLinus Torvalds 	return error;
33451da177e4SLinus Torvalds }
33461da177e4SLinus Torvalds 
3347a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
33485590ff0dSUlrich Drepper {
33495590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
33505590ff0dSUlrich Drepper }
33515590ff0dSUlrich Drepper 
33521da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
33531da177e4SLinus Torvalds {
33541da177e4SLinus Torvalds 	int len;
33551da177e4SLinus Torvalds 
33561da177e4SLinus Torvalds 	len = PTR_ERR(link);
33571da177e4SLinus Torvalds 	if (IS_ERR(link))
33581da177e4SLinus Torvalds 		goto out;
33591da177e4SLinus Torvalds 
33601da177e4SLinus Torvalds 	len = strlen(link);
33611da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
33621da177e4SLinus Torvalds 		len = buflen;
33631da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
33641da177e4SLinus Torvalds 		len = -EFAULT;
33651da177e4SLinus Torvalds out:
33661da177e4SLinus Torvalds 	return len;
33671da177e4SLinus Torvalds }
33681da177e4SLinus Torvalds 
33691da177e4SLinus Torvalds /*
33701da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
33711da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
33721da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
33731da177e4SLinus Torvalds  */
33741da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
33751da177e4SLinus Torvalds {
33761da177e4SLinus Torvalds 	struct nameidata nd;
3377cc314eefSLinus Torvalds 	void *cookie;
3378694a1764SMarcin Slusarz 	int res;
3379cc314eefSLinus Torvalds 
33801da177e4SLinus Torvalds 	nd.depth = 0;
3381cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3382694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3383694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3384694a1764SMarcin Slusarz 
3385694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
33861da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3387cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3388694a1764SMarcin Slusarz 	return res;
33891da177e4SLinus Torvalds }
33901da177e4SLinus Torvalds 
33911da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
33921da177e4SLinus Torvalds {
33931da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
33941da177e4SLinus Torvalds }
33951da177e4SLinus Torvalds 
33961da177e4SLinus Torvalds /* get the link contents into pagecache */
33971da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
33981da177e4SLinus Torvalds {
3399ebd09abbSDuane Griffin 	char *kaddr;
34001da177e4SLinus Torvalds 	struct page *page;
34011da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3402090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
34031da177e4SLinus Torvalds 	if (IS_ERR(page))
34046fe6900eSNick Piggin 		return (char*)page;
34051da177e4SLinus Torvalds 	*ppage = page;
3406ebd09abbSDuane Griffin 	kaddr = kmap(page);
3407ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3408ebd09abbSDuane Griffin 	return kaddr;
34091da177e4SLinus Torvalds }
34101da177e4SLinus Torvalds 
34111da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
34121da177e4SLinus Torvalds {
34131da177e4SLinus Torvalds 	struct page *page = NULL;
34141da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
34151da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
34161da177e4SLinus Torvalds 	if (page) {
34171da177e4SLinus Torvalds 		kunmap(page);
34181da177e4SLinus Torvalds 		page_cache_release(page);
34191da177e4SLinus Torvalds 	}
34201da177e4SLinus Torvalds 	return res;
34211da177e4SLinus Torvalds }
34221da177e4SLinus Torvalds 
3423cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
34241da177e4SLinus Torvalds {
3425cc314eefSLinus Torvalds 	struct page *page = NULL;
34261da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3427cc314eefSLinus Torvalds 	return page;
34281da177e4SLinus Torvalds }
34291da177e4SLinus Torvalds 
3430cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
34311da177e4SLinus Torvalds {
3432cc314eefSLinus Torvalds 	struct page *page = cookie;
3433cc314eefSLinus Torvalds 
3434cc314eefSLinus Torvalds 	if (page) {
34351da177e4SLinus Torvalds 		kunmap(page);
34361da177e4SLinus Torvalds 		page_cache_release(page);
34371da177e4SLinus Torvalds 	}
34381da177e4SLinus Torvalds }
34391da177e4SLinus Torvalds 
344054566b2cSNick Piggin /*
344154566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
344254566b2cSNick Piggin  */
344354566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
34441da177e4SLinus Torvalds {
34451da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
34460adb25d2SKirill Korotaev 	struct page *page;
3447afddba49SNick Piggin 	void *fsdata;
3448beb497abSDmitriy Monakhov 	int err;
34491da177e4SLinus Torvalds 	char *kaddr;
345054566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
345154566b2cSNick Piggin 	if (nofs)
345254566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
34531da177e4SLinus Torvalds 
34547e53cac4SNeilBrown retry:
3455afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
345654566b2cSNick Piggin 				flags, &page, &fsdata);
34571da177e4SLinus Torvalds 	if (err)
3458afddba49SNick Piggin 		goto fail;
3459afddba49SNick Piggin 
34601da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
34611da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
34621da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
3463afddba49SNick Piggin 
3464afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3465afddba49SNick Piggin 							page, fsdata);
34661da177e4SLinus Torvalds 	if (err < 0)
34671da177e4SLinus Torvalds 		goto fail;
3468afddba49SNick Piggin 	if (err < len-1)
3469afddba49SNick Piggin 		goto retry;
3470afddba49SNick Piggin 
34711da177e4SLinus Torvalds 	mark_inode_dirty(inode);
34721da177e4SLinus Torvalds 	return 0;
34731da177e4SLinus Torvalds fail:
34741da177e4SLinus Torvalds 	return err;
34751da177e4SLinus Torvalds }
34761da177e4SLinus Torvalds 
34770adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
34780adb25d2SKirill Korotaev {
34790adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
348054566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
34810adb25d2SKirill Korotaev }
34820adb25d2SKirill Korotaev 
348392e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
34841da177e4SLinus Torvalds 	.readlink	= generic_readlink,
34851da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
34861da177e4SLinus Torvalds 	.put_link	= page_put_link,
34871da177e4SLinus Torvalds };
34881da177e4SLinus Torvalds 
34892d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3490cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
34911da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
34921da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
34931da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
34941da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
34951da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
34961da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
34971da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
34981da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
34991da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
35000adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
35011da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
35021da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3503c9c6cac0SAl Viro EXPORT_SYMBOL(kern_path_parent);
3504d1811465SAl Viro EXPORT_SYMBOL(kern_path);
350516f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3506f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
35078c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission);
35081da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
35091da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
35101da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
35111da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
35121da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
35131da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
35141da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
35151da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
35161da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
35171da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
35181da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
35191da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
35201da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
35211da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3522